// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
movq %rdi, %r15
leaq 0xc(%rsp), %rdi
callq 0x7680
testl %eax, %eax
js 0x113d3
callq 0x7180
movl %eax, %ebx
testl %eax, %eax
je 0x1129e
cmpl $-0x1, %ebx
jne 0x11310
movl 0xc(%rsp), %edi
callq 0x7960
movl 0x10(%rsp), %edi
callq 0x7960
pushq $0x30
popq %rdi
callq 0x7290
movq %rax, %r14
leaq 0x1b937(%rip), %rsi     # 0x2cbc3
movq %rax, %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x11ef6
jmp 0x11413
movq 0x20(%r15), %rdi
pushq $0x41
popq %rsi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x73e0
cmpl $-0x1, %eax
je 0x113cb
movl %eax, %ebp
movl 0x10(%rsp), %edi
callq 0x7960
movl 0xc(%rsp), %edi
xorl %esi, %esi
callq 0x74e0
testl %eax, %eax
js 0x113cb
pushq $0x1
popq %rsi
movl %ebp, %edi
callq 0x74e0
testl %eax, %eax
js 0x113cb
movq %r15, %rdi
callq 0x117e4
movq 0x110(%r15), %rsi
movq 0x118(%r15), %rdx
movq (%rsi), %rdi
callq 0x7820
testl %eax, %eax
js 0x113cb
movl 0xc(%rsp), %edi
callq 0x7960
movq 0x8(%r14), %rdx
testq %rdx, %rdx
je 0x1136f
movl 0x10(%rsp), %edi
movq (%r14), %rsi
callq 0x71e0
testq %rax, %rax
jg 0x1136f
leaq 0x1b898(%rip), %rsi     # 0x2cbd2
leaq 0x38(%rsp), %rdi
leaq 0xb(%rsp), %rdx
callq 0x8032
leaq 0x38(%rsp), %rdi
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %esi, %esi
callq 0xbe10
leaq 0x38(%rsp), %rdi
callq 0x79b8
pushq $0x2
popq %rsi
movl %ebx, %edi
callq 0x76b0
movl 0x10(%rsp), %edi
callq 0x7960
leaq 0x14(%rsp), %r14
movl %ebx, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x7430
testb $0x7f, (%r14)
jne 0x113f4
leaq 0x1b857(%rip), %rsi     # 0x2cbed
leaq 0x18(%rsp), %rdi
leaq 0xb(%rsp), %rdx
callq 0x8032
leaq 0x18(%rsp), %rdi
pushq $0x1
popq %rdx
xorl %esi, %esi
xorl %ecx, %ecx
callq 0xbe10
leaq 0x18(%rsp), %rdi
callq 0x79b8
addq $0x58, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rdi
callq 0x75e0
pushq $0x30
popq %rdi
callq 0x7290
movq %rax, %r14
leaq 0x1b7cf(%rip), %rsi     # 0x2cbb4
movq %rax, %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x11ef6
jmp 0x11413
pushq $0x30
popq %rdi
callq 0x7290
movq %rax, %r14
leaq 0x1b805(%rip), %rsi     # 0x2cc0b
movq %rax, %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x11ef6
leaq 0x31336(%rip), %rsi     # 0x42750
leaq 0xb19(%rip), %rdx       # 0x11f3a
movq %r14, %rdi
callq 0x7830
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
jmp 0x1144e
jmp 0x11455
jmp 0x11439
jmp 0x11439
movq %rax, %rbx
movq %r14, %rdi
callq 0x73d0
jmp 0x11458
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x79b8
jmp 0x11458
movq %rax, %rbx
movq %rbx, %rdi
callq 0x7860