// File: /workspace/github_cpp_makefile_star2_O2/solaldunckel[P]webserv/srcs/HTTP/Server.cpp
// Function: Server::setup()
void Server::setup() {
  int yes = 1;
  int server_fd = 0;

  std::vector<Listen> is_bind;

  for (std::vector<ServerConfig>::iterator it = servers_.begin(); it != servers_.end(); it++) {
    if (it->getListens().empty())
      it->getListens().push_back(Listen("0.0.0.0", 80));
    for (std::vector<Listen>::iterator list = it->getListens().begin(); list != it->getListens().end(); list++) {
      if (std::find(is_bind.begin(), is_bind.end(), *list) == is_bind.end()) {
        if ((server_fd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
          throw webserv_exception("socket() failed", errno);

        fcntl(server_fd, F_SETFL, O_NONBLOCK);

        struct sockaddr_in address;
        memset(&address, 0, sizeof(address));
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = inet_addr(list->ip_.c_str());
        address.sin_port = htons(list->port_);

        setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

        if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
          throw webserv_exception("bind() to % failed", errno, list->ip_ + ":" + ft::to_string(list->port_));

        if (listen(server_fd, MAX_CONNECTION) < 0)
          throw webserv_exception("listen() failed", errno);

        running_server_[server_fd] = Listen(list->ip_, list->port_);

        Log.print(INFO, "listening on " + ft::to_string(list->ip_) + ":" + ft::to_string(list->port_), GREEN);

        add_to_fd_set(server_fd);
        is_bind.push_back(*list);
      }
    }
  }

  if (is_bind.empty())
    throw webserv_exception("listen() failed", errno);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
leaq 0xc(%rsp), %rax
movl $0x1, (%rax)
leaq 0x4(%rsp), %rax
andl $0x0, (%rax)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rax
andq $0x0, 0x10(%rax)
movaps %xmm0, (%rax)
movq 0x8(%rdi), %rax
movq (%rax), %r14
movq %rdi, 0x28(%rsp)
leaq 0x18(%rdi), %rcx
movq %rcx, 0x88(%rsp)
leaq 0x40(%rsp), %r15
pushq $0x2
popq %r12
pushq $0x4
popq %rbx
cmpq 0x8(%rax), %r14
je 0x1366e
movq %r14, %rdi
callq 0xb00c
movq (%rax), %rcx
cmpq 0x8(%rax), %rcx
jne 0x133fb
movq %r14, %rdi
callq 0xb00c
movq %rax, %rbp
leaq 0x238(%rsp), %r13
movq %r13, %rdi
leaq 0x8f6c(%rip), %rsi      # 0x1c32a
leaq 0x68(%rsp), %rdx
callq 0x5328
movq %r15, %rdi
movq %r13, %rsi
callq 0x45e0
movl $0x50, 0x60(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xbf78
movq %r15, %rdi
callq 0x4898
leaq 0x238(%rsp), %rdi
callq 0x4898
movq %r14, %rdi
callq 0xb00c
movq (%rax), %rbp
movq %r14, 0x90(%rsp)
movq %r14, %rdi
callq 0xb00c
cmpq 0x8(%rax), %rbp
je 0x13659
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rbp, %rdx
callq 0xbe4f
cmpq 0x18(%rsp), %rax
jne 0x13650
movl %r12d, %edi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x40c0
movl %eax, 0x4(%rsp)
testl %eax, %eax
js 0x1369a
movl %eax, %edi
movl %ebx, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x4260
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movw $0x2, 0x30(%rsp)
movq (%rbp), %rdi
callq 0x4480
movl %eax, 0x34(%rsp)
movzwl 0x20(%rbp), %eax
rolw $0x8, %ax
movw %ax, 0x32(%rsp)
movl 0x4(%rsp), %edi
pushq $0x1
popq %rsi
movl %r12d, %edx
leaq 0xc(%rsp), %rcx
movl %ebx, %r8d
callq 0x4770
movl 0x4(%rsp), %edi
leaq 0x30(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x4070
testl %eax, %eax
js 0x13796
movl 0x4(%rsp), %edi
movl $0x80, %esi
callq 0x44d0
testl %eax, %eax
js 0x13718
leaq 0x158(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x45e0
movq %r15, %rdi
movl 0x20(%rbp), %r14d
movq %r13, %rsi
callq 0x45e0
movl %r14d, 0x60(%rsp)
movq 0x88(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x1495e
movq %rax, %rdi
movq %r15, %rsi
callq 0x149ac
movq %r15, %rdi
callq 0x4898
leaq 0x158(%rsp), %rdi
callq 0x4898
leaq 0x118(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0x45e0
leaq 0x98(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x149cd
leaq 0x68(%rsp), %r14
movq %r14, %rdi
leaq 0x9450(%rip), %rsi      # 0x1c9b3
movq %r13, %rdx
callq 0x8234
movq %r15, %rdi
movq %r14, %rsi
leaq 0x8e94(%rip), %rdx      # 0x1c40c
callq 0x5292
movl 0x20(%rbp), %esi
leaq 0xf8(%rsp), %r13
movq %r13, %rdi
callq 0xde17
leaq 0x138(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x61d5
leaq 0x258(%rsp), %rbx
movq %rbx, %rdi
leaq 0x940c(%rip), %rsi      # 0x1c9c1
leaq 0xb(%rsp), %rdx
callq 0x5328
leaq 0x17f22(%rip), %rdi     # 0x2b4e8
pushq $0x1
popq %rsi
leaq 0x138(%rsp), %r13
movq %r13, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x18e1e
movq %rbx, %rdi
callq 0x4898
movq %r13, %rdi
callq 0x4898
leaq 0xf8(%rsp), %rdi
callq 0x4898
movq %r15, %rdi
callq 0x4898
movq %r14, %rdi
callq 0x4898
leaq 0x98(%rsp), %rdi
callq 0x4898
leaq 0x118(%rsp), %rdi
callq 0x4898
movl 0x4(%rsp), %esi
movq 0x28(%rsp), %rdi
callq 0x13a62
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x14a28
pushq $0x2
popq %r12
pushq $0x4
popq %rbx
movq 0x90(%rsp), %r14
addq $0x28, %rbp
jmp 0x1340e
addq $0x198, %r14            # imm = 0x198
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
jmp 0x13386
movq 0x10(%rsp), %rax
cmpq 0x18(%rsp), %rax
je 0x13833
leaq 0x10(%rsp), %rdi
callq 0x6f08
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x30
popq %rdi
callq 0x41e0
movq %rax, %r14
leaq 0x92d4(%rip), %rsi      # 0x1c980
leaq 0x218(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x5328
callq 0x4040
movl (%rax), %ebx
leaq 0x935e(%rip), %rsi      # 0x1ca2a
leaq 0x1f8(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x5328
movb $0x1, %bpl
leaq 0x218(%rsp), %rsi
leaq 0x1f8(%rsp), %rcx
movq %r14, %rdi
movl %ebx, %edx
callq 0x5e96
xorl %ebp, %ebp
leaq 0x1760c(%rip), %rsi     # 0x2ad10
leaq -0xd6b9(%rip), %rdx     # 0x6052
movq %r14, %rdi
callq 0x4730
jmp 0x138ac
pushq $0x30
popq %rdi
callq 0x41e0
movq %rax, %r14
leaq 0x9279(%rip), %rsi      # 0x1c9a3
leaq 0x198(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x5328
callq 0x4040
movl (%rax), %ebx
leaq 0x92e0(%rip), %rsi      # 0x1ca2a
leaq 0x178(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x5328
movb $0x1, %bpl
leaq 0x198(%rsp), %rsi
leaq 0x178(%rsp), %rcx
movq %r14, %rdi
movl %ebx, %edx
callq 0x5e96
xorl %ebp, %ebp
leaq 0x1758e(%rip), %rsi     # 0x2ad10
leaq -0xd737(%rip), %rdx     # 0x6052
movq %r14, %rdi
callq 0x4730
jmp 0x138ac
pushq $0x30
popq %rdi
callq 0x41e0
movq %rax, %r14
leaq 0x91e8(%rip), %rsi      # 0x1c990
leaq 0x1d8(%rsp), %rdi
leaq 0x98(%rsp), %rdx
callq 0x5328
callq 0x4040
movl (%rax), %ebx
leaq 0x8c41(%rip), %rdx      # 0x1c40c
leaq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x60d3
movl 0x20(%rbp), %esi
leaq 0x68(%rsp), %rdi
callq 0xde17
leaq 0x1b8(%rsp), %rdi
leaq 0x40(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x61d5
movb $0x1, %bpl
leaq 0x1d8(%rsp), %rsi
leaq 0x1b8(%rsp), %rcx
movq %r14, %rdi
movl %ebx, %edx
callq 0x5e96
xorl %ebp, %ebp
leaq 0x174ee(%rip), %rsi     # 0x2ad10
leaq -0xd7d7(%rip), %rdx     # 0x6052
movq %r14, %rdi
callq 0x4730
jmp 0x138ac
pushq $0x30
popq %rdi
callq 0x41e0
movq %rax, %r14
leaq 0x915e(%rip), %rsi      # 0x1c9a3
leaq 0xd8(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x5328
callq 0x4040
movl (%rax), %ebx
leaq 0x91c5(%rip), %rsi      # 0x1ca2a
leaq 0xb8(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x5328
movb $0x1, %bpl
leaq 0xd8(%rsp), %rsi
leaq 0xb8(%rsp), %rcx
movq %r14, %rdi
movl %ebx, %edx
callq 0x5e96
xorl %ebp, %ebp
leaq 0x17473(%rip), %rsi     # 0x2ad10
leaq -0xd852(%rip), %rdx     # 0x6052
movq %r14, %rdi
callq 0x4730
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x4898
jmp 0x138c4
movq %rax, %rbx
movb $0x1, %bpl
leaq 0xd8(%rsp), %rdi
jmp 0x1398c
jmp 0x1399f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4898
jmp 0x138e8
movq %rax, %rbx
leaq 0x238(%rsp), %rdi
jmp 0x13a3e
jmp 0x13a4d
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
callq 0x4898
jmp 0x13912
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x68(%rsp), %rdi
callq 0x4898
jmp 0x1392e
jmp 0x13a4d
jmp 0x13a4d
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x40(%rsp), %rdi
callq 0x4898
jmp 0x13940
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x1d8(%rsp), %rdi
jmp 0x1398c
movq %rax, %rbx
leaq 0x178(%rsp), %rdi
callq 0x4898
jmp 0x13962
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x198(%rsp), %rdi
jmp 0x1398c
movq %rax, %rbx
leaq 0x1f8(%rsp), %rdi
callq 0x4898
jmp 0x13984
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x218(%rsp), %rdi
callq 0x4898
testb %bpl, %bpl
jne 0x139a2
jmp 0x13a50
jmp 0x1399f
jmp 0x1399f
movq %rax, %rbx
movq %r14, %rdi
callq 0x4310
jmp 0x13a50
movq %rax, %rbx
jmp 0x13a1f
movq %rax, %rbx
leaq 0x258(%rsp), %rdi
callq 0x4898
jmp 0x139d8
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4898
jmp 0x139ef
movq %rax, %rbx
leaq 0x138(%rsp), %rdi
callq 0x4898
jmp 0x139fc
movq %rax, %rbx
jmp 0x13a29
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
jmp 0x13a3e
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x4898
jmp 0x13a15
movq %rax, %rbx
jmp 0x13a36
jmp 0x13a4d
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4898
leaq 0x68(%rsp), %rdi
callq 0x4898
leaq 0x98(%rsp), %rdi
callq 0x4898
leaq 0x118(%rsp), %rdi
callq 0x4898
jmp 0x13a50
jmp 0x13a4d
jmp 0x13a4d
jmp 0x13a4d
jmp 0x13a4d
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x6f08
movq %rbx, %rdi
callq 0x4750