// File: /workspace/github_cpp_makefile_star2_O3/solaldunckel[P]webserv/srcs/HTTP/CGI.cpp
// Function: CGI::execute()
int CGI::execute() {
  file_path_ = cwd_ + "/" + file_.getPath();

  if (!setCGIEnv())
    return 500;
  if (!(argv_[0] = ft::strdup(cgi_path_.c_str())))
    return 500;
  if (!(argv_[1] = ft::strdup(file_path_.c_str())))
    return 500;
  argv_[2] = NULL;

  int pip[2];

  if (pipe(pip) != 0)
    return 500;

  pid_t pid = fork();

  if (pid == 0) {
    if (chdir(file_path_.substr(0, file_path_.find_last_of('/')).c_str()) == -1)
      return 500;
    close(pip[1]);
    if (dup2(pip[0], 0) == -1)
      return 500;
    if (dup2(tmp_file_.getFd(), 1) == -1)
      return 500;
    close(pip[0]);
    execve(argv_[0], argv_, env_);
    exit(1);
  }
  else if (pid > 0) {
    close(pip[0]);
    if (req_body_.length() && write(pip[1], req_body_.c_str(), req_body_.length()) <= 0)
      return 500;
    close(pip[1]);

    int status;

    if (waitpid(pid, &status, 0) == -1)
      return 500;
    if (WIFEXITED(status) && WEXITSTATUS(status))
      return 502;
  }
  else {
    return 502;
  }

  body_ = tmp_file_.getContent();
  return 200;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
leaq 0x18(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x78(%rdi), %rsi
movq 0x80(%rdi), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x6c2a
leaq 0x16126(%rip), %rsi     # 0x25594
movq %r14, %rdi
callq 0x4840
movq (%rbx), %rdi
callq 0x1f480
movq (%rax), %rsi
movq 0x8(%rax), %rdx
leaq 0x8(%rsp), %rdi
callq 0x4100
leaq 0x38(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xf4b7
movq %rdx, 0x28(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x38(%rsp)
jmp 0xf4bf
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rdx
leaq 0x28(%rsp), %r15
movq %rdx, 0x8(%r15)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x98(%rbx), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x4570
movq (%r15), %rdi
cmpq %r12, %rdi
je 0xf502
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x4470
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0xf519
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x4470
movq %rbx, %rdi
callq 0xf736
movl $0x1f4, %ebp            # imm = 0x1F4
testb %al, %al
je 0xf575
movq 0x18(%rbx), %rdi
callq 0x2438f
movq %rax, 0x1d0(%rbx)
testq %rax, %rax
je 0xf575
movq 0x98(%rbx), %rdi
callq 0x2438f
movq %rax, 0x1d8(%rbx)
testq %rax, %rax
je 0xf575
movq $0x0, 0x1e0(%rbx)
leaq 0x8(%rsp), %rdi
callq 0x45c0
movl $0x1f4, %ebp            # imm = 0x1F4
testl %eax, %eax
je 0xf586
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x40d0
testl %eax, %eax
je 0xf646
jle 0xf707
movl %eax, %r15d
movl 0x8(%rsp), %edi
callq 0x4850
movq 0xe0(%rbx), %rdx
testq %rdx, %rdx
je 0xf5c6
movl 0xc(%rsp), %edi
movq 0xd8(%rbx), %rsi
callq 0x4150
testq %rax, %rax
jle 0xf575
movl 0xc(%rsp), %edi
callq 0x4850
leaq 0x28(%rsp), %rsi
movl %r15d, %edi
xorl %edx, %edx
callq 0x4370
cmpl $-0x1, %eax
je 0xf575
movl 0x28(%rsp), %eax
testb $0x7f, %al
sete %cl
testl $0xff00, %eax           # imm = 0xFF00
setne %al
movl $0x1f6, %ebp            # imm = 0x1F6
testb %al, %cl
jne 0xf575
leaq 0xf8(%rbx), %rsi
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x1ed12
addq $0xb8, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4570
movq (%r14), %rdi
cmpq %r12, %rdi
je 0xf63c
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x4470
movl $0xc8, %ebp
jmp 0xf575
movq %r14, %rdi
movl $0x2f, %esi
movq $-0x1, %rdx
callq 0x4860
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4490
movq (%r15), %rdi
callq 0x4620
movl %eax, %r14d
movq (%r15), %rdi
cmpq %r12, %rdi
je 0xf68f
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x4470
cmpl $-0x1, %r14d
je 0xf575
movl 0xc(%rsp), %edi
callq 0x4850
movl 0x8(%rsp), %edi
xorl %esi, %esi
callq 0x4400
cmpl $-0x1, %eax
je 0xf575
leaq 0xf8(%rbx), %rdi
callq 0x1f476
movl (%rax), %edi
movl $0x1, %esi
callq 0x4400
cmpl $-0x1, %eax
je 0xf575
leaq 0x1d0(%rbx), %r14
movl 0x8(%rsp), %edi
callq 0x4850
movq 0x1c8(%rbx), %rdx
movq 0x1d0(%rbx), %rdi
movq %r14, %rsi
callq 0x4750
movl $0x1, %edi
callq 0x4560
movl $0x1f6, %ebp            # imm = 0x1F6
jmp 0xf575
jmp 0xf713
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0xf72d
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x4470
movq %rbx, %rdi
callq 0x4790
nop