/* Generated by Nim Compiler v0.13.0 */
/*   (c) 2015 Andreas Rumpf */
/* The generated code is subject to the original license. */
#define NIM_INTBITS 32

#include "nimbase.h"
#include <string.h>
#include <stdio.h>
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct Ropeobj172006 Ropeobj172006;
typedef struct TNimObject TNimObject;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct Cell47304 Cell47304;
typedef struct Cellseq47320 Cellseq47320;
typedef struct Gcheap49618 Gcheap49618;
typedef struct Gcstack49616 Gcstack49616;
typedef struct Cellset47316 Cellset47316;
typedef struct Pagedesc47312 Pagedesc47312;
typedef struct Memregion29487 Memregion29487;
typedef struct Smallchunk29439 Smallchunk29439;
typedef struct Llchunk29481 Llchunk29481;
typedef struct Bigchunk29441 Bigchunk29441;
typedef struct Intset29414 Intset29414;
typedef struct Trunk29410 Trunk29410;
typedef struct Avlnode29485 Avlnode29485;
typedef struct Gcstat49614 Gcstat49614;
typedef struct TY184350 TY184350;
typedef struct Basechunk29437 Basechunk29437;
typedef struct Freecell29429 Freecell29429;
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
typedef struct {
N_NIMCALL_PTR(void, ClPrc) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning, void* ClEnv);
void* ClEnv;
} TY172017;
typedef Ropeobj172006* TY172072[4096];
typedef N_NIMCALL_PTR(void, TY3489) (void* p, NI op);
typedef N_NIMCALL_PTR(void*, TY3494) (void* p);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY3489 marker;
TY3494 deepcopy;
};
struct  TNimObject  {
TNimType* m_type;
};
struct  Ropeobj172006  {
  TNimObject Sup;
Ropeobj172006* left;
Ropeobj172006* right;
NI length;
NimStringDesc* data;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct  Cell47304  {
NI refcount;
TNimType* typ;
};
struct  Cellseq47320  {
NI len;
NI cap;
Cell47304** d;
};
struct  Cellset47316  {
NI counter;
NI max;
Pagedesc47312* head;
Pagedesc47312** data;
};
typedef Smallchunk29439* TY29502[512];
typedef Trunk29410* Trunkbuckets29412[256];
struct  Intset29414  {
Trunkbuckets29412 data;
};
struct  Memregion29487  {
NI minlargeobj;
NI maxlargeobj;
TY29502 freesmallchunks;
Llchunk29481* llmem;
NI currmem;
NI maxmem;
NI freemem;
NI lastsize;
Bigchunk29441* freechunkslist;
Intset29414 chunkstarts;
Avlnode29485* root;
Avlnode29485* deleted;
Avlnode29485* last;
Avlnode29485* freeavlnodes;
};
struct  Gcstat49614  {
NI stackscans;
NI cyclecollections;
NI maxthreshold;
NI maxstacksize;
NI maxstackcells;
NI cycletablesize;
NI64 maxpause;
};
struct  Gcheap49618  {
Gcstack49616* stack;
void* stackbottom;
NI cyclethreshold;
Cellseq47320 zct;
Cellseq47320 decstack;
Cellset47316 cycleroots;
Cellseq47320 tempstack;
NI recgclock;
Memregion29487 region;
Gcstat49614 stat;
};
typedef Ropeobj172006* TY172507[1];
typedef N_CLOSURE_PTR(void, TMP3762) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef N_CLOSURE_PTR(void, TMP3763) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef N_CLOSURE_PTR(void, TMP3764) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef N_CLOSURE_PTR(void, TMP3765) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef N_CLOSURE_PTR(void, TMP4591) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef NIM_CHAR TY173409[1024];
struct  Gcstack49616  {
Gcstack49616* prev;
Gcstack49616* next;
void* starts;
void* pos;
NI maxstacksize;
};
typedef NI TY29418[16];
struct  Pagedesc47312  {
Pagedesc47312* next;
NI key;
TY29418 bits;
};
struct  Basechunk29437  {
NI prevsize;
NI size;
NIM_BOOL used;
};
struct  Smallchunk29439  {
  Basechunk29437 Sup;
Smallchunk29439* next;
Smallchunk29439* prev;
Freecell29429* freelist;
NI free;
NI acc;
NF data;
};
struct  Llchunk29481  {
NI size;
NI acc;
Llchunk29481* next;
};
struct  Bigchunk29441  {
  Basechunk29437 Sup;
Bigchunk29441* next;
Bigchunk29441* prev;
NI align;
NF data;
};
struct  Trunk29410  {
Trunk29410* next;
NI key;
TY29418 bits;
};
typedef Avlnode29485* TY29492[2];
struct  Avlnode29485  {
TY29492 link;
NI key;
NI upperbound;
NI level;
};
struct  Freecell29429  {
Freecell29429* next;
NI zerofield;
};
struct TY184350 {
  TGenericSeq Sup;
  Ropeobj172006* data[SEQ_DECL_SIZE];
};
N_NIMCALL(Ropeobj172006*, newrope_172041)(NimStringDesc* data);
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP133)(void* p, NI op);
N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
N_NIMCALL(NimStringDesc*, copyStringRC1)(NimStringDesc* src);
static N_INLINE(void, nimGCunrefNoCycle)(void* p);
static N_INLINE(Cell47304*, usrtocell_51240)(void* usr);
static N_INLINE(void, rtladdzct_52801)(Cell47304* c);
N_NOINLINE(void, addzct_51217)(Cellseq47320* s, Cell47304* c);
static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src);
N_NIMCALL(Ropeobj172006*, HEX26_172418)(Ropeobj172006* a, Ropeobj172006* b);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(Ropeobj172006*, insertincache_172241)(NimStringDesc* s);
N_NIMCALL(NI, hash_140827)(NimStringDesc* x);
static N_INLINE(NIM_BOOL, eqStrings)(NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(NimStringDesc*, mnewString)(NI len);
N_NIMCALL(NimStringDesc*, mnewString)(NI len);
N_NIMCALL(NI, len_172025)(Ropeobj172006* a);
N_NIMCALL(NimStringDesc*, setLengthStr)(NimStringDesc* s, NI newlen);
N_NIMCALL(void*, newSeq)(TNimType* typ, NI len);
static N_INLINE(Ropeobj172006*, pop_172530)(TY184350** s);
N_NIMCALL(TGenericSeq*, setLengthSeq)(TGenericSeq* seq, NI elemsize, NI newlen);
N_NIMCALL(TGenericSeq*, incrSeqV2)(TGenericSeq* seq, NI elemsize);
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src);
N_NIMCALL(NimStringDesc*, resizeString)(NimStringDesc* dest, NI addlen);
N_NIMCALL(Ropeobj172006*, HEX26_172447)(Ropeobj172006* a, NimStringDesc* b);
N_NIMCALL(Ropeobj172006*, rope_172277)(NimStringDesc* s);
N_NIMCALL(void, add_172487)(Ropeobj172006** a, NimStringDesc* b);
N_NIMCALL(void, add_172482)(Ropeobj172006** a, Ropeobj172006* b);
N_NIMCALL(NimStringDesc*, nimIntToStr)(NI x);
N_NIMCALL(NimStringDesc*, nimCharToStr)(NIM_CHAR x);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI start_79610, NI last);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI first, NI last);
N_NIMCALL(NimStringDesc*, nimInt64ToStr)(NI64 x);
N_NIMCALL(Ropeobj172006*, HEX25_172905)(NimStringDesc* frmt, Ropeobj172006** args, NI argsLen0);
N_NIMCALL(NIM_BOOL, open_15403)(FILE** f, NimStringDesc* filename, NU8 mode, NI bufsize);
N_NIMCALL(void, writerope_172822)(FILE* f, Ropeobj172006* r);
N_NIMCALL(void, write_15489)(FILE* f, NimStringDesc* s);
N_NIMCALL(NIM_BOOL, equalsfile_173483)(Ropeobj172006* r, NimStringDesc* filename);
N_NIMCALL(NIM_BOOL, equalsfile_173403)(Ropeobj172006* r, FILE* f);
N_NIMCALL(NI, readbuffer_15554)(FILE* f, void* buffer, NI len);
N_NIMCALL(void, writerope_172836)(Ropeobj172006* head, NimStringDesc* filename, NIM_BOOL usewarning);
STRING_LITERAL(TMP3761, "$", 1);
TY172017 errorhandler_172022;
TY172072 cache_172073;
NI gcachetries_172238;
NI gcachemisses_172239;
NI gcacheinttries_172240;
Ropeobj172006* rnl_172903;
extern TNimType NTI3608; /* RootObj */
TNimType NTI172006; /* RopeObj */
extern TNimType NTI104; /* int */
extern TNimType NTI138; /* string */
TNimType NTI172004; /* Rope */
extern Gcheap49618 gch_49659;
extern NimStringDesc* tnl_170618;
Ropeobj172006* softrnl_172904;
extern TNimType NTI184350; /* seq[Rope] */
N_NIMCALL(void, TMP133)(void* p, NI op) {
	Ropeobj172006* a;
	a = (Ropeobj172006*)p;
	nimGCvisit((void*)(*a).left, op);
	nimGCvisit((void*)(*a).right, op);
	nimGCvisit((void*)(*a).data, op);
}

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

