// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movl 0x20(%rbp), %eax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movq -0x18(%rbp), %rax
movl %eax, %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x28(%rbp)
ja 0x24cebd
movl $0x0, -0x4(%rbp)
jmp 0x24d4d8
movl $0x0, -0x64(%rbp)
movq -0x30(%rbp), %rax
movq $0x0, (%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x24cf37
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x24cefc
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x70(%rbp)
jmp 0x24cf07
movl $0x2, %eax
movq %rax, -0x70(%rbp)
jmp 0x24cf07
movq -0x70(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0x24cf5b
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x24cfc3
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x24cf88
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x78(%rbp)
jmp 0x24cf93
movl $0x2, %eax
movq %rax, -0x78(%rbp)
jmp 0x24cf93
movq -0x78(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rdx
movq (%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, (%rdx)
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl $0x2, -0x50(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, %ecx
xorl %eax, %eax
cmpq -0x28(%rbp), %rcx
movb %al, -0x79(%rbp)
jae 0x24d00d
cmpl $0x0, -0x64(%rbp)
setne %al
xorb $-0x1, %al
movb %al, -0x79(%rbp)
movb -0x79(%rbp), %al
testb $0x1, %al
jne 0x24d019
jmp 0x24d34b
movq -0x20(%rbp), %rax
movl -0x3c(%rbp), %ecx
movsbl (%rax,%rcx), %eax
movl %eax, %eax
movl %eax, %ecx
leaq 0x177621(%rip), %rax    # 0x3c4650
movzbl (%rax,%rcx), %eax
movl %eax, -0x4c(%rbp)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
subl -0x50(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0x24d31a
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x54(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x48(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x48(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movl %eax, -0x44(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0x24d302
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x48(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x0, %eax
je 0x24d0e4
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x48(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x1, %eax
jne 0x24d0e9
jmp 0x24d2f4
cmpl $0x0, 0x20(%rbp)
je 0x24d148
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x48(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x10(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jb 0x24d148
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x48(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq -0x10(%rbp), %rcx
cmpl 0x2c(%rcx), %eax
jae 0x24d148
jmp 0x24d2f4
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x48(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movq 0x20(%rcx), %rcx
movl -0x38(%rbp), %edx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x3, %eax
cmpl -0x4c(%rbp), %eax
jne 0x24d2f2
jmp 0x24d18b
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x24d1fc
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x24d1bb
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x88(%rbp)
jmp 0x24d1c9
movl $0x2, %eax
movq %rax, -0x88(%rbp)
jmp 0x24d1c9
movq -0x88(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x48(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movl %edx, (%rax,%rcx,4)
jmp 0x24d23c
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x24d2ad
movq -0x30(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x24d26c
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x90(%rbp)
jmp 0x24d27a
movl $0x2, %eax
movq %rax, -0x90(%rbp)
jmp 0x24d27a
movq -0x90(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x54(%rbp), %edx
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movl %edx, (%rax,%rcx,4)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
cmpq -0x28(%rbp), %rax
je 0x24d2e7
movq -0x30(%rbp), %rax
movq (%rax), %rax
cmpq 0x18(%rbp), %rax
jbe 0x24d2f0
movl $0x1, -0x64(%rbp)
jmp 0x24d302
jmp 0x24d2f2
jmp 0x24d2f4
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x24d08c
cmpl $0x0, -0x64(%rbp)
je 0x24d30a
jmp 0x24d31a
jmp 0x24d30c
movl -0x54(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x54(%rbp)
jmp 0x24d049
movq -0x30(%rbp), %rax
movq (%rax), %rax
movl -0x58(%rbp), %ecx
cmpq %rcx, %rax
ja 0x24d32b
jmp 0x24d34b
movq -0x30(%rbp), %rax
movq (%rax), %rax
movl -0x58(%rbp), %ecx
subq %rcx, %rax
movl %eax, -0x50(%rbp)
jmp 0x24d33d
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x24cff1
cmpl $0x0, -0x3c(%rbp)
jbe 0x24d4cb
cmpl $0x0, -0x50(%rbp)
jbe 0x24d4cb
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movq (%rcx), %rcx
movl -0x50(%rbp), %edx
subq %rdx, %rcx
movl (%rax,%rcx,4), %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
movq (%rax), %rax
movl -0x50(%rbp), %ecx
subq %rcx, %rax
movl %eax, -0x54(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x54(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
cmpl $-0x1, (%rax,%rcx,4)
je 0x24d4c9
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x54(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x60(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x54(%rbp), %edx
addl $0x1, %edx
movl %edx, %edx
movl (%rcx,%rdx,4), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpq -0x18(%rbp), %rax
je 0x24d3ef
movl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x24d3f8
movl -0x34(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x5c(%rbp), %esi
movl -0x60(%rbp), %edx
movl -0x4c(%rbp), %r8d
movl $0x1, %ecx
callq 0x26cd20
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x24d424
jmp 0x24d443
leaq 0x134ff7(%rip), %rdi    # 0x382422
leaq 0x1417d9(%rip), %rsi    # 0x38ec0b
movl $0x2f3, %edx            # imm = 0x2F3
leaq 0x14182a(%rip), %rcx    # 0x38ec68
callq 0x2130
movq -0x10(%rbp), %rdi
movl -0x60(%rbp), %esi
movl -0x5c(%rbp), %edx
movl -0x4c(%rbp), %r8d
xorl %ecx, %ecx
callq 0x26cd20
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x24d463
jmp 0x24d482
leaq 0x134fb8(%rip), %rdi    # 0x382422
leaq 0x14179a(%rip), %rsi    # 0x38ec0b
movl $0x2f4, %edx            # imm = 0x2F4
leaq 0x1417eb(%rip), %rcx    # 0x38ec68
callq 0x2130
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x60(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %edx
shrl $0x3, %edx
addl $0x1, %edx
movl (%rax), %ecx
andl $0x1fffffff, %edx       # imm = 0x1FFFFFFF
shll $0x3, %edx
andl $0x7, %ecx
orl %edx, %ecx
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x54(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x54(%rbp)
jmp 0x24d38d
jmp 0x24d4d2
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)