/* 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 <string.h>
typedef struct Intset222056 Intset222056;
typedef struct Trunk222052 Trunk222052;
typedef struct Trunkseq222054 Trunkseq222054;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct TGenericSeq TGenericSeq;
typedef struct Cell49146 Cell49146;
typedef struct Cellset49158 Cellset49158;
typedef struct Gcheap51218 Gcheap51218;
typedef struct Gcstack51216 Gcstack51216;
typedef struct Cellseq49162 Cellseq49162;
typedef struct Pagedesc49154 Pagedesc49154;
typedef struct Memregion31091 Memregion31091;
typedef struct Smallchunk31043 Smallchunk31043;
typedef struct Llchunk31085 Llchunk31085;
typedef struct Bigchunk31045 Bigchunk31045;
typedef struct Intset31017 Intset31017;
typedef struct Trunk31013 Trunk31013;
typedef struct Avlnode31089 Avlnode31089;
typedef struct Gcstat51214 Gcstat51214;
typedef struct Basechunk31041 Basechunk31041;
typedef struct Freecell31033 Freecell31033;
struct  Intset222056  {
NI counter;
NI max;
Trunk222052* head;
Trunkseq222054* data;
};
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  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
typedef NI TY31022[8];
struct  Trunk222052  {
Trunk222052* next;
NI key;
TY31022 bits;
};
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  Cell49146  {
NI refcount;
TNimType* typ;
};
struct  Cellseq49162  {
NI len;
NI cap;
Cell49146** d;
};
struct  Cellset49158  {
NI counter;
NI max;
Pagedesc49154* head;
Pagedesc49154** data;
};
typedef Smallchunk31043* TY31106[512];
typedef Trunk31013* Trunkbuckets31015[256];
struct  Intset31017  {
Trunkbuckets31015 data;
};
struct  Memregion31091  {
NI minlargeobj;
NI maxlargeobj;
TY31106 freesmallchunks;
Llchunk31085* llmem;
NI currmem;
NI maxmem;
NI freemem;
NI lastsize;
Bigchunk31045* freechunkslist;
Intset31017 chunkstarts;
Avlnode31089* root;
Avlnode31089* deleted;
Avlnode31089* last;
Avlnode31089* freeavlnodes;
};
struct  Gcstat51214  {
NI stackscans;
NI cyclecollections;
NI maxthreshold;
NI maxstacksize;
NI maxstackcells;
NI cycletablesize;
NI64 maxpause;
};
struct  Gcheap51218  {
Gcstack51216* stack;
void* stackbottom;
NI cyclethreshold;
Cellseq49162 zct;
Cellseq49162 decstack;
Cellset49158 cycleroots;
Cellseq49162 tempstack;
NI recgclock;
Memregion31091 region;
Gcstat51214 stat;
};
struct  Gcstack51216  {
Gcstack51216* prev;
Gcstack51216* next;
void* starts;
void* pos;
NI maxstacksize;
};
struct  Pagedesc49154  {
Pagedesc49154* next;
NI key;
TY31022 bits;
};
struct  Basechunk31041  {
NI prevsize;
NI size;
NIM_BOOL used;
};
struct  Smallchunk31043  {
  Basechunk31041 Sup;
Smallchunk31043* next;
Smallchunk31043* prev;
Freecell31033* freelist;
NI free;
NI acc;
NF data;
};
struct  Llchunk31085  {
NI size;
NI acc;
Llchunk31085* next;
};
struct  Bigchunk31045  {
  Basechunk31041 Sup;
Bigchunk31045* next;
Bigchunk31045* prev;
NI align;
NF data;
};
struct  Trunk31013  {
Trunk31013* next;
NI key;
TY31022 bits;
};
typedef Avlnode31089* TY31096[2];
struct  Avlnode31089  {
TY31096 link;
NI key;
NI upperbound;
NI level;
};
struct  Freecell31033  {
Freecell31033* next;
NI zerofield;
};
struct Trunkseq222054 {
  TGenericSeq Sup;
  Trunk222052* data[SEQ_DECL_SIZE];
};
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP1127)(void* p, NI op);
N_NIMCALL(void, TMP1128)(void* p, NI op);
N_NIMCALL(void*, newSeq)(TNimType* typ, NI len);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(Trunk222052*, intsetget_222218)(Intset222056* t, NI key);
static N_INLINE(NI, nexttry_222207)(NI h, NI maxhash);
N_NIMCALL(void, incl_222856)(Intset222056* s, NI key);
N_NIMCALL(Trunk222052*, intsetput_222689)(Intset222056* t, NI key);
static N_INLINE(NIM_BOOL, mustrehash_222075)(NI length, NI counter);
N_NIMCALL(void, intsetenlarge_222614)(Intset222056* t);
N_NIMCALL(void, intsetrawinsert_222249)(Intset222056* t, Trunkseq222054** data, Trunk222052* desc);
static N_INLINE(void, asgnRef)(void** dest, void* src);
static N_INLINE(void, incref_55222)(Cell49146* c);
static N_INLINE(NIM_BOOL, canbecycleroot_52867)(Cell49146* c);
static N_INLINE(void, rtladdcycleroot_53623)(Cell49146* c);
N_NOINLINE(void, incl_49865)(Cellset49158* s, Cell49146* cell);
static N_INLINE(Cell49146*, usrtocell_52846)(void* usr);
static N_INLINE(void, decref_54804)(Cell49146* c);
static N_INLINE(void, rtladdzct_54404)(Cell49146* c);
N_NOINLINE(void, addzct_52817)(Cellseq49162* s, Cell49146* c);
N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
TNimType NTI222056; /* IntSet */
extern TNimType NTI108; /* int */
TNimType NTI222052; /* Trunk */
extern TNimType NTI31022; /* array[0..7, int] */
TNimType NTI222050; /* PTrunk */
TNimType NTI222054; /* TrunkSeq */
extern Gcheap51218 gch_51259;
N_NIMCALL(void, TMP1127)(void* p, NI op) {
	Trunk222052* a;
	NI LOC1;
	a = (Trunk222052*)p;
	nimGCvisit((void*)(*a).next, op);
	LOC1 = 0;
	for (LOC1 = 0; LOC1 < 8; LOC1++) {
	}
}
N_NIMCALL(void, TMP1128)(void* p, NI op) {
	Trunkseq222054* a;
	NI LOC1;
	a = (Trunkseq222054*)p;
	LOC1 = 0;
	for (LOC1 = 0; LOC1 < a->Sup.len; LOC1++) {
	nimGCvisit((void*)a->data[LOC1], op);
	}
}

