/* Generated by Nim Compiler v0.11.2 */
/*   (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 Addrinfo105868 Addrinfo105868;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct TY517225 TY517225;
typedef struct Timeval105866 Timeval105866;
typedef struct Tfdset105864 Tfdset105864;
typedef NIM_CHAR TY105873[257];
typedef NIM_CHAR TY105876[129];
typedef N_STDCALL_PTR(int, TY106236) (NI16 wversionrequired, WSADATA* Wsdata);
typedef N_STDCALL_PTR(NI, TY106024) (int af, int typ, int protocol);
typedef N_STDCALL_PTR(int, TY106048) (NI s, SOCKADDR* name, unsigned int namelen);
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct  Addrinfo105868  {
int aiflags;
int aifamily;
int aisocktype;
int aiprotocol;
NI aiaddrlen;
NCSTRING aicanonname;
SOCKADDR* aiaddr;
Addrinfo105868* ainext;
};
typedef N_STDCALL_PTR(int, TY106244) (NCSTRING nodename, NCSTRING servname, Addrinfo105868* hints, Addrinfo105868** res);
typedef N_STDCALL_PTR(void, TY106256) (Addrinfo105868* ai);
typedef N_STDCALL_PTR(int, TY106097) (NI s, int backlog);
struct  Timeval105866  {
NI32 tvsec;
NI32 tvusec;
};
typedef NI TY105915[64];
struct  Tfdset105864  {
int fdcount;
TY105915 fdarray;
};
typedef N_STDCALL_PTR(int, TY106126) (int nfds, Tfdset105864* readfds, Tfdset105864* writefds, Tfdset105864* exceptfds, Timeval105866* timeout);
typedef N_STDCALL_PTR(int, TY106032) (NI s);
typedef NIM_CHAR TY105883[14];
struct TY517225 {
  TGenericSeq Sup;
  NI data[SEQ_DECL_SIZE];
};
N_NIMCALL(void, raiseoserror_114430)(NI32 errorcode);
N_NIMCALL(NI32, oslasterror_114454)(void);
N_NIMCALL(int, toint_509829)(NU8 domain);
N_NIMCALL(int, toint_509835)(NU8 typ);
N_NIMCALL(int, toint_509841)(NU8 p);
N_NIMCALL(NI16, ntohs_510807)(NI16 x);
N_NIMCALL(NI32, ntohl_510618)(NI32 x);
N_NIMCALL(NimStringDesc*, HEX24_6401)(NU64 x);
N_NIMCALL(Timeval105866, timevalfrommilliseconds_512618)(NI timeout);
N_NIMCALL(void, createfdset_512635)(Tfdset105864* fd, TY517225* s, NI* m);
N_NIMCALL(void, Fdzero_106225)(Tfdset105864* s);
N_NIMCALL(void, Fdset_106204)(NI socket, Tfdset105864* s);
N_NIMCALL(void, prunesocketset_512838)(TY517225** s, Tfdset105864* fd);
N_NIMCALL(int, Fdisset_106192)(NI socket, Tfdset105864* set);
N_NIMCALL(TGenericSeq*, setLengthSeq)(TGenericSeq* seq, NI elemsize, NI newlen);
NI osinvalidsocket_509651;
WSADATA wsa_512954;
extern TY106236 Dl_106235;
extern TY106024 Dl_106023;
extern TY106048 Dl_106047;
extern TY106244 Dl_106243;
extern TY106256 Dl_106255;
extern TY106097 Dl_106096;
extern TY106126 Dl_106125;
extern TY106032 Dl_106031;

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

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

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

N_NIMCALL(NI, newrawsocket_510004)(NU8 domain, NU8 typ, NU8 protocol) {
	NI result;
	int LOC1;
	int LOC2;
	int LOC3;
	result = 0;
	LOC1 = 0;
	LOC1 = toint_509829(domain);
	LOC2 = 0;
	LOC2 = toint_509835(typ);
	LOC3 = 0;
	LOC3 = toint_509841(protocol);
	result = Dl_106023(LOC1, LOC2, LOC3);
	return result;
}

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

N_NIMCALL(NI16, htons_511017)(NI16 x) {
	NI16 result;
	result = 0;
	result = ntohs_510807(x);
	return result;
}

N_NIMCALL(NI32, ntohl_510618)(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(NI32, htonl_511007)(NI32 x) {
	NI32 result;
	result = 0;
	result = ntohl_510618(x);
	return result;
}

N_NIMCALL(int, bindaddr_510207)(NI socket, SOCKADDR* name, unsigned int namelen) {
	int result;
	result = 0;
	result = Dl_106047(socket, name, namelen);
	return result;
}

N_NIMCALL(Addrinfo105868*, getaddrinfo_510408)(NimStringDesc* address, NU16 port, NU8 af, NU8 typ, NU8 prot) {
	Addrinfo105868* result;
	Addrinfo105868 hints;
	int gairesult;
	NimStringDesc* LOC1;
	result = 0;
	memset((void*)(&hints), 0, sizeof(hints));
	result = NIM_NIL;
	hints.aifamily = toint_509829(af);
	hints.aisocktype = toint_509835(typ);
	hints.aiprotocol = toint_509841(prot);
	LOC1 = 0;
	LOC1 = HEX24_6401(port);
	gairesult = Dl_106243(address->data, LOC1->data, (&hints), &result);
	{
		NI32 LOC6;
		if (!!((gairesult == ((NI32) 0)))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_114454();
		raiseoserror_114430(LOC6);
	}
	LA4: ;
	return result;
}

N_NIMCALL(void, dealloc_510608)(Addrinfo105868* ai) {
	Dl_106255(ai);
}

N_NIMCALL(int, listen_510220)(NI socket, int backlog) {
	int result;
	result = 0;
	result = Dl_106096(socket, backlog);
	return result;
}

N_NIMCALL(Timeval105866, timevalfrommilliseconds_512618)(NI timeout) {
	Timeval105866 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_512635)(Tfdset105864* fd, TY517225* s, NI* m) {
	Fdzero_106225(fd);
	{
		NI i_512811;
		NI i_512820;
		NI L_512822;
		i_512811 = 0;
		i_512820 = ((NI) 0);
		L_512822 = (s ? s->Sup.len : 0);
		{
			while (1) {
				if (!(i_512820 < L_512822)) goto LA3;
				i_512811 = s->data[i_512820];
				(*m) = (((*m) >= i_512811) ? (*m) : i_512811);
				Fdset_106204(i_512811, fd);
				i_512820 += ((NI) 1);
			} LA3: ;
		}
	}
}

N_NIMCALL(void, prunesocketset_512838)(TY517225** s, Tfdset105864* 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_106192((*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) = (TY517225*) setLengthSeq(&((*s))->Sup, sizeof(NI), ((NI) (L)));
}

N_NIMCALL(NI, select_512896)(TY517225** readfds, NI timeout) {
	NI result;
	Timeval105866 tv;
	Tfdset105864 rd;
	NI m;
	result = 0;
	tv = timevalfrommilliseconds_512618(timeout);
	memset((void*)(&rd), 0, sizeof(rd));
	m = ((NI) 0);
	createfdset_512635((&rd), (*readfds), (&m));
	{
		int LOC5;
		if (!!((timeout == ((NI) -1)))) goto LA3;
		LOC5 = 0;
		LOC5 = Dl_106125(((int) ((NI)(m + ((NI) 1)))), (&rd), NIM_NIL, NIM_NIL, (&tv));
		result = ((NI) (LOC5));
	}
	goto LA1;
	LA3: ;
	{
		int LOC7;
		LOC7 = 0;
		LOC7 = Dl_106125(((int) ((NI)(m + ((NI) 1)))), (&rd), NIM_NIL, NIM_NIL, NIM_NIL);
		result = ((NI) (LOC7));
	}
	LA1: ;
	prunesocketset_512838(readfds, (&rd));
	return result;
}

N_NIMCALL(void, close_510028)(NI socket) {
	int LOC1;
	LOC1 = 0;
	LOC1 = Dl_106031(socket);
}
NIM_EXTERNC N_NOINLINE(void, stdlib_rawsocketsInit)(void) {
	osinvalidsocket_509651 = INVALID_SOCKET;
	{
		int LOC3;
		NI32 LOC6;
		LOC3 = 0;
		LOC3 = Dl_106235(((NI16) 257), (&wsa_512954));
		if (!!((LOC3 == ((NI32) 0)))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_114454();
		raiseoserror_114430(LOC6);
	}
	LA4: ;
}

NIM_EXTERNC N_NOINLINE(void, stdlib_rawsocketsDatInit)(void) {
}

