/* 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 Ropeobj176009 Ropeobj176009;
typedef struct TNimObject TNimObject;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct Cell49147 Cell49147;
typedef struct Cellseq49163 Cellseq49163;
typedef struct Gcheap51218 Gcheap51218;
typedef struct Gcstack51216 Gcstack51216;
typedef struct Cellset49159 Cellset49159;
typedef struct Pagedesc49155 Pagedesc49155;
typedef struct Memregion31090 Memregion31090;
typedef struct Smallchunk31042 Smallchunk31042;
typedef struct Llchunk31084 Llchunk31084;
typedef struct Bigchunk31044 Bigchunk31044;
typedef struct Intset31017 Intset31017;
typedef struct Trunk31013 Trunk31013;
typedef struct Avlnode31088 Avlnode31088;
typedef struct Gcstat51214 Gcstat51214;
typedef struct TY190365 TY190365;
typedef struct Basechunk31040 Basechunk31040;
typedef struct Freecell31032 Freecell31032;
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;
} TY176023;
typedef Ropeobj176009* TY176105[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  Ropeobj176009  {
  TNimObject Sup;
Ropeobj176009* left;
Ropeobj176009* right;
NI length;
NimStringDesc* data;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct  Cell49147  {
NI refcount;
TNimType* typ;
};
struct  Cellseq49163  {
NI len;
NI cap;
Cell49147** d;
};
struct  Cellset49159  {
NI counter;
NI max;
Pagedesc49155* head;
Pagedesc49155** data;
};
typedef Smallchunk31042* TY31105[512];
typedef Trunk31013* Trunkbuckets31015[256];
struct  Intset31017  {
Trunkbuckets31015 data;
};
struct  Memregion31090  {
NI minlargeobj;
NI maxlargeobj;
TY31105 freesmallchunks;
Llchunk31084* llmem;
NI currmem;
NI maxmem;
NI freemem;
NI lastsize;
Bigchunk31044* freechunkslist;
Intset31017 chunkstarts;
Avlnode31088* root;
Avlnode31088* deleted;
Avlnode31088* last;
Avlnode31088* 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;
Cellseq49163 zct;
Cellseq49163 decstack;
Cellset49159 cycleroots;
Cellseq49163 tempstack;
NI recgclock;
Memregion31090 region;
Gcstat51214 stat;
};
typedef Ropeobj176009* TY176572[1];
typedef N_CLOSURE_PTR(void, TMP3742) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef N_CLOSURE_PTR(void, TMP3743) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef N_CLOSURE_PTR(void, TMP3744) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef N_CLOSURE_PTR(void, TMP3745) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef N_CLOSURE_PTR(void, TMP4568) (NU8 err, NimStringDesc* msg, NIM_BOOL usewarning);
typedef NIM_CHAR TY177815[1024];
struct  Gcstack51216  {
Gcstack51216* prev;
Gcstack51216* next;
void* starts;
void* pos;
NI maxstacksize;
};
typedef NI TY31021[16];
struct  Pagedesc49155  {
Pagedesc49155* next;
NI key;
TY31021 bits;
};
struct  Basechunk31040  {
NI prevsize;
NI size;
NIM_BOOL used;
};
struct  Smallchunk31042  {
  Basechunk31040 Sup;
Smallchunk31042* next;
Smallchunk31042* prev;
Freecell31032* freelist;
NI free;
NI acc;
NF data;
};
struct  Llchunk31084  {
NI size;
NI acc;
Llchunk31084* next;
};
struct  Bigchunk31044  {
  Basechunk31040 Sup;
Bigchunk31044* next;
Bigchunk31044* prev;
NI align;
NF data;
};
struct  Trunk31013  {
Trunk31013* next;
NI key;
TY31021 bits;
};
typedef Avlnode31088* TY31095[2];
struct  Avlnode31088  {
TY31095 link;
NI key;
NI upperbound;
NI level;
};
struct  Freecell31032  {
Freecell31032* next;
NI zerofield;
};
struct TY190365 {
  TGenericSeq Sup;
  Ropeobj176009* data[SEQ_DECL_SIZE];
};
N_NIMCALL(Ropeobj176009*, newrope_176056)(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(Cell49147*, usrtocell_52846)(void* usr);
static N_INLINE(void, rtladdzct_54404)(Cell49147* c);
N_NOINLINE(void, addzct_52817)(Cellseq49163* s, Cell49147* c);
static N_INLINE(void, asgnRefNoCycle)(void** dest, void* src);
N_NIMCALL(Ropeobj176009*, HEX26_176436)(Ropeobj176009* a, Ropeobj176009* b);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(Ropeobj176009*, insertincache_176280)(NimStringDesc* s);
N_NIMCALL(NI, hash_143848)(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_176034)(Ropeobj176009* a);
N_NIMCALL(NimStringDesc*, setLengthStr)(NimStringDesc* s, NI newlen);
N_NIMCALL(void*, newSeq)(TNimType* typ, NI len);
static N_INLINE(Ropeobj176009*, pop_176595)(TY190365** 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(Ropeobj176009*, HEX26_176471)(Ropeobj176009* a, NimStringDesc* b);
N_NIMCALL(Ropeobj176009*, rope_176322)(NimStringDesc* s);
N_NIMCALL(void, add_176545)(Ropeobj176009** a, NimStringDesc* b);
N_NIMCALL(void, add_176534)(Ropeobj176009** a, Ropeobj176009* b);
N_NIMCALL(NimStringDesc*, nimIntToStr)(NI x);
N_NIMCALL(NimStringDesc*, nimCharToStr)(NIM_CHAR x);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI start_81216, NI last);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI first, NI last);
N_NIMCALL(NimStringDesc*, nimInt64ToStr)(NI64 x);
N_NIMCALL(Ropeobj176009*, HEX25_177236)(NimStringDesc* frmt, Ropeobj176009** args, NI argsLen0);
N_NIMCALL(NIM_BOOL, open_17003)(FILE** f, NimStringDesc* filename, NU8 mode, NI bufsize);
N_NIMCALL(void, writerope_177071)(FILE* f, Ropeobj176009* r);
N_NIMCALL(void, write_17095)(FILE* f, NimStringDesc* s);
N_NIMCALL(NIM_BOOL, equalsfile_177916)(Ropeobj176009* r, NimStringDesc* filename);
N_NIMCALL(NIM_BOOL, equalsfile_177809)(Ropeobj176009* r, FILE* f);
N_NIMCALL(NI, readbuffer_17160)(FILE* f, void* buffer, NI len);
N_NIMCALL(void, writerope_177106)(Ropeobj176009* head, NimStringDesc* filename, NIM_BOOL usewarning);
STRING_LITERAL(TMP3741, "$", 1);
TY176023 errorhandler_176028;
TY176105 cache_176106;
NI gcachetries_176268;
NI gcachemisses_176272;
NI gcacheinttries_176276;
Ropeobj176009* rnl_177228;
extern TNimType NTI4011; /* RootObj */
TNimType NTI176009; /* RopeObj */
extern TNimType NTI108; /* int */
extern TNimType NTI149; /* string */
TNimType NTI176007; /* Rope */
extern Gcheap51218 gch_51259;
extern NimStringDesc* tnl_174654;
Ropeobj176009* softrnl_177229;
extern TNimType NTI190365; /* seq[Rope] */
N_NIMCALL(void, TMP131)(void* p, NI op) {
	Ropeobj176009* a;
	a = (Ropeobj176009*)p;
	nimGCvisit((void*)(*a).left, op);
	nimGCvisit((void*)(*a).right, op);
	nimGCvisit((void*)(*a).data, op);
}

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

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

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

