// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %r13
movl %r13d, %esi
xorl %edx, %edx
callq 0x77e4
movq %rax, %r14
pushq $0x2
popq %rsi
pushq $0x5
popq %rbp
movq %rbx, %rdi
movl %ebp, %edx
callq 0x7867
pushq $0x3
popq %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x7867
pushq $0x4
popq %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x7867
callq 0x3430
testl %eax, %eax
je 0x404e
movl %eax, %r15d
leaq 0x110f7(%rip), %rdx     # 0x15028
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0x428f
movq %rax, %rbx
andq $0x0, 0x8(%rax)
movq 0x1c305(%rip), %rax     # 0x20250
movq %rax, (%rbx)
movq %rbx, 0x1c2fb(%rip)     # 0x20250
leaq 0x18(%rsp), %rdi
movl $0x90, %edx
xorl %esi, %esi
callq 0x31a0
cmpb $0x0, 0x1c2eb(%rip)     # 0x20258
jne 0x3fc3
movb $0x1, 0x1c2e2(%rip)     # 0x20258
leaq 0x4d0(%rip), %rax       # 0x444d
movq %rax, 0x10(%rsp)
movl $0x10000000, 0x98(%rsp) # imm = 0x10000000
leaq 0x1c2ac(%rip), %rdi     # 0x20240
callq 0x31d0
testl %eax, %eax
jne 0x3fdc
movl 0x1c29d(%rip), %edi     # 0x20240
pushq $0x1
popq %rsi
callq 0x4243
cmpl $-0x1, %eax
je 0x3ffd
pushq $0x11
popq %rdi
leaq 0x10(%rsp), %rsi
xorl %edx, %edx
callq 0x3090
testl %eax, %eax
jne 0x401b
movl %r15d, 0x8(%rbx)
pushq $0x1
popq %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1c00d(%rip), %rax     # 0x1fff0
movq (%rax), %r14
callq 0x3060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x3410
leaq 0x11137(%rip), %rsi     # 0x15132
jmp 0x403a
movq 0x1bfec(%rip), %rax     # 0x1fff0
movq (%rax), %rcx
leaq 0x11143(%rip), %rdi     # 0x15151
pushq $0x27
popq %rsi
pushq $0x1
popq %rdx
callq 0x33e0
jmp 0x3fc3
movq 0x1bfce(%rip), %rax     # 0x1fff0
movq (%rax), %r14
callq 0x3060
movl (%rax), %ebp
movl %ebp, %edi
callq 0x3410
leaq 0x1113f(%rip), %rsi     # 0x15179
movq %r14, %rdi
movl %ebp, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x3250
jmp 0x3fc3
movq %r14, 0x8(%rsp)
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x3260
pushq $0x2
popq %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x3030
testl %eax, %eax
jne 0x4154
pushq $-0x1
popq %r12
movl %r13d, %ebp
movq %rbx, %rdi
pushq $0x4
popq %rsi
movl %ebp, %edx
callq 0x641f
movq %rbx, %rdi
movl %r12d, %esi
callq 0x5ba4
cmpl $0x5, %eax
jne 0x4120
movq %rbx, %rdi
movl %r12d, %esi
movl %r13d, %edx
callq 0x641f
movq %rbx, %rdi
pushq $-0x2
popq %r14
movl %r14d, %esi
pushq $0x2
popq %rdx
callq 0x641f
movq %rbx, %rdi
movl %r14d, %esi
callq 0x4491
movl %eax, %r15d
movq %rbx, %rdi
movl %r12d, %r14d
movl %r12d, %esi
callq 0x4491
movl %eax, %r12d
movq %rbx, %rdi
pushq $-0x4
popq %rsi
callq 0x59b7
cmpl $-0x1, %r15d
je 0x412b
testl %r12d, %r12d
js 0x410e
cmpl %r15d, %r12d
je 0x4102
movl %r12d, %edi
movl %r15d, %esi
callq 0x3150
movl %r15d, %edi
xorl %esi, %esi
callq 0x4243
jmp 0x4116
movl %r15d, %edi
callq 0x31c0
incl %ebp
movl %r14d, %r12d
jmp 0x407c
pushq $-0x2
popq %rsi
movq %rbx, %rdi
callq 0x59b7
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x43b7
movq %rax, %r15
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x43b7
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x3210
pushq $0x7f
popq %rdi
callq 0x33c0