static N_INLINE(void, rtladdzct_52801)(Cell47304* c) {
	addzct_51217((&gch_49659.zct), c);
}

static N_INLINE(void, nimGCunrefNoCycle)(void* p) {
	Cell47304* c;
	c = usrtocell_51240(p);
	{
		(*c).refcount -= ((NI) 8);
		if (!((NU32)((*c).refcount) < (NU32)(((NI) 8)))) goto LA3;
		rtladdzct_52801(c);
	}
	LA3: ;
}

N_NIMCALL(Ropeobj172006*, newrope_172041)(NimStringDesc* data) {
	Ropeobj172006* result;
	result = 0;
	result = (Ropeobj172006*) newObj((&NTI172004), sizeof(Ropeobj172006));
	(*result).Sup.m_type = (&NTI172006);
	{
		NimStringDesc* LOC5;
		if (!!((data == NIM_NIL))) goto LA3;
		(*result).length = (data ? data->Sup.len : 0);
		LOC5 = 0;
		LOC5 = (*result).data; (*result).data = copyStringRC1(data);
		if (LOC5) nimGCunrefNoCycle(LOC5);
	}
	LA3: ;
	return result;
}

static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src) {
	{
		Cell47304* c;
		if (!!((src == NIM_NIL))) goto LA3;
		c = usrtocell_51240(src);
		(*c).refcount += ((NI) 8);
	}
	LA3: ;
	{
		Cell47304* c;
		if (!!(((*dest) == NIM_NIL))) goto LA7;
		c = usrtocell_51240((*dest));
		{
			(*c).refcount -= ((NI) 8);
			if (!((NU32)((*c).refcount) < (NU32)(((NI) 8)))) goto LA11;
			rtladdzct_52801(c);
		}
		LA11: ;
	}
	LA7: ;
	(*dest) = src;
}

