/* 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/babelcmd.o compiler/nimcache/babelcmd.c */
#define NIM_INTBITS 32
#include "nimbase.h"

#include <dirent.h>

#include <sys/types.h>

#include <string.h>

#include <sys/stat.h>

#include <sys/time.h>
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct tlineinfo163527 tlineinfo163527;
typedef struct tlinkedlist118023 tlinkedlist118023;
typedef struct tlistentry118017 tlistentry118017;
typedef struct tstringtable121810 tstringtable121810;
typedef struct TNimObject TNimObject;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct tkeyvaluepairseq121808 tkeyvaluepairseq121808;
typedef struct tkeyvaluepair121806 tkeyvaluepair121806;
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct  tlineinfo163527  {
NI16 Line;
NI16 Col;
NI32 Fileindex;
};
struct  tlinkedlist118023  {
tlistentry118017* Head;
tlistentry118017* Tail;
NI Counter;
};
typedef NIM_CHAR TY97549[256];
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 tkeyvaluepair121806 {
NimStringDesc* Field0;
NimStringDesc* Field1;
};
struct  tstringtable121810  {
  TNimObject Sup;
NI Counter;
tkeyvaluepairseq121808* Data;
NU8 Mode;
};
struct  tlistentry118017  {
  TNimObject Sup;
tlistentry118017* Prev;
tlistentry118017* Next;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct tkeyvaluepairseq121808 {
  TGenericSeq Sup;
  tkeyvaluepair121806 data[SEQ_DECL_SIZE];
};
N_NIMCALL(NIM_BOOL, contains_118220)(tlinkedlist118023 list, NimStringDesc* data);
N_NIMCALL(void, prependstr_118420)(tlinkedlist118023* list, NimStringDesc* data);
N_NIMCALL(void, addpathrec_172216)(NimStringDesc* dir, tlineinfo163527 info);
N_NIMCALL(tstringtable121810*, nstnewStringTable)(NU8 mode);
N_NIMCALL(NimStringDesc*, cstrToNimstr)(NCSTRING str);
static N_INLINE(NIM_BOOL, eqStrings)(NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(NimStringDesc*, HEX2F_108477)(NimStringDesc* head, NimStringDesc* tail);
N_NIMCALL(void, addpackage_172137)(tstringtable121810* packages, NimStringDesc* p);
N_NIMCALL(NI, versionsplitpos_172014)(NimStringDesc* s);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI start_70624, 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_172054)(NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(NI, npuParseInt)(NimStringDesc* s, NI* number, NI start);
N_NIMCALL(NimStringDesc*, nstGet)(tstringtable121810* t, NimStringDesc* key);
N_NIMCALL(void, nstPut)(tstringtable121810* 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, addbabelpath_172163)(NimStringDesc* p, tlineinfo163527 info);
N_NIMCALL(void, message_165584)(tlineinfo163527 info, NU16 msg, NimStringDesc* arg);
STRING_LITERAL(TMP1162, ".", 1);
STRING_LITERAL(TMP1163, "..", 2);
STRING_LITERAL(TMP1164, "head", 4);
extern tlinkedlist118023 searchpaths_137115;
extern NI gverbosity_137120;
extern tlinkedlist118023 lazypaths_137116;

N_NIMCALL(void, addpath_172007)(NimStringDesc* path, tlineinfo163527 info) {
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = contains_118220(searchpaths_137115, path);
		if (!!(LOC3)) goto LA4;
		prependstr_118420(&searchpaths_137115, 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)), (NI32)((*a).Sup.len * 1));
	LOC11 = (LOC13 == ((NI32) 0));
	LA12: ;
	result = LOC11;
	goto BeforeRet;
	BeforeRet: ;
	return result;
}

N_NIMCALL(NI, versionsplitpos_172014)(NimStringDesc* s) {
	NI result;
	result = 0;
	result = (NI32)(s->Sup.len - 2);
	while (1) {
		NIM_BOOL LOC2;
		LOC2 = 0;
		LOC2 = (1 < result);
		if (!(LOC2)) goto LA3;
		LOC2 = (((NU8)(s->data[result])) >= ((NU8)(48)) && ((NU8)(s->data[result])) <= ((NU8)(57)) || ((NU8)(s->data[result])) == ((NU8)(46)));
		LA3: ;
		if (!LOC2) goto LA1;
		result -= 1;
	} LA1: ;
	{
		if (!!(((NU8)(s->data[result]) == (NU8)(45)))) goto LA6;
		result = s->Sup.len;
	}
	LA6: ;
	return result;
}

