// File: /workspace/github_cpp_makefile_star2_O1/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, (%rsp)
callq 0x2754
movl %eax, %ebx
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 0x2ad8
movzwl 0x5a(%rsp), %edx
leaq 0x1b00(%rip), %rdi      # 0x43ea
leaq 0x40(%rsp), %rsi
xorl %eax, %eax
callq 0x1050
movl %ebx, 0xc(%rsp)
movl %ebx, %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x1180
testl %eax, %eax
js 0x2aed
leaq 0x10(%rsp), %r14
leaq 0x1b85(%rip), %rbp      # 0x449f
leaq 0x1baf(%rip), %r15      # 0x44d0
movl 0xc(%rsp), %edi
callq 0x282b
movl %eax, %r12d
xorl %r13d, %r13d
testl %r13d, %r13d
je 0x29d4
movq %rbp, %rdi
callq 0x11f0
movq %r15, %rdi
callq 0x11f0
movl $0x30, %edx
movl %r12d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x10a0
cmpq $0x30, %rax
jne 0x2a38
movl $0x30, %esi
movq %r14, %rdi
callq 0x1a5e
movq %r14, %rdi
callq 0x1ab2
cmpl $0x1, 0x10(%rsp)
jne 0x2990
movq (%rsp), %rdi
movl %r12d, %esi
movq %r14, %rdx
callq 0x1eb4
cmpl $0x2, 0x10(%rsp)
jne 0x29a9
movl 0x24(%rsp), %esi
leaq 0x1ab6(%rip), %rdi      # 0x4458
xorl %eax, %eax
callq 0x1050
cmpl $0x3, 0x10(%rsp)
jl 0x2a80
leaq 0x1b26(%rip), %rdi      # 0x44e1
callq 0x11f0
movl %r12d, %edi
callq 0x1240
movl $0x1, %eax
xorl %ebx, %ebx
jmp 0x2a84
movl $0x8, %edx
movl %r12d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x10a0
movq %rax, %rbx
cmpq $0x8, %rax
jne 0x2a5e
rolw $0x8, 0x12(%rsp)
leaq 0x1a95(%rip), %rdi      # 0x4491
callq 0x11f0
movzwl 0x12(%rsp), %esi
xorl %r13d, %r13d
leaq 0x1a37(%rip), %rdi      # 0x4447
xorl %eax, %eax
callq 0x1050
movzwl 0x12(%rsp), %eax
cmpl $0x8003, %eax           # imm = 0x8003
je 0x2a94
cmpl $0x8005, %eax           # imm = 0x8005
jne 0x2aa9
movq (%rsp), %rdi
movl %r12d, %esi
callq 0x1630
jmp 0x2aa9
callq 0x1040
movl (%rax), %edi
callq 0x10b0
xorl %ebx, %ebx
leaq 0x19e6(%rip), %rdi      # 0x4433
movq %rax, %rsi
xorl %eax, %eax
callq 0x1050
movl $0x5, %eax
jmp 0x2a84
callq 0x1040
movl (%rax), %edi
callq 0x10b0
xorl %r13d, %r13d
leaq 0x19bf(%rip), %rdi      # 0x4433
movq %rax, %rsi
xorl %eax, %eax
callq 0x1050
jmp 0x2aa9
xorl %eax, %eax
movb $0x1, %bl
movl $0x1, %r13d
testl %eax, %eax
je 0x2930
jmp 0x2ab8
movq (%rsp), %rdi
movl %r12d, %esi
callq 0x1850
xorl %r13d, %r13d
testl %eax, %eax
sete %r13b
cmpq $0x8, %rbx
je 0x2930
jmp 0x2921
cmpl $0x5, %eax
je 0x2921
testb %bl, %bl
jne 0x2921
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1040
movl (%rax), %edi
callq 0x10b0
leaq 0x191f(%rip), %rdi      # 0x440a
jmp 0x2b00
callq 0x1040
movl (%rax), %edi
callq 0x10b0
leaq 0x1920(%rip), %rdi      # 0x4420
movq %rax, %rsi
xorl %eax, %eax
callq 0x1050
movl $0x1, %edi
callq 0x11a0