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

#include "nimbase.h"
#include "winsock2.h"
#include <string.h>
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct Addrinfo107850 Addrinfo107850;
typedef struct TY527219 TY527219;
typedef struct Timeval107848 Timeval107848;
typedef struct Tfdset107846 Tfdset107846;
typedef NIM_CHAR TY107856[257];
typedef NIM_CHAR TY107859[129];
typedef N_STDCALL_PTR(int, TY108129) (NI16 wversionrequired, WSADATA* Wsdata);
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
typedef N_NIMCALL_PTR(void, TY3489) (void* p, NI op);
typedef N_NIMCALL_PTR(void*, TY3494) (void* p);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY3489 marker;
TY3494 deepcopy;
};
typedef N_STDCALL_PTR(NI, TY107982) (int af, int typ, int protocol);
typedef N_STDCALL_PTR(int, TY107997) (NI s, SOCKADDR* name, unsigned int namelen);
struct  Addrinfo107850  {
int aiflags;
int aifamily;
int aisocktype;
int aiprotocol;
NI aiaddrlen;
NCSTRING aicanonname;
SOCKADDR* aiaddr;
Addrinfo107850* ainext;
};
typedef N_STDCALL_PTR(int, TY108134) (NCSTRING nodename, NCSTRING servname, Addrinfo107850* hints, Addrinfo107850** res);
typedef N_STDCALL_PTR(void, TY108143) (Addrinfo107850* ai);
typedef N_STDCALL_PTR(int, TY108038) (NI s, int backlog);
struct  Timeval107848  {
NI32 tvsec;
NI32 tvusec;
};
typedef NI TY107898[64];
struct  Tfdset107846  {
int fdcount;
TY107898 fdarray;
};
typedef N_STDCALL_PTR(int, TY108058) (int nfds, Tfdset107846* readfds, Tfdset107846* writefds, Tfdset107846* exceptfds, Timeval107848* timeout);
typedef N_STDCALL_PTR(int, TY107987) (NI s);
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
typedef NIM_CHAR TY107866[14];
struct TY527219 {
  TGenericSeq Sup;
  NI data[SEQ_DECL_SIZE];
};
N_NOINLINE(void, raiseoserror_120024)(NI32 errorcode, NimStringDesc* additionalinfo);
N_NIMCALL(NI32, oslasterror_120040)(void);
N_NIMCALL(int, toint_518820)(NU8 domain);
N_NIMCALL(int, toint_518823)(NU8 typ);
N_NIMCALL(int, toint_518826)(NU8 p);
N_NIMCALL(NimStringDesc*, HEX24_7801)(NU64 x);
N_NIMCALL(Timeval107848, timevalfrommilliseconds_522812)(NI timeout);
N_NIMCALL(void, createfdset_522823)(Tfdset107846* fd, TY527219* s, NI* m);
N_NIMCALL(void, Fdzero_108124)(Tfdset107846* s);
N_NIMCALL(void, Fdset_108109)(NI socket, Tfdset107846* s);
N_NIMCALL(void, prunesocketset_523014)(TY527219** s, Tfdset107846* fd);
N_NIMCALL(int, Fdisset_108103)(NI socket, Tfdset107846* set);
N_NIMCALL(TGenericSeq*, setLengthSeq)(TGenericSeq* seq, NI elemsize, NI newlen);
STRING_LITERAL(TMP1209, "", 0);
NI osinvalidsocket_518645;
WSADATA wsa_523106;
extern TY108129 Dl_108128;
TNimType NTI518604; /* Domain */
TNimType NTI518606; /* SockType */
TNimType NTI518608; /* Protocol */
extern TY107982 Dl_107981;
extern TY107997 Dl_107996;
extern TY108134 Dl_108133;
extern TY108143 Dl_108142;
extern TY108038 Dl_108037;
extern TY108058 Dl_108057;
extern TY107987 Dl_107986;

N_NIMCALL(int, toint_518820)(NU8 domain) {
	int result;
	result = 0;
	result = ((int) (((NI16)(NU16)(NU)(((NI) (domain))))));
	return result;
}

N_NIMCALL(int, toint_518823)(NU8 typ) {
	int result;
	result = 0;
	result = ((int) (typ));
	return result;
}

N_NIMCALL(int, toint_518826)(NU8 p) {
	int result;
	result = 0;
	result = ((int) (p));
	return result;
}

