// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/dap.c
// Function: dap_jtag_flush
void dap_jtag_flush(void)
{
  uint8_t buf[DBG_MAX_EP_SIZE];
  int tdo_size[DBG_MAX_EP_SIZE / 2];
  int tdo_count, req_count, req_size, index, remaining;

  if (0 == dap_jtag_request_count)
    return;

  memset(dap_jtag_response_buf, 0, sizeof(dap_jtag_response_buf));
  dap_jtag_response_count = 0;

  memset(buf, 0, sizeof(buf));

  index     = 0;
  tdo_count = 0;
  req_count = 0;
  req_size  = 2; // Command and Count
  remaining = dbg_get_packet_size() - req_size - 1;

  while (index < dap_jtag_request_count)
  {
    int count = 0;
    int size;

    for (int i = index; i < dap_jtag_request_count; i++, count++)
    {
      if (dap_jtag_request[i].opt != dap_jtag_request[index].opt)
        break;
    }

    if (count > 64)
      count = 64;

    if (count > (remaining * 8))
      count = remaining * 8;

    buf[req_size] = JTAG_SEQUENCE_COUNT(count) | dap_jtag_request[index].opt;

    for (int i = 0; i < count; i++)
      buf[req_size + 1 + i / 8] |= (dap_jtag_request[index + i].tdi << (i % 8));

    if (dap_jtag_request[index].opt & JTAG_SEQUENCE_TDO)
      tdo_size[tdo_count++] = count;

    size = 1 + (count + 7) / 8;
    req_size += size;
    remaining -= size;

    req_count++;
    index += count;

    if (remaining < 2 || index == dap_jtag_request_count)
    {
      int tdo_index = 1;

      buf[0] = ID_DAP_JTAG_SEQUENCE;
      buf[1] = req_count;

      dbg_dap_cmd(buf, sizeof(buf), req_size);
      check(DAP_OK == buf[0], "JTAG_SEQUENCE failed");

      for (int i = 0; i < tdo_count; i++)
      {
        for (int j = 0; j < tdo_size[i]; j++)
        {
          int bit = (buf[tdo_index + j / 8] & (1 << (j % 8))) ? 1 : 0;
          dap_jtag_response_buf[dap_jtag_response_count / 8] |= (bit << (dap_jtag_response_count % 8));
          dap_jtag_response_count++;
        }

        tdo_index += (tdo_size[i] + 7) / 8;
      }

      memset(buf, 0, sizeof(buf));

      tdo_count = 0;
      req_count = 0;
      req_size  = 2; // Command and Count
      remaining = dbg_get_packet_size() - req_size - 1;
    }
  }

  dap_jtag_request_count = 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc60, %rsp            # imm = 0xC60
xorl %eax, %eax
cmpl 0x5858d(%rip), %eax     # 0x60160
jne 0x7bda
jmp 0x80b3
leaq 0x5858f(%rip), %rdi     # 0x60170
xorl %esi, %esi
movl $0x2000, %edx           # imm = 0x2000
callq 0x60f0
movl $0x0, 0x5a579(%rip)     # 0x62170
leaq -0x400(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x60f0
movl $0x0, -0xc10(%rbp)
movl $0x0, -0xc04(%rbp)
movl $0x0, -0xc08(%rbp)
movl $0x2, -0xc0c(%rbp)
callq 0x13090
subl -0xc0c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0xc14(%rbp)
movl -0xc10(%rbp), %eax
cmpl 0x5850e(%rip), %eax     # 0x60160
jge 0x80a9
movl $0x0, -0xc18(%rbp)
movl -0xc10(%rbp), %eax
movl %eax, -0xc20(%rbp)
movl -0xc20(%rbp), %eax
cmpl 0x584e6(%rip), %eax     # 0x60160
jge 0x7cc8
movslq -0xc20(%rbp), %rcx
leaq 0x5a4f6(%rip), %rax     # 0x62180
movzbl (%rax,%rcx,2), %eax
movslq -0xc10(%rbp), %rdx
leaq 0x5a4e4(%rip), %rcx     # 0x62180
movzbl (%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
je 0x7ca6
jmp 0x7cc8
jmp 0x7ca8
movl -0xc20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc20(%rbp)
movl -0xc18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc18(%rbp)
jmp 0x7c6e
cmpl $0x40, -0xc18(%rbp)
jle 0x7cdb
movl $0x40, -0xc18(%rbp)
movl -0xc18(%rbp), %eax
movl -0xc14(%rbp), %ecx
shll $0x3, %ecx
cmpl %ecx, %eax
jle 0x7cfd
movl -0xc14(%rbp), %eax
shll $0x3, %eax
movl %eax, -0xc18(%rbp)
cmpl $0x40, -0xc18(%rbp)
jne 0x7d10
xorl %eax, %eax
movl %eax, -0xc38(%rbp)
jmp 0x7d1c
movl -0xc18(%rbp), %eax
movl %eax, -0xc38(%rbp)
movl -0xc38(%rbp), %eax
movslq -0xc10(%rbp), %rdx
leaq 0x5a450(%rip), %rcx     # 0x62180
movzbl (%rcx,%rdx,2), %ecx
orl %ecx, %eax
movb %al, %cl
movslq -0xc0c(%rbp), %rax
movb %cl, -0x400(%rbp,%rax)
movl $0x0, -0xc24(%rbp)
movl -0xc24(%rbp), %eax
cmpl -0xc18(%rbp), %eax
jge 0x7df5
movl -0xc10(%rbp), %eax
addl -0xc24(%rbp), %eax
movslq %eax, %rcx
leaq 0x5a408(%rip), %rax     # 0x62180
movzbl 0x1(%rax,%rcx,2), %eax
movl %eax, -0xc44(%rbp)
movl -0xc24(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl -0xc44(%rbp), %eax
movl %edx, %ecx
shll %cl, %eax
movl %eax, -0xc40(%rbp)
movl -0xc0c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc3c(%rbp)
movl -0xc24(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl -0xc40(%rbp), %edx
movl %eax, %ecx
movl -0xc3c(%rbp), %eax
addl %ecx, %eax
cltq
movzbl -0x400(%rbp,%rax), %ecx
orl %edx, %ecx
movb %cl, -0x400(%rbp,%rax)
movl -0xc24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc24(%rbp)
jmp 0x7d50
movslq -0xc10(%rbp), %rcx
leaq 0x5a37d(%rip), %rax     # 0x62180
movzbl (%rax,%rcx,2), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x7e31
movl -0xc18(%rbp), %ecx
movl -0xc04(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0xc04(%rbp)
cltq
movl %ecx, -0xc00(%rbp,%rax,4)
movl -0xc18(%rbp), %eax
addl $0x7, %eax
movl $0x8, %ecx
cltd
idivl %ecx
addl $0x1, %eax
movl %eax, -0xc1c(%rbp)
movl -0xc1c(%rbp), %eax
addl -0xc0c(%rbp), %eax
movl %eax, -0xc0c(%rbp)
movl -0xc1c(%rbp), %ecx
movl -0xc14(%rbp), %eax
subl %ecx, %eax
movl %eax, -0xc14(%rbp)
movl -0xc08(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc08(%rbp)
movl -0xc18(%rbp), %eax
addl -0xc10(%rbp), %eax
movl %eax, -0xc10(%rbp)
cmpl $0x2, -0xc14(%rbp)
jl 0x7ead
movl -0xc10(%rbp), %eax
cmpl 0x582b9(%rip), %eax     # 0x60160
jne 0x80a4
movl $0x1, -0xc28(%rbp)
movb $0x14, -0x400(%rbp)
movl -0xc08(%rbp), %eax
movb %al, -0x3ff(%rbp)
leaq -0x400(%rbp), %rdi
movl -0xc0c(%rbp), %edx
movl $0x400, %esi            # imm = 0x400
callq 0x130d0
movzbl -0x400(%rbp), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
sete %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0xc312(%rip), %rsi      # 0x1420e
xorl %eax, %eax
callq 0x8880
movl $0x0, -0xc2c(%rbp)
movl -0xc2c(%rbp), %eax
cmpl -0xc04(%rbp), %eax
jge 0x805f
movl $0x0, -0xc30(%rbp)
movl -0xc30(%rbp), %eax
movslq -0xc2c(%rbp), %rcx
cmpl -0xc00(%rbp,%rcx,4), %eax
jge 0x8026
movl -0xc28(%rbp), %eax
movl %eax, -0xc54(%rbp)
movl -0xc30(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0xc54(%rbp), %eax
addl %ecx, %eax
cltq
movzbl -0x400(%rbp,%rax), %eax
movl %eax, -0xc50(%rbp)
movl -0xc30(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %edx, %ecx
movl -0xc50(%rbp), %edx
movl $0x1, %eax
shll %cl, %eax
andl %eax, %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0xc34(%rbp)
movl -0xc34(%rbp), %eax
movl %eax, -0xc4c(%rbp)
movl 0x5a1b5(%rip), %eax     # 0x62170
movl $0x8, %ecx
cltd
idivl %ecx
movl -0xc4c(%rbp), %eax
movl %edx, %ecx
shll %cl, %eax
movl %eax, -0xc48(%rbp)
movl 0x5a197(%rip), %eax     # 0x62170
movl $0x8, %ecx
cltd
idivl %ecx
movl -0xc48(%rbp), %edx
movslq %eax, %rcx
leaq 0x5817f(%rip), %rax     # 0x60170
movzbl (%rax,%rcx), %eax
orl %edx, %eax
movb %al, %dl
leaq 0x58170(%rip), %rax     # 0x60170
movb %dl, (%rax,%rcx)
movl 0x5a167(%rip), %eax     # 0x62170
addl $0x1, %eax
movl %eax, 0x5a15e(%rip)     # 0x62170
movl -0xc30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc30(%rbp)
jmp 0x7f29
movslq -0xc2c(%rbp), %rax
movl -0xc00(%rbp,%rax,4), %eax
addl $0x7, %eax
movl $0x8, %ecx
cltd
idivl %ecx
addl -0xc28(%rbp), %eax
movl %eax, -0xc28(%rbp)
movl -0xc2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc2c(%rbp)
jmp 0x7f0d
leaq -0x400(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x60f0
movl $0x0, -0xc04(%rbp)
movl $0x0, -0xc08(%rbp)
movl $0x2, -0xc0c(%rbp)
callq 0x13090
subl -0xc0c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0xc14(%rbp)
jmp 0x7c46
movl $0x0, 0x580ad(%rip)     # 0x60160
addq $0xc60, %rsp            # imm = 0xC60
popq %rbp
retq
nopl (%rax)