// File: solaldunckel[P]webserv/srcs/HTTP/Server.cpp
// Function: Server::run(int)
void Server::run(int worker_id) {
  worker_id_ = worker_id;

  head_ = "server : ";
  if (worker_id_ > 0)
    head_ = "worker[" + ft::to_string(worker_id_) + "] : ";

  if (!worker_id_ || worker_id_ == 1) {
    signal(SIGINT, interruptHandler);
    signal(SIGQUIT, interruptHandler);
  }

  running_ = true;
  Log.print(INFO, head_ + "starting", GREEN);

  struct timeval timeout;

  timeout.tv_sec = 1;
  timeout.tv_usec = 0;

  while (running_) {
    read_fds_ = master_fds_;
    write_fds_ = master_fds_;

    int ret = select(max_fd_ + 1, &read_fds_, &write_fds_, NULL, &timeout);

    if (ret >= 0) {
      for (std::map<int, Listen>::iterator it = running_server_.begin(); it != running_server_.end(); it++) {
        if (FD_ISSET(it->first, &read_fds_)) {
          pthread_mutex_lock(&g_accept);
          newConnection(it->first);
          pthread_mutex_unlock(&g_accept);
        }
      }

      std::map<int, Client*>::iterator next_it;

      for (std::map<int, Client*>::iterator it = clients_.begin(), next_it = it; it != clients_.end(); it = next_it) {
        int client_fd = it->first;

        next_it++;

        if (FD_ISSET(client_fd, &read_fds_) && !recv(client_fd)) {
          clientDisconnect(client_fd);
          continue;
        }

        check_timeout_disconnect(it->second);

        if (FD_ISSET(client_fd, &write_fds_) && !send(client_fd)) {
          clientDisconnect(client_fd);
          continue;
        }
      }
    } else if (ret == -1 && running_)
      throw webserv_exception("select() failed", errno);
    usleep(500);
  }

  std::map<int, Client*>::iterator next_it;

  for (std::map<int, Client*>::iterator it = clients_.begin(), next_it = it; it != clients_.end(); it = next_it) {
    next_it++;
    clientDisconnect(it->first);
  }

  Log.print(INFO, head_ + "shutdown", GREEN);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
movl -0xc(%rbp), %eax
movl %eax, (%rdi)
addq $0x218, %rdi            # imm = 0x218
leaq 0x1609e(%rip), %rsi     # 0x43a1e
callq 0x5780
movq -0x1f0(%rbp), %rax
cmpl $0x0, (%rax)
jle 0x2da42
movq -0x1f0(%rbp), %rax
movl (%rax), %esi
leaq -0x70(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x9070
movq -0x1f8(%rbp), %rdx
leaq 0x15bff(%rip), %rsi     # 0x435bb
leaq -0x50(%rbp), %rdi
callq 0x107f0
jmp 0x2d9c7
leaq 0x1605a(%rip), %rdx     # 0x43a28
leaq -0x30(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x6a80
jmp 0x2d9dd
movq -0x1f0(%rbp), %rdi
addq $0x218, %rdi            # imm = 0x218
leaq -0x30(%rbp), %rsi
callq 0x56d0
leaq -0x30(%rbp), %rdi
callq 0x5aa8
leaq -0x50(%rbp), %rdi
callq 0x5aa8
leaq -0x70(%rbp), %rdi
callq 0x5aa8
jmp 0x2da42
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x2da34
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x5aa8
leaq -0x70(%rbp), %rdi
callq 0x5aa8
jmp 0x2e1ac
movq -0x1f0(%rbp), %rax
cmpl $0x0, (%rax)
je 0x2da5a
movq -0x1f0(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x2da7c
movl $0x2, %edi
leaq 0x75a(%rip), %rsi       # 0x2e1c0
callq 0x56a0
movl $0x3, %edi
leaq 0x749(%rip), %rsi       # 0x2e1c0
callq 0x56a0
movq -0x1f0(%rbp), %rsi
movb $0x1, 0x2ebb7(%rip)     # 0x5c641
addq $0x218, %rsi            # imm = 0x218
leaq 0x15f95(%rip), %rdx     # 0x43a2d
leaq -0xa0(%rbp), %rdi
callq 0x8f70
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x5960
movq -0x200(%rbp), %rdx
leaq 0x15f26(%rip), %rsi     # 0x439eb
leaq -0xc0(%rbp), %rdi
callq 0x6be0
jmp 0x2dad3
leaq 0x2eb1e(%rip), %rdi     # 0x5c5f8
movl $0x1, %esi
leaq -0xa0(%rbp), %rdx
leaq -0xc0(%rbp), %rcx
xorl %r8d, %r8d
callq 0x3b240
jmp 0x2daf7
leaq -0xc0(%rbp), %rdi
callq 0x5aa8
leaq -0xc1(%rbp), %rdi
callq 0x55e0
leaq -0xa0(%rbp), %rdi
callq 0x5aa8
movq $0x1, -0xd8(%rbp)
movq $0x0, -0xd0(%rbp)
testb $0x1, 0x2eb09(%rip)     # 0x5c641
je 0x2e02d
movq -0x1f0(%rbp), %rdi
movq %rdi, %rsi
addq $0x78, %rsi
addq $0xf8, %rdi
movl $0x80, %edx
callq 0x5440
movq -0x1f0(%rbp), %rdi
movq %rdi, %rsi
addq $0x78, %rsi
addq $0x178, %rdi            # imm = 0x178
movl $0x80, %edx
callq 0x5440
movq -0x1f0(%rbp), %rdx
movl 0x210(%rdx), %edi
addl $0x1, %edi
movq %rdx, %rsi
addq $0xf8, %rsi
addq $0x178, %rdx            # imm = 0x178
xorl %eax, %eax
movl %eax, %ecx
leaq -0xd8(%rbp), %r8
callq 0x5510
movl %eax, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
jl 0x2dec3
movq -0x1f0(%rbp), %rdi
addq $0x18, %rdi
callq 0x2f110
movq %rax, -0xe8(%rbp)
movq -0x1f0(%rbp), %rdi
addq $0x18, %rdi
callq 0x2f170
movq %rax, -0xf0(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x2f140
testb $0x1, %al
jne 0x2dc0a
jmp 0x2dd01
leaq -0xe8(%rbp), %rdi
callq 0x2f1a0
movl (%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x1f0(%rbp), %rax
movslq %ecx, %rcx
movq 0xf8(%rax,%rcx,8), %rax
movq %rax, -0x208(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x2f1a0
movl (%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x208(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x2dce5
leaq 0x2e91b(%rip), %rdi     # 0x5c590
callq 0x5800
leaq -0xe8(%rbp), %rdi
callq 0x2f1a0
movq -0x1f0(%rbp), %rdi
movl (%rax), %esi
callq 0x2ca90
leaq 0x2e8f5(%rip), %rdi     # 0x5c590
callq 0x53f0
jmp 0x2dce5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x2dcc8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x5aa8
leaq -0xc1(%rbp), %rdi
callq 0x55e0
leaq -0xa0(%rbp), %rdi
callq 0x5aa8
jmp 0x2e1ac
jmp 0x2dce7
leaq -0xe8(%rbp), %rdi
xorl %esi, %esi
callq 0x2f1c0
movq %rax, -0xf8(%rbp)
jmp 0x2dbd7
leaq -0x100(%rbp), %rdi
callq 0x2f200
movq -0x1f0(%rbp), %rdi
addq $0x48, %rdi
callq 0x2f220
movq %rax, -0x108(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0x110(%rbp)
movq -0x1f0(%rbp), %rdi
addq $0x48, %rdi
callq 0x2eaa0
movq %rax, -0x118(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x2ea40
testb $0x1, %al
jne 0x2dd65
jmp 0x2debe
leaq -0x108(%rbp), %rdi
callq 0x2f250
movl (%rax), %eax
movl %eax, -0x11c(%rbp)
leaq -0x110(%rbp), %rdi
xorl %esi, %esi
callq 0x2f270
movq %rax, -0x128(%rbp)
movl -0x11c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x1f0(%rbp), %rax
movslq %ecx, %rcx
movq 0xf8(%rax,%rcx,8), %rax
movq %rax, -0x210(%rbp)
movl -0x11c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x210(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x2de0f
movq -0x1f0(%rbp), %rdi
movl -0x11c(%rbp), %esi
callq 0x2d200
testb $0x1, %al
jne 0x2de0f
movq -0x1f0(%rbp), %rdi
movl -0x11c(%rbp), %esi
callq 0x2cf60
jmp 0x2deab
leaq -0x108(%rbp), %rdi
callq 0x2f250
movq -0x1f0(%rbp), %rdi
movq 0x8(%rax), %rsi
callq 0x2d8e0
movl -0x11c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x1f0(%rbp), %rax
movslq %ecx, %rcx
movq 0x178(%rax,%rcx,8), %rax
movq %rax, -0x218(%rbp)
movl -0x11c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x218(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x2dea9
movq -0x1f0(%rbp), %rdi
movl -0x11c(%rbp), %esi
callq 0x2d620
testb $0x1, %al
jne 0x2dea9
movq -0x1f0(%rbp), %rdi
movl -0x11c(%rbp), %esi
callq 0x2cf60
jmp 0x2deab
jmp 0x2deab
movq -0x110(%rbp), %rax
movq %rax, -0x108(%rbp)
jmp 0x2dd32
jmp 0x2e01e
cmpl $-0x1, -0xdc(%rbp)
jne 0x2e01c
testb $0x1, 0x2e76a(%rip)     # 0x5c641
je 0x2e01c
movb $0x1, -0x172(%rbp)
movl $0x30, %edi
callq 0x5250
movq %rax, -0x228(%rbp)
leaq -0x149(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0x5960
movq -0x220(%rbp), %rdx
leaq 0x15b20(%rip), %rsi     # 0x43a36
leaq -0x148(%rbp), %rdi
callq 0x6be0
jmp 0x2df24
callq 0x5060
movl (%rax), %eax
movl %eax, -0x234(%rbp)
leaq -0x171(%rbp), %rdi
movq %rdi, -0x230(%rbp)
callq 0x5960
movq -0x230(%rbp), %rdx
leaq 0x15b02(%rip), %rsi     # 0x43a54
leaq -0x170(%rbp), %rdi
callq 0x6be0
jmp 0x2df60
movl -0x234(%rbp), %edx
movq -0x228(%rbp), %rdi
leaq -0x148(%rbp), %rsi
leaq -0x170(%rbp), %rcx
callq 0x8b80
jmp 0x2df82
movq -0x228(%rbp), %rdi
movb $0x0, -0x172(%rbp)
leaq 0x2dd59(%rip), %rsi     # 0x5bcf0
leaq -0x2516e(%rip), %rdx    # 0x8e30
callq 0x5920
jmp 0x2e1b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x2dff4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x2dfdc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x5aa8
leaq -0x171(%rbp), %rdi
callq 0x55e0
leaq -0x148(%rbp), %rdi
callq 0x5aa8
leaq -0x149(%rbp), %rdi
callq 0x55e0
testb $0x1, -0x172(%rbp)
jne 0x2e00b
jmp 0x2e017
movq -0x228(%rbp), %rdi
callq 0x53b0
jmp 0x2e1ac
jmp 0x2e01e
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x57c0
jmp 0x2db31
leaq -0x180(%rbp), %rdi
callq 0x2f200
movq -0x1f0(%rbp), %rdi
addq $0x48, %rdi
callq 0x2f220
movq %rax, -0x188(%rbp)
movq -0x188(%rbp), %rax
movq %rax, -0x190(%rbp)
movq -0x1f0(%rbp), %rdi
addq $0x48, %rdi
callq 0x2eaa0
movq %rax, -0x198(%rbp)
leaq -0x188(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x2ea40
testb $0x1, %al
jne 0x2e08e
jmp 0x2e0cd
leaq -0x190(%rbp), %rdi
xorl %esi, %esi
callq 0x2f270
movq %rax, -0x1a0(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x2f250
movq -0x1f0(%rbp), %rdi
movl (%rax), %esi
callq 0x2cf60
movq -0x190(%rbp), %rax
movq %rax, -0x188(%rbp)
jmp 0x2e05e
movq -0x1f0(%rbp), %rsi
addq $0x218, %rsi            # imm = 0x218
leaq 0x15964(%rip), %rdx     # 0x43a46
leaq -0x1c0(%rbp), %rdi
callq 0x8f70
leaq -0x1e1(%rbp), %rdi
movq %rdi, -0x240(%rbp)
callq 0x5960
movq -0x240(%rbp), %rdx
leaq 0x158dc(%rip), %rsi     # 0x439eb
leaq -0x1e0(%rbp), %rdi
callq 0x6be0
jmp 0x2e11d
leaq 0x2e4d4(%rip), %rdi     # 0x5c5f8
movl $0x1, %esi
leaq -0x1c0(%rbp), %rdx
leaq -0x1e0(%rbp), %rcx
xorl %r8d, %r8d
callq 0x3b240
jmp 0x2e141
leaq -0x1e0(%rbp), %rdi
callq 0x5aa8
leaq -0x1e1(%rbp), %rdi
callq 0x55e0
leaq -0x1c0(%rbp), %rdi
callq 0x5aa8
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x2e194
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x5aa8
leaq -0x1e1(%rbp), %rdi
callq 0x55e0
leaq -0x1c0(%rbp), %rdi
callq 0x5aa8
movq -0x78(%rbp), %rdi
callq 0x5950
nopw %cs:(%rax,%rax)