/* Generated by Nimrod Compiler v0.9.5 */
/*   (c) 2014 Andreas Rumpf */
/* The generated code is subject to the original license. */
/* Compiled for: Linux, i386, gcc */
/* Command for C compiler:
   gcc -c  -w -O3 -fno-strict-aliasing  -I/build/nimrod/src/nimrod/lib -o compiler/nimcache/stdlib_cgi.o compiler/nimcache/stdlib_cgi.c */
#define NIM_INTBITS 32
#include "nimbase.h"

#include <string.h>
typedef struct tstringtable121810 tstringtable121810;
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct TNimObject TNimObject;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct tkeyvaluepairseq121808 tkeyvaluepairseq121808;
typedef struct tkeyvaluepair121806 tkeyvaluepair121806;
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
typedef N_NIMCALL_PTR(void, TY2289) (void* p, NI op);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY2289 marker;
};
struct  TNimObject  {
TNimType* m_type;
};
struct tkeyvaluepair121806 {
NimStringDesc* Field0;
NimStringDesc* Field1;
};
struct  tstringtable121810  {
  TNimObject Sup;
NI Counter;
tkeyvaluepairseq121808* Data;
NU8 Mode;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct tkeyvaluepairseq121808 {
  TGenericSeq Sup;
  tkeyvaluepair121806 data[SEQ_DECL_SIZE];
};
N_NIMCALL(NimStringDesc*, rawNewString)(NI space);
N_NIMCALL(NimStringDesc*, rawNewString)(NI cap);
N_NIMCALL(NimStringDesc*, addChar)(NimStringDesc* s, NIM_CHAR c);
N_NIMCALL(NimStringDesc*, nsuToHex)(NI64 x, NI len);
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src);
N_NIMCALL(NimStringDesc*, resizeString)(NimStringDesc* dest, NI addlen);
tstringtable121810* gcookies_336893;

static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src) {
	memcpy(((NCSTRING) (&(*dest).data[((*dest).Sup.len)- 0])), ((NCSTRING) ((*src).data)), (NI32)((*src).Sup.len + 1));
	(*dest).Sup.len += (*src).Sup.len;
}

N_NIMCALL(NimStringDesc*, urlencode_336002)(NimStringDesc* s) {
	NimStringDesc* result;
	NI i_336016;
	NI HEX3Atmp_336025;
	NI res_336027;
	result = 0;
	result = rawNewString((NI32)(s->Sup.len + (NI)((NU32)(s->Sup.len) >> (NU32)(2))));
	i_336016 = 0;
	HEX3Atmp_336025 = 0;
	HEX3Atmp_336025 = (NI32)(s->Sup.len - 1);
	res_336027 = 0;
	while (1) {
		if (!(res_336027 <= HEX3Atmp_336025)) goto LA1;
		i_336016 = res_336027;
		switch (((NU8)(s->data[i_336016]))) {
		case 97 ... 122:
		case 65 ... 90:
		case 48 ... 57:
		case 95:
		{
			result = addChar(result, s->data[i_336016]);
		}
		break;
		case 32:
		{
			result = addChar(result, 43);
		}
		break;
		default:
		{
			NimStringDesc* LOC5;
			result = addChar(result, 37);
			LOC5 = 0;
			LOC5 = nsuToHex(((NI64) (((NU8)(s->data[i_336016])))), 2);
			result = resizeString(result, LOC5->Sup.len + 0);
appendString(result, LOC5);
		}
		break;
		}
		res_336027 += 1;
	} LA1: ;
	return result;
}
N_NOINLINE(void, stdlibcgiInit)(void) {
}

N_NOINLINE(void, stdlibcgiDatInit)(void) {
}