N_NIMCALL(void, initintset_222927)(Intset222056* Result) {
	unsureAsgnRef((void**) (&(*Result).data), (Trunkseq222054*) newSeq((&NTI222054), ((NI) 8)));
	(*Result).max = ((NI) 7);
	(*Result).counter = ((NI) 0);
	unsureAsgnRef((void**) (&(*Result).head), NIM_NIL);
}

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

N_NIMCALL(Trunk222052*, intsetget_222218)(Intset222056* t, NI key) {
	Trunk222052* result;
	NI h;
{	result = 0;
	h = (NI)(key & (*t).max);
	{
		while (1) {
			if (!!(((*t).data->data[h] == NIM_NIL))) goto LA2;
			{
				if (!((*(*t).data->data[h]).key == key)) goto LA5;
				result = (*t).data->data[h];
				goto BeforeRet;
			}
			LA5: ;
			h = nexttry_222207(h, (*t).max);
		} LA2: ;
	}
	result = NIM_NIL;
	}BeforeRet: ;
	return result;
}

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

static N_INLINE(NIM_BOOL, canbecycleroot_52867)(Cell49146* c) {
	NIM_BOOL result;
	result = 0;
	result = !((((*(*c).typ).flags &(1<<((((NU8) 1))&7)))!=0));
	return result;
}

static N_INLINE(void, rtladdcycleroot_53623)(Cell49146* c) {
	{
		if (!!((((NI) ((NI)((*c).refcount & ((NI) 3)))) == ((NI) 3)))) goto LA3;
		(*c).refcount = (NI)((NI)((*c).refcount & ((NI) -4)) | ((NI) 3));
		incl_49865((&gch_51259.cycleroots), c);
	}
	LA3: ;
}

