#!/bin/sh
sed -e << EOF \
'/.*TYPEDEF/s/\(.*\)TYPEDEF \(.*\) \([^ ]*\);$/\1#if defined(__NEED_\3) \&\& !defined(__DEFINED_\3)\\\
\1 \&\& !defined(_\U\3\E)\
\1  typedef \2 \3;\
\1  #define __DEFINED_\3\
\1  #define _\U\3\E\
\1#endif\
/
/.*STRUCT/s/\(.*\)STRUCT * \([^ ]*\) \(.*\);$/\1#if defined(__NEED_struct_\2) \&\& !defined(__DEFINED_struct_\2)\
\1  struct \2 \3;\
\1  #define __DEFINED_struct_\2\
\1#endif\
/
/.*UNION/s/\(.*\)UNION * \([^ ]*\) \(.*\);$/\1#if defined(__NEED_union_\2) \&\& !defined(__DEFINED_union_\2)\
\1  union \2 \3;\
\1  #define __DEFINED_union_\2\
\1#endif\
/'

#if defined(__clang__)
  TYPEDEF __typeof__(sizeof(int)) size_t;
#else
  TYPEDEF unsigned long size_t;
#endif

#if defined(__x86_64__)
  TYPEDEF long ssize_t;
#elif defined(__arm__) || defined(__i386__)
  TYPEDEF int ssize_t;
#else
  #error ssize_t is not defined for this processor
#endif

#if defined(__clang__)
  TYPEDEF __typeof__(((int*)0)-((int*)0)) ptrdiff_t;
#else
  #if defined(__x86_64__) || defined(__arm__) || defined(__i386__)
    TYPEDEF long ptrdiff_t;
  #else
    #error ptrdiff_t is not defined for this processor
  #endif
#endif

TYPEDEF __builtin_va_list va_list;

#ifndef __cplusplus
  #if defined(__WCHAR_TYPE__)
    TYPEDEF __WCHAR_TYPE__ wchar_t;
  #else
    #if defined(__x86_64__)
      TYPEDEF int wchar_t;
    #elif defined(__arm__) || defined(__i386__)
      TYPEDEF unsigned wchar_t;
    #elif defined(__i386__)
      TYPEDEF long wchar_t;
    #else
      #error wchar_t is not defined for this processor
    #endif
  #endif
#endif

#if defined(__WINT_TYPE__)
  TYPEDEF __WINT_TYPE__ wint_t;
#else
  #if defined(__x86_64__) || defined(__arm__)
    TYPEDEF int wint_t;
  #elif defined(__i386__)
    TYPEDEF long wint_t;
  #else
    #error wint_t is not defined for this processor
  #endif
#endif

#if defined(__x86_64__)
  TYPEDEF int wctrans_t;
#elif defined(__arm__) || defined(__i386__)
  TYPEDEF long wctrans_t;
#else
  #error wctrans_t is not defined for this processor
#endif

#if defined(__x86_64__)
  TYPEDEF int wctype_t;
#elif defined(__arm__) || defined(__i386__)
  TYPEDEF long wctype_t;
#else
  #error wctype_t is not defined for this processor
#endif

#if !defined(__int8_t_defined)
  #if defined(__INT8_TYPE__)
    TYPEDEF signed __INT8_TYPE__ int8_t;
    TYPEDEF unsigned __INT8_TYPE__ uint8_t;
  #else
    TYPEDEF signed char int8_t;
    TYPEDEF unsigned char uint8_t;
  #endif
  TYPEDEF int8_t int_fast8_t;
  TYPEDEF uint8_t uint_fast8_t;
  TYPEDEF int8_t int_least8_t;
  TYPEDEF uint8_t uint_least8_t;
#endif