N_NIMCALL(Ropeobj172006*, HEX26_172418)(Ropeobj172006* a, Ropeobj172006* b) {
	Ropeobj172006* result;
	result = 0;
	{
		if (!(a == NIM_NIL)) goto LA3;
		result = b;
	}
	goto LA1;
	LA3: ;
	{
		if (!(b == NIM_NIL)) goto LA6;
		result = a;
	}
	goto LA1;
	LA6: ;
	{
		result = newrope_172041(NIM_NIL);
		(*result).length = (NI)((*a).length + (*b).length);
		asgnRefNoCycle((void**) (&(*result).left), a);
		asgnRefNoCycle((void**) (&(*result).right), b);
	}
	LA1: ;
	return result;
}

N_NIMCALL(void, add_172482)(Ropeobj172006** a, Ropeobj172006* b) {
	unsureAsgnRef((void**) (&(*a)), HEX26_172418((*a), b));
}

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(Ropeobj172006*, insertincache_172241)(NimStringDesc* s) {
	Ropeobj172006* result;
	NI h;
	NI LOC1;
	result = 0;
	gcachetries_172238 += ((NI) 1);
	LOC1 = 0;
	LOC1 = hash_140827(s);
	h = (NI)(LOC1 & ((NI) 4095));
	result = cache_172073[(h)- 0];
	{
		NIM_BOOL LOC4;
		LOC4 = 0;
		LOC4 = result == 0;
		if (LOC4) goto LA5;
		LOC4 = !(eqStrings((*result).data, s));
		LA5: ;
		if (!LOC4) goto LA6;
		gcachemisses_172239 += ((NI) 1);
		result = newrope_172041(s);
		asgnRefNoCycle((void**) (&cache_172073[(h)- 0]), result);
	}
	LA6: ;
	return result;
}

