// File: /workspace/github_cpp_makefile_star2_O3/DavieV[P]virtual-usb-printer/server.cc
// Function: run_server(UsbPrinter)
void run_server(UsbPrinter printer) {
  int listenfd = setup_server_socket();
  struct sockaddr_in server = bind_server_socket(listenfd);
  char address[INET_ADDRSTRLEN];

  if (inet_ntop(AF_INET, &server.sin_addr, address, INET_ADDRSTRLEN)) {
    printf("Bound server to address %s:%hu\n", address, server.sin_port);
  } else {
    printf("inet_ntop error : %s\n", strerror(errno));
    exit(1);
  }

  if (listen(listenfd, SOMAXCONN) < 0) {
    printf("listen error : %s\n", strerror(errno));
    exit(1);
  }

  while (1) {
    int connection = accept_connection(listenfd);
    int attached = 0;

    while (1) {
      if (!attached) {
        // Read in the header first in order to determine whether the request is
        // an OP_REQ_DEVLIST or an OP_REQ_IMPORT.
        OP_HEADER request;
        ssize_t received = recv(connection, &request, sizeof(request), 0);
        if (received != sizeof(request)) {
          printf("receive error : %s\n", strerror (errno));
          break;
        }

        request.command = ntohs(request.command);
        printf("Header Packet\n");
        printf("command: 0x%02X\n", request.command);

        // Request is OP_REQ_DEVLIST 
        if (request.command == OP_REQ_DEVLIST_CMD) {
          handle_device_list(printer, connection);
        // Request is OP_REQ_IMPORT
        } else if (request.command == OP_REQ_IMPORT_CMD) {
          if (!handle_attach(printer, connection)) {
            attached = 1;
          }
        }
      } else {
        printf("------------------------------------------------\n");
        printf("handles requests\n");
        USBIP_CMD_SUBMIT command;
        ssize_t received = recv(connection, &command, sizeof(command), 0);
        if (received != sizeof(command)) {
          printf("receive error : %s\n", strerror(errno));
          break;
        }

        unpack_usbip((int *)&command, sizeof(command));
        print_usbip_cmd_submit(command);

        if (command.command == 1) {
          printer.HandleUsbRequest(connection, command);
        }

        // Unlink URB
        if (command.command == 2) {
          printf("####################### Unlink URB %u  (not working!!!)\n",
                 command.transfer_flags);
          // FIXME
          /*
           USBIP_RET_UNLINK ret;
           printf("####################### Unlink URB %u\n",cmd.transfer_flags);
           ret.command=htonl(0x04);
           ret.devid=htonl(cmd.devid);
           ret.direction=htonl(cmd.direction);
           ret.ep=htonl(cmd.ep);
           ret.seqnum=htonl(cmd.seqnum);
           ret.status=htonl(1);

           if (send (sockfd, (char *)&ret, sizeof(USBIP_RET_UNLINK), 0) !=
           sizeof(USBIP_RET_UNLINK))
           {
             printf ("send error : %s \n", strerror (errno));
             exit(-1);
           }
          */
        }

        if (command.command > 2) {
          printf("Unknown USBIP cmd!\n");
          close(connection);
          return;
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdi, %rbx
callq 0x2754
movl %eax, %r15d
movl %eax, %edi
callq 0x27d3
leaq 0x5c(%rsp), %rsi
movq %rax, -0x4(%rsi)
movq %rdx, 0x4(%rsi)
leaq 0x40(%rsp), %rdx
movl $0x2, %edi
movl $0x10, %ecx
callq 0x1170
testq %rax, %rax
je 0x2a94
movzwl 0x5a(%rsp), %edx
leaq 0x1afb(%rip), %rdi      # 0x43ea
leaq 0x40(%rsp), %rsi
xorl %eax, %eax
callq 0x1050
movl %r15d, %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x1180
testl %eax, %eax
js 0x2aa9
leaq 0x10(%rsp), %r14
leaq 0x1b83(%rip), %r13      # 0x449f
leaq 0x1bad(%rip), %rbp      # 0x44d0
movl %r15d, 0xc(%rsp)
movl %r15d, %edi
callq 0x2830
movl %eax, %r15d
xorl %r12d, %r12d
testl %r12d, %r12d
je 0x29ca
movq %r13, %rdi
callq 0x11f0
movq %rbp, %rdi
callq 0x11f0
movl $0x30, %edx
movl %r15d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x10a0
cmpq $0x30, %rax
jne 0x2a4a
movl $0x30, %esi
movq %r14, %rdi
callq 0x1a5e
movq %r14, %rdi
callq 0x1ab2
movl 0x10(%rsp), %eax
cmpl $0x1, %eax
jne 0x299b
movq %rbx, %rdi
movl %r15d, %esi
movq %r14, %rdx
callq 0x1eb0
movl 0x10(%rsp), %eax
cmpl $0x2, %eax
jne 0x29b6
movl 0x24(%rsp), %esi
leaq 0x1aad(%rip), %rdi      # 0x4458
xorl %eax, %eax
callq 0x1050
movl 0x10(%rsp), %eax
cmpl $0x2, %eax
jg 0x2a71
movl $0x1, %r12d
jmp 0x2936
movl $0x8, %edx
movl %r15d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x10a0
cmpq $0x8, %rax
jne 0x2a4a
rolw $0x8, 0x12(%rsp)
leaq 0x1aa2(%rip), %rdi      # 0x4491
callq 0x11f0
movzwl 0x12(%rsp), %esi
xorl %r12d, %r12d
leaq 0x1a44(%rip), %rdi      # 0x4447
xorl %eax, %eax
callq 0x1050
movzwl 0x12(%rsp), %eax
cmpl $0x8003, %eax           # imm = 0x8003
je 0x2a31
cmpl $0x8005, %eax           # imm = 0x8005
jne 0x2936
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1630
jmp 0x2936
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1850
xorl %r12d, %r12d
testl %eax, %eax
sete %r12b
jmp 0x2936
callq 0x1040
movl (%rax), %edi
callq 0x10b0
leaq 0x19d6(%rip), %rdi      # 0x4433
movq %rax, %rsi
xorl %eax, %eax
callq 0x1050
movl 0xc(%rsp), %r15d
jmp 0x2928
leaq 0x1a69(%rip), %rdi      # 0x44e1
callq 0x11f0
movl %r15d, %edi
callq 0x1240
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1040
movl (%rax), %edi
callq 0x10b0
leaq 0x1963(%rip), %rdi      # 0x440a
jmp 0x2abc
callq 0x1040
movl (%rax), %edi
callq 0x10b0
leaq 0x1964(%rip), %rdi      # 0x4420
movq %rax, %rsi
xorl %eax, %eax
callq 0x1050
movl $0x1, %edi
callq 0x11a0