static N_INLINE(void, incref_55222)(Cell49146* c) {
	(*c).refcount = (NI)((NU64)((*c).refcount) + (NU64)(((NI) 8)));
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = canbecycleroot_52867(c);
		if (!LOC3) goto LA4;
		rtladdcycleroot_53623(c);
	}
	LA4: ;
}

static N_INLINE(Cell49146*, usrtocell_52846)(void* usr) {
	Cell49146* result;
	result = 0;
	result = ((Cell49146*) ((NI)((NU64)(((NI) (usr))) - (NU64)(((NI)sizeof(Cell49146))))));
	return result;
}

static N_INLINE(void, rtladdzct_54404)(Cell49146* c) {
	addzct_52817((&gch_51259.zct), c);
}

static N_INLINE(void, decref_54804)(Cell49146* c) {
	{
		(*c).refcount -= ((NI) 8);
		if (!((NU64)((*c).refcount) < (NU64)(((NI) 8)))) goto LA3;
		rtladdzct_54404(c);
	}
	goto LA1;
	LA3: ;
	{
		NIM_BOOL LOC6;
		LOC6 = 0;
		LOC6 = canbecycleroot_52867(c);
		if (!LOC6) goto LA7;
		rtladdcycleroot_53623(c);
	}
	goto LA1;
	LA7: ;
	LA1: ;
}

static N_INLINE(void, asgnRef)(void** dest, void* src) {
	{
		Cell49146* LOC5;
		if (!!((src == NIM_NIL))) goto LA3;
		LOC5 = 0;
		LOC5 = usrtocell_52846(src);
		incref_55222(LOC5);
	}
	LA3: ;
	{
		Cell49146* LOC10;
		if (!!(((*dest) == NIM_NIL))) goto LA8;
		LOC10 = 0;
		LOC10 = usrtocell_52846((*dest));
		decref_54804(LOC10);
	}
	LA8: ;
	(*dest) = src;
}

N_NIMCALL(void, intsetrawinsert_222249)(Intset222056* t, Trunkseq222054** data, Trunk222052* desc) {
	NI h;
	h = (NI)((*desc).key & (*t).max);
	{
		while (1) {
			if (!!(((*data)->data[h] == NIM_NIL))) goto LA2;
			h = nexttry_222207(h, (*t).max);
		} LA2: ;
	}
	asgnRef((void**) (&(*data)->data[h]), desc);
}

N_NIMCALL(void, intsetenlarge_222614)(Intset222056* t) {
	Trunkseq222054* n;
	NI oldmax;
	Trunkseq222054* LOC8;
	n = 0;
	oldmax = (*t).max;
	(*t).max = (NI)((NI)((NI)((*t).max + ((NI) 1)) * ((NI) 2)) - ((NI) 1));
	n = (Trunkseq222054*) newSeq((&NTI222054), ((NI) ((NI)((*t).max + ((NI) 1)))));
	{
		NI i_222642;
		NI res_222670;
		i_222642 = 0;
		res_222670 = ((NI) 0);
		{
			while (1) {
				if (!(res_222670 <= oldmax)) goto LA3;
				i_222642 = res_222670;
				{
					if (!!(((*t).data->data[i_222642] == NIM_NIL))) goto LA6;
					intsetrawinsert_222249((&(*t)), (&n), (*t).data->data[i_222642]);
				}
				LA6: ;
				res_222670 += ((NI) 1);
			} LA3: ;
		}
	}
	LOC8 = 0;
	LOC8 = (*t).data;
	unsureAsgnRef((void**) (&(*t).data), n);
	n = LOC8;
}

