// File: 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
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x140(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x13650
movq -0x18(%rbp), %rdi
callq 0xef90
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rdi
callq 0xbdd0
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x13670
testb $0x1, %al
jne 0x12307
jmp 0x12412
leaq -0x38(%rbp), %rdi
callq 0x136b0
movq %rax, %rdi
addq $0x8, %rdi
callq 0x136d0
movq %rax, -0x48(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x136b0
movq %rax, %rdi
addq $0x8, %rdi
callq 0x13740
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x13700
testb $0x1, %al
jne 0x1234f
jmp 0x123fc
leaq -0x48(%rbp), %rdi
callq 0x13800
movq %rax, %rdi
movq -0x140(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x13770
testb $0x1, %al
jne 0x1239b
leaq -0x48(%rbp), %rdi
callq 0x13800
movq %rax, %rdi
leaq 0x30fba(%rip), %rsi     # 0x4333c
callq 0x8ff0
movb %al, -0x141(%rbp)
jmp 0x1238f
movb -0x141(%rbp), %al
testb $0x1, %al
jne 0x1239b
jmp 0x123e6
leaq -0x48(%rbp), %rdi
callq 0x13800
movq -0x140(%rbp), %rcx
movl 0x20(%rax), %eax
movq 0x8(%rcx), %rcx
cmpl 0x20(%rcx), %eax
jne 0x123e6
leaq -0x38(%rbp), %rdi
callq 0x13850
movq %rax, -0x68(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x13820
jmp 0x123d3
jmp 0x123fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x12774
jmp 0x123e8
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x13870
movq %rax, -0x70(%rbp)
jmp 0x12320
jmp 0x123fe
leaq -0x38(%rbp), %rdi
xorl %esi, %esi
callq 0x138b0
movq %rax, -0x78(%rbp)
jmp 0x122e4
leaq -0x30(%rbp), %rdi
callq 0x138f0
cmpq $0x1, %rax
jne 0x1243d
leaq -0x30(%rbp), %rdi
callq 0x13910
movq (%rax), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x7c(%rbp)
jmp 0x1275e
movq -0x140(%rbp), %rax
movq (%rax), %rax
addq $0xc0, %rax
movq %rax, -0x158(%rbp)
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x5960
movq -0x150(%rbp), %rdx
leaq 0x30ecf(%rip), %rsi     # 0x43344
leaq -0xc0(%rbp), %rdi
callq 0x6be0
jmp 0x12483
movq -0x158(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x13940
movq %rax, -0x160(%rbp)
jmp 0x1249f
movq -0x140(%rbp), %rax
movq (%rax), %rax
addq $0xc0, %rax
movq %rax, -0x170(%rbp)
leaq -0xe9(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0x5960
movq -0x168(%rbp), %rdx
leaq 0x30e6d(%rip), %rsi     # 0x43344
leaq -0xe8(%rbp), %rdi
callq 0x6be0
jmp 0x124e5
movq -0x170(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x13940
movq %rax, -0x178(%rbp)
jmp 0x12501
movq -0x178(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, -0x180(%rbp)
movl $0x3a, %esi
callq 0x5730
movq -0x160(%rbp), %rsi
movq -0x180(%rbp), %rdx
movq %rax, %rcx
leaq -0xa0(%rbp), %rdi
callq 0x55a0
jmp 0x1253c
leaq -0xe8(%rbp), %rdi
callq 0x5aa8
leaq -0xe9(%rbp), %rdi
callq 0x55e0
leaq -0xc0(%rbp), %rdi
callq 0x5aa8
leaq -0xc1(%rbp), %rdi
callq 0x55e0
leaq -0x30(%rbp), %rdi
callq 0x13a20
movq %rax, -0xf8(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x13a90
movq %rax, -0x100(%rbp)
leaq -0xf8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x13a50
testb $0x1, %al
jne 0x125a8
jmp 0x1273b
leaq -0xf8(%rbp), %rdi
callq 0x13ac0
movq (%rax), %rdi
callq 0x187a0
movq %rax, -0x188(%rbp)
jmp 0x125c5
movq -0x188(%rbp), %rsi
leaq -0x118(%rbp), %rdi
callq 0xc1e0
jmp 0x125da
leaq -0x118(%rbp), %rdi
callq 0x9210
movq %rax, -0x120(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x9280
movq %rax, -0x128(%rbp)
leaq -0x120(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x9240
testb $0x1, %al
jne 0x1261c
jmp 0x12703
leaq -0x120(%rbp), %rdi
callq 0x92b0
movq %rax, %rsi
leaq -0xa0(%rbp), %rdi
callq 0x13770
testb $0x1, %al
jne 0x12640
jmp 0x126e7
leaq -0xf8(%rbp), %rdi
callq 0x13ac0
movq (%rax), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x7c(%rbp)
jmp 0x1270a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x126b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x126ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x126a1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x5aa8
leaq -0xe9(%rbp), %rdi
callq 0x55e0
leaq -0xc0(%rbp), %rdi
callq 0x5aa8
leaq -0xc1(%rbp), %rdi
callq 0x55e0
jmp 0x12774
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x5aa8
jmp 0x12774
jmp 0x126e9
leaq -0x120(%rbp), %rdi
xorl %esi, %esi
callq 0x13ae0
movq %rax, -0x130(%rbp)
jmp 0x125ed
movl $0x0, -0x7c(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x8ec0
movl -0x7c(%rbp), %eax
testl %eax, %eax
jne 0x12752
jmp 0x1271f
jmp 0x12721
leaq -0xf8(%rbp), %rdi
xorl %esi, %esi
callq 0x13b20
movq %rax, -0x138(%rbp)
jmp 0x1257c
leaq -0x30(%rbp), %rdi
callq 0x13910
movq (%rax), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0x7c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x5aa8
leaq -0x30(%rbp), %rdi
callq 0x13b60
movq -0x8(%rbp), %rax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
leaq -0x30(%rbp), %rdi
callq 0x13b60
movq -0x58(%rbp), %rdi
callq 0x5950
nopw %cs:(%rax,%rax)