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

#include "nimbase.h"
#include <dirent.h>
#include <sys/types.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct Tlineinfo182337 Tlineinfo182337;
typedef struct Tlinkedlist135016 Tlinkedlist135016;
typedef struct Tlistentry135010 Tlistentry135010;
typedef struct Stringtableobj140209 Stringtableobj140209;
typedef struct TNimObject TNimObject;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct Keyvaluepairseq140207 Keyvaluepairseq140207;
typedef struct Keyvaluepair140205 Keyvaluepair140205;
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct  Tlineinfo182337  {
NI16 line;
NI16 col;
NI32 fileindex;
};
struct  Tlinkedlist135016  {
Tlistentry135010* head;
Tlistentry135010* tail;
NI counter;
};
typedef NIM_CHAR TY110368[256];
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;
};
struct  TNimObject  {
TNimType* m_type;
};
struct  Stringtableobj140209  {
  TNimObject Sup;
NI counter;
Keyvaluepairseq140207* data;
NU8 mode;
};
struct Keyvaluepair140205 {
NimStringDesc* Field0;
NimStringDesc* Field1;
};
struct  Tlistentry135010  {
  TNimObject Sup;
Tlistentry135010* prev;
Tlistentry135010* next;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct Keyvaluepairseq140207 {
  TGenericSeq Sup;
  Keyvaluepair140205 data[SEQ_DECL_SIZE];
};
N_NIMCALL(NIM_BOOL, contains_135213)(Tlinkedlist135016* list, NimStringDesc* data);
N_NIMCALL(void, prependstr_135413)(Tlinkedlist135016* list, NimStringDesc* data);
N_NIMCALL(void, addpathrec_197182)(NimStringDesc* dir, Tlineinfo182337 info);
N_NIMCALL(Stringtableobj140209*, nstnewStringTable)(NU8 mode);
N_NIMCALL(NimStringDesc*, cstrToNimstr)(NCSTRING str);
static N_INLINE(NIM_BOOL, eqStrings)(NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(NimStringDesc*, HEX2F_118465)(NimStringDesc* head, NimStringDesc* tail);
N_NIMCALL(void, addpackage_197084)(Stringtableobj140209* packages, NimStringDesc* p);
N_NIMCALL(NI, versionsplitpos_197008)(NimStringDesc* s);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI start_79810, NI last);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI first, NI last);
N_NIMCALL(NimStringDesc*, copyStr)(NimStringDesc* s, NI start);
N_NIMCALL(NimStringDesc*, copyStr)(NimStringDesc* s, NI first);
N_NIMCALL(NIM_BOOL, HEX3CHEX2E_197034)(NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(NI, npuParseInt)(NimStringDesc* s, NI* number, NI start);
N_NIMCALL(NimStringDesc*, getordefault_140814)(Stringtableobj140209* t, NimStringDesc* key);
N_NIMCALL(void, nstPut)(Stringtableobj140209* t, NimStringDesc* key, NimStringDesc* val);
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src);
static N_INLINE(void, appendChar)(NimStringDesc* dest, NIM_CHAR c);
N_NIMCALL(NimStringDesc*, rawNewString)(NI space);
N_NIMCALL(void, addnimblepath_197108)(NimStringDesc* p, Tlineinfo182337 info);
N_NIMCALL(void, message_187095)(Tlineinfo182337 info, NU16 msg, NimStringDesc* arg);
STRING_LITERAL(TMP1794, ".", 1);
STRING_LITERAL(TMP1795, "..", 2);
STRING_LITERAL(TMP1796, "head", 4);
extern Tlinkedlist135016 searchpaths_160125;
extern Tlinkedlist135016 lazypaths_160126;

N_NIMCALL(void, addpath_197003)(NimStringDesc* path, Tlineinfo182337 info) {
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = contains_135213((&searchpaths_160125), path);
		if (!!(LOC3)) goto LA4;
		prependstr_135413((&searchpaths_160125), path);
	}
	LA4: ;
}

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)), (*a).Sup.len);
	LOC11 = (LOC13 == ((NI32) 0));
	LA12: ;
	result = LOC11;
	goto BeforeRet;
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NI, versionsplitpos_197008)(NimStringDesc* s) {
	NI result;
	result = 0;
	result = (NI)((s ? s->Sup.len : 0) - ((NI) 2));
	{
		while (1) {
			NIM_BOOL LOC3;
			LOC3 = 0;
			LOC3 = (((NI) 1) < result);
			if (!(LOC3)) goto LA4;
			LOC3 = (((NU8)(s->data[result])) >= ((NU8)(48)) && ((NU8)(s->data[result])) <= ((NU8)(57)) || ((NU8)(s->data[result])) == ((NU8)(46)));
			LA4: ;
			if (!LOC3) goto LA2;
			result -= ((NI) 1);
		} LA2: ;
	}
	{
		if (!!(((NU8)(s->data[result]) == (NU8)(45)))) goto LA7;
		result = (s ? s->Sup.len : 0);
	}
	LA7: ;
	return result;
}

