// Copyright 2020 The Libc Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package libc // import "modernc.org/libc"

import (
	
	
	
	
	
	
	
	
	
	
	gotime 
	

	
	
	
	

	gonetdb 
	
	
	
	
	

	
	
	
	
	
	
	
	
)

const (
	maxPathLen = 1024
)

// var (
// 	in6_addr_any in.In6_addr
// )

type (
	long  = types.User_long_t
	ulong = types.User_ulong_t
)

// // Keep these outside of the var block otherwise go generate will miss them.
var X__stderrp = Xstdout
var X__stdinp = Xstdin
var X__stdoutp = Xstdout

type file uintptr

func ( file) () int32      { return int32((*stdio.FILE)(unsafe.Pointer()).F_file) }
func ( file) ( int32) { (*stdio.FILE)(unsafe.Pointer()).F_file = int16() }

func ( file) () bool {
	return (*stdio.FILE)(unsafe.Pointer()).F_flags&1 != 0
}

func ( file) () {
	(*stdio.FILE)(unsafe.Pointer()).F_flags |= 1
}

func ( file) ( *TLS) int32 {
	 := Xclose(, .fd())
	Xfree(, uintptr())
	if  < 0 {
		return stdio.EOF
	}

	return 0
}

func ( *TLS,  int32) uintptr {
	 := Xcalloc(, 1, types.Size_t(unsafe.Sizeof(stdio.FILE{})))
	if  == 0 {
		return 0
	}

	file().setFd()
	return 
}

func ( int32,  []byte) (int, error) {
	if  == unistd.STDOUT_FILENO {
		return write()
	}

	if dmesgs {
		dmesg("%v: fd %v: %s", origin(1), , hex.Dump())
	}
	return unix.Write(int(), )
}

func ( *TLS,  float64) int32 { return Xisnan(, ) }
func ( *TLS,  float32) int32 { return Xisnanf(, ) }
func ( *TLS,  float64) int32 { return Xisnan(, ) }

// int fprintf(FILE *stream, const char *format, ...);
func ( *TLS, , ,  uintptr) int32 {
	,  := fwrite(int32((*stdio.FILE)(unsafe.Pointer()).F_file), printf(, ))
	return int32()
}

// int usleep(useconds_t usec);
func ( *TLS,  types.Useconds_t) int32 {
	gotime.Sleep(gotime.Microsecond * gotime.Duration())
	return 0
}

// int futimes(int fd, const struct timeval tv[2]);
func ( *TLS,  int32,  uintptr) int32 {
	var  []unix.Timeval
	if  != 0 {
		 = make([]unix.Timeval, 2)
		[0] = *(*unix.Timeval)(unsafe.Pointer())
		[1] = *(*unix.Timeval)(unsafe.Pointer( + unsafe.Sizeof(unix.Timeval{})))
	}
	if  := unix.Futimes(int(), );  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 0
}

// void srandomdev(void);
func ( *TLS) {
	panic(todo(""))
}

