// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/dap.c
// Function: dap_transfer
void dap_transfer(void)
{
  int count, status;
  uint32_t *data;

  dap_response_count = 0;
  dap_csw = 0;

  while (dap_response_count < dap_request_count)
  {
    dap_buf[0] = ID_DAP_TRANSFER;
    dap_buf[1] = dap_jtag_index;
    dap_buf[2] = 0; // Request size (placeholder)

    dap_buf_size = 3;
    dap_ops_size = 0;
    dap_response_size = 2; // count and status

    for (int i = dap_response_count; i < dap_request_count; i++)
    {
      if (!buffer_request(&dap_request[i]))
        break;
    }

    dap_buf[2] = dap_ops_size;

    //verbose("--- %d / %d, req_cnt = %d, resp_cnt = %d, resp_size = %d\n",
    //  dap_ops_size, dap_buf_size, dap_request_count, dap_response_count, dap_response_size);

    dbg_dap_cmd(dap_buf, sizeof(dap_buf), dap_buf_size);

    count  = dap_buf[0];
    status = dap_buf[1];
    data   = (uint32_t *)&dap_buf[2];

    if (dap_ops_size != count || DAP_TRANSFER_OK != status)
      error_exit("invalid response during transfer (count = %d/%d, status = %d)", count, dap_ops_size, status);

    for (int i = 0; i < count; i++)
    {
      dap_request_t *req = &dap_request[dap_response_count];

      if (OP_READ == dap_ops[i])
      {
        dap_response[dap_response_count++] = from_lane(req->size, req->addr, *data);
        data++;
      }
      else if (OP_WRITE == dap_ops[i])
      {
        dap_response[dap_response_count++] = req->data;
      }
    }
  }

  dap_request_count = 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movl $0x0, 0x48c3e(%rip)     # 0x4f8c0
movl $0x0, 0x48c38(%rip)     # 0x4f8c4
movl 0x48c2e(%rip), %eax     # 0x4f8c0
cmpl 0x18c20(%rip), %eax     # 0x1f8b8
jge 0x6e69
movb $0x5, 0x48c2b(%rip)     # 0x4f8d0
movl 0x18c09(%rip), %eax     # 0x1f8b4
movb %al, 0x48c20(%rip)      # 0x4f8d1
movb $0x0, 0x48c1a(%rip)     # 0x4f8d2
movl $0x3, 0x4904e(%rip)     # 0x4fd10
movl $0x0, 0x49048(%rip)     # 0x4fd14
movl $0x2, 0x49042(%rip)     # 0x4fd18
movl 0x48be4(%rip), %eax     # 0x4f8c0
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl 0x18bd0(%rip), %eax     # 0x1f8b8
jge 0x6d14
movslq -0x14(%rbp), %rax
leaq 0x18bcb(%rip), %rdi     # 0x1f8c0
imulq $0xc, %rax, %rax
addq %rax, %rdi
callq 0x71a0
testb $0x1, %al
jne 0x6d07
jmp 0x6d14
jmp 0x6d09
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x6cdf
movl 0x48ffa(%rip), %eax     # 0x4fd14
movb %al, 0x48bb2(%rip)      # 0x4f8d2
movl 0x48fea(%rip), %edx     # 0x4fd10
leaq 0x48ba3(%rip), %rdi     # 0x4f8d0
movl $0x440, %esi            # imm = 0x440
callq 0x130d0
movzbl 0x48b92(%rip), %eax     # 0x4f8d0
movl %eax, -0x4(%rbp)
movzbl 0x48b89(%rip), %eax     # 0x4f8d1
movl %eax, -0x8(%rbp)
leaq 0x48b7e(%rip), %rax     # 0x4f8d0
addq $0x2, %rax
movq %rax, -0x10(%rbp)
movl 0x48fb4(%rip), %eax     # 0x4fd14
cmpl -0x4(%rbp), %eax
jne 0x6d6f
movl $0x1, %eax
cmpl -0x8(%rbp), %eax
je 0x6d89
movl -0x4(%rbp), %esi
movl 0x48f9c(%rip), %edx     # 0x4fd14
movl -0x8(%rbp), %ecx
leaq 0xd401(%rip), %rdi      # 0x14183
movb $0x0, %al
callq 0x8970
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x4(%rbp), %eax
jge 0x6e64
movslq 0x48b1d(%rip), %rcx     # 0x4f8c0
leaq 0x18b16(%rip), %rax     # 0x1f8c0
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movslq -0x18(%rbp), %rcx
leaq 0x48f60(%rip), %rax     # 0x4fd20
movzbl (%rax,%rcx), %ecx
movl $0x4, %eax
cmpl %ecx, %eax
jne 0x6e15
movq -0x20(%rbp), %rax
movzbl 0x1(%rax), %edi
movq -0x20(%rbp), %rax
movl 0x4(%rax), %esi
movq -0x10(%rbp), %rax
movl (%rax), %edx
callq 0x7620
movl %eax, %edx
movl 0x48ad1(%rip), %eax     # 0x4f8c0
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x48ac6(%rip)     # 0x4f8c0
movslq %eax, %rcx
leaq 0x4935c(%rip), %rax     # 0x50160
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x10(%rbp)
jmp 0x6e54
movslq -0x18(%rbp), %rcx
leaq 0x48f00(%rip), %rax     # 0x4fd20
movzbl (%rax,%rcx), %ecx
movl $0x5, %eax
cmpl %ecx, %eax
jne 0x6e52
movq -0x20(%rbp), %rax
movl 0x8(%rax), %edx
movl 0x48a86(%rip), %eax     # 0x4f8c0
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x48a7b(%rip)     # 0x4f8c0
movslq %eax, %rcx
leaq 0x49311(%rip), %rax     # 0x50160
movl %edx, (%rax,%rcx,4)
jmp 0x6e54
jmp 0x6e56
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x6d90
jmp 0x6c8c
movl $0x0, 0x18a45(%rip)     # 0x1f8b8
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)