N_NIMCALL(Ropeobj172006*, rope_172277)(NimStringDesc* s) {
	Ropeobj172006* result;
	result = 0;
	{
		if (!((s ? s->Sup.len : 0) == ((NI) 0))) goto LA3;
		result = NIM_NIL;
	}
	goto LA1;
	LA3: ;
	{
		result = insertincache_172241(s);
	}
	LA1: ;
	return result;
}

N_NIMCALL(NI, len_172025)(Ropeobj172006* a) {
	NI result;
	result = 0;
	{
		if (!(a == NIM_NIL)) goto LA3;
		result = ((NI) 0);
	}
	goto LA1;
	LA3: ;
	{
		result = (*a).length;
	}
	LA1: ;
	return result;
}

static N_INLINE(Ropeobj172006*, pop_172530)(TY184350** s) {
	Ropeobj172006* result;
	NI L;
	result = 0;
	L = (NI)(((*s) ? (*s)->Sup.len : 0) - ((NI) 1));
	result = (*s)->data[L];
	(*s) = (TY184350*) setLengthSeq(&((*s))->Sup, sizeof(Ropeobj172006*), ((NI) (L)));
	return result;
}

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;
}

N_NIMCALL(NimStringDesc*, HEX24_172856)(Ropeobj172006* r) {
	NimStringDesc* result;
	NI LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = len_172025(r);
	result = mnewString(((NI) (LOC1)));
	result = setLengthStr(result, ((NI) 0));
	{
		NimStringDesc* s_172860;
		s_172860 = 0;
		{
			TY184350* stack_172865;
			TY172507 LOC7;
			if (!!((r == NIM_NIL))) goto LA5;
			stack_172865 = (TY184350*) newSeq((&NTI184350), 1);
			memset((void*)LOC7, 0, sizeof(LOC7));
			LOC7[0] = r;
			asgnRefNoCycle((void**) (&stack_172865->data[0]), LOC7[0]);
			{
				while (1) {
					Ropeobj172006* it_172867;
					if (!(((NI) 0) < (stack_172865 ? stack_172865->Sup.len : 0))) goto LA9;
					it_172867 = pop_172530((&stack_172865));
					{
						while (1) {
							if (!(*it_172867).data == 0) goto LA11;
							stack_172865 = (TY184350*) incrSeqV2(&(stack_172865)->Sup, sizeof(Ropeobj172006*));
							asgnRefNoCycle((void**) (&stack_172865->data[stack_172865->Sup.len]), (*it_172867).right);
							++stack_172865->Sup.len;
							it_172867 = (*it_172867).left;
						} LA11: ;
					}
					s_172860 = (*it_172867).data;
					result = resizeString(result, s_172860->Sup.len + 0);
appendString(result, s_172860);
				} LA9: ;
			}
		}
		LA5: ;
	}
	return result;
}

N_NIMCALL(Ropeobj172006*, HEX26_172447)(Ropeobj172006* a, NimStringDesc* b) {
	Ropeobj172006* result;
	Ropeobj172006* LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = rope_172277(b);
	result = HEX26_172418(a, LOC1);
	return result;
}

N_NIMCALL(void, add_172487)(Ropeobj172006** a, NimStringDesc* b) {
	unsureAsgnRef((void**) (&(*a)), HEX26_172447((*a), b));
}

N_NIMCALL(Ropeobj172006*, HEX25_172905)(NimStringDesc* frmt, Ropeobj172006** args, NI argsLen0) {
	Ropeobj172006* result;
	NI i;
	NI length;
	NI num;
	result = 0;
	i = ((NI) 0);
	length = (frmt ? frmt->Sup.len : 0);
	result = NIM_NIL;
	num = ((NI) 0);
	{
		while (1) {
			NI start;
			if (!(i < length)) goto LA2;
			{
				if (!((NU8)(frmt->data[i]) == (NU8)(36))) goto LA5;
				i += ((NI) 1);
				switch (((NU8)(frmt->data[i]))) {
				case 36:
				{
					add_172487(&result, ((NimStringDesc*) &TMP3761));
					i += ((NI) 1);
				}
				break;
				case 35:
				{
					i += ((NI) 1);
					add_172482(&result, args[num]);
					num += ((NI) 1);
				}
				break;
				case 48 ... 57:
				{
					NI j;
					j = ((NI) 0);
					{
						while (1) {
							j = (NI)((NI)((NI)(j * ((NI) 10)) + ((NI) (((NU8)(frmt->data[i]))))) - ((NI) 48));
							i += ((NI) 1);
							{
								if (!!((((NU8)(frmt->data[i])) >= ((NU8)(48)) && ((NU8)(frmt->data[i])) <= ((NU8)(57))))) goto LA14;
								goto LA10;
							}
							LA14: ;
						}
					} LA10: ;
					num = j;
					{
						NimStringDesc* LOC20;
						if (!((NI)((argsLen0-1) + ((NI) 1)) < j)) goto LA18;
						LOC20 = 0;
						LOC20 = nimIntToStr(j);
						errorhandler_172022.ClEnv? errorhandler_172022.ClPrc(((NU8) 1), LOC20, NIM_FALSE, errorhandler_172022.ClEnv):((TMP3762)(errorhandler_172022.ClPrc))(((NU8) 1), LOC20, NIM_FALSE);
					}
					goto LA16;
					LA18: ;
					{
						add_172482(&result, args[(NI)(j - ((NI) 1))]);
					}
					LA16: ;
				}
				break;
				case 123:
				{
					NI j;
					i += ((NI) 1);
					j = ((NI) 0);
					{
						while (1) {
							if (!(((NU8)(frmt->data[i])) >= ((NU8)(48)) && ((NU8)(frmt->data[i])) <= ((NU8)(57)))) goto LA24;
							j = (NI)((NI)((NI)(j * ((NI) 10)) + ((NI) (((NU8)(frmt->data[i]))))) - ((NI) 48));
							i += ((NI) 1);
						} LA24: ;
					}
					num = j;
					{
						if (!((NU8)(frmt->data[i]) == (NU8)(125))) goto LA27;
						i += ((NI) 1);
					}
					goto LA25;
					LA27: ;
					{
						NimStringDesc* LOC30;
						LOC30 = 0;
						LOC30 = nimCharToStr(frmt->data[i]);
						errorhandler_172022.ClEnv? errorhandler_172022.ClPrc(((NU8) 1), LOC30, NIM_FALSE, errorhandler_172022.ClEnv):((TMP3763)(errorhandler_172022.ClPrc))(((NU8) 1), LOC30, NIM_FALSE);
					}
					LA25: ;
					{
						NimStringDesc* LOC35;
						if (!((NI)((argsLen0-1) + ((NI) 1)) < j)) goto LA33;
						LOC35 = 0;
						LOC35 = nimIntToStr(j);
						errorhandler_172022.ClEnv? errorhandler_172022.ClPrc(((NU8) 1), LOC35, NIM_FALSE, errorhandler_172022.ClEnv):((TMP3764)(errorhandler_172022.ClPrc))(((NU8) 1), LOC35, NIM_FALSE);
					}
					goto LA31;
					LA33: ;
					{
						add_172482(&result, args[(NI)(j - ((NI) 1))]);
					}
					LA31: ;
				}
				break;
				case 110:
				{
					add_172482(&result, softrnl_172904);
					i += ((NI) 1);
				}
				break;
				case 78:
				{
					add_172482(&result, rnl_172903);
					i += ((NI) 1);
				}
				break;
				default:
				{
					NimStringDesc* LOC40;
					LOC40 = 0;
					LOC40 = nimCharToStr(frmt->data[i]);
					errorhandler_172022.ClEnv? errorhandler_172022.ClPrc(((NU8) 1), LOC40, NIM_FALSE, errorhandler_172022.ClEnv):((TMP3765)(errorhandler_172022.ClPrc))(((NU8) 1), LOC40, NIM_FALSE);
				}
				break;
				}
			}
			LA5: ;
			start = i;
			{
				while (1) {
					if (!(i < length)) goto LA42;
					{
						if (!!(((NU8)(frmt->data[i]) == (NU8)(36)))) goto LA45;
						i += ((NI) 1);
					}
					goto LA43;
					LA45: ;
					{
						goto LA41;
					}
					LA43: ;
				} LA42: ;
			} LA41: ;
			{
				NimStringDesc* LOC52;
				if (!(start <= (NI)(i - ((NI) 1)))) goto LA50;
				LOC52 = 0;
				LOC52 = copyStrLast(frmt, start, (NI)(i - ((NI) 1)));
				add_172487(&result, LOC52);
			}
			LA50: ;
		} LA2: ;
	}
	return result;
}

N_NIMCALL(Ropeobj172006*, rope_172401)(NI64 i) {
	Ropeobj172006* result;
	NimStringDesc* LOC1;
	result = 0;
	gcacheinttries_172240 += ((NI) 1);
	LOC1 = 0;
	LOC1 = nimInt64ToStr(i);
	result = rope_172277(LOC1);
	return result;
}

N_NIMCALL(void, addf_173205)(Ropeobj172006** c, NimStringDesc* frmt, Ropeobj172006** args, NI argsLen0) {
	Ropeobj172006* LOC1;
	LOC1 = 0;
	LOC1 = HEX25_172905(frmt, args, argsLen0);
	add_172482(c, LOC1);
}

N_NIMCALL(Ropeobj172006*, HEX26_172452)(NimStringDesc* a, Ropeobj172006* b) {
	Ropeobj172006* result;
	Ropeobj172006* LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = rope_172277(a);
	result = HEX26_172418(LOC1, b);
	return result;
}

N_NIMCALL(void, prepend_172893)(Ropeobj172006** a, Ropeobj172006* b) {
	unsureAsgnRef((void**) (&(*a)), HEX26_172418(b, (*a)));
}

N_NIMCALL(void, writerope_172822)(FILE* f, Ropeobj172006* r) {
	{
		NimStringDesc* s_172826;
		s_172826 = 0;
		{
			TY184350* stack_172833;
			TY172507 LOC6;
			if (!!((r == NIM_NIL))) goto LA4;
			stack_172833 = (TY184350*) newSeq((&NTI184350), 1);
			memset((void*)LOC6, 0, sizeof(LOC6));
			LOC6[0] = r;
			asgnRefNoCycle((void**) (&stack_172833->data[0]), LOC6[0]);
			{
				while (1) {
					Ropeobj172006* it_172835;
					if (!(((NI) 0) < (stack_172833 ? stack_172833->Sup.len : 0))) goto LA8;
					it_172835 = pop_172530((&stack_172833));
					{
						while (1) {
							if (!(*it_172835).data == 0) goto LA10;
							stack_172833 = (TY184350*) incrSeqV2(&(stack_172833)->Sup, sizeof(Ropeobj172006*));
							asgnRefNoCycle((void**) (&stack_172833->data[stack_172833->Sup.len]), (*it_172835).right);
							++stack_172833->Sup.len;
							it_172835 = (*it_172835).left;
						} LA10: ;
					}
					s_172826 = (*it_172835).data;
					write_15489(f, s_172826);
				} LA8: ;
			}
		}
		LA4: ;
	}
}

N_NIMCALL(void, writerope_172836)(Ropeobj172006* head, NimStringDesc* filename, NIM_BOOL usewarning) {
	FILE* f;
	f = 0;
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = open_15403(&f, filename, ((NU8) 1), ((NI) -1));
		if (!LOC3) goto LA4;
		{
			if (!!((head == NIM_NIL))) goto LA8;
			writerope_172822(f, head);
		}
		LA8: ;
		fclose(f);
	}
	goto LA1;
	LA4: ;
	{
		errorhandler_172022.ClEnv? errorhandler_172022.ClPrc(((NU8) 0), filename, usewarning, errorhandler_172022.ClEnv):((TMP4591)(errorhandler_172022.ClPrc))(((NU8) 0), filename, usewarning);
	}
	LA1: ;
}

N_NIMCALL(NIM_BOOL, equalsfile_173403)(Ropeobj172006* r, FILE* f) {
	NIM_BOOL result;
	TY173409 buf;
	NI bpos;
	NI blen;
	NI LOC26;
{	result = 0;
	memset((void*)buf, 0, sizeof(buf));
	bpos = ((NI) 1024);
	blen = ((NI) 1024);
	{
		NimStringDesc* s_173431;
		s_173431 = 0;
		{
			TY184350* stack_173480;
			TY172507 LOC6;
			if (!!((r == NIM_NIL))) goto LA4;
			stack_173480 = (TY184350*) newSeq((&NTI184350), 1);
			memset((void*)LOC6, 0, sizeof(LOC6));
			LOC6[0] = r;
			asgnRefNoCycle((void**) (&stack_173480->data[0]), LOC6[0]);
			{
				while (1) {
					Ropeobj172006* it_173482;
					NI spos;
					NI slen;
					if (!(((NI) 0) < (stack_173480 ? stack_173480->Sup.len : 0))) goto LA8;
					it_173482 = pop_172530((&stack_173480));
					{
						while (1) {
							if (!(*it_173482).data == 0) goto LA10;
							stack_173480 = (TY184350*) incrSeqV2(&(stack_173480)->Sup, sizeof(Ropeobj172006*));
							asgnRefNoCycle((void**) (&stack_173480->data[stack_173480->Sup.len]), (*it_173482).right);
							++stack_173480->Sup.len;
							it_173482 = (*it_173482).left;
						} LA10: ;
					}
					s_173431 = (*it_173482).data;
					spos = ((NI) 0);
					slen = (s_173431 ? s_173431->Sup.len : 0);
					{
						while (1) {
							NI n;
							if (!(spos < slen)) goto LA12;
							{
								if (!(bpos == blen)) goto LA15;
								bpos = ((NI) 0);
								blen = readbuffer_15554(f, ((void*) ((&buf[(((NI) 0))- 0]))), ((NI) 1024));
								{
									if (!(blen == ((NI) 0))) goto LA19;
									result = NIM_FALSE;
									goto BeforeRet;
								}
								LA19: ;
							}
							LA15: ;
							n = (((NI)(blen - bpos) <= (NI)(slen - spos)) ? (NI)(blen - bpos) : (NI)(slen - spos));
							{
								NIM_BOOL LOC23;
								LOC23 = 0;
								LOC23 = equalMem(((void*) ((&buf[(bpos)- 0]))), ((void*) ((NI)(((NI) (s_173431->data)) + spos))), ((NI) (n)));
								if (!!(LOC23)) goto LA24;
								result = NIM_FALSE;
								goto BeforeRet;
							}
							LA24: ;
							spos += n;
							bpos += n;
						} LA12: ;
					}
				} LA8: ;
			}
		}
		LA4: ;
	}
	LOC26 = 0;
	LOC26 = readbuffer_15554(f, ((void*) ((&buf[(((NI) 0))- 0]))), ((NI) 1));
	result = (LOC26 == ((NI) 0));
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NIM_BOOL, equalsfile_173483)(Ropeobj172006* r, NimStringDesc* filename) {
	NIM_BOOL result;
	FILE* f;
	result = 0;
	f = 0;
	result = open_15403(&f, filename, ((NU8) 0), ((NI) -1));
	{
		if (!result) goto LA3;
		result = equalsfile_173403(r, f);
		fclose(f);
	}
	LA3: ;
	return result;
}

N_NIMCALL(NIM_BOOL, writeropeifnotequal_173491)(Ropeobj172006* r, NimStringDesc* filename) {
	NIM_BOOL result;
	result = 0;
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = equalsfile_173483(r, filename);
		if (!!(LOC3)) goto LA4;
		writerope_172836(r, filename, NIM_FALSE);
		result = NIM_TRUE;
	}
	goto LA1;
	LA4: ;
	{
		result = NIM_FALSE;
	}
	LA1: ;
	return result;
}