// int gethostuuid(uuid_t id, const struct timespec *wait);
func ( *TLS,  uintptr,  uintptr) int32 {
	if , ,  := unix.Syscall(unix.SYS_GETHOSTUUID, , , 0);  != 0 { // Cannot avoid the syscall here.
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 0
}

// int flock(int fd, int operation);
func ( *TLS, ,  int32) int32 {
	if  := unix.Flock(int(), int());  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 0
}

// int fsctl(const char *,unsigned long,void*,unsigned int);
func ( *TLS,  uintptr,  ulong,  uintptr,  uint32) int32 {
	panic(todo(""))
	// if _, _, err := unix.Syscall6(unix.SYS_FSCTL, path, uintptr(request), data, uintptr(options), 0, 0); err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return 0
}

// int * __error(void);
func ( *TLS) uintptr {
	return .errnop
}

// int isspace(int c);
func ( *TLS,  int32) int32 {
	return __isspace(, )
}

// void __assert_rtn(const char *, const char *, int, const char *)
func ( *TLS, ,  uintptr,  int32,  uintptr) {
	panic(todo(""))
	// fmt.Fprintf(os.Stderr, "assertion failure: %s:%d.%s: %s\n", GoString(file), line, GoString(function), GoString(assertion))
	// os.Stderr.Sync()
	// Xexit(t, 1)
}

// int getrusage(int who, struct rusage *usage);
func ( *TLS,  int32,  uintptr) int32 {
	panic(todo(""))
	// if _, _, err := unix.Syscall(unix.SYS_GETRUSAGE, uintptr(who), usage, 0); err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return 0
}

// char *fgets(char *s, int size, FILE *stream);
func ( *TLS,  uintptr,  int32,  uintptr) uintptr {
	 := int((*stdio.FILE)(unsafe.Pointer()).F_file)
	var  []byte
	 := [1]byte{}
	for ;  > 0; -- {
		,  := unix.Read(, [:])
		if  != 0 {
			 = append(, [0])
			if [0] == '\n' {
				 = append(, 0)
				copy((*RawMem)(unsafe.Pointer())[:len():len()], )
				return 
			}

			continue
		}

		switch {
		case  == 0 &&  == nil && len() == 0:
			return 0
		default:
			panic(todo(""))
		}

		// if err == nil {
		// 	panic("internal error")
		// }

		// if len(b) != 0 {
		// 		b = append(b, 0)
		// 		copy((*RawMem)(unsafe.Pointer(s)[:len(b)]), b)
		// 		return s
		// }

		// t.setErrno(err)
	}
	panic(todo(""))
}

// int lstat(const char *pathname, struct stat *statbuf);
func ( *TLS, ,  uintptr) int32 {
	return Xlstat64(, , )
}

// int stat(const char *pathname, struct stat *statbuf);
func ( *TLS, ,  uintptr) int32 {
	return Xstat64(, , )
}

// int chdir(const char *path);
func ( *TLS,  uintptr) int32 {
	if  := unix.Chdir(GoString());  != nil {
		if dmesgs {
			dmesg("%v: %q: %v FAIL", origin(1), GoString(), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: %q: ok", origin(1), GoString())
	}
	return 0
}

var localtime time.Tm

// struct tm *localtime(const time_t *timep);
func ( *TLS,  uintptr) uintptr {
	 := gotime.Local
	if  := getenv(Environ(), "TZ");  != 0 {
		,  := parseZone(GoString())
		 = gotime.FixedZone(, -)
	}
	 := *(*time.Time_t)(unsafe.Pointer())
	 := gotime.Unix(int64(), 0).In()
	localtime.Ftm_sec = int32(.Second())
	localtime.Ftm_min = int32(.Minute())
	localtime.Ftm_hour = int32(.Hour())
	localtime.Ftm_mday = int32(.Day())
	localtime.Ftm_mon = int32(.Month() - 1)
	localtime.Ftm_year = int32(.Year() - 1900)
	localtime.Ftm_wday = int32(.Weekday())
	localtime.Ftm_yday = int32(.YearDay())
	localtime.Ftm_isdst = Bool32(isTimeDST())
	return uintptr(unsafe.Pointer(&localtime))
}

// struct tm *localtime_r(const time_t *timep, struct tm *result);
func ( *TLS, ,  uintptr) uintptr {
	panic(todo(""))
	// loc := gotime.Local
	// if r := getenv(Environ(), "TZ"); r != 0 {
	// 	zone, off := parseZone(GoString(r))
	// 	loc = gotime.FixedZone(zone, -off)
	// }
	// ut := *(*unix.Time_t)(unsafe.Pointer(timep))
	// t := gotime.Unix(int64(ut), 0).In(loc)
	// (*time.Tm)(unsafe.Pointer(result)).Ftm_sec = int32(t.Second())
	// (*time.Tm)(unsafe.Pointer(result)).Ftm_min = int32(t.Minute())
	// (*time.Tm)(unsafe.Pointer(result)).Ftm_hour = int32(t.Hour())
	// (*time.Tm)(unsafe.Pointer(result)).Ftm_mday = int32(t.Day())
	// (*time.Tm)(unsafe.Pointer(result)).Ftm_mon = int32(t.Month() - 1)
	// (*time.Tm)(unsafe.Pointer(result)).Ftm_year = int32(t.Year() - 1900)
	// (*time.Tm)(unsafe.Pointer(result)).Ftm_wday = int32(t.Weekday())
	// (*time.Tm)(unsafe.Pointer(result)).Ftm_yday = int32(t.YearDay())
	// (*time.Tm)(unsafe.Pointer(result)).Ftm_isdst = Bool32(isTimeDST(t))
	// return result
}

// int open(const char *pathname, int flags, ...);
func ( *TLS,  uintptr,  int32,  uintptr) int32 {
	var  types.Mode_t
	if  != 0 {
		 = *(*types.Mode_t)(unsafe.Pointer())
	}
	,  := unix.Open(GoString(), int(), uint32())
	if  != nil {
		if dmesgs {
			dmesg("%v: %q %#x %#o: %v FAIL", origin(1), GoString(), , , )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: %q flags %#x mode %#o: fd %v", origin(1), GoString(), , , )
	}
	return int32()
}

// off_t lseek(int fd, off_t offset, int whence);
func ( *TLS,  int32,  types.Off_t,  int32) types.Off_t {
	return types.Off_t(Xlseek64(, , , ))
}

func ( int32) string {
	switch  {
	case fcntl.SEEK_CUR:
		return "SEEK_CUR"
	case fcntl.SEEK_END:
		return "SEEK_END"
	case fcntl.SEEK_SET:
		return "SEEK_SET"
	default:
		return fmt.Sprintf("whence(%d)", )
	}
}

var fsyncStatbuf stat.Stat

// int fsync(int fd);
func ( *TLS,  int32) int32 {
	if noFsync {
		// Simulate -DSQLITE_NO_SYNC for sqlite3 testfixture, see function full_sync in sqlite3.c
		return Xfstat(, , uintptr(unsafe.Pointer(&fsyncStatbuf)))
	}

	if  := unix.Fsync(int());  != nil {
		if dmesgs {
			dmesg("%v: %v: %v FAIL", origin(1), , )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: %d: ok", origin(1), )
	}
	return 0
}

// long sysconf(int name);
func ( *TLS,  int32) long {
	switch  {
	case unistd.X_SC_PAGESIZE:
		return long(unix.Getpagesize())
	}

	panic(todo(""))
}

// int close(int fd);
func ( *TLS,  int32) int32 {
	if  := unix.Close(int());  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: %d: ok", origin(1), )
	}
	return 0
}

// char *getcwd(char *buf, size_t size);
func ( *TLS,  uintptr,  types.Size_t) uintptr {
	if ,  := unix.Getcwd((*RawMem)(unsafe.Pointer())[::]);  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return 0
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 
}

// int fstat(int fd, struct stat *statbuf);
func ( *TLS,  int32,  uintptr) int32 {
	return Xfstat64(, , )
}

// int ftruncate(int fd, off_t length);
func ( *TLS,  int32,  types.Off_t) int32 {
	if  := unix.Ftruncate(int(), int64());  != nil {
		if dmesgs {
			dmesg("%v: fd %d: %v FAIL", origin(1), , )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: %d %#x: ok", origin(1), , )
	}
	return 0
}

// int fcntl(int fd, int cmd, ... /* arg */ );
func ( *TLS, ,  int32,  uintptr) int32 {
	return Xfcntl64(, , , )
}

// ssize_t read(int fd, void *buf, size_t count);
func ( *TLS,  int32,  uintptr,  types.Size_t) types.Ssize_t {
	var  int
	var  error
	switch {
	case  == 0:
		,  = unix.Read(int(), nil)
	default:
		,  = unix.Read(int(), (*RawMem)(unsafe.Pointer())[::])
		if dmesgs &&  == nil {
			dmesg("%v: fd %v, count %#x, n %#x\n%s", origin(1), , , , hex.Dump((*RawMem)(unsafe.Pointer())[::]))
		}
	}
	if  != nil {
		if dmesgs {
			dmesg("%v: fd %v, %v FAIL", origin(1), , )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return types.Ssize_t()
}

// ssize_t write(int fd, const void *buf, size_t count);
func ( *TLS,  int32,  uintptr,  types.Size_t) types.Ssize_t {
	var  int
	var  error
	switch {
	case  == 0:
		,  = unix.Write(int(), nil)
	default:
		,  = unix.Write(int(), (*RawMem)(unsafe.Pointer())[::])
		if dmesgs {
			dmesg("%v: fd %v, count %#x\n%s", origin(1), , , hex.Dump((*RawMem)(unsafe.Pointer())[::]))
		}
	}
	if  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return types.Ssize_t()
}

// int fchmod(int fd, mode_t mode);
func ( *TLS,  int32,  types.Mode_t) int32 {
	if  := unix.Fchmod(int(), uint32());  != nil {
		if dmesgs {
			dmesg("%v: %d %#o: %v FAIL", origin(1), , , )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: %d %#o: ok", origin(1), , )
	}
	return 0
}

// int fchown(int fd, uid_t owner, gid_t group);
func ( *TLS,  int32,  types.Uid_t,  types.Gid_t) int32 {
	panic(todo(""))
	// if _, _, err := unix.Syscall(unix.SYS_FCHOWN, uintptr(fd), uintptr(owner), uintptr(group)); err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return 0
}

// uid_t geteuid(void);
func ( *TLS) types.Uid_t {
	 := types.Uid_t(unix.Geteuid())
	if dmesgs {
		dmesg("%v: %v", origin(1), )
	}
	return 
}

// void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
func ( *TLS,  uintptr,  types.Size_t, , ,  int32,  types.Off_t) uintptr {
	// Cannot avoid the syscall here, addr sometimes matter.
	, ,  := unix.Syscall6(unix.SYS_MMAP, , uintptr(), uintptr(), uintptr(), uintptr(), uintptr())
	if  != 0 {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return ^uintptr(0) // (void*)-1
	}

	if dmesgs {
		dmesg("%v: %#x", origin(1), )
	}
	return 
}

// int munmap(void *addr, size_t length);
func ( *TLS,  uintptr,  types.Size_t) int32 {
	if , ,  := unix.Syscall(unix.SYS_MUNMAP, , uintptr(), 0);  != 0 { // Cannot avoid the syscall here, must pair with mmap.
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	return 0
}

// int gettimeofday(struct timeval *tv, struct timezone *tz);
func ( *TLS, ,  uintptr) int32 {
	if  != 0 {
		panic(todo(""))
	}

	var  unix.Timeval
	 := unix.Gettimeofday(&)
	if  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	*(*unix.Timeval)(unsafe.Pointer()) = 
	return 0
}

// int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen);
func ( *TLS, , ,  int32, ,  uintptr) int32 {
	panic(todo(""))
	// if _, _, err := unix.Syscall6(unix.SYS_GETSOCKOPT, uintptr(sockfd), uintptr(level), uintptr(optname), optval, optlen, 0); err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return 0
}

// int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
func ( *TLS, , ,  int32,  uintptr,  socket.Socklen_t) int32 {
	panic(todo(""))
	// if _, _, err := unix.Syscall6(unix.SYS_SETSOCKOPT, uintptr(sockfd), uintptr(level), uintptr(optname), optval, uintptr(optlen), 0); err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return 0
}

// int ioctl(int fd, unsigned long request, ...);
func ( *TLS,  int32,  ulong,  uintptr) int32 {
	panic(todo(""))
	// var argp uintptr
	// if va != 0 {
	// 	argp = VaUintptr(&va)
	// }
	// n, _, err := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(request), argp)
	// if err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return int32(n)
}

// int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
func ( *TLS,  int32, ,  uintptr) int32 {
	if , ,  := unix.Syscall(unix.SYS_GETSOCKNAME, uintptr(), , );  != 0 { // Cannot avoid the syscall here.
		if dmesgs {
			dmesg("%v: fd %v: %v FAIL", origin(1), , )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: fd %v: ok", origin(1), )
	}
	return 0
}

// int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
func ( *TLS,  int32, , , ,  uintptr) int32 {
	,  := unix.Select(
		int(),
		(*unix.FdSet)(unsafe.Pointer()),
		(*unix.FdSet)(unsafe.Pointer()),
		(*unix.FdSet)(unsafe.Pointer()),
		(*unix.Timeval)(unsafe.Pointer()),
	)
	if  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return int32()
}

// int mkfifo(const char *pathname, mode_t mode);
func ( *TLS,  uintptr,  types.Mode_t) int32 {
	panic(todo(""))
	// if err := unix.Mkfifo(GoString(pathname), mode); err != nil {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return 0
}

// mode_t umask(mode_t mask);
func ( *TLS,  types.Mode_t) types.Mode_t {
	return types.Mode_t(unix.Umask(int()))
}

// // int execvp(const char *file, char *const argv[]);
// func Xexecvp(t *TLS, file, argv uintptr) int32 {
// 	if _, _, err := unix.Syscall(unix.SYS_EXECVE, file, argv, Environ()); err != 0 {
// 		t.setErrno(err)
// 		return -1
// 	}
//
// 	return 0
// }

// pid_t (pid_t pid, int *wstatus, int options);
func ( *TLS,  types.Pid_t,  uintptr,  int32) types.Pid_t {
	,  := unix.Wait4(int(), (*unix.WaitStatus)(unsafe.Pointer()), int(), nil)
	if  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return types.Pid_t()
}

// int uname(struct utsname *buf);
func ( *TLS,  uintptr) int32 {
	if  := unix.Uname((*unix.Utsname)(unsafe.Pointer()));  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 0
}

// ssize_t recv(int sockfd, void *buf, size_t len, int flags);
func ( *TLS,  int32,  uintptr,  types.Size_t,  int32) types.Ssize_t {
	panic(todo(""))
	// n, _, err := unix.Syscall6(unix.SYS_RECVFROM, uintptr(sockfd), buf, uintptr(len), uintptr(flags), 0, 0)
	// if err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return types.Ssize_t(n)
}

// ssize_t send(int sockfd, const void *buf, size_t len, int flags);
func ( *TLS,  int32,  uintptr,  types.Size_t,  int32) types.Ssize_t {
	panic(todo(""))
	// n, _, err := unix.Syscall6(unix.SYS_SENDTO, uintptr(sockfd), buf, uintptr(len), uintptr(flags), 0, 0)
	// if err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return types.Ssize_t(n)
}

// int shutdown(int sockfd, int how);
func ( *TLS, ,  int32) int32 {
	panic(todo(""))
	// if _, _, err := unix.Syscall(unix.SYS_SHUTDOWN, uintptr(sockfd), uintptr(how), 0); err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return 0
}

// int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
func ( *TLS,  int32,  uintptr,  uintptr) int32 {
	panic(todo(""))
	// if _, _, err := unix.Syscall(unix.SYS_GETPEERNAME, uintptr(sockfd), addr, uintptr(addrlen)); err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return 0
}

// int socket(int domain, int type, int protocol);
func ( *TLS, , ,  int32) int32 {
	panic(todo(""))
	// n, _, err := unix.Syscall(unix.SYS_SOCKET, uintptr(domain), uintptr(type1), uintptr(protocol))
	// if err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return int32(n)
}

// int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
func ( *TLS,  int32,  uintptr,  uint32) int32 {
	panic(todo(""))
	// n, _, err := unix.Syscall(unix.SYS_BIND, uintptr(sockfd), addr, uintptr(addrlen))
	// if err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return int32(n)
}

// int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
func ( *TLS,  int32,  uintptr,  uint32) int32 {
	panic(todo(""))
	// if _, _, err := unix.Syscall(unix.SYS_CONNECT, uintptr(sockfd), addr, uintptr(addrlen)); err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return 0
}

// int listen(int sockfd, int backlog);
func ( *TLS, ,  int32) int32 {
	panic(todo(""))
	// if _, _, err := unix.Syscall(unix.SYS_LISTEN, uintptr(sockfd), uintptr(backlog), 0); err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return 0
}

// int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
func ( *TLS,  int32,  uintptr,  uintptr) int32 {
	panic(todo(""))
	// n, _, err := unix.Syscall6(unix.SYS_ACCEPT4, uintptr(sockfd), addr, uintptr(addrlen), 0, 0, 0)
	// if err != 0 {
	// 	t.setErrno(err)
	// 	return -1
	// }

	// return int32(n)
}

// // int getrlimit(int resource, struct rlimit *rlim);
// func Xgetrlimit(t *TLS, resource int32, rlim uintptr) int32 {
// 	return Xgetrlimit64(t, resource, rlim)
// }
//
// // int setrlimit(int resource, const struct rlimit *rlim);
// func Xsetrlimit(t *TLS, resource int32, rlim uintptr) int32 {
// 	return Xsetrlimit64(t, resource, rlim)
// }
//
// // int setrlimit(int resource, const struct rlimit *rlim);
// func Xsetrlimit64(t *TLS, resource int32, rlim uintptr) int32 {
// 	if _, _, err := unix.Syscall(unix.SYS_SETRLIMIT, uintptr(resource), uintptr(rlim), 0); err != 0 {
// 		t.setErrno(err)
// 		return -1
// 	}
//
// 	return 0
// }

// uid_t getuid(void);
func ( *TLS) types.Uid_t {
	 := types.Uid_t(os.Getuid())
	if dmesgs {
		dmesg("%v: %v", origin(1), )
	}
	return 
}

// pid_t getpid(void);
func ( *TLS) int32 {
	 := int32(os.Getpid())
	if dmesgs {
		dmesg("%v: %v", origin(1), )
	}
	return 
}

// int system(const char *command);
func ( *TLS,  uintptr) int32 {
	 := GoString()
	if  == 0 {
		panic(todo(""))
	}

	 := exec.Command("sh", "-c", )
	.Stdout = os.Stdout
	.Stderr = os.Stderr
	 := .Run()
	if  != nil {
		 := .(*exec.ExitError)
		return int32(.ExitCode())
	}

	return 0
}

var staticGetpwuid pwd.Passwd

func () {
	atExit = append(atExit, func() { closePasswd(&staticGetpwuid) })
}

func ( *pwd.Passwd) {
	Xfree(nil, .Fpw_name)
	Xfree(nil, .Fpw_passwd)
	Xfree(nil, .Fpw_gecos)
	Xfree(nil, .Fpw_dir)
	Xfree(nil, .Fpw_shell)
	* = pwd.Passwd{}
}

// struct passwd *getpwuid(uid_t uid);
func ( *TLS,  uint32) uintptr {
	,  := os.Open("/etc/passwd")
	if  != nil {
		if dmesgs {
			dmesg("%v: %v", origin(1), )
		}
		panic(todo("", ))
	}

	defer .Close()

	 := strconv.Itoa(int())
	 := bufio.NewScanner()
	for .Scan() {
		 := strings.TrimSpace(.Text())
		if len() == 0 || strings.HasPrefix(, "#") {
			continue
		}

		// eg. "root:x:0:0:root:/root:/bin/bash"
		 := strings.Split(.Text(), ":")
		if len() < 7 {
			panic(todo("%q", .Text()))
		}

		if [2] ==  {
			,  := strconv.Atoi([2])
			if  != nil {
				panic(todo(""))
			}

			,  := strconv.Atoi([3])
			if  != nil {
				panic(todo(""))
			}

			closePasswd(&staticGetpwuid)
			 := [4]
			if strings.Contains(, ",") {
				 := strings.Split(, ",")
				 = [0]
			}
			initPasswd(, &staticGetpwuid, [0], [1], uint32(), uint32(), , [5], [6])
			if dmesgs {
				dmesg("%v: ok", origin(1))
			}
			return uintptr(unsafe.Pointer(&staticGetpwuid))
		}
	}

	if .Err() != nil {
		panic(todo(""))
	}

	if dmesgs {
		dmesg("%v: 0", origin(1))
	}
	return 0
}

func ( *TLS,  *pwd.Passwd, ,  string, ,  uint32, , ,  string) {
	.Fpw_name = cString(, )
	.Fpw_passwd = cString(, )
	.Fpw_uid = 
	.Fpw_gid = 
	.Fpw_gecos = cString(, )
	.Fpw_dir = cString(, )
	.Fpw_shell = cString(, )
}

// int setvbuf(FILE *stream, char *buf, int mode, size_t size);
func ( *TLS, ,  uintptr,  int32,  types.Size_t) int32 {
	return 0 //TODO
}

// int raise(int sig);
func ( *TLS,  int32) int32 {
	panic(todo(""))
}

// // int backtrace(void **buffer, int size);
// func Xbacktrace(t *TLS, buf uintptr, size int32) int32 {
// 	panic(todo(""))
// }
//
// // void backtrace_symbols_fd(void *const *buffer, int size, int fd);
// func Xbacktrace_symbols_fd(t *TLS, buffer uintptr, size, fd int32) {
// 	panic(todo(""))
// }

// int fileno(FILE *stream);
func ( *TLS,  uintptr) int32 {
	if  == 0 {
		if dmesgs {
			dmesg("%v: FAIL", origin(1))
		}
		.setErrno(errno.EBADF)
		return -1
	}

	if  := int32((*stdio.FILE)(unsafe.Pointer()).F_file);  >= 0 {
		return 
	}

	if dmesgs {
		dmesg("%v: FAIL", origin(1))
	}
	.setErrno(errno.EBADF)
	return -1
}

// var staticGetpwnam pwd.Passwd
//
// func init() {
// 	atExit = append(atExit, func() { closePasswd(&staticGetpwnam) })
// }

// struct passwd *getpwnam(const char *name);
func ( *TLS,  uintptr) uintptr {
	panic(todo(""))
	// f, err := os.Open("/etc/passwd")
	// if err != nil {
	// 	panic(todo("", err))
	// }

	// defer f.Close()

	// sname := GoString(name)
	// sc := bufio.NewScanner(f)
	// for sc.Scan() {
	// 	// eg. "root:x:0:0:root:/root:/bin/bash"
	// 	a := strings.Split(sc.Text(), ":")
	// 	if len(a) < 7 {
	// 		panic(todo(""))
	// 	}

	// 	if a[0] == sname {
	// 		uid, err := strconv.Atoi(a[2])
	// 		if err != nil {
	// 			panic(todo(""))
	// 		}

	// 		gid, err := strconv.Atoi(a[3])
	// 		if err != nil {
	// 			panic(todo(""))
	// 		}

	// 		closePasswd(&staticGetpwnam)
	// 		gecos := a[4]
	// 		if strings.Contains(gecos, ",") {
	// 			a := strings.Split(gecos, ",")
	// 			gecos = a[0]
	// 		}
	// 		initPasswd(t, &staticGetpwnam, a[0], a[1], uint32(uid), uint32(gid), gecos, a[5], a[6])
	// 		return uintptr(unsafe.Pointer(&staticGetpwnam))
	// 	}
	// }

	// if sc.Err() != nil {
	// 	panic(todo(""))
	// }

	// return 0
}

// var staticGetgrnam grp.Group
//
// func init() {
// 	atExit = append(atExit, func() { closeGroup(&staticGetgrnam) })
// }

// struct group *getgrnam(const char *name);
func ( *TLS,  uintptr) uintptr {
	panic(todo(""))
	// f, err := os.Open("/etc/group")
	// if err != nil {
	// 	panic(todo(""))
	// }

	// defer f.Close()

	// sname := GoString(name)
	// sc := bufio.NewScanner(f)
	// for sc.Scan() {
	// 	// eg. "root:x:0:"
	// 	a := strings.Split(sc.Text(), ":")
	// 	if len(a) < 4 {
	// 		panic(todo(""))
	// 	}

	// 	if a[0] == sname {
	// 		closeGroup(&staticGetgrnam)
	// 		gid, err := strconv.Atoi(a[2])
	// 		if err != nil {
	// 			panic(todo(""))
	// 		}

	// 		var names []string
	// 		if a[3] != "" {
	// 			names = strings.Split(a[3], ",")
	// 		}
	// 		initGroup(t, &staticGetgrnam, a[0], a[1], uint32(gid), names)
	// 		return uintptr(unsafe.Pointer(&staticGetgrnam))
	// 	}
	// }

	// if sc.Err() != nil {
	// 	panic(todo(""))
	// }

	// return 0
}

// func closeGroup(p *grp.Group) {
// 	Xfree(nil, p.Fgr_name)
// 	Xfree(nil, p.Fgr_passwd)
// 	if p.Fgr_mem != 0 {
// 		panic(todo(""))
// 	}
//
// 	*p = grp.Group{}
// }
//
// func initGroup(t *TLS, p *grp.Group, name, pwd string, gid uint32, names []string) {
// 	p.Fgr_name = cString(t, name)
// 	p.Fgr_passwd = cString(t, pwd)
// 	p.Fgr_gid = gid
// 	p.Fgr_mem = 0
// 	if len(names) != 0 {
// 		panic(todo("%q %q %v %q %v", name, pwd, gid, names, len(names)))
// 	}
// }
//
// func init() {
// 	atExit = append(atExit, func() { closeGroup(&staticGetgrgid) })
// }
//
// var staticGetgrgid grp.Group

// struct group *getgrgid(gid_t gid);
func ( *TLS,  uint32) uintptr {
	panic(todo(""))
	// f, err := os.Open("/etc/group")
	// if err != nil {
	// 	panic(todo(""))
	// }

	// defer f.Close()

	// sid := strconv.Itoa(int(gid))
	// sc := bufio.NewScanner(f)
	// for sc.Scan() {
	// 	// eg. "root:x:0:"
	// 	a := strings.Split(sc.Text(), ":")
	// 	if len(a) < 4 {
	// 		panic(todo(""))
	// 	}

	// 	if a[2] == sid {
	// 		closeGroup(&staticGetgrgid)
	// 		var names []string
	// 		if a[3] != "" {
	// 			names = strings.Split(a[3], ",")
	// 		}
	// 		initGroup(t, &staticGetgrgid, a[0], a[1], gid, names)
	// 		return uintptr(unsafe.Pointer(&staticGetgrgid))
	// 	}
	// }

	// if sc.Err() != nil {
	// 	panic(todo(""))
	// }

	// return 0
}

// int mkstemps(char *template, int suffixlen);
func ( *TLS,  uintptr,  int32) int32 {
	panic(todo(""))
}

// int mkstemps(char *template, int suffixlen);
func ( *TLS,  uintptr,  int32) int32 {
	 := uintptr(Xstrlen(, ))
	 :=  + uintptr(-6) - uintptr()
	for  := uintptr(0);  < 6; ++ {
		if *(*byte)(unsafe.Pointer( + )) != 'X' {
			if dmesgs {
				dmesg("%v: FAIL", origin(1))
			}
			.setErrno(errno.EINVAL)
			return -1
		}
	}

	,  := tempFile(, )
	if  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	return int32()
}

// int mkstemp(char *template);
func ( *TLS,  uintptr) int32 {
	return Xmkstemps64(, , 0)
}

func ( *TLS,  int,  string,  *unix.Stat_t,  syscall.Errno) ( *fts.FTSENT) {
	var  uintptr
	if  != nil {
		 = Xmalloc(, types.Size_t(unsafe.Sizeof(unix.Stat_t{})))
		if  == 0 {
			panic("OOM")
		}

		*(*unix.Stat_t)(unsafe.Pointer()) = *
	}
	,  := CString()
	if  != nil {
		panic("OOM")
	}

	return &fts.FTSENT{
		Ffts_info:    uint16(),
		Ffts_path:    ,
		Ffts_pathlen: uint16(len()),
		Ffts_statp:   ,
		Ffts_errno:   int32(),
	}
}

func ( *TLS,  int,  string,  *unix.Stat_t,  syscall.Errno) uintptr {
	 := Xcalloc(, 1, types.Size_t(unsafe.Sizeof(fts.FTSENT{})))
	if  == 0 {
		panic("OOM")
	}

	*(*fts.FTSENT)(unsafe.Pointer()) = *newFtsent(, , , , )
	return 
}

func ( *TLS,  uintptr) {
	Xfree(, (*fts.FTSENT)(unsafe.Pointer()).Ffts_path)
	Xfree(, (*fts.FTSENT)(unsafe.Pointer()).Ffts_statp)
}

type ftstream struct {
	s []uintptr
	x int
}

func ( *ftstream) ( *TLS) {
	for ,  := range .s {
		ftsentClose(, )
		Xfree(, )
	}
	* = ftstream{}
}

// FTS *fts_open(char * const *path_argv, int options, int (*compar)(const FTSENT **, const FTSENT **));
func ( *TLS,  uintptr,  int32,  uintptr) uintptr {
	 := &ftstream{}

	var  func(string)
	 = func( string) {
		var  os.FileInfo
		var  error
		switch {
		case &fts.FTS_LOGICAL != 0:
			,  = os.Stat()
		case &fts.FTS_PHYSICAL != 0:
			,  = os.Lstat()
		default:
			panic(todo(""))
		}

		if  != nil {
			return
		}

		var  *unix.Stat_t
		if &fts.FTS_NOSTAT == 0 {
			var  unix.Stat_t
			switch {
			case &fts.FTS_LOGICAL != 0:
				if  := unix.Stat(, &);  != nil {
					panic(todo(""))
				}
			case &fts.FTS_PHYSICAL != 0:
				if  := unix.Lstat(, &);  != nil {
					panic(todo(""))
				}
			default:
				panic(todo(""))
			}

			 = &
		}

	:
		switch {
		case .IsDir():
			.s = append(.s, newCFtsent(, fts.FTS_D, , , 0))
			,  := os.Open()
			switch x := .(type) {
			case nil:
				// ok
			case *os.PathError:
				.s = append(.s, newCFtsent(, fts.FTS_DNR, , , errno.EACCES))
				break 
			default:
				panic(todo("%q: %v %T", , , ))
			}

			,  := .Readdirnames(-1)
			.Close()
			if  != nil {
				panic(todo(""))
			}

			for ,  := range  {
				( + "/" + )
				if  == nil {
					break 
				}
			}

			.s = append(.s, newCFtsent(, fts.FTS_DP, , , 0))
		default:
			 := fts.FTS_F
			if .Mode()&os.ModeSymlink != 0 {
				 = fts.FTS_SL
			}
			switch {
			case  != nil:
				.s = append(.s, newCFtsent(, , , , 0))
			case &fts.FTS_NOSTAT != 0:
				.s = append(.s, newCFtsent(, fts.FTS_NSOK, , nil, 0))
			default:
				panic(todo(""))
			}
		}
	}

	for {
		 := *(*uintptr)(unsafe.Pointer())
		if  == 0 {
			if  == nil {
				return 0
			}

			if  != 0 {
				panic(todo(""))
			}

			return addObject()
		}

		(GoString())
		 += unsafe.Sizeof(uintptr(0))
	}
}

// FTSENT *fts_read(FTS *ftsp);
func ( *TLS,  uintptr) uintptr {
	 := getObject().(*ftstream)
	if .x == len(.s) {
		if dmesgs {
			dmesg("%v: FAIL", origin(1))
		}
		.setErrno(0)
		return 0
	}

	 := .s[.x]
	if  := (*fts.FTSENT)(unsafe.Pointer()).Ffts_errno;  != 0 {
		if dmesgs {
			dmesg("%v: FAIL", origin(1))
		}
		.setErrno()
	}
	.x++
	return 
}

// int fts_close(FTS *ftsp);
func ( *TLS,  uintptr) int32 {
	getObject().(*ftstream).close()
	removeObject()
	return 0
}

// void tzset (void);
func ( *TLS) {
	//TODO
}

// char *strerror(int errnum);
func ( *TLS,  int32) uintptr {
	panic(todo(""))
}

// void *dlopen(const char *filename, int flags);
func ( *TLS,  uintptr,  int32) uintptr {
	panic(todo(""))
}

// char *dlerror(void);
func ( *TLS) uintptr {
	panic(todo(""))
}

// int dlclose(void *handle);
func ( *TLS,  uintptr) int32 {
	panic(todo(""))
}

// void *dlsym(void *handle, const char *symbol);
func ( *TLS, ,  uintptr) uintptr {
	panic(todo(""))
}

// void perror(const char *s);
func ( *TLS,  uintptr) {
	panic(todo(""))
}

// int pclose(FILE *stream);
func ( *TLS,  uintptr) int32 {
	panic(todo(""))
}

// var gai_strerrorBuf [100]byte

// const char *gai_strerror(int errcode);
func ( *TLS,  int32) uintptr {
	panic(todo(""))
	// copy(gai_strerrorBuf[:], fmt.Sprintf("gai error %d\x00", errcode))
	// return uintptr(unsafe.Pointer(&gai_strerrorBuf))
}

// int tcgetattr(int fd, struct termios *termios_p);
func ( *TLS,  int32,  uintptr) int32 {
	panic(todo(""))
}

// int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);
func ( *TLS, ,  int32,  uintptr) int32 {
	panic(todo(""))
}

// speed_t cfgetospeed(const struct termios *termios_p);
func ( *TLS,  uintptr) termios.Speed_t {
	panic(todo(""))
}

// int cfsetospeed(struct termios *termios_p, speed_t speed);
// func Xcfsetospeed(t *TLS, termios_p uintptr, speed uint32) int32 {
func (...interface{}) int32 {
	panic(todo(""))
}

// int cfsetispeed(struct termios *termios_p, speed_t speed);
// func Xcfsetispeed(t *TLS, termios_p uintptr, speed uint32) int32 {
func (...interface{}) int32 {
	panic(todo(""))
}

// pid_t fork(void);
func ( *TLS) int32 {
	if dmesgs {
		dmesg("%v: FAIL", origin(1))
	}
	.setErrno(errno.ENOSYS)
	return -1
}

// char *setlocale(int category, const char *locale);
func ( *TLS,  int32,  uintptr) uintptr {
	return 0 //TODO
}

// char *nl_langinfo(nl_item item);
func ( *TLS,  langinfo.Nl_item) uintptr {
	panic(todo(""))
}

// FILE *popen(const char *command, const char *type);
func ( *TLS, ,  uintptr) uintptr {
	panic(todo(""))
}

// char *realpath(const char *path, char *resolved_path);
func ( *TLS, ,  uintptr) uintptr {
	,  := filepath.EvalSymlinks(GoString())
	if  != nil {
		if os.IsNotExist() {
			if dmesgs {
				dmesg("%v: %q: %v FAIL", origin(1), GoString(), )
			}
			.setErrno(errno.ENOENT)
			return 0
		}

		panic(todo("", ))
	}

	if  == 0 {
		panic(todo(""))
	}

	if len() >= limits.PATH_MAX {
		 = [:limits.PATH_MAX-1]
	}

	copy((*RawMem)(unsafe.Pointer())[:len():len()], )
	(*RawMem)(unsafe.Pointer())[len()] = 0
	if dmesgs {
		dmesg("%v: %q: ok", origin(1), GoString())
	}
	return 
}

// struct tm *gmtime_r(const time_t *timep, struct tm *result);
func ( *TLS, ,  uintptr) uintptr {
	panic(todo(""))
}

// char *inet_ntoa(struct in_addr in);
func ( *TLS,  in.In_addr) uintptr {
	panic(todo(""))
}

func ( *TLS) uintptr {
	panic(todo(""))
	// return uintptr(unsafe.Pointer(&in6_addr_any))
}

func ( *TLS) {
	if dmesgs {
		dmesg("%v:", origin(1))
	}
	 := Xcalloc(, 1, types.Size_t(unsafe.Sizeof(signal.Sigaction{})))
	if  == 0 {
		panic("OOM")
	}

	(*signal.Sigaction)(unsafe.Pointer()).F__sigaction_u.F__sa_handler = signal.SIG_DFL
	Xsigaction(, signal.SIGABRT, , 0)
	Xfree(, )
	unix.Kill(unix.Getpid(), syscall.Signal(signal.SIGABRT))
	panic(todo("unrechable"))
}

// int fflush(FILE *stream);
func ( *TLS,  uintptr) int32 {
	return 0 //TODO
}

// size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
func ( *TLS,  uintptr, ,  types.Size_t,  uintptr) types.Size_t {
	 := uintptr(file().fd())
	 :=  * 
	var  int
	var  error
	switch {
	case  == 0:
		,  = unix.Read(int(), nil)
	default:
		,  = unix.Read(int(), (*RawMem)(unsafe.Pointer())[::])
		if dmesgs &&  == nil {
			dmesg("%v: fd %v, n %#x\n%s", origin(1), , , hex.Dump((*RawMem)(unsafe.Pointer())[::]))
		}
	}
	if  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		file().setErr()
		return types.Size_t() / 
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return types.Size_t() / 
}

// size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
func ( *TLS,  uintptr, ,  types.Size_t,  uintptr) types.Size_t {
	 := uintptr(file().fd())
	 :=  * 
	var  int
	var  error
	switch {
	case  == 0:
		,  = unix.Write(int(), nil)
	default:
		,  = unix.Write(int(), (*RawMem)(unsafe.Pointer())[::])
		if dmesgs {
			dmesg("%v: fd %v, count %#x\n%s", origin(1), , , hex.Dump((*RawMem)(unsafe.Pointer())[::]))
		}
	}
	if  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		file().setErr()
		return types.Size_t() / 
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return types.Size_t() / 
}

// int fclose(FILE *stream);
func ( *TLS,  uintptr) int32 {
	 := file().close()
	if  != 0 {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return stdio.EOF
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 0
}

// int fputc(int c, FILE *stream);
func ( *TLS,  int32,  uintptr) int32 {
	if ,  := fwrite(file().fd(), []byte{byte()});  != nil {
		return stdio.EOF
	}

	return int32(byte())
}

// int fseek(FILE *stream, long offset, int whence);
func ( *TLS,  uintptr,  long,  int32) int32 {
	if  := Xlseek(, int32(file().fd()), types.Off_t(), );  < 0 {
		if dmesgs {
			dmesg("%v: fd %v, off %#x, whence %v: %v", origin(1), file().fd(), , whenceStr(), )
		}
		file().setErr()
		return -1
	}

	if dmesgs {
		dmesg("%v: fd %v, off %#x, whence %v: ok", origin(1), file().fd(), , whenceStr())
	}
	return 0
}

// long ftell(FILE *stream);
func ( *TLS,  uintptr) long {
	 := Xlseek(, file().fd(), 0, stdio.SEEK_CUR)
	if  < 0 {
		file().setErr()
		return -1
	}

	if dmesgs {
		dmesg("%v: fd %v, n %#x: ok %#x", origin(1), file().fd(), , long())
	}
	return long()
}

// int ferror(FILE *stream);
func ( *TLS,  uintptr) int32 {
	return Bool32(file().err())
}

// int fgetc(FILE *stream);
func ( *TLS,  uintptr) int32 {
	panic(todo(""))
}

// // int getc(FILE *stream);
// func Xgetc(t *TLS, stream uintptr) int32 {
// 	return Xfgetc(t, stream)
// }

// FILE *fdopen(int fd, const char *mode);
func ( *TLS,  int32,  uintptr) uintptr {
	panic(todo(""))
}

// int fputs(const char *s, FILE *stream);
func ( *TLS, ,  uintptr) int32 {
	panic(todo(""))
	// if _, _, err := unix.Syscall(unix.SYS_WRITE, uintptr(file(stream).fd()), s, uintptr(Xstrlen(t, s))); err != 0 {
	// 	return -1
	// }

	// return 0
}

var getservbynameStaticResult netdb.Servent

// struct servent *getservbyname(const char *name, const char *proto);
func ( *TLS, ,  uintptr) uintptr {
	var  *gonetdb.Protoent
	if  != 0 {
		 = gonetdb.GetProtoByName(GoString())
	}
	 := gonetdb.GetServByName(GoString(), )
	if  == nil {
		if dmesgs {
			dmesg("%q %q: nil (protoent %+v)", GoString(), GoString(), )
		}
		return 0
	}

	Xfree(, (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_name)
	if  := (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_aliases;  != 0 {
		for {
			 := *(*uintptr)(unsafe.Pointer())
			if  == 0 {
				break
			}

			Xfree(, )
			 += unsafe.Sizeof(uintptr(0))
		}
		Xfree(, )
	}
	Xfree(, (*netdb.Servent)(unsafe.Pointer(&getservbynameStaticResult)).Fs_proto)
	,  := CString(.Name)
	if  != nil {
		getservbynameStaticResult = netdb.Servent{}
		return 0
	}

	var  uintptr
	if  != nil {
		if ,  = CString(.Name);  != nil {
			Xfree(, )
			getservbynameStaticResult = netdb.Servent{}
			return 0
		}
	}
	var  []uintptr
	for ,  := range .Aliases {
		,  := CString()
		if  != nil {
			for ,  := range  {
				Xfree(, )
			}
			return 0
		}

		 = append(, )
	}
	 := Xcalloc(, types.Size_t(len()+1), types.Size_t(unsafe.Sizeof(uintptr(0))))
	if  == 0 {
		Xfree(, )
		Xfree(, )
		for ,  := range  {
			Xfree(, )
		}
		getservbynameStaticResult = netdb.Servent{}
		return 0
	}
	for ,  := range  {
		*(*uintptr)(unsafe.Pointer()) = 
		 += unsafe.Sizeof(uintptr(0))
	}

	getservbynameStaticResult = netdb.Servent{
		Fs_name:    ,
		Fs_aliases: ,
		Fs_port:    int32(.Port),
		Fs_proto:   ,
	}
	return uintptr(unsafe.Pointer(&getservbynameStaticResult))
}

// //TODO- func Xreaddir64(t *TLS, dir uintptr) uintptr {
// //TODO- 	return Xreaddir(t, dir)
// //TODO- }
//
// func __syscall(r, _ uintptr, errno syscall.Errno) long {
// 	if errno != 0 {
// 		return long(-errno)
// 	}
//
// 	return long(r)
// }

func ( int32) string {
	switch  {
	case fcntl.F_GETOWN:
		return "F_GETOWN"
	case fcntl.F_SETLK:
		return "F_SETLK"
	case fcntl.F_GETLK:
		return "F_GETLK"
	case fcntl.F_SETFD:
		return "F_SETFD"
	case fcntl.F_GETFD:
		return "F_GETFD"
	default:
		return fmt.Sprintf("cmd(%d)", )
	}
}

// // struct __float2 { float __sinval; float __cosval; };
// // struct __double2 { double __sinval; double __cosval; };
// //
// // extern struct __float2 __sincosf_stret(float);
// // extern struct __double2 __sincos_stret(double);
// // extern struct __float2 __sincospif_stret(float);
// // extern struct __double2 __sincospi_stret(double);
//
// type X__float2 struct{ F__sinval, F__cosval float32 }
// type X__double2 struct{ F__sinval, F__cosval float32 }
//
// func X__sincosf_stret(*TLS, float32) X__float2 {
// 	panic(todo(""))
// }
//
// func X__sincos_stret(*TLS, float64) X__double2 {
// 	panic(todo(""))
// }
//
// func X__sincospif_stret(*TLS, float32) X__float2 {
// 	panic(todo(""))
// }
//
// func X__sincospi_stret(*TLS, float64) X__double2 {
// 	panic(todo(""))
// }

// ssize_t pread(int fd, void *buf, size_t count, off_t offset);
func ( *TLS,  int32,  uintptr,  types.Size_t,  types.Off_t) types.Ssize_t {
	var  int
	var  error
	switch {
	case  == 0:
		,  = unix.Pread(int(), nil, int64())
	default:
		,  = unix.Pread(int(), (*RawMem)(unsafe.Pointer())[::], int64())
		if dmesgs &&  == nil {
			dmesg("%v: fd %v, off %#x, count %#x, n %#x\n%s", origin(1), , , , , hex.Dump((*RawMem)(unsafe.Pointer())[::]))
		}
	}
	if  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return types.Ssize_t()
}

// ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
func ( *TLS,  int32,  uintptr,  types.Size_t,  types.Off_t) types.Ssize_t {
	var  int
	var  error
	switch {
	case  == 0:
		,  = unix.Pwrite(int(), nil, int64())
	default:
		,  = unix.Pwrite(int(), (*RawMem)(unsafe.Pointer())[::], int64())
		if dmesgs {
			dmesg("%v: fd %v, off %#x, count %#x\n%s", origin(1), , , , hex.Dump((*RawMem)(unsafe.Pointer())[::]))
		}
	}
	if  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return types.Ssize_t()
}

// char***_NSGetEnviron()
func ( *TLS) uintptr {
	return EnvironP()
}

// int chflags(const char *path, u_int flags);
func ( *TLS,  uintptr,  uint32) int32 {
	if  := unix.Chflags(GoString(), int());  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 0
}

// int rmdir(const char *pathname);
func ( *TLS,  uintptr) int32 {
	if  := unix.Rmdir(GoString());  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 0
}

func (...interface{}) {
	panic(todo(""))
}

func (...interface{}) {
	panic(todo(""))
}

func (...interface{}) int32 {
	panic(todo(""))
}

// uint64_t mach_absolute_time(void);
func ( *TLS) uint64 {
	return uint64(gotime.Now().UnixNano())
}

// See https://developer.apple.com/library/archive/qa/qa1398/_index.html
type machTimebaseInfo = struct {
	Fnumer uint32
	Fdenom uint32
} /* mach_time.h:36:1 */

// kern_return_t mach_timebase_info(mach_timebase_info_t info);
func ( *TLS,  uintptr) int32 {
	*(*machTimebaseInfo)(unsafe.Pointer()) = machTimebaseInfo{Fnumer: 1, Fdenom: 1}
	return 0
}

// int getattrlist(const char* path, struct attrlist * attrList, void * attrBuf, size_t attrBufSize, unsigned long options);
func ( *TLS, , ,  uintptr,  types.Size_t,  uint32) int32 {
	if , ,  := unix.Syscall6(unix.SYS_GETATTRLIST, , , , uintptr(), uintptr(), 0);  != 0 { // Cannot avoid the syscall here.
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 0
}

// int setattrlist(const char* path, struct attrlist * attrList, void * attrBuf, size_t attrBufSize, unsigned long options);
func ( *TLS, , ,  uintptr,  types.Size_t,  uint32) int32 {
	if , ,  := unix.Syscall6(unix.SYS_SETATTRLIST, , , , uintptr(), uintptr(), 0);  != 0 { // Cannot avoid the syscall here.
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	return 0
}

// int copyfile(const char *from, const char *to, copyfile_state_t state, copyfile_flags_t flags);
func (...interface{}) int32 {
	panic(todo(""))
}

// int truncate(const char *path, off_t length);
func (...interface{}) int32 {
	panic(todo(""))
}

type darwinDir struct {
	buf [4096]byte
	fd  int
	h   int
	l   int

	eof bool
}

// DIR *opendir(const char *name);
func ( *TLS,  uintptr) uintptr {
	 := Xmalloc(, uint64(unsafe.Sizeof(darwinDir{})))
	if  == 0 {
		panic("OOM")
	}

	 := int(Xopen(, , fcntl.O_RDONLY|fcntl.O_DIRECTORY|fcntl.O_CLOEXEC, 0))
	if  < 0 {
		if dmesgs {
			dmesg("%v: FAIL %v", origin(1), (*darwinDir)(unsafe.Pointer()).fd)
		}
		Xfree(, )
		// trc("==== opendir: %#x", 0)
		return 0
	}

	if dmesgs {
		dmesg("%v: ok", origin(1))
	}
	(*darwinDir)(unsafe.Pointer()).fd = 
	(*darwinDir)(unsafe.Pointer()).h = 0
	(*darwinDir)(unsafe.Pointer()).l = 0
	(*darwinDir)(unsafe.Pointer()).eof = false
	// trc("==== opendir: %#x", p)
	return 
}

// struct dirent *readdir(DIR *dirp);
func ( *TLS,  uintptr) uintptr {
	if (*darwinDir)(unsafe.Pointer()).eof {
		return 0
	}

	// trc(".... readdir %#x: l %v, h %v", dir, (*darwinDir)(unsafe.Pointer(dir)).l, (*darwinDir)(unsafe.Pointer(dir)).h)
	if (*darwinDir)(unsafe.Pointer()).l == (*darwinDir)(unsafe.Pointer()).h {
		,  := unix.Getdirentries((*darwinDir)(unsafe.Pointer()).fd, (*darwinDir)(unsafe.Pointer()).buf[:], nil)
		// trc("must read: %v %v", n, err)
		if  == 0 {
			if  != nil &&  != io.EOF {
				if dmesgs {
					dmesg("%v: %v FAIL", origin(1), )
				}
				.setErrno()
			}
			(*darwinDir)(unsafe.Pointer()).eof = true
			return 0
		}

		(*darwinDir)(unsafe.Pointer()).l = 0
		(*darwinDir)(unsafe.Pointer()).h = 
		// trc("new l %v, h %v", (*darwinDir)(unsafe.Pointer(dir)).l, (*darwinDir)(unsafe.Pointer(dir)).h)
	}
	 :=  + unsafe.Offsetof(darwinDir{}.buf) + uintptr((*darwinDir)(unsafe.Pointer()).l)
	// trc("dir %#x de %#x", dir, de)
	(*darwinDir)(unsafe.Pointer()).l += int((*unix.Dirent)(unsafe.Pointer()).Reclen)
	// trc("final l %v, h %v", (*darwinDir)(unsafe.Pointer(dir)).l, (*darwinDir)(unsafe.Pointer(dir)).h)
	return 
}

func ( *TLS,  uintptr) int32 {
	// trc("---- closedir: %#x", dir)
	 := Xclose(, int32((*darwinDir)(unsafe.Pointer()).fd))
	Xfree(, )
	return 
}

// int pipe(int pipefd[2]);
func ( *TLS,  uintptr) int32 {
	var  [2]int
	if  := syscall.Pipe([:]);  != nil {
		if dmesgs {
			dmesg("%v: %v FAIL", origin(1), )
		}
		.setErrno()
		return -1
	}

	*(*[2]int32)(unsafe.Pointer()) = [2]int32{int32([0]), int32([1])}
	if dmesgs {
		dmesg("%v: %v ok", origin(1), )
	}
	return 0
}

// int __isoc99_sscanf(const char *str, const char *format, ...);
func ( *TLS, , ,  uintptr) int32 {
	 := scanf(strings.NewReader(GoString()), , )
	// if dmesgs {
	// 	dmesg("%v: %q %q: %d", origin(1), GoString(str), GoString(format), r)
	// }
	return 
}

// int sscanf(const char *str, const char *format, ...);
func ( *TLS, , ,  uintptr) int32 {
	 := scanf(strings.NewReader(GoString()), , )
	// if dmesgs {
	// 	dmesg("%v: %q %q: %d", origin(1), GoString(str), GoString(format), r)
	// }
	return 
}