// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/dap.c
// Function: buffer_request
static bool buffer_request(dap_request_t *req)
{
  int packet_size = dbg_get_packet_size();
  int buf_size, ops_size, response_size, address_inc;
  uint32_t address, csw;
  bool set_address;

  buf_size      = dap_buf_size;
  ops_size      = dap_ops_size;
  response_size = dap_response_size;
  set_address   = dap_set_address;
  address_inc   = dap_address_inc;
  address       = dap_address;
  csw           = dap_csw;

  if (TRANSFER_TYPE_READ == req->type || TRANSFER_TYPE_WRITE == req->type ||
      TRANSFER_TYPE_WRITE_READ == req->type)
  {
    dap_csw = AP_CSW_DBGSWENABLE | AP_CSW_PROT(0x23);

    if (TRANSFER_SIZE_BYTE == req->size)
    {
      dap_csw |= AP_CSW_SIZE_BYTE;
      dap_address_inc = sizeof(uint8_t);
    }
    else if (TRANSFER_SIZE_HALF == req->size)
    {
      dap_csw |= AP_CSW_SIZE_HALF;
      dap_address_inc = sizeof(uint16_t);
    }
    else if (TRANSFER_SIZE_WORD == req->size)
    {
      dap_csw |= AP_CSW_SIZE_WORD;
      dap_address_inc = sizeof(uint32_t);
    }

    if (TRANSFER_TYPE_WRITE_READ == req->type)
      dap_address_inc = 0;
    else
      dap_csw |= AP_CSW_ADDRINC_SINGLE;

    if (dap_csw != csw)
    {
      dap_buf[dap_buf_size++] = SWD_AP_CSW;
      append_word(dap_csw);
      dap_ops[dap_ops_size++] = OP_SIZE;
    }

    if (dap_set_address || dap_address != req->addr || 0 == (dap_address & 0x3ff))
    {
      dap_buf[dap_buf_size++] = SWD_AP_TAR;
      append_word(req->addr);
      dap_ops[dap_ops_size++] = OP_ADDRESS;
      dap_address = req->addr;
      dap_set_address = false;
    }

    if (TRANSFER_TYPE_WRITE == req->type || TRANSFER_TYPE_WRITE_READ == req->type)
    {
      dap_buf[dap_buf_size++] = SWD_AP_DRW;
      append_word(to_lane(req->size, req->addr, req->data));
      dap_ops[dap_ops_size++] = (TRANSFER_TYPE_WRITE == req->type) ? OP_WRITE : OP_SKIP;
    }

    if (TRANSFER_TYPE_READ == req->type || TRANSFER_TYPE_WRITE_READ == req->type)
    {
      dap_buf[dap_buf_size++] = SWD_AP_DRW | DAP_TRANSFER_RnW;
      dap_ops[dap_ops_size++] = OP_READ;
      dap_response_size += sizeof(uint32_t);
    }

    dap_address += dap_address_inc;
  }
  else if (TRANSFER_TYPE_WRITE_REG == req->type)
  {
    dap_buf[dap_buf_size++] = req->addr;
    append_word(req->data);
    dap_ops[dap_ops_size++] = OP_WRITE;
  }
  else // TRANSFER_TYPE_READ_REG
  {
    dap_buf[dap_buf_size++] = req->addr | DAP_TRANSFER_RnW;
    dap_ops[dap_ops_size++] = OP_READ;
    dap_response_size += sizeof(uint32_t);
  }

  if (dap_buf_size > packet_size || dap_response_size > packet_size || dap_ops_size > 255)
  {
    dap_buf_size      = buf_size;
    dap_ops_size      = ops_size;
    dap_response_size = response_size;
    dap_set_address   = set_address;
    dap_address_inc   = address_inc;
    dap_address       = address;
    dap_csw           = csw;
    return false;
  }

  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
callq 0x13090
movl %eax, -0x14(%rbp)
movl 0x48b56(%rip), %eax     # 0x4fd10
movl %eax, -0x18(%rbp)
movl 0x48b51(%rip), %eax     # 0x4fd14
movl %eax, -0x1c(%rbp)
movl 0x48b4c(%rip), %eax     # 0x4fd18
movl %eax, -0x20(%rbp)
movb 0x14fb3(%rip), %al      # 0x1c188
andb $0x1, %al
movb %al, -0x2d(%rbp)
movl 0x7afa0(%rip), %eax     # 0x82180
movl %eax, -0x24(%rbp)
movl 0x7af9b(%rip), %eax     # 0x82184
movl %eax, -0x28(%rbp)
movl 0x486d2(%rip), %eax     # 0x4f8c4
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movzbl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x7226
movq -0x10(%rbp), %rax
movzbl (%rax), %ecx
movl $0x1, %eax
cmpl %ecx, %eax
je 0x7226
movq -0x10(%rbp), %rax
movzbl (%rax), %ecx
movl $0x2, %eax
cmpl %ecx, %eax
jne 0x74d9
movl $0xa3000000, 0x48694(%rip) # imm = 0xA3000000
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x7259
movl 0x48680(%rip), %eax     # 0x4f8c4
orl $0x0, %eax
movl %eax, 0x48677(%rip)     # 0x4f8c4
movl $0x1, 0x7af29(%rip)     # 0x82180
jmp 0x72b3
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %ecx
movl $0x1, %eax
cmpl %ecx, %eax
jne 0x7285
movl 0x48654(%rip), %eax     # 0x4f8c4
orl $0x1, %eax
movl %eax, 0x4864b(%rip)     # 0x4f8c4
movl $0x2, 0x7aefd(%rip)     # 0x82180
jmp 0x72b1
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %ecx
movl $0x2, %eax
cmpl %ecx, %eax
jne 0x72af
movl 0x48628(%rip), %eax     # 0x4f8c4
orl $0x2, %eax
movl %eax, 0x4861f(%rip)     # 0x4f8c4
movl $0x4, 0x7aed1(%rip)     # 0x82180
jmp 0x72b1
jmp 0x72b3
movq -0x10(%rbp), %rax
movzbl (%rax), %ecx
movl $0x2, %eax
cmpl %ecx, %eax
jne 0x72cf
movl $0x0, 0x7aeb3(%rip)     # 0x82180
jmp 0x72de
movl 0x485ef(%rip), %eax     # 0x4f8c4
orl $0x10, %eax
movl %eax, 0x485e6(%rip)     # 0x4f8c4
movl 0x485e0(%rip), %eax     # 0x4f8c4
cmpl -0x2c(%rbp), %eax
je 0x7332
movl 0x48a21(%rip), %eax     # 0x4fd10
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x48a16(%rip)     # 0x4fd10
movslq %eax, %rcx
leaq 0x485cc(%rip), %rax     # 0x4f8d0
movb $0x1, (%rax,%rcx)
movl 0x485b6(%rip), %edi     # 0x4f8c4
callq 0x8510
movl 0x489fb(%rip), %eax     # 0x4fd14
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x489f0(%rip)     # 0x4fd14
movslq %eax, %rcx
leaq 0x489f2(%rip), %rax     # 0x4fd20
movb $0x1, (%rax,%rcx)
testb $0x1, 0x14e4f(%rip)     # 0x1c188
jne 0x735c
movl 0x7ae43(%rip), %eax     # 0x82184
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jne 0x735c
movl 0x7ae34(%rip), %ecx     # 0x82184
andl $0x3ff, %ecx            # imm = 0x3FF
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x73ba
movl 0x489ae(%rip), %eax     # 0x4fd10
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x489a3(%rip)     # 0x4fd10
movslq %eax, %rcx
leaq 0x48559(%rip), %rax     # 0x4f8d0
movb $0x5, (%rax,%rcx)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x8510
movl 0x48987(%rip), %eax     # 0x4fd14
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x4897c(%rip)     # 0x4fd14
movslq %eax, %rcx
leaq 0x4897e(%rip), %rax     # 0x4fd20
movb $0x2, (%rax,%rcx)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x7add1(%rip)     # 0x82184
movb $0x0, 0x14dce(%rip)     # 0x1c188
movq -0x10(%rbp), %rax
movzbl (%rax), %ecx
movl $0x1, %eax
cmpl %ecx, %eax
je 0x73da
movq -0x10(%rbp), %rax
movzbl (%rax), %ecx
movl $0x2, %eax
cmpl %ecx, %eax
jne 0x7456
movl 0x48930(%rip), %eax     # 0x4fd10
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x48925(%rip)     # 0x4fd10
movslq %eax, %rcx
leaq 0x484db(%rip), %rax     # 0x4f8d0
movb $0xd, (%rax,%rcx)
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %edi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %esi
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edx
callq 0x85c0
movl %eax, %edi
callq 0x8510
movq -0x10(%rbp), %rax
movzbl (%rax), %esi
movl $0x1, %edx
movl $0x3, %eax
movl $0x5, %ecx
cmpl %esi, %edx
cmovel %ecx, %eax
movb %al, %dl
movl 0x488d6(%rip), %eax     # 0x4fd14
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x488cb(%rip)     # 0x4fd14
movslq %eax, %rcx
leaq 0x488cd(%rip), %rax     # 0x4fd20
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rax
movzbl (%rax), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x7473
movq -0x10(%rbp), %rax
movzbl (%rax), %ecx
movl $0x2, %eax
cmpl %ecx, %eax
jne 0x74c2
movl 0x48897(%rip), %eax     # 0x4fd10
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x4888c(%rip)     # 0x4fd10
movslq %eax, %rcx
leaq 0x48442(%rip), %rax     # 0x4f8d0
movb $0xf, (%rax,%rcx)
movl 0x4887c(%rip), %eax     # 0x4fd14
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x48871(%rip)     # 0x4fd14
movslq %eax, %rcx
leaq 0x48873(%rip), %rax     # 0x4fd20
movb $0x4, (%rax,%rcx)
movslq 0x48860(%rip), %rax     # 0x4fd18
addq $0x4, %rax
movl %eax, 0x48856(%rip)     # 0x4fd18
movl 0x7acb8(%rip), %eax     # 0x82180
addl 0x7acb6(%rip), %eax     # 0x82184
movl %eax, 0x7acb0(%rip)     # 0x82184
jmp 0x7599
movq -0x10(%rbp), %rax
movzbl (%rax), %ecx
movl $0x4, %eax
cmpl %ecx, %eax
jne 0x753d
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
movb %al, %dl
movl 0x48818(%rip), %eax     # 0x4fd10
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x4880d(%rip)     # 0x4fd10
movslq %eax, %rcx
leaq 0x483c3(%rip), %rax     # 0x4f8d0
movb %dl, (%rax,%rcx)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edi
callq 0x8510
movl 0x487f2(%rip), %eax     # 0x4fd14
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x487e7(%rip)     # 0x4fd14
movslq %eax, %rcx
leaq 0x487e9(%rip), %rax     # 0x4fd20
movb $0x5, (%rax,%rcx)
jmp 0x7597
movq -0x10(%rbp), %rax
movl 0x4(%rax), %eax
orl $0x2, %eax
movb %al, %dl
movl 0x487c1(%rip), %eax     # 0x4fd10
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x487b6(%rip)     # 0x4fd10
movslq %eax, %rcx
leaq 0x4836c(%rip), %rax     # 0x4f8d0
movb %dl, (%rax,%rcx)
movl 0x487a7(%rip), %eax     # 0x4fd14
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x4879c(%rip)     # 0x4fd14
movslq %eax, %rcx
leaq 0x4879e(%rip), %rax     # 0x4fd20
movb $0x4, (%rax,%rcx)
movslq 0x4878b(%rip), %rax     # 0x4fd18
addq $0x4, %rax
movl %eax, 0x48781(%rip)     # 0x4fd18
jmp 0x7599
movl 0x48771(%rip), %eax     # 0x4fd10
cmpl -0x14(%rbp), %eax
jg 0x75bb
movl 0x4876e(%rip), %eax     # 0x4fd18
cmpl -0x14(%rbp), %eax
jg 0x75bb
cmpl $0xff, 0x4875b(%rip)    # 0x4fd14
jle 0x7602
movl -0x18(%rbp), %eax
movl %eax, 0x4874c(%rip)     # 0x4fd10
movl -0x1c(%rbp), %eax
movl %eax, 0x48747(%rip)     # 0x4fd14
movl -0x20(%rbp), %eax
movl %eax, 0x48742(%rip)     # 0x4fd18
movb -0x2d(%rbp), %al
andb $0x1, %al
movb %al, 0x14ba7(%rip)      # 0x1c188
movl -0x24(%rbp), %eax
movl %eax, 0x7ab96(%rip)     # 0x82180
movl -0x28(%rbp), %eax
movl %eax, 0x7ab91(%rip)     # 0x82184
movl -0x2c(%rbp), %eax
movl %eax, 0x482c8(%rip)     # 0x4f8c4
movb $0x0, -0x1(%rbp)
jmp 0x7606
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)