/* 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 "winsock2.h"
#include <string.h>
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct Addrinfo112868 Addrinfo112868;
typedef struct TY572225 TY572225;
typedef struct Timeval112866 Timeval112866;
typedef struct Tfdset112864 Tfdset112864;
typedef NIM_CHAR TY112874[257];
typedef NIM_CHAR TY112877[129];
typedef N_STDCALL_PTR(int, TY113246) (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, 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;
};
typedef N_STDCALL_PTR(NI, TY113024) (int af, int typ, int protocol);
typedef N_STDCALL_PTR(int, TY113048) (NI s, SOCKADDR* name, unsigned int namelen);
struct  Addrinfo112868  {
int aiflags;
int aifamily;
int aisocktype;
int aiprotocol;
NI aiaddrlen;
NCSTRING aicanonname;
SOCKADDR* aiaddr;
Addrinfo112868* ainext;
};
typedef N_STDCALL_PTR(int, TY113254) (NCSTRING nodename, NCSTRING servname, Addrinfo112868* hints, Addrinfo112868** res);
typedef N_STDCALL_PTR(void, TY113266) (Addrinfo112868* ai);
typedef N_STDCALL_PTR(int, TY113107) (NI s, int backlog);
struct  Timeval112866  {
NI32 tvsec;
NI32 tvusec;
};
typedef NI TY112916[64];
struct  Tfdset112864  {
int fdcount;
TY112916 fdarray;
};
typedef N_STDCALL_PTR(int, TY113136) (int nfds, Tfdset112864* readfds, Tfdset112864* writefds, Tfdset112864* exceptfds, Timeval112866* timeout);
typedef N_STDCALL_PTR(int, TY113032) (NI s);
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
typedef NIM_CHAR TY112884[14];
struct TY572225 {
  TGenericSeq Sup;
  NI data[SEQ_DECL_SIZE];
};
N_NOINLINE(void, raiseoserror_125030)(NI32 errorcode, NimStringDesc* additionalinfo);
N_NIMCALL(NI32, oslasterror_125055)(void);
N_NIMCALL(int, toint_563829)(NU8 domain);
N_NIMCALL(int, toint_563835)(NU8 typ);
N_NIMCALL(int, toint_563841)(NU8 p);
N_NIMCALL(NimStringDesc*, HEX24_8801)(NU64 x);
N_NIMCALL(Timeval112866, timevalfrommilliseconds_567818)(NI timeout);
N_NIMCALL(void, createfdset_567833)(Tfdset112864* fd, TY572225* s, NI* m);
N_NIMCALL(void, Fdzero_113235)(Tfdset112864* s);
N_NIMCALL(void, Fdset_113214)(NI socket, Tfdset112864* s);
N_NIMCALL(void, prunesocketset_568038)(TY572225** s, Tfdset112864* fd);
N_NIMCALL(int, Fdisset_113202)(NI socket, Tfdset112864* set);
N_NIMCALL(TGenericSeq*, setLengthSeq)(TGenericSeq* seq, NI elemsize, NI newlen);
STRING_LITERAL(TMP1170, "", 0);
NI osinvalidsocket_563651;
WSADATA wsa_568150;
extern TY113246 Dl_113245;
TNimType NTI563604; /* Domain */
TNimType NTI563606; /* SockType */
TNimType NTI563608; /* Protocol */
extern TY113024 Dl_113023;
extern TY113048 Dl_113047;
extern TY113254 Dl_113253;
extern TY113266 Dl_113265;
extern TY113107 Dl_113106;
extern TY113136 Dl_113135;
extern TY113032 Dl_113031;

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

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

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

N_NIMCALL(NI, newnativesocket_564004)(NU8 domain, NU8 socktype, NU8 protocol) {
	NI result;
	int LOC1;
	int LOC2;
	int LOC3;
	result = 0;
	LOC1 = 0;
	LOC1 = toint_563829(domain);
	LOC2 = 0;
	LOC2 = toint_563835(socktype);
	LOC3 = 0;
	LOC3 = toint_563841(protocol);
	result = Dl_113023(LOC1, LOC2, LOC3);
	return result;
}

