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

#include <string.h>
typedef struct tintset185047 tintset185047;
typedef struct ttrunk185043 ttrunk185043;
typedef struct ttrunkseq185045 ttrunkseq185045;
typedef struct TGenericSeq TGenericSeq;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct tcell41090 tcell41090;
typedef struct tcellset41102 tcellset41102;
typedef struct tgcheap43016 tgcheap43016;
typedef struct tcellseq41106 tcellseq41106;
typedef struct tpagedesc41098 tpagedesc41098;
typedef struct tmemregion23410 tmemregion23410;
typedef struct tsmallchunk22638 tsmallchunk22638;
typedef struct tllchunk23404 tllchunk23404;
typedef struct tbigchunk22640 tbigchunk22640;
typedef struct tintset22615 tintset22615;
typedef struct ttrunk22611 ttrunk22611;
typedef struct tavlnode23408 tavlnode23408;
typedef struct tgcstat43014 tgcstat43014;
typedef struct tbasechunk22636 tbasechunk22636;
typedef struct tfreecell22628 tfreecell22628;
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  tintset185047  {
NI Counter;
NI Max;
ttrunk185043* Head;
ttrunkseq185045* Data;
};
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  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
typedef NI TY22618[16];
struct  ttrunk185043  {
ttrunk185043* Next;
NI Key;
TY22618 Bits;
};
struct  tcell41090  {
NI Refcount;
TNimType* Typ;
};
struct  tcellseq41106  {
NI Len;
NI Cap;
tcell41090** D;
};
struct  tcellset41102  {
NI Counter;
NI Max;
tpagedesc41098* Head;
tpagedesc41098** Data;
};
typedef tsmallchunk22638* TY23422[512];
typedef ttrunk22611* ttrunkbuckets22613[256];
struct  tintset22615  {
ttrunkbuckets22613 Data;
};
struct  tmemregion23410  {
NI Minlargeobj;
NI Maxlargeobj;
TY23422 Freesmallchunks;
tllchunk23404* Llmem;
NI Currmem;
NI Maxmem;
NI Freemem;
NI Lastsize;
tbigchunk22640* Freechunkslist;
tintset22615 Chunkstarts;
tavlnode23408* Root;
tavlnode23408* Deleted;
tavlnode23408* Last;
tavlnode23408* Freeavlnodes;
};
struct  tgcstat43014  {
NI Stackscans;
NI Cyclecollections;
NI Maxthreshold;
NI Maxstacksize;
NI Maxstackcells;
NI Cycletablesize;
NI64 Maxpause;
};
struct  tgcheap43016  {
void* Stackbottom;
NI Cyclethreshold;
tcellseq41106 Zct;
tcellseq41106 Decstack;
tcellset41102 Cycleroots;
tcellseq41106 Tempstack;
NI Recgclock;
tmemregion23410 Region;
tgcstat43014 Stat;
};
struct  tpagedesc41098  {
tpagedesc41098* Next;
NI Key;
TY22618 Bits;
};
struct  tbasechunk22636  {
NI Prevsize;
NI Size;
NIM_BOOL Used;
};
struct  tsmallchunk22638  {
  tbasechunk22636 Sup;
tsmallchunk22638* Next;
tsmallchunk22638* Prev;
tfreecell22628* Freelist;
NI Free;
NI Acc;
NF Data;
};
struct  tllchunk23404  {
NI Size;
NI Acc;
tllchunk23404* Next;
};
struct  tbigchunk22640  {
  tbasechunk22636 Sup;
tbigchunk22640* Next;
tbigchunk22640* Prev;
NI Align;
NF Data;
};
struct  ttrunk22611  {
ttrunk22611* Next;
NI Key;
TY22618 Bits;
};
typedef tavlnode23408* TY23414[2];
struct  tavlnode23408  {
TY23414 Link;
NI Key;
NI Upperbound;
NI Level;
};
struct  tfreecell22628  {
tfreecell22628* Next;
NI Zerofield;
};
struct ttrunkseq185045 {
  TGenericSeq Sup;
  ttrunk185043* data[SEQ_DECL_SIZE];
};
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP947)(void* p, NI op);
N_NIMCALL(void, TMP948)(void* p, NI op);
N_NIMCALL(void*, newSeq)(TNimType* typ, NI len);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(ttrunk185043*, intsetget_185212)(tintset185047 t, NI key);
static N_INLINE(NI, nexttry_185205)(NI h, NI maxhash);
N_NIMCALL(void, incl_185849)(tintset185047* s, NI key);
N_NIMCALL(ttrunk185043*, intsetput_185679)(tintset185047* t, NI key);
static N_INLINE(NIM_BOOL, mustrehash_185058)(NI length, NI counter);
N_NIMCALL(void, intsetenlarge_185604)(tintset185047* t);
N_NIMCALL(void, intsetrawinsert_185238)(tintset185047 t, ttrunkseq185045** data, ttrunk185043* desc);
static N_INLINE(void, asgnRef)(void** dest, void* src);
static N_INLINE(void, incref_46827)(tcell41090* c);
static N_INLINE(NIM_BOOL, canbecycleroot_44462)(tcell41090* c);
static N_INLINE(void, rtladdcycleroot_45235)(tcell41090* c);
N_NOINLINE(void, incl_41855)(tcellset41102* s, tcell41090* cell);
static N_INLINE(tcell41090*, usrtocell_44443)(void* usr);
static N_INLINE(void, decref_46402)(tcell41090* c);
static N_INLINE(void, rtladdzct_46002)(tcell41090* c);
N_NOINLINE(void, addzct_44418)(tcellseq41106* s, tcell41090* c);
N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
TNimType NTI185047; /* TIntSet */
extern TNimType NTI106; /* int */
TNimType NTI185043; /* TTrunk */
extern TNimType NTI22618; /* array[0..15, int] */
TNimType NTI185041; /* PTrunk */
TNimType NTI185045; /* TTrunkSeq */
extern tgcheap43016 gch_43044;
N_NIMCALL(void, TMP947)(void* p, NI op) {
	ttrunk185043* a;
	NI LOC1;
	a = (ttrunk185043*)p;
	nimGCvisit((void*)(*a).Next, op);
	LOC1 = 0;
	for (LOC1 = 0; LOC1 < 16; LOC1++) {
	}
}
N_NIMCALL(void, TMP948)(void* p, NI op) {
	ttrunkseq185045* a;
	NI LOC1;
	a = (ttrunkseq185045*)p;
	LOC1 = 0;
	for (LOC1 = 0; LOC1 < a->Sup.len; LOC1++) {
	nimGCvisit((void*)a->data[LOC1], op);
	}
}

N_NIMCALL(void, initintset_185918)(tintset185047* Result) {
	unsureAsgnRef((void**) &(*Result).Data, (ttrunkseq185045*) newSeq((&NTI185045), 8));
	(*Result).Max = 7;
	(*Result).Counter = 0;
	unsureAsgnRef((void**) &(*Result).Head, NIM_NIL);
}

static N_INLINE(NI, nexttry_185205)(NI h, NI maxhash) {
	NI result;
	result = 0;
	result = (NI)((NI32)((NI32)(5 * h) + 1) & maxhash);
	return result;
}

N_NIMCALL(ttrunk185043*, intsetget_185212)(tintset185047 t, NI key) {
	ttrunk185043* result;
	NI h;
	result = 0;
	h = (NI)(key & t.Max);
	while (1) {
		if (!!((t.Data->data[h] == NIM_NIL))) goto LA1;
		{
			if (!((*t.Data->data[h]).Key == key)) goto LA4;
			result = t.Data->data[h];
			goto BeforeRet;
		}
		LA4: ;
		h = nexttry_185205(h, t.Max);
	} LA1: ;
	result = NIM_NIL;
	BeforeRet: ;
	return result;
}

static N_INLINE(NIM_BOOL, mustrehash_185058)(NI length, NI counter) {
	NIM_BOOL result;
	NIM_BOOL LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = ((NI32)(length * 2) < (NI32)(counter * 3));
	if (LOC1) goto LA2;
	LOC1 = ((NI32)(length - counter) < 4);
	LA2: ;
	result = LOC1;
	return result;
}

