/* Generated by Nimrod Compiler v0.9.5 */
/*   (c) 2014 Andreas Rumpf */
/* The generated code is subject to the original license. */
/* Compiled for: Linux, i386, gcc */
/* Command for C compiler:
   gcc -c  -w -O3 -fno-strict-aliasing  -I/build/nimrod/src/nimrod/lib -o compiler/nimcache/stdlib_parseopt.o compiler/nimcache/stdlib_parseopt.c */
#define NIM_INTBITS 32
#include "nimbase.h"

#include <string.h>
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct toptparser521807 toptparser521807;
typedef struct TNimObject TNimObject;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
typedef N_NIMCALL_PTR(void, TY2289) (void* p, NI op);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY2289 marker;
};
struct  TNimObject  {
TNimType* m_type;
};
struct  toptparser521807  {
  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 NU8 TY90161[32];
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NIMCALL(NI, paramcount_114233)(void);
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src);
N_NIMCALL(NimStringDesc*, quoteifcontainswhite_90237)(NimStringDesc* s);
N_NIMCALL(NimStringDesc*, paramstr_114205)(NI i);
static N_INLINE(void, appendChar)(NimStringDesc* dest, NIM_CHAR c);
N_NIMCALL(NimStringDesc*, rawNewString)(NI space);
N_NIMCALL(NimStringDesc*, setLengthStr)(NimStringDesc* s, NI newlen);
N_NIMCALL(void, handleshortoption_522106)(toptparser521807* p);
N_NIMCALL(NimStringDesc*, addChar)(NimStringDesc* s, NIM_CHAR c);
N_NIMCALL(NI, parseword_522002)(NimStringDesc* s, NI i, NimStringDesc** w, TY90161 delim);
N_NIMCALL(NimStringDesc*, nsuStrip)(NimStringDesc* s, NIM_BOOL leading, NIM_BOOL trailing);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI start_70624, NI last);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI first, NI last);
STRING_LITERAL(TMP1014, "", 0);
STRING_LITERAL(TMP1018, "", 0);
static NIM_CONST TY90161 TMP1023 = {
0x01, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
;
static NIM_CONST TY90161 TMP1024 = {
0x01, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x24,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
;
extern TNimType NTI2409; /* TObject */
TNimType NTI521807; /* TOptParser */
extern TNimType NTI143; /* string */
extern TNimType NTI106; /* int */
extern TNimType NTI134; /* bool */
TNimType NTI521805; /* TCmdLineKind */

static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src) {
	memcpy(((NCSTRING) (&(*dest).data[((*dest).Sup.len)- 0])), ((NCSTRING) ((*src).data)), (NI32)((*src).Sup.len + 1));
	(*dest).Sup.len += (*src).Sup.len;
}

static N_INLINE(void, appendChar)(NimStringDesc* dest, NIM_CHAR c) {
	(*dest).data[((*dest).Sup.len)- 0] = c;
	(*dest).data[((NI32)((*dest).Sup.len + 1))- 0] = 0;
	(*dest).Sup.len += 1;
}

N_NIMCALL(void, initoptparser_521819)(NimStringDesc* cmdline, toptparser521807* Result) {
	(*Result).Pos = 0;
	(*Result).Inshortstate = NIM_FALSE;
	{
		if (!!(((cmdline) && (cmdline)->Sup.len == 0))) goto LA3;
		unsureAsgnRef((void**) &(*Result).Cmd, copyString(cmdline));
	}
	goto LA1;
	LA3: ;
	{
		NI i_521835;
		NI HEX3Atmp_521837;
		NI res_521839;
		unsureAsgnRef((void**) &(*Result).Cmd, copyString(((NimStringDesc*) &TMP1014)));
		i_521835 = 0;
		HEX3Atmp_521837 = 0;
		HEX3Atmp_521837 = paramcount_114233();
		res_521839 = 1;
		while (1) {
			NimStringDesc* LOC7;
			NimStringDesc* LOC8;
			NimStringDesc* LOC9;
			if (!(res_521839 <= HEX3Atmp_521837)) goto LA6;
			i_521835 = res_521839;
			LOC7 = 0;
			LOC8 = 0;
			LOC8 = paramstr_114205(i_521835);
			LOC9 = 0;
			LOC9 = quoteifcontainswhite_90237(LOC8);
			LOC7 = rawNewString((*Result).Cmd->Sup.len + LOC9->Sup.len + 1);
appendString(LOC7, (*Result).Cmd);
appendString(LOC7, LOC9);
appendChar(LOC7, 32);
			unsureAsgnRef((void**) &(*Result).Cmd, LOC7);
			res_521839 += 1;
		} LA6: ;
	}
	LA1: ;
	(*Result).Kind = ((NU8) 0);
	unsureAsgnRef((void**) &(*Result).Key, copyString(((NimStringDesc*) &TMP1018)));
	unsureAsgnRef((void**) &(*Result).Val, copyString(((NimStringDesc*) &TMP1018)));
}

N_NIMCALL(NI, parseword_522002)(NimStringDesc* s, NI i, NimStringDesc** w, TY90161 delim) {
	NI result;
	result = 0;
	result = i;
	{
		if (!((NU8)(s->data[result]) == (NU8)(34))) goto LA3;
		result += 1;
		while (1) {
			if (!!((((NU8)(s->data[result])) == ((NU8)(0)) || ((NU8)(s->data[result])) == ((NU8)(34))))) goto LA5;
			(*w) = addChar((*w), s->data[result]);
			result += 1;
		} LA5: ;
		{
			if (!((NU8)(s->data[result]) == (NU8)(34))) goto LA8;
			result += 1;
		}
		LA8: ;
	}
	goto LA1;
	LA3: ;
	{
		while (1) {
			if (!!(((delim[((NU8)(s->data[result]))/8] &(1<<(((NU8)(s->data[result]))%8)))!=0))) goto LA11;
			(*w) = addChar((*w), s->data[result]);
			result += 1;
		} LA11: ;
	}
	LA1: ;
	return result;
}

N_NIMCALL(void, handleshortoption_522106)(toptparser521807* p) {
	NI i;
	i = (*p).Pos;
	(*p).Kind = ((NU8) 3);
	(*p).Key = addChar((*p).Key, (*p).Cmd->data[i]);
	i += 1;
	(*p).Inshortstate = NIM_TRUE;
	while (1) {
		if (!(((NU8)((*p).Cmd->data[i])) == ((NU8)(9)) || ((NU8)((*p).Cmd->data[i])) == ((NU8)(32)))) goto LA1;
		i += 1;
		(*p).Inshortstate = NIM_FALSE;
	} LA1: ;
	{
		if (!(((NU8)((*p).Cmd->data[i])) == ((NU8)(58)) || ((NU8)((*p).Cmd->data[i])) == ((NU8)(61)))) goto LA4;
		i += 1;
		(*p).Inshortstate = NIM_FALSE;
		while (1) {
			if (!(((NU8)((*p).Cmd->data[i])) == ((NU8)(9)) || ((NU8)((*p).Cmd->data[i])) == ((NU8)(32)))) goto LA6;
			i += 1;
		} LA6: ;
		i = parseword_522002((*p).Cmd, i, &(*p).Val, TMP1023);
	}
	LA4: ;
	{
		if (!((NU8)((*p).Cmd->data[i]) == (NU8)(0))) goto LA9;
		(*p).Inshortstate = NIM_FALSE;
	}
	LA9: ;
	(*p).Pos = i;
}

N_NIMCALL(void, nponext)(toptparser521807* p) {
	NI i;
	i = (*p).Pos;
	while (1) {
		if (!(((NU8)((*p).Cmd->data[i])) == ((NU8)(9)) || ((NU8)((*p).Cmd->data[i])) == ((NU8)(32)))) goto LA1;
		i += 1;
	} LA1: ;
	(*p).Pos = i;
	(*p).Key = setLengthStr((*p).Key, 0);
	(*p).Val = setLengthStr((*p).Val, 0);
	{
		if (!(*p).Inshortstate) goto LA4;
		handleshortoption_522106(p);
		goto BeforeRet;
	}
	LA4: ;
	switch (((NU8)((*p).Cmd->data[i]))) {
	case 0:
	{
		(*p).Kind = ((NU8) 0);
	}
	break;
	case 45:
	{
		i += 1;
		{
			if (!((NU8)((*p).Cmd->data[i]) == (NU8)(45))) goto LA10;
			(*p).Kind = ((NU8) 2);
			i += 1;
			i = parseword_522002((*p).Cmd, i, &(*p).Key, TMP1024);
			while (1) {
				if (!(((NU8)((*p).Cmd->data[i])) == ((NU8)(9)) || ((NU8)((*p).Cmd->data[i])) == ((NU8)(32)))) goto LA12;
				i += 1;
			} LA12: ;
			{
				if (!(((NU8)((*p).Cmd->data[i])) == ((NU8)(58)) || ((NU8)((*p).Cmd->data[i])) == ((NU8)(61)))) goto LA15;
				i += 1;
				while (1) {
					if (!(((NU8)((*p).Cmd->data[i])) == ((NU8)(9)) || ((NU8)((*p).Cmd->data[i])) == ((NU8)(32)))) goto LA17;
					i += 1;
				} LA17: ;
				(*p).Pos = parseword_522002((*p).Cmd, i, &(*p).Val, TMP1023);
			}
			goto LA13;
			LA15: ;
			{
				(*p).Pos = i;
			}
			LA13: ;
		}
		goto LA8;
		LA10: ;
		{
			(*p).Pos = i;
			handleshortoption_522106(p);
		}
		LA8: ;
	}
	break;
	default:
	{
		(*p).Kind = ((NU8) 1);
		(*p).Pos = parseword_522002((*p).Cmd, i, &(*p).Key, TMP1023);
	}
	break;
	}
	BeforeRet: ;
}

N_NIMCALL(NimStringDesc*, npocmdLineRest)(toptparser521807* p) {
	NimStringDesc* result;
	NimStringDesc* LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = copyStrLast((*p).Cmd, (*p).Pos, (NI32)((*p).Cmd->Sup.len - 1));
	result = nsuStrip(LOC1, NIM_TRUE, NIM_TRUE);
	return result;
}
N_NOINLINE(void, stdlibparseoptInit)(void) {
}

N_NOINLINE(void, stdlibparseoptDatInit)(void) {
static TNimNode* TMP1019[6];
static TNimNode* TMP1020[4];
NI TMP1022;
static char* NIM_CONST TMP1021[4] = {
"cmdEnd", 
"cmdArgument", 
"cmdLongoption", 
"cmdShortOption"};
static TNimNode TMP972[12];
NTI521807.size = sizeof(toptparser521807);
NTI521807.kind = 17;
NTI521807.base = (&NTI2409);
TMP1019[0] = &TMP972[1];
TMP972[1].kind = 1;
TMP972[1].offset = offsetof(toptparser521807, Cmd);
TMP972[1].typ = (&NTI143);
TMP972[1].name = "cmd";
TMP1019[1] = &TMP972[2];
TMP972[2].kind = 1;
TMP972[2].offset = offsetof(toptparser521807, Pos);
TMP972[2].typ = (&NTI106);
TMP972[2].name = "pos";
TMP1019[2] = &TMP972[3];
TMP972[3].kind = 1;
TMP972[3].offset = offsetof(toptparser521807, Inshortstate);
TMP972[3].typ = (&NTI134);
TMP972[3].name = "inShortState";
TMP1019[3] = &TMP972[4];
NTI521805.size = sizeof(NU8);
NTI521805.kind = 14;
NTI521805.base = 0;
NTI521805.flags = 3;
for (TMP1022 = 0; TMP1022 < 4; TMP1022++) {
TMP972[TMP1022+5].kind = 1;
TMP972[TMP1022+5].offset = TMP1022;
TMP972[TMP1022+5].name = TMP1021[TMP1022];
TMP1020[TMP1022] = &TMP972[TMP1022+5];
}
TMP972[9].len = 4; TMP972[9].kind = 2; TMP972[9].sons = &TMP1020[0];
NTI521805.node = &TMP972[9];
TMP972[4].kind = 1;
TMP972[4].offset = offsetof(toptparser521807, Kind);
TMP972[4].typ = (&NTI521805);
TMP972[4].name = "kind";
TMP1019[4] = &TMP972[10];
TMP972[10].kind = 1;
TMP972[10].offset = offsetof(toptparser521807, Key);
TMP972[10].typ = (&NTI143);
TMP972[10].name = "key";
TMP1019[5] = &TMP972[11];
TMP972[11].kind = 1;
TMP972[11].offset = offsetof(toptparser521807, Val);
TMP972[11].typ = (&NTI143);
TMP972[11].name = "val";
TMP972[0].len = 6; TMP972[0].kind = 2; TMP972[0].sons = &TMP1019[0];
NTI521807.node = &TMP972[0];
}

