// File: /workspace/github_c_makefile_star2_O1/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, 0x5552a(%rip)     # 0x5ca00
je 0x7749
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc08, %rsp            # imm = 0xC08
leaq 0x5551c(%rip), %rbx     # 0x5ca10
movl $0x2000, %edx           # imm = 0x2000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x60e0
movl $0x0, 0x57503(%rip)     # 0x5ea10
movq %rsp, %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x60e0
callq 0xf486
movl 0x554d9(%rip), %esi     # 0x5ca00
testl %esi, %esi
jle 0x772e
addl $-0x3, %eax
movl $0x2, %edx
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %r15d, %r15d
movslq %r15d, %rcx
leaq 0x574d7(%rip), %rdi     # 0x5ea20
leaq (%rdi,%rcx,2), %r8
movb (%r8), %r9b
movslq %esi, %r10
xorl %edi, %edi
cmpb %r9b, (%r8,%rdi,2)
jne 0x756a
leaq (%rcx,%rdi), %r11
incq %r11
incq %rdi
cmpq %r10, %r11
jl 0x7555
cmpl $0x40, %edi
movl $0x40, %r8d
cmovael %r8d, %edi
leal (,%rax,8), %r8d
cmpl %r8d, %edi
cmovgel %r8d, %edi
cmpl $0x40, %edi
leaq 0x57490(%rip), %r8      # 0x5ea20
movb (%r8,%rcx,2), %r8b
movl %edi, %r9d
movl $0x0, %r10d
cmovel %r10d, %r9d
orb %r8b, %r9b
movslq %edx, %r10
movb %r9b, (%rsp,%r10)
testl %edi, %edi
jle 0x75ec
leal 0x1(%rdx), %r9d
movl %edi, %r10d
leaq 0x57463(%rip), %r11     # 0x5ea20
leaq (%r11,%rcx,2), %r11
incq %r11
xorl %r14d, %r14d
movb (%r11,%r14,2), %r13b
movl %r14d, %ecx
andb $0x7, %cl
shlb %cl, %r13b
movl %r14d, %ecx
shrl $0x3, %ecx
addl %r9d, %ecx
movslq %ecx, %rcx
orb %r13b, (%rsp,%rcx)
incq %r14
cmpq %r14, %r10
jne 0x75c7
testb %r8b, %r8b
jns 0x75fe
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
incl %ebp
addl %edi, %r15d
cmpl $0x2, %eax
jl 0x762e
cmpl %esi, %r15d
jne 0x771f
movb $0x14, (%rsp)
movb %bpl, 0x1(%rsp)
movq %rsp, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0xf4a0
xorl %edi, %edi
cmpb $0x0, (%rsp)
sete %dil
leaq 0x8bd5(%rip), %rsi      # 0x1022a
xorl %eax, %eax
callq 0x7bb4
testl %r12d, %r12d
jle 0x76fe
movl 0x573a5(%rip), %eax     # 0x5ea10
movl %r12d, %edx
movl $0x1, %edi
xorl %esi, %esi
movl 0x400(%rsp,%rsi,4), %r8d
testl %r8d, %r8d
jle 0x76da
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 0x7685
addl %r9d, %eax
movl %eax, 0x57336(%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 0x7675
xorl %ebp, %ebp
movl $0x400, %edx            # imm = 0x400
movq %rsp, %rdi
xorl %esi, %esi
callq 0x60e0
callq 0xf486
addl $-0x3, %eax
movl $0x2, %edx
xorl %r12d, %r12d
movl 0x552db(%rip), %esi     # 0x5ca00
cmpl %esi, %r15d
jl 0x753f
movl $0x0, 0x552c8(%rip)     # 0x5ca00
addq $0xc08, %rsp            # imm = 0xC08
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq