// File: llefranc[P]42_webserv/srcs/utils/utils.cpp
// Function: getCgiExecutableName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, Location const*)
std::string* getCgiExecutableName(const std::string& uri, const Location* loc)
{
	size_t dotPos = isExtension(uri);

	// Target ends with a '/', no extension so no cgi
	if (dotPos == std::string::npos)
		return 0;

	// Case target is a script (ends by .cgi)
	if (!uri.compare(dotPos, std::string::npos, ".cgi"))
		return new std::string(".cgi");
	
	// Case target match an extension in the appropriate location block,
	// we return the executable name
	std::map<std::string, std::string>::const_iterator it;
	if (loc && (it = loc->getCgiExe().find(std::string(uri.c_str() + dotPos))) != loc->getCgiExe().end())
		return new std::string(it->second);
	
	// Case the extension doesn't match anything
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
callq 0x213f0
movq %rax, -0x20(%rbp)
cmpq $-0x1, -0x20(%rbp)
jne 0x214b4
movq $0x0, -0x8(%rbp)
jmp 0x21731
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq $-0x1, %rdx
leaq 0x2673(%rip), %rcx      # 0x23b3d
callq 0x5710
cmpl $0x0, %eax
jne 0x2156a
movl $0x20, %edi
callq 0x5550
movq %rax, -0x88(%rbp)
movq %rax, -0x90(%rbp)
movb $0x1, -0x35(%rbp)
leaq -0x21(%rbp), %rdi
movq %rdi, -0x80(%rbp)
callq 0x58f0
movq -0x88(%rbp), %rdi
movq -0x80(%rbp), %rdx
leaq 0x262a(%rip), %rsi      # 0x23b3d
callq 0x63e0
jmp 0x2151a
movq -0x90(%rbp), %rax
movb $0x0, -0x35(%rbp)
movq %rax, -0x8(%rbp)
leaq -0x21(%rbp), %rdi
callq 0x55c0
jmp 0x21731
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x21(%rbp), %rdi
callq 0x55c0
testb $0x1, -0x35(%rbp)
jne 0x21554
jmp 0x21565
movq -0x90(%rbp), %rdi
movl $0x20, %esi
callq 0x5560
jmp 0x2173e
leaq -0x40(%rbp), %rdi
callq 0x21d60
movq -0x18(%rbp), %rcx
movb $0x0, -0x6a(%rbp)
movb $0x0, -0x6b(%rbp)
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, -0x91(%rbp)
je 0x2164a
movq -0x18(%rbp), %rdi
callq 0xf910
movq %rax, -0xb0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5240
movq -0x20(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
leaq -0x69(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
callq 0x58f0
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
movb $0x1, -0x6a(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x63e0
jmp 0x215e5
movq -0xb0(%rbp), %rdi
movb $0x1, -0x6b(%rbp)
leaq -0x68(%rbp), %rsi
callq 0x1f060
movq %rax, -0xb8(%rbp)
jmp 0x21602
movq -0xb8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
callq 0xf910
movq %rax, -0xc0(%rbp)
jmp 0x21627
movq -0xc0(%rbp), %rdi
callq 0x10c80
movq %rax, -0x78(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x10c50
movb %al, -0x91(%rbp)
movb -0x91(%rbp), %al
movb %al, -0xc1(%rbp)
testb $0x1, -0x6b(%rbp)
jne 0x2165e
jmp 0x21667
leaq -0x68(%rbp), %rdi
callq 0x5a08
testb $0x1, -0x6a(%rbp)
jne 0x2166f
jmp 0x21678
leaq -0x69(%rbp), %rdi
callq 0x55c0
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0x21687
jmp 0x21729
movl $0x20, %edi
callq 0x5550
movq %rax, -0xd0(%rbp)
movq %rax, -0xd8(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x10cb0
movq -0xd0(%rbp), %rdi
movq %rax, %rsi
addq $0x20, %rsi
callq 0x5250
jmp 0x216bd
movq -0xd8(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x21731
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x216f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
testb $0x1, -0x6b(%rbp)
jne 0x216ec
jmp 0x216f5
leaq -0x68(%rbp), %rdi
callq 0x5a08
jmp 0x216f7
testb $0x1, -0x6a(%rbp)
jne 0x216ff
jmp 0x21708
leaq -0x69(%rbp), %rdi
callq 0x55c0
jmp 0x2173e
movq -0xd8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl $0x20, %esi
callq 0x5560
jmp 0x2173e
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x58e0
nopw (%rax,%rax)