N_NIMCALL(NI, newnativesocket_519001)(NU8 domain, NU8 socktype, NU8 protocol) {
	NI result;
	int LOC1;
	int LOC2;
	int LOC3;
	result = 0;
	LOC1 = 0;
	LOC1 = toint_518820(domain);
	LOC2 = 0;
	LOC2 = toint_518823(socktype);
	LOC3 = 0;
	LOC3 = toint_518826(protocol);
	result = Dl_107981(LOC1, LOC2, LOC3);
	return result;
}

N_NIMCALL(NI16, ntohs_520001)(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_519806)(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_519201)(NI socket, SOCKADDR* name, unsigned int namelen) {
	int result;
	result = 0;
	result = Dl_107996(socket, name, namelen);
	return result;
}

N_NIMCALL(Addrinfo107850*, getaddrinfo_519402)(NimStringDesc* address, NU16 port, NU8 domain, NU8 socktype, NU8 protocol) {
	Addrinfo107850* result;
	Addrinfo107850 hints;
	int gairesult;
	NimStringDesc* LOC5;
	result = 0;
	memset((void*)(&hints), 0, sizeof(hints));
	result = NIM_NIL;
	hints.aifamily = toint_518820(domain);
	hints.aisocktype = toint_518823(socktype);
	hints.aiprotocol = toint_518826(protocol);
	{
		if (!(domain == ((NU8) 23))) goto LA3;
		hints.aiflags = ((int) 8);
	}
	LA3: ;
	LOC5 = 0;
	LOC5 = HEX24_7801(port);
	gairesult = Dl_108133(address->data, LOC5->data, (&hints), &result);
	{
		NI32 LOC10;
		if (!!((gairesult == ((NI32) 0)))) goto LA8;
		LOC10 = 0;
		LOC10 = oslasterror_120040();
		raiseoserror_120024(LOC10, ((NimStringDesc*) &TMP1209));
	}
	LA8: ;
	return result;
}

N_NIMCALL(void, dealloc_519802)(Addrinfo107850* ai) {
	Dl_108142(ai);
}

N_NIMCALL(int, listen_519208)(NI socket, int backlog) {
	int result;
	result = 0;
	result = Dl_108037(socket, backlog);
	return result;
}

N_NIMCALL(Timeval107848, timevalfrommilliseconds_522812)(NI timeout) {
	Timeval107848 result;
	memset((void*)(&result), 0, sizeof(result));
	{
		NI seconds;
		if (!!((timeout == ((NI) -1)))) goto LA3;
		seconds = (NI)(timeout / ((NI) 1000));
		result.tvsec = ((NI32) (seconds));
		result.tvusec = ((NI32) ((NI)((NI)(timeout - (NI)(seconds * ((NI) 1000))) * ((NI) 1000))));
	}
	LA3: ;
	return result;
}

N_NIMCALL(void, createfdset_522823)(Tfdset107846* fd, TY527219* s, NI* m) {
	Fdzero_108124(fd);
	{
		NI i_523002;
		NI i_523011;
		NI L_523013;
		i_523002 = 0;
		i_523011 = ((NI) 0);
		L_523013 = (s ? s->Sup.len : 0);
		{
			while (1) {
				if (!(i_523011 < L_523013)) goto LA3;
				i_523002 = s->data[i_523011];
				(*m) = (((*m) >= i_523002) ? (*m) : i_523002);
				Fdset_108109(i_523002, fd);
				i_523011 += ((NI) 1);
			} LA3: ;
		}
	}
}

