/* 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 Ropeobj175009 Ropeobj175009;
typedef struct TNimObject TNimObject;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct Cell48946 Cell48946;
typedef struct Cellseq48962 Cellseq48962;
typedef struct Gcheap51018 Gcheap51018;
typedef struct Gcstack51016 Gcstack51016;
typedef struct Cellset48958 Cellset48958;
typedef struct Pagedesc48954 Pagedesc48954;
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 TY189365 TY189365;
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;
} TY175023;
typedef Ropeobj175009* TY175105[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  Ropeobj175009  {
  TNimObject Sup;
Ropeobj175009* left;
Ropeobj175009* right;
NI length;
NimStringDesc* data;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct  Cell48946  {
NI refcount;
TNimType* typ;
};
struct  Cellseq48962  {
NI len;
NI cap;
Cell48946** d;
};
struct  Cellset48958  {
NI counter;
NI max;
Pagedesc48954* head;
Pagedesc48954** 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;
Cellseq48962 zct;
Cellseq48962 decstack;
Cellset48958 cycleroots;
Cellseq48962 tempstack;
NI recgclock;
Memregion30890 region;
Gcstat51014 stat;
};
typedef Ropeobj175009* TY175572[1];
typedef N_CLOSURE_PTR(void, TMP3745) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
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, TMP4571) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef NIM_CHAR TY176815[1024];
struct  Gcstack51016  {
Gcstack51016* prev;
Gcstack51016* next;
void* starts;
void* pos;
NI maxstacksize;
};
typedef NI TY30821[16];
struct  Pagedesc48954  {
Pagedesc48954* 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 TY189365 {
  TGenericSeq Sup;
  Ropeobj175009* data[SEQ_DECL_SIZE];
};
N_NIMCALL(Ropeobj175009*, newrope_175056)(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(Cell48946*, usrtocell_52646)(void* usr);
static N_INLINE(void, rtladdzct_54204)(Cell48946* c);
N_NOINLINE(void, addzct_52617)(Cellseq48962* s, Cell48946* c);
static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src);
N_NIMCALL(Ropeobj175009*, HEX26_175436)(Ropeobj175009* a, Ropeobj175009* b);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(Ropeobj175009*, insertincache_175280)(NimStringDesc* s);
N_NIMCALL(NI, hash_142848)(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_175034)(Ropeobj175009* a);
N_NIMCALL(NimStringDesc*, setLengthStr)(NimStringDesc* s, NI newlen);
N_NIMCALL(void*, newSeq)(TNimType* typ, NI len);
static N_INLINE(Ropeobj175009*, pop_175595)(TY189365** 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(Ropeobj175009*, HEX26_175471)(Ropeobj175009* a, NimStringDesc* b);
N_NIMCALL(Ropeobj175009*, rope_175322)(NimStringDesc* s);
N_NIMCALL(void, add_175545)(Ropeobj175009** a, NimStringDesc* b);
N_NIMCALL(void, add_175534)(Ropeobj175009** a, Ropeobj175009* 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(Ropeobj175009*, HEX25_176236)(NimStringDesc* frmt, Ropeobj175009** args, NI argsLen0);
N_NIMCALL(NIM_BOOL, open_16803)(FILE** f, NimStringDesc* filename, NU8 mode, NI bufsize);
N_NIMCALL(void, writerope_176071)(FILE* f, Ropeobj175009* r);
N_NIMCALL(void, write_16895)(FILE* f, NimStringDesc* s);
N_NIMCALL(NIM_BOOL, equalsfile_176916)(Ropeobj175009* r, NimStringDesc* filename);
N_NIMCALL(NIM_BOOL, equalsfile_176809)(Ropeobj175009* r, FILE* f);
N_NIMCALL(NI, readbuffer_16960)(FILE* f, void* buffer, NI len);
N_NIMCALL(void, writerope_176106)(Ropeobj175009* head, NimStringDesc* filename, NIM_BOOL usewarning);
STRING_LITERAL(TMP3744, "$", 1);
TY175023 errorhandler_175028;
TY175105 cache_175106;
NI gcachetries_175268;
NI gcachemisses_175272;
NI gcacheinttries_175276;
Ropeobj175009* rnl_176228;
extern TNimType NTI4011; /* RootObj */
TNimType NTI175009; /* RopeObj */
extern TNimType NTI108; /* int */
extern TNimType NTI149; /* string */
TNimType NTI175007; /* Rope */
extern Gcheap51018 gch_51059;
extern NimStringDesc* tnl_173654;
Ropeobj175009* softrnl_176229;
extern TNimType NTI189365; /* seq[Rope] */
N_NIMCALL(void, TMP131)(void* p, NI op) {
	Ropeobj175009* a;
	a = (Ropeobj175009*)p;
	nimGCvisit((void*)(*a).left, op);
	nimGCvisit((void*)(*a).right, op);
	nimGCvisit((void*)(*a).data, op);
}

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

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

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

