// File: /workspace/github_c_makefile_star2_O3/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, 0x55501(%rip)     # 0x5ca00
je 0x7769
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc08, %rsp            # imm = 0xC08
leaq 0x554f3(%rip), %rbx     # 0x5ca10
movl $0x2000, %edx           # imm = 0x2000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x60e0
movl $0x0, 0x574da(%rip)     # 0x5ea10
movq %rsp, %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x60e0
callq 0xf458
movl 0x554b0(%rip), %esi     # 0x5ca00
testl %esi, %esi
jle 0x774e
addl $-0x3, %eax
movl $0x2, %edx
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %r15d, %r15d
movslq %r15d, %rcx
leaq 0x574ae(%rip), %rdi     # 0x5ea20
leaq (%rdi,%rcx,2), %r9
movb (%r9), %r8b
movslq %esi, %r10
xorl %edi, %edi
cmpb %r8b, (%r9,%rdi,2)
jne 0x7593
leaq (%rcx,%rdi), %r11
incq %r11
incq %rdi
cmpq %r10, %r11
jl 0x757e
cmpl $0x40, %edi
movl $0x40, %r9d
cmovael %r9d, %edi
leal (,%rax,8), %r9d
cmpl %r9d, %edi
cmovgel %r9d, %edi
cmpl $0x40, %edi
movl %edi, %r9d
movl $0x0, %r10d
cmovel %r10d, %r9d
orb %r8b, %r9b
movslq %edx, %r10
movb %r9b, (%rsp,%r10)
testl %edi, %edi
jle 0x760b
leal 0x1(%rdx), %r9d
movl %edi, %r10d
leaq 0x57445(%rip), %r11     # 0x5ea20
leaq (%r11,%rcx,2), %r11
incq %r11
xorl %r14d, %r14d
movzbl (%r11,%r14,2), %r13d
movl %r14d, %ecx
andb $0x7, %cl
shll %cl, %r13d
movl %r14d, %ecx
shrl $0x3, %ecx
addl %r9d, %ecx
movslq %ecx, %rcx
orb %r13b, (%rsp,%rcx)
incq %r14
cmpq %r14, %r10
jne 0x75e5
testb %r8b, %r8b
jns 0x761d
movslq %r12d, %rcx
incl %r12d
movl %edi, 0x400(%rsp,%rcx,4)
leal 0x7(%rdi), %ecx
leal 0xe(%rdi), %r8d
testl %ecx, %ecx
cmovnsl %ecx, %r8d
sarl $0x3, %r8d
addl %r8d, %edx
incl %edx
movl %r8d, %ecx
incl %ecx
subl %ecx, %eax
incb %bpl
addl %edi, %r15d
cmpl $0x2, %eax
jl 0x764e
cmpl %esi, %r15d
jne 0x7745
movb $0x14, (%rsp)
movb %bpl, 0x1(%rsp)
movq %rsp, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0xf472
xorl %edi, %edi
cmpb $0x0, (%rsp)
sete %dil
leaq 0x8bb5(%rip), %rsi      # 0x1022a
xorl %eax, %eax
callq 0x7bcc
testl %r12d, %r12d
jle 0x771e
movl 0x57385(%rip), %eax     # 0x5ea10
movl %r12d, %edx
movl $0x1, %edi
xorl %esi, %esi
movl 0x400(%rsp,%rsi,4), %r8d
testl %r8d, %r8d
jle 0x76fa
xorl %r9d, %r9d
leal (%rax,%r9), %r10d
movl %r9d, %ecx
shrl $0x3, %ecx
addl %edi, %ecx
movslq %ecx, %rcx
movzbl (%rsp,%rcx), %ecx
movl %r9d, %r11d
andl $0x7, %r11d
xorl %ebp, %ebp
btl %r11d, %ecx
setb %bpl
movl %r10d, %ecx
andb $0x7, %cl
shll %cl, %ebp
leal (%rax,%r9), %ecx
addl $0x7, %ecx
testl %r10d, %r10d
cmovnsl %r10d, %ecx
sarl $0x3, %ecx
movslq %ecx, %rcx
orb %bpl, (%rcx,%rbx)
incl %r9d
cmpl %r9d, %r8d
jne 0x76a5
addl %r9d, %eax
movl %eax, 0x57316(%rip)     # 0x5ea10
leal 0x7(%r8), %ecx
addl $0xe, %r8d
testl %ecx, %ecx
cmovnsl %ecx, %r8d
sarl $0x3, %r8d
addl %edi, %r8d
incq %rsi
movl %r8d, %edi
cmpq %rdx, %rsi
jne 0x7695
xorl %ebp, %ebp
movl $0x400, %edx            # imm = 0x400
movq %rsp, %rdi
xorl %esi, %esi
callq 0x60e0
callq 0xf458
addl $-0x3, %eax
movl $0x2, %edx
movl 0x552be(%rip), %esi     # 0x5ca00
xorl %r12d, %r12d
cmpl %esi, %r15d
jl 0x7568
movl $0x0, 0x552a8(%rip)     # 0x5ca00
addq $0xc08, %rsp            # imm = 0xC08
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq