/* 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 <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 Tlineinfo188352 Tlineinfo188352;
typedef struct Tlinkedlist139028 Tlinkedlist139028;
typedef struct Tlistentry139022 Tlistentry139022;
typedef struct Stringtableobj144212 Stringtableobj144212;
typedef struct TNimObject TNimObject;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct Keyvaluepairseq144210 Keyvaluepairseq144210;
typedef struct Keyvaluepair144208 Keyvaluepair144208;
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct  Tlineinfo188352  {
NI16 line;
NI16 col;
NI32 fileindex;
};
struct  Tlinkedlist139028  {
Tlistentry139022* head;
Tlistentry139022* tail;
NI counter;
};
typedef NIM_CHAR TY113383[256];
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;
};
struct  TNimObject  {
TNimType* m_type;
};
struct  Stringtableobj144212  {
  TNimObject Sup;
NI counter;
Keyvaluepairseq144210* data;
NU8 mode;
};
struct Keyvaluepair144208 {
NimStringDesc* Field0;
NimStringDesc* Field1;
};
struct  Tlistentry139022  {
  TNimObject Sup;
Tlistentry139022* prev;
Tlistentry139022* next;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct Keyvaluepairseq144210 {
  TGenericSeq Sup;
  Keyvaluepair144208 data[SEQ_DECL_SIZE];
};
N_NIMCALL(NIM_BOOL, contains_139219)(Tlinkedlist139028* list, NimStringDesc* data);
N_NIMCALL(void, prependstr_139419)(Tlinkedlist139028* list, NimStringDesc* data);
N_NIMCALL(void, addpathrec_205258)(NimStringDesc* dir, Tlineinfo188352 info);
N_NIMCALL(Stringtableobj144212*, nstnewStringTable)(NU8 mode);
N_NIMCALL(NimStringDesc*, cstrToNimstr)(NCSTRING str);
static N_INLINE(NIM_BOOL, eqStrings)(NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(NimStringDesc*, HEX2F_122481)(NimStringDesc* head, NimStringDesc* tail);
N_NIMCALL(void, addpackage_205126)(Stringtableobj144212* packages, NimStringDesc* p);
N_NIMCALL(NI, versionsplitpos_205023)(NimStringDesc* s);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI start_81016, 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_205064)(NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(NI, npuParseInt)(NimStringDesc* s, NI* number, NI start);
N_NIMCALL(NimStringDesc*, getordefault_145020)(Stringtableobj144212* t, NimStringDesc* key);
N_NIMCALL(void, nstPut)(Stringtableobj144212* 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_205175)(NimStringDesc* p, Tlineinfo188352 info);
N_NIMCALL(void, message_194161)(Tlineinfo188352 info, NU16 msg, NimStringDesc* arg);
STRING_LITERAL(TMP1760, ".", 1);
STRING_LITERAL(TMP1761, "..", 2);
STRING_LITERAL(TMP1762, "head", 4);
extern Tlinkedlist139028 searchpaths_165136;
extern Tlinkedlist139028 lazypaths_165137;

N_NIMCALL(void, addpath_205012)(NimStringDesc* path, Tlineinfo188352 info) {
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = contains_139219((&searchpaths_165136), path);
		if (!!(LOC3)) goto LA4;
		prependstr_139419((&searchpaths_165136), 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_205023)(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_205064)(NimStringDesc* a, NimStringDesc* b) {
	NIM_BOOL result;
	NI i;
	NI j;
	NI vera;
	NI verb;
{	result = 0;
	{
		if (!eqStrings(a, ((NimStringDesc*) &TMP1762))) 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_205126)(Stringtableobj144212* packages, NimStringDesc* p) {
	NI x;
	NimStringDesc* name;
	x = versionsplitpos_205023(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_145020(packages, name);
			LOC8 = 0;
			LOC8 = HEX3CHEX2E_205064(LOC7, version);
			if (!LOC8) goto LA9;
			nstPut(packages, name, version);
		}
		LA9: ;
	}
	goto LA1;
	LA3: ;
	{
		nstPut(packages, name, ((NimStringDesc*) &TMP1762));
	}
	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_205175)(NimStringDesc* p, Tlineinfo188352 info) {
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = contains_139219((&searchpaths_165136), p);
		if (!!(LOC3)) goto LA4;
		message_194161(info, ((NU16) 280), p);
		prependstr_139419((&lazypaths_165137), p);
	}
	LA4: ;
}

N_NIMCALL(void, addpathrec_205258)(NimStringDesc* dir, Tlineinfo188352 info) {
	Stringtableobj144212* 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_205284;
		NimStringDesc* p_205285;
		DIR* d_205299;
		k_205284 = 0;
		p_205285 = 0;
		d_205299 = opendir(dir->data);
		{
			int LOC47;
			if (!!((d_205299 == NIM_NIL))) goto LA8;
			{
				while (1) {
					struct dirent* x_205301;
					NimStringDesc* y_205303;
					x_205301 = readdir(d_205299);
					{
						if (!(x_205301 == NIM_NIL)) goto LA14;
						goto LA10;
					}
					LA14: ;
					y_205303 = cstrToNimstr(((NCSTRING) ((*x_205301).d_name)));
					{
						NIM_BOOL LOC18;
						struct stat s_205305;
						NU8 k_205307;
						LOC18 = 0;
						LOC18 = !(eqStrings(y_205303, ((NimStringDesc*) &TMP1760)));
						if (!(LOC18)) goto LA19;
						LOC18 = !(eqStrings(y_205303, ((NimStringDesc*) &TMP1761)));
						LA19: ;
						if (!LOC18) goto LA20;
						memset((void*)(&s_205305), 0, sizeof(s_205305));
						{
							if (!NIM_TRUE) goto LA24;
							y_205303 = HEX2F_122481(dir, y_205303);
						}
						LA24: ;
						k_205307 = ((NU8) 0);
						{
							int LOC28;
							LOC28 = 0;
							LOC28 = lstat(y_205303->data, (&s_205305));
							if (!(LOC28 < ((NI32) 0))) goto LA29;
							goto LA10;
						}
						LA29: ;
						{
							NIM_BOOL LOC33;
							LOC33 = 0;
							LOC33 = S_ISDIR(s_205305.st_mode);
							if (!LOC33) goto LA34;
							k_205307 = ((NU8) 2);
						}
						LA34: ;
						{
							NIM_BOOL LOC38;
							LOC38 = 0;
							LOC38 = S_ISLNK(s_205305.st_mode);
							if (!LOC38) goto LA39;
							k_205307 = (NU8)(k_205307 + ((NI) 1));
						}
						LA39: ;
						k_205284 = k_205307;
						p_205285 = y_205303;
						{
							NIM_BOOL LOC43;
							LOC43 = 0;
							LOC43 = (k_205284 == ((NU8) 2));
							if (!(LOC43)) goto LA44;
							LOC43 = !(((NU8)(p_205285->data[pos]) == (NU8)(46)));
							LA44: ;
							if (!LOC43) goto LA45;
							addpackage_205126(packages, p_205285);
						}
						LA45: ;
					}
					LA20: ;
				}
			} LA10: ;
			LOC47 = 0;
			LOC47 = closedir(d_205299);
		}
		LA8: ;
	}
	{
		NimStringDesc* p_205295;
		p_205295 = 0;
		{
			NimStringDesc* key_205313;
			NimStringDesc* val_205315;
			key_205313 = 0;
			val_205315 = 0;
			{
				NI h_205317;
				NI HEX3Atmp_205319;
				NI res_205321;
				h_205317 = 0;
				HEX3Atmp_205319 = 0;
				HEX3Atmp_205319 = ((*packages).data ? ((*packages).data->Sup.len-1) : -1);
				res_205321 = ((NI) 0);
				{
					while (1) {
						if (!(res_205321 <= HEX3Atmp_205319)) goto LA52;
						h_205317 = res_205321;
						{
							NimStringDesc* res_205325;
							if (!!((*packages).data->data[h_205317].Field0 == 0)) goto LA55;
							key_205313 = (*packages).data->data[h_205317].Field0;
							val_205315 = (*packages).data->data[h_205317].Field1;
							{
								if (!eqStrings(val_205315, ((NimStringDesc*) &TMP1762))) goto LA59;
								res_205325 = key_205313;
							}
							goto LA57;
							LA59: ;
							{
								NimStringDesc* LOC62;
								LOC62 = 0;
								LOC62 = rawNewString(key_205313->Sup.len + val_205315->Sup.len + 1);
appendString(LOC62, key_205313);
appendChar(LOC62, 45);
appendString(LOC62, val_205315);
								res_205325 = LOC62;
							}
							LA57: ;
							p_205295 = res_205325;
							addnimblepath_205175(p_205295, info);
						}
						LA55: ;
						res_205321 += ((NI) 1);
					} LA52: ;
				}
			}
		}
	}
}

N_NIMCALL(void, nimblepath_205356)(NimStringDesc* path, Tlineinfo188352 info) {
	addpathrec_205258(path, info);
	addnimblepath_205175(path, info);
}
NIM_EXTERNC N_NOINLINE(void, compiler_nimblecmdInit000)(void) {
}

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

