// File: /workspace/github_c_makefile_star2_O2/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:
cmpl $0x0, 0x5681e(%rip)     # 0x5da00
je 0x7433
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc18, %rsp            # imm = 0xC18
leaq 0x56810(%rip), %rbx     # 0x5da10
movl $0x2000, %edx           # imm = 0x2000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x60e0
andl $0x0, 0x587fa(%rip)     # 0x5fa10
leaq 0x10(%rsp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x60e0
callq 0xed14
movl %eax, %esi
pushq $0x2
popq %rax
leaq 0x587e8(%rip), %r12     # 0x5fa20
pushq $0x8
popq %rbp
xorl %r14d, %r14d
addl $-0x3, %esi
movslq 0x567b8(%rip), %rdi     # 0x5da00
movl %eax, %r8d
xorl %r9d, %r9d
movq $0x0, (%rsp)
cmpl %edi, %r14d
jge 0x741b
movslq %r14d, %rcx
leaq (%r12,%rcx,2), %rdx
movb (%rdx), %al
xorl %r10d, %r10d
leaq (%rcx,%r10), %r11
cmpq %rdi, %r11
jge 0x727f
cmpb %al, (%rdx,%r10,2)
jne 0x727f
incq %r10
jmp 0x726b
movl %r14d, 0xc(%rsp)
cmpl $0x40, %r10d
pushq $0x40
popq %rdx
cmovael %edx, %r10d
leal (,%rsi,8), %edx
cmpl %edx, %r10d
cmovgel %edx, %r10d
cmpl $0x40, %r10d
movl %r10d, %r11d
movl $0x0, %edx
cmovel %edx, %r11d
orb %al, %r11b
movslq %r8d, %rdx
movb %r11b, 0x10(%rsp,%rdx)
incl %edx
movslq %r10d, %r11
movq %r12, %r14
leaq (%r12,%rcx,2), %r15
incq %r15
xorl %r13d, %r13d
cmpq %r11, %r13
jge 0x72f2
movzbl (%r15,%r13,2), %r12d
movl %r13d, %ecx
andb $0x7, %cl
shll %cl, %r12d
movl %r13d, %ecx
shrl $0x3, %ecx
addl %edx, %ecx
movslq %ecx, %rcx
orb %r12b, 0x10(%rsp,%rcx)
incq %r13
jmp 0x72ca
testb %al, %al
jns 0x730b
movq (%rsp), %rcx
movslq %ecx, %rax
incl %ecx
movq %rcx, (%rsp)
movl %r10d, 0x410(%rsp,%rax,4)
movq %r14, %r12
leal 0x7(%r10), %eax
cltd
idivl %ebp
addl %eax, %r8d
incl %r8d
incl %eax
subl %eax, %esi
incb %r9b
movl 0xc(%rsp), %r14d
addl %r10d, %r14d
cmpl $0x2, %esi
jl 0x7338
cmpl %edi, %r14d
jne 0x7256
movb $0x14, 0x10(%rsp)
movb %r9b, 0x11(%rsp)
leaq 0x10(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movl %r8d, %edx
callq 0xed2e
xorl %edi, %edi
cmpb $0x0, 0x10(%rsp)
sete %dil
leaq 0x8ec4(%rip), %rsi      # 0x1022a
xorl %eax, %eax
callq 0x793d
movl 0x5869d(%rip), %esi     # 0x5fa10
movq (%rsp), %r13
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
xorl %r8d, %r8d
pushq $0x1
popq %r9
cmpq %r13, %r8
je 0x73fb
movl 0x410(%rsp,%r8,4), %edi
xorl %r10d, %r10d
testl %edi, %edi
movl $0x0, %r11d
cmovgl %edi, %r11d
cmpl %r10d, %r11d
je 0x73eb
movl %r10d, %eax
shrl $0x3, %eax
addl %r9d, %eax
cltq
movzbl 0x10(%rsp,%rax), %eax
movl %r10d, %ecx
andl $0x7, %ecx
xorl %r15d, %r15d
btl %ecx, %eax
setb %r15b
movl %esi, %ecx
andb $0x7, %cl
shll %cl, %r15d
movl %esi, %eax
cltd
idivl %ebp
cltq
orb %r15b, (%rax,%rbx)
incl %esi
movl %esi, 0x5862a(%rip)     # 0x5fa10
incl %r10d
jmp 0x73a6
addl $0x7, %edi
movl %edi, %eax
cltd
idivl %ebp
addl %eax, %r9d
incq %r8
jmp 0x738a
movl $0x400, %edx            # imm = 0x400
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x60e0
callq 0xed14
movl %eax, %esi
pushq $0x2
popq %rax
jmp 0x723e
andl $0x0, 0x565de(%rip)     # 0x5da00
addq $0xc18, %rsp            # imm = 0xC18
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq