/* Generated by Nim Compiler v0.10.2 */
/*   (c) 2014 Andreas Rumpf */
/* The generated code is subject to the original license. */
#define NIM_INTBITS 32
#include "nimbase.h"

#include <string.h>

#include <stdio.h>

#include <stdlib.h>

#include "Winsock2.h"
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct tcell44733 tcell44733;
typedef struct TNimType TNimType;
typedef struct tcellseq44749 tcellseq44749;
typedef struct tgcheap46816 tgcheap46816;
typedef struct tcellset44745 tcellset44745;
typedef struct tpagedesc44741 tpagedesc44741;
typedef struct tmemregion27010 tmemregion27010;
typedef struct tsmallchunk26240 tsmallchunk26240;
typedef struct tllchunk27004 tllchunk27004;
typedef struct tbigchunk26242 tbigchunk26242;
typedef struct tintset26217 tintset26217;
typedef struct ttrunk26213 ttrunk26213;
typedef struct tavlnode27008 tavlnode27008;
typedef struct tgcstat46814 tgcstat46814;
typedef struct optparser208011 optparser208011;
typedef struct TNimObject TNimObject;
typedef struct TNimNode TNimNode;
typedef struct tsocketimpl183607 tsocketimpl183607;
typedef struct tbasechunk26238 tbasechunk26238;
typedef struct tfreecell26230 tfreecell26230;
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct  tcell44733  {
NI Refcount;
TNimType* Typ;
};
struct  tcellseq44749  {
NI Len;
NI Cap;
tcell44733** D;
};
struct  tcellset44745  {
NI Counter;
NI Max;
tpagedesc44741* Head;
tpagedesc44741** Data;
};
typedef tsmallchunk26240* TY27022[512];
typedef ttrunk26213* ttrunkbuckets26215[256];
struct  tintset26217  {
ttrunkbuckets26215 Data;
};
struct  tmemregion27010  {
NI Minlargeobj;
NI Maxlargeobj;
TY27022 Freesmallchunks;
tllchunk27004* Llmem;
NI Currmem;
NI Maxmem;
NI Freemem;
NI Lastsize;
tbigchunk26242* Freechunkslist;
tintset26217 Chunkstarts;
tavlnode27008* Root;
tavlnode27008* Deleted;
tavlnode27008* Last;
tavlnode27008* Freeavlnodes;
};
struct  tgcstat46814  {
NI Stackscans;
NI Cyclecollections;
NI Maxthreshold;
NI Maxstacksize;
NI Maxstackcells;
NI Cycletablesize;
NI64 Maxpause;
};
struct  tgcheap46816  {
void* Stackbottom;
NI Cyclethreshold;
tcellseq44749 Zct;
tcellseq44749 Decstack;
tcellset44745 Cycleroots;
tcellseq44749 Tempstack;
NI Recgclock;
tmemregion27010 Region;
tgcstat46814 Stat;
};
typedef N_NIMCALL_PTR(void, TY3089) (void* p, NI op);
typedef N_NIMCALL_PTR(void*, TY3094) (void* p);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY3089 marker;
TY3094 deepcopy;
};
struct  TNimObject  {
TNimType* m_type;
};
struct  optparser208011  {
  TNimObject Sup;
NimStringDesc* Cmd;
NI Pos;
NIM_BOOL Inshortstate;
NU8 Kind;
NimStringDesc* Key;
NimStringDesc* Val;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
typedef NimStringDesc* TY261532[1];
typedef N_NIMCALL_PTR(void, TY611261) (void);
typedef NIM_CHAR TY183632[4001];
struct  tsocketimpl183607  {
NI Fd;
NIM_BOOL Isbuffered;
union {
struct {TY183632 Buffer;
NI Currpos;
NI Buflen;
} S1;
} isbufferedU;
NIM_BOOL Nonblocking;
};
typedef NI TY26220[16];
struct  tpagedesc44741  {
tpagedesc44741* Next;
NI Key;
TY26220 Bits;
};
struct  tbasechunk26238  {
NI Prevsize;
NI Size;
NIM_BOOL Used;
};
struct  tsmallchunk26240  {
  tbasechunk26238 Sup;
tsmallchunk26240* Next;
tsmallchunk26240* Prev;
tfreecell26230* Freelist;
NI Free;
NI Acc;
NF Data;
};
struct  tllchunk27004  {
NI Size;
NI Acc;
tllchunk27004* Next;
};
struct  tbigchunk26242  {
  tbasechunk26238 Sup;
tbigchunk26242* Next;
tbigchunk26242* Prev;
NI Align;
NF Data;
};
struct  ttrunk26213  {
ttrunk26213* Next;
NI Key;
TY26220 Bits;
};
typedef tavlnode27008* TY27014[2];
struct  tavlnode27008  {
TY27014 Link;
NI Key;
NI Upperbound;
NI Level;
};
struct  tfreecell26230  {
tfreecell26230* Next;
NI Zerofield;
};
N_NIMCALL(NimStringDesc*, copyStringRC1)(NimStringDesc* src);
static N_INLINE(void, nimGCunrefNoCycle)(void* p);
static N_INLINE(tcell44733*, usrtocell_48446)(void* usr);
static N_INLINE(void, rtladdzct_50004)(tcell44733* c);
N_NOINLINE(void, addzct_48417)(tcellseq44749* s, tcell44733* c);
N_NIMCALL(void, initoptparser_208030)(NimStringDesc* cmdline, optparser208011* Result);
N_NOINLINE(void, chckNil)(void* p);
N_NIMCALL(void, nponext)(optparser208011* p);
N_NIMCALL(void, processswitch_211148)(NU8 pass, optparser208011* p);
N_NIMCALL(NIM_BOOL, processargument_211161)(NU8 pass, optparser208011* p, NI* argscount);
N_NIMCALL(NimStringDesc*, nsuNormalize)(NimStringDesc* s);
static N_INLINE(NIM_BOOL, eqStrings)(NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(void, rawmessage_198643)(NU16 msg, NimStringDesc** args, NI argsLen0);
N_NIMCALL(NimStringDesc*, getconfigvar_170350)(NimStringDesc* key);
N_NIMCALL(NimStringDesc*, readline_12886)(FILE* f);
N_NIMCALL(void, processcmdline_611209)(NU8 pass, NimStringDesc* cmd);
N_NIMCALL(tsocketimpl183607*, socket_184604)(NU8 domain, NU8 typ, NU8 protocol, NIM_BOOL buffered);
N_NIMCALL(void, raiseoserror_112435)(NI32 errorcode);
N_NIMCALL(NI32, oslasterror_112467)(void);
N_NIMCALL(NI, nsuParseInt)(NimStringDesc* s);
N_NIMCALL(void, bindaddr_185773)(tsocketimpl183607* socket, NU16 port, NimStringDesc* address);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NIMCALL(void, listen_185608)(tsocketimpl183607* socket, int backlog);
N_NIMCALL(void*, newObjRC1)(TNimType* typ, NI size);
N_NIMCALL(void, accept_187401)(tsocketimpl183607* server, tsocketimpl183607** client);
N_NIMCALL(void, readline_192447)(tsocketimpl183607* socket, NimStringDesc** line, NI timeout);
N_NIMCALL(void, send_193808)(tsocketimpl183607* socket, NimStringDesc* data);
N_NIMCALL(void, close_187478)(tsocketimpl183607* socket);
STRING_LITERAL(TMP1004, "", 0);
STRING_LITERAL(TMP1897, "run", 3);
STRING_LITERAL(TMP4854, "server.type", 11);
STRING_LITERAL(TMP4855, "stdin", 5);
STRING_LITERAL(TMP4856, "tcp", 3);
STRING_LITERAL(TMP4858, "quit", 4);
STRING_LITERAL(TMP4862, "server.port", 11);
STRING_LITERAL(TMP4863, "server.address", 14);
STRING_LITERAL(TMP4871, "\015\012", 2);
STRING_LITERAL(TMP4872, "Invalid server.type:", 20);
NimStringDesc* curcaascmd_611204;
extern tgcheap46816 gch_46844;
NimStringDesc* lastcaascmd_611205;
extern TNimType NTI208011; /* OptParser */
extern NU32 gglobaloptions_170118;
extern NimStringDesc* arguments_211144;
extern NimStringDesc* command_170239;
extern NI32 gdirtybufferidx_170134;
extern NI32 gdirtyoriginalidx_170135;
extern NI gerrorcounter_197306;
extern tsocketimpl183607* invalidsocket_183696;
extern tsocketimpl183607* stdoutsocket_197318;
extern TNimType NTI183609; /* Socket */

static N_INLINE(tcell44733*, usrtocell_48446)(void* usr) {
	tcell44733* result;
	result = 0;
	result = ((tcell44733*) ((NI)((NU32)(((NI) (usr))) - (NU32)(((NI)sizeof(tcell44733))))));
	return result;
}

static N_INLINE(void, rtladdzct_50004)(tcell44733* c) {
	addzct_48417((&gch_46844.Zct), c);
}

static N_INLINE(void, nimGCunrefNoCycle)(void* p) {
	tcell44733* c;
	c = usrtocell_48446(p);
	{
		(*c).Refcount -= 8;
		if (!((NU32)((*c).Refcount) < (NU32)(8))) goto LA3;
		rtladdzct_50004(c);
	}
	LA3: ;
}

static N_INLINE(NIM_BOOL, eqStrings)(NimStringDesc* a, NimStringDesc* b) {
	NIM_BOOL result;
	NIM_BOOL LOC11;
	int LOC13;
	result = 0;
	{
		if (!(a == b)) goto LA3;
		result = NIM_TRUE;
		goto BeforeRet;
	}
	LA3: ;
	{
		NIM_BOOL LOC7;
		LOC7 = 0;
		LOC7 = (a == NIM_NIL);
		if (LOC7) goto LA8;
		LOC7 = (b == NIM_NIL);
		LA8: ;
		if (!LOC7) goto LA9;
		result = NIM_FALSE;
		goto BeforeRet;
	}
	LA9: ;
	LOC11 = 0;
	LOC11 = ((*a).Sup.len == (*b).Sup.len);
	if (!(LOC11)) goto LA12;
	LOC13 = 0;
	LOC13 = memcmp(((NCSTRING) ((*a).data)), ((NCSTRING) ((*b).data)), (NI32)((*a).Sup.len * 1));
	LOC11 = (LOC13 == ((NI32) 0));
	LA12: ;
	result = LOC11;
	goto BeforeRet;
	BeforeRet: ;
	return result;
}

N_NIMCALL(void, processcmdline_611209)(NU8 pass, NimStringDesc* cmd) {
	optparser208011 p;
	NI argscount;
	memset((void*)(&p), 0, sizeof(p));
	p.Sup.m_type = (&NTI208011);
	chckNil((void*)(&p));
	memset((void*)(&p), 0, sizeof(p));
	p.Sup.m_type = (&NTI208011);
	initoptparser_208030(cmd, (&p));
	argscount = 0;
	{
		while (1) {
			nponext((&p));
			switch (p.Kind) {
			case ((NU8) 0):
			{
				goto LA1;
			}
			break;
			case ((NU8) 2):
			case ((NU8) 3):
			{
				processswitch_211148(pass, (&p));
			}
			break;
			case ((NU8) 1):
			{
				{
					NIM_BOOL LOC8;
					LOC8 = 0;
					LOC8 = processargument_211161(pass, (&p), (&argscount));
					if (!LOC8) goto LA9;
					goto LA1;
				}
				LA9: ;
			}
			break;
			}
		}
	} LA1: ;
	{
		if (!(pass == ((NU8) 1))) goto LA13;
		{
			NIM_BOOL LOC17;
			NIM_BOOL LOC18;
			NimStringDesc* LOC21;
			TY261532 LOC24;
			LOC17 = 0;
			LOC18 = 0;
			LOC18 = !(((gglobaloptions_170118 &(1<<((((NU8) 13))&31)))!=0));
			if (!(LOC18)) goto LA19;
			LOC18 = !(((arguments_211144) && (arguments_211144)->Sup.len == 0));
			LA19: ;
			LOC17 = LOC18;
			if (!(LOC17)) goto LA20;
			LOC21 = 0;
			LOC21 = nsuNormalize(command_170239);
			LOC17 = !(eqStrings(LOC21, ((NimStringDesc*) &TMP1897)));
			LA20: ;
			if (!LOC17) goto LA22;
			memset((void*)LOC24, 0, sizeof(LOC24));
			rawmessage_198643(((NU16) 57), LOC24, 0);
		}
		LA22: ;
	}
	LA13: ;
}

N_NIMCALL(void, serve_611259)(TY611261 action) {
	NimStringDesc* typ;
	typ = getconfigvar_170350(((NimStringDesc*) &TMP4854));
	if (eqStrings(typ, ((NimStringDesc*) &TMP4855))) goto LA1;
	if (eqStrings(typ, ((NimStringDesc*) &TMP4856))) goto LA2;
	if (eqStrings(typ, ((NimStringDesc*) &TMP1004))) goto LA2;
	goto LA3;
	LA1: ;
	{
		{
			while (1) {
				NimStringDesc* line;
				NimStringDesc* LOC12;
				line = readline_12886(stdin);
				{
					if (!eqStrings(line, ((NimStringDesc*) &TMP4858))) goto LA10;
					exit(0);
				}
				LA10: ;
				LOC12 = 0;
				LOC12 = curcaascmd_611204; curcaascmd_611204 = copyStringRC1(line);
				if (LOC12) nimGCunrefNoCycle(LOC12);
				processcmdline_611209(((NU8) 1), line);
				action();
				gdirtybufferidx_170134 = ((NI32) 0);
				gdirtyoriginalidx_170135 = ((NI32) 0);
				gerrorcounter_197306 = 0;
				printf("%s\015\012", (((NimStringDesc*) &TMP1004))->data);
				fflush(stdout);
			}
		}
	}
	goto LA4;
	LA2: ;
	{
		tsocketimpl183607* server;
		NimStringDesc* p;
		NU16 port;
		NimStringDesc* LOC25;
		NimStringDesc* inp;
		server = socket_184604(((NU8) 2), ((NU8) 1), ((NU8) 6), NIM_TRUE);
		{
			NI32 LOC18;
			if (!(server == invalidsocket_183696)) goto LA16;
			LOC18 = 0;
			LOC18 = oslasterror_112467();
			raiseoserror_112435(LOC18);
		}
		LA16: ;
		p = getconfigvar_170350(((NimStringDesc*) &TMP4862));
		{
			NI LOC23;
			if (!(0 < p->Sup.len)) goto LA21;
			LOC23 = 0;
			LOC23 = nsuParseInt(p);
			port = ((NU16) (LOC23));
		}
		goto LA19;
		LA21: ;
		{
			port = ((NU16) 6000);
		}
		LA19: ;
		LOC25 = 0;
		LOC25 = getconfigvar_170350(((NimStringDesc*) &TMP4863));
		bindaddr_185773(server, port, LOC25);
		inp = copyString(((NimStringDesc*) &TMP1004));
		listen_185608(server, SOMAXCONN);
		if (stdoutsocket_197318) nimGCunrefNoCycle(stdoutsocket_197318);
		stdoutsocket_197318 = (tsocketimpl183607*) newObjRC1((&NTI183609), sizeof(tsocketimpl183607));
		{
			while (1) {
				NimStringDesc* LOC28;
				accept_187401(server, &stdoutsocket_197318);
				readline_192447(stdoutsocket_197318, (&inp), -1);
				LOC28 = 0;
				LOC28 = curcaascmd_611204; curcaascmd_611204 = copyStringRC1(inp);
				if (LOC28) nimGCunrefNoCycle(LOC28);
				processcmdline_611209(((NU8) 1), inp);
				action();
				gdirtybufferidx_170134 = ((NI32) 0);
				gdirtyoriginalidx_170135 = ((NI32) 0);
				gerrorcounter_197306 = 0;
				send_193808(stdoutsocket_197318, ((NimStringDesc*) &TMP4871));
				close_187478(stdoutsocket_197318);
			}
		}
	}
	goto LA4;
	LA3: ;
	{
		printf("%s%s\015\012", (((NimStringDesc*) &TMP4872))->data, (typ)->data);
		exit(1);
	}
	LA4: ;
}
NIM_EXTERNC N_NOINLINE(void, HEX00_serviceInit)(void) {
	NimStringDesc* LOC1;
	NimStringDesc* LOC2;
	LOC1 = 0;
	LOC1 = curcaascmd_611204; curcaascmd_611204 = copyStringRC1(((NimStringDesc*) &TMP1004));
	if (LOC1) nimGCunrefNoCycle(LOC1);
	LOC2 = 0;
	LOC2 = lastcaascmd_611205; lastcaascmd_611205 = copyStringRC1(((NimStringDesc*) &TMP1004));
	if (LOC2) nimGCunrefNoCycle(LOC2);
}

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