N_NIMCALL(Ropeobj175009*, newrope_175056)(NimStringDesc* data) {
	Ropeobj175009* result;
	result = 0;
	result = (Ropeobj175009*) newObj((&NTI175007), sizeof(Ropeobj175009));
	(*result).Sup.m_type = (&NTI175009);
	{
		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) {
	{
		Cell48946* c;
		if (!!((src == NIM_NIL))) goto LA3;
		c = usrtocell_52646(src);
		(*c).refcount += ((NI) 8);
	}
	LA3: ;
	{
		Cell48946* 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(Ropeobj175009*, HEX26_175436)(Ropeobj175009* a, Ropeobj175009* b) {
	Ropeobj175009* 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_175056(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_175534)(Ropeobj175009** a, Ropeobj175009* b) {
	unsureAsgnRef((void**) (&(*a)), HEX26_175436((*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(Ropeobj175009*, insertincache_175280)(NimStringDesc* s) {
	Ropeobj175009* result;
	NI h;
	NI LOC1;
	result = 0;
	gcachetries_175268 += ((NI) 1);
	LOC1 = 0;
	LOC1 = hash_142848(s);
	h = (NI)(LOC1 & ((NI) 4095));
	result = cache_175106[(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_175272 += ((NI) 1);
		result = newrope_175056(s);
		asgnRefNoCycle((void**) (&cache_175106[(h)- 0]), result);
	}
	LA6: ;
	return result;
}

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

N_NIMCALL(NI, len_175034)(Ropeobj175009* 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(Ropeobj175009*, pop_175595)(TY189365** s) {
	Ropeobj175009* result;
	NI L;
	result = 0;
	L = (NI)(((*s) ? (*s)->Sup.len : 0) - ((NI) 1));
	result = (*s)->data[L];
	(*s) = (TY189365*) setLengthSeq(&((*s))->Sup, sizeof(Ropeobj175009*), ((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_176132)(Ropeobj175009* r) {
	NimStringDesc* result;
	NI LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = len_175034(r);
	result = mnewString(((NI) (LOC1)));
	result = setLengthStr(result, ((NI) 0));
	{
		NimStringDesc* s_176136;
		s_176136 = 0;
		{
			TY189365* stack_176141;
			TY175572 LOC7;
			if (!!((r == NIM_NIL))) goto LA5;
			stack_176141 = (TY189365*) newSeq((&NTI189365), 1);
			memset((void*)LOC7, 0, sizeof(LOC7));
			LOC7[0] = r;
			asgnRefNoCycle((void**) (&stack_176141->data[0]), LOC7[0]);
			{
				while (1) {
					Ropeobj175009* it_176143;
					if (!(((NI) 0) < (stack_176141 ? stack_176141->Sup.len : 0))) goto LA9;
					it_176143 = pop_175595((&stack_176141));
					{
						while (1) {
							if (!(*it_176143).data == 0) goto LA11;
							stack_176141 = (TY189365*) incrSeqV2(&(stack_176141)->Sup, sizeof(Ropeobj175009*));
							asgnRefNoCycle((void**) (&stack_176141->data[stack_176141->Sup.len]), (*it_176143).right);
							++stack_176141->Sup.len;
							it_176143 = (*it_176143).left;
						} LA11: ;
					}
					s_176136 = (*it_176143).data;
					result = resizeString(result, s_176136->Sup.len + 0);
appendString(result, s_176136);
				} LA9: ;
			}
		}
		LA5: ;
	}
	return result;
}

N_NIMCALL(Ropeobj175009*, HEX26_175471)(Ropeobj175009* a, NimStringDesc* b) {
	Ropeobj175009* result;
	Ropeobj175009* LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = rope_175322(b);
	result = HEX26_175436(a, LOC1);
	return result;
}

N_NIMCALL(void, add_175545)(Ropeobj175009** a, NimStringDesc* b) {
	unsureAsgnRef((void**) (&(*a)), HEX26_175471((*a), b));
}

N_NIMCALL(Ropeobj175009*, HEX25_176236)(NimStringDesc* frmt, Ropeobj175009** args, NI argsLen0) {
	Ropeobj175009* 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_175545(&result, ((NimStringDesc*) &TMP3744));
					i += ((NI) 1);
				}
				break;
				case 35:
				{
					i += ((NI) 1);
					add_175534(&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_175028.ClEnv? errorhandler_175028.ClPrc(((NU8) 1), LOC20, NIM_FALSE, errorhandler_175028.ClEnv):((TMP3745)(errorhandler_175028.ClPrc))(((NU8) 1), LOC20, NIM_FALSE);
					}
					goto LA16;
					LA18: ;
					{
						add_175534(&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_175028.ClEnv? errorhandler_175028.ClPrc(((NU8) 1), LOC30, NIM_FALSE, errorhandler_175028.ClEnv):((TMP3746)(errorhandler_175028.ClPrc))(((NU8) 1), LOC30, NIM_FALSE);
					}
					LA25: ;
					{
						NimStringDesc* LOC35;
						if (!((NI)((argsLen0-1) + ((NI) 1)) < j)) goto LA33;
						LOC35 = 0;
						LOC35 = nimIntToStr(j);
						errorhandler_175028.ClEnv? errorhandler_175028.ClPrc(((NU8) 1), LOC35, NIM_FALSE, errorhandler_175028.ClEnv):((TMP3747)(errorhandler_175028.ClPrc))(((NU8) 1), LOC35, NIM_FALSE);
					}
					goto LA31;
					LA33: ;
					{
						add_175534(&result, args[(NI)(j - ((NI) 1))]);
					}
					LA31: ;
				}
				break;
				case 110:
				{
					add_175534(&result, softrnl_176229);
					i += ((NI) 1);
				}
				break;
				case 78:
				{
					add_175534(&result, rnl_176228);
					i += ((NI) 1);
				}
				break;
				default:
				{
					NimStringDesc* LOC40;
					LOC40 = 0;
					LOC40 = nimCharToStr(frmt->data[i]);
					errorhandler_175028.ClEnv? errorhandler_175028.ClPrc(((NU8) 1), LOC40, NIM_FALSE, errorhandler_175028.ClEnv):((TMP3748)(errorhandler_175028.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_175545(&result, LOC52);
			}
			LA50: ;
		} LA2: ;
	}
	return result;
}

N_NIMCALL(Ropeobj175009*, rope_175407)(NI64 i) {
	Ropeobj175009* result;
	NimStringDesc* LOC1;
	result = 0;
	gcacheinttries_175276 += ((NI) 1);
	LOC1 = 0;
	LOC1 = nimInt64ToStr(i);
	result = rope_175322(LOC1);
	return result;
}

N_NIMCALL(void, addf_176635)(Ropeobj175009** c, NimStringDesc* frmt, Ropeobj175009** args, NI argsLen0) {
	Ropeobj175009* LOC1;
	LOC1 = 0;
	LOC1 = HEX25_176236(frmt, args, argsLen0);
	add_175534(c, LOC1);
}

N_NIMCALL(Ropeobj175009*, HEX26_175482)(NimStringDesc* a, Ropeobj175009* b) {
	Ropeobj175009* result;
	Ropeobj175009* LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = rope_175322(a);
	result = HEX26_175436(LOC1, b);
	return result;
}

N_NIMCALL(void, prepend_176206)(Ropeobj175009** a, Ropeobj175009* b) {
	unsureAsgnRef((void**) (&(*a)), HEX26_175436(b, (*a)));
}

N_NIMCALL(void, writerope_176071)(FILE* f, Ropeobj175009* r) {
	{
		NimStringDesc* s_176075;
		s_176075 = 0;
		{
			TY189365* stack_176082;
			TY175572 LOC6;
			if (!!((r == NIM_NIL))) goto LA4;
			stack_176082 = (TY189365*) newSeq((&NTI189365), 1);
			memset((void*)LOC6, 0, sizeof(LOC6));
			LOC6[0] = r;
			asgnRefNoCycle((void**) (&stack_176082->data[0]), LOC6[0]);
			{
				while (1) {
					Ropeobj175009* it_176084;
					if (!(((NI) 0) < (stack_176082 ? stack_176082->Sup.len : 0))) goto LA8;
					it_176084 = pop_175595((&stack_176082));
					{
						while (1) {
							if (!(*it_176084).data == 0) goto LA10;
							stack_176082 = (TY189365*) incrSeqV2(&(stack_176082)->Sup, sizeof(Ropeobj175009*));
							asgnRefNoCycle((void**) (&stack_176082->data[stack_176082->Sup.len]), (*it_176084).right);
							++stack_176082->Sup.len;
							it_176084 = (*it_176084).left;
						} LA10: ;
					}
					s_176075 = (*it_176084).data;
					write_16895(f, s_176075);
				} LA8: ;
			}
		}
		LA4: ;
	}
}

N_NIMCALL(void, writerope_176106)(Ropeobj175009* 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_176071(f, head);
		}
		LA8: ;
		fclose(f);
	}
	goto LA1;
	LA4: ;
	{
		errorhandler_175028.ClEnv? errorhandler_175028.ClPrc(((NU8) 0), filename, usewarning, errorhandler_175028.ClEnv):((TMP4571)(errorhandler_175028.ClPrc))(((NU8) 0), filename, usewarning);
	}
	LA1: ;
}

N_NIMCALL(NIM_BOOL, equalsfile_176809)(Ropeobj175009* r, FILE* f) {
	NIM_BOOL result;
	TY176815 buf;
	NI bpos;
	NI blen;
	NI LOC26;
{	result = 0;
	memset((void*)buf, 0, sizeof(buf));
	bpos = ((NI) 1024);
	blen = ((NI) 1024);
	{
		NimStringDesc* s_176837;
		s_176837 = 0;
		{
			TY189365* stack_176886;
			TY175572 LOC6;
			if (!!((r == NIM_NIL))) goto LA4;
			stack_176886 = (TY189365*) newSeq((&NTI189365), 1);
			memset((void*)LOC6, 0, sizeof(LOC6));
			LOC6[0] = r;
			asgnRefNoCycle((void**) (&stack_176886->data[0]), LOC6[0]);
			{
				while (1) {
					Ropeobj175009* it_176888;
					NI spos;
					NI slen;
					if (!(((NI) 0) < (stack_176886 ? stack_176886->Sup.len : 0))) goto LA8;
					it_176888 = pop_175595((&stack_176886));
					{
						while (1) {
							if (!(*it_176888).data == 0) goto LA10;
							stack_176886 = (TY189365*) incrSeqV2(&(stack_176886)->Sup, sizeof(Ropeobj175009*));
							asgnRefNoCycle((void**) (&stack_176886->data[stack_176886->Sup.len]), (*it_176888).right);
							++stack_176886->Sup.len;
							it_176888 = (*it_176888).left;
						} LA10: ;
					}
					s_176837 = (*it_176888).data;
					spos = ((NI) 0);
					slen = (s_176837 ? s_176837->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_176837->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_176916)(Ropeobj175009* 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_176809(r, f);
		fclose(f);
	}
	LA3: ;
	return result;
}

N_NIMCALL(NIM_BOOL, writeropeifnotequal_176930)(Ropeobj175009* r, NimStringDesc* filename) {
	NIM_BOOL result;
	result = 0;
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = equalsfile_176916(r, filename);
		if (!!(LOC3)) goto LA4;
		writerope_176106(r, filename, NIM_FALSE);
		result = NIM_TRUE;
	}
	goto LA1;
	LA4: ;
	{
		result = NIM_FALSE;
	}
	LA1: ;
	return result;
}

N_NIMCALL(void, resetropecache_175111)(void) {
	{
		NI i_175223;
		NI res_175228;
		i_175223 = 0;
		res_175228 = ((NI) 0);
		{
			while (1) {
				if (!(res_175228 <= ((NI) 4095))) goto LA3;
				i_175223 = ((NI) (res_175228));
				asgnRefNoCycle((void**) (&cache_175106[(i_175223)- 0]), NIM_NIL);
				res_175228 += ((NI) 1);
			} LA3: ;
		}
	}
}
NIM_EXTERNC N_NOINLINE(void, compiler_ropesInit000)(void) {
	gcachetries_175268 = ((NI) 0);
	gcachemisses_175272 = ((NI) 0);
	gcacheinttries_175276 = ((NI) 0);
	asgnRefNoCycle((void**) (&rnl_176228), newrope_175056(tnl_173654));
	asgnRefNoCycle((void**) (&softrnl_176229), newrope_175056(tnl_173654));
}

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

