// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/ecovlp.cpp
// Function: trace_cns_bp(cns_gfa*, unsigned long, char*, unsigned long, asg32_v*, unsigned int, unsigned int*, unsigned long, unsigned int)
uint32_t trace_cns_bp(cns_gfa *cns, uint64_t s0, char *tstr, uint64_t tl, asg32_v* b32, uint32_t plus0, uint32_t *rn, uint64_t max_trace, uint32_t av_bp)
{
    (*rn) = s0;
    if(tl <= 0) return 0;
    // fprintf(stderr, "\n[M::%s] tl::%lu\n", __func__, tl);
    uint32_t k, i, s, e, m, bp, nm, bi, bn0, src, des, ff = 0; b32->n = 0;

    kv_push(uint32_t, (*b32), s0); kv_push(uint32_t, (*b32), ((uint32_t)-1)); nm = 2;
    // if(s0 == 2863) {
    //     fprintf(stderr, "***0***[M::%s] s::%lu\tb32->n::%u\n", __func__, s0, (uint32_t)b32->n);
    // }

    for (i = 0; (i < tl) && (!ff); i++) {
        bp = seq_nt6_table[(uint32_t)(tstr[i])]; bn0 = b32->n;
        for (bi = bn0 - nm; bi < bn0; bi += 2) {
            m = b32->a[bi]; s = 0; e = (*cns).a[m].arc.nou;
            for (k = s; k < e; k++) {
                if((*cns).a[m].arc.a[k].v == 0 || (*cns).a[m].arc.a[k].v == 1) continue;
                if(av_bp && (*cns).a[m].arc.a[k].v >= (*cns).bb0 && (*cns).a[m].arc.a[k].v < (*cns).bb1) continue;///no backbone
                if((*cns).a[(*cns).a[m].arc.a[k].v].c == bp) {
                    kv_push(uint32_t, (*b32), (*cns).a[m].arc.a[k].v);
                    kv_push(uint32_t, (*b32), bi);

                    // if(s0 == 2863) {
                    //     fprintf(stderr, "***1***[M::%s] s::%u\tb32->n::%u\n", __func__, (*cns).a[m].arc.a[k].v, (uint32_t)b32->n);
                    // }
                    // if((i + 1) == tl) break;///quick end
                    // if(b32->n > max_trace) break;///redue the size of b32
                    if(((i + 1) == tl) || (b32->n > max_trace)) {
                        ff = 1; break;
                    }
                }
            }
            if(ff) break;
        }

        if(b32->n <= bn0) {///no node
            break;
        } else {
            nm = b32->n - bn0;
        } 
    }
    // fprintf(stderr, "[M::%s] b32->n::%u, nm::%u\n", __func__, (uint32_t)b32->n, nm);

    // if(s0 == 2863) {
    //     fprintf(stderr, "[M::%s] i::%u\tnm::%u\tb32->n::%u\n", __func__, i, nm, (uint32_t)b32->n);
    // }
    if(i > 0 && nm > 0) {
        (*rn) = b32->a[b32->n - nm];
        for (bi = b32->n - nm; b32->a[bi + 1] != ((uint32_t)-1); bi = b32->a[bi + 1]) {
            // fprintf(stderr, "[M::%s] bi::%u, p_bi::%u\n", __func__, bi, b32->a[bi + 1]);
            des = b32->a[bi]; src = b32->a[b32->a[bi + 1]]; bp = ((src!=s0)?(1):(plus0));
            m = add_cns_arc(cns, src, des, 1, bp); assert(m);
            m = add_cns_arc(cns, des, src, 0, bp); assert(m);
            (*cns).a[des].sc++;

            // if(s0 == 2863) {
            //     fprintf(stderr, "***2***[M::%s] src::%u\tdes::%u\n", __func__, src, des);
            // }
        }
    } else {
        i = 0;
    }

    return i;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, 0x38(%rsp)
movq 0x80(%rsp), %rax
movq %rsi, 0x18(%rsp)
movl %esi, (%rax)
movq %rcx, 0x30(%rsp)
testq %rcx, %rcx
je 0x1819bf
movl %r9d, %ebx
movq %r8, %r14
movq %rdi, %r12
movq $0x0, (%r8)
movq 0x8(%r8), %rcx
testq %rcx, %rcx
je 0x181639
movq 0x10(%r14), %rax
xorl %esi, %esi
jmp 0x18165a
movq $0x2, 0x8(%r14)
movq 0x10(%r14), %rdi
movl $0x8, %esi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rsi
movq 0x8(%r14), %rcx
leaq 0x1(%rsi), %rdx
movq %rdx, (%r14)
movq 0x18(%rsp), %rdi
movl %edi, (%rax,%rsi,4)
cmpq %rcx, %rdx
movl %ebx, 0xc(%rsp)
jne 0x181699
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rdx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rdx
movl 0x90(%rsp), %r8d
leaq 0x1(%rdx), %r9
movq %r9, (%r14)
movl $0xffffffff, (%rax,%rdx,4) # imm = 0xFFFFFFFF
movl $0x2, %edi
xorl %r13d, %r13d
movl %r9d, %r15d
movl %edi, 0x14(%rsp)
subl %edi, %r15d
cmpl %r9d, %r15d
movq %r13, 0x20(%rsp)
movq %r9, 0x28(%rsp)
jae 0x18183a
movq 0x38(%rsp), %rcx
movsbq (%rcx,%r13), %rcx
movl %ecx, %ecx
leaq 0x113f49(%rip), %rdx    # 0x295630
movzbl (%rdx,%rcx), %r10d
leal 0x1(%r13), %ecx
movq %rcx, 0x40(%rsp)
movq %r9, %rcx
movl %r10d, 0x10(%rsp)
movl %r15d, %edx
movl (%rax,%rdx,4), %edx
movq 0x10(%r12), %rsi
leaq (%rdx,%rdx,4), %rbx
movl 0x18(%rsi,%rbx,8), %r13d
testq %r13, %r13
je 0x181829
xorl %ebp, %ebp
movq 0x10(%r12), %rsi
movq 0x20(%rsi,%rbx,8), %rdx
movl (%rdx,%rbp,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpl $0x2, %edx
jb 0x18181d
testl %r8d, %r8d
je 0x18174f
cmpl 0x28(%r12), %edx
jb 0x18174f
cmpl 0x2c(%r12), %edx
jb 0x18181d
leaq (%rdx,%rdx,4), %rdi
movl (%rsi,%rdi,8), %esi
andl $0x3, %esi
cmpl %r10d, %esi
jne 0x18181d
movq 0x8(%r14), %rdi
cmpq %rdi, %rcx
jne 0x1817b7
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x10(%rsp), %r10d
movl 0x90(%rsp), %r8d
movq %rax, 0x10(%r14)
movq 0x10(%r12), %rcx
movq 0x20(%rcx,%rbx,8), %rdx
movq (%r14), %rcx
movq 0x8(%r14), %rdi
movl (%rdx,%rbp,8), %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %edx
leaq 0x1(%rcx), %rsi
movq %rsi, (%r14)
movl %edx, (%rax,%rcx,4)
cmpq %rdi, %rsi
jne 0x1817fa
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0x10(%rsp), %r10d
movl 0x90(%rsp), %r8d
movq %rax, 0x10(%r14)
movq (%r14), %rsi
leaq 0x1(%rsi), %rcx
xorl %edx, %edx
movq %rcx, (%r14)
movl %r15d, (%rax,%rsi,4)
movq 0x40(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x18183f
cmpq 0x88(%rsp), %rcx
ja 0x18183f
incq %rbp
cmpq %rbp, %r13
jne 0x18171c
addl $0x2, %r15d
cmpl 0x28(%rsp), %r15d
jb 0x1816fd
jmp 0x18183d
movq %r9, %rcx
movb $0x1, %dl
movq 0x28(%rsp), %r9
movl %r9d, %esi
cmpq %rsi, %rcx
jbe 0x181870
movl %ecx, %edi
subl %r9d, %edi
movq 0x20(%rsp), %r13
incl %r13d
cmpq 0x30(%rsp), %r13
setb %sil
movq %rcx, %r9
testb %dl, %sil
jne 0x1816b7
jmp 0x181879
movq 0x20(%rsp), %r13
movl 0x14(%rsp), %edi
testl %r13d, %r13d
sete %dl
testl %edi, %edi
sete %sil
orb %dl, %sil
jne 0x1819bf
movl %edi, %edx
movq %rcx, %rsi
subq %rdx, %rsi
movl (%rax,%rsi,4), %edx
movq 0x80(%rsp), %rsi
movl %edx, (%rsi)
subl %edi, %ecx
leal 0x1(%rcx), %edx
movl (%rax,%rdx,4), %edi
cmpl $-0x1, %edi
je 0x1819c2
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movl 0xc(%rsp), %r15d
movl %edi, %edi
movl (%rax,%rdi,4), %r9d
cmpq 0x18(%rsp), %r9
movl $0x1, %r8d
cmovel %r15d, %r8d
movq 0x10(%r12), %rdi
leaq (%r9,%r9,4), %r11
movl 0x18(%rdi,%r11,8), %r10d
testq %r10, %r10
je 0x1819d4
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movq 0x20(%rdi,%r11,8), %rcx
movl (%rcx), %r11d
andl %esi, %r11d
cmpl %eax, %r11d
jne 0x181905
addl %r8d, 0x4(%rcx)
jmp 0x181932
leaq -0x1(%r10), %rbx
xorl %r11d, %r11d
cmpq %r11, %rbx
je 0x181926
movl 0x8(%rcx,%r11,8), %ebp
andl %esi, %ebp
incq %r11
cmpl %eax, %ebp
jne 0x18190c
addl %r8d, 0x4(%rcx,%r11,8)
jmp 0x181929
incq %r11
cmpq %r10, %r11
jae 0x1819d4
leaq (%rax,%rax,4), %rax
movq 0x8(%rdi,%rax,8), %r11
movq 0x18(%rdi,%rax,8), %rcx
movl %ecx, %r10d
subl %r11d, %r10d
jae 0x1819f3
movl %ecx, %ecx
movq 0x20(%rdi,%rax,8), %rbx
movl (%rbx,%rcx,8), %ebp
andl %esi, %ebp
cmpl %r9d, %ebp
jne 0x181964
addl %r8d, 0x4(%rbx,%rcx,8)
jmp 0x1819a3
movl %r11d, %r11d
leaq (%rbx,%rcx,8), %rbx
addq $0x4, %rbx
incl %r10d
xorl %r15d, %r15d
cmpl %r15d, %r10d
je 0x181993
movl 0x4(%rbx), %ebp
andl %esi, %ebp
decq %r15
addq $0x8, %rbx
cmpl %r9d, %ebp
jne 0x181975
subq %r15, %rcx
addl %r8d, (%rbx)
jmp 0x181999
subq %r15, %rcx
incq %rcx
cmpq %r11, %rcx
movl 0xc(%rsp), %r15d
jae 0x1819f3
addl $0x8, (%rdi,%rax,8)
movq 0x10(%r14), %rax
movl (%rax,%rdx,4), %ecx
leal 0x1(%rcx), %edx
movl (%rax,%rdx,4), %edi
cmpl $-0x1, %edi
jne 0x1818be
jmp 0x1819c2
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xafe7f(%rip), %rdi     # 0x23185a
leaq 0xbc659(%rip), %rsi     # 0x23e03b
leaq 0xbc6af(%rip), %rcx     # 0x23e098
movl $0x2f3, %edx            # imm = 0x2F3
callq 0x2110
leaq 0xafe60(%rip), %rdi     # 0x23185a
leaq 0xbc63a(%rip), %rsi     # 0x23e03b
leaq 0xbc690(%rip), %rcx     # 0x23e098
movl $0x2f4, %edx            # imm = 0x2F4
callq 0x2110