// File: /workspace/github_cpp_makefile_star2_O3/mbourand[P]webserv/srcs/Utils/CGI.cpp
// Function: CGI::process(Response&)
void	CGI::process(Response& response)
{
	std::ifstream		inputFileStream(m_tmpfilename.c_str());
	std::stringstream	convert;
	std::string			content;
	int					status;

	if (!inputFileStream.is_open())
		throw CGI::CGIException("Couldn't read tmp buffer.", 500);
	convert << inputFileStream.rdbuf();
	content = convert.str();
	inputFileStream.close();
	if (content.find("\r\n\r\n") != std::string::npos)
	{
		convert.str("");
		if (content.find("Status: ") != std::string::npos)
		{
			convert << content.substr(8, content.find(' ', 8) - 8);
			convert >> status;
			response.setCode(status);
			if (status >= 400)
			{
				Logger::print("CGI returned status "+convert.str(), NULL, ERROR, NORMAL);
				return;
			}
		}
		while (content.find(": ") < content.find("\r\n\r\n"))
		{
			size_t found = content.find(": ");
			if (content.substr(0, found) != "Status")
			response.addHeader(content.substr(0, found), content.substr(found + 2, (content.find("\r\n")) - found - 2));
			content.erase(0, content.find("\r\n"));
			if (content.find(": ") < content.find("\r\n\r\n"))
				content.erase(0, 2);
		}
		content.erase(0, 4);
		convert.clear();
		convert.str("");
		convert << content.size();
		response.addHeader("Content-Length", convert.str());
		response.addDateHeader();
		response.addHeader("Server", "Webserv");
		response.setBody(content);
	}
	else
	{
		Logger::print("CGI produced invalid headers.", NULL, ERROR, NORMAL);
		response.setCode(500);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movq %rsi, 0x28(%rsp)
movq 0x20(%rdi), %rsi
leaq 0x240(%rsp), %rdi
movl $0x8, %edx
callq 0x7870
leaq 0xb8(%rsp), %rdi
callq 0x7360
leaq 0x68(%rsp), %r15
movq %r15, -0x10(%r15)
movq $0x0, -0x8(%r15)
movb $0x0, (%r15)
leaq 0x2b8(%rsp), %rdi
callq 0x7130
testb %al, %al
je 0x16692
leaq 0xc8(%rsp), %r12
leaq 0x250(%rsp), %rsi
movq %r12, %rdi
callq 0x7100
leaq 0xd0(%rsp), %r14
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x7710
leaq 0x58(%rsp), %rdi
leaq 0x8(%rsp), %rbx
movq %rbx, %rsi
callq 0x7590
movq (%rbx), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x1606c
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x240(%rsp), %rdi
callq 0x70f0
leaq 0x2786e(%rip), %rsi     # 0x3d8ee
leaq 0x58(%rsp), %rdi
movl $0x4, %ecx
xorl %edx, %edx
callq 0x77f0
cmpq $-0x1, %rax
je 0x16563
leaq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
movq %rax, (%rdi)
leaq 0x24cec(%rip), %rdx     # 0x3ad9b
movq %rdx, %rsi
callq 0x80f4
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x7690
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x160e0
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x24ba0(%rip), %rsi     # 0x3ac87
leaq 0x58(%rsp), %rdi
movl $0x8, %ecx
xorl %edx, %edx
callq 0x77f0
cmpq $-0x1, %rax
je 0x161e3
leaq 0x58(%rsp), %rbx
movl $0x8, %edx
movq %rbx, %rdi
movl $0x20, %esi
callq 0x75c0
leaq -0x8(%rax), %rcx
leaq 0x8(%rsp), %rdi
movl $0x8, %edx
movq %rbx, %rsi
callq 0x74d0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r12, %rdi
callq 0x7510
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x1615d
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0xb8(%rsp), %rdi
leaq 0x34(%rsp), %rsi
callq 0x7320
movl 0x34(%rsp), %esi
movq 0x28(%rsp), %rdi
callq 0x37aee
cmpl $0x190, 0x34(%rsp)      # imm = 0x190
jl 0x161e3
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x7710
leaq 0x24af5(%rip), %rcx     # 0x3ac90
leaq 0x8(%rsp), %rdi
movl $0x14, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x7500
leaq 0x88(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x165d1
movq %rdx, 0x78(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x88(%rsp)
jmp 0x165d8
leaq 0x276af(%rip), %rsi     # 0x3d899
leaq 0x58(%rsp), %rbx
movl $0x2, %ecx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x77f0
movq %rax, %r14
leaq 0x276e6(%rip), %rsi     # 0x3d8ee
movl $0x4, %ecx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x77f0
cmpq %rax, %r14
jae 0x163b8
leaq 0x27672(%rip), %r13     # 0x3d899
leaq 0x58(%rsp), %rbp
leaq 0x8(%rsp), %r15
leaq 0x38(%rsp), %r12
movq %r12, %rbx
movl $0x2, %ecx
movq %rbp, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x77f0
movq %rax, %r14
movq %r15, %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x74d0
movq %r15, %rdi
leaq 0x25069(%rip), %rsi     # 0x3b2d1
callq 0x7180
movl %eax, %r12d
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x1628c
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
testl %r12d, %r12d
movq %rbx, %r12
je 0x16322
movq %r15, %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq %r14, %rcx
callq 0x74d0
leaq 0x2(%r14), %r12
movl $0x2, %ecx
movq %rbp, %rdi
leaq 0x27635(%rip), %rsi     # 0x3d8f0
xorl %edx, %edx
callq 0x77f0
subq %r14, %rax
leaq -0x2(%rax), %rcx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x74d0
movq 0x28(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x37020
movq %rbx, %r12
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x16306
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x16322
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
movl $0x2, %ecx
movq %rbp, %rdi
leaq 0x275bf(%rip), %rsi     # 0x3d8f0
xorl %edx, %edx
callq 0x77f0
movq %rbp, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x7120
movl $0x2, %ecx
movq %rbp, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x77f0
movq %rax, %r14
movl $0x4, %ecx
movq %rbp, %rdi
leaq 0x27585(%rip), %rsi     # 0x3d8ee
xorl %edx, %edx
callq 0x77f0
cmpq %rax, %r14
jae 0x16384
movl $0x2, %edx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x7120
movl $0x2, %ecx
movq %rbp, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x77f0
movq %rax, %r14
movl $0x4, %ecx
movq %rbp, %rdi
leaq 0x27546(%rip), %rsi     # 0x3d8ee
xorl %edx, %edx
callq 0x77f0
cmpq %rax, %r14
jb 0x16236
leaq 0x58(%rsp), %rdi
movl $0x4, %edx
xorl %esi, %esi
leaq 0x68(%rsp), %r15
callq 0x7120
leaq 0xd0(%rsp), %rbx
leaq 0xc8(%rsp), %r14
leaq 0xb8(%rsp), %rdi
movq 0xb8(%rsp), %rax
addq -0x18(%rax), %rdi
xorl %esi, %esi
callq 0x77b0
leaq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
movq %rax, (%rdi)
leaq 0x2498e(%rip), %rdx     # 0x3ad9b
movq %rdx, %rsi
callq 0x80f4
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x7690
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x1643e
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x60(%rsp), %rsi
movq %r14, %rdi
callq 0x7330
leaq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
movq %rax, (%rdi)
leaq 0x23ca5(%rip), %rsi     # 0x3a104
leaq 0x23cac(%rip), %rdx     # 0x3a112
callq 0x80f4
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x7710
leaq 0x8(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movq 0x28(%rsp), %rdi
callq 0x37020
leaq 0x48(%rsp), %r14
movq -0x10(%r14), %rdi
cmpq %r14, %rdi
je 0x164a7
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x164c3
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x28(%rsp), %rdi
callq 0x373a0
leaq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
movq %rax, (%rdi)
leaq 0x23db6(%rip), %rsi     # 0x3a297
leaq 0x23db5(%rip), %rdx     # 0x3a29d
callq 0x80f4
leaq 0x38(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x246e0(%rip), %rsi     # 0x3abdc
leaq 0x246e0(%rip), %rdx     # 0x3abe3
callq 0x80f4
leaq 0x8(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movq 0x28(%rsp), %rdi
callq 0x37020
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x16533
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x1654f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x58(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x37b04
jmp 0x1663b
leaq 0xa8(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x2472f(%rip), %rsi     # 0x3aca5
leaq 0x24745(%rip), %rdx     # 0x3acc2
leaq 0x98(%rsp), %rdi
callq 0x80f4
leaq 0x98(%rsp), %rdi
xorl %esi, %esi
movl $0x3, %edx
movl $0x1, %ecx
callq 0xdea4
movq 0x98(%rsp), %rdi
cmpq %r14, %rdi
je 0x165c0
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x28(%rsp), %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0x37aee
jmp 0x1663b
movups (%rcx), %xmm0
movups %xmm0, (%r14)
movq 0x8(%rax), %rdx
leaq 0x78(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
xorl %esi, %esi
movl $0x3, %edx
movl $0x1, %ecx
callq 0xdea4
movq 0x78(%rsp), %rdi
cmpq %r14, %rdi
je 0x1661f
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x1663b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x16652
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x37917(%rip), %rsi     # 0x4df70
leaq 0xb8(%rsp), %rdi
callq 0x73b0
leaq 0x138(%rsp), %rdi
callq 0x71c0
leaq 0x240(%rsp), %rdi
callq 0x7150
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x30, %edi
callq 0x7260
movq %rax, %r14
leaq 0x245c7(%rip), %rsi     # 0x3ac6d
movq %rax, %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x167fc
leaq 0x3707e(%rip), %rsi     # 0x4d738
leaq 0x17f(%rip), %rdx       # 0x16840
movq %r14, %rdi
callq 0x77d0
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r14, %rdi
je 0x16785
movq 0x88(%rsp), %rsi
jmp 0x1677d
jmp 0x16761
jmp 0x167a7
movq %rax, %rbx
movq 0x98(%rsp), %rdi
cmpq %r14, %rdi
je 0x167aa
movq 0xa8(%rsp), %rsi
jmp 0x16799
jmp 0x167a7
jmp 0x16761
jmp 0x167a7
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
jne 0x16778
jmp 0x16785
jmp 0x16761
jmp 0x167a7
movq %rax, %rbx
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
jmp 0x16773
jmp 0x16761
jmp 0x167a7
jmp 0x16761
jmp 0x167a7
movq %rax, %rbx
movq %r14, %rdi
callq 0x7390
jmp 0x167aa
jmp 0x16761
jmp 0x167a7
jmp 0x167a7
movq %rax, %rbx
jmp 0x167e7
jmp 0x167a7
movq %rax, %rbx
jmp 0x16785
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x16785
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x167aa
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x167aa
jmp 0x167a7
jmp 0x167a7
movq %rax, %rbx
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x167c6
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x377a3(%rip), %rsi     # 0x4df70
leaq 0xb8(%rsp), %rdi
callq 0x73b0
leaq 0x138(%rsp), %rdi
callq 0x71c0
leaq 0x240(%rsp), %rdi
callq 0x7150
movq %rbx, %rdi
callq 0x7810