// File: /workspace/github_cpp_makefile_star2_O2/solaldunckel[P]webserv/srcs/main.cpp
// Function: main
int main(int argc, char **argv) {
  try {
    InputArgs options(argc, argv);

    options.parse();

    if (options.help()) {
      std::cout << options.helpText() << std::endl;
      return 0;
    }

    Log.setLogLevel(options.log());

    Config config(options.getPath());

    config.parse();

    if (options.test()) {
      Log.print(INFO, "# configuration file " + config.getPath() + "\n\n" + config.getFileContent());
      return 0;
    }

    Log.print(INFO, "# configuration file " + config.getPath());

    config.clear();

    Server serv(config.getServers(), options);

    serv.setup();

    if (config.getWorkers() > 0) {
      g_workers.resize(config.getWorkers());

      pthread_mutex_init(&g_accept, NULL);
	    pthread_mutex_init(&g_write, NULL);

      for (int i = 0; i < config.getWorkers(); i++) {
        g_workers[i].id_ = i + 1;
        g_workers[i].serv_ = new Server(serv);
        pthread_create(&g_workers[i].thr_, NULL, startWorker, &g_workers[i]);
        usleep(500);
      }

      for (int i = 0; i < config.getWorkers(); i++) {
        pthread_join(g_workers[i].thr_, NULL);
        delete g_workers[i].serv_;
      }

      pthread_mutex_destroy(&g_write);
      pthread_mutex_destroy(&g_accept);
    } else {
      serv.run();
    }
  }
  catch (std::exception &e) {
    Log.print(INFO, e.what(), RED, true);
    return 1;
  }
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x408, %rsp            # imm = 0x408
movq %rsi, %rdx
movl %edi, %esi
leaq 0x168(%rsp), %rdi
callq 0x7878
leaq 0x168(%rsp), %rdi
callq 0x7a3a
leaq 0x168(%rsp), %rdi
callq 0x7f72
testb %al, %al
je 0x4d53
leaq 0x1d0(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x7edc
movq 0x2628c(%rip), %rdi     # 0x2afb8
leaq 0x1d0(%rsp), %rsi
callq 0x43c0
movq %rax, %rdi
callq 0x41d0
leaq 0x1d0(%rsp), %rdi
callq 0x4898
jmp 0x50a2
leaq 0x168(%rsp), %rdi
callq 0x8018
leaq 0x26781(%rip), %rdi     # 0x2b4e8
movl %eax, %esi
callq 0x18e16
leaq 0x168(%rsp), %rdi
callq 0x7f6c
leaq 0xf0(%rsp), %rdi
movq %rax, %rsi
callq 0x5670
leaq 0xf0(%rsp), %rdi
callq 0x5bea
leaq 0x168(%rsp), %rdi
callq 0x801c
testb %al, %al
je 0x4e86
leaq 0xf0(%rsp), %rdi
callq 0x5e82
leaq 0x17243(%rip), %rsi     # 0x1c004
leaq 0xb0(%rsp), %rdi
movq %rax, %rdx
callq 0x52b1
leaq 0x1741e(%rip), %rdx     # 0x1c1f6
leaq 0x1d0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x5292
leaq 0xf0(%rsp), %rdi
callq 0x5e90
leaq 0xd0(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
movq %rax, %rdx
callq 0x5273
leaq 0x17201(%rip), %rsi     # 0x1c01a
leaq 0x90(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x5328
leaq 0x266b6(%rip), %rdi     # 0x2b4e8
pushq $0x1
popq %rsi
leaq 0xd0(%rsp), %rdx
leaq 0x90(%rsp), %rcx
xorl %r8d, %r8d
callq 0x18e1e
leaq 0x90(%rsp), %rdi
callq 0x4898
leaq 0xd0(%rsp), %rdi
callq 0x4898
leaq 0x1d0(%rsp), %rdi
callq 0x4898
leaq 0xb0(%rsp), %rdi
callq 0x4898
jmp 0x5095
leaq 0xf0(%rsp), %rdi
callq 0x5e82
leaq 0x1716a(%rip), %rsi     # 0x1c004
leaq 0x70(%rsp), %rdi
movq %rax, %rdx
callq 0x52b1
leaq 0x1716c(%rip), %rsi     # 0x1c01a
leaq 0x50(%rsp), %rdi
leaq 0x1d0(%rsp), %rdx
callq 0x5328
leaq 0x26621(%rip), %rdi     # 0x2b4e8
pushq $0x1
popq %rsi
leaq 0x70(%rsp), %rdx
leaq 0x50(%rsp), %rcx
xorl %r8d, %r8d
callq 0x18e1e
leaq 0x50(%rsp), %rdi
callq 0x4898
leaq 0x70(%rsp), %rdi
callq 0x4898
leaq 0xf0(%rsp), %rdi
callq 0x57c2
leaq 0xf0(%rsp), %rdi
callq 0x5e86
leaq 0x1d0(%rsp), %rdi
leaq 0x168(%rsp), %rdx
movq %rax, %rsi
callq 0x1323e
leaq 0x1d0(%rsp), %rdi
callq 0x1332e
leaq 0xf0(%rsp), %rdi
callq 0x5e8c
testl %eax, %eax
jle 0x5079
leaq 0xf0(%rsp), %rdi
callq 0x5e8c
movslq %eax, %rsi
leaq 0x26575(%rip), %rdi     # 0x2b4d0
callq 0x5364
leaq 0x26519(%rip), %rdi     # 0x2b480
xorl %r13d, %r13d
xorl %esi, %esi
callq 0x4650
leaq 0x26530(%rip), %rdi     # 0x2b4a8
xorl %esi, %esi
callq 0x4650
leaq 0xf0(%rsp), %rbx
leaq 0x1d0(%rsp), %r14
leaq -0x2da(%rip), %r15      # 0x4cbc
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0x5e8c
cltq
cmpq %rax, %rbp
jge 0x5001
movq 0x26522(%rip), %rax     # 0x2b4d0
leal 0x1(%rbp), %ecx
movl %ecx, 0x8(%rax,%r13)
movl $0x238, %edi            # imm = 0x238
callq 0x4460
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x130f4
movq 0x264fb(%rip), %rcx     # 0x2b4d0
leaq (%rcx,%r13), %rdi
addq $0x10, %rdi
addq %r13, %rcx
movq %r12, -0x10(%rdi)
xorl %esi, %esi
movq %r15, %rdx
callq 0x47d0
addq $0x18, %r13
incq %rbp
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x45f0
jmp 0x4f98
xorl %r15d, %r15d
leaq 0xf0(%rsp), %rbx
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x5e8c
cltq
cmpq %rax, %r12
jge 0x505f
movq 0x264ab(%rip), %rax     # 0x2b4d0
movq 0x10(%rax,%r15), %rdi
xorl %esi, %esi
callq 0x43d0
movq 0x26498(%rip), %rax     # 0x2b4d0
movq (%rax,%r15), %r14
testq %r14, %r14
je 0x5049
movq %r14, %rdi
callq 0x132fc
movl $0x238, %esi            # imm = 0x238
movq %r14, %rdi
callq 0x4490
incq %r12
addq $0x18, %r15
jmp 0x500f
leaq 0x26442(%rip), %rdi     # 0x2b4a8
callq 0x4520
leaq 0x2640e(%rip), %rdi     # 0x2b480
callq 0x4520
jmp 0x5088
leaq 0x1d0(%rsp), %rdi
xorl %esi, %esi
callq 0x14352
leaq 0x1d0(%rsp), %rdi
callq 0x132fc
leaq 0xf0(%rsp), %rdi
callq 0x578c
leaq 0x168(%rsp), %rdi
callq 0x7a20
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x408, %rsp            # imm = 0x408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r14
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x4898
jmp 0x50e0
movq %rdx, %r14
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x4898
jmp 0x511e
movq %rdx, %r14
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4898
jmp 0x5107
movq %rdx, %r14
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x5133
movq %rdx, %r14
movq %rax, %rbx
jmp 0x512b
jmp 0x515f
movq %rdx, %r14
movq %rax, %rbx
leaq 0x1d0(%rsp), %rdi
callq 0x4898
leaq 0xb0(%rsp), %rdi
callq 0x4898
jmp 0x5199
jmp 0x515f
jmp 0x5167
jmp 0x5186
jmp 0x5167
movq %rdx, %r14
movq %rax, %rbx
leaq 0x1d0(%rsp), %rdi
callq 0x4898
jmp 0x51a6
movq %rdx, %r14
movq %rax, %rbx
jmp 0x51b3
movq %rdx, %r14
movq %rax, %rbx
jmp 0x5199
movq %rdx, %r14
movq %rax, %rbx
jmp 0x51a6
movq %rdx, %r14
movq %rax, %rbx
movl $0x238, %esi            # imm = 0x238
movq %r12, %rdi
callq 0x4490
jmp 0x518c
jmp 0x5186
movq %rdx, %r14
movq %rax, %rbx
leaq 0x1d0(%rsp), %rdi
callq 0x132fc
leaq 0xf0(%rsp), %rdi
callq 0x578c
leaq 0x168(%rsp), %rdi
callq 0x7a20
cmpl $0x1, %r14d
jne 0x525e
movq %rbx, %rdi
callq 0x4180
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
leaq 0x30(%rsp), %rdi
leaq 0x1d0(%rsp), %rdx
movq %rax, %rsi
callq 0x5328
leaq 0x16e36(%rip), %rsi     # 0x1c020
leaq 0x10(%rsp), %rdi
leaq 0xf0(%rsp), %rdx
callq 0x5328
leaq 0x262e5(%rip), %rdi     # 0x2b4e8
leaq 0x30(%rsp), %rdx
leaq 0x10(%rsp), %rcx
pushq $0x1
popq %rbx
movl %ebx, %esi
movl %ebx, %r8d
callq 0x18e1e
leaq 0x10(%rsp), %rdi
callq 0x4898
leaq 0x30(%rsp), %rdi
callq 0x4898
callq 0x4710
jmp 0x50b1
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x4898
jmp 0x524a
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x4898
jmp 0x5259
movq %rax, %rbx
callq 0x4710
movq %rbx, %rdi
callq 0x4750
movq %rax, %rdi
callq 0x539c