// File: /workspace/github_cpp_makefile_star2_O3/llefranc[P]42_webserv/srcs/server/Cgi.cpp
// Function: CGI::executeCGI()
void CGI::executeCGI()
{
	int fdOut[2];
	int fdIN[2];
	
	if (pipe(fdOut) < 0 || pipe(fdIN) < 0)
		throw StatusLine(500, REASON_500, "pipe failed in executeCGI method");
	
	pid_t pid = fork();
	
	if (!pid){
		
		// stdout is now a copy of fdOut[1] and in case post method, stdin is a copy of fdIn[0]
		dup2(fdOut[1], STDOUT_FILENO);
		close(fdOut[0]);
		close(fdOut[1]);

		dup2(fdIN[0], STDIN_FILENO);
		close(fdIN[0]);
		close(fdIN[1]);

		// change the repo into where the program is
		chdir(_path_info.first.c_str());
	
		if (execve(_args[0], _args, _envvar) < 0){
			exit(EXECVE_FAIL);
		}
	
	}
	else if (pid > 0){
		
		close(fdOut[1]);
		if (_req->getMethod() == POST){
			if (write(fdIN[1], _req->getBody().getBody().c_str(), _req->getBody().getBody().size()) < 0)
				throw StatusLine(500, REASON_500, "write failed in executeCGI method");
		}
		
		else{
			if (write(fdIN[1], _getBuffFile.c_str(), _getBuffFile.size()) < 0)
				throw StatusLine(500, REASON_500, "write failed in executeCGI method");
		}
		
		close(fdIN[1]);
		close(fdIN[0]);
		
        // Checking if execve correctly worked
        int status = 0;
        waitpid(pid, &status, 0);
        if (WIFEXITED(status) && WEXITSTATUS(status) == EXECVE_FAIL)
            throw StatusLine(500, REASON_500, "execve failed in executeCGI method");
        
		char buf[CGI_PIPE_BUFFER_SIZE + 1] = {0};
		std::string msgbody;	
		while (read(fdOut[0], buf, CGI_PIPE_BUFFER_SIZE) > 0)
		{
			msgbody += buf;
			memset(buf, 0, CGI_PIPE_BUFFER_SIZE + 1);
		}
		msgbody += buf;

		close(fdOut[0]);

		_emptyBody->setBuff(msgbody);
		
		// remove the header part of the cgi output
		_emptyBody->setSize(msgbody.size() - (msgbody.find("\r\n\r\n") + 4)); 
	}
	else{
		close(fdOut[1]); close(fdOut[0]);
		if (_req->getMethod() == POST) { close(fdIN[0]); close(fdIN[1]); }
		throw StatusLine(500, REASON_500, "fork failed executeCGI method");
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x840, %rsp            # imm = 0x840
movq %rdi, %rbx
leaq -0x30(%rbp), %rdi
callq 0x4530
testl %eax, %eax
js 0x133f8
leaq -0x28(%rbp), %rdi
callq 0x4530
testl %eax, %eax
js 0x133f8
callq 0x4110
testl %eax, %eax
je 0x1325e
movl %eax, %r14d
movl -0x2c(%rbp), %edi
callq 0x4780
testl %r14d, %r14d
jle 0x13466
movq 0x18(%rbx), %rdi
callq 0xcad2
movl -0x24(%rbp), %r15d
cmpl $0x2, %eax
jne 0x132c2
movq 0x18(%rbx), %rdi
callq 0xcaba
movq 0x18(%rax), %r12
movq 0x18(%rbx), %rdi
callq 0xcaba
movq 0x20(%rax), %rdx
movl %r15d, %edi
movq %r12, %rsi
callq 0x4190
testq %rax, %rax
jns 0x132e1
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq -0x850(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x2a2c(%rip), %rsi      # 0x15c3b
leaq 0x2a46(%rip), %rdx      # 0x15c5c
leaq -0x860(%rbp), %rdi
callq 0x5796
movb $0x1, %r12b
leaq 0x28c1(%rip), %rdx      # 0x15aed
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xe0d4
xorl %r12d, %r12d
leaq 0x9b26(%rip), %rsi      # 0x1cd70
leaq -0x510f(%rip), %rdx     # 0xe142
movq %r14, %rdi
callq 0x46a0
jmp 0x135ce
movl -0x2c(%rbp), %edi
movl $0x1, %esi
callq 0x4390
movl -0x30(%rbp), %edi
callq 0x4780
movl -0x2c(%rbp), %edi
callq 0x4780
movl -0x28(%rbp), %edi
xorl %esi, %esi
callq 0x4390
movl -0x28(%rbp), %edi
callq 0x4780
movl -0x24(%rbp), %edi
callq 0x4780
movq 0x290(%rbx), %rdi
callq 0x4580
movq (%rbx), %rdx
movq 0x8(%rbx), %rsi
movq (%rsi), %rdi
callq 0x4690
testl %eax, %eax
jns 0x133e8
movl $0xff, %edi
callq 0x44c0
movq 0x270(%rbx), %rsi
movq 0x278(%rbx), %rdx
movl %r15d, %edi
callq 0x4190
testq %rax, %rax
js 0x13565
movl -0x24(%rbp), %edi
callq 0x4780
movl -0x28(%rbp), %edi
callq 0x4780
leaq -0x54(%rbp), %r15
movl $0x0, (%r15)
movl %r14d, %edi
movq %r15, %rsi
xorl %edx, %edx
callq 0x4320
movl $0xff7f, %eax           # imm = 0xFF7F
andl (%r15), %eax
cmpl $0xff00, %eax           # imm = 0xFF00
je 0x134fa
leaq -0x860(%rbp), %r14
movl $0x7ff, %edx            # imm = 0x7FF
movq %r14, %rdi
xorl %esi, %esi
callq 0x4250
leaq -0x40(%rbp), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
leaq -0x50(%rbp), %r15
movl -0x30(%rbp), %edi
movl $0x7fe, %edx            # imm = 0x7FE
movq %r14, %rsi
callq 0x4560
testq %rax, %rax
jle 0x1337e
movq %r15, %rdi
movq %r14, %rsi
callq 0x4760
movl $0x7ff, %edx            # imm = 0x7FF
movq %r14, %rdi
xorl %esi, %esi
callq 0x4250
jmp 0x1334d
leaq -0x50(%rbp), %rdi
leaq -0x860(%rbp), %rsi
callq 0x4760
movl -0x30(%rbp), %edi
callq 0x4780
movq 0x10(%rbx), %rdi
addq $0x18, %rdi
leaq -0x50(%rbp), %rsi
callq 0x4240
movq 0x10(%rbx), %r14
leaq -0x50(%rbp), %rbx
movq 0x8(%rbx), %r15
leaq 0x28c6(%rip), %rsi      # 0x15c80
movl $0x4, %ecx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x46c0
subq %rax, %r15
addq $-0x4, %r15
movq %r15, 0x8(%r14)
movq (%rbx), %rdi
cmpq %r12, %rdi
je 0x133e8
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x43e0
addq $0x840, %rsp            # imm = 0x840
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq -0x850(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x2803(%rip), %rsi      # 0x15c1a
leaq 0x281c(%rip), %rdx      # 0x15c3a
leaq -0x860(%rbp), %rdi
callq 0x5796
movb $0x1, %r12b
leaq 0x26b9(%rip), %rdx      # 0x15aed
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xe0d4
xorl %r12d, %r12d
leaq 0x991e(%rip), %rsi      # 0x1cd70
leaq -0x5317(%rip), %rdx     # 0xe142
movq %r14, %rdi
callq 0x46a0
jmp 0x135ce
movl -0x30(%rbp), %edi
callq 0x4780
movq 0x18(%rbx), %rdi
callq 0xcad2
cmpl $0x2, %eax
jne 0x1348c
movl -0x28(%rbp), %edi
callq 0x4780
movl -0x24(%rbp), %edi
callq 0x4780
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq -0x850(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x27da(%rip), %rsi      # 0x15c85
leaq 0x27f0(%rip), %rdx      # 0x15ca2
leaq -0x860(%rbp), %rdi
callq 0x5796
movb $0x1, %r12b
leaq 0x2625(%rip), %rdx      # 0x15aed
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xe0d4
xorl %r12d, %r12d
leaq 0x988a(%rip), %rsi      # 0x1cd70
leaq -0x53ab(%rip), %rdx     # 0xe142
movq %r14, %rdi
callq 0x46a0
jmp 0x135ce
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq -0x850(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x2744(%rip), %rsi      # 0x15c5d
leaq 0x275f(%rip), %rdx      # 0x15c7f
leaq -0x860(%rbp), %rdi
callq 0x5796
movb $0x1, %r12b
leaq 0x25b7(%rip), %rdx      # 0x15aed
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xe0d4
xorl %r12d, %r12d
leaq 0x981c(%rip), %rsi      # 0x1cd70
leaq -0x5419(%rip), %rdx     # 0xe142
movq %r14, %rdi
callq 0x46a0
jmp 0x135ce
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq -0x850(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x26b7(%rip), %rsi      # 0x15c3b
leaq 0x26d1(%rip), %rdx      # 0x15c5c
leaq -0x860(%rbp), %rdi
callq 0x5796
movb $0x1, %r12b
leaq 0x254c(%rip), %rdx      # 0x15aed
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xe0d4
xorl %r12d, %r12d
leaq 0x97b1(%rip), %rsi      # 0x1cd70
leaq -0x5484(%rip), %rdx     # 0xe142
movq %r14, %rdi
callq 0x46a0
jmp 0x135de
jmp 0x135de
jmp 0x13603
jmp 0x13603
jmp 0x135de
jmp 0x13603
jmp 0x135de
jmp 0x13603
movq %rax, %rbx
movq -0x860(%rbp), %rdi
cmpq %r15, %rdi
je 0x135fc
movq -0x850(%rbp), %rsi
incq %rsi
callq 0x43e0
testb %r12b, %r12b
jne 0x13606
jmp 0x1362a
movq %rax, %rbx
movq %r14, %rdi
callq 0x42e0
jmp 0x1362a
jmp 0x13612
movq %rax, %rbx
movq -0x50(%rbp), %rdi
cmpq %r12, %rdi
je 0x1362a
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x43e0
movq %rbx, %rdi
callq 0x46d0