/* Generated by Nim Compiler v0.12.0 */
/*   (c) 2015 Andreas Rumpf */
/* The generated code is subject to the original license. */
#define NIM_INTBITS 32

#include "nimbase.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <sys/select.h>
#include <unistd.h>
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct TY570225 TY570225;
typedef N_NIMCALL_PTR(void, TY3889) (void* p, NI op);
typedef N_NIMCALL_PTR(void*, TY3894) (void* p);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY3889 marker;
TY3894 deepcopy;
};
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
typedef NIM_CHAR TY114385[256];
struct TY570225 {
  TGenericSeq Sup;
  int data[SEQ_DECL_SIZE];
};
N_NIMCALL(int, toint_561829)(NU8 domain);
N_NIMCALL(int, toint_561835)(NU8 typ);
N_NIMCALL(int, toint_561841)(NU8 p);
N_NIMCALL(NimStringDesc*, HEX24_8801)(NU64 x);
N_NOINLINE(void, raiseoserror_127209)(NI32 errorcode, NimStringDesc* additionalinfo);
N_NIMCALL(NI32, oslasterror_127234)(void);
N_NIMCALL(NimStringDesc*, cstrToNimstr)(NCSTRING str);
N_NIMCALL(struct timeval, timevalfrommilliseconds_565828)(NI timeout);
N_NIMCALL(void, createfdset_565845)(fd_set* fd, TY570225* s, NI* m);
N_NIMCALL(void, prunesocketset_566046)(TY570225** s, fd_set* fd);
N_NIMCALL(TGenericSeq*, setLengthSeq)(TGenericSeq* seq, NI elemsize, NI newlen);
int osinvalidsocket_561653;
int nativeafinet_561654;
int nativeafinet6_561655;
TNimType NTI561607; /* Domain */
TNimType NTI561609; /* SockType */
TNimType NTI561611; /* Protocol */

N_NIMCALL(int, toint_561829)(NU8 domain) {
	int result;
	result = 0;
	switch (domain) {
	case ((NU8) 0):
	{
		result = AF_UNIX;
	}
	break;
	case ((NU8) 2):
	{
		result = AF_INET;
	}
	break;
	case ((NU8) 23):
	{
		result = AF_INET6;
	}
	break;
	default:
	{
	}
	break;
	}
	return result;
}

N_NIMCALL(int, toint_561835)(NU8 typ) {
	int result;
	result = 0;
	switch (typ) {
	case ((NU8) 1):
	{
		result = SOCK_STREAM;
	}
	break;
	case ((NU8) 2):
	{
		result = SOCK_DGRAM;
	}
	break;
	case ((NU8) 5):
	{
		result = SOCK_SEQPACKET;
	}
	break;
	case ((NU8) 3):
	{
		result = SOCK_RAW;
	}
	break;
	default:
	{
	}
	break;
	}
	return result;
}

N_NIMCALL(int, toint_561841)(NU8 p) {
	int result;
	result = 0;
	switch (p) {
	case ((NU8) 6):
	{
		result = IPPROTO_TCP;
	}
	break;
	case ((NU8) 17):
	{
		result = IPPROTO_UDP;
	}
	break;
	case ((NU8) 18):
	{
		result = IPPROTO_IP;
	}
	break;
	case ((NU8) 19):
	{
		result = IPPROTO_IPV6;
	}
	break;
	case ((NU8) 20):
	{
		result = IPPROTO_RAW;
	}
	break;
	case ((NU8) 21):
	{
		result = IPPROTO_ICMP;
	}
	break;
	default:
	{
	}
	break;
	}
	return result;
}

N_NIMCALL(int, newnativesocket_562004)(NU8 domain, NU8 socktype, NU8 protocol) {
	int result;
	int LOC1;
	int LOC2;
	int LOC3;
	result = 0;
	LOC1 = 0;
	LOC1 = toint_561829(domain);
	LOC2 = 0;
	LOC2 = toint_561835(socktype);
	LOC3 = 0;
	LOC3 = toint_561841(protocol);
	result = socket(LOC1, LOC2, LOC3);
	return result;
}

N_NIMCALL(NI16, ntohs_563007)(NI16 x) {
	NI16 result;
	result = 0;
	result = (NI16)((NI16)((NU16)(x) >> (NU16)(((NI16) 8))) | (NI16)((NU16)(x) << (NU16)(((NI16) 8))));
	return result;
}