N_NIMCALL(Trunk222052*, intsetput_222689)(Intset222056* t, NI key) {
	Trunk222052* result;
	NI h;
{	result = 0;
	h = (NI)(key & (*t).max);
	{
		while (1) {
			if (!!(((*t).data->data[h] == NIM_NIL))) goto LA2;
			{
				if (!((*(*t).data->data[h]).key == key)) goto LA5;
				result = (*t).data->data[h];
				goto BeforeRet;
			}
			LA5: ;
			h = nexttry_222207(h, (*t).max);
		} LA2: ;
	}
	{
		NIM_BOOL LOC9;
		LOC9 = 0;
		LOC9 = mustrehash_222075((NI)((*t).max + ((NI) 1)), (*t).counter);
		if (!LOC9) goto LA10;
		intsetenlarge_222614(t);
	}
	LA10: ;
	(*t).counter += ((NI) 1);
	h = (NI)(key & (*t).max);
	{
		while (1) {
			if (!!(((*t).data->data[h] == NIM_NIL))) goto LA13;
			h = nexttry_222207(h, (*t).max);
		} LA13: ;
	}
	result = (Trunk222052*) newObj((&NTI222050), sizeof(Trunk222052));
	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_222856)(Intset222056* s, NI key) {
	Trunk222052* t;
	NI u;
	t = intsetput_222689(s, (NI)((NU64)(key) >> (NU64)(((NI) 9))));
	u = (NI)(key & ((NI) 511));
	(*t).bits[((NI)((NU64)(((NI) (u))) >> (NU64)(((NI) 6))))- 0] = (NI)((*t).bits[((NI)((NU64)(((NI) (u))) >> (NU64)(((NI) 6))))- 0] | (NI)((NU64)(((NI) 1)) << (NU64)(((NI) ((NI)(((NI) (u)) & ((NI) 63)))))));
}

N_NIMCALL(NIM_BOOL, containsorincl_222898)(Intset222056* s, NI key) {
	NIM_BOOL result;
	Trunk222052* t;
	result = 0;
	t = intsetget_222218((&(*s)), (NI)((NU64)(key) >> (NU64)(((NI) 9))));
	{
		NI u;
		if (!!((t == NIM_NIL))) goto LA3;
		u = (NI)(key & ((NI) 511));
		result = !(((NI)((*t).bits[((NI)((NU64)(((NI) (u))) >> (NU64)(((NI) 6))))- 0] & (NI)((NU64)(((NI) 1)) << (NU64)(((NI) ((NI)(((NI) (u)) & ((NI) 63))))))) == ((NI) 0)));
		{
			if (!!(result)) goto LA7;
			(*t).bits[((NI)((NU64)(((NI) (u))) >> (NU64)(((NI) 6))))- 0] = (NI)((*t).bits[((NI)((NU64)(((NI) (u))) >> (NU64)(((NI) 6))))- 0] | (NI)((NU64)(((NI) 1)) << (NU64)(((NI) ((NI)(((NI) (u)) & ((NI) 63)))))));
		}
		LA7: ;
	}
	goto LA1;
	LA3: ;
	{
		incl_222856(s, key);
		result = NIM_FALSE;
	}
	LA1: ;
	return result;
}

N_NIMCALL(NIM_BOOL, contains_222829)(Intset222056* s, NI key) {
	NIM_BOOL result;
	Trunk222052* t;
	result = 0;
	t = intsetget_222218(s, (NI)((NU64)(key) >> (NU64)(((NI) 9))));
	{
		NI u;
		if (!!((t == NIM_NIL))) goto LA3;
		u = (NI)(key & ((NI) 511));
		result = !(((NI)((*t).bits[((NI)((NU64)(((NI) (u))) >> (NU64)(((NI) 6))))- 0] & (NI)((NU64)(((NI) 1)) << (NU64)(((NI) ((NI)(((NI) (u)) & ((NI) 63))))))) == ((NI) 0)));
	}
	goto LA1;
	LA3: ;
	{
		result = NIM_FALSE;
	}
	LA1: ;
	return result;
}

N_NIMCALL(void, excl_222871)(Intset222056* s, NI key) {
	Trunk222052* t;
	t = intsetget_222218((&(*s)), (NI)((NU64)(key) >> (NU64)(((NI) 9))));
	{
		NI u;
		if (!!((t == NIM_NIL))) goto LA3;
		u = (NI)(key & ((NI) 511));
		(*t).bits[((NI)((NU64)(((NI) (u))) >> (NU64)(((NI) 6))))- 0] = (NI)((*t).bits[((NI)((NU64)(((NI) (u))) >> (NU64)(((NI) 6))))- 0] & (NI)((NU64) ~((NI)((NU64)(((NI) 1)) << (NU64)(((NI) ((NI)(((NI) (u)) & ((NI) 63)))))))));
	}
	LA3: ;
}

N_NIMCALL(void, assign_222964)(Intset222056* dest, Intset222056* src) {
	Trunk222052* it;
	(*dest).counter = (*src).counter;
	(*dest).max = (*src).max;
	unsureAsgnRef((void**) (&(*dest).data), (Trunkseq222054*) newSeq((&NTI222054), ((NI) (((*src).data ? (*src).data->Sup.len : 0)))));
	it = (*src).head;
	{
		while (1) {
			NI h;
			Trunk222052* n;
			if (!!((it == NIM_NIL))) goto LA2;
			h = (NI)((*it).key & (*dest).max);
			{
				while (1) {
					if (!!(((*dest).data->data[h] == NIM_NIL))) goto LA4;
					h = nexttry_222207(h, (*dest).max);
				} LA4: ;
			}
			n = 0;
			n = (Trunk222052*) newObj((&NTI222050), sizeof(Trunk222052));
			asgnRef((void**) (&(*n).next), (*dest).head);
			(*n).key = (*it).key;
			memcpy((void*)(*n).bits, (NIM_CONST void*)(*it).bits, sizeof(TY31022));
			unsureAsgnRef((void**) (&(*dest).head), n);
			asgnRef((void**) (&(*dest).data->data[h]), n);
			it = (*it).next;
		} LA2: ;
	}
}
NIM_EXTERNC N_NOINLINE(void, stdlib_intsetsInit000)(void) {
}

NIM_EXTERNC N_NOINLINE(void, stdlib_intsetsDatInit000)(void) {
static TNimNode* TMP1125[4];
static TNimNode* TMP1126[3];
static TNimNode TMP801[9];
NTI222056.size = sizeof(Intset222056);
NTI222056.kind = 18;
NTI222056.base = 0;
NTI222056.flags = 2;
TMP1125[0] = &TMP801[1];
TMP801[1].kind = 1;
TMP801[1].offset = offsetof(Intset222056, counter);
TMP801[1].typ = (&NTI108);
TMP801[1].name = "counter";
TMP1125[1] = &TMP801[2];
TMP801[2].kind = 1;
TMP801[2].offset = offsetof(Intset222056, max);
TMP801[2].typ = (&NTI108);
TMP801[2].name = "max";
TMP1125[2] = &TMP801[3];
NTI222052.size = sizeof(Trunk222052);
NTI222052.kind = 18;
NTI222052.base = 0;
TMP1126[0] = &TMP801[5];
TMP801[5].kind = 1;
TMP801[5].offset = offsetof(Trunk222052, next);
TMP801[5].typ = (&NTI222050);
TMP801[5].name = "next";
TMP1126[1] = &TMP801[6];
TMP801[6].kind = 1;
TMP801[6].offset = offsetof(Trunk222052, key);
TMP801[6].typ = (&NTI108);
TMP801[6].name = "key";
TMP1126[2] = &TMP801[7];
TMP801[7].kind = 1;
TMP801[7].offset = offsetof(Trunk222052, bits);
TMP801[7].typ = (&NTI31022);
TMP801[7].name = "bits";
TMP801[4].len = 3; TMP801[4].kind = 2; TMP801[4].sons = &TMP1126[0];
NTI222052.node = &TMP801[4];
NTI222050.size = sizeof(Trunk222052*);
NTI222050.kind = 22;
NTI222050.base = (&NTI222052);
NTI222050.marker = TMP1127;
TMP801[3].kind = 1;
TMP801[3].offset = offsetof(Intset222056, head);
TMP801[3].typ = (&NTI222050);
TMP801[3].name = "head";
TMP1125[3] = &TMP801[8];
NTI222054.size = sizeof(Trunkseq222054*);
NTI222054.kind = 24;
NTI222054.base = (&NTI222050);
NTI222054.flags = 2;
NTI222054.marker = TMP1128;
TMP801[8].kind = 1;
TMP801[8].offset = offsetof(Intset222056, data);
TMP801[8].typ = (&NTI222054);
TMP801[8].name = "data";
TMP801[0].len = 4; TMP801[0].kind = 2; TMP801[0].sons = &TMP1125[0];
NTI222056.node = &TMP801[0];
}

