// File: /workspace/github_cpp_makefile_star2_O1/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 0x24e48(%rip), %rsi     # 0x3a874
leaq 0x24e45(%rip), %rdx     # 0x3a878
movq %rsp, %rdi
callq 0x80f4
movq %rsp, %rsi
movq %r14, %rdi
callq 0x2b4c6
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 0x8a8a
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x15a82
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)
movq 0x90(%rsp), %rdx
testq %rdx, %rdx
jne 0x15b2c
leaq 0x24f7d(%rip), %rcx     # 0x3aa94
leaq 0x88(%rsp), %rdi
movl $0x1, %r8d
xorl %esi, %esi
callq 0x76f0
movq %r15, 0x40(%rsp)
movq %rsp, %rdi
movl $0x2f, %esi
xorl %edx, %edx
callq 0x75c0
cmpq $-0x1, %rax
je 0x15d30
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 0x15bac
movq %rdx, 0x48(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x58(%rsp)
jmp 0x15bb3
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 0x15bf6
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x15c1b
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x74b0
testl %ebx, %ebx
js 0x15d30
movq %r13, %rdi
movl $0x2e, %esi
movq $-0x1, %rdx
callq 0x7900
cmpq $-0x1, %rax
je 0x15c8a
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 0x15c8a
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x74b0
cmpq $0x0, 0x70(%rsp)
je 0x15cd0
movq %r14, %rdi
callq 0x2b494
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
callq 0xf34e
movq %rax, %rbx
movq %r14, %rdi
callq 0x2b494
addq $0x8, %rax
cmpq %rax, %rbx
je 0x15cd0
movl 0xe0(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x8000, %eax           # imm = 0x8000
je 0x15d19
movl 0xe0(%rsp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x15d12
incq %r15
movq %r13, %rdi
movl $0x2f, %esi
movq %r15, %rdx
callq 0x75c0
movq %rax, %r15
cmpq $-0x1, %rax
jne 0x15d06
movq 0x8(%rsp), %r15
cmpq $-0x1, %r15
jne 0x15b5d
jmp 0x15d30
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x15d30
movq %rsp, %rsi
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
movq %r15, %rcx
callq 0x74d0
jmp 0x15d4e
movq 0x40(%rsp), %rbx
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x25058(%rip), %rdx     # 0x3ad9b
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x80f4
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x15d6a
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x15d8b
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x74b0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x15da6
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x15dc2
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 0x15e42
jmp 0x15e42
movq %rax, %rbx
jmp 0x15e82
movq %rax, %rbx
jmp 0x15e9d
movq %rax, %rbx
jmp 0x15eb9
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x15eb9
movq 0x10(%rsp), %rsi
jmp 0x15eb1
jmp 0x15e42
jmp 0x15e42
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x15e45
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x15e45
jmp 0x15e42
movq %rax, %rbx
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x15e61
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x98(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x15e82
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x74b0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x15e9d
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x15eb9
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x74b0
movq %rbx, %rdi
callq 0x7810
nop