// File: 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
movq %rsp, %rbp
subq $0x540, %rsp            # imm = 0x540
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x8(%rbp), %esi
movq -0x10(%rbp), %rdx
leaq -0x78(%rbp), %rdi
callq 0xf2a0
jmp 0x60cb
leaq -0x78(%rbp), %rdi
callq 0xf750
jmp 0x60d6
leaq -0x78(%rbp), %rdi
callq 0x10030
movb %al, -0x48a(%rbp)
jmp 0x60e7
movb -0x48a(%rbp), %al
testb $0x1, %al
jne 0x60f6
jmp 0x61a3
leaq -0xa8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xff00
jmp 0x6108
movq 0x55e99(%rip), %rdi     # 0x5bfa8
leaq -0xa8(%rbp), %rsi
callq 0x5490
movq %rax, -0x498(%rbp)
jmp 0x6124
movq -0x498(%rbp), %rdi
movq 0x55e4e(%rip), %rsi     # 0x5bf80
callq 0x55c0
jmp 0x6139
leaq -0xa8(%rbp), %rdi
callq 0x5aa8
movl $0x0, -0x4(%rbp)
movl $0x1, -0xac(%rbp)
jmp 0x67d6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x6816
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x680d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x5aa8
jmp 0x680d
leaq -0x78(%rbp), %rdi
callq 0x101e0
movl %eax, -0x49c(%rbp)
jmp 0x61b4
movl -0x49c(%rbp), %esi
leaq 0x56437(%rip), %rdi     # 0x5c5f8
callq 0x3b210
jmp 0x61c8
leaq -0x78(%rbp), %rdi
callq 0x10010
movq %rax, -0x4a8(%rbp)
jmp 0x61da
movq -0x4a8(%rbp), %rsi
leaq -0x128(%rbp), %rdi
callq 0x7c60
jmp 0x61ef
leaq -0x128(%rbp), %rdi
callq 0x85c0
jmp 0x61fd
leaq -0x78(%rbp), %rdi
callq 0x10200
movb %al, -0x4a9(%rbp)
jmp 0x620e
movb -0x4a9(%rbp), %al
testb $0x1, %al
jne 0x621d
jmp 0x63d7
leaq -0x128(%rbp), %rdi
callq 0x8ad0
movq %rax, -0x4b8(%rbp)
jmp 0x6232
movq -0x4b8(%rbp), %rdx
leaq 0x3cdc4(%rip), %rsi     # 0x43004
leaq -0x188(%rbp), %rdi
callq 0x6ad0
jmp 0x624e
leaq 0x3cfa1(%rip), %rdx     # 0x431f6
leaq -0x168(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x6a80
jmp 0x626a
leaq -0x128(%rbp), %rdi
callq 0x8b20
movq %rax, -0x4c0(%rbp)
jmp 0x627f
movq -0x4c0(%rbp), %rdx
leaq -0x148(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x6a30
jmp 0x629b
leaq -0x1a9(%rbp), %rdi
movq %rdi, -0x4c8(%rbp)
callq 0x5960
movq -0x4c8(%rbp), %rdx
leaq 0x3cd5e(%rip), %rsi     # 0x4301a
leaq -0x1a8(%rbp), %rdi
callq 0x6be0
jmp 0x62ca
leaq 0x56327(%rip), %rdi     # 0x5c5f8
movl $0x1, %esi
leaq -0x148(%rbp), %rdx
leaq -0x1a8(%rbp), %rcx
xorl %r8d, %r8d
callq 0x3b240
jmp 0x62ee
leaq -0x1a8(%rbp), %rdi
callq 0x5aa8
leaq -0x1a9(%rbp), %rdi
callq 0x55e0
leaq -0x148(%rbp), %rdi
callq 0x5aa8
leaq -0x168(%rbp), %rdi
callq 0x5aa8
leaq -0x188(%rbp), %rdi
callq 0x5aa8
movl $0x0, -0x4(%rbp)
movl $0x1, -0xac(%rbp)
jmp 0x67ca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x6801
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x63c6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x63ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x63a2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x1a8(%rbp), %rdi
callq 0x5aa8
leaq -0x1a9(%rbp), %rdi
callq 0x55e0
leaq -0x148(%rbp), %rdi
callq 0x5aa8
leaq -0x168(%rbp), %rdi
callq 0x5aa8
leaq -0x188(%rbp), %rdi
callq 0x5aa8
jmp 0x6801
leaq -0x128(%rbp), %rdi
callq 0x8ad0
movq %rax, -0x4d0(%rbp)
jmp 0x63ec
movq -0x4d0(%rbp), %rdx
leaq 0x3cc0a(%rip), %rsi     # 0x43004
leaq -0x1d0(%rbp), %rdi
callq 0x6ad0
jmp 0x6408
leaq -0x1f1(%rbp), %rdi
movq %rdi, -0x4d8(%rbp)
callq 0x5960
movq -0x4d8(%rbp), %rdx
leaq 0x3cbf1(%rip), %rsi     # 0x4301a
leaq -0x1f0(%rbp), %rdi
callq 0x6be0
jmp 0x6437
leaq 0x561ba(%rip), %rdi     # 0x5c5f8
movl $0x1, %esi
leaq -0x1d0(%rbp), %rdx
leaq -0x1f0(%rbp), %rcx
xorl %r8d, %r8d
callq 0x3b240
jmp 0x645b
leaq -0x1f0(%rbp), %rdi
callq 0x5aa8
leaq -0x1f1(%rbp), %rdi
callq 0x55e0
leaq -0x1d0(%rbp), %rdi
callq 0x5aa8
leaq -0x128(%rbp), %rdi
callq 0x7e90
jmp 0x648d
leaq -0x128(%rbp), %rdi
callq 0x8ae0
movq %rax, -0x4e0(%rbp)
jmp 0x64a2
movq -0x4e0(%rbp), %rsi
leaq -0x430(%rbp), %rdi
leaq -0x78(%rbp), %rdx
callq 0x2bc30
jmp 0x64bb
leaq -0x430(%rbp), %rdi
callq 0x2bdb0
jmp 0x64c9
leaq -0x128(%rbp), %rdi
callq 0x8b00
movl %eax, -0x4e4(%rbp)
jmp 0x64dd
movl -0x4e4(%rbp), %eax
cmpl $0x0, %eax
jle 0x67a2
leaq -0x128(%rbp), %rdi
callq 0x8b00
movl %eax, -0x4e8(%rbp)
jmp 0x6500
movl -0x4e8(%rbp), %eax
movslq %eax, %rsi
leaq 0x560d0(%rip), %rdi     # 0x5c5e0
callq 0x6c90
jmp 0x6517
leaq 0x56072(%rip), %rdi     # 0x5c590
xorl %eax, %eax
movl %eax, %esi
callq 0x5820
leaq 0x5608a(%rip), %rdi     # 0x5c5b8
xorl %eax, %eax
movl %eax, %esi
callq 0x5820
movl $0x0, -0x434(%rbp)
movl -0x434(%rbp), %eax
movl %eax, -0x4f0(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x8b00
movl %eax, -0x4ec(%rbp)
jmp 0x6561
movl -0x4f0(%rbp), %eax
movl -0x4ec(%rbp), %ecx
cmpl %ecx, %eax
jge 0x66d6
movslq -0x434(%rbp), %rsi
movl %esi, %eax
incl %eax
movl %eax, -0x4fc(%rbp)
leaq 0x56053(%rip), %rdi     # 0x5c5e0
callq 0x6d20
movl -0x4fc(%rbp), %ecx
movl %ecx, 0x8(%rax)
movl $0x238, %edi            # imm = 0x238
callq 0x5550
movq %rax, -0x4f8(%rbp)
jmp 0x65ae
movq -0x4f8(%rbp), %rdi
leaq -0x430(%rbp), %rsi
callq 0x2baa0
jmp 0x65c3
movslq -0x434(%rbp), %rsi
leaq 0x5600f(%rip), %rdi     # 0x5c5e0
movq %rdi, -0x510(%rbp)
callq 0x6d20
movq -0x4f8(%rbp), %rcx
movq -0x510(%rbp), %rdi
movq %rcx, (%rax)
movslq -0x434(%rbp), %rsi
callq 0x6d20
movq -0x510(%rbp), %rdi
addq $0x10, %rax
movq %rax, -0x508(%rbp)
movslq -0x434(%rbp), %rsi
callq 0x6d20
movq -0x508(%rbp), %rdi
movq %rax, %rcx
leaq -0x5c9(%rip), %rdx      # 0x6060
xorl %eax, %eax
movl %eax, %esi
callq 0x59e0
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x57c0
jmp 0x663e
jmp 0x6640
movl -0x434(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x434(%rbp)
jmp 0x6541
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x6680
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x1f0(%rbp), %rdi
callq 0x5aa8
leaq -0x1f1(%rbp), %rdi
callq 0x55e0
leaq -0x1d0(%rbp), %rdi
callq 0x5aa8
jmp 0x6801
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x67f5
movq -0x4f8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
movl $0x238, %esi            # imm = 0x238
callq 0x5580
jmp 0x67f5
movl $0x0, -0x438(%rbp)
movl -0x438(%rbp), %eax
movl %eax, -0x518(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x8b00
movl %eax, -0x514(%rbp)
jmp 0x6700
movl -0x518(%rbp), %eax
movl -0x514(%rbp), %ecx
cmpl %ecx, %eax
jge 0x6788
movslq -0x438(%rbp), %rsi
leaq 0x55ec2(%rip), %rdi     # 0x5c5e0
callq 0x6d20
movq 0x10(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x54a0
jmp 0x6732
movslq -0x438(%rbp), %rsi
leaq 0x55ea0(%rip), %rdi     # 0x5c5e0
callq 0x6d20
movq (%rax), %rax
movq %rax, -0x520(%rbp)
cmpq $0x0, %rax
je 0x6772
movq -0x520(%rbp), %rdi
callq 0x2bd60
movq -0x520(%rbp), %rdi
movl $0x238, %esi            # imm = 0x238
callq 0x5580
jmp 0x6774
movl -0x438(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x438(%rbp)
jmp 0x66e0
leaq 0x55e29(%rip), %rdi     # 0x5c5b8
callq 0x5690
leaq 0x55df5(%rip), %rdi     # 0x5c590
callq 0x5690
jmp 0x67b4
leaq -0x430(%rbp), %rdi
xorl %esi, %esi
callq 0x2d950
jmp 0x67b2
jmp 0x67b4
leaq -0x430(%rbp), %rdi
callq 0x2bd60
movl $0x0, -0xac(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x7e30
leaq -0x78(%rbp), %rdi
callq 0xf720
movl -0xac(%rbp), %eax
testl %eax, %eax
je 0x67f0
jmp 0x67eb
jmp 0x698c
jmp 0x6985
leaq -0x430(%rbp), %rdi
callq 0x2bd60
leaq -0x128(%rbp), %rdi
callq 0x7e30
leaq -0x78(%rbp), %rdi
callq 0xf720
movl -0x84(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x6998
movq -0x80(%rbp), %rdi
callq 0x51e0
movq %rax, -0x440(%rbp)
movq -0x440(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq %rax, -0x530(%rbp)
leaq -0x461(%rbp), %rdi
movq %rdi, -0x528(%rbp)
callq 0x5960
movq -0x530(%rbp), %rsi
movq -0x528(%rbp), %rdx
leaq -0x460(%rbp), %rdi
callq 0x6be0
jmp 0x687f
leaq -0x489(%rbp), %rdi
movq %rdi, -0x538(%rbp)
callq 0x5960
movq -0x538(%rbp), %rdx
leaq 0x3c780(%rip), %rsi     # 0x43020
leaq -0x488(%rbp), %rdi
callq 0x6be0
jmp 0x68ae
leaq 0x55d43(%rip), %rdi     # 0x5c5f8
leaq -0x460(%rbp), %rdx
leaq -0x488(%rbp), %rcx
movl $0x1, %r8d
movl %r8d, %esi
callq 0x3b240
jmp 0x68d3
leaq -0x488(%rbp), %rdi
callq 0x5aa8
leaq -0x489(%rbp), %rdi
callq 0x55e0
leaq -0x460(%rbp), %rdi
callq 0x5aa8
leaq -0x461(%rbp), %rdi
callq 0x55e0
movl $0x1, -0x4(%rbp)
movl $0x1, -0xac(%rbp)
callq 0x58f0
jmp 0x698c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x6970
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x6958
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x488(%rbp), %rdi
callq 0x5aa8
leaq -0x489(%rbp), %rdi
callq 0x55e0
leaq -0x460(%rbp), %rdi
callq 0x5aa8
leaq -0x461(%rbp), %rdi
callq 0x55e0
callq 0x58f0
jmp 0x6983
jmp 0x6998
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x540, %rsp            # imm = 0x540
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x5950
movq %rax, %rdi
callq 0x6d40
nopl (%rax)