N_NIMCALL(NI32, ntohl_562818)(NI32 x) {
	NI32 result;
	result = 0;
	result = (NI32)((NI32)((NI32)((NI32)((NU32)(x) >> (NU32)(((NI32) 24))) | (NI32)((NI32)((NU32)(x) >> (NU32)(((NI32) 8))) & ((NI32) 65280))) | (NI32)((NI32)((NU32)(x) << (NU32)(((NI32) 8))) & ((NI32) 16711680))) | (NI32)((NU32)(x) << (NU32)(((NI32) 24))));
	return result;
}

N_NIMCALL(int, bindaddr_562214)(int socket, struct sockaddr* name, socklen_t namelen) {
	int result;
	result = 0;
	result = bind(socket, name, namelen);
	return result;
}

N_NIMCALL(struct addrinfo*, getaddrinfo_562408)(NimStringDesc* address, NU16 port, NU8 domain, NU8 socktype, NU8 protocol) {
	struct addrinfo* result;
	struct addrinfo hints;
	int gairesult;
	NimStringDesc* LOC1;
	result = 0;
	memset((void*)(&hints), 0, sizeof(hints));
	result = NIM_NIL;
	hints.ai_family = toint_561829(domain);
	hints.ai_socktype = toint_561835(socktype);
	hints.ai_protocol = toint_561841(protocol);
	hints.ai_flags = AI_V4MAPPED;
	LOC1 = 0;
	LOC1 = HEX24_8801(port);
	gairesult = getaddrinfo(address->data, LOC1->data, (&hints), &result);
	{
		NI32 LOC6;
		NCSTRING LOC7;
		NimStringDesc* LOC8;
		if (!!((gairesult == ((NI32) 0)))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_127234();
		LOC7 = 0;
		LOC7 = (char *)gai_strerror(gairesult);
		LOC8 = 0;
		LOC8 = cstrToNimstr(LOC7);
		raiseoserror_127209(LOC6, LOC8);
	}
	LA4: ;
	return result;
}

N_NIMCALL(void, dealloc_562808)(struct addrinfo* ai) {
	freeaddrinfo(ai);
}

N_NIMCALL(int, listen_562227)(int socket, int backlog) {
	int result;
	result = 0;
	result = listen(socket, backlog);
	return result;
}

N_NIMCALL(struct timeval, timevalfrommilliseconds_565828)(NI timeout) {
	struct timeval result;
	memset((void*)(&result), 0, sizeof(result));
	{
		NI seconds;
		if (!!((timeout == ((NI) -1)))) goto LA3;
		seconds = (NI)(timeout / ((NI) 1000));
		result.tv_sec = ((NI) (((NI32) (seconds))));
		result.tv_usec = ((NI) (((NI32) ((NI)((NI)(timeout - (NI)(seconds * ((NI) 1000))) * ((NI) 1000))))));
	}
	LA3: ;
	return result;
}

N_NIMCALL(void, createfdset_565845)(fd_set* fd, TY570225* s, NI* m) {
	FD_ZERO(fd);
	{
		int i_566011;
		NI i_566028;
		NI L_566030;
		i_566011 = 0;
		i_566028 = ((NI) 0);
		L_566030 = (s ? s->Sup.len : 0);
		{
			while (1) {
				if (!(i_566028 < L_566030)) goto LA3;
				i_566011 = s->data[i_566028];
				(*m) = (((*m) >= ((NI) (i_566011))) ? (*m) : ((NI) (i_566011)));
				FD_SET(i_566011, fd);
				i_566028 += ((NI) 1);
			} LA3: ;
		}
	}
}

N_NIMCALL(void, prunesocketset_566046)(TY570225** s, fd_set* fd) {
	NI i;
	NI L;
	i = ((NI) 0);
	L = ((*s) ? (*s)->Sup.len : 0);
	{
		while (1) {
			if (!(i < L)) goto LA2;
			{
				int LOC5;
				LOC5 = 0;
				LOC5 = FD_ISSET((*s)->data[i], fd);
				if (!(LOC5 == ((NI32) 0))) goto LA6;
				(*s)->data[i] = (*s)->data[(NI)(L - ((NI) 1))];
				L -= ((NI) 1);
			}
			goto LA3;
			LA6: ;
			{
				i += ((NI) 1);
			}
			LA3: ;
		} LA2: ;
	}
	(*s) = (TY570225*) setLengthSeq(&((*s))->Sup, sizeof(int), ((NI) (L)));
}

N_NIMCALL(NI, select_566113)(TY570225** readfds, NI timeout) {
	NI result;
	struct timeval tv;
	fd_set rd;
	NI m;
	result = 0;
	tv = timevalfrommilliseconds_565828(timeout);
	memset((void*)(&rd), 0, sizeof(rd));
	m = ((NI) 0);
	createfdset_565845((&rd), (*readfds), (&m));
	{
		int LOC5;
		if (!!((timeout == ((NI) -1)))) goto LA3;
		LOC5 = 0;
		LOC5 = select(((int) ((NI)(m + ((NI) 1)))), (&rd), NIM_NIL, NIM_NIL, (&tv));
		result = ((NI) (LOC5));
	}
	goto LA1;
	LA3: ;
	{
		int LOC7;
		LOC7 = 0;
		LOC7 = select(((int) ((NI)(m + ((NI) 1)))), (&rd), NIM_NIL, NIM_NIL, NIM_NIL);
		result = ((NI) (LOC7));
	}
	LA1: ;
	prunesocketset_566046(readfds, (&rd));
	return result;
}

N_NIMCALL(void, close_562028)(int socket) {
	int LOC1;
	LOC1 = 0;
	LOC1 = close(socket);
}
NIM_EXTERNC N_NOINLINE(void, stdlib_nativesocketsInit000)(void) {
	osinvalidsocket_561653 = ((int) -1);
	nativeafinet_561654 = AF_INET;
	nativeafinet6_561655 = AF_INET6;
}

NIM_EXTERNC N_NOINLINE(void, stdlib_nativesocketsDatInit000)(void) {
static TNimNode* TMP5240[3];
NI TMP5242;
static char* NIM_CONST TMP5241[3] = {
"AF_UNIX", 
"AF_INET", 
"AF_INET6"};
static TNimNode* TMP5243[4];
NI TMP5245;
static char* NIM_CONST TMP5244[4] = {
"SOCK_STREAM", 
"SOCK_DGRAM", 
"SOCK_RAW", 
"SOCK_SEQPACKET"};
static TNimNode* TMP5246[6];
NI TMP5248;
static char* NIM_CONST TMP5247[6] = {
"IPPROTO_TCP", 
"IPPROTO_UDP", 
"IPPROTO_IP", 
"IPPROTO_IPV6", 
"IPPROTO_RAW", 
"IPPROTO_ICMP"};
static TNimNode TMP1154[16];
NTI561607.size = sizeof(NU8);
NTI561607.kind = 14;
NTI561607.base = 0;
NTI561607.flags = 3;
for (TMP5242 = 0; TMP5242 < 3; TMP5242++) {
TMP1154[TMP5242+0].kind = 1;
TMP1154[TMP5242+0].offset = TMP5242;
TMP1154[TMP5242+0].name = TMP5241[TMP5242];
TMP5240[TMP5242] = &TMP1154[TMP5242+0];
}
TMP1154[0].offset = 0;
TMP1154[1].offset = 2;
TMP1154[2].offset = 23;
TMP1154[3].len = 3; TMP1154[3].kind = 2; TMP1154[3].sons = &TMP5240[0];
NTI561607.node = &TMP1154[3];
NTI561607.flags = 1<<2;
NTI561609.size = sizeof(NU8);
NTI561609.kind = 14;
NTI561609.base = 0;
NTI561609.flags = 3;
for (TMP5245 = 0; TMP5245 < 4; TMP5245++) {
TMP1154[TMP5245+4].kind = 1;
TMP1154[TMP5245+4].offset = TMP5245;
TMP1154[TMP5245+4].name = TMP5244[TMP5245];
TMP5243[TMP5245] = &TMP1154[TMP5245+4];
}
TMP1154[4].offset = 1;
TMP1154[5].offset = 2;
TMP1154[6].offset = 3;
TMP1154[7].offset = 5;
TMP1154[8].len = 4; TMP1154[8].kind = 2; TMP1154[8].sons = &TMP5243[0];
NTI561609.node = &TMP1154[8];
NTI561609.flags = 1<<2;
NTI561611.size = sizeof(NU8);
NTI561611.kind = 14;
NTI561611.base = 0;
NTI561611.flags = 3;
for (TMP5248 = 0; TMP5248 < 6; TMP5248++) {
TMP1154[TMP5248+9].kind = 1;
TMP1154[TMP5248+9].offset = TMP5248;
TMP1154[TMP5248+9].name = TMP5247[TMP5248];
TMP5246[TMP5248] = &TMP1154[TMP5248+9];
}
TMP1154[9].offset = 6;
TMP1154[10].offset = 17;
TMP1154[11].offset = 18;
TMP1154[12].offset = 19;
TMP1154[13].offset = 20;
TMP1154[14].offset = 21;
TMP1154[15].len = 6; TMP1154[15].kind = 2; TMP1154[15].sons = &TMP5246[0];
NTI561611.node = &TMP1154[15];
NTI561611.flags = 1<<2;
}