N_NIMCALL(NI16, ntohs_565007)(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_564818)(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_564207)(NI socket, SOCKADDR* name, unsigned int namelen) {
	int result;
	result = 0;
	result = Dl_113047(socket, name, namelen);
	return result;
}

N_NIMCALL(Addrinfo112868*, getaddrinfo_564408)(NimStringDesc* address, NU16 port, NU8 domain, NU8 socktype, NU8 protocol) {
	Addrinfo112868* result;
	Addrinfo112868 hints;
	int gairesult;
	NimStringDesc* LOC1;
	result = 0;
	memset((void*)(&hints), 0, sizeof(hints));
	result = NIM_NIL;
	hints.aifamily = toint_563829(domain);
	hints.aisocktype = toint_563835(socktype);
	hints.aiprotocol = toint_563841(protocol);
	hints.aiflags = ((int) 8);
	LOC1 = 0;
	LOC1 = HEX24_8801(port);
	gairesult = Dl_113253(address->data, LOC1->data, (&hints), &result);
	{
		NI32 LOC6;
		if (!!((gairesult == ((NI32) 0)))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_125055();
		raiseoserror_125030(LOC6, ((NimStringDesc*) &TMP1170));
	}
	LA4: ;
	return result;
}

N_NIMCALL(void, dealloc_564808)(Addrinfo112868* ai) {
	Dl_113265(ai);
}

N_NIMCALL(int, listen_564220)(NI socket, int backlog) {
	int result;
	result = 0;
	result = Dl_113106(socket, backlog);
	return result;
}

N_NIMCALL(Timeval112866, timevalfrommilliseconds_567818)(NI timeout) {
	Timeval112866 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_567833)(Tfdset112864* fd, TY572225* s, NI* m) {
	Fdzero_113235(fd);
	{
		NI i_568011;
		NI i_568020;
		NI L_568022;
		i_568011 = 0;
		i_568020 = ((NI) 0);
		L_568022 = (s ? s->Sup.len : 0);
		{
			while (1) {
				if (!(i_568020 < L_568022)) goto LA3;
				i_568011 = s->data[i_568020];
				(*m) = (((*m) >= i_568011) ? (*m) : i_568011);
				Fdset_113214(i_568011, fd);
				i_568020 += ((NI) 1);
			} LA3: ;
		}
	}
}

N_NIMCALL(void, prunesocketset_568038)(TY572225** s, Tfdset112864* 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_113202((*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) = (TY572225*) setLengthSeq(&((*s))->Sup, sizeof(NI), ((NI) (L)));
}

N_NIMCALL(NI, select_568096)(TY572225** readfds, NI timeout) {
	NI result;
	Timeval112866 tv;
	Tfdset112864 rd;
	NI m;
	result = 0;
	tv = timevalfrommilliseconds_567818(timeout);
	memset((void*)(&rd), 0, sizeof(rd));
	m = ((NI) 0);
	createfdset_567833((&rd), (*readfds), (&m));
	{
		int LOC5;
		if (!!((timeout == ((NI) -1)))) goto LA3;
		LOC5 = 0;
		LOC5 = Dl_113135(((int) ((NI)(m + ((NI) 1)))), (&rd), NIM_NIL, NIM_NIL, (&tv));
		result = ((NI) (LOC5));
	}
	goto LA1;
	LA3: ;
	{
		int LOC7;
		LOC7 = 0;
		LOC7 = Dl_113135(((int) ((NI)(m + ((NI) 1)))), (&rd), NIM_NIL, NIM_NIL, NIM_NIL);
		result = ((NI) (LOC7));
	}
	LA1: ;
	prunesocketset_568038(readfds, (&rd));
	return result;
}

N_NIMCALL(void, close_564028)(NI socket) {
	int LOC1;
	LOC1 = 0;
	LOC1 = Dl_113031(socket);
}
NIM_EXTERNC N_NOINLINE(void, stdlib_nativesocketsInit000)(void) {
	osinvalidsocket_563651 = INVALID_SOCKET;
	{
		int LOC3;
		NI32 LOC6;
		LOC3 = 0;
		LOC3 = Dl_113245(((NI16) 257), (&wsa_568150));
		if (!!((LOC3 == ((NI32) 0)))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_125055();
		raiseoserror_125030(LOC6, ((NimStringDesc*) &TMP1170));
	}
	LA4: ;
}

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

