// File: /workspace/github_c_makefile_star2_O2/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
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x40(%rsp)
movaps %xmm0, 0x10(%rsp)
andq $0x0, 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 0x6190
movq 0x71103(%rip), %rax     # 0x7fe98
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 0xedc2
movl 0x44(%rax), %r13d
leaq 0x10(%rsp), %r15
movl %r13d, 0x18(%r15)
movl 0xe35f(%rip), %edi      # 0x1d130
movl $0x8038550a, %esi       # imm = 0x8038550A
movq %r15, %rdx
xorl %eax, %eax
callq 0x60f0
movl %eax, %edi
notl %edi
shrl $0x1f, %edi
leaq 0x4fd8(%rip), %rsi      # 0x13dc6
movl %eax, %edx
xorl %eax, %eax
callq 0x793d
movl 0xe333(%rip), %edi      # 0x1d130
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 0x4fc3(%rip), %rsi      # 0x13de2
movl %eax, %edx
xorl %eax, %eax
callq 0x793d
movl 0x18(%r15), %edx
movl 0x1c(%r15), %ecx
xorl %edi, %edi
cmpl %edx, %ecx
sete %dil
leaq 0x4fbd(%rip), %rsi      # 0x13dfc
xorl %eax, %eax
callq 0x793d
movq 0x7104b(%rip), %rax     # 0x7fe98
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 0xe2a7(%rip), %edi      # 0x1d130
movl $0x8038550a, %esi       # imm = 0x8038550A
movq %r15, %rdx
xorl %eax, %eax
callq 0x60f0
movl %eax, %edi
notl %edi
shrl $0x1f, %edi
leaq 0x4f86(%rip), %rsi      # 0x13e2c
movl %eax, %edx
xorl %eax, %eax
callq 0x793d
movl 0xe27b(%rip), %edi      # 0x1d130
movl $0x4008550c, %esi       # imm = 0x4008550C
movq %r13, %rdx
xorl %eax, %eax
callq 0x60f0
movl %eax, %edi
notl %edi
shrl $0x1f, %edi
leaq 0x4f76(%rip), %rsi      # 0x13e48
movl %eax, %edx
xorl %eax, %eax
callq 0x793d
xorl %edi, %edi
cmpl $0x0, 0x1c(%r15)
setne %dil
leaq 0x4f75(%rip), %rsi      # 0x13e62
xorl %eax, %eax
callq 0x793d
movzbl 0x50(%rsp), %edx
cmpb %r14b, %dl
je 0xef0f
leaq 0x4f75(%rip), %rdi      # 0x13e7a
movl %r14d, %esi
xorl %eax, %eax
callq 0x7a0e
leaq 0x51(%rsp), %rsi
movl 0x2c(%rsp), %r14d
decl %r14d
cmpl %ebp, %r14d
cmovll %r14d, %ebp
movslq %ebp, %rdx
movq %rbx, %rdi
callq 0x6190
movl %r14d, %eax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq