/* Generated by Nim Compiler v0.12.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 Ropeobj174009 Ropeobj174009;
typedef struct TNimObject TNimObject;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct Cell48947 Cell48947;
typedef struct Cellseq48963 Cellseq48963;
typedef struct Gcheap51018 Gcheap51018;
typedef struct Gcstack51016 Gcstack51016;
typedef struct Cellset48959 Cellset48959;
typedef struct Pagedesc48955 Pagedesc48955;
typedef struct Memregion30890 Memregion30890;
typedef struct Smallchunk30842 Smallchunk30842;
typedef struct Llchunk30884 Llchunk30884;
typedef struct Bigchunk30844 Bigchunk30844;
typedef struct Intset30817 Intset30817;
typedef struct Trunk30813 Trunk30813;
typedef struct Avlnode30888 Avlnode30888;
typedef struct Gcstat51014 Gcstat51014;
typedef struct TY188365 TY188365;
typedef struct Basechunk30840 Basechunk30840;
typedef struct Freecell30832 Freecell30832;
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;
} TY174023;
typedef Ropeobj174009* TY174105[4096];
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  TNimObject  {
TNimType* m_type;
};
struct  Ropeobj174009  {
  TNimObject Sup;
Ropeobj174009* left;
Ropeobj174009* right;
NI length;
NimStringDesc* data;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct  Cell48947  {
NI refcount;
TNimType* typ;
};
struct  Cellseq48963  {
NI len;
NI cap;
Cell48947** d;
};
struct  Cellset48959  {
NI counter;
NI max;
Pagedesc48955* head;
Pagedesc48955** data;
};
typedef Smallchunk30842* TY30905[512];
typedef Trunk30813* Trunkbuckets30815[256];
struct  Intset30817  {
Trunkbuckets30815 data;
};
struct  Memregion30890  {
NI minlargeobj;
NI maxlargeobj;
TY30905 freesmallchunks;
Llchunk30884* llmem;
NI currmem;
NI maxmem;
NI freemem;
NI lastsize;
Bigchunk30844* freechunkslist;
Intset30817 chunkstarts;
Avlnode30888* root;
Avlnode30888* deleted;
Avlnode30888* last;
Avlnode30888* freeavlnodes;
};
struct  Gcstat51014  {
NI stackscans;
NI cyclecollections;
NI maxthreshold;
NI maxstacksize;
NI maxstackcells;
NI cycletablesize;
NI64 maxpause;
};
struct  Gcheap51018  {
Gcstack51016* stack;
void* stackbottom;
NI cyclethreshold;
Cellseq48963 zct;
Cellseq48963 decstack;
Cellset48959 cycleroots;
Cellseq48963 tempstack;
NI recgclock;
Memregion30890 region;
Gcstat51014 stat;
};
typedef Ropeobj174009* TY174572[1];
typedef N_CLOSURE_PTR(void, TMP3746) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef N_CLOSURE_PTR(void, TMP3747) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef N_CLOSURE_PTR(void, TMP3748) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef N_CLOSURE_PTR(void, TMP3749) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef N_CLOSURE_PTR(void, TMP4572) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef NIM_CHAR TY175815[1024];
struct  Gcstack51016  {
Gcstack51016* prev;
Gcstack51016* next;
void* starts;
void* pos;
NI maxstacksize;
};
typedef NI TY30821[16];
struct  Pagedesc48955  {
Pagedesc48955* next;
NI key;
TY30821 bits;
};
struct  Basechunk30840  {
NI prevsize;
NI size;
NIM_BOOL used;
};
struct  Smallchunk30842  {
  Basechunk30840 Sup;
Smallchunk30842* next;
Smallchunk30842* prev;
Freecell30832* freelist;
NI free;
NI acc;
NF data;
};
struct  Llchunk30884  {
NI size;
NI acc;
Llchunk30884* next;
};
struct  Bigchunk30844  {
  Basechunk30840 Sup;
Bigchunk30844* next;
Bigchunk30844* prev;
NI align;
NF data;
};
struct  Trunk30813  {
Trunk30813* next;
NI key;
TY30821 bits;
};
typedef Avlnode30888* TY30895[2];
struct  Avlnode30888  {
TY30895 link;
NI key;
NI upperbound;
NI level;
};
struct  Freecell30832  {
Freecell30832* next;
NI zerofield;
};
struct TY188365 {
  TGenericSeq Sup;
  Ropeobj174009* data[SEQ_DECL_SIZE];
};
N_NIMCALL(Ropeobj174009*, newrope_174056)(NimStringDesc* data);
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP131)(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(Cell48947*, usrtocell_52646)(void* usr);
static N_INLINE(void, rtladdzct_54204)(Cell48947* c);
N_NOINLINE(void, addzct_52617)(Cellseq48963* s, Cell48947* c);
static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src);
N_NIMCALL(Ropeobj174009*, HEX26_174436)(Ropeobj174009* a, Ropeobj174009* b);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(Ropeobj174009*, insertincache_174280)(NimStringDesc* s);
N_NIMCALL(NI, hash_141848)(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_174034)(Ropeobj174009* a);
N_NIMCALL(NimStringDesc*, setLengthStr)(NimStringDesc* s, NI newlen);
N_NIMCALL(void*, newSeq)(TNimType* typ, NI len);
static N_INLINE(Ropeobj174009*, pop_174595)(TY188365** 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(Ropeobj174009*, HEX26_174471)(Ropeobj174009* a, NimStringDesc* b);
N_NIMCALL(Ropeobj174009*, rope_174322)(NimStringDesc* s);
N_NIMCALL(void, add_174545)(Ropeobj174009** a, NimStringDesc* b);
N_NIMCALL(void, add_174534)(Ropeobj174009** a, Ropeobj174009* b);
N_NIMCALL(NimStringDesc*, nimIntToStr)(NI x);
N_NIMCALL(NimStringDesc*, nimCharToStr)(NIM_CHAR x);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI start_81016, NI last);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI first, NI last);
N_NIMCALL(NimStringDesc*, nimInt64ToStr)(NI64 x);
N_NIMCALL(Ropeobj174009*, HEX25_175236)(NimStringDesc* frmt, Ropeobj174009** args, NI argsLen0);
N_NIMCALL(NIM_BOOL, open_16803)(FILE** f, NimStringDesc* filename, NU8 mode, NI bufsize);
N_NIMCALL(void, writerope_175071)(FILE* f, Ropeobj174009* r);
N_NIMCALL(void, write_16895)(FILE* f, NimStringDesc* s);
N_NIMCALL(NIM_BOOL, equalsfile_175916)(Ropeobj174009* r, NimStringDesc* filename);
N_NIMCALL(NIM_BOOL, equalsfile_175809)(Ropeobj174009* r, FILE* f);
N_NIMCALL(NI, readbuffer_16960)(FILE* f, void* buffer, NI len);
N_NIMCALL(void, writerope_175106)(Ropeobj174009* head, NimStringDesc* filename, NIM_BOOL usewarning);
STRING_LITERAL(TMP3745, "$", 1);
TY174023 errorhandler_174028;
TY174105 cache_174106;
NI gcachetries_174268;
NI gcachemisses_174272;
NI gcacheinttries_174276;
Ropeobj174009* rnl_175228;
extern TNimType NTI4011; /* RootObj */
TNimType NTI174009; /* RopeObj */
extern TNimType NTI108; /* int */
extern TNimType NTI149; /* string */
TNimType NTI174007; /* Rope */
extern Gcheap51018 gch_51059;
extern NimStringDesc* tnl_172654;
Ropeobj174009* softrnl_175229;
extern TNimType NTI188365; /* seq[Rope] */
N_NIMCALL(void, TMP131)(void* p, NI op) {
	Ropeobj174009* a;
	a = (Ropeobj174009*)p;
	nimGCvisit((void*)(*a).left, op);
	nimGCvisit((void*)(*a).right, op);
	nimGCvisit((void*)(*a).data, op);
}

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

static N_INLINE(void, rtladdzct_54204)(Cell48947* c) {
	addzct_52617((&gch_51059.zct), c);
}

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

N_NIMCALL(Ropeobj174009*, newrope_174056)(NimStringDesc* data) {
	Ropeobj174009* result;
	result = 0;
	result = (Ropeobj174009*) newObj((&NTI174007), sizeof(Ropeobj174009));
	(*result).Sup.m_type = (&NTI174009);
	{
		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) {
	{
		Cell48947* c;
		if (!!((src == NIM_NIL))) goto LA3;
		c = usrtocell_52646(src);
		(*c).refcount += ((NI) 8);
	}
	LA3: ;
	{
		Cell48947* c;
		if (!!(((*dest) == NIM_NIL))) goto LA7;
		c = usrtocell_52646((*dest));
		{
			(*c).refcount -= ((NI) 8);
			if (!((NU32)((*c).refcount) < (NU32)(((NI) 8)))) goto LA11;
			rtladdzct_54204(c);
		}
		LA11: ;
	}
	LA7: ;
	(*dest) = src;
}

N_NIMCALL(Ropeobj174009*, HEX26_174436)(Ropeobj174009* a, Ropeobj174009* b) {
	Ropeobj174009* 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_174056(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_174534)(Ropeobj174009** a, Ropeobj174009* b) {
	unsureAsgnRef((void**) (&(*a)), HEX26_174436((*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(Ropeobj174009*, insertincache_174280)(NimStringDesc* s) {
	Ropeobj174009* result;
	NI h;
	NI LOC1;
	result = 0;
	gcachetries_174268 += ((NI) 1);
	LOC1 = 0;
	LOC1 = hash_141848(s);
	h = (NI)(LOC1 & ((NI) 4095));
	result = cache_174106[(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_174272 += ((NI) 1);
		result = newrope_174056(s);
		asgnRefNoCycle((void**) (&cache_174106[(h)- 0]), result);
	}
	LA6: ;
	return result;
}

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

N_NIMCALL(NI, len_174034)(Ropeobj174009* 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(Ropeobj174009*, pop_174595)(TY188365** s) {
	Ropeobj174009* result;
	NI L;
	result = 0;
	L = (NI)(((*s) ? (*s)->Sup.len : 0) - ((NI) 1));
	result = (*s)->data[L];
	(*s) = (TY188365*) setLengthSeq(&((*s))->Sup, sizeof(Ropeobj174009*), ((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_175132)(Ropeobj174009* r) {
	NimStringDesc* result;
	NI LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = len_174034(r);
	result = mnewString(((NI) (LOC1)));
	result = setLengthStr(result, ((NI) 0));
	{
		NimStringDesc* s_175136;
		s_175136 = 0;
		{
			TY188365* stack_175141;
			TY174572 LOC7;
			if (!!((r == NIM_NIL))) goto LA5;
			stack_175141 = (TY188365*) newSeq((&NTI188365), 1);
			memset((void*)LOC7, 0, sizeof(LOC7));
			LOC7[0] = r;
			asgnRefNoCycle((void**) (&stack_175141->data[0]), LOC7[0]);
			{
				while (1) {
					Ropeobj174009* it_175143;
					if (!(((NI) 0) < (stack_175141 ? stack_175141->Sup.len : 0))) goto LA9;
					it_175143 = pop_174595((&stack_175141));
					{
						while (1) {
							if (!(*it_175143).data == 0) goto LA11;
							stack_175141 = (TY188365*) incrSeqV2(&(stack_175141)->Sup, sizeof(Ropeobj174009*));
							asgnRefNoCycle((void**) (&stack_175141->data[stack_175141->Sup.len]), (*it_175143).right);
							++stack_175141->Sup.len;
							it_175143 = (*it_175143).left;
						} LA11: ;
					}
					s_175136 = (*it_175143).data;
					result = resizeString(result, s_175136->Sup.len + 0);
appendString(result, s_175136);
				} LA9: ;
			}
		}
		LA5: ;
	}
	return result;
}

N_NIMCALL(Ropeobj174009*, HEX26_174471)(Ropeobj174009* a, NimStringDesc* b) {
	Ropeobj174009* result;
	Ropeobj174009* LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = rope_174322(b);
	result = HEX26_174436(a, LOC1);
	return result;
}

N_NIMCALL(void, add_174545)(Ropeobj174009** a, NimStringDesc* b) {
	unsureAsgnRef((void**) (&(*a)), HEX26_174471((*a), b));
}

N_NIMCALL(Ropeobj174009*, HEX25_175236)(NimStringDesc* frmt, Ropeobj174009** args, NI argsLen0) {
	Ropeobj174009* 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_174545(&result, ((NimStringDesc*) &TMP3745));
					i += ((NI) 1);
				}
				break;
				case 35:
				{
					i += ((NI) 1);
					add_174534(&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_174028.ClEnv? errorhandler_174028.ClPrc(((NU8) 1), LOC20, NIM_FALSE, errorhandler_174028.ClEnv):((TMP3746)(errorhandler_174028.ClPrc))(((NU8) 1), LOC20, NIM_FALSE);
					}
					goto LA16;
					LA18: ;
					{
						add_174534(&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_174028.ClEnv? errorhandler_174028.ClPrc(((NU8) 1), LOC30, NIM_FALSE, errorhandler_174028.ClEnv):((TMP3747)(errorhandler_174028.ClPrc))(((NU8) 1), LOC30, NIM_FALSE);
					}
					LA25: ;
					{
						NimStringDesc* LOC35;
						if (!((NI)((argsLen0-1) + ((NI) 1)) < j)) goto LA33;
						LOC35 = 0;
						LOC35 = nimIntToStr(j);
						errorhandler_174028.ClEnv? errorhandler_174028.ClPrc(((NU8) 1), LOC35, NIM_FALSE, errorhandler_174028.ClEnv):((TMP3748)(errorhandler_174028.ClPrc))(((NU8) 1), LOC35, NIM_FALSE);
					}
					goto LA31;
					LA33: ;
					{
						add_174534(&result, args[(NI)(j - ((NI) 1))]);
					}
					LA31: ;
				}
				break;
				case 110:
				{
					add_174534(&result, softrnl_175229);
					i += ((NI) 1);
				}
				break;
				case 78:
				{
					add_174534(&result, rnl_175228);
					i += ((NI) 1);
				}
				break;
				default:
				{
					NimStringDesc* LOC40;
					LOC40 = 0;
					LOC40 = nimCharToStr(frmt->data[i]);
					errorhandler_174028.ClEnv? errorhandler_174028.ClPrc(((NU8) 1), LOC40, NIM_FALSE, errorhandler_174028.ClEnv):((TMP3749)(errorhandler_174028.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_174545(&result, LOC52);
			}
			LA50: ;
		} LA2: ;
	}
	return result;
}

N_NIMCALL(Ropeobj174009*, rope_174407)(NI64 i) {
	Ropeobj174009* result;
	NimStringDesc* LOC1;
	result = 0;
	gcacheinttries_174276 += ((NI) 1);
	LOC1 = 0;
	LOC1 = nimInt64ToStr(i);
	result = rope_174322(LOC1);
	return result;
}

N_NIMCALL(void, addf_175635)(Ropeobj174009** c, NimStringDesc* frmt, Ropeobj174009** args, NI argsLen0) {
	Ropeobj174009* LOC1;
	LOC1 = 0;
	LOC1 = HEX25_175236(frmt, args, argsLen0);
	add_174534(c, LOC1);
}

N_NIMCALL(Ropeobj174009*, HEX26_174482)(NimStringDesc* a, Ropeobj174009* b) {
	Ropeobj174009* result;
	Ropeobj174009* LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = rope_174322(a);
	result = HEX26_174436(LOC1, b);
	return result;
}

N_NIMCALL(void, prepend_175206)(Ropeobj174009** a, Ropeobj174009* b) {
	unsureAsgnRef((void**) (&(*a)), HEX26_174436(b, (*a)));
}

N_NIMCALL(void, writerope_175071)(FILE* f, Ropeobj174009* r) {
	{
		NimStringDesc* s_175075;
		s_175075 = 0;
		{
			TY188365* stack_175082;
			TY174572 LOC6;
			if (!!((r == NIM_NIL))) goto LA4;
			stack_175082 = (TY188365*) newSeq((&NTI188365), 1);
			memset((void*)LOC6, 0, sizeof(LOC6));
			LOC6[0] = r;
			asgnRefNoCycle((void**) (&stack_175082->data[0]), LOC6[0]);
			{
				while (1) {
					Ropeobj174009* it_175084;
					if (!(((NI) 0) < (stack_175082 ? stack_175082->Sup.len : 0))) goto LA8;
					it_175084 = pop_174595((&stack_175082));
					{
						while (1) {
							if (!(*it_175084).data == 0) goto LA10;
							stack_175082 = (TY188365*) incrSeqV2(&(stack_175082)->Sup, sizeof(Ropeobj174009*));
							asgnRefNoCycle((void**) (&stack_175082->data[stack_175082->Sup.len]), (*it_175084).right);
							++stack_175082->Sup.len;
							it_175084 = (*it_175084).left;
						} LA10: ;
					}
					s_175075 = (*it_175084).data;
					write_16895(f, s_175075);
				} LA8: ;
			}
		}
		LA4: ;
	}
}

N_NIMCALL(void, writerope_175106)(Ropeobj174009* head, NimStringDesc* filename, NIM_BOOL usewarning) {
	FILE* f;
	f = 0;
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = open_16803(&f, filename, ((NU8) 1), ((NI) -1));
		if (!LOC3) goto LA4;
		{
			if (!!((head == NIM_NIL))) goto LA8;
			writerope_175071(f, head);
		}
		LA8: ;
		fclose(f);
	}
	goto LA1;
	LA4: ;
	{
		errorhandler_174028.ClEnv? errorhandler_174028.ClPrc(((NU8) 0), filename, usewarning, errorhandler_174028.ClEnv):((TMP4572)(errorhandler_174028.ClPrc))(((NU8) 0), filename, usewarning);
	}
	LA1: ;
}

N_NIMCALL(NIM_BOOL, equalsfile_175809)(Ropeobj174009* r, FILE* f) {
	NIM_BOOL result;
	TY175815 buf;
	NI bpos;
	NI blen;
	NI LOC26;
{	result = 0;
	memset((void*)buf, 0, sizeof(buf));
	bpos = ((NI) 1024);
	blen = ((NI) 1024);
	{
		NimStringDesc* s_175837;
		s_175837 = 0;
		{
			TY188365* stack_175886;
			TY174572 LOC6;
			if (!!((r == NIM_NIL))) goto LA4;
			stack_175886 = (TY188365*) newSeq((&NTI188365), 1);
			memset((void*)LOC6, 0, sizeof(LOC6));
			LOC6[0] = r;
			asgnRefNoCycle((void**) (&stack_175886->data[0]), LOC6[0]);
			{
				while (1) {
					Ropeobj174009* it_175888;
					NI spos;
					NI slen;
					if (!(((NI) 0) < (stack_175886 ? stack_175886->Sup.len : 0))) goto LA8;
					it_175888 = pop_174595((&stack_175886));
					{
						while (1) {
							if (!(*it_175888).data == 0) goto LA10;
							stack_175886 = (TY188365*) incrSeqV2(&(stack_175886)->Sup, sizeof(Ropeobj174009*));
							asgnRefNoCycle((void**) (&stack_175886->data[stack_175886->Sup.len]), (*it_175888).right);
							++stack_175886->Sup.len;
							it_175888 = (*it_175888).left;
						} LA10: ;
					}
					s_175837 = (*it_175888).data;
					spos = ((NI) 0);
					slen = (s_175837 ? s_175837->Sup.len : 0);
					{
						while (1) {
							NI n;
							if (!(spos < slen)) goto LA12;
							{
								if (!(bpos == blen)) goto LA15;
								bpos = ((NI) 0);
								blen = readbuffer_16960(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_175837->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_16960(f, ((void*) ((&buf[(((NI) 0))- 0]))), ((NI) 1));
	result = (LOC26 == ((NI) 0));
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NIM_BOOL, equalsfile_175916)(Ropeobj174009* r, NimStringDesc* filename) {
	NIM_BOOL result;
	FILE* f;
	result = 0;
	f = 0;
	result = open_16803(&f, filename, ((NU8) 0), ((NI) -1));
	{
		if (!result) goto LA3;
		result = equalsfile_175809(r, f);
		fclose(f);
	}
	LA3: ;
	return result;
}

N_NIMCALL(NIM_BOOL, writeropeifnotequal_175930)(Ropeobj174009* r, NimStringDesc* filename) {
	NIM_BOOL result;
	result = 0;
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = equalsfile_175916(r, filename);
		if (!!(LOC3)) goto LA4;
		writerope_175106(r, filename, NIM_FALSE);
		result = NIM_TRUE;
	}
	goto LA1;
	LA4: ;
	{
		result = NIM_FALSE;
	}
	LA1: ;
	return result;
}

N_NIMCALL(void, resetropecache_174111)(void) {
	{
		NI i_174223;
		NI res_174228;
		i_174223 = 0;
		res_174228 = ((NI) 0);
		{
			while (1) {
				if (!(res_174228 <= ((NI) 4095))) goto LA3;
				i_174223 = ((NI) (res_174228));
				asgnRefNoCycle((void**) (&cache_174106[(i_174223)- 0]), NIM_NIL);
				res_174228 += ((NI) 1);
			} LA3: ;
		}
	}
}
NIM_EXTERNC N_NOINLINE(void, compiler_ropesInit000)(void) {
	gcachetries_174268 = ((NI) 0);
	gcachemisses_174272 = ((NI) 0);
	gcacheinttries_174276 = ((NI) 0);
	asgnRefNoCycle((void**) (&rnl_175228), newrope_174056(tnl_172654));
	asgnRefNoCycle((void**) (&softrnl_175229), newrope_174056(tnl_172654));
}

NIM_EXTERNC N_NOINLINE(void, compiler_ropesDatInit000)(void) {
static TNimNode* TMP130[4];
static TNimNode TMP57[5];
NTI174009.size = sizeof(Ropeobj174009);
NTI174009.kind = 17;
NTI174009.base = (&NTI4011);
NTI174009.flags = 2;
TMP130[0] = &TMP57[1];
TMP57[1].kind = 1;
TMP57[1].offset = offsetof(Ropeobj174009, left);
TMP57[1].typ = (&NTI174007);
TMP57[1].name = "left";
TMP130[1] = &TMP57[2];
TMP57[2].kind = 1;
TMP57[2].offset = offsetof(Ropeobj174009, right);
TMP57[2].typ = (&NTI174007);
TMP57[2].name = "right";
TMP130[2] = &TMP57[3];
TMP57[3].kind = 1;
TMP57[3].offset = offsetof(Ropeobj174009, length);
TMP57[3].typ = (&NTI108);
TMP57[3].name = "length";
TMP130[3] = &TMP57[4];
TMP57[4].kind = 1;
TMP57[4].offset = offsetof(Ropeobj174009, data);
TMP57[4].typ = (&NTI149);
TMP57[4].name = "data";
TMP57[0].len = 4; TMP57[0].kind = 2; TMP57[0].sons = &TMP130[0];
NTI174009.node = &TMP57[0];
NTI174007.size = sizeof(Ropeobj174009*);
NTI174007.kind = 22;
NTI174007.base = (&NTI174009);
NTI174007.flags = 2;
NTI174007.marker = TMP131;
}