static N_INLINE(NIM_BOOL, canbecycleroot_44462)(tcell41090* c) {
	NIM_BOOL result;
	result = 0;
	result = !((((*(*c).Typ).flags &(1<<((((NU8) 1))&7)))!=0));
	return result;
}

static N_INLINE(void, rtladdcycleroot_45235)(tcell41090* c) {
	{
		if (!!((((NI) ((NI)((*c).Refcount & 3))) == 3))) goto LA3;
		(*c).Refcount = (NI)((NI)((*c).Refcount & -4) | 3);
		incl_41855(&gch_43044.Cycleroots, c);
	}
	LA3: ;
}

static N_INLINE(void, incref_46827)(tcell41090* c) {
	(*c).Refcount = (NI)((NU32)((*c).Refcount) + (NU32)(8));
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = canbecycleroot_44462(c);
		if (!LOC3) goto LA4;
		rtladdcycleroot_45235(c);
	}
	LA4: ;
}

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

static N_INLINE(void, rtladdzct_46002)(tcell41090* c) {
	addzct_44418(&gch_43044.Zct, c);
}

static N_INLINE(void, decref_46402)(tcell41090* c) {
	{
		(*c).Refcount -= 8;
		if (!((NU32)((*c).Refcount) < (NU32)(8))) goto LA3;
		rtladdzct_46002(c);
	}
	goto LA1;
	LA3: ;
	{
		NIM_BOOL LOC6;
		LOC6 = 0;
		LOC6 = canbecycleroot_44462(c);
		if (!LOC6) goto LA7;
		rtladdcycleroot_45235(c);
	}
	goto LA1;
	LA7: ;
	LA1: ;
}

static N_INLINE(void, asgnRef)(void** dest, void* src) {
	{
		tcell41090* LOC5;
		if (!!((src == NIM_NIL))) goto LA3;
		LOC5 = 0;
		LOC5 = usrtocell_44443(src);
		incref_46827(LOC5);
	}
	LA3: ;
	{
		tcell41090* LOC10;
		if (!!(((*dest) == NIM_NIL))) goto LA8;
		LOC10 = 0;
		LOC10 = usrtocell_44443((*dest));
		decref_46402(LOC10);
	}
	LA8: ;
	(*dest) = src;
}

N_NIMCALL(void, intsetrawinsert_185238)(tintset185047 t, ttrunkseq185045** data, ttrunk185043* desc) {
	NI h;
	h = (NI)((*desc).Key & t.Max);
	while (1) {
		if (!!(((*data)->data[h] == NIM_NIL))) goto LA1;
		h = nexttry_185205(h, t.Max);
	} LA1: ;
	asgnRef((void**) &(*data)->data[h], desc);
}

N_NIMCALL(void, intsetenlarge_185604)(tintset185047* t) {
	ttrunkseq185045* n;
	NI oldmax;
	NI i_185638;
	NI res_185674;
	ttrunkseq185045* LOC6;
	n = 0;
	oldmax = (*t).Max;
	(*t).Max = (NI32)((NI32)((NI32)((*t).Max + 1) * 2) - 1);
	n = (ttrunkseq185045*) newSeq((&NTI185045), (NI32)((*t).Max + 1));
	i_185638 = 0;
	res_185674 = 0;
	while (1) {
		if (!(res_185674 <= oldmax)) goto LA1;
		i_185638 = res_185674;
		{
			if (!!(((*t).Data->data[i_185638] == NIM_NIL))) goto LA4;
			intsetrawinsert_185238((*t), &n, (*t).Data->data[i_185638]);
		}
		LA4: ;
		res_185674 += 1;
	} LA1: ;
	LOC6 = 0;
	LOC6 = (*t).Data;
	unsureAsgnRef((void**) &(*t).Data, n);
	n = LOC6;
}

