// File: /workspace/github_c_makefile_star2_O3/bhk[P]tooltree/monoglot/./xpio_c.c
// Function: xpio__spawn
static int xpio__spawn(lua_State *L)
{
   const char *path = luaL_checkstring(L, 1);
   luaL_checktype(L, 2, LUA_TTABLE);
   luaL_checktype(L, 3, LUA_TTABLE);
   luaL_checktype(L, 4, LUA_TTABLE);

   pid_t pid = fork();
   if (pid) {
      XPProc *pproc = xpproc_new(L);
      pproc->pid = pid;
      return 1;
   }

   // exec

   // reset signals

   // TODO: what about reversing SIG_IGN for SIGPIPE?
   sigset_t sigmask;
   sigemptyset(&sigmask);
   BAIL_IF(sigprocmask(SIG_SETMASK, &sigmask, NULL));

   // perform file actions

   int fdFrom, fdTo, ndx;
   for (ndx = 1; xpio_getAction(L, 4, ndx, &fdFrom, &fdTo); ++ndx) {
      if (fdFrom >= 0) {
         if (fdFrom != fdTo) {
            dup2(fdFrom, fdTo);
         }
         // make granted FDs blocking
         (void) setNonBlocking(fdTo, 0);
      } else {
         close(fdTo);
      }
   }

   char **argv = readStringArray(L, 2);
   char **envp = readStringArray(L, 3);

   execve(path, argv, envp);

 bail:
   exit(127);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
movl $0x1, %esi
xorl %edx, %edx
callq 0x7c9c
movq %rax, %r14
movq %rbx, %rdi
movl $0x2, %esi
movl $0x5, %edx
callq 0x7d2c
movq %rbx, %rdi
movl $0x3, %esi
movl $0x5, %edx
callq 0x7d2c
movq %rbx, %rdi
movl $0x4, %esi
movl $0x5, %edx
callq 0x7d2c
callq 0x3430
testl %eax, %eax
je 0x415f
movl %eax, %ebp
leaq 0x12f95(%rip), %rdx     # 0x17028
movl $0x10, %esi
movq %rbx, %rdi
callq 0x43d1
movq %rax, %rbx
movq $0x0, 0x8(%rax)
movq 0x1c19e(%rip), %rax     # 0x20250
movq %rax, (%rbx)
movq %rbx, 0x1c194(%rip)     # 0x20250
leaq 0x18(%rsp), %rdi
movl $0x90, %edx
xorl %esi, %esi
callq 0x31a0
cmpb $0x0, 0x1c184(%rip)     # 0x20258
jne 0x4130
movb $0x1, 0x1c17b(%rip)     # 0x20258
leaq 0x553(%rip), %rax       # 0x4637
movq %rax, 0x10(%rsp)
movl $0x10000000, 0x98(%rsp) # imm = 0x10000000
leaq 0x1c145(%rip), %rdi     # 0x20240
callq 0x31d0
testl %eax, %eax
jne 0x414a
movl 0x1c136(%rip), %edi     # 0x20240
movl $0x800, %esi            # imm = 0x800
xorl %edx, %edx
callq 0x442e
cmpl $-0x1, %eax
je 0x4151
leaq 0x10(%rsp), %rsi
movl $0x11, %edi
xorl %edx, %edx
callq 0x3090
testl %eax, %eax
jne 0x4158
movl %ebp, 0x8(%rbx)
movl $0x1, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3470
jmp 0x4130
callq 0x34df
jmp 0x4130
callq 0x34a7
jmp 0x4130
movq %r14, 0x8(%rsp)
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x3260
movl $0x2, %edi
movq %r15, %rsi
xorl %edx, %edx
callq 0x3030
testl %eax, %eax
jne 0x42be
movq %rbx, %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x6855
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x5f9e
cmpl $0x5, %eax
jne 0x4284
movl $0x2, %ebp
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x1, %edx
callq 0x6855
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
movl $0x2, %edx
callq 0x6855
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
leaq 0x4(%rsp), %r15
movq %r15, %rdx
callq 0x6237
movq %rax, %r12
movl 0x4(%rsp), %r14d
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movq %r15, %rdx
callq 0x6237
cmpl $0x1, 0x4(%rsp)
movq %rax, %r15
movl $0x0, %r13d
sbbl %r13d, %r13d
movq %rbx, %rdi
movl $0xfffffffc, %esi       # imm = 0xFFFFFFFC
callq 0x5c94
testl %r14d, %r14d
je 0x4291
cmpl $-0x1, %r12d
je 0x4291
orl %r15d, %r13d
js 0x4255
cmpl %r12d, %r13d
je 0x4244
movl %r13d, %edi
movl %r12d, %esi
callq 0x3150
movl %r12d, %edi
xorl %esi, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x442e
jmp 0x425d
movl %r12d, %edi
callq 0x31c0
movq %rbx, %rdi
movl $0x4, %esi
movl %ebp, %edx
callq 0x6855
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x5f9e
incl %ebp
cmpl $0x5, %eax
je 0x41b5
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x5c94
movq %rbx, %rdi
movl $0x2, %esi
callq 0x4586
movq %rax, %r15
movq %rbx, %rdi
movl $0x3, %esi
callq 0x4586
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x3210
movl $0x7f, %edi
callq 0x33c0