N_NIMCALL(NIM_BOOL, HEX3CHEX2E_197034)(NimStringDesc* a, NimStringDesc* b) {
	NIM_BOOL result;
	NI i;
	NI j;
	NI vera;
	NI verb;
{	result = 0;
	{
		if (!eqStrings(a, ((NimStringDesc*) &TMP1796))) goto LA3;
		result = NIM_FALSE;
		goto BeforeRet;
	}
	LA3: ;
	i = ((NI) 0);
	j = ((NI) 0);
	vera = ((NI) 0);
	verb = ((NI) 0);
	{
		while (1) {
			NI ii;
			NI jj;
			ii = npuParseInt(a, (&vera), i);
			jj = npuParseInt(b, (&verb), j);
			{
				NIM_BOOL LOC9;
				LOC9 = 0;
				LOC9 = (ii <= ((NI) 0));
				if (LOC9) goto LA10;
				LOC9 = (jj <= ((NI) 0));
				LA10: ;
				if (!LOC9) goto LA11;
				result = (((NI) 0) < jj);
				goto BeforeRet;
			}
			LA11: ;
			{
				if (!(vera < verb)) goto LA15;
				result = NIM_TRUE;
				goto BeforeRet;
			}
			goto LA13;
			LA15: ;
			{
				if (!(verb < vera)) goto LA18;
				result = NIM_FALSE;
				goto BeforeRet;
			}
			goto LA13;
			LA18: ;
			LA13: ;
			i += ii;
			j += jj;
			{
				if (!((NU8)(a->data[i]) == (NU8)(46))) goto LA22;
				i += ((NI) 1);
			}
			LA22: ;
			{
				if (!((NU8)(b->data[j]) == (NU8)(46))) goto LA26;
				j += ((NI) 1);
			}
			LA26: ;
		}
	}
	}BeforeRet: ;
	return result;
}

N_NIMCALL(void, addpackage_197084)(Stringtableobj140209* packages, NimStringDesc* p) {
	NI x;
	NimStringDesc* name;
	x = versionsplitpos_197008(p);
	name = copyStrLast(p, ((NI) 0), (NI)(x - ((NI) 1)));
	{
		NimStringDesc* version;
		if (!(x < (p ? p->Sup.len : 0))) goto LA3;
		version = copyStr(p, (NI)(x + ((NI) 1)));
		{
			NimStringDesc* LOC7;
			NIM_BOOL LOC8;
			LOC7 = 0;
			LOC7 = getordefault_140814(packages, name);
			LOC8 = 0;
			LOC8 = HEX3CHEX2E_197034(LOC7, version);
			if (!LOC8) goto LA9;
			nstPut(packages, name, version);
		}
		LA9: ;
	}
	goto LA1;
	LA3: ;
	{
		nstPut(packages, name, ((NimStringDesc*) &TMP1796));
	}
	LA1: ;
}