N_NIMCALL(NIM_BOOL, HEX3CHEX2E_172054)(NimStringDesc* a, NimStringDesc* b) {
	NIM_BOOL result;
	NI i;
	NI j;
	NI vera;
	NI verb;
	result = 0;
	{
		if (!eqStrings(a, ((NimStringDesc*) &TMP1164))) goto LA3;
		result = NIM_FALSE;
		goto BeforeRet;
	}
	LA3: ;
	i = 0;
	j = 0;
	vera = 0;
	verb = 0;
	while (1) {
		NI ii;
		NI jj;
		ii = npuParseInt(a, &vera, i);
		jj = npuParseInt(b, &verb, j);
		{
			NIM_BOOL LOC8;
			LOC8 = 0;
			LOC8 = (ii <= 0);
			if (LOC8) goto LA9;
			LOC8 = (jj <= 0);
			LA9: ;
			if (!LOC8) goto LA10;
			result = (0 < jj);
			goto BeforeRet;
		}
		LA10: ;
		{
			if (!(vera < verb)) goto LA14;
			result = NIM_TRUE;
			goto BeforeRet;
		}
		goto LA12;
		LA14: ;
		{
			if (!(verb < vera)) goto LA17;
			result = NIM_FALSE;
			goto BeforeRet;
		}
		goto LA12;
		LA17: ;
		LA12: ;
		i += ii;
		j += jj;
		{
			if (!((NU8)(a->data[i]) == (NU8)(46))) goto LA21;
			i += 1;
		}
		LA21: ;
		{
			if (!((NU8)(b->data[j]) == (NU8)(46))) goto LA25;
			j += 1;
		}
		LA25: ;
	}
	BeforeRet: ;
	return result;
}

N_NIMCALL(void, addpackage_172137)(tstringtable121810* packages, NimStringDesc* p) {
	NI x;
	NimStringDesc* name;
	x = versionsplitpos_172014(p);
	name = copyStrLast(p, 0, (NI32)(x - 1));
	{
		NimStringDesc* version;
		if (!(x < p->Sup.len)) goto LA3;
		version = copyStr(p, (NI32)(x + 1));
		{
			NimStringDesc* LOC7;
			NIM_BOOL LOC8;
			LOC7 = 0;
			LOC7 = nstGet(packages, name);
			LOC8 = 0;
			LOC8 = HEX3CHEX2E_172054(LOC7, version);
			if (!LOC8) goto LA9;
			nstPut(packages, name, version);
		}
		LA9: ;
	}
	goto LA1;
	LA3: ;
	{
		nstPut(packages, name, ((NimStringDesc*) &TMP1164));
	}
	LA1: ;
}

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, addbabelpath_172163)(NimStringDesc* p, tlineinfo163527 info) {
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = contains_118220(searchpaths_137115, p);
		if (!!(LOC3)) goto LA4;
		{
			if (!(1 <= gverbosity_137120)) goto LA8;
			message_165584(info, ((NU16) 269), p);
		}
		LA8: ;
		prependstr_118420(&lazypaths_137116, p);
	}
	LA4: ;
}