N_NIMCALL(Ropeobj176009*, newrope_176056)(NimStringDesc* data) {
	Ropeobj176009* result;
	result = 0;
	result = (Ropeobj176009*) newObj((&NTI176007), sizeof(Ropeobj176009));
	(*result).Sup.m_type = (&NTI176009);
	{
		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) {
	{
		Cell49147* c;
		if (!!((src == NIM_NIL))) goto LA3;
		c = usrtocell_52846(src);
		(*c).refcount += ((NI) 8);
	}
	LA3: ;
	{
		Cell49147* c;
		if (!!(((*dest) == NIM_NIL))) goto LA7;
		c = usrtocell_52846((*dest));
		{
			(*c).refcount -= ((NI) 8);
			if (!((NU32)((*c).refcount) < (NU32)(((NI) 8)))) goto LA11;
			rtladdzct_54404(c);
		}
		LA11: ;
	}
	LA7: ;
	(*dest) = src;
}

N_NIMCALL(Ropeobj176009*, HEX26_176436)(Ropeobj176009* a, Ropeobj176009* b) {
	Ropeobj176009* 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_176056(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_176534)(Ropeobj176009** a, Ropeobj176009* b) {
	unsureAsgnRef((void**) (&(*a)), HEX26_176436((*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(Ropeobj176009*, insertincache_176280)(NimStringDesc* s) {
	Ropeobj176009* result;
	NI h;
	NI LOC1;
	result = 0;
	gcachetries_176268 += ((NI) 1);
	LOC1 = 0;
	LOC1 = hash_143848(s);
	h = (NI)(LOC1 & ((NI) 4095));
	result = cache_176106[(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_176272 += ((NI) 1);
		result = newrope_176056(s);
		asgnRefNoCycle((void**) (&cache_176106[(h)- 0]), result);
	}
	LA6: ;
	return result;
}

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

N_NIMCALL(NI, len_176034)(Ropeobj176009* 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(Ropeobj176009*, pop_176595)(TY190365** s) {
	Ropeobj176009* result;
	NI L;
	result = 0;
	L = (NI)(((*s) ? (*s)->Sup.len : 0) - ((NI) 1));
	result = (*s)->data[L];
	(*s) = (TY190365*) setLengthSeq(&((*s))->Sup, sizeof(Ropeobj176009*), ((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_177132)(Ropeobj176009* r) {
	NimStringDesc* result;
	NI LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = len_176034(r);
	result = mnewString(((NI) (LOC1)));
	result = setLengthStr(result, ((NI) 0));
	{
		NimStringDesc* s_177136;
		s_177136 = 0;
		{
			TY190365* stack_177141;
			TY176572 LOC7;
			if (!!((r == NIM_NIL))) goto LA5;
			stack_177141 = (TY190365*) newSeq((&NTI190365), 1);
			memset((void*)LOC7, 0, sizeof(LOC7));
			LOC7[0] = r;
			asgnRefNoCycle((void**) (&stack_177141->data[0]), LOC7[0]);
			{
				while (1) {
					Ropeobj176009* it_177143;
					if (!(((NI) 0) < (stack_177141 ? stack_177141->Sup.len : 0))) goto LA9;
					it_177143 = pop_176595((&stack_177141));
					{
						while (1) {
							if (!(*it_177143).data == 0) goto LA11;
							stack_177141 = (TY190365*) incrSeqV2(&(stack_177141)->Sup, sizeof(Ropeobj176009*));
							asgnRefNoCycle((void**) (&stack_177141->data[stack_177141->Sup.len]), (*it_177143).right);
							++stack_177141->Sup.len;
							it_177143 = (*it_177143).left;
						} LA11: ;
					}
					s_177136 = (*it_177143).data;
					result = resizeString(result, s_177136->Sup.len + 0);
appendString(result, s_177136);
				} LA9: ;
			}
		}
		LA5: ;
	}
	return result;
}

N_NIMCALL(Ropeobj176009*, HEX26_176471)(Ropeobj176009* a, NimStringDesc* b) {
	Ropeobj176009* result;
	Ropeobj176009* LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = rope_176322(b);
	result = HEX26_176436(a, LOC1);
	return result;
}

N_NIMCALL(void, add_176545)(Ropeobj176009** a, NimStringDesc* b) {
	unsureAsgnRef((void**) (&(*a)), HEX26_176471((*a), b));
}

N_NIMCALL(Ropeobj176009*, HEX25_177236)(NimStringDesc* frmt, Ropeobj176009** args, NI argsLen0) {
	Ropeobj176009* 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_176545(&result, ((NimStringDesc*) &TMP3741));
					i += ((NI) 1);
				}
				break;
				case 35:
				{
					i += ((NI) 1);
					add_176534(&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_176028.ClEnv? errorhandler_176028.ClPrc(((NU8) 1), LOC20, NIM_FALSE, errorhandler_176028.ClEnv):((TMP3742)(errorhandler_176028.ClPrc))(((NU8) 1), LOC20, NIM_FALSE);
					}
					goto LA16;
					LA18: ;
					{
						add_176534(&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_176028.ClEnv? errorhandler_176028.ClPrc(((NU8) 1), LOC30, NIM_FALSE, errorhandler_176028.ClEnv):((TMP3743)(errorhandler_176028.ClPrc))(((NU8) 1), LOC30, NIM_FALSE);
					}
					LA25: ;
					{
						NimStringDesc* LOC35;
						if (!((NI)((argsLen0-1) + ((NI) 1)) < j)) goto LA33;
						LOC35 = 0;
						LOC35 = nimIntToStr(j);
						errorhandler_176028.ClEnv? errorhandler_176028.ClPrc(((NU8) 1), LOC35, NIM_FALSE, errorhandler_176028.ClEnv):((TMP3744)(errorhandler_176028.ClPrc))(((NU8) 1), LOC35, NIM_FALSE);
					}
					goto LA31;
					LA33: ;
					{
						add_176534(&result, args[(NI)(j - ((NI) 1))]);
					}
					LA31: ;
				}
				break;
				case 110:
				{
					add_176534(&result, softrnl_177229);
					i += ((NI) 1);
				}
				break;
				case 78:
				{
					add_176534(&result, rnl_177228);
					i += ((NI) 1);
				}
				break;
				default:
				{
					NimStringDesc* LOC40;
					LOC40 = 0;
					LOC40 = nimCharToStr(frmt->data[i]);
					errorhandler_176028.ClEnv? errorhandler_176028.ClPrc(((NU8) 1), LOC40, NIM_FALSE, errorhandler_176028.ClEnv):((TMP3745)(errorhandler_176028.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_176545(&result, LOC52);
			}
			LA50: ;
		} LA2: ;
	}
	return result;
}

N_NIMCALL(Ropeobj176009*, rope_176407)(NI64 i) {
	Ropeobj176009* result;
	NimStringDesc* LOC1;
	result = 0;
	gcacheinttries_176276 += ((NI) 1);
	LOC1 = 0;
	LOC1 = nimInt64ToStr(i);
	result = rope_176322(LOC1);
	return result;
}

N_NIMCALL(void, addf_177635)(Ropeobj176009** c, NimStringDesc* frmt, Ropeobj176009** args, NI argsLen0) {
	Ropeobj176009* LOC1;
	LOC1 = 0;
	LOC1 = HEX25_177236(frmt, args, argsLen0);
	add_176534(c, LOC1);
}

N_NIMCALL(Ropeobj176009*, HEX26_176482)(NimStringDesc* a, Ropeobj176009* b) {
	Ropeobj176009* result;
	Ropeobj176009* LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = rope_176322(a);
	result = HEX26_176436(LOC1, b);
	return result;
}

N_NIMCALL(void, prepend_177206)(Ropeobj176009** a, Ropeobj176009* b) {
	unsureAsgnRef((void**) (&(*a)), HEX26_176436(b, (*a)));
}

N_NIMCALL(void, writerope_177071)(FILE* f, Ropeobj176009* r) {
	{
		NimStringDesc* s_177075;
		s_177075 = 0;
		{
			TY190365* stack_177082;
			TY176572 LOC6;
			if (!!((r == NIM_NIL))) goto LA4;
			stack_177082 = (TY190365*) newSeq((&NTI190365), 1);
			memset((void*)LOC6, 0, sizeof(LOC6));
			LOC6[0] = r;
			asgnRefNoCycle((void**) (&stack_177082->data[0]), LOC6[0]);
			{
				while (1) {
					Ropeobj176009* it_177084;
					if (!(((NI) 0) < (stack_177082 ? stack_177082->Sup.len : 0))) goto LA8;
					it_177084 = pop_176595((&stack_177082));
					{
						while (1) {
							if (!(*it_177084).data == 0) goto LA10;
							stack_177082 = (TY190365*) incrSeqV2(&(stack_177082)->Sup, sizeof(Ropeobj176009*));
							asgnRefNoCycle((void**) (&stack_177082->data[stack_177082->Sup.len]), (*it_177084).right);
							++stack_177082->Sup.len;
							it_177084 = (*it_177084).left;
						} LA10: ;
					}
					s_177075 = (*it_177084).data;
					write_17095(f, s_177075);
				} LA8: ;
			}
		}
		LA4: ;
	}
}

N_NIMCALL(void, writerope_177106)(Ropeobj176009* head, NimStringDesc* filename, NIM_BOOL usewarning) {
	FILE* f;
	f = 0;
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = open_17003(&f, filename, ((NU8) 1), ((NI) -1));
		if (!LOC3) goto LA4;
		{
			if (!!((head == NIM_NIL))) goto LA8;
			writerope_177071(f, head);
		}
		LA8: ;
		fclose(f);
	}
	goto LA1;
	LA4: ;
	{
		errorhandler_176028.ClEnv? errorhandler_176028.ClPrc(((NU8) 0), filename, usewarning, errorhandler_176028.ClEnv):((TMP4568)(errorhandler_176028.ClPrc))(((NU8) 0), filename, usewarning);
	}
	LA1: ;
}

N_NIMCALL(NIM_BOOL, equalsfile_177809)(Ropeobj176009* r, FILE* f) {
	NIM_BOOL result;
	TY177815 buf;
	NI bpos;
	NI blen;
	NI LOC26;
{	result = 0;
	memset((void*)buf, 0, sizeof(buf));
	bpos = ((NI) 1024);
	blen = ((NI) 1024);
	{
		NimStringDesc* s_177837;
		s_177837 = 0;
		{
			TY190365* stack_177886;
			TY176572 LOC6;
			if (!!((r == NIM_NIL))) goto LA4;
			stack_177886 = (TY190365*) newSeq((&NTI190365), 1);
			memset((void*)LOC6, 0, sizeof(LOC6));
			LOC6[0] = r;
			asgnRefNoCycle((void**) (&stack_177886->data[0]), LOC6[0]);
			{
				while (1) {
					Ropeobj176009* it_177888;
					NI spos;
					NI slen;
					if (!(((NI) 0) < (stack_177886 ? stack_177886->Sup.len : 0))) goto LA8;
					it_177888 = pop_176595((&stack_177886));
					{
						while (1) {
							if (!(*it_177888).data == 0) goto LA10;
							stack_177886 = (TY190365*) incrSeqV2(&(stack_177886)->Sup, sizeof(Ropeobj176009*));
							asgnRefNoCycle((void**) (&stack_177886->data[stack_177886->Sup.len]), (*it_177888).right);
							++stack_177886->Sup.len;
							it_177888 = (*it_177888).left;
						} LA10: ;
					}
					s_177837 = (*it_177888).data;
					spos = ((NI) 0);
					slen = (s_177837 ? s_177837->Sup.len : 0);
					{
						while (1) {
							NI n;
							if (!(spos < slen)) goto LA12;
							{
								if (!(bpos == blen)) goto LA15;
								bpos = ((NI) 0);
								blen = readbuffer_17160(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_177837->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_17160(f, ((void*) ((&buf[(((NI) 0))- 0]))), ((NI) 1));
	result = (LOC26 == ((NI) 0));
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NIM_BOOL, equalsfile_177916)(Ropeobj176009* r, NimStringDesc* filename) {
	NIM_BOOL result;
	FILE* f;
	result = 0;
	f = 0;
	result = open_17003(&f, filename, ((NU8) 0), ((NI) -1));
	{
		if (!result) goto LA3;
		result = equalsfile_177809(r, f);
		fclose(f);
	}
	LA3: ;
	return result;
}

N_NIMCALL(NIM_BOOL, writeropeifnotequal_177930)(Ropeobj176009* r, NimStringDesc* filename) {
	NIM_BOOL result;
	result = 0;
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = equalsfile_177916(r, filename);
		if (!!(LOC3)) goto LA4;
		writerope_177106(r, filename, NIM_FALSE);
		result = NIM_TRUE;
	}
	goto LA1;
	LA4: ;
	{
		result = NIM_FALSE;
	}
	LA1: ;
	return result;
}

N_NIMCALL(void, resetropecache_176111)(void) {
	{
		NI i_176223;
		NI res_176228;
		i_176223 = 0;
		res_176228 = ((NI) 0);
		{
			while (1) {
				if (!(res_176228 <= ((NI) 4095))) goto LA3;
				i_176223 = ((NI) (res_176228));
				asgnRefNoCycle((void**) (&cache_176106[(i_176223)- 0]), NIM_NIL);
				res_176228 += ((NI) 1);
			} LA3: ;
		}
	}
}
NIM_EXTERNC N_NOINLINE(void, compiler_ropesInit000)(void) {
	gcachetries_176268 = ((NI) 0);
	gcachemisses_176272 = ((NI) 0);
	gcacheinttries_176276 = ((NI) 0);
	asgnRefNoCycle((void**) (&rnl_177228), newrope_176056(tnl_174654));
	asgnRefNoCycle((void**) (&softrnl_177229), newrope_176056(tnl_174654));
}

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

