// File: /workspace/github_cpp_makefile_star2_O2/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
movl %r9d, 0x20(%rsp)
movq %rdx, 0x40(%rsp)
movq 0x80(%rsp), %rax
movq %rsi, 0x28(%rsp)
movl %esi, (%rax)
movq %rcx, 0x30(%rsp)
testq %rcx, %rcx
je 0x13e286
movq %r8, %r14
movq %rdi, %r12
andq $0x0, (%r8)
movq 0x8(%r8), %rcx
testq %rcx, %rcx
je 0x13dfc4
movq 0x10(%r14), %rax
xorl %esi, %esi
jmp 0x13dfe3
movq $0x2, 0x8(%r14)
movq 0x10(%r14), %rdi
pushq $0x8
popq %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rsi
movq 0x8(%r14), %rcx
leaq 0x1(%rsi), %rdx
movq %rdx, (%r14)
movq 0x28(%rsp), %rdi
movl %edi, (%rax,%rsi,4)
pushq $0x2
popq %rdi
cmpq %rcx, %rdx
jne 0x13e01c
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %rdi, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rdx
leaq 0x1(%rdx), %rdi
movq %rdi, (%r14)
orl $-0x1, (%rax,%rdx,4)
movl 0x90(%rsp), %r8d
xorl %ecx, %ecx
pushq $0x2
popq %rsi
xorl %edx, %edx
movq %rcx, 0x10(%rsp)
movl %ecx, %ecx
cmpq 0x30(%rsp), %rcx
jae 0x13e1ef
testl %edx, %edx
jne 0x13e1ef
movq 0x40(%rsp), %rdx
movsbq (%rdx,%rcx), %rcx
movl %ecx, %ecx
leaq 0x1155cd(%rip), %rdx    # 0x253630
movzbl (%rdx,%rcx), %r15d
movl %edi, %r9d
movl %esi, 0x24(%rsp)
subl %esi, %r9d
movq 0x10(%rsp), %rcx
incl %ecx
movq %rcx, 0x38(%rsp)
movq %rdi, %rcx
movq %rdi, 0x18(%rsp)
cmpl %edi, %r9d
jae 0x13e1e8
movl %r9d, %edx
movl (%rax,%rdx,4), %edx
movq 0x10(%r12), %rsi
imulq $0x28, %rdx, %rbx
movl 0x18(%rsi,%rbx), %r13d
xorl %ebp, %ebp
movl %r9d, 0xc(%rsp)
cmpq %rbp, %r13
je 0x13e1b6
movq 0x10(%r12), %rsi
movq 0x20(%rsi,%rbx), %rdx
movl (%rdx,%rbp,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpl $0x2, %edx
jb 0x13e1ae
testl %r8d, %r8d
je 0x13e0e6
cmpl 0x28(%r12), %edx
jb 0x13e0e6
cmpl 0x2c(%r12), %edx
jb 0x13e1ae
imulq $0x28, %rdx, %rdi
movl (%rsi,%rdi), %esi
andl $0x3, %esi
cmpl %r15d, %esi
jne 0x13e1ae
movq 0x8(%r14), %rdi
cmpq %rdi, %rcx
jne 0x13e14c
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0xc(%rsp), %r9d
movl 0x90(%rsp), %r8d
movq %rax, 0x10(%r14)
movq 0x10(%r12), %rcx
movq 0x20(%rcx,%rbx), %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 0x13e18d
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movl 0xc(%rsp), %r9d
movl 0x90(%rsp), %r8d
movq %rax, 0x10(%r14)
movq (%r14), %rsi
leaq 0x1(%rsi), %rcx
movq %rcx, (%r14)
movl %r9d, (%rax,%rsi,4)
movq 0x38(%rsp), %rdx
cmpq 0x30(%rsp), %rdx
je 0x13e1c4
cmpq 0x88(%rsp), %rcx
ja 0x13e1c4
incq %rbp
jmp 0x13e0aa
addl $0x2, %r9d
movq 0x18(%rsp), %rdi
jmp 0x13e086
pushq $0x1
popq %rdx
movq 0x18(%rsp), %rdi
movl %edi, %esi
cmpq %rsi, %rcx
movl 0x24(%rsp), %esi
jbe 0x13e1ec
movl %ecx, %esi
subl %edi, %esi
movq %rcx, %rdi
movq 0x38(%rsp), %rcx
jmp 0x13e036
xorl %edx, %edx
jmp 0x13e1cc
movq %rcx, %rdi
cmpl $0x0, 0x10(%rsp)
sete %cl
testl %esi, %esi
sete %dl
orb %cl, %dl
jne 0x13e286
movl %esi, %ecx
movq %rdi, %rdx
subq %rcx, %rdx
movl (%rax,%rdx,4), %ecx
movq 0x80(%rsp), %rdx
movl %ecx, (%rdx)
subl %esi, %edi
leal 0x1(%rdi), %r13d
movl (%rax,%r13,4), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq %rdx, %rcx
je 0x13e28a
movl %edi, %edx
movl (%rax,%rdx,4), %ebp
movl (%rax,%rcx,4), %r15d
cmpq 0x28(%rsp), %r15
pushq $0x1
popq %rcx
movl %ecx, %ebx
cmovel 0x20(%rsp), %ebx
movq %r12, %rdi
movl %r15d, %esi
movl %ebp, %edx
movl %ebx, %r8d
callq 0x14f092
testl %eax, %eax
je 0x13e29e
movq %r12, %rdi
movl %ebp, %esi
movl %r15d, %edx
xorl %ecx, %ecx
movl %ebx, %r8d
callq 0x14f092
testl %eax, %eax
je 0x13e2bd
movq 0x10(%r12), %rax
imulq $0x28, %rbp, %rcx
addl $0x8, (%rax,%rcx)
movq 0x10(%r14), %rax
movl (%rax,%r13,4), %edi
jmp 0x13e21b
xorl %eax, %eax
jmp 0x13e28f
movq 0x10(%rsp), %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa5505(%rip), %rdi     # 0x1e37aa
leaq 0xb1c4f(%rip), %rsi     # 0x1efefb
leaq 0xb1ca5(%rip), %rcx     # 0x1eff58
movl $0x2f3, %edx            # imm = 0x2F3
callq 0x2110
leaq 0xa54e6(%rip), %rdi     # 0x1e37aa
leaq 0xb1c30(%rip), %rsi     # 0x1efefb
leaq 0xb1c86(%rip), %rcx     # 0x1eff58
movl $0x2f4, %edx            # imm = 0x2F4
callq 0x2110