// File: UTAP[P]APHTTP/server/server.cpp
// Function: Server::Server(int)
Server::Server(int _port) : port(_port) {
#ifdef _WIN32
  WSADATA wsa_data;
  int initializeResult = WSAStartup(MAKEWORD(2, 2), &wsa_data);
  if (initializeResult != 0) {
    throw Exception("Error: WinSock WSAStartup failed: " + string(getSocketError()));
  }
#endif

  notFoundHandler = new NotFoundHandler();

  sc = socket(AF_INET, SOCK_STREAM, 0);
  int sc_option = 1;

#ifdef _WIN32
  setsockopt(sc, SOL_SOCKET, SO_REUSEADDR, (char*)&sc_option, sizeof(sc_option));
#else
  setsockopt(sc, SOL_SOCKET, SO_REUSEADDR, &sc_option, sizeof(sc_option));
#endif
  if (!ISVALIDSOCKET(sc))
    throw Exception("Error on opening socket: " + string(getSocketError()));

  struct sockaddr_in serv_addr;
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(port);

  if (::bind(sc, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0) {
    throw Exception("Error on binding: " + string(getSocketError()));
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x100(%rbp)
movl -0xc(%rbp), %eax
movl %eax, 0x4(%rdi)
addq $0x8, %rdi
movq %rdi, -0xf8(%rbp)
callq 0x1bed0
movl $0x28, %edi
callq 0x4400
movq %rax, -0xf0(%rbp)
jmp 0x1a586
movb $0x1, -0x42(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x46c0
movq -0x108(%rbp), %rdx
leaq 0x6fed(%rip), %rsi      # 0x21595
leaq -0x40(%rbp), %rdi
callq 0x4550
jmp 0x1a5b3
movq -0xf0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x1bef0
jmp 0x1a5c5
movq -0x100(%rbp), %rax
movq -0xf0(%rbp), %rcx
movb $0x0, -0x42(%rbp)
movq %rcx, 0x20(%rax)
leaq -0x40(%rbp), %rdi
callq 0x4768
leaq -0x41(%rbp), %rdi
callq 0x4440
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x4120
movl %eax, %ecx
movq -0x100(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x48(%rbp)
movl (%rax), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x48(%rbp), %rcx
movl $0x4, %r8d
callq 0x46d0
movq -0x100(%rbp), %rax
cmpl $0x0, (%rax)
jge 0x1a7aa
movb $0x1, -0x8a(%rbp)
movl $0x28, %edi
callq 0x4250
movq %rax, -0x118(%rbp)
callq 0x1a940
movq %rax, -0x110(%rbp)
jmp 0x1a661
leaq -0x89(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x46c0
movq -0x110(%rbp), %rsi
movq -0x120(%rbp), %rdx
leaq -0x88(%rbp), %rdi
callq 0x4550
jmp 0x1a690
leaq 0x7257(%rip), %rsi      # 0x218ee
leaq -0x68(%rbp), %rdi
leaq -0x88(%rbp), %rdx
callq 0x9a70
jmp 0x1a6a9
movq -0x118(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x1b360
jmp 0x1a6bb
movq -0x118(%rbp), %rdi
movb $0x0, -0x8a(%rbp)
leaq 0x123f8(%rip), %rsi     # 0x2cac8
leaq -0x10cc7(%rip), %rdx    # 0x9a10
callq 0x4680
jmp 0x1a939
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x1a924
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x1a715
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x4768
leaq -0x41(%rbp), %rdi
callq 0x4440
testb $0x1, -0x42(%rbp)
jne 0x1a726
jmp 0x1a732
movq -0xf0(%rbp), %rdi
callq 0x43a0
jmp 0x1a924
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x1a78e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x1a782
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x1a776
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x4768
leaq -0x88(%rbp), %rdi
callq 0x4768
leaq -0x89(%rbp), %rdi
callq 0x4440
testb $0x1, -0x8a(%rbp)
jne 0x1a799
jmp 0x1a7a5
movq -0x118(%rbp), %rdi
callq 0x4310
jmp 0x1a924
movq -0x100(%rbp), %rax
movw $0x2, -0x9c(%rbp)
movl $0x0, -0x98(%rbp)
movl 0x4(%rax), %eax
movzwl %ax, %edi
callq 0x4080
movw %ax, %cx
movq -0x100(%rbp), %rax
movw %cx, -0x9a(%rbp)
movl (%rax), %edi
leaq -0x9c(%rbp), %rsi
movl $0x10, %edx
callq 0x40a0
cmpl $0x0, %eax
je 0x1a91b
movb $0x1, -0xe2(%rbp)
movl $0x28, %edi
callq 0x4250
movq %rax, -0x130(%rbp)
callq 0x1a940
movq %rax, -0x128(%rbp)
jmp 0x1a822
leaq -0xe1(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x46c0
movq -0x128(%rbp), %rsi
movq -0x138(%rbp), %rdx
leaq -0xe0(%rbp), %rdi
callq 0x4550
jmp 0x1a851
leaq 0x70b0(%rip), %rsi      # 0x21908
leaq -0xc0(%rbp), %rdi
leaq -0xe0(%rbp), %rdx
callq 0x9a70
jmp 0x1a86d
movq -0x130(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x1b360
jmp 0x1a882
movq -0x130(%rbp), %rdi
movb $0x0, -0xe2(%rbp)
leaq 0x12231(%rip), %rsi     # 0x2cac8
leaq -0x10e8e(%rip), %rdx    # 0x9a10
callq 0x4680
jmp 0x1a939
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x1a902
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x1a8f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x1a8ea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x4768
leaq -0xe0(%rbp), %rdi
callq 0x4768
leaq -0xe1(%rbp), %rdi
callq 0x4440
testb $0x1, -0xe2(%rbp)
jne 0x1a90d
jmp 0x1a919
movq -0x130(%rbp), %rdi
callq 0x4310
jmp 0x1a924
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0xf8(%rbp), %rdi
callq 0x1bf60
movq -0x18(%rbp), %rdi
callq 0x46b0
nopl (%rax)