// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movq (%rsi), %r15
cmpq 0x8(%rsi), %r15
je 0x98ce
movq %rsi, %r14
leaq 0x50(%rsp), %r12
leaq 0x1badf(%rip), %r13     # 0x25340
movq 0x8(%r15), %rbp
cmpq 0x10(%r15), %rbp
je 0x98c1
movq 0x8(%rbx), %rax
movq 0x8(%rbp), %rdx
cmpq 0x8(%rax), %rdx
jne 0x988e
testq %rdx, %rdx
je 0x989d
movq (%rax), %rsi
movq (%rbp), %rdi
callq 0x4470
testl %eax, %eax
je 0x989d
movq %rbp, %rdi
movq %r13, %rsi
callq 0x40f0
testl %eax, %eax
jne 0x98a9
movl 0x20(%rbp), %eax
movq 0x8(%rbx), %rcx
cmpl 0x20(%rcx), %eax
je 0x98af
addq $0x28, %rbp
jmp 0x9865
movq %r15, 0x50(%rsp)
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0xa906
addq $0x198, %r15            # imm = 0x198
cmpq 0x8(%r14), %r15
jne 0x9861
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
cmpq $0x8, %rcx
jne 0x98e9
movq (%rax), %rbx
jmp 0x9a8b
movq (%rbx), %r14
leaq 0x40(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1ba4c(%rip), %rsi     # 0x25348
leaq 0x1ba49(%rip), %rdx     # 0x2534c
leaq 0x30(%rsp), %rdi
callq 0x578e
addq $0xc0, %r14
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0xa7d2
movq %rax, %r14
movq (%rbx), %rbx
leaq 0x88(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x1ba0d(%rip), %rsi     # 0x25348
leaq 0x1ba0a(%rip), %rdx     # 0x2534c
leaq 0x78(%rsp), %rdi
callq 0x578e
addq $0xc0, %rbx
leaq 0x78(%rsp), %rsi
movq %rbx, %rdi
callq 0xa7d2
movq %rax, %rdi
movl $0x3a, %esi
xorl %edx, %edx
callq 0x4580
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4480
movq 0x78(%rsp), %rdi
cmpq %r12, %rdi
je 0x999b
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x4460
movq 0x30(%rsp), %rdi
cmpq %r15, %rdi
je 0x99b2
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x4460
movq 0x10(%rsp), %r13
cmpq 0x18(%rsp), %r13
sete %cl
je 0x9a58
leaq 0x30(%rsp), %r15
movb %cl, 0xf(%rsp)
movq (%r13), %rdi
callq 0xcba0
movq %r15, %rdi
movq %rax, %rsi
callq 0x799c
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %rbp
cmpq %rbp, %r14
sete %bl
je 0x9a37
movq 0x50(%rsp), %r15
movq 0x58(%rsp), %r12
cmpq 0x8(%r14), %r12
jne 0x9a1b
testq %r12, %r12
je 0x9a29
movq (%r14), %rsi
movq %r15, %rdi
movq %r12, %rdx
callq 0x4470
testl %eax, %eax
je 0x9a29
addq $0x20, %r14
cmpq %rbp, %r14
sete %bl
jne 0x99fe
jmp 0x9a32
movq (%r13), %rax
movq %rax, 0x70(%rsp)
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x69ba
testb $0x1, %bl
je 0x9a5a
addq $0x8, %r13
cmpq 0x18(%rsp), %r13
sete %cl
jne 0x99ca
jmp 0x9a5e
jmp 0x9a5e
movb 0xf(%rsp), %cl
testb $0x1, %cl
movq 0x70(%rsp), %rbx
je 0x9a70
movq 0x10(%rsp), %rax
movq (%rax), %rbx
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x9a8b
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x4460
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x9aa2
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4460
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9ab9
movq %rax, %rbx
jmp 0x9add
jmp 0x9aee
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r12, %rdi
je 0x9add
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x4460
movq 0x30(%rsp), %rdi
cmpq %r15, %rdi
je 0x9b11
movq 0x40(%rsp), %rsi
jmp 0x9b09
movq %rax, %rbx
jmp 0x9b11
movq %rax, %rbx
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x9b11
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x4460
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x9b28
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4460
movq %rbx, %rdi
callq 0x4780