// File: UTAP[P]APHTTP/server/server.cpp
// Function: Server::run()
void Server::run() {
  ::listen(sc, 10);

  struct sockaddr_in cli_addr;
  socklen_t clilen;
  clilen = sizeof(cli_addr);
  SOCKET newsc;

  while (true) {
    newsc = ::accept(sc, (struct sockaddr *)&cli_addr, &clilen);
    if (!ISVALIDSOCKET(newsc))
      throw Exception("Error on accept: " + string(getSocketError()));
    Response *res = NULL;
    try {
      char* data = new char[BUFSIZE + 1];
      size_t recv_len, recv_total_len = 0;
      Request *req = NULL;
      while (!req) {
        recv_len =
            recv(newsc, data + recv_total_len, BUFSIZE - recv_total_len, 0);
        if (recv_len > 0) {
          recv_total_len += recv_len;
          data[recv_total_len >= 0 ? recv_total_len : 0] = 0;
          req = parseRawReq(data, recv_total_len);
        } else
          break;
      }
      delete[] data;
      if (!recv_total_len) {
        CLOSESOCKET(newsc);
        continue;
      }
      req->log();
      size_t i = 0;
      for (; i < routes.size(); i++) {
        if (routes[i]->isMatch(req->getMethod(), req->getPath())) {
          res = routes[i]->handle(req);
          break;
        }
      }
      if (i == routes.size() && notFoundHandler) {
        res = notFoundHandler->callback(req);
      }
      delete req;
    } catch (const Exception& exc) {
      delete res;
      res = ServerErrorHandler::callback(exc.getMessage());
    }
    int si;
    res->log();
    string res_data = res->print(si);
    delete res;
    int wr = send(newsc, res_data.c_str(), si, 0);
    if (wr != si)
      throw Exception("Send error: " + string(getSocketError()));
    CLOSESOCKET(newsc);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x230, %rsp            # imm = 0x230
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x168(%rbp)
movl (%rax), %edi
movl $0xa, %esi
callq 0x4470
movl $0x10, -0x1c(%rbp)
movq -0x168(%rbp), %rax
movl (%rax), %edi
leaq -0x18(%rbp), %rsi
leaq -0x1c(%rbp), %rdx
callq 0x4180
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jge 0x1ac51
movb $0x1, -0x6e(%rbp)
movl $0x28, %edi
callq 0x4250
movq %rax, -0x178(%rbp)
callq 0x1a940
movq %rax, -0x170(%rbp)
jmp 0x1ab73
leaq -0x6d(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq 0x46c0
movq -0x170(%rbp), %rsi
movq -0x180(%rbp), %rdx
leaq -0x60(%rbp), %rdi
callq 0x4550
jmp 0x1ab9c
leaq 0x6d78(%rip), %rsi      # 0x2191b
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0x9a70
jmp 0x1abb2
movq -0x178(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x1b360
jmp 0x1abc4
movq -0x178(%rbp), %rdi
movb $0x0, -0x6e(%rbp)
leaq 0x11ef2(%rip), %rsi     # 0x2cac8
leaq -0x111cd(%rip), %rdx    # 0x9a10
callq 0x4680
jmp 0x1b259
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x1ac38
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x1ac2f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x1ac26
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x4768
leaq -0x60(%rbp), %rdi
callq 0x4768
leaq -0x6d(%rbp), %rdi
callq 0x4440
testb $0x1, -0x6e(%rbp)
jne 0x1ac40
jmp 0x1ac4c
movq -0x178(%rbp), %rdi
callq 0x4310
jmp 0x1b248
movq $0x0, -0x78(%rbp)
movl $0x3f4001, %edi         # imm = 0x3F4001
callq 0x4030
movq %rax, -0x188(%rbp)
jmp 0x1ac6c
movq -0x188(%rbp), %rax
movq %rax, -0x80(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
cmpq $0x0, -0x98(%rbp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x1aca3
jmp 0x1ad80
movl -0x20(%rbp), %edi
movq -0x80(%rbp), %rsi
movq -0x90(%rbp), %rax
addq %rax, %rsi
movl $0x3f4000, %edx         # imm = 0x3F4000
subq %rax, %rdx
xorl %ecx, %ecx
callq 0x4190
movq %rax, -0x190(%rbp)
jmp 0x1accc
movq -0x190(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
jbe 0x1ad79
movq -0x88(%rbp), %rax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x198(%rbp)
cmpq $0x0, -0x90(%rbp)
jb 0x1ad22
movq -0x90(%rbp), %rax
movq %rax, -0x1a0(%rbp)
jmp 0x1ad2d
xorl %eax, %eax
movq %rax, -0x1a0(%rbp)
jmp 0x1ad2d
movq -0x198(%rbp), %rax
movq -0x1a0(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x80(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x16ca0
movq %rax, -0x1a8(%rbp)
jmp 0x1ad58
movq -0x1a8(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x1ad7b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x1aedd
jmp 0x1ad80
jmp 0x1ac8d
movq -0x80(%rbp), %rax
movq %rax, -0x1b0(%rbp)
cmpq $0x0, %rax
je 0x1ad9d
movq -0x1b0(%rbp), %rdi
callq 0x44f0
cmpq $0x0, -0x90(%rbp)
jne 0x1adb6
movl -0x20(%rbp), %edi
callq 0x4720
jmp 0x1adb1
jmp 0x1ab2d
movq -0x98(%rbp), %rdi
callq 0x11010
jmp 0x1adc4
movq $0x0, -0xa0(%rbp)
movq -0x168(%rbp), %rdi
movq -0xa0(%rbp), %rax
movq %rax, -0x1b8(%rbp)
addq $0x8, %rdi
callq 0x1c030
movq %rax, %rcx
movq -0x1b8(%rbp), %rax
cmpq %rcx, %rax
jae 0x1b0d0
movq -0x168(%rbp), %rdi
addq $0x8, %rdi
movq -0xa0(%rbp), %rsi
callq 0x1c050
movq (%rax), %rax
movq %rax, -0x1c8(%rbp)
movq -0x98(%rbp), %rdi
callq 0x106c0
movl %eax, -0x1bc(%rbp)
jmp 0x1ae35
movq -0x98(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x10670
jmp 0x1ae4a
movl -0x1bc(%rbp), %esi
movq -0x1c8(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
callq 0x1da40
movb %al, -0x1c9(%rbp)
jmp 0x1ae6b
leaq -0xc0(%rbp), %rdi
callq 0x4768
movb -0x1c9(%rbp), %al
testb $0x1, %al
jne 0x1ae86
jmp 0x1b0b7
movq -0x168(%rbp), %rdi
addq $0x8, %rdi
movq -0xa0(%rbp), %rsi
callq 0x1c050
movq (%rax), %rdi
movq -0x98(%rbp), %rsi
callq 0x1daa0
movq %rax, -0x1d8(%rbp)
jmp 0x1aeb5
movq -0x1d8(%rbp), %rax
movq %rax, -0x78(%rbp)
jmp 0x1b0d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x4768
movl -0x6c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x1b248
movq -0x68(%rbp), %rdi
callq 0x41e0
movq %rax, -0xc8(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x1e0(%rbp)
cmpq $0x0, %rax
je 0x1af26
movq -0x1e0(%rbp), %rdi
callq 0x1c0c0
movq -0x1e0(%rbp), %rdi
callq 0x43a0
movq -0xc8(%rbp), %rsi
leaq -0xe8(%rbp), %rdi
callq 0x1b260
jmp 0x1af3b
leaq -0xe8(%rbp), %rdi
callq 0x1c100
movq %rax, -0x1e8(%rbp)
jmp 0x1af50
movq -0x1e8(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x4768
callq 0x4640
movq -0x78(%rbp), %rdi
xorl %esi, %esi
callq 0xbc20
movq -0x78(%rbp), %rsi
leaq -0x110(%rbp), %rdi
leaq -0xec(%rbp), %rdx
callq 0xb6b0
movq -0x78(%rbp), %rax
movq %rax, -0x1f0(%rbp)
cmpq $0x0, %rax
je 0x1afb7
movq -0x1f0(%rbp), %rdi
callq 0x1c0c0
movq -0x1f0(%rbp), %rdi
callq 0x43a0
movl -0x20(%rbp), %eax
movl %eax, -0x1fc(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x41f0
movl -0x1fc(%rbp), %edi
movq %rax, %rsi
movslq -0xec(%rbp), %rdx
xorl %ecx, %ecx
callq 0x4260
movq %rax, -0x1f8(%rbp)
jmp 0x1afec
movq -0x1f8(%rbp), %rax
movl %eax, -0x114(%rbp)
movl -0x114(%rbp), %eax
cmpl -0xec(%rbp), %eax
je 0x1b221
movb $0x1, -0x15a(%rbp)
movl $0x28, %edi
callq 0x4250
movq %rax, -0x210(%rbp)
callq 0x1a940
movq %rax, -0x208(%rbp)
jmp 0x1b031
leaq -0x159(%rbp), %rdi
movq %rdi, -0x218(%rbp)
callq 0x46c0
movq -0x208(%rbp), %rsi
movq -0x218(%rbp), %rdx
leaq -0x158(%rbp), %rdi
callq 0x4550
jmp 0x1b060
leaq 0x68c6(%rip), %rsi      # 0x2192d
leaq -0x138(%rbp), %rdi
leaq -0x158(%rbp), %rdx
callq 0x9a70
jmp 0x1b07c
movq -0x210(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x1b360
jmp 0x1b091
movq -0x210(%rbp), %rdi
movb $0x0, -0x15a(%rbp)
leaq 0x11a22(%rip), %rsi     # 0x2cac8
leaq -0x1169d(%rip), %rdx    # 0x9a10
callq 0x4680
jmp 0x1b259
jmp 0x1b0b9
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0x1adcf
movq -0x168(%rbp), %rdi
movq -0xa0(%rbp), %rax
movq %rax, -0x220(%rbp)
addq $0x8, %rdi
callq 0x1c030
movq %rax, %rcx
movq -0x220(%rbp), %rax
cmpq %rcx, %rax
jne 0x1b13a
movq -0x168(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x1b13a
movq -0x168(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x98(%rbp), %rsi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq %rax, -0x228(%rbp)
jmp 0x1b12f
movq -0x228(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x230(%rbp)
cmpq $0x0, %rax
je 0x1b166
movq -0x230(%rbp), %rdi
callq 0x1c070
movq -0x230(%rbp), %rdi
callq 0x43a0
jmp 0x1af6c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x1b191
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x4768
callq 0x4640
jmp 0x1b198
jmp 0x1b248
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x1b23c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x1b208
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x1b1fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x1b1f0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x4768
leaq -0x158(%rbp), %rdi
callq 0x4768
leaq -0x159(%rbp), %rdi
callq 0x4440
testb $0x1, -0x15a(%rbp)
jne 0x1b213
jmp 0x1b21f
movq -0x210(%rbp), %rdi
callq 0x4310
jmp 0x1b23c
movl -0x20(%rbp), %edi
callq 0x4720
jmp 0x1b22b
leaq -0x110(%rbp), %rdi
callq 0x4768
jmp 0x1ab2d
leaq -0x110(%rbp), %rdi
callq 0x4768
movq -0x68(%rbp), %rdi
callq 0x46b0
movq %rax, %rdi
callq 0x8960
nopl (%rax)