// File: 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
movq %rsp, %rbp
subq $0x470, %rsp            # imm = 0x470
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x3a0(%rbp)
movl $0x1, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x187e0
movq -0x3a0(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xef90
movq %rax, -0x30(%rbp)
movq -0x3a0(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0xbdd0
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x13670
testb $0x1, %al
jne 0x2be1f
jmp 0x2c885
leaq -0x30(%rbp), %rdi
callq 0x136b0
movq %rax, %rdi
callq 0x18780
movq %rax, -0x3a8(%rbp)
jmp 0x2be39
movq -0x3a8(%rbp), %rdi
callq 0x2e680
testb $0x1, %al
jne 0x2be4e
jmp 0x2bf42
leaq -0x30(%rbp), %rdi
callq 0x136b0
movq %rax, %rdi
callq 0x18780
movq %rax, -0x3b0(%rbp)
jmp 0x2be68
leaq -0x91(%rbp), %rdi
movq %rdi, -0x3b8(%rbp)
callq 0x5960
movq -0x3b8(%rbp), %rdx
leaq 0x174b3(%rip), %rsi     # 0x4333c
leaq -0x90(%rbp), %rdi
callq 0x6be0
jmp 0x2be97
leaq -0x70(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movl $0x50, %edx
callq 0x18cf0
jmp 0x2beae
movq -0x3b0(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x18d70
jmp 0x2bec0
leaq -0x70(%rbp), %rdi
callq 0xcba0
leaq -0x90(%rbp), %rdi
callq 0x5aa8
leaq -0x91(%rbp), %rdi
callq 0x55e0
jmp 0x2bf42
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c9e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2bf31
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2bf25
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x70(%rbp), %rdi
callq 0xcba0
leaq -0x90(%rbp), %rdi
callq 0x5aa8
leaq -0x91(%rbp), %rdi
callq 0x55e0
jmp 0x2c9e5
leaq -0x30(%rbp), %rdi
callq 0x136b0
movq %rax, %rdi
callq 0x18780
movq %rax, -0x3c0(%rbp)
jmp 0x2bf5c
movq -0x3c0(%rbp), %rdi
callq 0x136d0
movq %rax, -0xa0(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x136b0
movq %rax, %rdi
callq 0x18780
movq %rax, -0x3c8(%rbp)
jmp 0x2bf89
movq -0x3c8(%rbp), %rdi
callq 0x13740
movq %rax, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x13700
testb $0x1, %al
jne 0x2bfb8
jmp 0x2c86c
leaq -0x28(%rbp), %rdi
movq %rdi, -0x3d8(%rbp)
callq 0x136d0
movq -0x3d8(%rbp), %rdi
movq %rax, -0xb8(%rbp)
callq 0x13740
movq %rax, -0xc0(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x1be90
movq %rax, %rdx
movq -0xb8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x18d20
movq %rax, -0x3d0(%rbp)
jmp 0x2c00d
movq -0x3d0(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x13740
movq %rax, -0xc8(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x2e6c0
testb $0x1, %al
jne 0x2c047
jmp 0x2c850
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x5100
movl %eax, -0x10(%rbp)
cmpl $0x0, %eax
jge 0x2c1a3
movb $0x1, -0x112(%rbp)
movl $0x30, %edi
callq 0x5250
movq %rax, -0x3e8(%rbp)
leaq -0xe9(%rbp), %rdi
movq %rdi, -0x3e0(%rbp)
callq 0x5960
movq -0x3e0(%rbp), %rdx
leaq 0x1790d(%rip), %rsi     # 0x439aa
leaq -0xe8(%rbp), %rdi
callq 0x6be0
jmp 0x2c0ab
callq 0x5060
movl (%rax), %eax
movl %eax, -0x3f4(%rbp)
leaq -0x111(%rbp), %rdi
movq %rdi, -0x3f0(%rbp)
callq 0x5960
movq -0x3f0(%rbp), %rdx
leaq 0x1797b(%rip), %rsi     # 0x43a54
leaq -0x110(%rbp), %rdi
callq 0x6be0
jmp 0x2c0e7
movl -0x3f4(%rbp), %edx
movq -0x3e8(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
leaq -0x110(%rbp), %rcx
callq 0x8b80
jmp 0x2c109
movq -0x3e8(%rbp), %rdi
movb $0x0, -0x112(%rbp)
leaq 0x2fbd2(%rip), %rsi     # 0x5bcf0
leaq -0x232f5(%rip), %rdx    # 0x8e30
callq 0x5920
jmp 0x2c9f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c17b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c163
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x5aa8
leaq -0x111(%rbp), %rdi
callq 0x55e0
leaq -0xe8(%rbp), %rdi
callq 0x5aa8
leaq -0xe9(%rbp), %rdi
callq 0x55e0
testb $0x1, -0x112(%rbp)
jne 0x2c192
jmp 0x2c19e
movq -0x3e8(%rbp), %rdi
callq 0x53b0
jmp 0x2c9e5
movl -0x10(%rbp), %edi
xorl %eax, %eax
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x52e0
jmp 0x2c1b9
leaq -0x124(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x52b0
movw $0x2, -0x124(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x13800
movq %rax, %rdi
callq 0x51f0
movq %rax, %rdi
callq 0x5570
movl %eax, -0x120(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x13800
movl 0x20(%rax), %eax
movzwl %ax, %edi
callq 0x5070
movw %ax, -0x122(%rbp)
movl -0x10(%rbp), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0xc(%rbp), %rcx
movl $0x4, %r8d
callq 0x5980
movl -0x10(%rbp), %edi
leaq -0x124(%rbp), %rsi
movl $0x10, %edx
callq 0x50a0
cmpl $0x0, %eax
jge 0x2c3e3
movb $0x1, -0x1b1(%rbp)
movl $0x30, %edi
callq 0x5250
movq %rax, -0x408(%rbp)
leaq -0x149(%rbp), %rdi
movq %rdi, -0x400(%rbp)
callq 0x5960
movq -0x400(%rbp), %rdx
leaq 0x17733(%rip), %rsi     # 0x439ba
leaq -0x148(%rbp), %rdi
callq 0x6be0
jmp 0x2c295
callq 0x5060
movl (%rax), %eax
movl %eax, -0x40c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x13800
movq %rax, %rsi
leaq 0x17179(%rip), %rdx     # 0x43431
leaq -0x190(%rbp), %rdi
callq 0x8f70
jmp 0x2c2c6
leaq -0xa0(%rbp), %rdi
callq 0x13800
movl 0x20(%rax), %esi
leaq -0x1b0(%rbp), %rdi
callq 0x20fb0
jmp 0x2c2e3
leaq -0x170(%rbp), %rdi
leaq -0x190(%rbp), %rsi
leaq -0x1b0(%rbp), %rdx
callq 0x9550
jmp 0x2c2ff
movl -0x40c(%rbp), %edx
movq -0x408(%rbp), %rdi
leaq -0x148(%rbp), %rsi
leaq -0x170(%rbp), %rcx
callq 0x8b80
jmp 0x2c321
movq -0x408(%rbp), %rdi
movb $0x0, -0x1b1(%rbp)
leaq 0x2f9ba(%rip), %rsi     # 0x5bcf0
leaq -0x2350d(%rip), %rdx    # 0x8e30
callq 0x5920
jmp 0x2c9f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c3bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c3af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c3a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c397
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x5aa8
leaq -0x1b0(%rbp), %rdi
callq 0x5aa8
leaq -0x190(%rbp), %rdi
callq 0x5aa8
leaq -0x148(%rbp), %rdi
callq 0x5aa8
leaq -0x149(%rbp), %rdi
callq 0x55e0
testb $0x1, -0x1b1(%rbp)
jne 0x2c3d2
jmp 0x2c3de
movq -0x408(%rbp), %rdi
callq 0x53b0
jmp 0x2c9e5
movl -0x10(%rbp), %edi
movl $0x80, %esi
callq 0x5640
cmpl $0x0, %eax
jge 0x2c538
movb $0x1, -0x202(%rbp)
movl $0x30, %edi
callq 0x5250
movq %rax, -0x420(%rbp)
leaq -0x1d9(%rbp), %rdi
movq %rdi, -0x418(%rbp)
callq 0x5960
movq -0x418(%rbp), %rdx
leaq 0x1759b(%rip), %rsi     # 0x439cd
leaq -0x1d8(%rbp), %rdi
callq 0x6be0
jmp 0x2c440
callq 0x5060
movl (%rax), %eax
movl %eax, -0x42c(%rbp)
leaq -0x201(%rbp), %rdi
movq %rdi, -0x428(%rbp)
callq 0x5960
movq -0x428(%rbp), %rdx
leaq 0x175e6(%rip), %rsi     # 0x43a54
leaq -0x200(%rbp), %rdi
callq 0x6be0
jmp 0x2c47c
movl -0x42c(%rbp), %edx
movq -0x420(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
leaq -0x200(%rbp), %rcx
callq 0x8b80
jmp 0x2c49e
movq -0x420(%rbp), %rdi
movb $0x0, -0x202(%rbp)
leaq 0x2f83d(%rip), %rsi     # 0x5bcf0
leaq -0x2368a(%rip), %rdx    # 0x8e30
callq 0x5920
jmp 0x2c9f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c510
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c4f8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x5aa8
leaq -0x201(%rbp), %rdi
callq 0x55e0
leaq -0x1d8(%rbp), %rdi
callq 0x5aa8
leaq -0x1d9(%rbp), %rdi
callq 0x55e0
testb $0x1, -0x202(%rbp)
jne 0x2c527
jmp 0x2c533
movq -0x420(%rbp), %rdi
callq 0x53b0
jmp 0x2c9e5
leaq -0xa0(%rbp), %rdi
callq 0x13800
movq %rax, %rsi
leaq -0x250(%rbp), %rdi
callq 0x5200
jmp 0x2c555
leaq -0xa0(%rbp), %rdi
callq 0x13800
movl 0x20(%rax), %edx
leaq -0x230(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x18cf0
jmp 0x2c579
movq -0x3a0(%rbp), %rdi
addq $0x18, %rdi
leaq -0x10(%rbp), %rsi
callq 0x2e700
movq %rax, -0x438(%rbp)
jmp 0x2c596
movq -0x438(%rbp), %rdi
leaq -0x230(%rbp), %rsi
movq %rsi, -0x440(%rbp)
callq 0x2e7e0
movq -0x440(%rbp), %rdi
callq 0xcba0
leaq -0x250(%rbp), %rdi
callq 0x5aa8
leaq -0xa0(%rbp), %rdi
callq 0x13800
movq %rax, %rsi
leaq -0x2f0(%rbp), %rdi
callq 0x5200
jmp 0x2c5e5
leaq -0x2d0(%rbp), %rdi
leaq -0x2f0(%rbp), %rsi
callq 0x2e820
jmp 0x2c5fa
leaq 0x173dc(%rip), %rsi     # 0x439dd
leaq -0x2b0(%rbp), %rdi
leaq -0x2d0(%rbp), %rdx
callq 0x107f0
jmp 0x2c616
leaq 0x16e14(%rip), %rdx     # 0x43431
leaq -0x290(%rbp), %rdi
leaq -0x2b0(%rbp), %rsi
callq 0x6a80
jmp 0x2c632
leaq -0xa0(%rbp), %rdi
callq 0x13800
movl 0x20(%rax), %esi
leaq -0x310(%rbp), %rdi
callq 0x20fb0
jmp 0x2c64f
leaq -0x270(%rbp), %rdi
leaq -0x290(%rbp), %rsi
leaq -0x310(%rbp), %rdx
callq 0x9550
jmp 0x2c66b
leaq -0x331(%rbp), %rdi
movq %rdi, -0x448(%rbp)
callq 0x5960
movq -0x448(%rbp), %rdx
leaq 0x1735f(%rip), %rsi     # 0x439eb
leaq -0x330(%rbp), %rdi
callq 0x6be0
jmp 0x2c69a
leaq 0x2ff57(%rip), %rdi     # 0x5c5f8
movl $0x1, %esi
leaq -0x270(%rbp), %rdx
leaq -0x330(%rbp), %rcx
xorl %r8d, %r8d
callq 0x3b240
jmp 0x2c6be
leaq -0x330(%rbp), %rdi
callq 0x5aa8
leaq -0x331(%rbp), %rdi
callq 0x55e0
leaq -0x270(%rbp), %rdi
callq 0x5aa8
leaq -0x310(%rbp), %rdi
callq 0x5aa8
leaq -0x290(%rbp), %rdi
callq 0x5aa8
leaq -0x2b0(%rbp), %rdi
callq 0x5aa8
leaq -0x2d0(%rbp), %rdi
callq 0x5aa8
leaq -0x2f0(%rbp), %rdi
callq 0x5aa8
movq -0x3a0(%rbp), %rdi
movl -0x10(%rbp), %esi
callq 0x2ca00
jmp 0x2c72f
leaq -0xa0(%rbp), %rdi
callq 0x1be90
movq %rax, %rsi
leaq -0x28(%rbp), %rdi
callq 0x2e8d0
jmp 0x2c749
jmp 0x2c850
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c774
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x230(%rbp), %rdi
callq 0xcba0
leaq -0x250(%rbp), %rdi
callq 0x5aa8
jmp 0x2c9e5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c83f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c833
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c827
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c81b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c80f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c7f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x330(%rbp), %rdi
callq 0x5aa8
leaq -0x331(%rbp), %rdi
callq 0x55e0
leaq -0x270(%rbp), %rdi
callq 0x5aa8
leaq -0x310(%rbp), %rdi
callq 0x5aa8
leaq -0x290(%rbp), %rdi
callq 0x5aa8
leaq -0x2b0(%rbp), %rdi
callq 0x5aa8
leaq -0x2d0(%rbp), %rdi
callq 0x5aa8
leaq -0x2f0(%rbp), %rdi
callq 0x5aa8
jmp 0x2c9e5
jmp 0x2c852
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
callq 0x13870
movq %rax, -0x340(%rbp)
jmp 0x2bf6f
jmp 0x2c86e
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x138b0
movq %rax, -0x348(%rbp)
jmp 0x2bdf5
leaq -0x28(%rbp), %rdi
callq 0x2e680
testb $0x1, %al
jne 0x2c897
jmp 0x2c9d3
movb $0x1, -0x392(%rbp)
movl $0x30, %edi
callq 0x5250
movq %rax, -0x458(%rbp)
leaq -0x369(%rbp), %rdi
movq %rdi, -0x450(%rbp)
callq 0x5960
movq -0x450(%rbp), %rdx
leaq 0x170fd(%rip), %rsi     # 0x439cd
leaq -0x368(%rbp), %rdi
callq 0x6be0
jmp 0x2c8de
callq 0x5060
movl (%rax), %eax
movl %eax, -0x464(%rbp)
leaq -0x391(%rbp), %rdi
movq %rdi, -0x460(%rbp)
callq 0x5960
movq -0x460(%rbp), %rdx
leaq 0x17148(%rip), %rsi     # 0x43a54
leaq -0x390(%rbp), %rdi
callq 0x6be0
jmp 0x2c91a
movl -0x464(%rbp), %edx
movq -0x458(%rbp), %rdi
leaq -0x368(%rbp), %rsi
leaq -0x390(%rbp), %rcx
callq 0x8b80
jmp 0x2c93c
movq -0x458(%rbp), %rdi
movb $0x0, -0x392(%rbp)
leaq 0x2f39f(%rip), %rsi     # 0x5bcf0
leaq -0x23b28(%rip), %rdx    # 0x8e30
callq 0x5920
jmp 0x2c9f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c9ae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x2c996
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x390(%rbp), %rdi
callq 0x5aa8
leaq -0x391(%rbp), %rdi
callq 0x55e0
leaq -0x368(%rbp), %rdi
callq 0x5aa8
leaq -0x369(%rbp), %rdi
callq 0x55e0
testb $0x1, -0x392(%rbp)
jne 0x2c9c5
jmp 0x2c9d1
movq -0x458(%rbp), %rdi
callq 0x53b0
jmp 0x2c9e5
leaq -0x28(%rbp), %rdi
callq 0xc460
addq $0x470, %rsp            # imm = 0x470
popq %rbp
retq
leaq -0x28(%rbp), %rdi
callq 0xc460
movq -0x40(%rbp), %rdi
callq 0x5950
nopw (%rax,%rax)