// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movl %esi, (%rdi)
leaq 0x218(%rdi), %r14
leaq 0x867e(%rip), %rsi      # 0x1c9f4
movq %r14, %rdi
callq 0x4830
movl (%r15), %esi
testl %esi, %esi
jle 0x143f1
leaq 0xf8(%rsp), %rbx
movq %rbx, %rdi
callq 0x6118
leaq 0x81fa(%rip), %rsi      # 0x1c596
leaq 0x38(%rsp), %rdi
movq %rbx, %rdx
callq 0x8234
leaq 0x864e(%rip), %rdx      # 0x1c9fe
leaq 0x58(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x5292
leaq 0x58(%rsp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4560
movq %rbx, %rdi
callq 0x4898
leaq 0x38(%rsp), %rdi
callq 0x4898
leaq 0xf8(%rsp), %rdi
callq 0x4898
movl (%r15), %esi
cmpl $0x1, %esi
ja 0x14413
leaq 0x3fb(%rip), %rbx       # 0x147f8
pushq $0x2
popq %rdi
movq %rbx, %rsi
callq 0x4530
pushq $0x3
popq %rdi
movq %rbx, %rsi
callq 0x4530
movb $0x1, 0x17117(%rip)     # 0x2b531
leaq 0x85e2(%rip), %rdx      # 0x1ca03
leaq 0xd8(%rsp), %rdi
movq %r14, %rsi
callq 0x60d3
leaq 0x8589(%rip), %rsi      # 0x1c9c1
leaq 0xb8(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x5328
movq %r14, 0x18(%rsp)
leaq 0x17092(%rip), %rdi     # 0x2b4e8
pushq $0x1
popq %rsi
leaq 0xd8(%rsp), %rdx
leaq 0xb8(%rsp), %rcx
xorl %r8d, %r8d
callq 0x18e1e
leaq 0xb8(%rsp), %rdi
callq 0x4898
leaq 0xd8(%rsp), %rdi
callq 0x4898
leaq 0x58(%rsp), %rax
movq $0x1, (%rax)
andq $0x0, 0x8(%rax)
leaq 0x78(%r15), %rax
movq %rax, 0x30(%rsp)
leaq 0xf8(%r15), %rax
movq %rax, (%rsp)
leaq 0x178(%r15), %rax
movq %rax, 0x28(%rsp)
leaq 0x20(%r15), %r14
leaq 0x50(%r15), %rax
movq %rax, 0x10(%rsp)
leaq 0x16fb0(%rip), %r12     # 0x2b480
movq %r15, 0x8(%rsp)
movq %r14, 0x20(%rsp)
movq 0x8(%rsp), %r13
cmpb $0x1, 0x1704b(%rip)     # 0x2b531
jne 0x14641
pushq $0x10
popq %r8
movq %r8, %rcx
movq (%rsp), %rdx
movq %rdx, %rdi
movq 0x30(%rsp), %rax
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %r8, %rcx
movq 0x28(%rsp), %r8
movq %r8, %rdi
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl 0x210(%r15), %edi
incl %edi
movq %rdx, %rsi
movq %r8, %rdx
xorl %ecx, %ecx
leaq 0x58(%rsp), %r8
callq 0x4440
testl %eax, %eax
js 0x1460f
movq 0x30(%r15), %rbx
cmpq %r14, %rbx
je 0x14583
movl 0x20(%rbx), %eax
cltd
pushq $0x40
popq %rcx
idivl %ecx
cltq
movq (%rsp), %rcx
movq (%rcx,%rax,8), %rax
btq %rdx, %rax
jae 0x14576
movq %r12, %rdi
callq 0x4630
movl 0x20(%rbx), %esi
movq %r15, %rdi
callq 0x13ac8
movq %r12, %rdi
callq 0x4350
movq %rbx, %rdi
callq 0x42e0
movq %rax, %rbx
jmp 0x1453d
movq 0x60(%r15), %r12
cmpq 0x10(%rsp), %r12
je 0x14621
movl 0x20(%r12), %ebx
movq %r12, %rdi
callq 0x42e0
movq %rax, %rbp
movl %ebx, %eax
cltd
pushq $0x40
popq %rcx
idivl %ecx
movl %edx, %ecx
movslq %eax, %r15
movq (%rsp), %rax
movq (%rax,%r15,8), %rax
pushq $0x1
popq %r14
shlq %cl, %r14
btq %rcx, %rax
jae 0x145d2
movq %r13, %rdi
movl %ebx, %esi
callq 0x13efa
testb %al, %al
je 0x145fd
movq 0x28(%r12), %rsi
movq %r13, %rdi
callq 0x142f6
movq %rbp, %r12
testq %r14, 0x178(%r13,%r15,8)
je 0x14587
movq %r13, %rdi
movl %ebx, %esi
callq 0x1414c
movq %rbp, %r12
testb %al, %al
jne 0x14587
movq %r13, %rdi
movl %ebx, %esi
callq 0x13da6
movq %rbp, %r12
jmp 0x14587
cmpl $-0x1, %eax
jne 0x14621
cmpb $0x1, 0x16f16(%rip)     # 0x2b531
je 0x146e1
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x45f0
movq 0x8(%rsp), %r15
movq 0x20(%rsp), %r14
leaq 0x16e44(%rip), %r12     # 0x2b480
jmp 0x144df
movq 0x60(%r15), %rbx
movq 0x18(%rsp), %r14
cmpq 0x10(%rsp), %rbx
je 0x1466c
movq %rbx, %rdi
callq 0x42e0
movq %rax, %r12
movl 0x20(%rbx), %esi
movq %r15, %rdi
callq 0x13da6
movq %r12, %rbx
jmp 0x1464a
leaq 0x83a9(%rip), %rdx      # 0x1ca1c
leaq 0x98(%rsp), %rdi
movq %r14, %rsi
callq 0x60d3
leaq 0x8337(%rip), %rsi      # 0x1c9c1
leaq 0x78(%rsp), %rdi
leaq 0x38(%rsp), %rdx
callq 0x5328
leaq 0x16e48(%rip), %rdi     # 0x2b4e8
pushq $0x1
popq %rsi
leaq 0x98(%rsp), %rdx
leaq 0x78(%rsp), %rcx
xorl %r8d, %r8d
callq 0x18e1e
leaq 0x78(%rsp), %rdi
callq 0x4898
leaq 0x98(%rsp), %rdi
callq 0x4898
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x30
popq %rdi
callq 0x41e0
movq %rax, %r14
leaq 0x8319(%rip), %rsi      # 0x1ca0c
leaq 0x138(%rsp), %rdi
leaq 0x38(%rsp), %rdx
callq 0x5328
callq 0x4040
movl (%rax), %ebx
leaq 0x8317(%rip), %rsi      # 0x1ca2a
leaq 0x118(%rsp), %rdi
leaq 0xf8(%rsp), %rdx
callq 0x5328
movb $0x1, %bpl
leaq 0x138(%rsp), %rsi
leaq 0x118(%rsp), %rcx
movq %r14, %rdi
movl %ebx, %edx
callq 0x5e96
xorl %ebp, %ebp
leaq 0x165c2(%rip), %rsi     # 0x2ad10
leaq -0xe703(%rip), %rdx     # 0x6052
movq %r14, %rdi
callq 0x4730
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x4898
jmp 0x1476f
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
jmp 0x147b2
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x4898
jmp 0x1478b
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
jmp 0x147b2
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x4898
jmp 0x147aa
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x4898
jmp 0x147f0
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x4898
jmp 0x147d1
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x138(%rsp), %rdi
callq 0x4898
testb %bpl, %bpl
jne 0x147e8
jmp 0x147f0
movq %rax, %rbx
movq %r14, %rdi
callq 0x4310
movq %rbx, %rdi
callq 0x4750