// File: /workspace/github_cpp_makefile_star2_O3/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 0xb7cf(%rip), %rcx      # 0x25a42
movl $0x9, %r8d
movq %r14, %rdi
xorl %esi, %esi
callq 0x4690
movl (%rbx), %esi
testl %esi, %esi
movq %rbx, 0x8(%rsp)
jle 0x1a3d5
leaq 0x138(%rsp), %r15
movq %r15, %rdi
callq 0x6a8a
leaq 0xb303(%rip), %rcx      # 0x255ac
movl $0x7, %r8d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x44d0
leaq 0x88(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x1a2e9
movq %rdx, 0x78(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x88(%rsp)
jmp 0x1a2f1
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 0xb738(%rip), %rsi      # 0x25a4c
callq 0x4840
leaq 0xa8(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x1a349
movq %rdx, 0x98(%rsp)
movq (%rcx), %rdx
movq %rdx, 0xa8(%rsp)
jmp 0x1a351
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 0x4570
movq (%r15), %rdi
cmpq %r13, %rdi
je 0x1a393
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x4470
movq 0x78(%rsp), %rdi
cmpq %r12, %rdi
movq 0x8(%rsp), %rbx
je 0x1a3b2
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x4470
leaq 0x148(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1a3d3
movq 0x148(%rsp), %rsi
incq %rsi
callq 0x4470
movl (%rbx), %esi
cmpl $0x1, %esi
ja 0x1a3fb
leaq 0x56d(%rip), %r14       # 0x1a94e
movl $0x2, %edi
movq %r14, %rsi
callq 0x4540
movl $0x3, %edi
movq %r14, %rsi
callq 0x4540
movb $0x1, 0x1812f(%rip)     # 0x32531
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 0x6c2a
leaq 0xb621(%rip), %rsi      # 0x25a51
movq %r14, %rdi
callq 0x4840
leaq 0x108(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0xb5c4(%rip), %rsi      # 0x25a0f
leaq 0xb5c2(%rip), %rdx      # 0x25a14
leaq 0xf8(%rsp), %rdi
callq 0x579e
leaq 0x18082(%rip), %rdi     # 0x324e8
leaq 0x38(%rsp), %rdx
leaq 0xf8(%rsp), %rcx
movl $0x1, %esi
xorl %r8d, %r8d
callq 0x20f32
movq 0xf8(%rsp), %rdi
cmpq %r14, %rdi
je 0x1a49d
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x4470
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x1a4b4
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x4470
movq $0x1, 0x98(%rsp)
movq $0x0, 0xa0(%rsp)
cmpb $0x1, 0x1805e(%rip)     # 0x32531
jne 0x1a68a
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 0x17f77(%rip), %r13     # 0x32480
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 0x4420
testl %eax, %eax
js 0x1a650
movq 0x30(%rbx), %r14
cmpq %r12, %r14
je 0x1a5ac
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 0x1a59c
movq %r13, %rdi
callq 0x4610
movl 0x20(%r14), %esi
movq %rbx, %rdi
callq 0x19308
movq %r13, %rdi
callq 0x4350
movq %r14, %rdi
callq 0x42e0
movq %rax, %r14
cmpq %r12, %rax
jne 0x1a564
movq 0x60(%rbx), %r13
cmpq 0x10(%rsp), %r13
je 0x1a662
movl 0x20(%r13), %r14d
movq %r13, %rdi
callq 0x42e0
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 0x1a60b
movq %r15, %rdi
movl %r14d, %esi
callq 0x19b32
testb %al, %al
je 0x1a630
movq 0x28(%r13), %rsi
movq %r15, %rdi
callq 0x1a1ec
testq %rbx, 0x178(%r15,%rbp,8)
je 0x1a63b
movq %r15, %rdi
movl %r14d, %esi
callq 0x19f00
testb %al, %al
jne 0x1a63b
movq %r15, %rdi
movl %r14d, %esi
callq 0x19926
movq %r12, %r13
cmpq 0x10(%rsp), %r12
movq 0x70(%rsp), %r15
jne 0x1a5bb
jmp 0x1a662
cmpl $-0x1, %eax
jne 0x1a662
cmpb $0x1, 0x17ed5(%rip)     # 0x32531
je 0x1a77a
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x45d0
cmpb $0x0, 0x17ebe(%rip)     # 0x32531
movq 0x8(%rsp), %rbx
movq 0x58(%rsp), %r12
leaq 0x17dfc(%rip), %r13     # 0x32480
jne 0x1a513
movq 0x60(%rbx), %r14
leaq 0x50(%rbx), %r12
cmpq %r12, %r14
je 0x1a6b6
movq %r14, %rdi
callq 0x42e0
movq %rax, %r15
movl 0x20(%r14), %esi
movq %rbx, %rdi
callq 0x19926
movq %r15, %r14
cmpq %r12, %r15
jne 0x1a697
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 0x6c2a
leaq 0xb386(%rip), %rsi      # 0x25a6a
movq %rbx, %rdi
callq 0x4840
leaq 0xc8(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xb310(%rip), %rsi      # 0x25a0f
leaq 0xb30e(%rip), %rdx      # 0x25a14
leaq 0xb8(%rsp), %rdi
callq 0x579e
leaq 0x17dce(%rip), %rdi     # 0x324e8
leaq 0x18(%rsp), %rdx
leaq 0xb8(%rsp), %rcx
movl $0x1, %esi
xorl %r8d, %r8d
callq 0x20f32
movq 0xb8(%rsp), %rdi
cmpq %r15, %rdi
je 0x1a751
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x4470
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x1a768
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4470
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 0xe8(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0xb2c0(%rip), %rsi      # 0x25a5a
leaq 0xb2c8(%rip), %rdx      # 0x25a69
leaq 0xd8(%rsp), %rdi
callq 0x579e
callq 0x4050
movl (%rax), %ebx
leaq 0x128(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0xb2af(%rip), %rdx      # 0x25a78
leaq 0x118(%rsp), %rdi
movq %rdx, %rsi
callq 0x579e
movb $0x1, %bpl
leaq 0xd8(%rsp), %rsi
leaq 0x118(%rsp), %rcx
movq %r14, %rdi
movl %ebx, %edx
callq 0x65b0
xorl %ebp, %ebp
leaq 0x17501(%rip), %rsi     # 0x31d00
leaq -0x13e3e(%rip), %rdx    # 0x69c8
movq %r14, %rdi
callq 0x4760
movq %rax, %rbx
movq 0x118(%rsp), %rdi
cmpq %r12, %rdi
je 0x1a82e
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x4470
movq 0xd8(%rsp), %rdi
cmpq %r15, %rdi
je 0x1a84b
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x4470
testb %bpl, %bpl
jne 0x1a93e
jmp 0x1a946
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
cmpq %r15, %rdi
je 0x1a93e
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x4470
jmp 0x1a93e
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r12, %rdi
je 0x1a8a4
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x4470
jmp 0x1a8a4
movq %rax, %rbx
leaq 0x148(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1a946
movq 0x148(%rsp), %rsi
jmp 0x1a931
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
cmpq %r15, %rdi
je 0x1a8ea
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x4470
jmp 0x1a8ea
jmp 0x1a8e7
movq %rax, %rbx
movq 0x18(%rsp), %rdi
cmpq %r14, %rdi
je 0x1a946
movq 0x28(%rsp), %rsi
jmp 0x1a931
movq %rax, %rbx
movq 0xf8(%rsp), %rdi
cmpq %r14, %rdi
je 0x1a922
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x4470
jmp 0x1a922
jmp 0x1a91f
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
je 0x1a946
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x4470
jmp 0x1a946
movq %rax, %rbx
movq %r14, %rdi
callq 0x4310
movq %rbx, %rdi
callq 0x4790