// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x24ef6(%rip), %rsi     # 0x3a874
leaq 0x24ef3(%rip), %rdx     # 0x3a878
movq %rsp, %rdi
callq 0x80f4
movq %rsp, %rsi
movq %r14, %rdi
callq 0x2b17e
movq (%rax), %rax
leaq 0x30(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq 0x10(%rax), %rsi
movq 0x18(%rax), %rdx
addq %rsi, %rdx
leaq 0x20(%rsp), %rdi
callq 0x8a92
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x159d4
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x74b0
addq $0xc0, %rbx
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rcx
movq %rbx, %rdi
movq $-0x1, %rdx
callq 0x7600
addq 0x28(%rsp), %rax
movq %rsp, %rdi
movq %rbx, %rsi
movq %rax, %rdx
movq $-0x1, %rcx
callq 0x74d0
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rcx
movq %rbx, %rdi
movq $-0x1, %rdx
callq 0x7600
addq 0x28(%rsp), %rax
leaq 0x88(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x74d0
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpq $0x0, 0x90(%rsp)
jne 0x15a7e
leaq 0x2502d(%rip), %rcx     # 0x3aa94
leaq 0x88(%rsp), %rdi
movl $0x1, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x76f0
movq %r15, 0x40(%rsp)
movq %rsp, %rdi
movl $0x2f, %esi
xorl %edx, %edx
callq 0x75c0
cmpq $-0x1, %rax
je 0x15c8a
movq %rax, %r15
leaq 0x58(%rsp), %rbp
leaq 0xa8(%rsp), %r12
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
movq %r15, %rcx
callq 0x74d0
movq 0x88(%rsp), %rcx
movq 0x90(%rsp), %r8
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x7500
movq %rbp, 0x48(%rsp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x15afe
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
jmp 0x15b05
movups (%rcx), %xmm0
movups %xmm0, (%rbp)
movq 0x8(%rax), %rdx
movq %rdx, 0x50(%rsp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq 0x48(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x78e0
movl %eax, %ebx
movq 0x48(%rsp), %rdi
cmpq %rbp, %rdi
je 0x15b48
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x15b6d
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x74b0
testl %ebx, %ebx
js 0x15c8a
movq %r13, %rdi
movl $0x2e, %esi
movq $-0x1, %rdx
callq 0x7900
cmpq $-0x1, %rax
je 0x15bdc
movq %r13, %rdi
movl $0x2e, %esi
xorl %edx, %edx
callq 0x75c0
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
movq $-0x1, %rcx
callq 0x74d0
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
callq 0x7590
movq 0x48(%rsp), %rdi
cmpq %rbp, %rdi
je 0x15bdc
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x74b0
cmpq $0x0, 0x70(%rsp)
je 0x15c2c
movq %r14, %rdi
callq 0x2b14c
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
callq 0xf300
movq %rax, %rbx
movq %r14, %rdi
callq 0x2b14c
addq $0x8, %rax
cmpq %rax, %rbx
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 0x15c33
jmp 0x15c73
movl 0xe0(%rsp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x15c6c
incq %r15
movq %r13, %rdi
movl $0x2f, %esi
movq %r15, %rdx
callq 0x75c0
movq %rax, %r15
cmpq $-0x1, %rax
jne 0x15c60
movq 0x8(%rsp), %r15
cmpq $-0x1, %r15
jne 0x15aaf
jmp 0x15c8a
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x15c8a
movq %rsp, %rsi
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x74d0
jmp 0x15ca8
movq 0x40(%rsp), %rbx
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x250fe(%rip), %rdx     # 0x3ad9b
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x80f4
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x15cc4
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x15ce5
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x74b0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x15d00
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x15d1c
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x74b0
movq %rbx, %rax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x15d9c
jmp 0x15d9c
movq %rax, %rbx
jmp 0x15ddc
movq %rax, %rbx
jmp 0x15df7
movq %rax, %rbx
jmp 0x15e13
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x15e13
movq 0x10(%rsp), %rsi
jmp 0x15e0b
jmp 0x15d9c
jmp 0x15d9c
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x15d9f
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x15d9f
jmp 0x15d9c
movq %rax, %rbx
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x15dbb
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x15ddc
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x74b0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x15df7
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x15e13
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x74b0
movq %rbx, %rdi
callq 0x7810
nop