// File: /workspace/github_cpp_makefile_star2_O1/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 0x6bac
leaq 0x161da(%rip), %rsi     # 0x25594
movq %r14, %rdi
callq 0x4830
movq (%rbx), %rdi
callq 0x1f45c
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 0xf403
movq %rdx, 0x28(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x38(%rsp)
jmp 0xf40b
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 0x4560
movq (%r15), %rdi
cmpq %r12, %rdi
je 0xf44e
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x4460
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0xf465
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x4460
movq %rbx, %rdi
callq 0xf694
movl $0x1f4, %ebp            # imm = 0x1F4
testb %al, %al
je 0xf4c1
movq 0x18(%rbx), %rdi
callq 0x24407
movq %rax, 0x1d0(%rbx)
testq %rax, %rax
je 0xf4c1
movq 0x98(%rbx), %rdi
callq 0x24407
movq %rax, 0x1d8(%rbx)
testq %rax, %rax
je 0xf4c1
movq $0x0, 0x1e0(%rbx)
leaq 0x8(%rsp), %rdi
callq 0x45b0
movl $0x1f4, %ebp            # imm = 0x1F4
testl %eax, %eax
je 0xf4d2
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 0xf551
jle 0xf612
movl %eax, %r15d
movl 0x8(%rsp), %edi
callq 0x4840
movq 0xe0(%rbx), %rdx
testq %rdx, %rdx
je 0xf50e
movl 0xc(%rsp), %edi
movq 0xd8(%rbx), %rsi
callq 0x4150
testq %rax, %rax
jle 0xf4c1
movl 0xc(%rsp), %edi
callq 0x4840
leaq 0x28(%rsp), %rsi
xorl %r14d, %r14d
movl %r15d, %edi
xorl %edx, %edx
callq 0x4360
cmpl $-0x1, %eax
je 0xf61c
movl 0x28(%rsp), %eax
testb $0x7f, %al
setne %cl
testl $0xff00, %eax           # imm = 0xFF00
sete %r14b
orb %cl, %r14b
movl $0x1f6, %ebp            # imm = 0x1F6
jmp 0xf621
movq %r14, %rdi
movl $0x2f, %esi
movq $-0x1, %rdx
callq 0x4850
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4480
movq (%r15), %rdi
callq 0x4610
movl %eax, %r14d
movq (%r15), %rdi
cmpq %r12, %rdi
je 0xf59a
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x4460
cmpl $-0x1, %r14d
je 0xf4c1
movl 0xc(%rsp), %edi
callq 0x4840
movl 0x8(%rsp), %edi
xorl %esi, %esi
callq 0x43f0
cmpl $-0x1, %eax
je 0xf4c1
leaq 0xf8(%rbx), %rdi
callq 0x1f452
movl (%rax), %edi
movl $0x1, %esi
callq 0x43f0
cmpl $-0x1, %eax
je 0xf4c1
leaq 0x1d0(%rbx), %r14
movl 0x8(%rsp), %edi
callq 0x4840
movq 0x1c8(%rbx), %rdx
movq 0x1d0(%rbx), %rdi
movq %r14, %rsi
callq 0x4740
movl $0x1, %edi
callq 0x4550
movl $0x1f6, %ebp            # imm = 0x1F6
jmp 0xf4c1
movl $0x1f4, %ebp            # imm = 0x1F4
testb %r14b, %r14b
je 0xf4c1
leaq 0xf8(%rbx), %rsi
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x1ecda
addq $0xb8, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4560
movq (%r14), %rdi
cmpq %r12, %rdi
je 0xf665
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x4460
movl $0xc8, %ebp
jmp 0xf4c1
jmp 0xf671
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0xf68b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x4460
movq %rbx, %rdi
callq 0x4780
nop