// File: /workspace/github_cpp_makefile_star2_O1/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 0x75f0
testl %eax, %eax
js 0x1587f
callq 0x7160
movl %eax, %ebx
testl %eax, %eax
je 0x15706
cmpl $-0x1, %ebx
jne 0x1577c
movl 0xc(%rsp), %edi
callq 0x78f0
movl 0x10(%rsp), %edi
callq 0x78f0
movl $0x30, %edi
callq 0x7260
movq %rax, %r14
leaq 0x2551b(%rip), %rsi     # 0x3ac0f
movq %rax, %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x168b8
jmp 0x158c3
movq 0x20(%r15), %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x73a0
cmpl $-0x1, %eax
je 0x15875
movl %eax, %ebp
movl 0x10(%rsp), %edi
callq 0x78f0
movl 0xc(%rsp), %edi
xorl %esi, %esi
callq 0x7460
testl %eax, %eax
js 0x15875
movl %ebp, %edi
movl $0x1, %esi
callq 0x7460
testl %eax, %eax
js 0x15875
movq %r15, %rdi
callq 0x15ec2
movq 0x110(%r15), %rsi
movq 0x118(%r15), %rdx
movq (%rsi), %rdi
callq 0x77c0
testl %eax, %eax
js 0x15875
movl 0xc(%rsp), %edi
callq 0x78f0
movq 0x8(%r14), %rdx
testq %rdx, %rdx
je 0x157fa
movl 0x10(%rsp), %edi
movq (%r14), %rsi
callq 0x71e0
testq %rax, %rax
jg 0x157fa
leaq 0x48(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2546f(%rip), %rsi     # 0x3ac1e
leaq 0x25482(%rip), %rdx     # 0x3ac38
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x80f4
movq %r14, %rdi
xorl %esi, %esi
movl $0x3, %edx
movl $0x1, %ecx
callq 0xded8
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x157ee
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x74b0
movl %ebx, %edi
movl $0x2, %esi
callq 0x7620
movl 0x10(%rsp), %edi
callq 0x78f0
leaq 0x14(%rsp), %r14
movl %ebx, %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x73e0
testb $0x7f, (%r14)
jne 0x158a2
leaq 0x28(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2540b(%rip), %rsi     # 0x3ac39
leaq 0x25421(%rip), %rdx     # 0x3ac56
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x80f4
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0xded8
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x1586a
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x74b0
addq $0x58, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
callq 0x7580
movl $0x30, %edi
callq 0x7260
movq %rax, %r14
leaq 0x2536d(%rip), %rsi     # 0x3ac00
movq %rax, %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x168b8
jmp 0x158c3
movl $0x30, %edi
callq 0x7260
movq %rax, %r14
leaq 0x253a1(%rip), %rsi     # 0x3ac57
movq %rax, %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x168b8
leaq 0x37e6e(%rip), %rsi     # 0x4d738
leaq 0x102b(%rip), %rdx      # 0x168fc
movq %r14, %rdi
callq 0x77d0
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x15918
movq 0x48(%rsp), %rsi
jmp 0x15910
jmp 0x158f1
jmp 0x158f1
movq %rax, %rbx
movq %r14, %rdi
callq 0x7390
jmp 0x15918
movq %rax, %rbx
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x15918
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x74b0
movq %rbx, %rdi
callq 0x7810