// File: mbourand[P]webserv/srcs/VirtualHost.cpp
// Function: VirtualHost::getServerByName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int, std::__cxx11::list<VirtualHost, std::allocator<VirtualHost>>&)
VirtualHost& VirtualHost::getServerByName(const std::string& name, int port, std::list<VirtualHost>& vhosts)
{
	std::list<VirtualHost*> matches;

	for (std::list<VirtualHost>::iterator vhost_it = vhosts.begin(); vhost_it != vhosts.end(); vhost_it++)
		if (ft::contains(vhost_it->getPorts(), port))
			matches.push_back(&(*vhost_it));
	if (matches.empty())
		throw std::invalid_argument("No matching server found"); // Si ça arrive c'est parce qu'on a un socket ouvert sur un port relié à aucun serveur
	for (std::list<VirtualHost*>::iterator matches_it = matches.begin(); matches_it != matches.end(); matches_it++)
		if (ft::contains((*matches_it)->getConfig().getNames(), name))
			return *(*matches_it);
	return (*matches.front());
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x66d30
movq -0x20(%rbp), %rdi
callq 0x23960
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
callq 0x239c0
movq %rax, -0x48(%rbp)
leaq -0x40(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x23990
testb $0x1, %al
jne 0x669ac
jmp 0x66a22
leaq -0x40(%rbp), %rdi
callq 0x239f0
movq %rax, %rdi
callq 0x66940
movq %rax, %rdi
leaq -0x14(%rbp), %rsi
callq 0x66d50
movb %al, -0x89(%rbp)
jmp 0x669d1
movb -0x89(%rbp), %al
testb $0x1, %al
jne 0x669dd
jmp 0x66a0c
leaq -0x40(%rbp), %rdi
callq 0x66e00
movq %rax, -0x60(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x66dc0
jmp 0x669f9
jmp 0x66a0c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x66b64
jmp 0x66a0e
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
callq 0x23a20
movq %rax, -0x68(%rbp)
jmp 0x6698c
leaq -0x38(%rbp), %rdi
callq 0x66e20
testb $0x1, %al
jne 0x66a31
jmp 0x66a92
movl $0x10, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x98(%rbp)
leaq 0x72b6(%rip), %rsi      # 0x6dd05
callq 0x9be0
jmp 0x66a56
movq -0x98(%rbp), %rdi
movq 0x28564(%rip), %rsi     # 0x8efc8
movq 0x2854d(%rip), %rdx     # 0x8efb8
callq 0x9aa0
jmp 0x66b76
movq -0x98(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
callq 0x94c0
jmp 0x66b64
leaq -0x38(%rbp), %rdi
callq 0x66e40
movq %rax, -0x70(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x66ea0
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x66e70
testb $0x1, %al
jne 0x66abf
jmp 0x66b37
leaq -0x70(%rbp), %rdi
callq 0x66ed0
movq (%rax), %rdi
callq 0x66920
movq %rax, %rdi
callq 0x4c690
movq %rax, -0xa0(%rbp)
jmp 0x66ae1
movq -0xa0(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x4d7b0
movb %al, -0xa1(%rbp)
jmp 0x66af9
movb -0xa1(%rbp), %al
testb $0x1, %al
jne 0x66b05
jmp 0x66b1e
leaq -0x70(%rbp), %rdi
callq 0x66ed0
movq (%rax), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x7c(%rbp)
jmp 0x66b4e
jmp 0x66b20
leaq -0x70(%rbp), %rdi
xorl %esi, %esi
callq 0x66ef0
movq %rax, -0x88(%rbp)
jmp 0x66a9f
leaq -0x38(%rbp), %rdi
callq 0x66f20
movq (%rax), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x7c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x66f50
movq -0x8(%rbp), %rax
addq $0xb0, %rsp
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x66f50
movq -0x50(%rbp), %rdi
callq 0x9af0
nopw %cs:(%rax,%rax)