// File: mbourand[P]webserv/srcs/Utils/CGI.cpp
// Function: CGI::execute(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void				CGI::execute(const std::string & body)
{
	int		fd;
	pid_t	pid;
	int		status;
	int		pipes[2];

	if (pipe(pipes) < 0)
		throw CGI::CGIException("pipe() failed.", 500);
	pid = fork();
	switch (pid) {
	case -1:
		close(pipes[0]);
		close(pipes[1]);
		throw CGI::CGIException("fork() failed.", 500);
	case 0:
		if ((fd = open(m_tmpfilename.c_str(), O_WRONLY | O_CREAT, 0666)) == -1)
			exit(1);
		close(pipes[1]); //close writing end query pipe
		if (dup2(pipes[0], STDIN_FILENO) < 0) //dup reading end query pipe to STDIN
			exit(1);
		if (dup2(fd, STDOUT_FILENO) < 0) //dup stdout to tmp file
			exit(1);
		VectorToArray();
		if (execve(m_c_args[0], m_c_args, m_c_env) < 0)
			exit (1);
	default:
		close(pipes[0]); // close reading end query pipe
		if (!body.empty())
			if (write(pipes[1], body.c_str(), body.size()) <= 0) // input writing end query pipe
			{
				Logger::print("Couldn't send Body to CGI.", NULL, ERROR, NORMAL);
				kill(pid, SIGINT);
			}
		close(pipes[1]);
		waitpid(pid, &status, 0);
		if (WIFEXITED(status))
			Logger::print("CGI execution was successful.", NULL, SUCCESS, VERBOSE);
		else
			throw CGI::CGIException("CGI execution failed.", 500);
		break;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x24(%rbp), %rdi
callq 0x98b0
cmpl $0x0, %eax
jge 0x2015d
movl $0x30, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x98(%rbp)
leaq 0x4aabb(%rip), %rsi     # 0x6abd5
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x21920
jmp 0x20126
movq -0x98(%rbp), %rdi
leaq 0x6e60c(%rip), %rsi     # 0x8e740
leaq 0x1885(%rip), %rdx      # 0x219c0
callq 0x9aa0
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x94c0
jmp 0x204b2
callq 0x91e0
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x9c(%rbp)
subl $-0x1, %eax
je 0x20184
jmp 0x20175
movl -0x9c(%rbp), %eax
testl %eax, %eax
je 0x201f5
jmp 0x202a9
movl -0x24(%rbp), %edi
callq 0x9c30
movl -0x20(%rbp), %edi
callq 0x9c30
movl $0x30, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xa8(%rbp)
leaq 0x4aa32(%rip), %rsi     # 0x6abe4
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x21920
jmp 0x201be
movq -0xa8(%rbp), %rdi
leaq 0x6e574(%rip), %rsi     # 0x8e740
leaq 0x17ed(%rip), %rdx      # 0x219c0
callq 0x9aa0
movq -0xa8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x94c0
jmp 0x204b2
movq -0x90(%rbp), %rdi
addq $0x20, %rdi
callq 0x92b0
movq %rax, %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
movb $0x0, %al
callq 0x94d0
movl %eax, -0x14(%rbp)
cmpl $-0x1, %eax
jne 0x2022b
movl $0x1, %edi
callq 0x97d0
movl -0x20(%rbp), %edi
callq 0x9c30
movl -0x24(%rbp), %edi
xorl %esi, %esi
callq 0x9620
cmpl $0x0, %eax
jge 0x2024c
movl $0x1, %edi
callq 0x97d0
movl -0x14(%rbp), %edi
movl $0x1, %esi
callq 0x9620
cmpl $0x0, %eax
jge 0x20268
movl $0x1, %edi
callq 0x97d0
movq -0x90(%rbp), %rdi
callq 0x20b70
movq -0x90(%rbp), %rax
movq 0x110(%rax), %rcx
movq (%rcx), %rdi
movq 0x110(%rax), %rsi
movq 0x118(%rax), %rdx
callq 0x9a90
cmpl $0x0, %eax
jge 0x202a7
movl $0x1, %edi
callq 0x97d0
jmp 0x202a9
movl -0x24(%rbp), %edi
callq 0x9c30
movq -0x10(%rbp), %rdi
callq 0x9a50
testb $0x1, %al
jne 0x20399
movl -0x20(%rbp), %eax
movl %eax, -0xb4(%rbp)
movq -0x10(%rbp), %rdi
callq 0x92b0
movq %rax, -0xb0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9480
movl -0xb4(%rbp), %edi
movq -0xb0(%rbp), %rsi
movq %rax, %rdx
callq 0x9250
cmpq $0x0, %rax
jg 0x20397
leaq -0x59(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x9b10
movq -0xc0(%rbp), %rdx
leaq 0x4a8d2(%rip), %rsi     # 0x6abf3
leaq -0x58(%rbp), %rdi
callq 0xa780
jmp 0x2032c
xorl %eax, %eax
movl %eax, %esi
leaq -0x58(%rbp), %rdi
movl $0x3, %edx
movl $0x1, %ecx
callq 0x138a0
jmp 0x20345
leaq -0x58(%rbp), %rdi
callq 0x9c98
leaq -0x59(%rbp), %rdi
callq 0x9700
movl -0x18(%rbp), %edi
movl $0x2, %esi
callq 0x98e0
jmp 0x20397
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x20389
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x9c98
leaq -0x59(%rbp), %rdi
callq 0x9700
jmp 0x204b2
jmp 0x20399
movl -0x20(%rbp), %edi
callq 0x9c30
movl -0x18(%rbp), %edi
leaq -0x1c(%rbp), %rsi
xorl %edx, %edx
callq 0x9530
movl -0x1c(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
jne 0x20449
leaq -0x81(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x9b10
movq -0xc8(%rbp), %rdx
leaq 0x4a82f(%rip), %rsi     # 0x6ac0e
leaq -0x80(%rbp), %rdi
callq 0xa780
jmp 0x203ea
xorl %ecx, %ecx
movl %ecx, %esi
leaq -0x80(%rbp), %rdi
movl $0x1, %edx
callq 0x138a0
jmp 0x203fe
leaq -0x80(%rbp), %rdi
callq 0x9c98
leaq -0x81(%rbp), %rdi
callq 0x9700
jmp 0x204a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x2043b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x9c98
leaq -0x81(%rbp), %rdi
callq 0x9700
jmp 0x204b2
movl $0x30, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xd0(%rbp)
leaq 0x4a7c5(%rip), %rsi     # 0x6ac2c
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x21920
jmp 0x20473
movq -0xd0(%rbp), %rdi
leaq 0x6e2bf(%rip), %rsi     # 0x8e740
leaq 0x1538(%rip), %rdx      # 0x219c0
callq 0x9aa0
movq -0xd0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x94c0
jmp 0x204b2
jmp 0x204a9
addq $0xd0, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x9af0
nopl (%rax,%rax)