// File: 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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rsi
movq %rsi, -0xc0(%rbp)
addq $0x78, %rsi
leaq 0x2447e(%rip), %rdx     # 0x435a3
leaq -0x50(%rbp), %rdi
callq 0x8f70
movq -0xc0(%rbp), %rax
movq (%rax), %rdi
callq 0x38460
movq %rax, -0xb8(%rbp)
jmp 0x1f146
movq -0xb8(%rbp), %rdx
leaq -0x30(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x6a30
jmp 0x1f15c
movq -0xc0(%rbp), %rdi
addq $0x98, %rdi
leaq -0x30(%rbp), %rsi
callq 0x56d0
leaq -0x30(%rbp), %rdi
callq 0x5aa8
leaq -0x50(%rbp), %rdi
callq 0x5aa8
movq -0xc0(%rbp), %rdi
callq 0x1f4c0
testb $0x1, %al
jne 0x1f1bb
movl $0x1f4, -0x4(%rbp)      # imm = 0x1F4
jmp 0x1f4a8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x5aa8
jmp 0x1f4b4
movq -0xc0(%rbp), %rdi
addq $0x18, %rdi
callq 0x51f0
movq %rax, %rdi
callq 0x416d0
movq -0xc0(%rbp), %rcx
movq %rax, 0x1d0(%rcx)
cmpq $0x0, %rax
jne 0x1f1f3
movl $0x1f4, -0x4(%rbp)      # imm = 0x1F4
jmp 0x1f4a8
movq -0xc0(%rbp), %rdi
addq $0x98, %rdi
callq 0x51f0
movq %rax, %rdi
callq 0x416d0
movq -0xc0(%rbp), %rcx
movq %rax, 0x1d8(%rcx)
cmpq $0x0, %rax
jne 0x1f22e
movl $0x1f4, -0x4(%rbp)      # imm = 0x1F4
jmp 0x1f4a8
movq -0xc0(%rbp), %rax
movq $0x0, 0x1e0(%rax)
leaq -0x64(%rbp), %rdi
callq 0x5790
cmpl $0x0, %eax
je 0x1f25a
movl $0x1f4, -0x4(%rbp)      # imm = 0x1F4
jmp 0x1f4a8
callq 0x5120
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x68(%rbp)
jne 0x1f37b
movq -0xc0(%rbp), %rdi
movq %rdi, %rax
addq $0x98, %rax
movq %rax, -0xd0(%rbp)
addq $0x98, %rdi
movl $0x2f, %esi
movq $-0x1, %rdx
callq 0x5a30
movq -0xd0(%rbp), %rsi
movq %rax, %rcx
leaq -0x88(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
callq 0x55a0
leaq -0x88(%rbp), %rdi
callq 0x51f0
movq %rax, %rdi
callq 0x5810
movl %eax, -0xc4(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x5aa8
movl -0xc4(%rbp), %eax
cmpl $-0x1, %eax
jne 0x1f2f2
movl $0x1f4, -0x4(%rbp)      # imm = 0x1F4
jmp 0x1f4a8
movl -0x60(%rbp), %edi
callq 0x5a50
movl -0x64(%rbp), %edi
xorl %esi, %esi
callq 0x54e0
cmpl $-0x1, %eax
jne 0x1f315
movl $0x1f4, -0x4(%rbp)      # imm = 0x1F4
jmp 0x1f4a8
movq -0xc0(%rbp), %rdi
addq $0xf8, %rdi
callq 0x38430
movl (%rax), %edi
movl $0x1, %esi
callq 0x54e0
cmpl $-0x1, %eax
jne 0x1f345
movl $0x1f4, -0x4(%rbp)      # imm = 0x1F4
jmp 0x1f4a8
movl -0x64(%rbp), %edi
callq 0x5a50
movq -0xc0(%rbp), %rax
movq 0x1d0(%rax), %rdi
movq %rax, %rsi
addq $0x1d0, %rsi            # imm = 0x1D0
movq 0x1c8(%rax), %rdx
callq 0x5910
movl $0x1, %edi
callq 0x56c0
cmpl $0x0, -0x68(%rbp)
jle 0x1f456
movl -0x64(%rbp), %edi
callq 0x5a50
movq -0xc0(%rbp), %rdi
addq $0xd8, %rdi
callq 0x5830
cmpq $0x0, %rax
je 0x1f403
movq -0xc0(%rbp), %rdi
movl -0x60(%rbp), %eax
movl %eax, -0xdc(%rbp)
addq $0xd8, %rdi
callq 0x51f0
movq -0xc0(%rbp), %rdi
movq %rax, -0xd8(%rbp)
addq $0xd8, %rdi
callq 0x5830
movl -0xdc(%rbp), %edi
movq -0xd8(%rbp), %rsi
movq %rax, %rdx
callq 0x5190
cmpq $0x0, %rax
jg 0x1f403
movl $0x1f4, -0x4(%rbp)      # imm = 0x1F4
jmp 0x1f4a8
movl -0x60(%rbp), %edi
callq 0x5a50
movl -0x68(%rbp), %edi
leaq -0x8c(%rbp), %rsi
xorl %edx, %edx
callq 0x5410
cmpl $-0x1, %eax
jne 0x1f42a
movl $0x1f4, -0x4(%rbp)      # imm = 0x1F4
jmp 0x1f4a8
movl -0x8c(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
jne 0x1f454
movl -0x8c(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
cmpl $0x0, %eax
je 0x1f454
movl $0x1f6, -0x4(%rbp)      # imm = 0x1F6
jmp 0x1f4a8
jmp 0x1f45f
movl $0x1f6, -0x4(%rbp)      # imm = 0x1F6
jmp 0x1f4a8
jmp 0x1f461
movq -0xc0(%rbp), %rsi
addq $0xf8, %rsi
leaq -0xb0(%rbp), %rdi
callq 0x37c50
movq -0xc0(%rbp), %rdi
addq $0xb8, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x56d0
leaq -0xb0(%rbp), %rdi
callq 0x5aa8
movl $0xc8, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x5950
nopl (%rax)