N_NIMCALL(void, prunesocketset_523014)(TY527219** s, Tfdset107846* 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 = Fdisset_108103((*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) = (TY527219*) setLengthSeq(&((*s))->Sup, sizeof(NI), ((NI) (L)));
}

N_NIMCALL(NI, select_523060)(TY527219** readfds, NI timeout) {
	NI result;
	Timeval107848 tv;
	Tfdset107846 rd;
	NI m;
	result = 0;
	tv = timevalfrommilliseconds_522812(timeout);
	memset((void*)(&rd), 0, sizeof(rd));
	m = ((NI) 0);
	createfdset_522823((&rd), (*readfds), (&m));
	{
		int LOC5;
		if (!!((timeout == ((NI) -1)))) goto LA3;
		LOC5 = 0;
		LOC5 = Dl_108057(((int) ((NI)(m + ((NI) 1)))), (&rd), NIM_NIL, NIM_NIL, (&tv));
		result = ((NI) (LOC5));
	}
	goto LA1;
	LA3: ;
	{
		int LOC7;
		LOC7 = 0;
		LOC7 = Dl_108057(((int) ((NI)(m + ((NI) 1)))), (&rd), NIM_NIL, NIM_NIL, NIM_NIL);
		result = ((NI) (LOC7));
	}
	LA1: ;
	prunesocketset_523014(readfds, (&rd));
	return result;
}

N_NIMCALL(void, close_519013)(NI socket) {
	int LOC1;
	LOC1 = 0;
	LOC1 = Dl_107986(socket);
}
NIM_EXTERNC N_NOINLINE(void, stdlib_nativesocketsInit000)(void) {
	osinvalidsocket_518645 = INVALID_SOCKET;
	{
		int LOC3;
		NI32 LOC6;
		LOC3 = 0;
		LOC3 = Dl_108128(((NI16) 257), (&wsa_523106));
		if (!!((LOC3 == ((NI32) 0)))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_120040();
		raiseoserror_120024(LOC6, ((NimStringDesc*) &TMP1209));
	}
	LA4: ;
}

NIM_EXTERNC N_NOINLINE(void, stdlib_nativesocketsDatInit000)(void) {
static TNimNode* TMP5265[3];
NI TMP5267;
static char* NIM_CONST TMP5266[3] = {
"AF_UNIX", 
"AF_INET", 
"AF_INET6"};
static TNimNode* TMP5268[4];
NI TMP5270;
static char* NIM_CONST TMP5269[4] = {
"SOCK_STREAM", 
"SOCK_DGRAM", 
"SOCK_RAW", 
"SOCK_SEQPACKET"};
static TNimNode* TMP5271[6];
NI TMP5273;
static char* NIM_CONST TMP5272[6] = {
"IPPROTO_TCP", 
"IPPROTO_UDP", 
"IPPROTO_IP", 
"IPPROTO_IPV6", 
"IPPROTO_RAW", 
"IPPROTO_ICMP"};
static TNimNode TMP1201[16];
NTI518604.size = sizeof(NU8);
NTI518604.kind = 14;
NTI518604.base = 0;
NTI518604.flags = 3;
for (TMP5267 = 0; TMP5267 < 3; TMP5267++) {
TMP1201[TMP5267+0].kind = 1;
TMP1201[TMP5267+0].offset = TMP5267;
TMP1201[TMP5267+0].name = TMP5266[TMP5267];
TMP5265[TMP5267] = &TMP1201[TMP5267+0];
}
TMP1201[0].offset = 0;
TMP1201[1].offset = 2;
TMP1201[2].offset = 23;
TMP1201[3].len = 3; TMP1201[3].kind = 2; TMP1201[3].sons = &TMP5265[0];
NTI518604.node = &TMP1201[3];
NTI518604.flags = 1<<2;
NTI518606.size = sizeof(NU8);
NTI518606.kind = 14;
NTI518606.base = 0;
NTI518606.flags = 3;
for (TMP5270 = 0; TMP5270 < 4; TMP5270++) {
TMP1201[TMP5270+4].kind = 1;
TMP1201[TMP5270+4].offset = TMP5270;
TMP1201[TMP5270+4].name = TMP5269[TMP5270];
TMP5268[TMP5270] = &TMP1201[TMP5270+4];
}
TMP1201[4].offset = 1;
TMP1201[5].offset = 2;
TMP1201[6].offset = 3;
TMP1201[7].offset = 5;
TMP1201[8].len = 4; TMP1201[8].kind = 2; TMP1201[8].sons = &TMP5268[0];
NTI518606.node = &TMP1201[8];
NTI518606.flags = 1<<2;
NTI518608.size = sizeof(NU8);
NTI518608.kind = 14;
NTI518608.base = 0;
NTI518608.flags = 3;
for (TMP5273 = 0; TMP5273 < 6; TMP5273++) {
TMP1201[TMP5273+9].kind = 1;
TMP1201[TMP5273+9].offset = TMP5273;
TMP1201[TMP5273+9].name = TMP5272[TMP5273];
TMP5271[TMP5273] = &TMP1201[TMP5273+9];
}
TMP1201[9].offset = 6;
TMP1201[10].offset = 17;
TMP1201[11].offset = 18;
TMP1201[12].offset = 19;
TMP1201[13].offset = 20;
TMP1201[14].offset = 21;
TMP1201[15].len = 6; TMP1201[15].kind = 2; TMP1201[15].sons = &TMP5271[0];
NTI518608.node = &TMP1201[15];
NTI518608.flags = 1<<2;
}

