// File: /workspace/github_cpp_makefile_star2_O2/solaldunckel[P]webserv/srcs/Config/RequestConfig.cpp
// Function: RequestConfig::getServerForRequest(std::vector<ServerConfig, std::allocator<ServerConfig>>&)
ServerConfig *RequestConfig::getServerForRequest(std::vector<ServerConfig> &servers) {
  std::vector<ServerConfig*> matching_servers;

  // Match server based on request ip + port
  for (std::vector<ServerConfig>::iterator it = servers.begin(); it != servers.end(); it++) {
    for (std::vector<Listen>::iterator list = it->listens_.begin(); list != it->listens_.end(); list++) {
      if ((list->ip_ == host_port_.ip_ || list->ip_ == "0.0.0.0") && list->port_ == host_port_.port_) {
        matching_servers.push_back(&(*it));
        break;
      }
    }
  }

  // If only one match, it's our server
  if (matching_servers.size() == 1)
    return matching_servers.front();

  std::string host = request_.headers_["Host"].substr(0, request_.headers_["Host"].find(':'));

  // We match based on server names
  for (std::vector<ServerConfig*>::iterator it = matching_servers.begin(); it != matching_servers.end(); it++) {
    std::vector<std::string> server_names = (*it)->getServerNames();

    for (std::vector<std::string>::iterator server_name = server_names.begin(); server_name != server_names.end(); server_name++) {
      if (host == *server_name)
        return *it;
    }
  }

  // We use default server (first one)
  return matching_servers.front();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %r14
movq %rdi, %rbx
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r15
andq $0x0, 0x10(%r15)
movaps %xmm0, (%r15)
movq (%rsi), %r12
leaq 0x13a2a(%rip), %r13     # 0x1c32a
cmpq 0x8(%r14), %r12
je 0x895e
movq 0x8(%r12), %rbp
cmpq 0x10(%r12), %rbp
je 0x8955
movq 0x8(%rbx), %rsi
movq %rbp, %rdi
callq 0x91d1
testb %al, %al
jne 0x8931
movq %rbp, %rdi
movq %r13, %rsi
callq 0x610b
testb %al, %al
je 0x893d
movl 0x20(%rbp), %eax
movq 0x8(%rbx), %rcx
cmpl 0x20(%rcx), %eax
je 0x8943
addq $0x28, %rbp
jmp 0x890b
movq %r12, 0x48(%rsp)
movq %r15, %rdi
leaq 0x48(%rsp), %rsi
callq 0x9312
addq $0x198, %r12            # imm = 0x198
jmp 0x8900
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
cmpq $0x8, %rcx
jne 0x8979
movq (%rax), %rbx
jmp 0x8a80
movq (%rbx), %r14
leaq 0x139af(%rip), %rsi     # 0x1c332
leaq 0x28(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x5328
addq $0xc0, %r14
leaq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x91fa
movq %rax, %r14
movq (%rbx), %rbx
leaq 0x1397f(%rip), %rsi     # 0x1c332
leaq 0x68(%rsp), %rdi
leaq 0xe(%rsp), %rdx
callq 0x5328
addq $0xc0, %rbx
leaq 0x68(%rsp), %rsi
movq %rbx, %rdi
callq 0x91fa
pushq $0x3a
popq %rsi
movq %rax, %rdi
xorl %edx, %edx
callq 0x45a0
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x44a0
leaq 0x68(%rsp), %rdi
callq 0x4898
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x4898
movq 0x10(%rsp), %r13
leaq 0x48(%rsp), %r15
cmpq 0x18(%rsp), %r13
je 0x8a6e
movq (%r13), %rdi
callq 0xb012
movq %r14, %rdi
movq %rax, %rsi
callq 0x6e58
movq 0x28(%rsp), %rcx
movq %rcx, %r12
movq 0x30(%rsp), %rbp
cmpq %rbp, %rcx
je 0x8a5b
movq %r15, %rdi
movq %r12, %rsi
callq 0x91d1
leaq 0x20(%r12), %rcx
testb %al, %al
je 0x8a36
movq (%r13), %rbx
movq %r14, %rdi
callq 0x608e
addq $0x8, %r13
cmpq %rbp, %r12
je 0x8a16
jmp 0x8a76
movq 0x10(%rsp), %rax
movq (%rax), %rbx
leaq 0x48(%rsp), %rdi
callq 0x4898
leaq 0x10(%rsp), %rdi
callq 0x92de
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8aa1
movq %rax, %rbx
jmp 0x8ab5
jmp 0x8abc
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x4898
leaq 0x28(%rsp), %rdi
jmp 0x8ac9
movq %rax, %rbx
jmp 0x8ace
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x4898
leaq 0x10(%rsp), %rdi
callq 0x92de
movq %rbx, %rdi
callq 0x4750