// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq (%rsi), %r15
cmpq 0x8(%rsi), %r15
je 0x9968
movq %rsi, %r14
leaq 0x1ba54(%rip), %r13     # 0x25340
movq 0x8(%r15), %rbp
cmpq 0x10(%r15), %rbp
je 0x9950
movq 0x8(%rbx), %r12
movq 0x8(%rbp), %rdx
cmpq 0x8(%r12), %rdx
jne 0x991b
testq %rdx, %rdx
je 0x992e
movq (%r12), %rsi
movq (%rbp), %rdi
callq 0x4480
testl %eax, %eax
je 0x992e
movq %rbp, %rdi
movq %r13, %rsi
callq 0x40f0
testl %eax, %eax
jne 0x9938
movq 0x8(%rbx), %r12
movl 0x20(%rbp), %eax
cmpl 0x20(%r12), %eax
je 0x993e
addq $0x28, %rbp
jmp 0x98f0
movq %r15, 0x38(%rsp)
movq %rsp, %rdi
leaq 0x38(%rsp), %rsi
callq 0xa960
addq $0x198, %r15            # imm = 0x198
cmpq 0x8(%r14), %r15
jne 0x98ec
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
jmp 0x996c
xorl %edi, %edi
xorl %eax, %eax
subq %rdi, %rax
cmpq $0x8, %rax
jne 0x997d
movq (%rdi), %rbx
jmp 0x9af2
movq (%rbx), %r14
leaq 0x28(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1b9b8(%rip), %rsi     # 0x25348
leaq 0x1b9b5(%rip), %rdx     # 0x2534c
leaq 0x18(%rsp), %rdi
callq 0x579e
addq $0xc0, %r14
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0xa828
movq %rax, %r14
movq (%rbx), %rbx
leaq 0x68(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x1b97c(%rip), %rsi     # 0x25348
leaq 0x1b979(%rip), %rdx     # 0x2534c
leaq 0x58(%rsp), %rdi
callq 0x579e
addq $0xc0, %rbx
leaq 0x58(%rsp), %rsi
movq %rbx, %rdi
callq 0xa828
movq %rax, %rdi
movl $0x3a, %esi
xorl %edx, %edx
callq 0x4590
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4490
movq 0x58(%rsp), %rdi
cmpq %r12, %rdi
je 0x9a29
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x4470
movq 0x18(%rsp), %rdi
cmpq %r15, %rdi
je 0x9a40
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4470
movq (%rsp), %r12
cmpq 0x8(%rsp), %r12
je 0x9ab7
leaq 0x18(%rsp), %rbx
movq (%r12), %rdi
callq 0xcc28
movq %rbx, %rdi
movq %rax, %rsi
callq 0x7a00
movq 0x18(%rsp), %r13
movq 0x20(%rsp), %rbp
cmpq %rbp, %r13
je 0x9aa4
movq 0x38(%rsp), %r14
movq 0x40(%rsp), %r15
cmpq 0x8(%r13), %r15
jne 0x9a9b
testq %r15, %r15
je 0x9ac0
movq (%r13), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x4480
testl %eax, %eax
je 0x9ac0
addq $0x20, %r13
cmpq %rbp, %r13
jne 0x9a7d
movq %rbx, %rdi
callq 0x6a2c
addq $0x8, %r12
cmpq 0x8(%rsp), %r12
jne 0x9a50
movq (%rsp), %rax
movq (%rax), %rbx
jmp 0x9ace
movq (%r12), %rbx
leaq 0x18(%rsp), %rdi
callq 0x6a2c
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x9ae9
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x4470
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x9aff
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9b13
movq %rax, %rbx
jmp 0x9b34
jmp 0x9b65
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r12, %rdi
je 0x9b34
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x4470
movq 0x18(%rsp), %rdi
cmpq %r15, %rdi
je 0x9b68
movq 0x28(%rsp), %rsi
jmp 0x9b5b
movq %rax, %rbx
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x9b68
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x4470
jmp 0x9b68
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x9b7e
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq %rbx, %rdi
callq 0x4790