// File: /workspace/github_c_makefile_star2_O1/ataradov[P]edbg/dbg_lin.c
// Function: dbg_dap_cmd
int dbg_dap_cmd(uint8_t *data, int resp_size, int req_size)
{
  uint8_t cmd = data[0];
  struct usbdevfs_urb urb = { 0 };
  struct usbdevfs_urb *purb = NULL;
  uint8_t buf[DBG_MAX_EP_SIZE];
  int res;

  memset(buf, 0xff, sizeof(buf));
  memcpy(buf, data, req_size);

  // TX
  urb.endpoint      = g_debugger->use_v2 ? g_debugger->v2_tx_ep : g_debugger->v1_tx_ep;
  urb.type          = g_debugger->use_v2 ? USBDEVFS_URB_TYPE_BULK : USBDEVFS_URB_TYPE_INTERRUPT;
  urb.buffer        = buf;
  urb.buffer_length = g_debugger->use_v2 ? req_size : g_debugger->v1_ep_size;

  res = ioctl(g_debugger_fd, USBDEVFS_SUBMITURB, &urb);
  check(res >= 0, "ioctl(SUBMITURB) for TX: %d", res);

  res = ioctl(g_debugger_fd, USBDEVFS_REAPURB, &purb);
  check(res >= 0, "ioctl(REAPURB) for TX: %d", res);
  check(urb.actual_length == urb.buffer_length, "incomplete buffer TX: request = %d, actual = %d",
      urb.buffer_length, urb.actual_length);

  // RX
  urb.endpoint      = g_debugger->use_v2 ? g_debugger->v2_rx_ep : g_debugger->v1_rx_ep;
  urb.type          = g_debugger->use_v2 ? USBDEVFS_URB_TYPE_BULK : USBDEVFS_URB_TYPE_INTERRUPT;
  urb.buffer        = buf;
  urb.buffer_length = g_debugger->use_v2 ? g_debugger->v2_ep_size : g_debugger->v1_ep_size;

  res = ioctl(g_debugger_fd, USBDEVFS_SUBMITURB, &urb);
  check(res >= 0, "ioctl(SUBMITURB) for RX: %d", res);

  res = ioctl(g_debugger_fd, USBDEVFS_REAPURB, &purb);
  check(res >= 0, "ioctl(REAPURB) for RX: %d", res);

  // Result
  check(urb.actual_length, "empty response received");

  if (buf[0] != cmd)
    error_exit("invalid response received: request = 0x%02x, response = 0x%02x", cmd, buf[0]);

  memcpy(data, &buf[1], (resp_size < (urb.actual_length - 1)) ? resp_size : (urb.actual_length - 1));

  return urb.actual_length - 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movl %edx, %r13d
movl %esi, %ebp
movq %rdi, %rbx
movzbl (%rdi), %r14d
xorl %eax, %eax
movq %rax, 0x40(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movq %rax, 0x8(%rsp)
leaq 0x50(%rsp), %r12
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
movl $0xff, %esi
callq 0x60e0
movslq %r13d, %rdx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x6180
movq 0x6f991(%rip), %rax     # 0x7ee98
movb 0x34(%rax), %cl
leaq 0x48(%rax), %rdx
leaq 0x60(%rax), %rsi
leal (%rcx,%rcx), %edi
incb %dil
testb %cl, %cl
cmovneq %rsi, %rdx
movb (%rdx), %cl
movb %cl, 0x11(%rsp)
movb %dil, 0x10(%rsp)
movq %r12, 0x20(%rsp)
jne 0xf534
movl 0x44(%rax), %r13d
leaq 0x10(%rsp), %r15
movl %r13d, 0x18(%r15)
movl 0xcbed(%rip), %edi      # 0x1c130
movl $0x8038550a, %esi       # imm = 0x8038550A
movq %r15, %rdx
xorl %eax, %eax
callq 0x60f0
movl %eax, %edi
notl %edi
shrl $0x1f, %edi
leaq 0x4857(%rip), %rsi      # 0x13db7
movl %eax, %edx
xorl %eax, %eax
callq 0x7bb4
movl 0xcbc1(%rip), %edi      # 0x1c130
leaq 0x8(%rsp), %r13
movl $0x4008550c, %esi       # imm = 0x4008550C
movq %r13, %rdx
xorl %eax, %eax
callq 0x60f0
movl %eax, %edi
notl %edi
shrl $0x1f, %edi
leaq 0x4842(%rip), %rsi      # 0x13dd3
movl %eax, %edx
xorl %eax, %eax
callq 0x7bb4
movl 0x18(%r15), %edx
movl 0x1c(%r15), %ecx
xorl %edi, %edi
cmpl %edx, %ecx
sete %dil
leaq 0x483c(%rip), %rsi      # 0x13ded
xorl %eax, %eax
callq 0x7bb4
movq 0x6f8d9(%rip), %rax     # 0x7ee98
movb 0x34(%rax), %cl
leaq 0x4c(%rax), %rdx
leaq 0x64(%rax), %rsi
leal (%rcx,%rcx), %edi
incb %dil
leaq 0x44(%rax), %r8
addq $0x5c, %rax
testb %cl, %cl
cmovneq %rsi, %rdx
movb (%rdx), %cl
movb %cl, 0x1(%r15)
movb %dil, (%r15)
cmoveq %r8, %rax
movq %r12, 0x10(%r15)
movl (%rax), %eax
movl %eax, 0x18(%r15)
movl 0xcb35(%rip), %edi      # 0x1c130
movl $0x8038550a, %esi       # imm = 0x8038550A
movq %r15, %rdx
xorl %eax, %eax
callq 0x60f0
movl %eax, %edi
notl %edi
shrl $0x1f, %edi
leaq 0x4805(%rip), %rsi      # 0x13e1d
movl %eax, %edx
xorl %eax, %eax
callq 0x7bb4
movl 0xcb09(%rip), %edi      # 0x1c130
movl $0x4008550c, %esi       # imm = 0x4008550C
movq %r13, %rdx
xorl %eax, %eax
callq 0x60f0
movl %eax, %edi
notl %edi
shrl $0x1f, %edi
leaq 0x47f5(%rip), %rsi      # 0x13e39
movl %eax, %edx
xorl %eax, %eax
callq 0x7bb4
xorl %edi, %edi
cmpl $0x0, 0x1c(%r15)
setne %dil
leaq 0x47f4(%rip), %rsi      # 0x13e53
xorl %eax, %eax
callq 0x7bb4
movzbl 0x50(%rsp), %edx
cmpb %r14b, %dl
je 0xf681
leaq 0x47f4(%rip), %rdi      # 0x13e6b
movl %r14d, %esi
xorl %eax, %eax
callq 0x7c8d
leaq 0x51(%rsp), %rsi
movl 0x2c(%rsp), %r14d
decl %r14d
cmpl %ebp, %r14d
cmovll %r14d, %ebp
movslq %ebp, %rdx
movq %rbx, %rdi
callq 0x6180
movl %r14d, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq