// File: /workspace/github_cpp_makefile_star2_O3/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 0x157d1
callq 0x7160
movl %eax, %ebx
testl %eax, %eax
je 0x15658
cmpl $-0x1, %ebx
jne 0x156ce
movl 0xc(%rsp), %edi
callq 0x78f0
movl 0x10(%rsp), %edi
callq 0x78f0
movl $0x30, %edi
callq 0x7260
movq %rax, %r14
leaq 0x255c9(%rip), %rsi     # 0x3ac0f
movq %rax, %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x167fc
jmp 0x15815
movq 0x20(%r15), %rdi
movl $0x41, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x73a0
cmpl $-0x1, %eax
je 0x157c7
movl %eax, %ebp
movl 0x10(%rsp), %edi
callq 0x78f0
movl 0xc(%rsp), %edi
xorl %esi, %esi
callq 0x7460
testl %eax, %eax
js 0x157c7
movl %ebp, %edi
movl $0x1, %esi
callq 0x7460
testl %eax, %eax
js 0x157c7
movq %r15, %rdi
callq 0x15e1c
movq 0x110(%r15), %rsi
movq 0x118(%r15), %rdx
movq (%rsi), %rdi
callq 0x77c0
testl %eax, %eax
js 0x157c7
movl 0xc(%rsp), %edi
callq 0x78f0
movq 0x8(%r14), %rdx
testq %rdx, %rdx
je 0x1574c
movl 0x10(%rsp), %edi
movq (%r14), %rsi
callq 0x71e0
testq %rax, %rax
jg 0x1574c
leaq 0x48(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2551d(%rip), %rsi     # 0x3ac1e
leaq 0x25530(%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 0xdea4
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x15740
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 0x157f4
leaq 0x28(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x254b9(%rip), %rsi     # 0x3ac39
leaq 0x254cf(%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 0xdea4
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x157bc
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 0x2541b(%rip), %rsi     # 0x3ac00
movq %rax, %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x167fc
jmp 0x15815
movl $0x30, %edi
callq 0x7260
movq %rax, %r14
leaq 0x2544f(%rip), %rsi     # 0x3ac57
movq %rax, %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x167fc
leaq 0x37f1c(%rip), %rsi     # 0x4d738
leaq 0x101d(%rip), %rdx      # 0x16840
movq %r14, %rdi
callq 0x77d0
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x1586a
movq 0x48(%rsp), %rsi
jmp 0x15862
jmp 0x15843
jmp 0x15843
movq %rax, %rbx
movq %r14, %rdi
callq 0x7390
jmp 0x1586a
movq %rax, %rbx
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x1586a
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x74b0
movq %rbx, %rdi
callq 0x7810