N_NIMCALL(ttrunk185043*, intsetput_185679)(tintset185047* t, NI key) {
	ttrunk185043* result;
	NI h;
	result = 0;
	h = (NI)(key & (*t).Max);
	while (1) {
		if (!!(((*t).Data->data[h] == NIM_NIL))) goto LA1;
		{
			if (!((*(*t).Data->data[h]).Key == key)) goto LA4;
			result = (*t).Data->data[h];
			goto BeforeRet;
		}
		LA4: ;
		h = nexttry_185205(h, (*t).Max);
	} LA1: ;
	{
		NIM_BOOL LOC8;
		LOC8 = 0;
		LOC8 = mustrehash_185058((NI32)((*t).Max + 1), (*t).Counter);
		if (!LOC8) goto LA9;
		intsetenlarge_185604(t);
	}
	LA9: ;
	(*t).Counter += 1;
	h = (NI)(key & (*t).Max);
	while (1) {
		if (!!(((*t).Data->data[h] == NIM_NIL))) goto LA11;
		h = nexttry_185205(h, (*t).Max);
	} LA11: ;
	result = (ttrunk185043*) newObj((&NTI185041), sizeof(ttrunk185043));
	asgnRef((void**) &(*result).Next, (*t).Head);
	(*result).Key = key;
	unsureAsgnRef((void**) &(*t).Head, result);
	asgnRef((void**) &(*t).Data->data[h], result);
	BeforeRet: ;
	return result;
}

N_NIMCALL(void, incl_185849)(tintset185047* s, NI key) {
	ttrunk185043* t;
	NI u;
	t = intsetput_185679(s, (NI)((NU32)(key) >> (NU32)(9)));
	u = (NI)(key & 511);
	(*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] = (NI)((*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] | (NI)((NU32)(1) << (NU32)(((NI) ((NI)(((NI) (u)) & 31))))));
}

N_NIMCALL(NIM_BOOL, containsorincl_185888)(tintset185047* s, NI key) {
	NIM_BOOL result;
	ttrunk185043* t;
	result = 0;
	t = intsetget_185212((*s), (NI)((NU32)(key) >> (NU32)(9)));
	{
		NI u;
		if (!!((t == NIM_NIL))) goto LA3;
		u = (NI)(key & 511);
		result = !(((NI)((*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] & (NI)((NU32)(1) << (NU32)(((NI) ((NI)(((NI) (u)) & 31)))))) == 0));
		{
			if (!!(result)) goto LA7;
			(*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] = (NI)((*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] | (NI)((NU32)(1) << (NU32)(((NI) ((NI)(((NI) (u)) & 31))))));
		}
		LA7: ;
	}
	goto LA1;
	LA3: ;
	{
		incl_185849(s, key);
		result = NIM_FALSE;
	}
	LA1: ;
	return result;
}

N_NIMCALL(NIM_BOOL, contains_185821)(tintset185047 s, NI key) {
	NIM_BOOL result;
	ttrunk185043* t;
	result = 0;
	t = intsetget_185212(s, (NI)((NU32)(key) >> (NU32)(9)));
	{
		NI u;
		if (!!((t == NIM_NIL))) goto LA3;
		u = (NI)(key & 511);
		result = !(((NI)((*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] & (NI)((NU32)(1) << (NU32)(((NI) ((NI)(((NI) (u)) & 31)))))) == 0));
	}
	goto LA1;
	LA3: ;
	{
		result = NIM_FALSE;
	}
	LA1: ;
	return result;
}

