// File: /workspace/github_cpp_makefile_star2_O3/DavieV[P]virtual-usb-printer/usbip.cc
// Function: SendUsbRequest(int, _USBIP_CMD_SUBMIT const&, char const*, unsigned int, unsigned int)
void SendUsbRequest(int sockfd, const USBIP_CMD_SUBMIT& usb_request,
                    const char* data, unsigned int data_size,
                    unsigned int status) {
  USBIP_RET_SUBMIT response = CreateUsbipRetSubmit(usb_request);
  response.status = status;
  response.actual_length = data_size;
  response.start_frame = 0;
  // TODO(daviev): Figure out what this means.
  response.number_of_packets = 0;
  response.error_count = 0;

  if (data_size > 0) {
    printf("Sending buffer: \n");
    for (int i = 0; i < data_size; ++i) {
      printf("%u ", data[i]);
    }
    printf("\n");
  }

  size_t response_size = sizeof(response);
  pack_usbip((int*)&response, response_size);

  if (send(sockfd, (char*)&response, response_size, 0) != response_size) {
    printf("send error : %s \n", strerror(errno));
    exit(-1);
  }

  // Skip sending data if there isn't any.
  if (data_size == 0) {
    return;
  }

  if (send(sockfd, data, data_size, 0) != data_size) {
    printf("send error : %s \n", strerror(errno));
    exit(-1);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %ecx, %r14d
movq %rdx, %rbx
movl %edi, %ebp
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movl $0x3, 0x8(%rsp)
movdqu 0x4(%rsi), %xmm0
movdqu %xmm0, 0xc(%rsp)
movl %r8d, 0x1c(%rsp)
movl %ecx, 0x20(%rsp)
movq %rax, 0x24(%rsp)
movl $0x0, 0x2c(%rsp)
movl %ecx, %r15d
testl %ecx, %ecx
je 0x1c8e
leaq 0x253e(%rip), %rdi      # 0x419c
callq 0x11f0
leaq 0x2513(%rip), %r12      # 0x417d
xorl %r13d, %r13d
movsbl (%rbx,%r13), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x1050
incq %r13
cmpq %r13, %r15
jne 0x1c6d
movl $0xa, %edi
callq 0x11d0
xorl %eax, %eax
pxor %xmm0, %xmm0
movdqu 0x8(%rsp,%rax,4), %xmm1
movdqa %xmm1, %xmm2
punpckhbw %xmm0, %xmm2    # xmm2 = xmm2[8],xmm0[8],xmm2[9],xmm0[9],xmm2[10],xmm0[10],xmm2[11],xmm0[11],xmm2[12],xmm0[12],xmm2[13],xmm0[13],xmm2[14],xmm0[14],xmm2[15],xmm0[15]
pshuflw $0x1b, %xmm2, %xmm2     # xmm2 = xmm2[3,2,1,0,4,5,6,7]
pshufhw $0x1b, %xmm2, %xmm2     # xmm2 = xmm2[0,1,2,3,7,6,5,4]
punpcklbw %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1],xmm1[2],xmm0[2],xmm1[3],xmm0[3],xmm1[4],xmm0[4],xmm1[5],xmm0[5],xmm1[6],xmm0[6],xmm1[7],xmm0[7]
pshuflw $0x1b, %xmm1, %xmm1     # xmm1 = xmm1[3,2,1,0,4,5,6,7]
pshufhw $0x1b, %xmm1, %xmm1     # xmm1 = xmm1[0,1,2,3,7,6,5,4]
packuswb %xmm2, %xmm1
movdqu %xmm1, 0x8(%rsp,%rax,4)
addq $0x4, %rax
cmpq $0xc, %rax
jne 0x1c94
leaq 0x8(%rsp), %rsi
movq 0x28(%rsi), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x28(%rsi)
movl $0x30, %edx
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x10f0
cmpq $0x30, %rax
jne 0x1d1e
testl %r14d, %r14d
je 0x1d0f
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x10f0
cmpq %r15, %rax
jne 0x1d1e
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1040
movl (%rax), %edi
callq 0x10b0
leaq 0x230d(%rip), %rdi      # 0x403e
movq %rax, %rsi
xorl %eax, %eax
callq 0x1050
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x11a0
nop