// File: /workspace/github_cpp_makefile_star2_O1/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, 0x20(%rsp)
movl %esi, (%rax)
movq %rcx, 0x30(%rsp)
testq %rcx, %rcx
je 0x17ad15
movl %r9d, %ebx
movq %r8, %r14
movq %rdi, %r9
movq $0x0, (%r8)
cmpq $0x0, 0x8(%r8)
movq %rdi, (%rsp)
jne 0x17a998
movq $0x2, 0x8(%r14)
movq 0x10(%r14), %rdi
movl $0x8, %esi
callq 0x2370
movq (%rsp), %r9
movq %rax, 0x10(%r14)
movl %ebx, 0x18(%rsp)
movq (%r14), %rax
movq 0x10(%r14), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, (%r14)
movq 0x20(%rsp), %rdx
movl %edx, (%rdi,%rax,4)
movq 0x8(%r14), %rax
cmpq %rax, %rcx
jne 0x17a9e0
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
callq 0x2370
movq (%rsp), %r9
movq %rax, 0x10(%r14)
movl 0x90(%rsp), %r12d
movq (%r14), %rax
movq 0x10(%r14), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%r14)
movl $0xffffffff, (%rcx,%rax,4) # imm = 0xFFFFFFFF
movl $0x2, %r8d
xorl %eax, %eax
xorl %esi, %esi
movq %rax, 0x10(%rsp)
movq (%r14), %rax
movl %eax, %r10d
movl %r8d, 0x1c(%rsp)
subl %r8d, %r10d
movq %rax, 0x28(%rsp)
cmpl %eax, %r10d
jae 0x17ab91
movq 0x10(%rsp), %rcx
movq 0x38(%rsp), %rax
movsbq (%rax,%rcx), %rax
movl %eax, %eax
leaq 0x10fbf0(%rip), %rdx    # 0x28a630
movzbl (%rdx,%rax), %r15d
leal 0x1(%rcx), %eax
movq %rax, 0x40(%rsp)
movq 0x10(%r14), %rax
movl %r10d, %ecx
movl (%rax,%rcx,4), %eax
movq 0x10(%r9), %rcx
leaq (%rax,%rax,4), %rbx
movl 0x18(%rcx,%rbx,8), %ebp
testq %rbp, %rbp
je 0x17ab80
xorl %r13d, %r13d
movl %r10d, 0xc(%rsp)
movq 0x10(%r9), %rax
movq 0x20(%rax,%rbx,8), %rcx
movl (%rcx,%r13,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl $0x2, %ecx
jb 0x17ab74
testl %r12d, %r12d
je 0x17aaa5
cmpl 0x28(%r9), %ecx
jb 0x17aaa5
cmpl 0x2c(%r9), %ecx
jb 0x17ab74
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %eax
andl $0x3, %eax
cmpl %r15d, %eax
jne 0x17ab74
movq 0x8(%r14), %rax
cmpq %rax, (%r14)
jne 0x17aaef
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x2, %rsi
callq 0x2370
movl 0xc(%rsp), %r10d
movq (%rsp), %r9
movq %rax, 0x10(%r14)
movq 0x10(%r9), %rax
movq 0x20(%rax,%rbx,8), %rax
movl (%rax,%r13,8), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movq (%r14), %rcx
movq 0x10(%r14), %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movl %eax, (%rdi,%rcx,4)
movq 0x8(%r14), %rax
cmpq %rax, %rdx
jne 0x17ab47
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
callq 0x2370
movl 0xc(%rsp), %r10d
movq (%rsp), %r9
movq %rax, 0x10(%r14)
movq (%r14), %rax
movq 0x10(%r14), %rdx
leaq 0x1(%rax), %rcx
movq %rcx, (%r14)
movl %r10d, (%rdx,%rax,4)
movl $0x1, %esi
movq 0x40(%rsp), %rax
cmpq 0x30(%rsp), %rax
je 0x17ab91
cmpq 0x88(%rsp), %rcx
ja 0x17ab91
incq %r13
cmpq %r13, %rbp
jne 0x17aa74
addl $0x2, %r10d
cmpl 0x28(%rsp), %r10d
jb 0x17aa4d
xorl %esi, %esi
movq (%r14), %rcx
movq 0x28(%rsp), %rax
movl %eax, %edx
cmpq %rdx, %rcx
jbe 0x17abbe
movl %ecx, %r8d
subl %eax, %r8d
movq 0x10(%rsp), %rax
incl %eax
cmpq 0x30(%rsp), %rax
jae 0x17abc8
testl %esi, %esi
je 0x17aa07
jmp 0x17abc8
movq 0x10(%rsp), %rax
movl 0x1c(%rsp), %r8d
testl %eax, %eax
sete %dl
testl %r8d, %r8d
sete %cl
orb %dl, %cl
jne 0x17ad15
movq (%r14), %rdx
movq 0x10(%r14), %rdi
movl %r8d, %ecx
movq %rdx, %rsi
subq %rcx, %rsi
movl (%rdi,%rsi,4), %ecx
movq 0x80(%rsp), %rsi
movl %ecx, (%rsi)
subl %r8d, %edx
movq 0x10(%r14), %rsi
leal 0x1(%rdx), %r13d
movl (%rsi,%r13,4), %edi
cmpl $-0x1, %edi
je 0x17ad17
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl 0x18(%rsp), %r15d
movl %edx, %edx
movl (%rsi,%rdx,4), %r8d
movl %edi, %edx
movl (%rsi,%rdx,4), %edi
cmpq 0x20(%rsp), %rdi
movl $0x1, %esi
cmovel %r15d, %esi
movq 0x10(%r9), %rdx
leaq (%rdi,%rdi,4), %r9
movl 0x18(%rdx,%r9,8), %r10d
testq %r10, %r10
setne %bl
je 0x17ac8b
movq 0x20(%rdx,%r9,8), %r9
movl (%r9), %r11d
andl %ecx, %r11d
cmpl %r8d, %r11d
jne 0x17ac5f
movb $0x1, %bl
xorl %r11d, %r11d
jmp 0x17ac81
movl $0x1, %r11d
cmpq %r11, %r10
je 0x17ac88
movl (%r9,%r11,8), %ebx
andl %ecx, %ebx
incq %r11
cmpl %r8d, %ebx
jne 0x17ac65
decq %r11
cmpq %r10, %r11
setb %bl
addl %esi, 0x4(%r9,%r11,8)
jmp 0x17ac8b
seta %bl
testb %bl, %bl
je 0x17ad45
leaq (%r8,%r8,4), %r8
movq 0x8(%rdx,%r8,8), %rbx
movq 0x18(%rdx,%r8,8), %r9
cmpl %ebx, %r9d
setb %r11b
jae 0x17acec
movl %r9d, %r9d
movq 0x20(%rdx,%r8,8), %r10
movl (%r10,%r9,8), %ebp
andl %ecx, %ebp
movb $0x1, %r11b
cmpl %edi, %ebp
je 0x17ace1
movl %ebx, %r11d
incq %r9
cmpq %r9, %r11
je 0x17ace8
movl (%r10,%r9,8), %ebx
andl %ecx, %ebx
incq %r9
cmpl %edi, %ebx
jne 0x17acc5
decq %r9
cmpq %r11, %r9
setb %r11b
addl %esi, 0x4(%r10,%r9,8)
jmp 0x17acec
seta %r11b
testb %r11b, %r11b
je 0x17ad26
addl $0x8, (%rdx,%r8,8)
movq 0x10(%r14), %rsi
movl (%rsi,%r13,4), %edx
leal 0x1(%rdx), %r13d
movl (%rsi,%r13,4), %edi
cmpl $-0x1, %edi
movq (%rsp), %r9
jne 0x17ac1a
jmp 0x17ad17
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xaaa1d(%rip), %rdi     # 0x22574a
leaq 0xb70b7(%rip), %rsi     # 0x231deb
leaq 0xb710d(%rip), %rcx     # 0x231e48
movl $0x2f4, %edx            # imm = 0x2F4
callq 0x2110
leaq 0xaa9fe(%rip), %rdi     # 0x22574a
leaq 0xb7098(%rip), %rsi     # 0x231deb
leaq 0xb70ee(%rip), %rcx     # 0x231e48
movl $0x2f3, %edx            # imm = 0x2F3
callq 0x2110