/* 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_algorithm.o compiler/nimcache/stdlib_algorithm.c */
#define NIM_INTBITS 32
#include "nimbase.h"
typedef struct tsym188833 tsym188833;
typedef struct tsymseq188805 tsymseq188805;
typedef struct TGenericSeq TGenericSeq;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct tentry360549 tentry360549;
typedef struct TY360556 TY360556;
typedef struct tidobj167013 tidobj167013;
typedef struct TNimObject TNimObject;
typedef struct ttypeseq188835 ttypeseq188835;
typedef struct ttype188837 ttype188837;
typedef struct tscope188827 tscope188827;
typedef struct TY188933 TY188933;
typedef struct tinstantiation188823 tinstantiation188823;
typedef struct tstrtable188807 tstrtable188807;
typedef struct tident167019 tident167019;
typedef struct tlineinfo163527 tlineinfo163527;
typedef struct tnode188803 tnode188803;
typedef struct tloc188817 tloc188817;
typedef struct trope145007 trope145007;
typedef struct tlib188821 tlib188821;
typedef struct tnodeseq188797 tnodeseq188797;
typedef struct TY188922 TY188922;
typedef struct NimStringDesc NimStringDesc;
typedef struct tlistentry118017 tlistentry118017;
typedef struct {
N_NIMCALL_PTR(NI, ClPrc) (tsym188833* x_357795, tsym188833* y_357797, void* ClEnv);
void* ClEnv;
} TY357801;
struct  TGenericSeq  {
NI len;
NI reserved;
};
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 N_CLOSURE_PTR(NI, TMP2493) (tsym188833* x_357870, tsym188833* y_357872);
typedef N_CLOSURE_PTR(NI, TMP2494) (tsym188833* x_357870, tsym188833* y_357872);
struct  tentry360549  {
NI Pos;
tsym188833* Sym;
};
typedef struct {
N_NIMCALL_PTR(NI, ClPrc) (tentry360549 x_361162, tentry360549 y_361164, void* ClEnv);
void* ClEnv;
} TY361168;
typedef N_CLOSURE_PTR(NI, TMP4526) (tentry360549 x_361237, tentry360549 y_361239);
typedef N_CLOSURE_PTR(NI, TMP4527) (tentry360549 x_361237, tentry360549 y_361239);
struct  TNimObject  {
TNimType* m_type;
};
struct  tidobj167013  {
  TNimObject Sup;
NI Id;
};
struct  tstrtable188807  {
NI Counter;
tsymseq188805* Data;
};
struct  tlineinfo163527  {
NI16 Line;
NI16 Col;
NI32 Fileindex;
};
struct  tloc188817  {
NU8 K;
NU8 S;
NU8 Flags;
ttype188837* T;
trope145007* R;
trope145007* Heaproot;
NI A;
};
struct  tsym188833  {
  tidobj167013 Sup;
NU8 Kind;
union {
struct {ttypeseq188835* Typeinstcache;
tscope188827* Typscope;
} S1;
struct {TY188933* Procinstcache;
tscope188827* Scope;
} S2;
struct {TY188933* Usedgenerics;
tstrtable188807 Tab;
} S3;
} kindU;
NU16 Magic;
ttype188837* Typ;
tident167019* Name;
tlineinfo163527 Info;
tsym188833* Owner;
NU32 Flags;
tnode188803* Ast;
NU32 Options;
NI Position;
NI Offset;
tloc188817 Loc;
tlib188821* Annex;
tnode188803* Constraint;
};
struct  ttype188837  {
  tidobj167013 Sup;
NU8 Kind;
NU8 Callconv;
NU32 Flags;
ttypeseq188835* Sons;
tnode188803* N;
tsym188833* Destructor;
tsym188833* Owner;
tsym188833* Sym;
NI64 Size;
NI Align;
tloc188817 Loc;
};
struct  tscope188827  {
NI Depthlevel;
tstrtable188807 Symbols;
tnodeseq188797* Usingsyms;
tscope188827* Parent;
};
struct  tinstantiation188823  {
tsym188833* Sym;
ttypeseq188835* Concretetypes;
TY188922* Usedby;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct  tident167019  {
  tidobj167013 Sup;
NimStringDesc* S;
tident167019* Next;
NI H;
};
struct  tnode188803  {
ttype188837* Typ;
tlineinfo163527 Info;
NU16 Flags;
NU8 Kind;
union {
struct {NI64 Intval;
} S1;
struct {NF Floatval;
} S2;
struct {NimStringDesc* Strval;
} S3;
struct {tsym188833* Sym;
} S4;
struct {tident167019* Ident;
} S5;
struct {tnodeseq188797* Sons;
} S6;
} kindU;
NimStringDesc* Comment;
};
struct  trope145007  {
  TNimObject Sup;
trope145007* Left;
trope145007* Right;
NI Length;
NimStringDesc* Data;
};
struct  tlistentry118017  {
  TNimObject Sup;
tlistentry118017* Prev;
tlistentry118017* Next;
};
struct  tlib188821  {
  tlistentry118017 Sup;
NU8 Kind;
NIM_BOOL Generated;
NIM_BOOL Isoverriden;
trope145007* Name;
tnode188803* Path;
};
struct tsymseq188805 {
  TGenericSeq Sup;
  tsym188833* data[SEQ_DECL_SIZE];
};
struct TY360556 {
  TGenericSeq Sup;
  tentry360549 data[SEQ_DECL_SIZE];
};
struct ttypeseq188835 {
  TGenericSeq Sup;
  ttype188837* data[SEQ_DECL_SIZE];
};
struct TY188933 {
  TGenericSeq Sup;
  tinstantiation188823* data[SEQ_DECL_SIZE];
};
struct tnodeseq188797 {
  TGenericSeq Sup;
  tnode188803* data[SEQ_DECL_SIZE];
};
struct TY188922 {
  TGenericSeq Sup;
  NI32 data[SEQ_DECL_SIZE];
};
N_NIMCALL(void*, newSeq)(TNimType* typ, NI len);
N_NIMCALL(void, merge_357843)(tsym188833** a_357851, NI a_357851Len0, tsym188833** b_357859, NI b_357859Len0, NI lo_357863, NI m_357865, NI hi_357867, TY357801 cmp_357875, NU8 order_357878);
static N_INLINE(NI, HEX2A_353702)(NI x, NU8 order);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(void, merge_361210)(tentry360549* a_361218, NI a_361218Len0, tentry360549* b_361226, NI b_361226Len0, NI lo_361230, NI m_361232, NI hi_361234, TY361168 cmp_361242, NU8 order_361245);
extern TNimType NTI188805; /* TSymSeq */
extern TNimType NTI106; /* int */
TNimType NTI353715; /* range -1..0(int) */
extern TNimType NTI360556; /* seq[TEntry] */

static N_INLINE(NI, HEX2A_353702)(NI x, NU8 order) {
	NI result;
	NI y;
	result = 0;
	y = (NI32)(((NI) (order)) - 1);
	result = (NI32)((NI)(x ^ ((NI) (y))) - ((NI) (y)));
	return result;
}

N_NIMCALL(void, merge_357843)(tsym188833** a_357851, NI a_357851Len0, tsym188833** b_357859, NI b_357859Len0, NI lo_357863, NI m_357865, NI hi_357867, TY357801 cmp_357875, NU8 order_357878) {
	NI j;
	NI bb;
	NI i;
	NI k;
	{
		NI LOC3;
		NI LOC4;
		LOC3 = 0;
		LOC3 = cmp_357875.ClEnv? cmp_357875.ClPrc(a_357851[m_357865], a_357851[(NI32)(m_357865 + 1)], cmp_357875.ClEnv):((TMP2493)(cmp_357875.ClPrc))(a_357851[m_357865], a_357851[(NI32)(m_357865 + 1)]);
		LOC4 = 0;
		LOC4 = HEX2A_353702(LOC3, order_357878);
		if (!(LOC4 <= 0)) goto LA5;
		goto BeforeRet;
	}
	LA5: ;
	j = lo_357863;
	bb = 0;
	while (1) {
		if (!(j <= m_357865)) goto LA7;
		unsureAsgnRef((void**) &b_357859[bb], a_357851[j]);
		bb += 1;
		j += 1;
	} LA7: ;
	i = 0;
	k = lo_357863;
	while (1) {
		NIM_BOOL LOC9;
		LOC9 = 0;
		LOC9 = (k < j);
		if (!(LOC9)) goto LA10;
		LOC9 = (j <= hi_357867);
		LA10: ;
		if (!LOC9) goto LA8;
		{
			NI LOC13;
			NI LOC14;
			LOC13 = 0;
			LOC13 = cmp_357875.ClEnv? cmp_357875.ClPrc(b_357859[i], a_357851[j], cmp_357875.ClEnv):((TMP2494)(cmp_357875.ClPrc))(b_357859[i], a_357851[j]);
			LOC14 = 0;
			LOC14 = HEX2A_353702(LOC13, order_357878);
			if (!(LOC14 <= 0)) goto LA15;
			unsureAsgnRef((void**) &a_357851[k], b_357859[i]);
			i += 1;
		}
		goto LA11;
		LA15: ;
		{
			unsureAsgnRef((void**) &a_357851[k], a_357851[j]);
			j += 1;
		}
		LA11: ;
		k += 1;
	} LA8: ;
	while (1) {
		if (!(k < j)) goto LA18;
		unsureAsgnRef((void**) &a_357851[k], b_357859[i]);
		k += 1;
		i += 1;
	} LA18: ;
	BeforeRet: ;
}

N_NIMCALL(void, sort_357782)(tsym188833** a_357790, NI a_357790Len0, TY357801 cmp_357800, NU8 order_357803) {
	NI n;
	tsymseq188805* b;
	NI s;
	n = a_357790Len0;
	b = 0;
	b = (tsymseq188805*) newSeq((&NTI188805), (NI32)(n / 2));
	s = 1;
	while (1) {
		NI m;
		if (!(s < n)) goto LA1;
		m = (NI32)((NI32)(n - 1) - s);
		while (1) {
			if (!(0 <= m)) goto LA2;
			merge_357843(a_357790, a_357790Len0, b->data, b->Sup.len, (((NI32)((NI32)(m - s) + 1) >= 0) ? (NI32)((NI32)(m - s) + 1) : 0), m, (NI32)(m + s), cmp_357800, order_357803);
			m -= (NI32)(s * 2);
		} LA2: ;
		s = (NI32)(s * 2);
	} LA1: ;
}

N_NIMCALL(void, merge_361210)(tentry360549* a_361218, NI a_361218Len0, tentry360549* b_361226, NI b_361226Len0, NI lo_361230, NI m_361232, NI hi_361234, TY361168 cmp_361242, NU8 order_361245) {
	NI j;
	NI bb;
	NI i;
	NI k;
	{
		NI LOC3;
		NI LOC4;
		LOC3 = 0;
		LOC3 = cmp_361242.ClEnv? cmp_361242.ClPrc(a_361218[m_361232], a_361218[(NI32)(m_361232 + 1)], cmp_361242.ClEnv):((TMP4526)(cmp_361242.ClPrc))(a_361218[m_361232], a_361218[(NI32)(m_361232 + 1)]);
		LOC4 = 0;
		LOC4 = HEX2A_353702(LOC3, order_361245);
		if (!(LOC4 <= 0)) goto LA5;
		goto BeforeRet;
	}
	LA5: ;
	j = lo_361230;
	bb = 0;
	while (1) {
		if (!(j <= m_361232)) goto LA7;
		b_361226[bb].Pos = a_361218[j].Pos;
		unsureAsgnRef((void**) &b_361226[bb].Sym, a_361218[j].Sym);
		bb += 1;
		j += 1;
	} LA7: ;
	i = 0;
	k = lo_361230;
	while (1) {
		NIM_BOOL LOC9;
		LOC9 = 0;
		LOC9 = (k < j);
		if (!(LOC9)) goto LA10;
		LOC9 = (j <= hi_361234);
		LA10: ;
		if (!LOC9) goto LA8;
		{
			NI LOC13;
			NI LOC14;
			LOC13 = 0;
			LOC13 = cmp_361242.ClEnv? cmp_361242.ClPrc(b_361226[i], a_361218[j], cmp_361242.ClEnv):((TMP4527)(cmp_361242.ClPrc))(b_361226[i], a_361218[j]);
			LOC14 = 0;
			LOC14 = HEX2A_353702(LOC13, order_361245);
			if (!(LOC14 <= 0)) goto LA15;
			a_361218[k].Pos = b_361226[i].Pos;
			unsureAsgnRef((void**) &a_361218[k].Sym, b_361226[i].Sym);
			i += 1;
		}
		goto LA11;
		LA15: ;
		{
			a_361218[k].Pos = a_361218[j].Pos;
			unsureAsgnRef((void**) &a_361218[k].Sym, a_361218[j].Sym);
			j += 1;
		}
		LA11: ;
		k += 1;
	} LA8: ;
	while (1) {
		if (!(k < j)) goto LA18;
		a_361218[k].Pos = b_361226[i].Pos;
		unsureAsgnRef((void**) &a_361218[k].Sym, b_361226[i].Sym);
		k += 1;
		i += 1;
	} LA18: ;
	BeforeRet: ;
}

N_NIMCALL(void, sort_361149)(tentry360549* a_361157, NI a_361157Len0, TY361168 cmp_361167, NU8 order_361170) {
	NI n;
	TY360556* b;
	NI s;
	n = a_361157Len0;
	b = 0;
	b = (TY360556*) newSeq((&NTI360556), (NI32)(n / 2));
	s = 1;
	while (1) {
		NI m;
		if (!(s < n)) goto LA1;
		m = (NI32)((NI32)(n - 1) - s);
		while (1) {
			if (!(0 <= m)) goto LA2;
			merge_361210(a_361157, a_361157Len0, b->data, b->Sup.len, (((NI32)((NI32)(m - s) + 1) >= 0) ? (NI32)((NI32)(m - s) + 1) : 0), m, (NI32)(m + s), cmp_361167, order_361170);
			m -= (NI32)(s * 2);
		} LA2: ;
		s = (NI32)(s * 2);
	} LA1: ;
}
N_NOINLINE(void, stdlibalgorithmInit)(void) {
}

N_NOINLINE(void, stdlibalgorithmDatInit)(void) {
NTI353715.size = sizeof(NI);
NTI353715.kind = 20;
NTI353715.base = (&NTI106);
NTI353715.flags = 3;
}