N_NIMCALL(void, resetropecache_172075)(void) {
	{
		NI i_172214;
		NI res_172219;
		i_172214 = 0;
		res_172219 = ((NI) 0);
		{
			while (1) {
				if (!(res_172219 <= ((NI) 4095))) goto LA3;
				i_172214 = ((NI) (res_172219));
				asgnRefNoCycle((void**) (&cache_172073[(i_172214)- 0]), NIM_NIL);
				res_172219 += ((NI) 1);
			} LA3: ;
		}
	}
}
NIM_EXTERNC N_NOINLINE(void, compiler_ropesInit000)(void) {
	gcachetries_172238 = ((NI) 0);
	gcachemisses_172239 = ((NI) 0);
	gcacheinttries_172240 = ((NI) 0);
	asgnRefNoCycle((void**) (&rnl_172903), newrope_172041(tnl_170618));
	asgnRefNoCycle((void**) (&softrnl_172904), newrope_172041(tnl_170618));
}

NIM_EXTERNC N_NOINLINE(void, compiler_ropesDatInit000)(void) {
static TNimNode* TMP132[4];
static TNimNode TMP59[5];
NTI172006.size = sizeof(Ropeobj172006);
NTI172006.kind = 17;
NTI172006.base = (&NTI3608);
NTI172006.flags = 2;
TMP132[0] = &TMP59[1];
TMP59[1].kind = 1;
TMP59[1].offset = offsetof(Ropeobj172006, left);
TMP59[1].typ = (&NTI172004);
TMP59[1].name = "left";
TMP132[1] = &TMP59[2];
TMP59[2].kind = 1;
TMP59[2].offset = offsetof(Ropeobj172006, right);
TMP59[2].typ = (&NTI172004);
TMP59[2].name = "right";
TMP132[2] = &TMP59[3];
TMP59[3].kind = 1;
TMP59[3].offset = offsetof(Ropeobj172006, length);
TMP59[3].typ = (&NTI104);
TMP59[3].name = "length";
TMP132[3] = &TMP59[4];
TMP59[4].kind = 1;
TMP59[4].offset = offsetof(Ropeobj172006, data);
TMP59[4].typ = (&NTI138);
TMP59[4].name = "data";
TMP59[0].len = 4; TMP59[0].kind = 2; TMP59[0].sons = &TMP132[0];
NTI172006.node = &TMP59[0];
NTI172004.size = sizeof(Ropeobj172006*);
NTI172004.kind = 22;
NTI172004.base = (&NTI172006);
NTI172004.flags = 2;
NTI172004.marker = TMP133;
}