#if !defined(__int16_t_defined)
  #if defined(__INT16_TYPE__)
    TYPEDEF signed __INT16_TYPE__ int16_t;
    TYPEDEF unsigned __INT16_TYPE__ uint16_t;
    TYPEDEF unsigned __INT16_TYPE__ __uint16_t;
  #else
    TYPEDEF signed short int16_t;
    TYPEDEF unsigned short uint16_t;
  #endif
  TYPEDEF int16_t int_fast16_t;
  TYPEDEF uint16_t uint_fast16_t;
  TYPEDEF int16_t int_least16_t;
  TYPEDEF uint16_t uint_least16_t;
#endif

#if !defined(__int32_t_defined)
  #if defined(__INT32_TYPE__)
    TYPEDEF signed __INT32_TYPE__ int32_t;
    TYPEDEF unsigned __INT32_TYPE__ uint32_t;
    TYPEDEF unsigned __INT32_TYPE__ __uint32_t;
  #else
    TYPEDEF signed int int32_t;
    TYPEDEF unsigned int uint32_t;
  #endif
  TYPEDEF int32_t int_fast32_t;
  TYPEDEF uint32_t uint_fast32_t;
  TYPEDEF int32_t int_least32_t;
  TYPEDEF uint32_t uint_least32_t;
#endif

#if !defined(__int64_t_defined)
  #if defined(__INT64_TYPE__)
    TYPEDEF signed __INT64_TYPE__ int64_t;
    TYPEDEF unsigned __INT64_TYPE__ uint64_t;
    TYPEDEF unsigned __INT64_TYPE__ __uint64_t;
  #else
    #if defined(__x86_64__)
      TYPEDEF signed long int64_t;
      TYPEDEF unsigned long uint64_t;
      TYPEDEF unsigned long __uint64_t;
    #elif defined(__arm__) || defined(__i386__)
      TYPEDEF signed long long int64_t;
      TYPEDEF unsigned long long uint64_t;
      TYPEDEF unsigned long long __uint64_t;
    #else
      #error int64_t, uint64_t, and __uint64_t are not defined for this processor
    #endif
  #endif
  TYPEDEF int64_t int_fast64_t;
  TYPEDEF uint64_t uint_fast64_t;
  TYPEDEF int64_t int_least64_t;
  TYPEDEF uint64_t uint_least64_t;
#endif

#if defined(__x86_64__) || defined(__arm__) || defined(__i386__)
  TYPEDEF long intptr_t;
  TYPEDEF unsigned long uintptr_t;
#else
  #error intptr_t and uintptr_t are not defined for this processor
#endif

#if defined(__x86_64__)
  TYPEDEF double float_t;
  TYPEDEF double double_t;
#elif defined(__i386__)
  #if defined(__FLT_EVAL_METHOD__) && __FLT_EVAL_METHOD__ == 0
    TYPEDEF float float_t;
    TYPEDEF double double_t;
  #else
    TYPEDEF long double float_t;
    TYPEDEF long double double_t;
  #endif
#elif defined(__arm__)
  TYPEDEF float float_t;
  TYPEDEF double double_t;
#else
  #error float_t and double_t are not defined for this processor
#endif

TYPEDEF long time_t;

#if defined(__x86_64__)
  TYPEDEF long suseconds_t;
  STRUCT timeval { time_t tv_sec; long tv_usec; };
  STRUCT timespec { time_t tv_sec; long tv_nsec; };
#elif defined(__i386__) || defined(__arm__)
  TYPEDEF int suseconds_t;
  STRUCT timeval { time_t tv_sec; int tv_usec; };
  STRUCT timespec { time_t tv_sec; long tv_nsec; };
#else
  #error suseconds_t is not defined for this processor
#endif

#if defined(__x86_64__)
  TYPEDEF int pid_t;
  TYPEDEF int id_t;
  TYPEDEF unsigned int uid_t;
  TYPEDEF unsigned int gid_t;
  TYPEDEF int key_t;
