// File: /workspace/github_cpp_makefile_star2_O1/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 0x1674e
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 0x16128
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x240(%rsp), %rdi
callq 0x70f0
leaq 0x277b2(%rip), %rsi     # 0x3d8ee
leaq 0x58(%rsp), %rdi
movl $0x4, %ecx
xorl %edx, %edx
callq 0x77f0
cmpq $-0x1, %rax
je 0x1661f
leaq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
movq %rax, (%rdi)
leaq 0x24c30(%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 0x1619c
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x24ae4(%rip), %rsi     # 0x3ac87
leaq 0x58(%rsp), %rdi
movl $0x8, %ecx
xorl %edx, %edx
callq 0x77f0
cmpq $-0x1, %rax
je 0x1629f
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 0x16219
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 0x37f1c
cmpl $0x190, 0x34(%rsp)      # imm = 0x190
jl 0x1629f
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x7710
leaq 0x24a39(%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 0x1668d
movq %rdx, 0x78(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x88(%rsp)
jmp 0x16694
leaq 0x275f3(%rip), %rsi     # 0x3d899
leaq 0x58(%rsp), %rbx
movl $0x2, %ecx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x77f0
movq %rax, %r14
leaq 0x2762a(%rip), %rsi     # 0x3d8ee
movl $0x4, %ecx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x77f0
cmpq %rax, %r14
jae 0x16474
leaq 0x275b6(%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 0x24fad(%rip), %rsi     # 0x3b2d1
callq 0x7180
movl %eax, %r12d
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x16348
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
testl %r12d, %r12d
movq %rbx, %r12
je 0x163de
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 0x27579(%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 0x3744c
movq %rbx, %r12
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x163c2
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x163de
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
movl $0x2, %ecx
movq %rbp, %rdi
leaq 0x27503(%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 0x274c9(%rip), %rsi     # 0x3d8ee
xorl %edx, %edx
callq 0x77f0
cmpq %rax, %r14
jae 0x16440
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 0x2748a(%rip), %rsi     # 0x3d8ee
xorl %edx, %edx
callq 0x77f0
cmpq %rax, %r14
jb 0x162f2
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 0x248d2(%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 0x164fa
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 0x23be9(%rip), %rsi     # 0x3a104
leaq 0x23bf0(%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 0x3744c
leaq 0x48(%rsp), %r14
movq -0x10(%r14), %rdi
cmpq %r14, %rdi
je 0x16563
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x1657f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x28(%rsp), %rdi
callq 0x377cc
leaq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
movq %rax, (%rdi)
leaq 0x23cfa(%rip), %rsi     # 0x3a297
leaq 0x23cf9(%rip), %rdx     # 0x3a29d
callq 0x80f4
leaq 0x38(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x24624(%rip), %rsi     # 0x3abdc
leaq 0x24624(%rip), %rdx     # 0x3abe3
callq 0x80f4
leaq 0x8(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movq 0x28(%rsp), %rdi
callq 0x3744c
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x165ef
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x1660b
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x58(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x37f32
jmp 0x166f7
leaq 0xa8(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x24673(%rip), %rsi     # 0x3aca5
leaq 0x24689(%rip), %rdx     # 0x3acc2
leaq 0x98(%rsp), %rdi
callq 0x80f4
leaq 0x98(%rsp), %rdi
xorl %esi, %esi
movl $0x3, %edx
movl $0x1, %ecx
callq 0xded8
movq 0x98(%rsp), %rdi
cmpq %r14, %rdi
je 0x1667c
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x28(%rsp), %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0x37f1c
jmp 0x166f7
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 0xded8
movq 0x78(%rsp), %rdi
cmpq %r14, %rdi
je 0x166db
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x166f7
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x1670e
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x3785b(%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 0x2450b(%rip), %rsi     # 0x3ac6d
movq %rax, %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x168b8
leaq 0x36fc2(%rip), %rsi     # 0x4d738
leaq 0x17f(%rip), %rdx       # 0x168fc
movq %r14, %rdi
callq 0x77d0
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r14, %rdi
je 0x16841
movq 0x88(%rsp), %rsi
jmp 0x16839
jmp 0x1681d
jmp 0x16863
movq %rax, %rbx
movq 0x98(%rsp), %rdi
cmpq %r14, %rdi
je 0x16866
movq 0xa8(%rsp), %rsi
jmp 0x16855
jmp 0x16863
jmp 0x1681d
jmp 0x16863
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
jne 0x16834
jmp 0x16841
jmp 0x1681d
jmp 0x16863
movq %rax, %rbx
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
jmp 0x1682f
jmp 0x1681d
jmp 0x16863
jmp 0x1681d
jmp 0x16863
movq %rax, %rbx
movq %r14, %rdi
callq 0x7390
jmp 0x16866
jmp 0x1681d
jmp 0x16863
jmp 0x16863
movq %rax, %rbx
jmp 0x168a3
jmp 0x16863
movq %rax, %rbx
jmp 0x16841
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x16841
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x16866
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x16866
jmp 0x16863
jmp 0x16863
movq %rax, %rbx
movq 0x58(%rsp), %rdi
leaq 0x68(%rsp), %rax
cmpq %rax, %rdi
je 0x16882
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x376e7(%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