// File: /workspace/github_cpp_makefile_star2_O1/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 0x134bd
leaq -0x28(%rbp), %rdi
callq 0x4530
testl %eax, %eax
js 0x134bd
callq 0x4110
testl %eax, %eax
je 0x13323
movl %eax, %r14d
movl -0x2c(%rbp), %edi
callq 0x4780
testl %r14d, %r14d
jle 0x1352c
movq 0x18(%rbx), %rdi
callq 0xca92
movl -0x24(%rbp), %r15d
cmpl $0x2, %eax
jne 0x13387
movq 0x18(%rbx), %rdi
callq 0xca7a
movq 0x18(%rax), %r12
movq 0x18(%rbx), %rdi
callq 0xca7a
movq 0x20(%rax), %rdx
movl %r15d, %edi
movq %r12, %rsi
callq 0x4190
testq %rax, %rax
jns 0x133a6
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq -0x850(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x2967(%rip), %rsi      # 0x15c3b
leaq 0x2981(%rip), %rdx      # 0x15c5c
leaq -0x860(%rbp), %rdi
callq 0x5790
movb $0x1, %r15b
leaq 0x27fc(%rip), %rdx      # 0x15aed
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xe106
xorl %r15d, %r15d
leaq 0x9a61(%rip), %rsi      # 0x1cd70
leaq -0x51a2(%rip), %rdx     # 0xe174
movq %r14, %rdi
callq 0x46a0
jmp 0x13697
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 0x134ad
movl $0xff, %edi
callq 0x44c0
movq 0x270(%rbx), %rsi
movq 0x278(%rbx), %rdx
movl %r15d, %edi
callq 0x4190
testq %rax, %rax
js 0x1362d
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 0x135c1
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 0x13443
movq %r15, %rdi
movq %r14, %rsi
callq 0x4760
movl $0x7ff, %edx            # imm = 0x7FF
movq %r14, %rdi
xorl %esi, %esi
callq 0x4250
jmp 0x13412
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 0x2801(%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 0x134ad
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), %r12
movq %r12, -0x10(%r12)
leaq 0x273d(%rip), %rsi      # 0x15c1a
leaq 0x2756(%rip), %rdx      # 0x15c3a
leaq -0x860(%rbp), %rdi
callq 0x5790
movb $0x1, %r15b
leaq 0x25f3(%rip), %rdx      # 0x15aed
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xe106
xorl %r15d, %r15d
leaq 0x9858(%rip), %rsi      # 0x1cd70
leaq -0x53ab(%rip), %rdx     # 0xe174
movq %r14, %rdi
callq 0x46a0
jmp 0x13697
movl -0x30(%rbp), %edi
callq 0x4780
movq 0x18(%rbx), %rdi
callq 0xca92
cmpl $0x2, %eax
jne 0x13552
movl -0x28(%rbp), %edi
callq 0x4780
movl -0x24(%rbp), %edi
callq 0x4780
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq -0x850(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x2713(%rip), %rsi      # 0x15c85
leaq 0x2729(%rip), %rdx      # 0x15ca2
leaq -0x860(%rbp), %rdi
callq 0x5790
movb $0x1, %r15b
leaq 0x255e(%rip), %rdx      # 0x15aed
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xe106
xorl %r15d, %r15d
leaq 0x97c3(%rip), %rsi      # 0x1cd70
leaq -0x5440(%rip), %rdx     # 0xe174
movq %r14, %rdi
callq 0x46a0
jmp 0x13697
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq -0x850(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x267c(%rip), %rsi      # 0x15c5d
leaq 0x2697(%rip), %rdx      # 0x15c7f
leaq -0x860(%rbp), %rdi
callq 0x5790
movb $0x1, %r15b
leaq 0x24ef(%rip), %rdx      # 0x15aed
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xe106
xorl %r15d, %r15d
leaq 0x9754(%rip), %rsi      # 0x1cd70
leaq -0x54af(%rip), %rdx     # 0xe174
movq %r14, %rdi
callq 0x46a0
jmp 0x13697
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq -0x850(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x25ee(%rip), %rsi      # 0x15c3b
leaq 0x2608(%rip), %rdx      # 0x15c5c
leaq -0x860(%rbp), %rdi
callq 0x5790
movb $0x1, %r15b
leaq 0x2483(%rip), %rdx      # 0x15aed
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xe106
xorl %r15d, %r15d
leaq 0x96e8(%rip), %rsi      # 0x1cd70
leaq -0x551b(%rip), %rdx     # 0xe174
movq %r14, %rdi
callq 0x46a0
jmp 0x136a7
jmp 0x136a7
jmp 0x136c7
jmp 0x136c7
jmp 0x136a7
jmp 0x136c7
jmp 0x136a7
jmp 0x136c7
movq %rax, %rbx
movq -0x860(%rbp), %rdi
cmpq %r12, %rdi
je 0x136cd
movq -0x850(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0x136cd
movq %rax, %rbx
movb $0x1, %r15b
testb %r15b, %r15b
je 0x136f6
movq %r14, %rdi
callq 0x42e0
jmp 0x136f6
jmp 0x136de
movq %rax, %rbx
movq -0x50(%rbp), %rdi
cmpq %r12, %rdi
je 0x136f6
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x43e0
movq %rbx, %rdi
callq 0x46d0