#elif defined(__i386__) || defined(__arm__)
  TYPEDEF int pid_t;
  TYPEDEF int id_t;
  TYPEDEF int uid_t;
  TYPEDEF int gid_t;
  TYPEDEF int key_t;
#else
  #error pid_t, id_t, uid_t, and gid_t are not defined for this processor
#endif

TYPEDEF struct __pthread * pthread_t;
TYPEDEF int pthread_once_t;
TYPEDEF int pthread_key_t;
TYPEDEF int pthread_spinlock_t;

#if defined(__x86_64__)
  TYPEDEF struct { union { int __i[14]; size_t __s[7]; } __u; } pthread_attr_t;
#elif defined(__i386__) || defined(__arm__)
  TYPEDEF struct { union { int __i[9]; size_t __s[9]; } __u; } pthread_attr_t;
#else
  #error pthread_attr_t is not defined for this processor
#endif

TYPEDEF unsigned pthread_mutexattr_t;
TYPEDEF unsigned pthread_condattr_t;
TYPEDEF unsigned pthread_barrierattr_t;
TYPEDEF struct { unsigned __attr[2]; } pthread_rwlockattr_t;

#if defined(__x86_64__)
  TYPEDEF struct { union { int __i[10]; void *__p[5]; } __u; } pthread_mutex_t;
  TYPEDEF struct { union { int __i[12]; void *__p[6]; } __u; } pthread_cond_t;
  TYPEDEF struct { union { int __i[14]; void *__p[7]; } __u; } pthread_rwlock_t;
  TYPEDEF struct { union { int __i[8]; void *__p[4]; } __u; } pthread_barrier_t;
#elif defined(__i386__) || defined(__arm__)
  TYPEDEF struct { union { int __i[6]; void *__p[6]; } __u; } pthread_mutex_t;
  TYPEDEF struct { union { int __i[12]; void *__p[12]; } __u; } pthread_cond_t;
  TYPEDEF struct { union { int __i[8]; void *__p[8]; } __u; } pthread_rwlock_t;
  TYPEDEF struct { union { int __i[5]; void *__p[5]; } __u; } pthread_barrier_t;
#else
  #error pthread_mutex_t, pthread_cond_t, pthread_rwlock_t, and pthread_barrier_t are not defined for this processor
#endif

#if defined(__x86_64__)
  TYPEDEF long off_t;
#elif defined(__i386__) || defined(__arm__)
  TYPEDEF long long off_t;
#else
  #error off_t is not defined for this processor
#endif

TYPEDEF unsigned int mode_t;

TYPEDEF unsigned long nlink_t;
TYPEDEF unsigned long long ino_t;

#if defined(__x86_64__)
  TYPEDEF unsigned long dev_t;
#elif defined(__i386__) || defined(__arm__)
  TYPEDEF long long dev_t;
#else
  #error dev_t is not defined for this processor
#endif

TYPEDEF long blksize_t;
TYPEDEF long long blkcnt_t;
TYPEDEF unsigned long long fsblkcnt_t;
TYPEDEF unsigned long long fsfilcnt_t;

TYPEDEF void * timer_t;
TYPEDEF int clockid_t;

#if defined(__x86_64__)
  TYPEDEF long clock_t;
#elif defined(__i386__) || defined(__arm__)
  TYPEDEF unsigned long clock_t;
#else
  #error clock_t is not defined for this processor
#endif


TYPEDEF struct { unsigned long __bits[128/sizeof(long)]; } sigset_t;
TYPEDEF struct __siginfo siginfo_t;

TYPEDEF unsigned int socklen_t;
TYPEDEF unsigned short sa_family_t;
TYPEDEF unsigned short in_port_t;
TYPEDEF unsigned int in_addr_t;
STRUCT in_addr { in_addr_t s_addr; };

TYPEDEF struct __FILE_s FILE;

TYPEDEF int nl_item;

TYPEDEF struct __locale * locale_t;

STRUCT iovec { void *iov_base; size_t iov_len; };

EOF