N_NIMCALL(void, assign_185939)(tintset185047* dest, tintset185047 src) {
	ttrunk185043* it;
	(*dest).Counter = src.Counter;
	(*dest).Max = src.Max;
	unsureAsgnRef((void**) &(*dest).Data, (ttrunkseq185045*) newSeq((&NTI185045), src.Data->Sup.len));
	it = src.Head;
	while (1) {
		NI h;
		ttrunk185043* n;
		if (!!((it == NIM_NIL))) goto LA1;
		h = (NI)((*it).Key & (*dest).Max);
		while (1) {
			if (!!(((*dest).Data->data[h] == NIM_NIL))) goto LA2;
			h = nexttry_185205(h, (*dest).Max);
		} LA2: ;
		n = 0;
		n = (ttrunk185043*) newObj((&NTI185041), sizeof(ttrunk185043));
		asgnRef((void**) &(*n).Next, (*dest).Head);
		(*n).Key = (*it).Key;
		memcpy((void*)(*n).Bits, (NIM_CONST void*)(*it).Bits, sizeof((*n).Bits));
		unsureAsgnRef((void**) &(*dest).Head, n);
		asgnRef((void**) &(*dest).Data->data[h], n);
		it = (*it).Next;
	} LA1: ;
}

N_NIMCALL(void, excl_185860)(tintset185047* s, NI key) {
	ttrunk185043* t;
	t = intsetget_185212((*s), (NI)((NU32)(key) >> (NU32)(9)));
	{
		NI u;
		if (!!((t == NIM_NIL))) goto LA3;
		u = (NI)(key & 511);
		(*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] = (NI)((*t).Bits[((NI)((NU32)(((NI) (u))) >> (NU32)(5)))- 0] & (NI)((NU32) ~((NI)((NU32)(1) << (NU32)(((NI) ((NI)(((NI) (u)) & 31))))))));
	}
	LA3: ;
}
N_NOINLINE(void, stdlibintsetsInit)(void) {
}

N_NOINLINE(void, stdlibintsetsDatInit)(void) {
static TNimNode* TMP945[4];
static TNimNode* TMP946[3];
static TNimNode TMP715[9];
NTI185047.size = sizeof(tintset185047);
NTI185047.kind = 18;
NTI185047.base = 0;
NTI185047.flags = 2;
TMP945[0] = &TMP715[1];
TMP715[1].kind = 1;
TMP715[1].offset = offsetof(tintset185047, Counter);
TMP715[1].typ = (&NTI106);
TMP715[1].name = "counter";
TMP945[1] = &TMP715[2];
TMP715[2].kind = 1;
TMP715[2].offset = offsetof(tintset185047, Max);
TMP715[2].typ = (&NTI106);
TMP715[2].name = "max";
TMP945[2] = &TMP715[3];
NTI185043.size = sizeof(ttrunk185043);
NTI185043.kind = 18;
NTI185043.base = 0;
TMP946[0] = &TMP715[5];
TMP715[5].kind = 1;
TMP715[5].offset = offsetof(ttrunk185043, Next);
TMP715[5].typ = (&NTI185041);
TMP715[5].name = "next";
TMP946[1] = &TMP715[6];
TMP715[6].kind = 1;
TMP715[6].offset = offsetof(ttrunk185043, Key);
TMP715[6].typ = (&NTI106);
TMP715[6].name = "key";
TMP946[2] = &TMP715[7];
TMP715[7].kind = 1;
TMP715[7].offset = offsetof(ttrunk185043, Bits);
TMP715[7].typ = (&NTI22618);
TMP715[7].name = "bits";
TMP715[4].len = 3; TMP715[4].kind = 2; TMP715[4].sons = &TMP946[0];
NTI185043.node = &TMP715[4];
NTI185041.size = sizeof(ttrunk185043*);
NTI185041.kind = 22;
NTI185041.base = (&NTI185043);
NTI185041.marker = TMP947;
TMP715[3].kind = 1;
TMP715[3].offset = offsetof(tintset185047, Head);
TMP715[3].typ = (&NTI185041);
TMP715[3].name = "head";
TMP945[3] = &TMP715[8];
NTI185045.size = sizeof(ttrunkseq185045*);
NTI185045.kind = 24;
NTI185045.base = (&NTI185041);
NTI185045.flags = 2;
NTI185045.marker = TMP948;
TMP715[8].kind = 1;
TMP715[8].offset = offsetof(tintset185047, Data);
TMP715[8].typ = (&NTI185045);
TMP715[8].name = "data";
TMP715[0].len = 4; TMP715[0].kind = 2; TMP715[0].sons = &TMP945[0];
NTI185047.node = &TMP715[0];
}

