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

#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 TY569225 TY569225;
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 TY113386[256];
struct TY569225 {
  TGenericSeq Sup;
  int data[SEQ_DECL_SIZE];
};
N_NIMCALL(int, toint_560829)(NU8 domain);
N_NIMCALL(int, toint_560835)(NU8 typ);
N_NIMCALL(int, toint_560841)(NU8 p);
N_NIMCALL(NimStringDesc*, HEX24_8801)(NU64 x);
N_NOINLINE(void, raiseoserror_126009)(NI32 errorcode, NimStringDesc* additionalinfo);
N_NIMCALL(NI32, oslasterror_126034)(void);
N_NIMCALL(NimStringDesc*, cstrToNimstr)(NCSTRING str);
N_NIMCALL(struct timeval, timevalfrommilliseconds_564828)(NI timeout);
N_NIMCALL(void, createfdset_564843)(fd_set* fd, TY569225* s, NI* m);
N_NIMCALL(void, prunesocketset_565046)(TY569225** s, fd_set* fd);
N_NIMCALL(TGenericSeq*, setLengthSeq)(TGenericSeq* seq, NI elemsize, NI newlen);
int osinvalidsocket_560650;
int nativeafinet_560651;
int nativeafinet6_560652;
TNimType NTI560604; /* Domain */
TNimType NTI560606; /* SockType */
TNimType NTI560608; /* Protocol */

N_NIMCALL(int, toint_560829)(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_560835)(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_560841)(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_561004)(NU8 domain, NU8 socktype, NU8 protocol) {
	int result;
	int LOC1;
	int LOC2;
	int LOC3;
	result = 0;
	LOC1 = 0;
	LOC1 = toint_560829(domain);
	LOC2 = 0;
	LOC2 = toint_560835(socktype);
	LOC3 = 0;
	LOC3 = toint_560841(protocol);
	result = socket(LOC1, LOC2, LOC3);
	return result;
}

N_NIMCALL(NI16, ntohs_562007)(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_561818)(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_561214)(int socket, struct sockaddr* name, socklen_t namelen) {
	int result;
	result = 0;
	result = bind(socket, name, namelen);
	return result;
}

N_NIMCALL(struct addrinfo*, getaddrinfo_561408)(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_560829(domain);
	hints.ai_socktype = toint_560835(socktype);
	hints.ai_protocol = toint_560841(protocol);
	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_126034();
		LOC7 = 0;
		LOC7 = (char *)gai_strerror(gairesult);
		LOC8 = 0;
		LOC8 = cstrToNimstr(LOC7);
		raiseoserror_126009(LOC6, LOC8);
	}
	LA4: ;
	return result;
}

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

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

N_NIMCALL(struct timeval, timevalfrommilliseconds_564828)(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_564843)(fd_set* fd, TY569225* s, NI* m) {
	FD_ZERO(fd);
	{
		int i_565011;
		NI i_565028;
		NI L_565030;
		i_565011 = 0;
		i_565028 = ((NI) 0);
		L_565030 = (s ? s->Sup.len : 0);
		{
			while (1) {
				if (!(i_565028 < L_565030)) goto LA3;
				i_565011 = s->data[i_565028];
				(*m) = (((*m) >= ((NI) (i_565011))) ? (*m) : ((NI) (i_565011)));
				FD_SET(i_565011, fd);
				i_565028 += ((NI) 1);
			} LA3: ;
		}
	}
}

N_NIMCALL(void, prunesocketset_565046)(TY569225** 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) = (TY569225*) setLengthSeq(&((*s))->Sup, sizeof(int), ((NI) (L)));
}

N_NIMCALL(NI, select_565113)(TY569225** readfds, NI timeout) {
	NI result;
	struct timeval tv;
	fd_set rd;
	NI m;
	result = 0;
	tv = timevalfrommilliseconds_564828(timeout);
	memset((void*)(&rd), 0, sizeof(rd));
	m = ((NI) 0);
	createfdset_564843((&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_565046(readfds, (&rd));
	return result;
}

N_NIMCALL(void, close_561028)(int socket) {
	int LOC1;
	LOC1 = 0;
	LOC1 = close(socket);
}
NIM_EXTERNC N_NOINLINE(void, stdlib_nativesocketsInit000)(void) {
	osinvalidsocket_560650 = ((int) -1);
	nativeafinet_560651 = AF_INET;
	nativeafinet6_560652 = AF_INET6;
}

NIM_EXTERNC N_NOINLINE(void, stdlib_nativesocketsDatInit000)(void) {
static TNimNode* TMP5245[3];
NI TMP5247;
static char* NIM_CONST TMP5246[3] = {
"AF_UNIX", 
"AF_INET", 
"AF_INET6"};
static TNimNode* TMP5248[4];
NI TMP5250;
static char* NIM_CONST TMP5249[4] = {
"SOCK_STREAM", 
"SOCK_DGRAM", 
"SOCK_RAW", 
"SOCK_SEQPACKET"};
static TNimNode* TMP5251[6];
NI TMP5253;
static char* NIM_CONST TMP5252[6] = {
"IPPROTO_TCP", 
"IPPROTO_UDP", 
"IPPROTO_IP", 
"IPPROTO_IPV6", 
"IPPROTO_RAW", 
"IPPROTO_ICMP"};
static TNimNode TMP1154[16];
NTI560604.size = sizeof(NU8);
NTI560604.kind = 14;
NTI560604.base = 0;
NTI560604.flags = 3;
for (TMP5247 = 0; TMP5247 < 3; TMP5247++) {
TMP1154[TMP5247+0].kind = 1;
TMP1154[TMP5247+0].offset = TMP5247;
TMP1154[TMP5247+0].name = TMP5246[TMP5247];
TMP5245[TMP5247] = &TMP1154[TMP5247+0];
}
TMP1154[0].offset = 0;
TMP1154[1].offset = 2;
TMP1154[2].offset = 23;
TMP1154[3].len = 3; TMP1154[3].kind = 2; TMP1154[3].sons = &TMP5245[0];
NTI560604.node = &TMP1154[3];
NTI560604.flags = 1<<2;
NTI560606.size = sizeof(NU8);
NTI560606.kind = 14;
NTI560606.base = 0;
NTI560606.flags = 3;
for (TMP5250 = 0; TMP5250 < 4; TMP5250++) {
TMP1154[TMP5250+4].kind = 1;
TMP1154[TMP5250+4].offset = TMP5250;
TMP1154[TMP5250+4].name = TMP5249[TMP5250];
TMP5248[TMP5250] = &TMP1154[TMP5250+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 = &TMP5248[0];
NTI560606.node = &TMP1154[8];
NTI560606.flags = 1<<2;
NTI560608.size = sizeof(NU8);
NTI560608.kind = 14;
NTI560608.base = 0;
NTI560608.flags = 3;
for (TMP5253 = 0; TMP5253 < 6; TMP5253++) {
TMP1154[TMP5253+9].kind = 1;
TMP1154[TMP5253+9].offset = TMP5253;
TMP1154[TMP5253+9].name = TMP5252[TMP5253];
TMP5251[TMP5253] = &TMP1154[TMP5253+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 = &TMP5251[0];
NTI560608.node = &TMP1154[15];
NTI560608.flags = 1<<2;
}

