require '_h2ph_pre.ph';

no warnings qw(redefine misc);

unless(defined(&_LIB_LIBRUMPUSER_RUMPUSER_PORT_H_)) {
    eval 'sub _LIB_LIBRUMPUSER_RUMPUSER_PORT_H_ () {1;}' unless defined(&_LIB_LIBRUMPUSER_RUMPUSER_PORT_H_);
    if(!defined(&RUMPUSER_CONFIG)) {
	eval 'sub HAVE_ALIGNED_ALLOC () {1;}' unless defined(&HAVE_ALIGNED_ALLOC);
	eval 'sub HAVE_ARC4RANDOM_BUF () {1;}' unless defined(&HAVE_ARC4RANDOM_BUF);
	eval 'sub HAVE_CHFLAGS () {1;}' unless defined(&HAVE_CHFLAGS);
	eval 'sub HAVE_CLOCKID_T () {1;}' unless defined(&HAVE_CLOCKID_T);
	eval 'sub HAVE_CLOCK_GETTIME () {1;}' unless defined(&HAVE_CLOCK_GETTIME);
	eval 'sub HAVE_CLOCK_NANOSLEEP () {1;}' unless defined(&HAVE_CLOCK_NANOSLEEP);
	eval 'sub HAVE_DLINFO () {1;}' unless defined(&HAVE_DLINFO);
	eval 'sub HAVE_FSYNC_RANGE () {1;}' unless defined(&HAVE_FSYNC_RANGE);
	eval 'sub HAVE_GETENV_R () {1;}' unless defined(&HAVE_GETENV_R);
	eval 'sub HAVE_GETPROGNAME () {1;}' unless defined(&HAVE_GETPROGNAME);
	eval 'sub HAVE_GETSUBOPT () {1;}' unless defined(&HAVE_GETSUBOPT);
	eval 'sub HAVE_INTTYPES_H () {1;}' unless defined(&HAVE_INTTYPES_H);
	eval 'sub HAVE_KQUEUE () {1;}' unless defined(&HAVE_KQUEUE);
	eval 'sub HAVE_MEMORY_H () {1;}' unless defined(&HAVE_MEMORY_H);
	eval 'sub HAVE_PATHS_H () {1;}' unless defined(&HAVE_PATHS_H);
	eval 'sub HAVE_POSIX_MEMALIGN () {1;}' unless defined(&HAVE_POSIX_MEMALIGN);
	eval 'sub HAVE_PTHREAD_SETNAME3 () {1;}' unless defined(&HAVE_PTHREAD_SETNAME3);
	eval 'sub HAVE_SETPROGNAME () {1;}' unless defined(&HAVE_SETPROGNAME);
	eval 'sub HAVE_STDINT_H () {1;}' unless defined(&HAVE_STDINT_H);
	eval 'sub HAVE_STDLIB_H () {1;}' unless defined(&HAVE_STDLIB_H);
	eval 'sub HAVE_STRINGS_H () {1;}' unless defined(&HAVE_STRINGS_H);
	eval 'sub HAVE_STRING_H () {1;}' unless defined(&HAVE_STRING_H);
	eval 'sub HAVE_STRSUFTOLL () {1;}' unless defined(&HAVE_STRSUFTOLL);
	eval 'sub HAVE_STRUCT_SOCKADDR_IN_SIN_LEN () {1;}' unless defined(&HAVE_STRUCT_SOCKADDR_IN_SIN_LEN);
	eval 'sub HAVE_SYS_ATOMIC_H () {1;}' unless defined(&HAVE_SYS_ATOMIC_H);
	eval 'sub HAVE_SYS_CDEFS_H () {1;}' unless defined(&HAVE_SYS_CDEFS_H);
	eval 'sub HAVE_SYS_DISKLABEL_H () {1;}' unless defined(&HAVE_SYS_DISKLABEL_H);
	eval 'sub HAVE_SYS_DISK_H () {1;}' unless defined(&HAVE_SYS_DISK_H);
	eval 'sub HAVE_SYS_DKIO_H () {1;}' unless defined(&HAVE_SYS_DKIO_H);
	eval 'sub HAVE_SYS_PARAM_H () {1;}' unless defined(&HAVE_SYS_PARAM_H);
	eval 'sub HAVE_SYS_STAT_H () {1;}' unless defined(&HAVE_SYS_STAT_H);
	eval 'sub HAVE_SYS_SYSCTL_H () {1;}' unless defined(&HAVE_SYS_SYSCTL_H);
	eval 'sub HAVE_SYS_TYPES_H () {1;}' unless defined(&HAVE_SYS_TYPES_H);
	eval 'sub HAVE_UNISTD_H () {1;}' unless defined(&HAVE_UNISTD_H);
	eval 'sub HAVE_UTIMENSAT () {1;}' unless defined(&HAVE_UTIMENSAT);
	eval 'sub HAVE___QUOTACTL () {1;}' unless defined(&HAVE___QUOTACTL);
	eval 'sub PACKAGE_BUGREPORT () {"https://github.com/rumpkernel/";}' unless defined(&PACKAGE_BUGREPORT);
	eval 'sub PACKAGE_NAME () {"rumpuser-posix";}' unless defined(&PACKAGE_NAME);
	eval 'sub PACKAGE_STRING () {"rumpuser-posix 999";}' unless defined(&PACKAGE_STRING);
	eval 'sub PACKAGE_TARNAME () {"rumpuser-posix";}' unless defined(&PACKAGE_TARNAME);
	eval 'sub PACKAGE_URL () {"";}' unless defined(&PACKAGE_URL);
	eval 'sub PACKAGE_VERSION () {"999";}' unless defined(&PACKAGE_VERSION);
	eval 'sub STDC_HEADERS () {1;}' unless defined(&STDC_HEADERS);
	unless(defined(&_DARWIN_USE_64_BIT_INODE)) {
	    eval 'sub _DARWIN_USE_64_BIT_INODE () {1;}' unless defined(&_DARWIN_USE_64_BIT_INODE);
	}
    } else {
	require './rump/rumpuser_config.ph';
    }
    if(defined(&__linux__) || defined(&__GNU__) || defined(&__GLIBC__)) {
	eval 'sub _GNU_SOURCE () {1;}' unless defined(&_GNU_SOURCE);
    }
    if(defined(&HAVE_SYS_CDEFS_H)) {
	require 'sys/cdefs.ph';
    }
    if(defined(&__FreeBSD__)) {
	if((defined(&__ISO_C_VISIBLE) ? &__ISO_C_VISIBLE : undef) < 2011) {
	    undef(&__ISO_C_VISIBLE) if defined(&__ISO_C_VISIBLE);
	    eval 'sub __ISO_C_VISIBLE () {2011;}' unless defined(&__ISO_C_VISIBLE);
	}
    }
    if(defined(&HAVE_SYS_PARAM_H)) {
	require 'sys/param.ph';
    }
    unless(defined(&MIN)) {
	eval 'sub MIN {
	    my($a,$b) = @_;
    	    eval q((( ($a)<($b))?($a):($b)));
	}' unless defined(&MIN);
    }
    unless(defined(&MAX)) {
	eval 'sub MAX {
	    my($a,$b) = @_;
    	    eval q((( ($a)>($b))?($a):($b)));
	}' unless defined(&MAX);
    }
    if(!defined(&HAVE_GETSUBOPT)) {
	eval 'sub getsubopt {
	    my($optionp,$tokens,$valuep) = @_;
    	    eval q({ -1; });
	}' unless defined(&getsubopt);
    }
    if(!defined(&HAVE_CLOCKID_T)) {
    }
    if(defined(&__ANDROID__)) {
	require 'stdint.ph';
	require 'sys/select.ph';
	eval 'sub atomic_inc_uint {
	    my($x) = @_;
    	    eval q( &__sync_fetch_and_add($x, 1));
	}' unless defined(&atomic_inc_uint);
	eval 'sub atomic_dec_uint {
	    my($x) = @_;
    	    eval q( &__sync_fetch_and_sub($x, 1));
	}' unless defined(&atomic_dec_uint);
    }
    if(defined(&__sun__)) {
	if(defined(&RUMPUSER_NO_FILE_OFFSET_BITS)) {
	    undef(&_FILE_OFFSET_BITS) if defined(&_FILE_OFFSET_BITS);
	    eval 'sub _FILE_OFFSET_BITS () {32;}' unless defined(&_FILE_OFFSET_BITS);
	}
    }
    if(!defined(&HAVE_CLOCK_GETTIME)) {
	require 'sys/time.ph';
	eval 'sub CLOCK_REALTIME () {0;}' unless defined(&CLOCK_REALTIME);
	eval 'sub clock_gettime {
	    my($clk,$ts) = @_;
    	    eval q({ \'struct timeval\'  &tv;  &if ( &gettimeofday( &tv, 0) == 0) {  ($ts->{tv_sec}) =  ($tv->{tv_sec});  ($ts->{tv_nsec}) =  ($tv->{tv_usec}) * 1000; 0; } -1; });
	}' unless defined(&clock_gettime);
    }
    if(defined(&__APPLE__)) {
	require 'libkern/OSAtomic.ph';
	eval 'sub atomic_inc_uint {
	    my($x) = @_;
    	    eval q( &OSAtomicIncrement32(( &volatile  &int32_t *)($x)));
	}' unless defined(&atomic_inc_uint);
	eval 'sub atomic_dec_uint {
	    my($x) = @_;
    	    eval q( &OSAtomicDecrement32(( &volatile  &int32_t *)($x)));
	}' unless defined(&atomic_dec_uint);
    }
    require 'sys/types.ph';
    if(!defined(&HAVE_GETENV_R)) {
	require 'errno.ph';
	require 'stdlib.ph';
	require 'string.ph';
	require 'inttypes.ph';
	eval 'sub getenv_r {
	    my($name,$buf,$buflen) = @_;
    	    eval q({ \'char\' * &tmp;  &if (( &tmp =  &getenv($name)) !=  &NULL) {  &if ( &strlen( &tmp) >= $buflen) {  &errno =  &ERANGE; -1; }  &strcpy($buf,  &tmp); 0; }  &else {  &errno =  &ENOENT; -1; } });
	}' unless defined(&getenv_r);
    }
    if(!defined(&HAVE_POSIX_MEMALIGN)) {
	if(!defined(&HAVE_MEMALIGN)) {
	    die("method\ for\ aligned\ memory\ allocation\ required");
	}
	require 'sys/sysmacros.ph';
	require 'stdlib.ph';
	eval 'sub posix_memalign {
	    my($ptr,$align,$size) = @_;
    	    eval q({ *$ptr =  &memalign($align, $size);  &if (*$ptr ==  &NULL)  &ENOMEM; 0; });
	}' unless defined(&posix_memalign);
    }
    if(defined(&__NetBSD__)) {
	eval 'sub RUMPUSER_LOCKALIGN () { &COHERENCY_UNIT;}' unless defined(&RUMPUSER_LOCKALIGN);
    } else {
	eval 'sub RUMPUSER_LOCKALIGN () {64;}' unless defined(&RUMPUSER_LOCKALIGN);
    }
    if(!defined(&HAVE_ALIGNED_ALLOC)) {
	require 'stdlib.ph';
	eval 'sub aligned_alloc {
	    my($alignment,$size) = @_;
    	    eval q({  &void * &ptr; \'int\'  &rv;  &rv =  &posix_memalign( &ptr, $alignment, $size);  &rv ?  &NULL :  &ptr; });
	}' unless defined(&aligned_alloc);
    }
    unless(defined(&__RCSID)) {
	eval 'sub __RCSID {
	    my($a) = @_;
    	    eval q();
	}' unless defined(&__RCSID);
    }
    require 'poll.ph';
    unless(defined(&INFTIM)) {
	eval 'sub INFTIM () {(-1);}' unless defined(&INFTIM);
    }
    unless(defined(&_DIAGASSERT)) {
	eval 'sub _DIAGASSERT {
	    my($_p_) = @_;
    	    eval q();
	}' unless defined(&_DIAGASSERT);
    }
    if(!defined(&HAVE_STRUCT_SOCKADDR_IN_SIN_LEN)) {
	eval 'sub SIN_SETLEN {
	    my($a,$b) = @_;
    	    eval q();
	}' unless defined(&SIN_SETLEN);
    } else {
	eval 'sub SIN_SETLEN {
	    my($_sin_, $_len_) = @_;
    	    eval q( ($_sin_->{sin_len}) = $_len_);
	}' unless defined(&SIN_SETLEN);
    }
    unless(defined(&__predict_true)) {
	eval 'sub __predict_true {
	    my($a) = @_;
    	    eval q($a);
	}' unless defined(&__predict_true);
	eval 'sub __predict_false {
	    my($a) = @_;
    	    eval q($a);
	}' unless defined(&__predict_false);
    }
    unless(defined(&__dead)) {
	eval 'sub __dead () { &__attribute__(( &__noreturn__));}' unless defined(&__dead);
    }
    unless(defined(&__printflike)) {
	if(defined(&__GNUC__)) {
	    eval 'sub __printflike {
	        my($a,$b) = @_;
    		eval q( &__attribute__(( &__format__ ( &__printf__,$a,$b))));
	    }' unless defined(&__printflike);
	} else {
	    eval 'sub __printflike {
	        my($a,$b) = @_;
    		eval q();
	    }' unless defined(&__printflike);
	}
    }
    unless(defined(&__noinline)) {
	if(defined(&__GNUC__)) {
	    eval 'sub __noinline () { &__attribute__(( &__noinline__));}' unless defined(&__noinline);
	} else {
	    eval 'sub __noinline () {1;}' unless defined(&__noinline);
	}
    }
    unless(defined(&__arraycount)) {
	eval 'sub __arraycount {
	    my($_ar_) = @_;
    	    eval q(($sizeof{$_ar_}/$sizeof{$_ar_->[0]}));
	}' unless defined(&__arraycount);
    }
    unless(defined(&__UNCONST)) {
	eval 'sub __UNCONST {
	    my($_a_) = @_;
    	    eval q((( &void *)( &const  &void *)($_a_)));
	}' unless defined(&__UNCONST);
    }
    unless(defined(&__CONCAT)) {
	eval 'sub __CONCAT {
	    my($x,$y) = @_;
    	    eval q($x  $y);
	}' unless defined(&__CONCAT);
    }
    unless(defined(&__STRING)) {
	eval 'sub __STRING {
	    my($x) = @_;
    	    eval q($x);
	}' unless defined(&__STRING);
    }
    unless(defined(&__NetBSD_Prereq__)) {
	eval 'sub __NetBSD_Prereq__ {
	    my($a,$b,$c) = @_;
    	    eval q(0);
	}' unless defined(&__NetBSD_Prereq__);
    }
    require 'sys/socket.ph';
    if(!defined(&__CMSG_ALIGN)) {
	if(defined(&CMSG_ALIGN)) {
	    eval 'sub __CMSG_ALIGN {
	        my($a) = @_;
    		eval q( &CMSG_ALIGN($a));
	    }' unless defined(&__CMSG_ALIGN);
	}
    }
    unless(defined(&PF_LOCAL)) {
	eval 'sub PF_LOCAL () { &PF_UNIX;}' unless defined(&PF_LOCAL);
    }
    unless(defined(&AF_LOCAL)) {
	eval 'sub AF_LOCAL () { &AF_UNIX;}' unless defined(&AF_LOCAL);
    }
    unless(defined(&MSG_NOSIGNAL)) {
	eval 'sub MSG_NOSIGNAL () {0;}' unless defined(&MSG_NOSIGNAL);
    }
    if(defined(&__NetBSD__)  && defined(&__mips_n32) ) {
	eval 'sub HAVE_REGISTER_T () {1;}' unless defined(&HAVE_REGISTER_T);
	eval 'sub RUMP_REGISTER_T () { &uint64_t;}' unless defined(&RUMP_REGISTER_T);
	eval 'sub PRIxREGISTER () { &PRIx64;}' unless defined(&PRIxREGISTER);
    }
    if(!defined(&HAVE_REGISTER_T)  && !defined(&RUMP_REGISTER_T)) {
	eval 'sub RUMP_REGISTER_T () {\'long\';}' unless defined(&RUMP_REGISTER_T);
	eval 'sub PRIxREGISTER () {"lx";}' unless defined(&PRIxREGISTER);
    }
    require 'sys/time.ph';
    unless(defined(&TIMEVAL_TO_TIMESPEC)) {
	eval 'sub TIMEVAL_TO_TIMESPEC {
	    my($tv, $ts) = @_;
    	    eval q( &do { ($ts)-> &tv_sec = ($tv)-> &tv_sec; ($ts)-> &tv_nsec = ($tv)-> &tv_usec * 1000; }  &while (0));
	}' unless defined(&TIMEVAL_TO_TIMESPEC);
    }
    if(!defined(&HAVE_SETPROGNAME)) {
	eval 'sub setprogname {
	    my($a) = @_;
    	    eval q();
	}' unless defined(&setprogname);
    }
    require 'limits.ph';
    unless(defined(&MAXPATHLEN)) {
	eval 'sub MAXPATHLEN () {4096;}' unless defined(&MAXPATHLEN);
    }
    unless(defined(&PATH_MAX)) {
	eval 'sub PATH_MAX () { &MAXPATHLEN;}' unless defined(&PATH_MAX);
    }
    unless(defined(&MAXHOSTNAMELEN)) {
	eval 'sub MAXHOSTNAMELEN () {256;}' unless defined(&MAXHOSTNAMELEN);
    }
}
1;