static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src) {
	memcpy(((NCSTRING) ((&(*dest).data[((*dest).Sup.len)- 0]))), ((NCSTRING) ((*src).data)), (NI)((*src).Sup.len + ((NI) 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[((NI)((*dest).Sup.len + ((NI) 1)))- 0] = 0;
	(*dest).Sup.len += ((NI) 1);
}

N_NIMCALL(void, addnimblepath_197108)(NimStringDesc* p, Tlineinfo182337 info) {
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = contains_135213((&searchpaths_160125), p);
		if (!!(LOC3)) goto LA4;
		message_187095(info, ((NU16) 280), p);
		prependstr_135413((&lazypaths_160126), p);
	}
	LA4: ;
}

N_NIMCALL(void, addpathrec_197182)(NimStringDesc* dir, Tlineinfo182337 info) {
	Stringtableobj140209* packages;
	NI pos;
	packages = nstnewStringTable(((NU8) 2));
	pos = (NI)((dir ? dir->Sup.len : 0) - ((NI) 1));
	{
		if (!(((NU8)(dir->data[pos])) == ((NU8)(47)) || ((NU8)(dir->data[pos])) == ((NU8)(47)))) goto LA3;
		pos += ((NI) 1);
	}
	LA3: ;
	{
		NU8 k_197208;
		NimStringDesc* p_197209;
		DIR* d_197235;
		k_197208 = 0;
		p_197209 = 0;
		d_197235 = opendir(dir->data);
		{
			int LOC47;
			if (!!((d_197235 == NIM_NIL))) goto LA8;
			{
				while (1) {
					struct dirent* x_197237;
					NimStringDesc* y_197239;
					x_197237 = readdir(d_197235);
					{
						if (!(x_197237 == NIM_NIL)) goto LA14;
						goto LA10;
					}
					LA14: ;
					y_197239 = cstrToNimstr(((NCSTRING) ((*x_197237).d_name)));
					{
						NIM_BOOL LOC18;
						struct stat s_197241;
						NU8 k_197243;
						LOC18 = 0;
						LOC18 = !(eqStrings(y_197239, ((NimStringDesc*) &TMP1794)));
						if (!(LOC18)) goto LA19;
						LOC18 = !(eqStrings(y_197239, ((NimStringDesc*) &TMP1795)));
						LA19: ;
						if (!LOC18) goto LA20;
						memset((void*)(&s_197241), 0, sizeof(s_197241));
						{
							if (!NIM_TRUE) goto LA24;
							y_197239 = HEX2F_118465(dir, y_197239);
						}
						LA24: ;
						k_197243 = ((NU8) 0);
						{
							int LOC28;
							LOC28 = 0;
							LOC28 = lstat(y_197239->data, (&s_197241));
							if (!(LOC28 < ((NI32) 0))) goto LA29;
							goto LA10;
						}
						LA29: ;
						{
							NIM_BOOL LOC33;
							LOC33 = 0;
							LOC33 = S_ISDIR(s_197241.st_mode);
							if (!LOC33) goto LA34;
							k_197243 = ((NU8) 2);
						}
						LA34: ;
						{
							NIM_BOOL LOC38;
							LOC38 = 0;
							LOC38 = S_ISLNK(s_197241.st_mode);
							if (!LOC38) goto LA39;
							k_197243 = (NU8)(k_197243 + ((NI) 1));
						}
						LA39: ;
						k_197208 = k_197243;
						p_197209 = y_197239;
						{
							NIM_BOOL LOC43;
							LOC43 = 0;
							LOC43 = (k_197208 == ((NU8) 2));
							if (!(LOC43)) goto LA44;
							LOC43 = !(((NU8)(p_197209->data[pos]) == (NU8)(46)));
							LA44: ;
							if (!LOC43) goto LA45;
							addpackage_197084(packages, p_197209);
						}
						LA45: ;
					}
					LA20: ;
				}
			} LA10: ;
			LOC47 = 0;
			LOC47 = closedir(d_197235);
		}
		LA8: ;
	}
	{
		NimStringDesc* p_197219;
		p_197219 = 0;
		{
			NimStringDesc* key_197249;
			NimStringDesc* val_197251;
			key_197249 = 0;
			val_197251 = 0;
			{
				NI h_197253;
				NI HEX3Atmp_197255;
				NI res_197257;
				h_197253 = 0;
				HEX3Atmp_197255 = 0;
				HEX3Atmp_197255 = ((*packages).data ? ((*packages).data->Sup.len-1) : -1);
				res_197257 = ((NI) 0);
				{
					while (1) {
						if (!(res_197257 <= HEX3Atmp_197255)) goto LA52;
						h_197253 = res_197257;
						{
							NimStringDesc* res_197261;
							if (!!((*packages).data->data[h_197253].Field0 == 0)) goto LA55;
							key_197249 = (*packages).data->data[h_197253].Field0;
							val_197251 = (*packages).data->data[h_197253].Field1;
							{
								if (!eqStrings(val_197251, ((NimStringDesc*) &TMP1796))) goto LA59;
								res_197261 = key_197249;
							}
							goto LA57;
							LA59: ;
							{
								NimStringDesc* LOC62;
								LOC62 = 0;
								LOC62 = rawNewString(key_197249->Sup.len + val_197251->Sup.len + 1);
appendString(LOC62, key_197249);
appendChar(LOC62, 45);
appendString(LOC62, val_197251);
								res_197261 = LOC62;
							}
							LA57: ;
							p_197219 = res_197261;
							addnimblepath_197108(p_197219, info);
						}
						LA55: ;
						res_197257 += ((NI) 1);
					} LA52: ;
				}
			}
		}
	}
}

N_NIMCALL(void, nimblepath_197262)(NimStringDesc* path, Tlineinfo182337 info) {
	addpathrec_197182(path, info);
	addnimblepath_197108(path, info);
}
NIM_EXTERNC N_NOINLINE(void, compiler_nimblecmdInit000)(void) {
}

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

