// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rdi, %rbx
movl %esi, (%rdi)
leaq 0x218(%rdi), %r14
movq 0x220(%rdi), %rdx
leaq 0xb84b(%rip), %rcx      # 0x25a42
movl $0x9, %r8d
movq %r14, %rdi
xorl %esi, %esi
callq 0x4680
movl (%rbx), %esi
testl %esi, %esi
movq %rbx, 0x8(%rsp)
jle 0x1a357
leaq 0x138(%rsp), %r15
movq %r15, %rdi
callq 0x6a15
leaq 0xb37f(%rip), %rcx      # 0x255ac
movl $0x7, %r8d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x44c0
leaq 0x88(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x1a26d
movq %rdx, 0x78(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x88(%rsp)
jmp 0x1a275
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rdx
leaq 0x78(%rsp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0xb7b4(%rip), %rsi      # 0x25a4c
callq 0x4830
leaq 0xa8(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x1a2cd
movq %rdx, 0x98(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xa8(%rsp)
jmp 0x1a2d5
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
leaq 0x98(%rsp), %r15
movq %rdx, 0x8(%r15)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0x4560
movq (%r15), %rdi
cmpq %r13, %rdi
je 0x1a317
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x4460
movq 0x78(%rsp), %rdi
cmpq %r12, %rdi
movq 0x8(%rsp), %rbx
je 0x1a336
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x4460
leaq 0x148(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1a357
movq 0x148(%rsp), %rsi
incq %rsi
callq 0x4460
cmpl $0x1, (%rbx)
ja 0x1a37d
leaq 0x54f(%rip), %r14       # 0x1a8b2
movl $0x2, %edi
movq %r14, %rsi
callq 0x4530
movl $0x3, %edi
movq %r14, %rsi
callq 0x4530
movb $0x1, 0x1819d(%rip)     # 0x32521
leaq 0x48(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x218(%rbx), %rsi
movq 0x220(%rbx), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x6bac
leaq 0xb69f(%rip), %rsi      # 0x25a51
movq %r14, %rdi
callq 0x4830
leaq 0xe8(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0xb642(%rip), %rsi      # 0x25a0f
leaq 0xb640(%rip), %rdx      # 0x25a14
leaq 0xd8(%rsp), %rdi
callq 0x578e
leaq 0x180f0(%rip), %rdi     # 0x324d8
leaq 0x38(%rsp), %rdx
leaq 0xd8(%rsp), %rcx
movl $0x1, %esi
xorl %r8d, %r8d
callq 0x20fae
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
je 0x1a41f
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x4460
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x1a436
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x4460
movq $0x1, 0x98(%rsp)
movq $0x0, 0xa0(%rsp)
cmpb $0x1, 0x180cc(%rip)     # 0x32521
jne 0x1a60c
leaq 0x78(%rbx), %rax
movq %rax, 0x68(%rsp)
leaq 0xf8(%rbx), %r15
leaq 0x178(%rbx), %rax
movq %rax, 0x60(%rsp)
leaq 0x20(%rbx), %r12
leaq 0x50(%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0x17fe5(%rip), %r13     # 0x32470
movq %r12, 0x58(%rsp)
movq %r15, 0x70(%rsp)
movl $0x10, %ecx
movq %r15, %rdi
movq 0x68(%rsp), %rax
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl $0x10, %ecx
movq 0x60(%rsp), %rdx
movq %rdx, %rdi
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl 0x210(%rbx), %edi
incl %edi
movq %r15, %rsi
xorl %ecx, %ecx
leaq 0x98(%rsp), %r8
callq 0x4410
testl %eax, %eax
js 0x1a5d2
movq 0x30(%rbx), %r14
cmpq %r12, %r14
je 0x1a52e
movl 0x20(%r14), %eax
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
movq (%r15,%rcx,8), %rcx
btq %rax, %rcx
jae 0x1a51e
movq %r13, %rdi
callq 0x4600
movl 0x20(%r14), %esi
movq %rbx, %rdi
callq 0x192b0
movq %r13, %rdi
callq 0x4340
movq %r14, %rdi
callq 0x42d0
movq %rax, %r14
cmpq %r12, %rax
jne 0x1a4e6
movq 0x60(%rbx), %r13
cmpq 0x10(%rsp), %r13
je 0x1a5e4
movl 0x20(%r13), %r14d
movq %r13, %rdi
callq 0x42d0
movq %rax, %r12
leal 0x3f(%r14), %eax
testl %r14d, %r14d
cmovnsl %r14d, %eax
movl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rbp
movq (%r15,%rbp,8), %rdx
andl $-0x40, %eax
movl %r14d, %ecx
subl %eax, %ecx
movl $0x1, %ebx
shlq %cl, %rbx
btq %rcx, %rdx
movq 0x8(%rsp), %r15
jae 0x1a58d
movq %r15, %rdi
movl %r14d, %esi
callq 0x19ab4
testb %al, %al
je 0x1a5b2
movq 0x28(%r13), %rsi
movq %r15, %rdi
callq 0x1a170
testq %rbx, 0x178(%r15,%rbp,8)
je 0x1a5bd
movq %r15, %rdi
movl %r14d, %esi
callq 0x19e84
testb %al, %al
jne 0x1a5bd
movq %r15, %rdi
movl %r14d, %esi
callq 0x1989c
movq %r12, %r13
cmpq 0x10(%rsp), %r12
movq 0x70(%rsp), %r15
jne 0x1a53d
jmp 0x1a5e4
cmpl $-0x1, %eax
jne 0x1a5e4
cmpb $0x1, 0x17f43(%rip)     # 0x32521
je 0x1a6fc
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x45c0
cmpb $0x0, 0x17f2c(%rip)     # 0x32521
movq 0x8(%rsp), %rbx
movq 0x58(%rsp), %r12
leaq 0x17e6a(%rip), %r13     # 0x32470
jne 0x1a495
movq 0x60(%rbx), %r14
leaq 0x50(%rbx), %r12
cmpq %r12, %r14
je 0x1a638
movq %r14, %rdi
callq 0x42d0
movq %rax, %r15
movl 0x20(%r14), %esi
movq %rbx, %rdi
callq 0x1989c
movq %r15, %r14
cmpq %r12, %r15
jne 0x1a619
leaq 0x28(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x218(%rbx), %rsi
movq 0x220(%rbx), %rdx
addq %rsi, %rdx
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x6bac
leaq 0xb404(%rip), %rsi      # 0x25a6a
movq %rbx, %rdi
callq 0x4830
leaq 0xc8(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xb38e(%rip), %rsi      # 0x25a0f
leaq 0xb38c(%rip), %rdx      # 0x25a14
leaq 0xb8(%rsp), %rdi
callq 0x578e
leaq 0x17e3c(%rip), %rdi     # 0x324d8
leaq 0x18(%rsp), %rdx
leaq 0xb8(%rsp), %rcx
movl $0x1, %esi
xorl %r8d, %r8d
callq 0x20fae
movq 0xb8(%rsp), %rdi
cmpq %r15, %rdi
je 0x1a6d3
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x4460
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x1a6ea
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4460
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x30, %edi
callq 0x41d0
movq %rax, %r14
leaq 0x128(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xb33e(%rip), %rsi      # 0x25a5a
leaq 0xb346(%rip), %rdx      # 0x25a69
leaq 0x118(%rsp), %rdi
callq 0x578e
callq 0x4050
movl (%rax), %ebx
leaq 0x108(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xb32d(%rip), %rdx      # 0x25a78
leaq 0xf8(%rsp), %rdi
movq %rdx, %rsi
callq 0x578e
movb $0x1, %bpl
leaq 0x118(%rsp), %rsi
leaq 0xf8(%rsp), %rcx
movq %r14, %rdi
movl %ebx, %edx
callq 0x6542
xorl %ebp, %ebp
leaq 0x1757f(%rip), %rsi     # 0x31d00
leaq -0x13e2c(%rip), %rdx    # 0x695c
movq %r14, %rdi
callq 0x4750
movq %rax, %rbx
movq 0xf8(%rsp), %rdi
cmpq %r12, %rdi
je 0x1a7b8
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x4460
jmp 0x1a7b8
movq %rax, %rbx
movb $0x1, %bpl
movq 0x118(%rsp), %rdi
cmpq %r15, %rdi
je 0x1a89d
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x4460
jmp 0x1a89d
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r12, %rdi
je 0x1a800
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x4460
jmp 0x1a800
movq %rax, %rbx
leaq 0x148(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1a8aa
movq 0x148(%rsp), %rsi
jmp 0x1a88d
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
cmpq %r15, %rdi
je 0x1a846
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x4460
jmp 0x1a846
jmp 0x1a843
movq %rax, %rbx
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x1a8aa
movq 0x28(%rsp), %rsi
jmp 0x1a88d
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
cmpq %r14, %rdi
je 0x1a87e
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x4460
jmp 0x1a87e
jmp 0x1a87b
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x1a8aa
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x4460
jmp 0x1a8aa
movq %rax, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
je 0x1a8aa
movq %r14, %rdi
callq 0x4300
movq %rbx, %rdi
callq 0x4780