/* 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 <sys/types.h>
#include <unistd.h>
#include <setjmp.h>
#include <spawn.h>
#include <signal.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct Processobj156404 Processobj156404;
typedef struct Stringtableobj146212 Stringtableobj146212;
typedef struct TNimObject TNimObject;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct Streamobj150427 Streamobj150427;
typedef struct TY131608 TY131608;
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 Exception Exception;
typedef struct TSafePoint TSafePoint;
typedef struct Keyvaluepairseq146210 Keyvaluepairseq146210;
typedef struct Keyvaluepair146208 Keyvaluepair146208;
typedef struct Startprocessdata157948 Startprocessdata157948;
typedef struct Filestreamobj151667 Filestreamobj151667;
typedef struct TY157201 TY157201;
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 NU8 TY213407[32];
typedef int TY158024[2];
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  Processobj156404  {
  TNimObject Sup;
int inhandle;
int outhandle;
int errhandle;
Streamobj150427* instream;
Streamobj150427* outstream;
Streamobj150427* errstream;
pid_t id;
int exitcode;
NU8 options;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
typedef NimStringDesc* TY193293[3];
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 NimStringDesc* TY192421[1];
struct  Exception  {
  TNimObject Sup;
Exception* parent;
NCSTRING name;
NimStringDesc* message;
NimStringDesc* trace;
};
typedef struct {
N_NIMCALL_PTR(NIM_BOOL, ClPrc) (Exception* e, void* ClEnv);
void* ClEnv;
} TY19411;
struct  TSafePoint  {
TSafePoint* prev;
NI status;
jmp_buf context;
NIM_BOOL hasRaiseAction;
TY19411 raiseAction;
};
struct  Stringtableobj146212  {
  TNimObject Sup;
NI counter;
Keyvaluepairseq146210* data;
NU8 mode;
};
struct Keyvaluepair146208 {
NimStringDesc* Field0;
NimStringDesc* Field1;
};
struct  Startprocessdata157948  {
NCSTRING syscommand;
NCSTRING* sysargs;
NCSTRING* sysenv;
NCSTRING workingdir;
TY158024 pstdin;
TY158024 pstdout;
TY158024 pstderr;
TY158024 perrorpipe;
NIM_BOOL optionpousepath;
NIM_BOOL optionpoparentstreams;
NIM_BOOL optionpostderrtostdout;
};
typedef struct {
N_NIMCALL_PTR(void, ClPrc) (NI idx, void* ClEnv);
void* ClEnv;
} TY204876;
typedef struct {
N_NIMCALL_PTR(void, ClPrc) (NI idx, Processobj156404* p, void* ClEnv);
void* ClEnv;
} TY204882;
typedef N_CLOSURE_PTR(void, TMP4594) (NI idx);
typedef NimStringDesc* TY253724[1];
typedef N_CLOSURE_PTR(void, TMP4596) (NI idx, Processobj156404* p);
typedef N_CLOSURE_PTR(void, TMP4597) (NI idx);
typedef N_CLOSURE_PTR(void, TMP4598) (NI idx, Processobj156404* p);
typedef N_CLOSURE_PTR(void, TMP4599) (NI idx);
typedef N_CLOSURE_PTR(void, TMP4600) (NI idx, Processobj156404* p);
typedef N_NIMCALL_PTR(void, TY150428) (Streamobj150427* s);
typedef N_NIMCALL_PTR(NIM_BOOL, TY150432) (Streamobj150427* s);
typedef N_NIMCALL_PTR(void, TY150436) (Streamobj150427* s, NI pos);
typedef N_NIMCALL_PTR(NI, TY150441) (Streamobj150427* s);
typedef N_NIMCALL_PTR(NI, TY150445) (Streamobj150427* s, void* buffer, NI buflen);
typedef N_NIMCALL_PTR(NI, TY150451) (Streamobj150427* s, void* buffer, NI buflen);
typedef N_NIMCALL_PTR(void, TY150457) (Streamobj150427* s, void* buffer, NI buflen);
typedef N_NIMCALL_PTR(void, TY150463) (Streamobj150427* s);
struct  Streamobj150427  {
  TNimObject Sup;
TY150428 closeimpl;
TY150432 atendimpl;
TY150436 setpositionimpl;
TY150441 getpositionimpl;
TY150445 readdataimpl;
TY150451 peekdataimpl;
TY150457 writedataimpl;
TY150463 flushimpl;
};
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  Filestreamobj151667  {
  Streamobj150427 Sup;
FILE* f;
};
struct  Freecell31032  {
Freecell31032* next;
NI zerofield;
};
struct TY131608 {
  TGenericSeq Sup;
  NimStringDesc* data[SEQ_DECL_SIZE];
};
struct TY157201 {
  TGenericSeq Sup;
  Processobj156404* data[SEQ_DECL_SIZE];
};
struct Keyvaluepairseq146210 {
  TGenericSeq Sup;
  Keyvaluepair146208 data[SEQ_DECL_SIZE];
};
N_NIMCALL(NimStringDesc*, nospquoteShellPosix)(NimStringDesc* s);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NIMCALL(NIM_BOOL, allcharsinset_104130)(NimStringDesc* s, TY213407 theset);
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src);
N_NIMCALL(NimStringDesc*, nsuReplaceStr)(NimStringDesc* s, NimStringDesc* sub, NimStringDesc* by);
N_NIMCALL(NimStringDesc*, rawNewString)(NI space);
N_NIMCALL(void, nimGCvisit)(void* d, NI op);
N_NIMCALL(void, TMP3006)(void* p, NI op);
N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
N_NOINLINE(void, raiseoserror_127209)(NI32 errorcode, NimStringDesc* additionalinfo);
N_NIMCALL(NI32, oslasterror_127234)(void);
N_NIMCALL(void*, newSeq)(TNimType* typ, NI len);
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);
N_NIMCALL(TGenericSeq*, incrSeqV2)(TGenericSeq* seq, NI elemsize);
N_NIMCALL(NCSTRING*, alloccstringarray_17801)(NimStringDesc** a, NI aLen0);
static N_INLINE(void, pushSafePoint)(TSafePoint* s);
N_NIMCALL(NCSTRING*, envtocstringarray_157854)(void);
N_NIMCALL(void, getenvvarsc_131804)(void);
N_NIMCALL(NI, nsuFindChar)(NimStringDesc* s, NIM_CHAR sub, NI start);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI start_81216, NI last);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI first, NI last);
N_NIMCALL(NimStringDesc*, copyStr)(NimStringDesc* s, NI start);
N_NIMCALL(NimStringDesc*, copyStr)(NimStringDesc* s, NI first);
N_NOCONV(void*, alloc0_8417)(NI size);
N_NOCONV(void*, alloc_8401)(NI size);
N_NIMCALL(NCSTRING*, envtocstringarray_157806)(Stringtableobj146212* t);
N_NIMCALL(NI, nstlen)(Stringtableobj146212* t);
N_NIMCALL(pid_t, startprocessauxspawn_158001)(Startprocessdata157948* data);
static N_INLINE(void, setcurrentdir_129207)(NimStringDesc* newdir);
N_NIMCALL(NimStringDesc*, cstrToNimstr)(NCSTRING str);
N_NIMCALL(NimStringDesc*, nsuJoinSep)(NimStringDesc** a, NI aLen0, NimStringDesc* sep);
static N_INLINE(void, popSafePoint)(void);
N_NIMCALL(void, dealloccstringarray_18050)(NCSTRING* a);
N_NIMCALL(void, reraiseException)(void);
N_NIMCALL(void, createstream_159241)(Streamobj150427** stream, int* handle, NU8 filemode);
N_NIMCALL(NIM_BOOL, open_17011)(FILE** f, int filehandle, NU8 mode);
N_NIMCALL(Filestreamobj151667*, newfilestream_151743)(FILE* f);
N_NIMCALL(void, unsureAsgnRef)(void** dest, void* src);
N_NIMCALL(NI, ncpicountProcessors)(void);
N_NIMCALL(void, TMP4592)(void* p, NI op);
N_NIMCALL(Processobj156404*, nospstartProcess)(NimStringDesc* command, NimStringDesc* workingdir, NimStringDesc** args, NI argsLen0, Stringtableobj146212* env, NU8 options);
static N_INLINE(void, asgnRef)(void** dest, void* src);
static N_INLINE(void, incref_55222)(Cell49147* c);
static N_INLINE(NIM_BOOL, canbecycleroot_52867)(Cell49147* c);
static N_INLINE(void, rtladdcycleroot_53623)(Cell49147* c);
N_NOINLINE(void, incl_49865)(Cellset49159* s, Cell49147* cell);
static N_INLINE(void, decref_54804)(Cell49147* c);
N_NIMCALL(void, nossleep)(NI milsecs);
N_NIMCALL(NIM_BOOL, nosprunning)(Processobj156404* p);
N_NIMCALL(NI, nospwaitForExit)(Processobj156404* p, NI timeout);
N_NIMCALL(void, nospclose)(Processobj156404* p);
N_NIMCALL(void, close_150491)(Streamobj150427* s);
STRING_LITERAL(TMP1872, "\'\'", 2);
static NIM_CONST TY213407 TMP1873 = {
0x00, 0x00, 0x00, 0x00, 0x20, 0xE8, 0xFF, 0x27,
0xFF, 0xFF, 0xFF, 0x87, 0xFE, 0xFF, 0xFF, 0x07,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
;
STRING_LITERAL(TMP1874, "\'", 1);
STRING_LITERAL(TMP1875, "\'\"\'\"\'", 5);
STRING_LITERAL(TMP3007, "", 0);
STRING_LITERAL(TMP3008, "/bin/sh", 7);
STRING_LITERAL(TMP3009, "-c", 2);
STRING_LITERAL(TMP3011, "=", 1);
STRING_LITERAL(TMP3013, " ", 1);
static NIM_CONST TY204876 TMP4593 = {NIM_NIL,NIM_NIL};
static NIM_CONST TY204882 TMP4595 = {NIM_NIL,NIM_NIL};
extern NCSTRING* environ;
extern TNimType NTI4011; /* RootObj */
TNimType NTI156404; /* ProcessObj */
extern TNimType NTI16608; /* FileHandle */
extern TNimType NTI150425; /* Stream */
extern TNimType NTI114289; /* Pid */
extern TNimType NTI8211; /* cint */
TNimType NTI156402; /* ProcessOption */
TNimType NTI156640; /* set[ProcessOption] */
TNimType NTI156406; /* Process */
extern TNimType NTI131608; /* seq[string] */
extern Gcheap51218 gch_51259;
extern TSafePoint* exchandler_21043;
extern TY131608* environment_131609;
TNimType NTI157201; /* seq[Process] */

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*, nospquoteShellPosix)(NimStringDesc* s) {
	NimStringDesc* result;
	NIM_BOOL safe;
{	result = 0;
	{
		if (!((s ? s->Sup.len : 0) == ((NI) 0))) goto LA3;
		result = copyString(((NimStringDesc*) &TMP1872));
		goto BeforeRet;
	}
	LA3: ;
	safe = allcharsinset_104130(s, TMP1873);
	{
		if (!safe) goto LA7;
		result = copyString(s);
		goto BeforeRet;
	}
	goto LA5;
	LA7: ;
	{
		NimStringDesc* LOC10;
		NimStringDesc* LOC11;
		LOC10 = 0;
		LOC11 = 0;
		LOC11 = nsuReplaceStr(s, ((NimStringDesc*) &TMP1874), ((NimStringDesc*) &TMP1875));
		LOC10 = rawNewString(LOC11->Sup.len + 2);
appendString(LOC10, ((NimStringDesc*) &TMP1874));
appendString(LOC10, LOC11);
appendString(LOC10, ((NimStringDesc*) &TMP1874));
		result = LOC10;
		goto BeforeRet;
	}
	LA5: ;
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NimStringDesc*, nospquoteShell)(NimStringDesc* s) {
	NimStringDesc* result;
{	result = 0;
	result = nospquoteShellPosix(s);
	goto BeforeRet;
	}BeforeRet: ;
	return result;
}
N_NIMCALL(void, TMP3006)(void* p, NI op) {
	Processobj156404* a;
	a = (Processobj156404*)p;
	nimGCvisit((void*)(*a).instream, op);
	nimGCvisit((void*)(*a).outstream, op);
	nimGCvisit((void*)(*a).errstream, 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: ;
}

static N_INLINE(void, pushSafePoint)(TSafePoint* s) {
	(*s).hasRaiseAction = NIM_FALSE;
	(*s).prev = exchandler_21043;
	exchandler_21043 = s;
}

N_NIMCALL(NCSTRING*, envtocstringarray_157854)(void) {
	NCSTRING* result;
	NI counter;
	void* LOC5;
	NI i;
	result = 0;
	counter = ((NI) 0);
	{
		NimStringDesc* key_157858;
		NimStringDesc* val_157859;
		key_157858 = 0;
		val_157859 = 0;
		getenvvarsc_131804();
		{
			NI i_157888;
			NI HEX3Atmp_157890;
			NI res_157892;
			i_157888 = 0;
			HEX3Atmp_157890 = 0;
			HEX3Atmp_157890 = (environment_131609 ? (environment_131609->Sup.len-1) : -1);
			res_157892 = ((NI) 0);
			{
				while (1) {
					NI p_157896;
					if (!(res_157892 <= HEX3Atmp_157890)) goto LA4;
					i_157888 = res_157892;
					p_157896 = nsuFindChar(environment_131609->data[i_157888], 61, ((NI) 0));
					key_157858 = copyStrLast(environment_131609->data[i_157888], ((NI) 0), (NI)(p_157896 - ((NI) 1)));
					val_157859 = copyStr(environment_131609->data[i_157888], (NI)(p_157896 + ((NI) 1)));
					counter += ((NI) 1);
					res_157892 += ((NI) 1);
				} LA4: ;
			}
		}
	}
	LOC5 = 0;
	LOC5 = alloc0_8417(((NI) ((NI)((NI)(counter + ((NI) 1)) * ((NI) 4)))));
	result = ((NCSTRING*) (LOC5));
	i = ((NI) 0);
	{
		NimStringDesc* key_157871;
		NimStringDesc* val_157872;
		key_157871 = 0;
		val_157872 = 0;
		getenvvarsc_131804();
		{
			NI i_157909;
			NI HEX3Atmp_157911;
			NI res_157913;
			i_157909 = 0;
			HEX3Atmp_157911 = 0;
			HEX3Atmp_157911 = (environment_131609 ? (environment_131609->Sup.len-1) : -1);
			res_157913 = ((NI) 0);
			{
				while (1) {
					NI p_157917;
					NimStringDesc* x;
					NimStringDesc* LOC10;
					void* LOC11;
					if (!(res_157913 <= HEX3Atmp_157911)) goto LA9;
					i_157909 = res_157913;
					p_157917 = nsuFindChar(environment_131609->data[i_157909], 61, ((NI) 0));
					key_157871 = copyStrLast(environment_131609->data[i_157909], ((NI) 0), (NI)(p_157917 - ((NI) 1)));
					val_157872 = copyStr(environment_131609->data[i_157909], (NI)(p_157917 + ((NI) 1)));
					LOC10 = 0;
					LOC10 = rawNewString(key_157871->Sup.len + val_157872->Sup.len + 1);
appendString(LOC10, key_157871);
appendString(LOC10, ((NimStringDesc*) &TMP3011));
appendString(LOC10, val_157872);
					x = LOC10;
					LOC11 = 0;
					LOC11 = alloc_8401(((NI) ((NI)((x ? x->Sup.len : 0) + ((NI) 1)))));
					result[(i)- 0] = ((NCSTRING) (LOC11));
					memcpy(((void*) (result[(i)- 0])), ((void*) ((&x->data[((NI) 0)]))), ((NI) ((NI)((x ? x->Sup.len : 0) + ((NI) 1)))));
					i += ((NI) 1);
					res_157913 += ((NI) 1);
				} LA9: ;
			}
		}
	}
	return result;
}

N_NIMCALL(NCSTRING*, envtocstringarray_157806)(Stringtableobj146212* t) {
	NCSTRING* result;
	NI LOC1;
	void* LOC2;
	NI i;
	result = 0;
	LOC1 = 0;
	LOC1 = nstlen(t);
	LOC2 = 0;
	LOC2 = alloc0_8417(((NI) ((NI)((NI)(LOC1 + ((NI) 1)) * ((NI) 4)))));
	result = ((NCSTRING*) (LOC2));
	i = ((NI) 0);
	{
		NimStringDesc* key_157812;
		NimStringDesc* val_157813;
		key_157812 = 0;
		val_157813 = 0;
		{
			NI h_157832;
			NI HEX3Atmp_157834;
			NI res_157836;
			h_157832 = 0;
			HEX3Atmp_157834 = 0;
			HEX3Atmp_157834 = ((*t).data ? ((*t).data->Sup.len-1) : -1);
			res_157836 = ((NI) 0);
			{
				while (1) {
					if (!(res_157836 <= HEX3Atmp_157834)) goto LA6;
					h_157832 = res_157836;
					{
						NimStringDesc* x;
						NimStringDesc* LOC11;
						void* LOC12;
						if (!!((*t).data->data[h_157832].Field0 == 0)) goto LA9;
						key_157812 = (*t).data->data[h_157832].Field0;
						val_157813 = (*t).data->data[h_157832].Field1;
						LOC11 = 0;
						LOC11 = rawNewString(key_157812->Sup.len + val_157813->Sup.len + 1);
appendString(LOC11, key_157812);
appendString(LOC11, ((NimStringDesc*) &TMP3011));
appendString(LOC11, val_157813);
						x = LOC11;
						LOC12 = 0;
						LOC12 = alloc_8401(((NI) ((NI)((x ? x->Sup.len : 0) + ((NI) 1)))));
						result[(i)- 0] = ((NCSTRING) (LOC12));
						memcpy(((void*) (result[(i)- 0])), ((void*) ((&x->data[((NI) 0)]))), ((NI) ((NI)((x ? x->Sup.len : 0) + ((NI) 1)))));
						i += ((NI) 1);
					}
					LA9: ;
					res_157836 += ((NI) 1);
				} LA6: ;
			}
		}
	}
	return result;
}

static N_INLINE(void, setcurrentdir_129207)(NimStringDesc* newdir) {
	{
		int LOC3;
		NI32 LOC6;
		LOC3 = 0;
		LOC3 = chdir(newdir->data);
		if (!!((LOC3 == ((NI32) 0)))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_127234();
		raiseoserror_127209(LOC6, ((NimStringDesc*) &TMP3007));
	}
	LA4: ;
}

N_NIMCALL(pid_t, startprocessauxspawn_158001)(Startprocessdata157948* data) {
	pid_t result;
	posix_spawnattr_t attr;
	posix_spawn_file_actions_t fops;
	sigset_t mask;
	int res;
	pid_t pid;
	int LOC98;
	int LOC99;
{	result = 0;
	memset((void*)(&attr), 0, sizeof(attr));
	memset((void*)(&fops), 0, sizeof(fops));
	{
		int LOC3;
		NI32 LOC6;
		LOC3 = 0;
		LOC3 = posix_spawn_file_actions_init((&fops));
		if (!!((LOC3 == ((NI32) 0)))) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_127234();
		raiseoserror_127209(LOC6, ((NimStringDesc*) &TMP3007));
	}
	LA4: ;
	{
		int LOC9;
		NI32 LOC12;
		LOC9 = 0;
		LOC9 = posix_spawnattr_init((&attr));
		if (!!((LOC9 == ((NI32) 0)))) goto LA10;
		LOC12 = 0;
		LOC12 = oslasterror_127234();
		raiseoserror_127209(LOC12, ((NimStringDesc*) &TMP3007));
	}
	LA10: ;
	memset((void*)(&mask), 0, sizeof(mask));
	{
		int LOC15;
		NI32 LOC18;
		LOC15 = 0;
		LOC15 = sigemptyset((&mask));
		if (!!((LOC15 == ((NI32) 0)))) goto LA16;
		LOC18 = 0;
		LOC18 = oslasterror_127234();
		raiseoserror_127209(LOC18, ((NimStringDesc*) &TMP3007));
	}
	LA16: ;
	{
		int LOC21;
		NI32 LOC24;
		LOC21 = 0;
		LOC21 = posix_spawnattr_setsigmask((&attr), (&mask));
		if (!!((LOC21 == ((NI32) 0)))) goto LA22;
		LOC24 = 0;
		LOC24 = oslasterror_127234();
		raiseoserror_127209(LOC24, ((NimStringDesc*) &TMP3007));
	}
	LA22: ;
	{
		int LOC27;
		NI32 LOC30;
		LOC27 = 0;
		LOC27 = posix_spawnattr_setpgroup((&attr), ((pid_t) 0));
		if (!!((LOC27 == ((NI32) 0)))) goto LA28;
		LOC30 = 0;
		LOC30 = oslasterror_127234();
		raiseoserror_127209(LOC30, ((NimStringDesc*) &TMP3007));
	}
	LA28: ;
	{
		int LOC33;
		NI32 LOC36;
		LOC33 = 0;
		LOC33 = posix_spawnattr_setflags((&attr), (NI32)((NI32)(((int) 0) | POSIX_SPAWN_SETSIGMASK) | POSIX_SPAWN_SETPGROUP));
		if (!!((LOC33 == ((NI32) 0)))) goto LA34;
		LOC36 = 0;
		LOC36 = oslasterror_127234();
		raiseoserror_127209(LOC36, ((NimStringDesc*) &TMP3007));
	}
	LA34: ;
	{
		if (!!((*data).optionpoparentstreams)) goto LA39;
		{
			int LOC43;
			NI32 LOC46;
			LOC43 = 0;
			LOC43 = posix_spawn_file_actions_addclose((&fops), (*data).pstdin[(((NI) 1))- 0]);
			if (!!((LOC43 == ((NI32) 0)))) goto LA44;
			LOC46 = 0;
			LOC46 = oslasterror_127234();
			raiseoserror_127209(LOC46, ((NimStringDesc*) &TMP3007));
		}
		LA44: ;
		{
			int LOC49;
			NI32 LOC52;
			LOC49 = 0;
			LOC49 = posix_spawn_file_actions_adddup2((&fops), (*data).pstdin[(((NI) 0))- 0], ((int) 0));
			if (!!((LOC49 == ((NI32) 0)))) goto LA50;
			LOC52 = 0;
			LOC52 = oslasterror_127234();
			raiseoserror_127209(LOC52, ((NimStringDesc*) &TMP3007));
		}
		LA50: ;
		{
			int LOC55;
			NI32 LOC58;
			LOC55 = 0;
			LOC55 = posix_spawn_file_actions_addclose((&fops), (*data).pstdout[(((NI) 0))- 0]);
			if (!!((LOC55 == ((NI32) 0)))) goto LA56;
			LOC58 = 0;
			LOC58 = oslasterror_127234();
			raiseoserror_127209(LOC58, ((NimStringDesc*) &TMP3007));
		}
		LA56: ;
		{
			int LOC61;
			NI32 LOC64;
			LOC61 = 0;
			LOC61 = posix_spawn_file_actions_adddup2((&fops), (*data).pstdout[(((NI) 1))- 0], ((int) 1));
			if (!!((LOC61 == ((NI32) 0)))) goto LA62;
			LOC64 = 0;
			LOC64 = oslasterror_127234();
			raiseoserror_127209(LOC64, ((NimStringDesc*) &TMP3007));
		}
		LA62: ;
		{
			int LOC67;
			NI32 LOC70;
			LOC67 = 0;
			LOC67 = posix_spawn_file_actions_addclose((&fops), (*data).pstderr[(((NI) 0))- 0]);
			if (!!((LOC67 == ((NI32) 0)))) goto LA68;
			LOC70 = 0;
			LOC70 = oslasterror_127234();
			raiseoserror_127209(LOC70, ((NimStringDesc*) &TMP3007));
		}
		LA68: ;
		{
			if (!(*data).optionpostderrtostdout) goto LA73;
			{
				int LOC77;
				NI32 LOC80;
				LOC77 = 0;
				LOC77 = posix_spawn_file_actions_adddup2((&fops), (*data).pstdout[(((NI) 1))- 0], ((int) 2));
				if (!!((LOC77 == ((NI32) 0)))) goto LA78;
				LOC80 = 0;
				LOC80 = oslasterror_127234();
				raiseoserror_127209(LOC80, ((NimStringDesc*) &TMP3007));
			}
			LA78: ;
		}
		goto LA71;
		LA73: ;
		{
			{
				int LOC84;
				NI32 LOC87;
				LOC84 = 0;
				LOC84 = posix_spawn_file_actions_adddup2((&fops), (*data).pstderr[(((NI) 1))- 0], ((int) 2));
				if (!!((LOC84 == ((NI32) 0)))) goto LA85;
				LOC87 = 0;
				LOC87 = oslasterror_127234();
				raiseoserror_127209(LOC87, ((NimStringDesc*) &TMP3007));
			}
			LA85: ;
		}
		LA71: ;
	}
	LA39: ;
	res = 0;
	{
		NimStringDesc* LOC92;
		if (!(((NI) 0) < ((*data).workingdir ? strlen((*data).workingdir) : 0))) goto LA90;
		LOC92 = 0;
		LOC92 = cstrToNimstr((*data).workingdir);
		setcurrentdir_129207(LOC92);
	}
	LA90: ;
	pid = 0;
	{
		if (!(*data).optionpousepath) goto LA95;
		res = posix_spawnp((&pid), (*data).syscommand, (&fops), (&attr), (*data).sysargs, (*data).sysenv);
	}
	goto LA93;
	LA95: ;
	{
		res = posix_spawn((&pid), (*data).syscommand, (&fops), (&attr), (*data).sysargs, (*data).sysenv);
	}
	LA93: ;
	LOC98 = 0;
	LOC98 = posix_spawn_file_actions_destroy((&fops));
	LOC99 = 0;
	LOC99 = posix_spawnattr_destroy((&attr));
	{
		NI32 LOC104;
		if (!!((res == ((NI32) 0)))) goto LA102;
		LOC104 = 0;
		LOC104 = oslasterror_127234();
		raiseoserror_127209(LOC104, ((NimStringDesc*) &TMP3007));
	}
	LA102: ;
	result = pid;
	goto BeforeRet;
	}BeforeRet: ;
	return result;
}

static N_INLINE(void, popSafePoint)(void) {
	exchandler_21043 = (*exchandler_21043).prev;
}

N_NIMCALL(Processobj156404*, nospstartProcess)(NimStringDesc* command, NimStringDesc* workingdir, NimStringDesc** args, NI argsLen0, Stringtableobj146212* env, NU8 options) {
	Processobj156404* result;
	TY158024 pstdin;
	TY158024 pstdout;
	TY158024 pstderr;
	NimStringDesc* syscommand;
	TY131608* sysargsraw;
	pid_t volatile pid;
	NCSTRING* sysargs;
	TSafePoint TMP3010;
	result = 0;
	memset((void*)pstdin, 0, sizeof(pstdin));
	memset((void*)pstdout, 0, sizeof(pstdout));
	memset((void*)pstderr, 0, sizeof(pstderr));
	result = (Processobj156404*) newObj((&NTI156406), sizeof(Processobj156404));
	(*result).Sup.m_type = (&NTI156404);
	(*result).options = options;
	(*result).exitcode = ((int) -3);
	{
		if (!!(((options &(1<<((((NU8) 4))&7)))!=0))) goto LA3;
		{
			NIM_BOOL LOC7;
			NIM_BOOL LOC8;
			int LOC9;
			int LOC11;
			int LOC13;
			NI32 LOC16;
			LOC7 = 0;
			LOC8 = 0;
			LOC9 = 0;
			LOC9 = pipe(pstdin);
			LOC8 = !((LOC9 == ((NI32) 0)));
			if (LOC8) goto LA10;
			LOC11 = 0;
			LOC11 = pipe(pstdout);
			LOC8 = !((LOC11 == ((NI32) 0)));
			LA10: ;
			LOC7 = LOC8;
			if (LOC7) goto LA12;
			LOC13 = 0;
			LOC13 = pipe(pstderr);
			LOC7 = !((LOC13 == ((NI32) 0)));
			LA12: ;
			if (!LOC7) goto LA14;
			LOC16 = 0;
			LOC16 = oslasterror_127234();
			raiseoserror_127209(LOC16, ((NimStringDesc*) &TMP3007));
		}
		LA14: ;
	}
	LA3: ;
	syscommand = 0;
	sysargsraw = 0;
	{
		TY193293 LOC21;
		NimStringDesc* LOC22;
		NimStringDesc* LOC23;
		NimStringDesc* LOC24;
		if (!((options &(1<<((((NU8) 2))&7)))!=0)) goto LA19;
		syscommand = copyString(((NimStringDesc*) &TMP3008));
		sysargsraw = (TY131608*) newSeq((&NTI131608), 3);
		memset((void*)LOC21, 0, sizeof(LOC21));
		LOC21[0] = copyString(syscommand);
		LOC21[1] = copyString(((NimStringDesc*) &TMP3009));
		LOC21[2] = copyString(command);
		LOC22 = 0;
		LOC22 = sysargsraw->data[0]; sysargsraw->data[0] = copyStringRC1(LOC21[0]);
		if (LOC22) nimGCunrefNoCycle(LOC22);
		LOC23 = 0;
		LOC23 = sysargsraw->data[1]; sysargsraw->data[1] = copyStringRC1(LOC21[1]);
		if (LOC23) nimGCunrefNoCycle(LOC23);
		LOC24 = 0;
		LOC24 = sysargsraw->data[2]; sysargsraw->data[2] = copyStringRC1(LOC21[2]);
		if (LOC24) nimGCunrefNoCycle(LOC24);
	}
	goto LA17;
	LA19: ;
	{
		TY192421 LOC26;
		NimStringDesc* LOC27;
		syscommand = copyString(command);
		sysargsraw = (TY131608*) newSeq((&NTI131608), 1);
		memset((void*)LOC26, 0, sizeof(LOC26));
		LOC26[0] = copyString(command);
		LOC27 = 0;
		LOC27 = sysargsraw->data[0]; sysargsraw->data[0] = copyStringRC1(LOC26[0]);
		if (LOC27) nimGCunrefNoCycle(LOC27);
		{
			NimStringDesc* arg_158222;
			NI i_158479;
			arg_158222 = 0;
			i_158479 = ((NI) 0);
			{
				while (1) {
					NimStringDesc* LOC31;
					if (!(i_158479 < argsLen0)) goto LA30;
					arg_158222 = args[i_158479];
					sysargsraw = (TY131608*) incrSeqV2(&(sysargsraw)->Sup, sizeof(NimStringDesc*));
					LOC31 = 0;
					LOC31 = sysargsraw->data[sysargsraw->Sup.len]; sysargsraw->data[sysargsraw->Sup.len] = copyStringRC1(arg_158222);
					if (LOC31) nimGCunrefNoCycle(LOC31);
					++sysargsraw->Sup.len;
					i_158479 += ((NI) 1);
				} LA30: ;
			}
		}
	}
	LA17: ;
	pid = 0;
	sysargs = alloccstringarray_17801(sysargsraw->data, sysargsraw->Sup.len);
	pushSafePoint(&TMP3010);
	TMP3010.status = _setjmp(TMP3010.context);
	if (TMP3010.status == 0) {
		NCSTRING* sysenv;
		TSafePoint TMP3012;
		{
			if (!(env == NIM_NIL)) goto LA35;
			sysenv = envtocstringarray_157854();
		}
		goto LA33;
		LA35: ;
		{
			sysenv = envtocstringarray_157806(env);
		}
		LA33: ;
		pushSafePoint(&TMP3012);
		TMP3012.status = _setjmp(TMP3012.context);
		if (TMP3012.status == 0) {
			Startprocessdata157948 data;
			memset((void*)(&data), 0, sizeof(data));
			data.syscommand = syscommand->data;
			data.sysargs = sysargs;
			data.sysenv = sysenv;
			memcpy((void*)data.pstdin, (NIM_CONST void*)pstdin, sizeof(TY158024));
			memcpy((void*)data.pstdout, (NIM_CONST void*)pstdout, sizeof(TY158024));
			memcpy((void*)data.pstderr, (NIM_CONST void*)pstderr, sizeof(TY158024));
			data.optionpoparentstreams = ((options &(1<<((((NU8) 4))&7)))!=0);
			data.optionpousepath = ((options &(1<<((((NU8) 1))&7)))!=0);
			data.optionpostderrtostdout = ((options &(1<<((((NU8) 3))&7)))!=0);
			data.workingdir = workingdir->data;
			pid = startprocessauxspawn_158001((&data));
			{
				NimStringDesc* LOC43;
				if (!((options &(1<<((((NU8) 0))&7)))!=0)) goto LA41;
				LOC43 = 0;
				LOC43 = nsuJoinSep(args, argsLen0, ((NimStringDesc*) &TMP3013));
				printf("%s%s%s\012", command? (command)->data:"nil", ((NimStringDesc*) &TMP3013)? (((NimStringDesc*) &TMP3013))->data:"nil", LOC43? (LOC43)->data:"nil");
			}
			LA41: ;
			(*result).id = pid;
			{
				if (!((options &(1<<((((NU8) 4))&7)))!=0)) goto LA46;
				(*result).inhandle = ((int) 0);
				(*result).outhandle = ((int) 1);
				{
					if (!((options &(1<<((((NU8) 3))&7)))!=0)) goto LA50;
					(*result).errhandle = (*result).outhandle;
				}
				goto LA48;
				LA50: ;
				{
					(*result).errhandle = ((int) 2);
				}
				LA48: ;
			}
			goto LA44;
			LA46: ;
			{
				int LOC60;
				int LOC61;
				int LOC62;
				(*result).inhandle = pstdin[(((NI) 1))- 0];
				(*result).outhandle = pstdout[(((NI) 0))- 0];
				{
					int LOC58;
					if (!((options &(1<<((((NU8) 3))&7)))!=0)) goto LA56;
					(*result).errhandle = (*result).outhandle;
					LOC58 = 0;
					LOC58 = close(pstderr[(((NI) 0))- 0]);
				}
				goto LA54;
				LA56: ;
				{
					(*result).errhandle = pstderr[(((NI) 0))- 0];
				}
				LA54: ;
				LOC60 = 0;
				LOC60 = close(pstderr[(((NI) 1))- 0]);
				LOC61 = 0;
				LOC61 = close(pstdin[(((NI) 0))- 0]);
				LOC62 = 0;
				LOC62 = close(pstdout[(((NI) 1))- 0]);
			}
			LA44: ;
			popSafePoint();
		}
		else {
			popSafePoint();
		}
		{
			dealloccstringarray_18050(sysenv);
		}
		if (TMP3012.status != 0) reraiseException();
		popSafePoint();
	}
	else {
		popSafePoint();
	}
	{
		dealloccstringarray_18050(sysargs);
	}
	if (TMP3010.status != 0) reraiseException();
	return result;
}

N_NIMCALL(void, createstream_159241)(Streamobj150427** stream, int* handle, NU8 filemode) {
	FILE* f;
	Filestreamobj151667* LOC7;
	f = 0;
	{
		NIM_BOOL LOC3;
		NI32 LOC6;
		LOC3 = 0;
		LOC3 = open_17011(&f, (*handle), filemode);
		if (!!(LOC3)) goto LA4;
		LOC6 = 0;
		LOC6 = oslasterror_127234();
		raiseoserror_127209(LOC6, ((NimStringDesc*) &TMP3007));
	}
	LA4: ;
	LOC7 = 0;
	LOC7 = newfilestream_151743(f);
	unsureAsgnRef((void**) (&(*stream)), &LOC7->Sup);
}

N_NIMCALL(Streamobj150427*, nospinputStream)(Processobj156404* p) {
	Streamobj150427* result;
{	result = 0;
	{
		if (!((*p).instream == NIM_NIL)) goto LA3;
		createstream_159241(&(*p).instream, (&(*p).inhandle), ((NU8) 1));
	}
	LA3: ;
	result = (*p).instream;
	goto BeforeRet;
	}BeforeRet: ;
	return result;
}

N_NIMCALL(Streamobj150427*, nospoutputStream)(Processobj156404* p) {
	Streamobj150427* result;
{	result = 0;
	{
		if (!((*p).outstream == NIM_NIL)) goto LA3;
		createstream_159241(&(*p).outstream, (&(*p).outhandle), ((NU8) 0));
	}
	LA3: ;
	result = (*p).outstream;
	goto BeforeRet;
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NI, nospwaitForExit)(Processobj156404* p, NI timeout) {
	NI result;
{	result = 0;
	{
		if (!!(((*p).exitcode == ((NI32) -3)))) goto LA3;
		result = ((NI) ((*p).exitcode));
		goto BeforeRet;
	}
	LA3: ;
	{
		pid_t LOC7;
		NI32 LOC10;
		LOC7 = 0;
		LOC7 = waitpid((*p).id, (&(*p).exitcode), ((int) 0));
		if (!(LOC7 < ((NI) 0))) goto LA8;
		(*p).exitcode = ((int) -3);
		LOC10 = 0;
		LOC10 = oslasterror_127234();
		raiseoserror_127209(LOC10, ((NimStringDesc*) &TMP3007));
	}
	LA8: ;
	result = (NI)((NU32)(((NI) ((*p).exitcode))) >> (NU32)(((NI) 8)));
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NI, nospexecCmd)(NimStringDesc* command) {
	NI result;
	int LOC1;
	result = 0;
	LOC1 = 0;
	LOC1 = system(command->data);
	result = ((NI) (LOC1));
	return result;
}

N_NIMCALL(NI, peekexitcode_156725)(Processobj156404* p) {
	NI result;
	pid_t ret;
	NIM_BOOL b;
{	result = 0;
	{
		if (!!(((*p).exitcode == ((NI32) -3)))) goto LA3;
		result = ((NI) ((*p).exitcode));
		goto BeforeRet;
	}
	LA3: ;
	ret = waitpid((*p).id, (&(*p).exitcode), WNOHANG);
	b = (ret == (*p).id);
	{
		if (!b) goto LA7;
		result = ((NI) -1);
	}
	LA7: ;
	{
		NIM_BOOL LOC11;
		LOC11 = 0;
		LOC11 = WIFEXITED((*p).exitcode);
		if (!!(LOC11)) goto LA12;
		result = ((NI) -1);
	}
	goto LA9;
	LA12: ;
	{
		result = (NI)((NU32)(((NI) ((*p).exitcode))) >> (NU32)(((NI) 8)));
	}
	LA9: ;
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NI, nospcountProcessors)(void) {
	NI result;
	result = 0;
	result = ncpicountProcessors();
	return result;
}
N_NIMCALL(void, TMP4592)(void* p, NI op) {
	TY157201* a;
	NI LOC1;
	a = (TY157201*)p;
	LOC1 = 0;
	for (LOC1 = 0; LOC1 < a->Sup.len; LOC1++) {
	nimGCvisit((void*)a->data[LOC1], op);
	}
}

static N_INLINE(NIM_BOOL, canbecycleroot_52867)(Cell49147* c) {
	NIM_BOOL result;
	result = 0;
	result = !((((*(*c).typ).flags &(1<<((((NU8) 1))&7)))!=0));
	return result;
}

static N_INLINE(void, rtladdcycleroot_53623)(Cell49147* c) {
	{
		if (!!((((NI) ((NI)((*c).refcount & ((NI) 3)))) == ((NI) 3)))) goto LA3;
		(*c).refcount = (NI)((NI)((*c).refcount & ((NI) -4)) | ((NI) 3));
		incl_49865((&gch_51259.cycleroots), c);
	}
	LA3: ;
}

static N_INLINE(void, incref_55222)(Cell49147* c) {
	(*c).refcount = (NI)((NU32)((*c).refcount) + (NU32)(((NI) 8)));
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = canbecycleroot_52867(c);
		if (!LOC3) goto LA4;
		rtladdcycleroot_53623(c);
	}
	LA4: ;
}

static N_INLINE(void, decref_54804)(Cell49147* c) {
	{
		(*c).refcount -= ((NI) 8);
		if (!((NU32)((*c).refcount) < (NU32)(((NI) 8)))) goto LA3;
		rtladdzct_54404(c);
	}
	goto LA1;
	LA3: ;
	{
		NIM_BOOL LOC6;
		LOC6 = 0;
		LOC6 = canbecycleroot_52867(c);
		if (!LOC6) goto LA7;
		rtladdcycleroot_53623(c);
	}
	goto LA1;
	LA7: ;
	LA1: ;
}

static N_INLINE(void, asgnRef)(void** dest, void* src) {
	{
		Cell49147* LOC5;
		if (!!((src == NIM_NIL))) goto LA3;
		LOC5 = 0;
		LOC5 = usrtocell_52846(src);
		incref_55222(LOC5);
	}
	LA3: ;
	{
		Cell49147* LOC10;
		if (!!(((*dest) == NIM_NIL))) goto LA8;
		LOC10 = 0;
		LOC10 = usrtocell_52846((*dest));
		decref_54804(LOC10);
	}
	LA8: ;
	(*dest) = src;
}

N_NIMCALL(NIM_BOOL, nosprunning)(Processobj156404* p) {
	NIM_BOOL result;
	NI ret;
{	result = 0;
	ret = 0;
	ret = waitpid((*p).id, (&(*p).exitcode), WNOHANG);
	{
		if (!(ret == ((NI) 0))) goto LA3;
		result = NIM_TRUE;
		goto BeforeRet;
	}
	LA3: ;
	result = (ret == (*p).id);
	}BeforeRet: ;
	return result;
}

N_NIMCALL(void, nospclose)(Processobj156404* p) {
	int LOC13;
	int LOC14;
	int LOC15;
	{
		if (!!(((*p).instream == NIM_NIL))) goto LA3;
		close_150491((*p).instream);
	}
	LA3: ;
	{
		if (!!(((*p).outstream == NIM_NIL))) goto LA7;
		close_150491((*p).outstream);
	}
	LA7: ;
	{
		if (!!(((*p).errstream == NIM_NIL))) goto LA11;
		close_150491((*p).errstream);
	}
	LA11: ;
	LOC13 = 0;
	LOC13 = close((*p).inhandle);
	LOC14 = 0;
	LOC14 = close((*p).outhandle);
	LOC15 = 0;
	LOC15 = close((*p).errhandle);
}

N_NIMCALL(NI, nospexecProcesses)(NimStringDesc** cmds, NI cmdsLen0, NU8 options_156793, NI n, TY204876 beforerunevent, TY204882 afterrunevent) {
	NI result;
	NU8 options;
	result = 0;
	options = (options_156793 & ~ 16);
	{
		TY157201* q;
		NI m;
		NI i;
		if (!(((NI) 1) < n)) goto LA3;
		q = 0;
		q = (TY157201*) newSeq((&NTI157201), ((NI) (n)));
		m = ((n <= cmdsLen0) ? n : cmdsLen0);
		{
			NI i_157235;
			NI HEX3Atmp_157555;
			NI res_157558;
			i_157235 = 0;
			HEX3Atmp_157555 = 0;
			HEX3Atmp_157555 = (NI)(m - ((NI) 1));
			res_157558 = ((NI) 0);
			{
				while (1) {
					TY253724 LOC12;
					if (!(res_157558 <= HEX3Atmp_157555)) goto LA7;
					i_157235 = res_157558;
					{
						if (!!((beforerunevent.ClPrc == TMP4593.ClPrc && beforerunevent.ClEnv == TMP4593.ClEnv))) goto LA10;
						beforerunevent.ClEnv? beforerunevent.ClPrc(i_157235, beforerunevent.ClEnv):((TMP4594)(beforerunevent.ClPrc))(i_157235);
					}
					LA10: ;
					memset((void*)LOC12, 0, sizeof(LOC12));
					asgnRef((void**) (&q->data[i_157235]), nospstartProcess(cmds[i_157235], ((NimStringDesc*) &TMP3007), LOC12, 0, NIM_NIL, (options | 4)));
					res_157558 += ((NI) 1);
				} LA7: ;
			}
		}
		i = m;
		{
			while (1) {
				if (!(i <= (cmdsLen0-1))) goto LA14;
				nossleep(((NI) 50));
				{
					NI r_157413;
					NI HEX3Atmp_157564;
					NI res_157567;
					r_157413 = 0;
					HEX3Atmp_157564 = 0;
					HEX3Atmp_157564 = (NI)(n - ((NI) 1));
					res_157567 = ((NI) 0);
					{
						while (1) {
							if (!(res_157567 <= HEX3Atmp_157564)) goto LA17;
							r_157413 = res_157567;
							{
								NIM_BOOL LOC20;
								NI LOC23;
								TY253724 LOC36;
								LOC20 = 0;
								LOC20 = nosprunning(q->data[r_157413]);
								if (!!(LOC20)) goto LA21;
								LOC23 = 0;
								LOC23 = nospwaitForExit(q->data[r_157413], ((NI) -1));
								result = ((LOC23 >= result) ? LOC23 : result);
								{
									if (!!((afterrunevent.ClPrc == TMP4595.ClPrc && afterrunevent.ClEnv == TMP4595.ClEnv))) goto LA26;
									afterrunevent.ClEnv? afterrunevent.ClPrc(r_157413, q->data[r_157413], afterrunevent.ClEnv):((TMP4596)(afterrunevent.ClPrc))(r_157413, q->data[r_157413]);
								}
								LA26: ;
								{
									if (!!((q->data[r_157413] == NIM_NIL))) goto LA30;
									nospclose(q->data[r_157413]);
								}
								LA30: ;
								{
									if (!!((beforerunevent.ClPrc == TMP4593.ClPrc && beforerunevent.ClEnv == TMP4593.ClEnv))) goto LA34;
									beforerunevent.ClEnv? beforerunevent.ClPrc(i, beforerunevent.ClEnv):((TMP4597)(beforerunevent.ClPrc))(i);
								}
								LA34: ;
								memset((void*)LOC36, 0, sizeof(LOC36));
								asgnRef((void**) (&q->data[r_157413]), nospstartProcess(cmds[i], ((NimStringDesc*) &TMP3007), LOC36, 0, NIM_NIL, (options | 4)));
								i += ((NI) 1);
								{
									if (!((cmdsLen0-1) < i)) goto LA39;
									goto LA15;
								}
								LA39: ;
							}
							LA21: ;
							res_157567 += ((NI) 1);
						} LA17: ;
					}
				} LA15: ;
			} LA14: ;
		}
		{
			NI j_157480;
			NI HEX3Atmp_157572;
			NI res_157575;
			j_157480 = 0;
			HEX3Atmp_157572 = 0;
			HEX3Atmp_157572 = (NI)(m - ((NI) 1));
			res_157575 = ((NI) 0);
			{
				while (1) {
					NI LOC44;
					if (!(res_157575 <= HEX3Atmp_157572)) goto LA43;
					j_157480 = res_157575;
					LOC44 = 0;
					LOC44 = nospwaitForExit(q->data[j_157480], ((NI) -1));
					result = ((LOC44 >= result) ? LOC44 : result);
					{
						if (!!((afterrunevent.ClPrc == TMP4595.ClPrc && afterrunevent.ClEnv == TMP4595.ClEnv))) goto LA47;
						afterrunevent.ClEnv? afterrunevent.ClPrc(j_157480, q->data[j_157480], afterrunevent.ClEnv):((TMP4598)(afterrunevent.ClPrc))(j_157480, q->data[j_157480]);
					}
					LA47: ;
					{
						if (!!((q->data[j_157480] == NIM_NIL))) goto LA51;
						nospclose(q->data[j_157480]);
					}
					LA51: ;
					res_157575 += ((NI) 1);
				} LA43: ;
			}
		}
	}
	goto LA1;
	LA3: ;
	{
		{
			NI i_157516;
			NI HEX3Atmp_157580;
			NI res_157583;
			i_157516 = 0;
			HEX3Atmp_157580 = 0;
			HEX3Atmp_157580 = (cmdsLen0-1);
			res_157583 = ((NI) 0);
			{
				while (1) {
					Processobj156404* p;
					TY253724 LOC61;
					NI LOC62;
					if (!(res_157583 <= HEX3Atmp_157580)) goto LA56;
					i_157516 = res_157583;
					{
						if (!!((beforerunevent.ClPrc == TMP4593.ClPrc && beforerunevent.ClEnv == TMP4593.ClEnv))) goto LA59;
						beforerunevent.ClEnv? beforerunevent.ClPrc(i_157516, beforerunevent.ClEnv):((TMP4599)(beforerunevent.ClPrc))(i_157516);
					}
					LA59: ;
					memset((void*)LOC61, 0, sizeof(LOC61));
					p = nospstartProcess(cmds[i_157516], ((NimStringDesc*) &TMP3007), LOC61, 0, NIM_NIL, (options | 4));
					LOC62 = 0;
					LOC62 = nospwaitForExit(p, ((NI) -1));
					result = ((LOC62 >= result) ? LOC62 : result);
					{
						if (!!((afterrunevent.ClPrc == TMP4595.ClPrc && afterrunevent.ClEnv == TMP4595.ClEnv))) goto LA65;
						afterrunevent.ClEnv? afterrunevent.ClPrc(i_157516, p, afterrunevent.ClEnv):((TMP4600)(afterrunevent.ClPrc))(i_157516, p);
					}
					LA65: ;
					nospclose(p);
					res_157583 += ((NI) 1);
				} LA56: ;
			}
		}
	}
	LA1: ;
	return result;
}
NIM_EXTERNC N_NOINLINE(void, stdlib_osprocInit000)(void) {
}

NIM_EXTERNC N_NOINLINE(void, stdlib_osprocDatInit000)(void) {
static TNimNode* TMP3002[9];
static TNimNode* TMP3003[7];
NI TMP3005;
static char* NIM_CONST TMP3004[7] = {
"poEchoCmd", 
"poUsePath", 
"poEvalCommand", 
"poStdErrToStdOut", 
"poParentStreams", 
"poInteractive", 
"poDemon"};
static TNimNode TMP31[19];
NTI156404.size = sizeof(Processobj156404);
NTI156404.kind = 17;
NTI156404.base = (&NTI4011);
TMP3002[0] = &TMP31[1];
TMP31[1].kind = 1;
TMP31[1].offset = offsetof(Processobj156404, inhandle);
TMP31[1].typ = (&NTI16608);
TMP31[1].name = "inHandle";
TMP3002[1] = &TMP31[2];
TMP31[2].kind = 1;
TMP31[2].offset = offsetof(Processobj156404, outhandle);
TMP31[2].typ = (&NTI16608);
TMP31[2].name = "outHandle";
TMP3002[2] = &TMP31[3];
TMP31[3].kind = 1;
TMP31[3].offset = offsetof(Processobj156404, errhandle);
TMP31[3].typ = (&NTI16608);
TMP31[3].name = "errHandle";
TMP3002[3] = &TMP31[4];
TMP31[4].kind = 1;
TMP31[4].offset = offsetof(Processobj156404, instream);
TMP31[4].typ = (&NTI150425);
TMP31[4].name = "inStream";
TMP3002[4] = &TMP31[5];
TMP31[5].kind = 1;
TMP31[5].offset = offsetof(Processobj156404, outstream);
TMP31[5].typ = (&NTI150425);
TMP31[5].name = "outStream";
TMP3002[5] = &TMP31[6];
TMP31[6].kind = 1;
TMP31[6].offset = offsetof(Processobj156404, errstream);
TMP31[6].typ = (&NTI150425);
TMP31[6].name = "errStream";
TMP3002[6] = &TMP31[7];
TMP31[7].kind = 1;
TMP31[7].offset = offsetof(Processobj156404, id);
TMP31[7].typ = (&NTI114289);
TMP31[7].name = "id";
TMP3002[7] = &TMP31[8];
TMP31[8].kind = 1;
TMP31[8].offset = offsetof(Processobj156404, exitcode);
TMP31[8].typ = (&NTI8211);
TMP31[8].name = "exitCode";
TMP3002[8] = &TMP31[9];
NTI156402.size = sizeof(NU8);
NTI156402.kind = 14;
NTI156402.base = 0;
NTI156402.flags = 3;
for (TMP3005 = 0; TMP3005 < 7; TMP3005++) {
TMP31[TMP3005+10].kind = 1;
TMP31[TMP3005+10].offset = TMP3005;
TMP31[TMP3005+10].name = TMP3004[TMP3005];
TMP3003[TMP3005] = &TMP31[TMP3005+10];
}
TMP31[17].len = 7; TMP31[17].kind = 2; TMP31[17].sons = &TMP3003[0];
NTI156402.node = &TMP31[17];
NTI156640.size = sizeof(NU8);
NTI156640.kind = 19;
NTI156640.base = (&NTI156402);
NTI156640.flags = 3;
TMP31[18].len = 0; TMP31[18].kind = 0;
NTI156640.node = &TMP31[18];
TMP31[9].kind = 1;
TMP31[9].offset = offsetof(Processobj156404, options);
TMP31[9].typ = (&NTI156640);
TMP31[9].name = "options";
TMP31[0].len = 9; TMP31[0].kind = 2; TMP31[0].sons = &TMP3002[0];
NTI156404.node = &TMP31[0];
NTI156406.size = sizeof(Processobj156404*);
NTI156406.kind = 22;
NTI156406.base = (&NTI156404);
NTI156406.marker = TMP3006;
NTI157201.size = sizeof(TY157201*);
NTI157201.kind = 24;
NTI157201.base = (&NTI156406);
NTI157201.marker = TMP4592;
}

