// File: /workspace/github_c_makefile_star2_O0/justy989[P]ce/ce_subprocess.c
// Function: bidirectional_popen
static pid_t bidirectional_popen(const char* cmd, CeProcCommFlag_t comms, int* in_fd, int* out_fd,
                                 bool use_shell){
     int input_fds[2];
     int output_fds[2];

     if(pipe(input_fds) != 0) return 0;
     if(pipe(output_fds) != 0) return 0;

     pid_t pid = fork();
     if(pid < 0) return 0;

     if(pid == 0){
          close(input_fds[1]);
          close(output_fds[0]);

          dup2(input_fds[0], STDIN_FILENO);
          dup2(output_fds[1], STDOUT_FILENO);
          dup2(output_fds[1], STDERR_FILENO);

          if(use_shell){
              // TODO: run user's SHELL ?
              execl("/bin/sh", "/bin/sh", "-c", cmd, NULL);
          }else{
              char** args = _split_command_args(cmd);
              execv(args[0], args);
              // Idk if we can actually free our memory here but whateva...
              int64_t args_index = 0;
              while(args[args_index]){
                  free(args[args_index]);
                  args_index++;
              }
              free(args);
          }
     }else{
         close(input_fds[0]);
         close(output_fds[1]);
         if(comms & CE_PROC_COMM_STDIN){
             *in_fd = input_fds[1];
         }else{
             close(input_fds[1]);
             *in_fd = -1;
         }
         if(comms & CE_PROC_COMM_STDOUT){
             *out_fd = output_fds[0];
         }else{
             close(output_fds[0]);
             *out_fd = -1;
         }
     }

     return pid;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movb %r8b, %al
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
andb $0x1, %al
movb %al, -0x29(%rbp)
leaq -0x34(%rbp), %rdi
callq 0xd2e0
cmpl $0x0, %eax
je 0x2ace9
movl $0x0, -0x4(%rbp)
jmp 0x2ae4a
leaq -0x3c(%rbp), %rdi
callq 0xd2e0
cmpl $0x0, %eax
je 0x2ad03
movl $0x0, -0x4(%rbp)
jmp 0x2ae4a
callq 0xd6c0
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jge 0x2ad1d
movl $0x0, -0x4(%rbp)
jmp 0x2ae4a
cmpl $0x0, -0x40(%rbp)
jne 0x2ade2
movl -0x30(%rbp), %edi
callq 0xd2d0
movl -0x3c(%rbp), %edi
callq 0xd2d0
movl -0x34(%rbp), %edi
xorl %esi, %esi
callq 0xd210
movl -0x38(%rbp), %edi
movl $0x1, %esi
callq 0xd210
movl -0x38(%rbp), %edi
movl $0x2, %esi
callq 0xd210
testb $0x1, -0x29(%rbp)
je 0x2ad84
movq -0x10(%rbp), %rcx
leaq 0x1a0e3(%rip), %rsi     # 0x44e4f
leaq 0x1a0e4(%rip), %rdx     # 0x44e57
xorl %eax, %eax
movl %eax, %r8d
movq %rsi, %rdi
movb $0x0, %al
callq 0xd640
jmp 0x2ade0
movq -0x10(%rbp), %rdi
callq 0x2ab20
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq (%rax), %rdi
movq -0x48(%rbp), %rsi
callq 0xd6b0
movq $0x0, -0x50(%rbp)
movq -0x48(%rbp), %rax
movq -0x50(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x2add7
movq -0x48(%rbp), %rax
movq -0x50(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0xd090
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x2ada9
movq -0x48(%rbp), %rdi
callq 0xd090
jmp 0x2ae44
movl -0x34(%rbp), %edi
callq 0xd2d0
movl -0x38(%rbp), %edi
callq 0xd2d0
movl -0x14(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x2ae08
movl -0x30(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
jmp 0x2ae1a
movl -0x30(%rbp), %edi
callq 0xd2d0
movq -0x20(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl -0x14(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x2ae30
movl -0x3c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
jmp 0x2ae42
movl -0x3c(%rbp), %edi
callq 0xd2d0
movq -0x28(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
jmp 0x2ae44
movl -0x40(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)