// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: debug_backtrace_step_err(unsigned long, ul_ov_t*, rtrace_t*, ul_idx_t const*, char*, UC_Read*, overlap_region*, bit_extz_t*, double)
void debug_backtrace_step_err(uint64_t rid, ul_ov_t *aln, rtrace_t *tc, const ul_idx_t *uref, char* qstr, UC_Read *tu, overlap_region *o, bit_extz_t *exz, double e_rate)
{
    // if(aln->qs == 0 && aln->qe == 3013 && aln->ts == 28965 && aln->te == 31993) {
        // fprintf(stderr, "\n[M::%s::] q::[%u, %u), t::[%u, %u), tot_e::%u, cid::[%d, %d], cq::[%u, %u), ct::[%u, %u), pfx_e::%d, sfx_e::%d, mid_e::%d\n", 
        //                 __func__, aln->qs, aln->qe, aln->ts, aln->te, aln->sec, tc->c_wsid, tc->c_weid,
        //                 tc->c_qs, tc->c_qe, tc->c_ts, tc->c_te, tc->pfx_e, tc->sfx_e, tc->mid_e);
        int64_t k, err, qs = aln->qs, qe = aln->qe, err0; rtrace_iter it;
        k = qe; it.k = INT32_MAX;
        err0 = get_rid_backward_cigar_err(&it, aln, NULL, tc, uref, qstr, tu, NULL, o, exz, e_rate, k); 
        err = get_rid_backward_cigar_err(&it, aln, NULL, tc, uref, qstr, tu, NULL, o, exz, e_rate, k); 
        assert(err == err0); 
        for (k = qe, it.k = INT32_MAX, err0 = 0; k >= qs; k-=8) {
            err = get_rid_backward_cigar_err(&it, aln, NULL, tc, uref, qstr, tu, NULL, o, exz, e_rate, k);  
            // if(!(err >= 0 && err >= err0)) {
            //     fprintf(stderr, "[M::%s::rid->%lu] q::[%u, %u), t::[%u, %u), tot_e::%u, cid::[%d, %d], cq::[%u, %u), ct::[%u, %u)\n", 
            //             __func__, rid, aln->qs, aln->qe, aln->ts, aln->te, aln->sec, tc->c_wsid, tc->c_weid,
            //             tc->c_qs, tc->c_qe, tc->c_ts, tc->c_te);
            //     fprintf(stderr, "[M::%s::] q::[%ld, %ld), err::%ld, err0::%ld\n", __func__, k, qe, err, err0);   
            // }
            // fprintf(stderr, "[M::%s::] q::[%ld, %ld), err::%ld\n", __func__, k, qe, err); 
            assert(err >= 0 && err >= err0); 
            err0 = err;  
        }
        k = qs;
        err = get_rid_backward_cigar_err(&it, aln, NULL, tc, uref, qstr, tu, NULL, o, exz, e_rate, k); 
        // if(!(err >= 0 && err >= err0)) {
        //     fprintf(stderr, "[M::%s::rid->%lu] q::[%u, %u), t::[%u, %u), tot_e::%u, cid::[%d, %d], cq::[%u, %u), ct::[%u, %u)\n", 
        //             __func__, rid, aln->qs, aln->qe, aln->ts, aln->te, aln->sec, tc->c_wsid, tc->c_weid,
        //             tc->c_qs, tc->c_qe, tc->c_ts, tc->c_te);
        //     fprintf(stderr, "[M::%s::] q::[%ld, %ld), err::%ld, err0::%ld\n", __func__, k, qe, err, err0);   
        // }
        // fprintf(stderr, "[M::%s::] q::[%ld, %ld), err::%ld, exz->err::%d, exz->cigar.n::%d\n", 
        // __func__, qs, qe, err, exz->err, (int32_t)exz->cigar.n);    
        assert(err >= 0 && err >= err0); 
        if(!(err == (int64_t)aln->sec)) {
            fprintf(stderr, "[M::%s::rid->%lu] q::[%u, %u), t::[%u, %u), tot_e::%u, cid::[%d, %d], cq::[%u, %u), ct::[%u, %u)\n", 
                        __func__, rid, aln->qs, aln->qe, aln->ts, aln->te, aln->sec, tc->c_wsid, tc->c_weid,
                        tc->c_qs, tc->c_qe, tc->c_ts, tc->c_te);
            fprintf(stderr, "[M::%s::] aln->sec::%u, err::%ld\n", __func__, aln->sec, err);  
        }
        assert(err == (int64_t)aln->sec);  
    // }   
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movsd %xmm0, 0x10(%rsp)
movq %r9, %r10
movq %r9, 0x8(%rsp)
movq %r8, %r13
movq %rcx, %rbp
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, 0x28(%rsp)
movl 0x4(%rsi), %eax
movq %rax, 0x30(%rsp)
movl 0x8(%rsi), %ebx
leaq 0x40(%rsp), %r15
movq $0x7fffffff, (%r15)     # imm = 0x7FFFFFFF
subq $0x8, %rsp
xorl %eax, %eax
movq %r15, %rdi
xorl %edx, %edx
movq %r14, %rcx
movq %rbp, %r8
movq %r13, %r9
pushq %rbx
pushq 0x128(%rsp)
pushq 0x128(%rsp)
pushq %rax
pushq %r10
callq 0x7fea8
addq $0x30, %rsp
movq %rax, 0x38(%rsp)
subq $0x8, %rsp
movq %r15, %rdi
movq %r12, %r15
movq %r12, %rsi
xorl %edx, %edx
movq %r14, 0x28(%rsp)
movq %r14, %rcx
movq %rbp, 0x20(%rsp)
movq %rbp, %r8
movq %r13, %rbp
movq %r13, %r9
movsd 0x18(%rsp), %xmm0
pushq %rbx
pushq 0x128(%rsp)
pushq 0x128(%rsp)
xorl %eax, %eax
pushq %rax
pushq 0x30(%rsp)
callq 0x7fea8
addq $0x30, %rsp
cmpq 0x38(%rsp), %rax
jne 0x80a4b
movq $0x7fffffff, 0x40(%rsp) # imm = 0x7FFFFFFF
movq 0x30(%rsp), %r13
cmpl %r13d, %ebx
jae 0x80891
xorl %r14d, %r14d
jmp 0x808f4
xorl %r12d, %r12d
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %r8
movq %rbp, %r9
movsd 0x18(%rsp), %xmm0
pushq %rbx
pushq 0x128(%rsp)
pushq 0x128(%rsp)
pushq $0x0
pushq 0x30(%rsp)
callq 0x7fea8
addq $0x30, %rsp
testq %rax, %rax
js 0x80a2c
movq %rax, %r14
cmpq %r12, %rax
jl 0x80a2c
addq $-0x8, %rbx
movq %r14, %r12
cmpq %r13, %rbx
jge 0x80894
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %r8
movq %rbp, %r9
movsd 0x18(%rsp), %xmm0
pushq %r13
pushq 0x128(%rsp)
pushq 0x128(%rsp)
pushq $0x0
pushq 0x30(%rsp)
callq 0x7fea8
addq $0x30, %rsp
testq %rax, %rax
js 0x80a6a
movq %rax, %r12
cmpq %r14, %rax
jl 0x80a6a
movl 0x18(%r15), %r10d
andl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
cmpq %r10, %r12
jne 0x80974
cmpq %r10, %r12
jne 0x80a89
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x214665(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
movl 0x4(%r15), %r8d
movl 0x8(%r15), %r9d
movl 0x10(%r15), %eax
movq %rax, 0x10(%rsp)
movl 0x14(%r15), %eax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rax
movl 0x10(%rax), %ecx
movq %rcx, 0x18(%rsp)
movl 0x14(%rax), %ebp
movq %r15, %r13
movl (%rax), %ebx
movl 0x4(%rax), %r14d
movl 0x8(%rax), %r11d
movl 0xc(%rax), %r15d
subq $0x8, %rsp
leaq 0x1b4a2c(%rip), %rsi    # 0x2353f0
leaq 0x1b4a87(%rip), %rdx    # 0x235452
movq 0x30(%rsp), %rcx
xorl %eax, %eax
pushq %r15
pushq %r11
pushq %r14
pushq %rbx
pushq %rbp
pushq 0x48(%rsp)
pushq %r10
pushq 0x48(%rsp)
pushq 0x58(%rsp)
callq 0x22b0
addq $0x50, %rsp
movq 0x2145e8(%rip), %rax    # 0x294fe0
movq (%rax), %rdi
movl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
andl 0x18(%r13), %ecx
leaq 0x1b4a60(%rip), %rsi    # 0x23546b
leaq 0x1b4a40(%rip), %rdx    # 0x235452
movq %r12, %r8
xorl %eax, %eax
callq 0x22b0
movl 0x18(%r13), %r10d
andl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
jmp 0x80959
leaq 0x1b49a5(%rip), %rdi    # 0x2353d8
leaq 0x1b0d19(%rip), %rsi    # 0x231753
leaq 0x1b490b(%rip), %rcx    # 0x23534c
movl $0x5f43, %edx           # imm = 0x5F43
callq 0x2110
leaq 0x1b48ee(%rip), %rdi    # 0x235340
leaq 0x1b0cfa(%rip), %rsi    # 0x231753
leaq 0x1b48ec(%rip), %rcx    # 0x23534c
movl $0x5f39, %edx           # imm = 0x5F39
callq 0x2110
leaq 0x1b4967(%rip), %rdi    # 0x2353d8
leaq 0x1b0cdb(%rip), %rsi    # 0x231753
leaq 0x1b48cd(%rip), %rcx    # 0x23534c
movl $0x5f50, %edx           # imm = 0x5F50
callq 0x2110
leaq 0x1b49fd(%rip), %rdi    # 0x23548d
leaq 0x1b0cbc(%rip), %rsi    # 0x231753
leaq 0x1b48ae(%rip), %rcx    # 0x23534c
movl $0x5f57, %edx           # imm = 0x5F57
callq 0x2110