// File: /workspace/github_cpp_makefile_star2_O2/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 %rbx
subq $0x48, %rsp
movq %rdi, %rbx
leaq 0x78(%rdi), %rsi
leaq 0xf4a8(%rip), %rdx      # 0x1c57e
leaq 0x8(%rsp), %rdi
callq 0x60d3
movq (%rbx), %rdi
callq 0x17e90
leaq 0x28(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %rax, %rdx
callq 0x5273
leaq 0x98(%rbx), %r14
leaq 0x28(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x4560
movq %r15, %rdi
callq 0x4898
leaq 0x8(%rsp), %rdi
callq 0x4898
movq %rbx, %rdi
callq 0xd304
movl $0x1f4, %ebp            # imm = 0x1F4
testb %al, %al
je 0xd17c
movq 0x18(%rbx), %rdi
callq 0x1b496
movq %rax, 0x1d0(%rbx)
testq %rax, %rax
je 0xd17c
movq 0x98(%rbx), %rdi
callq 0x1b496
movq %rax, 0x1d8(%rbx)
testq %rax, %rax
je 0xd17c
andq $0x0, 0x1e0(%rbx)
leaq 0x8(%rsp), %rdi
callq 0x45d0
movl $0x1f4, %ebp            # imm = 0x1F4
testl %eax, %eax
je 0xd189
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x40e0
testl %eax, %eax
je 0xd23b
jle 0xd2e5
movl %eax, %r15d
movl 0x8(%rsp), %edi
callq 0x4850
movq 0xe0(%rbx), %rdx
testq %rdx, %rdx
je 0xd1c9
movl 0xc(%rsp), %edi
movq 0xd8(%rbx), %rsi
callq 0x4140
testq %rax, %rax
jle 0xd17c
movl 0xc(%rsp), %edi
callq 0x4850
leaq 0x28(%rsp), %rsi
movl %r15d, %edi
xorl %edx, %edx
callq 0x4370
cmpl $-0x1, %eax
je 0xd17c
movl 0x28(%rsp), %eax
testb $0x7f, %al
sete %cl
testw $0xff00, %ax            # imm = 0xFF00
setne %al
movl $0x1f6, %ebp            # imm = 0x1F6
testb %al, %cl
jne 0xd17c
leaq 0xf8(%rbx), %rsi
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x17a2e
addq $0xb8, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4560
movq %r14, %rdi
callq 0x4898
movl $0xc8, %ebp
jmp 0xd17c
pushq $0x2f
popq %rsi
pushq $-0x1
popq %rdx
movq %r14, %rdi
callq 0x4860
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x44a0
movq (%r15), %rdi
callq 0x4640
movl %eax, %r14d
movq %r15, %rdi
callq 0x4898
cmpl $-0x1, %r14d
je 0xd17c
movl 0xc(%rsp), %edi
callq 0x4850
movl 0x8(%rsp), %edi
xorl %esi, %esi
callq 0x4410
cmpl $-0x1, %eax
je 0xd17c
leaq 0xf8(%rbx), %rdi
callq 0x17e86
movl (%rax), %edi
pushq $0x1
popq %rsi
callq 0x4410
cmpl $-0x1, %eax
je 0xd17c
leaq 0x1d0(%rbx), %r14
movl 0x8(%rsp), %edi
callq 0x4850
movq 0x1c8(%rbx), %rdx
movq 0x1d0(%rbx), %rdi
movq %r14, %rsi
callq 0x4720
pushq $0x1
popq %rdi
callq 0x4550
movl $0x1f6, %ebp            # imm = 0x1F6
jmp 0xd17c
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x4898
movq %rbx, %rdi
callq 0x4750