// File: DavieV[P]virtual-usb-printer/usb_printer.cc
// Function: UsbPrinter::HandleGetConfigurationDescriptor(int, _USBIP_CMD_SUBMIT const&, _StandardDeviceRequest const&) const
void UsbPrinter::HandleGetConfigurationDescriptor(
    int sockfd, const USBIP_CMD_SUBMIT& usb_request,
    const StandardDeviceRequest& control_request) const {
  printf("HandleGetConfigurationDescriptor %u[%u]\n", control_request.wValue1,
         control_request.wValue0);

  if (control_request.wLength == configuration_descriptor_.bLength) {
    // Only the configuration descriptor itself has been requested.
    printf("Only configuration descriptor requested\n");
    SendUsbRequest(sockfd, usb_request, (char*)&configuration_descriptor_,
                   control_request.wLength, 0);
    return;
  }

  // NOTE: This currently assumes that if the requested length was not just the
  // configuration descriptor then all of the descriptors have been requested.
  // TODO(daviev): If there are any instances where the client requests a size
  // other than the configuration descriptors bLength or wTotalLength then
  // change this function to address it.

  auto buf = std::make_unique<char[]>(control_request.wLength);
  int total = 0;

  // Place the configuration descriptor into the buffer.
  FillBuffer((char*)&configuration_descriptor_,
             configuration_descriptor_.bLength, buf.get() + total);
  total += configuration_descriptor_.bLength;

  // Place each of the corresponding interfaces into the buffer.
  for (int i = 0; i < configuration_descriptor_.bNumInterfaces; ++i) {
    auto interface = interfaces_[i];
    FillBuffer((char*)&interface, interface.bLength, buf.get() + total);
    total += interface.bLength;
    // TODO(daviev): For now this assumes that each interface has 2
    // corresponding endpoints. Update this in the future to support interfaces
    // that have only 1 endpoint.
    for (int j = i * 2; j < (i * 2) + 2; ++j) {
      auto endpoint = endpoints_[j];
      FillBuffer((char*)&endpoint, endpoint.bLength, buf.get() + total);
      total += endpoint.bLength;
    }
  }

  // After filling the buffer with all of the necessary descriptors we can send
  // a response.
  SendUsbRequest(sockfd, usb_request, buf.get(), control_request.wLength, 0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rax
movzbl 0x3(%rax), %esi
movq -0x20(%rbp), %rax
movzbl 0x2(%rax), %edx
leaq 0x526e(%rip), %rdi      # 0x92a7
movb $0x0, %al
callq 0x2050
movq -0x68(%rbp), %rcx
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %eax
movzbl 0x12(%rcx), %ecx
cmpl %ecx, %eax
jne 0x4086
leaq 0x5275(%rip), %rdi      # 0x92d0
movb $0x0, %al
callq 0x2050
movq -0x68(%rbp), %rdx
movl -0xc(%rbp), %edi
movq -0x18(%rbp), %rsi
addq $0x12, %rdx
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %ecx
xorl %r8d, %r8d
callq 0x2df0
jmp 0x4241
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
movq %rdi, -0x80(%rbp)
callq 0x47d0
movq -0x68(%rbp), %rax
movq -0x80(%rbp), %rdi
movl $0x0, -0x2c(%rbp)
movq %rax, %rcx
addq $0x12, %rcx
movq %rcx, -0x78(%rbp)
movzbl 0x12(%rax), %eax
movl %eax, -0x6c(%rbp)
callq 0x4830
movq -0x78(%rbp), %rdi
movl -0x6c(%rbp), %esi
movq %rax, %rdx
movslq -0x2c(%rbp), %rax
addq %rax, %rdx
callq 0x43f0
jmp 0x40db
movq -0x68(%rbp), %rax
movzbl 0x12(%rax), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x68(%rbp), %rcx
movl -0x40(%rbp), %eax
movzbl 0x16(%rcx), %ecx
cmpl %ecx, %eax
jge 0x41f9
movq -0x68(%rbp), %rdi
addq $0x50, %rdi
movslq -0x40(%rbp), %rsi
callq 0x2f70
movb 0x8(%rax), %cl
movb %cl, -0x48(%rbp)
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movzbl -0x50(%rbp), %eax
movl %eax, -0x84(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x4830
movl -0x84(%rbp), %esi
movq %rax, %rdx
movslq -0x2c(%rbp), %rax
addq %rax, %rdx
leaq -0x50(%rbp), %rdi
callq 0x43f0
jmp 0x414f
movzbl -0x50(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x40(%rbp), %eax
shll %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl -0x40(%rbp), %ecx
shll %ecx
addl $0x2, %ecx
cmpl %ecx, %eax
jge 0x41e9
movq -0x68(%rbp), %rdi
addq $0x68, %rdi
movslq -0x54(%rbp), %rsi
callq 0x4860
movq %rax, %rcx
movl (%rcx), %eax
movl 0x3(%rcx), %ecx
movl %ecx, -0x59(%rbp)
movl %eax, -0x5c(%rbp)
movzbl -0x5c(%rbp), %eax
movl %eax, -0x88(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x4830
movl -0x88(%rbp), %esi
movq %rax, %rdx
movslq -0x2c(%rbp), %rax
addq %rax, %rdx
leaq -0x5c(%rbp), %rdi
callq 0x43f0
jmp 0x41bd
movzbl -0x5c(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x4161
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x4880
jmp 0x424a
jmp 0x41eb
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x40f0
movl -0xc(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x4830
movl -0x94(%rbp), %edi
movq -0x90(%rbp), %rsi
movq %rax, %rdx
movq -0x20(%rbp), %rax
movzwl 0x6(%rax), %ecx
xorl %r8d, %r8d
callq 0x2df0
jmp 0x4238
leaq -0x28(%rbp), %rdi
callq 0x4880
addq $0xa0, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x2270
nopw %cs:(%rax,%rax)