// File: /workspace/github_cpp_makefile_star2_O2/mbourand[P]webserv/srcs/Utils/CGI.cpp
// Function: CGI::find_first_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, ConfigContext const&)
std::string		CGI::find_first_file(const std::string &, const ConfigContext& config)
{
	struct stat	file_stats;
	size_t		start = 0;
	size_t		end = 0;

	std::string config_root = config.getParam("root").front();
	std::string url_after_root = m_realPath.substr(m_realPath.rfind(config_root) + config_root.size());
	std::string url_until_root = m_realPath.substr(0, m_realPath.rfind(config_root) + config_root.size());
	std::string extension;
	if (url_until_root.empty())
		url_until_root = "/";

	end = url_after_root.find('/', end);

	while (end != std::string::npos)
	{
		if (lstat((url_until_root + url_after_root.substr(start, end)).c_str(), &file_stats) < 0)
			break;
		if (url_after_root.rfind('.') != std::string::npos)
			extension = url_after_root.substr(url_after_root.find('.'));
		if ((!extension.empty() && config.getCGIExtensions().find(extension) != config.getCGIExtensions().end() && S_ISREG(file_stats.st_mode)) || S_ISREG(file_stats.st_mode))
			return (url_after_root.substr(start, end));
		else if (S_ISDIR(file_stats.st_mode))
		{
			if ((end = url_after_root.find('/', end + 1)) == std::string::npos)
				end = url_after_root.length();
		}
		else
			break;
	}
	return ("");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rcx, %r14
movq %rsi, %rbx
movq %rdi, (%rsp)
leaq 0x1b32c(%rip), %rsi     # 0x2c828
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x8032
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x2167a
movq (%rax), %rsi
addq $0x10, %rsi
leaq 0x68(%rsp), %rdi
callq 0x7690
leaq 0x8(%rsp), %rdi
callq 0x79b8
addq $0xc0, %rbx
leaq 0x68(%rsp), %r15
pushq $-0x1
popq %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x7400
addq 0x8(%r15), %rax
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0x7540
leaq 0x68(%rsp), %r15
pushq $-0x1
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x7400
addq 0x8(%r15), %rax
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x7540
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpq $0x0, 0x30(%rsp)
jne 0x115bb
leaq 0x1b497(%rip), %rsi     # 0x2ca48
leaq 0x28(%rsp), %rdi
callq 0x7930
leaq 0x8(%rsp), %r12
pushq $0x2f
popq %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x7650
movq %rax, %r15
leaq 0xa8(%rsp), %r13
leaq 0x88(%rsp), %rbx
cmpq $-0x1, %r15
je 0x11726
movq %r13, %rdi
movq %r12, %rsi
xorl %edx, %edx
movq %r15, %rcx
callq 0x7540
movq %rbx, %rdi
leaq 0x28(%rsp), %rsi
movq %r13, %rdx
callq 0x12004
movq 0x88(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x7940
movl %eax, %ebp
movq %rbx, %rdi
callq 0x79b8
movq %r13, %rdi
callq 0x79b8
testl %ebp, %ebp
js 0x11726
movq %r12, %rdi
pushq $0x2e
popq %rsi
pushq $-0x1
popq %rdx
callq 0x7970
cmpq $-0x1, %rax
je 0x11680
movq %r12, %rdi
pushq $0x2e
popq %rsi
xorl %edx, %edx
callq 0x7650
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
pushq $-0x1
popq %rcx
callq 0x7540
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x75f0
movq %rbx, %rdi
callq 0x79b8
cmpq $0x0, 0x50(%rsp)
je 0x116d0
movq %r14, %rdi
callq 0x21648
movq %rax, %rdi
leaq 0x48(%rsp), %rsi
callq 0xcdc8
movq %rax, %rbp
movq %r14, %rdi
callq 0x21648
addq $0x8, %rax
cmpq %rax, %rbp
setne %cl
movl 0xe0(%rsp), %eax
movl %eax, %edx
andl $0xf000, %edx           # imm = 0xF000
cmpl $0x8000, %edx           # imm = 0x8000
sete %dl
testb %dl, %cl
je 0x116d7
jmp 0x11711
movl 0xe0(%rsp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpw $0x4000, %ax            # imm = 0x4000
jne 0x1170a
incq %r15
movq %r12, %rdi
pushq $0x2f
popq %rsi
movq %r15, %rdx
callq 0x7650
movq %rax, %r15
cmpq $-0x1, %rax
jne 0x115e0
movq 0x10(%rsp), %r15
jmp 0x115e0
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x11726
leaq 0x8(%rsp), %rsi
movq (%rsp), %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x7540
jmp 0x1173e
leaq 0x1b622(%rip), %rsi     # 0x2cd4f
leaq 0x88(%rsp), %rdx
movq (%rsp), %rdi
callq 0x8032
leaq 0x48(%rsp), %rdi
callq 0x79b8
leaq 0x28(%rsp), %rdi
callq 0x79b8
leaq 0x8(%rsp), %rdi
callq 0x79b8
leaq 0x68(%rsp), %rdi
callq 0x79b8
movq (%rsp), %rax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x117b1
jmp 0x117b1
movq %rax, %rbx
jmp 0x117c8
movq %rax, %rbx
jmp 0x117d2
movq %rax, %rbx
jmp 0x117dc
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
jmp 0x117d7
jmp 0x117b1
jmp 0x117b1
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x79b8
jmp 0x117b4
jmp 0x117b1
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x79b8
leaq 0x28(%rsp), %rdi
callq 0x79b8
leaq 0x8(%rsp), %rdi
callq 0x79b8
leaq 0x68(%rsp), %rdi
callq 0x79b8
movq %rbx, %rdi
callq 0x7860