N_NIMCALL(void, addpathrec_172216)(NimStringDesc* dir, tlineinfo163527 info) {
	tstringtable121810* packages;
	NI pos;
	NU8 k_172252;
	NimStringDesc* p_172253;
	DIR* d_172265;
	NimStringDesc* p_172263;
	NimStringDesc* key_172275;
	NimStringDesc* val_172277;
	NI h_172279;
	NI HEX3Atmp_172281;
	NI res_172283;
	packages = nstnewStringTable(((NU8) 2));
	pos = (NI32)(dir->Sup.len - 1);
	{
		if (!(((NU8)(dir->data[pos])) == ((NU8)(47)) || ((NU8)(dir->data[pos])) == ((NU8)(47)))) goto LA3;
		pos += 1;
	}
	LA3: ;
	k_172252 = 0;
	p_172253 = 0;
	d_172265 = opendir(dir->data);
	{
		int LOC41;
		if (!!((d_172265 == NIM_NIL))) goto LA7;
		while (1) {
			struct dirent* x_172267;
			NimStringDesc* y_172269;
			x_172267 = readdir(d_172265);
			{
				if (!(x_172267 == NIM_NIL)) goto LA12;
				goto LA9;
			}
			LA12: ;
			y_172269 = cstrToNimstr(((NCSTRING) ((*x_172267).d_name)));
			{
				NIM_BOOL LOC16;
				struct stat s_172271;
				NU8 k_172273;
				LOC16 = 0;
				LOC16 = !(eqStrings(y_172269, ((NimStringDesc*) &TMP1162)));
				if (!(LOC16)) goto LA17;
				LOC16 = !(eqStrings(y_172269, ((NimStringDesc*) &TMP1163)));
				LA17: ;
				if (!LOC16) goto LA18;
				memset((void*)&s_172271, 0, sizeof(s_172271));
				y_172269 = HEX2F_108477(dir, y_172269);
				{
					int LOC22;
					LOC22 = 0;
					LOC22 = lstat(y_172269->data, &s_172271);
					if (!(LOC22 < ((NI32) 0))) goto LA23;
					goto LA9;
				}
				LA23: ;
				k_172273 = ((NU8) 0);
				{
					NIM_BOOL LOC27;
					LOC27 = 0;
					LOC27 = S_ISDIR(s_172271.st_mode);
					if (!LOC27) goto LA28;
					k_172273 = ((NU8) 2);
				}
				LA28: ;
				{
					NIM_BOOL LOC32;
					LOC32 = 0;
					LOC32 = S_ISLNK(s_172271.st_mode);
					if (!LOC32) goto LA33;
					k_172273 = k_172273 + 1;
				}
				LA33: ;
				k_172252 = k_172273;
				p_172253 = y_172269;
				{
					NIM_BOOL LOC37;
					LOC37 = 0;
					LOC37 = (k_172252 == ((NU8) 2));
					if (!(LOC37)) goto LA38;
					LOC37 = !(((NU8)(p_172253->data[pos]) == (NU8)(46)));
					LA38: ;
					if (!LOC37) goto LA39;
					addpackage_172137(packages, p_172253);
				}
				LA39: ;
			}
			LA18: ;
		} LA9: ;
		LOC41 = 0;
		LOC41 = closedir(d_172265);
	}
	LA7: ;
	p_172263 = 0;
	key_172275 = 0;
	val_172277 = 0;
	h_172279 = 0;
	HEX3Atmp_172281 = 0;
	HEX3Atmp_172281 = ((*packages).Data->Sup.len-1);
	res_172283 = 0;
	while (1) {
		if (!(res_172283 <= HEX3Atmp_172281)) goto LA42;
		h_172279 = res_172283;
		{
			NimStringDesc* res_172285;
			if (!!((*packages).Data->data[h_172279].Field0 == 0)) goto LA45;
			key_172275 = (*packages).Data->data[h_172279].Field0;
			val_172277 = (*packages).Data->data[h_172279].Field1;
			{
				if (!eqStrings(val_172277, ((NimStringDesc*) &TMP1164))) goto LA49;
				res_172285 = key_172275;
			}
			goto LA47;
			LA49: ;
			{
				NimStringDesc* LOC52;
				LOC52 = 0;
				LOC52 = rawNewString(key_172275->Sup.len + val_172277->Sup.len + 1);
appendString(LOC52, key_172275);
appendChar(LOC52, 45);
appendString(LOC52, val_172277);
				res_172285 = LOC52;
			}
			LA47: ;
			p_172263 = res_172285;
			addbabelpath_172163(p_172263, info);
		}
		LA45: ;
		res_172283 += 1;
	} LA42: ;
}

N_NIMCALL(void, babelpath_172290)(NimStringDesc* path, tlineinfo163527 info) {
	addpathrec_172216(path, info);
	addbabelpath_172163(path, info);
}
N_NOINLINE(void, babelcmdInit)(void) {
}

N_NOINLINE(void, babelcmdDatInit)(void) {
}

