// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: get_sub_cigar_err(ul_ov_t*, rtrace_t*, ul_idx_t const*, char*, UC_Read*, overlap_region*, bit_extz_t*, double, long)
int64_t get_sub_cigar_err(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, int64_t is_rev)
{
    int64_t k, q[2], t[2], cq[2], ct[2], ci[2], err, qwl, twl; 
    window_list *wa = o->w_list.a; bit_extz_t aux;
    if(!is_rev) {
        err = 0; q[0] = aln->qs; t[0] = aln->ts;
        for (k = tc->c_wsid; k <= tc->c_weid; k++) {
            q[1] = wa[k].x_start; t[1] = wa[k].y_start;
            if(q[0] <= q[1] && t[0] <= t[1]) {
                err += debug_aln_err(o, q[0], q[1], t[0], t[1], e_rate, k==tc->c_wsid?tc->pfx_e:-1, uref, qstr, tu, exz);
                // fprintf(stderr, "-0-[M::%s::k->%ld] q::[%ld, %ld), t::[%ld, %ld), err::%ld\n", 
                //         __func__, k, q[0], q[1], t[0], t[1], err);
            }

            cq[0] = q[0] = wa[k].x_start; cq[1] = q[1] = wa[k].x_end+1; 
            ct[0] = t[0] = wa[k].y_start; ct[1] = t[1] = wa[k].y_end+1; 
            ci[0] = 0; ci[1] = wa[k].clen; qwl = q[1] - q[0]; twl = t[1] - t[0];
            if(is_ualn_win(wa[k])) {
                err += gen_err_unaligned(qwl, twl); 
                // fprintf(stderr, "-1-[M::%s::k->%ld] q::[%ld, %ld), t::[%ld, %ld), err::%ld\n", 
                //         __func__, k, q[0], q[1], t[0], t[1], err);
            } else {
                set_bit_extz_t(aux, (*o), k); 
                if(k == tc->c_wsid) {
                    cq[0] = tc->c_qs; ct[0] = tc->c_ts; ci[0] = tc->c_wsii;
                    q[0] = MAX((int32_t)aln->qs, tc->c_qs);
                    t[0] = MAX((int32_t)aln->ts, tc->c_ts);
                }
                if(k == tc->c_weid) {
                    cq[1] = tc->c_qe; ct[1] = tc->c_te; ci[1] = tc->c_weii;
                    q[1] = MIN((int32_t)aln->qe, tc->c_qe);
                    t[1] = MIN((int32_t)aln->te, tc->c_te);
                }
                // err += scan_single_wcigar(&aux, ci[0], ci[1], ct[0], ct[1], cq[0], cq[1], 
                //     t[0], t[1], q[0], q[1]);
                err += scan_single_wcigar_toff_backward(&aux, ci[0], ci[1], ct[0], ct[1], cq[0], cq[1], 
                    t[0], t[1], q[0], q[1]);
                // fprintf(stderr, "-2-[M::%s::k->%ld] q::[%ld, %ld), t::[%ld, %ld), err::%ld, cq::[%ld, %ld), ct::[%ld, %ld)\n", 
                //         __func__, k, q[0], q[1], t[0], t[1], err, cq[0], cq[1], ct[0], ct[1]);
            }
            q[0] = q[1]; t[0] = t[1];
        }

        q[1] = aln->qe; t[1] = aln->te;
        if(q[0] <= q[1] && t[0] <= t[1]) {
            err += debug_aln_err(o, q[0], q[1], t[0], t[1], e_rate, tc->sfx_e, uref, qstr, tu, exz);
            // fprintf(stderr, "-3-[M::%s::k->%ld] q::[%ld, %ld), t::[%ld, %ld), err::%ld\n", 
            //             __func__, k, q[0], q[1], t[0], t[1], err);
        }
    } else {
        err = 0; q[1] = aln->qe; t[1] = aln->te;
        for (k = tc->c_weid; k >= tc->c_wsid; k--) {
            q[0] = wa[k].x_end+1; t[0] = wa[k].y_end+1;
            if(q[0] <= q[1] && t[0] <= t[1]) {
                err += debug_aln_err(o, q[0], q[1], t[0], t[1], e_rate, k==tc->c_weid?tc->sfx_e:-1, uref, qstr, tu, exz);
            }

            cq[0] = q[0] = wa[k].x_start; cq[1] = q[1] = wa[k].x_end+1; 
            ct[0] = t[0] = wa[k].y_start; ct[1] = t[1] = wa[k].y_end+1; 
            ci[0] = 0; ci[1] = wa[k].clen; qwl = q[1] - q[0]; twl = t[1] - t[0];
            if(is_ualn_win(wa[k])) {
                err += gen_err_unaligned(qwl, twl); 
            } else {
                set_bit_extz_t(aux, (*o), k); 
                if(k == tc->c_wsid) {
                    cq[0] = tc->c_qs; ct[0] = tc->c_ts; ci[0] = tc->c_wsii;
                    q[0] = MAX((int32_t)aln->qs, tc->c_qs);
                    t[0] = MAX((int32_t)aln->ts, tc->c_ts);
                }
                if(k == tc->c_weid) {
                    cq[1] = tc->c_qe; ct[1] = tc->c_te; ci[1] = tc->c_weii;
                    q[1] = MIN((int32_t)aln->qe, tc->c_qe);
                    t[1] = MIN((int32_t)aln->te, tc->c_te);
                }
                // err += scan_single_wcigar(&aux, ci[0], ci[1], ct[0], ct[1], cq[0], cq[1], 
                //     t[0], t[1], q[0], q[1]);
                err += scan_single_wcigar_toff_backward(&aux, ci[0], ci[1], ct[0], ct[1], cq[0], cq[1], 
                    t[0], t[1], q[0], q[1]);
            }
            q[1] = q[0]; t[1] = t[0];
        }

        q[0] = aln->qs; t[0] = aln->ts;
        if(q[0] <= q[1] && t[0] <= t[1]) {
            err += debug_aln_err(o, q[0], q[1], t[0], t[1], e_rate, tc->pfx_e, uref, qstr, tu, exz);
        }
    }
    // fprintf(stderr, "[M::%s::] err::%ld, aln_err::%u\n", __func__, err, aln->sec);
    return err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movsd %xmm0, 0x10(%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, %rbp
movq %rdi, %r14
movq %r9, (%rsp)
movq 0x58(%r9), %rbx
cmpq $0x0, 0x158(%rsp)
movq %rdi, 0x30(%rsp)
je 0x7ebeb
movl 0x8(%r14), %r11d
movl 0x14(%r14), %r10d
movslq 0x14(%rbp), %r12
cmpl 0x10(%rbp), %r12d
jge 0x7ec67
xorl %edi, %edi
movl 0x4(%r14), %esi
cmpq %rsi, %r11
jl 0x7ec52
movl 0x10(%r14), %ecx
cmpq %rcx, %r10
jl 0x7ec52
movslq 0x28(%rbp), %r9
movq %rdi, %rbx
movq (%rsp), %rdi
movq %r11, %rdx
movq %r10, %r8
jmp 0x7ec2a
movl 0x4(%r14), %r15d
movl 0x10(%r14), %r14d
movslq 0x10(%rbp), %r12
cmpl 0x14(%rbp), %r12d
jle 0x7eea7
xorl %edi, %edi
movq 0x30(%rsp), %rax
movl 0x8(%rax), %edx
cmpq %rdx, %r15
jg 0x7ec52
movl 0x14(%rax), %r8d
cmpq %r8, %r14
jg 0x7ec52
movslq 0x24(%rbp), %r9
movq %rdi, %rbx
movq (%rsp), %rdi
movq %r15, %rsi
movq %r14, %rcx
movsd 0x10(%rsp), %xmm0
pushq 0x150(%rsp)
pushq 0x20(%rsp)
pushq 0x30(%rsp)
pushq 0x40(%rsp)
callq 0x7e9dd
movq %rbx, %rdi
addq $0x20, %rsp
addq %rax, %rdi
movq %rdi, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %r13
shlq $0x5, %r13
xorl %edi, %edi
movq %rbp, 0x38(%rsp)
movslq 0x4(%rbx,%r13), %rax
incq %rax
movslq 0xc(%rbx,%r13), %rcx
incq %rcx
cmpq %rax, %r11
jl 0x7ecf0
cmpq %rcx, %r10
jl 0x7ecf0
movq %rdi, %r14
movslq 0x14(%rbp), %rdx
movq $-0x1, %r9
cmpq %rdx, %r12
jne 0x7eca6
movslq 0x24(%rbp), %r9
movq (%rsp), %rdi
movq %rax, %rsi
movq %r11, %rdx
movq %r10, %r8
movsd 0x10(%rsp), %xmm0
pushq 0x150(%rsp)
pushq 0x20(%rsp)
pushq 0x30(%rsp)
pushq 0x40(%rsp)
callq 0x7e9dd
addq $0x20, %rsp
movq %r14, %rdi
addq %rax, %rdi
movslq 0x4(%rbx,%r13), %rax
movslq 0xc(%rbx,%r13), %rcx
incq %rax
incq %rcx
movq 0x30(%rsp), %r14
movslq (%rbx,%r13), %r11
movslq 0x8(%rbx,%r13), %r10
movl 0x1c(%rbx,%r13), %edx
cmpw $0x7fff, 0x14(%rbx,%r13) # imm = 0x7FFF
jne 0x7ed19
testl %edx, %edx
jne 0x7ed19
cmpw $0x0, 0x12(%rbx,%r13)
js 0x7ee36
movq %rdi, 0x8(%rsp)
movq (%rsp), %r8
movq 0x58(%r8), %rsi
movl 0x18(%rsi,%r13), %edi
addq %rdi, %rdi
addq 0x70(%r8), %rdi
movq %rdi, 0xf8(%rsp)
movl 0x1c(%rsi,%r13), %edi
movq %rdi, 0xf0(%rsp)
movq %rdi, 0xe8(%rsp)
movq (%rsi,%r13), %rdi
movq %rdi, 0x5c(%rsp)
movq 0x8(%rsi,%r13), %rdi
movq %rdi, 0x50(%rsp)
movswl 0x14(%rsi,%r13), %esi
movl %esi, 0x6c(%rsp)
movl %esi, 0x68(%rsp)
movslq 0x10(%rbp), %rsi
cmpq %rsi, %r12
jne 0x7eda2
movl (%rbp), %edi
movl 0x8(%rbp), %r8d
movslq 0x18(%rbp), %rsi
movl 0x4(%r14), %r9d
movl 0x10(%r14), %r10d
cmpl %edi, %r9d
cmovgl %r9d, %edi
movslq %edi, %r11
cmpl %r8d, %r10d
cmovgl %r10d, %r8d
movslq %r8d, %r10
jmp 0x7eda4
xorl %esi, %esi
movslq 0x14(%rbp), %rdi
cmpq %rdi, %r12
jne 0x7edd9
movslq 0x4(%rbp), %r9
movslq 0xc(%rbp), %r8
movslq 0x1c(%rbp), %rdx
movl 0x8(%r14), %eax
movq %r14, %r15
movl 0x14(%r14), %ecx
cmpl %r9d, %eax
cmovgel %r9d, %eax
cltq
cmpl %r8d, %ecx
cmovgel %r8d, %ecx
movslq %ecx, %rcx
jmp 0x7ede2
movq %r14, %r15
movq %rax, %r9
movq %rcx, %r8
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
pushq %rax
pushq %r11
pushq %rcx
pushq %r10
pushq %r9
movq %r10, %r14
movq %r11, %rbp
callq 0x7e70d
movq %rbp, %r11
movq %r14, %r10
addq $0x30, %rsp
movq 0x8(%rsp), %rdi
addq %rax, %rdi
movq %r15, %r14
movq 0x38(%rsp), %rbp
leaq -0x1(%r12), %rax
movslq 0x10(%rbp), %rcx
addq $-0x20, %r13
cmpq %rcx, %r12
movq %rax, %r12
jg 0x7ec75
jmp 0x7ebc2
subq %r11, %rax
subq %r10, %rcx
cmpq $0x200, %rax            # imm = 0x200
jg 0x7ee55
cmpq %rcx, %rax
cmovgq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
jmp 0x7ee91
cmpq %rcx, %rax
cmovlq %rax, %rcx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
mulsd 0x1b27d4(%rip), %xmm2   # 0x231648
addsd 0x1b2494(%rip), %xmm2   # 0x231310
movapd %xmm2, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rdi, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %rdi
jmp 0x7ee18
movq %r12, %r13
shlq $0x5, %r13
xorl %edi, %edi
movslq (%rbx,%r13), %rcx
movslq 0x8(%rbx,%r13), %rax
cmpq %rcx, %r15
jg 0x7ef1f
cmpq %rax, %r14
jg 0x7ef1f
movq %rdi, 0x8(%rsp)
movslq 0x10(%rbp), %rdx
movq $-0x1, %r9
cmpq %rdx, %r12
jne 0x7eedc
movslq 0x28(%rbp), %r9
movq (%rsp), %rdi
movq %r15, %rsi
movq %rcx, %rdx
movq %r14, %rcx
movq %rax, %r8
movsd 0x10(%rsp), %xmm0
pushq 0x150(%rsp)
pushq 0x20(%rsp)
pushq 0x30(%rsp)
pushq 0x40(%rsp)
callq 0x7e9dd
addq $0x20, %rsp
movq 0x8(%rsp), %rdi
addq %rax, %rdi
movslq (%rbx,%r13), %rcx
movslq 0x8(%rbx,%r13), %rax
movslq 0x4(%rbx,%r13), %r15
incq %r15
movslq 0xc(%rbx,%r13), %r14
incq %r14
movl 0x1c(%rbx,%r13), %edx
cmpw $0x7fff, 0x14(%rbx,%r13) # imm = 0x7FFF
jne 0x7ef4f
testl %edx, %edx
jne 0x7ef4f
cmpw $0x0, 0x12(%rbx,%r13)
js 0x7f05a
movq %rdi, 0x8(%rsp)
movq (%rsp), %r8
movq 0x58(%r8), %rsi
movl 0x18(%rsi,%r13), %edi
addq %rdi, %rdi
addq 0x70(%r8), %rdi
movq %rdi, 0xf8(%rsp)
movl 0x1c(%rsi,%r13), %edi
movq %rdi, 0xf0(%rsp)
movq %rdi, 0xe8(%rsp)
movq (%rsi,%r13), %rdi
movq %rdi, 0x5c(%rsp)
movq 0x8(%rsi,%r13), %rdi
movq %rdi, 0x50(%rsp)
movswl 0x14(%rsi,%r13), %esi
movl %esi, 0x6c(%rsp)
movl %esi, 0x68(%rsp)
movslq 0x10(%rbp), %rsi
cmpq %rsi, %r12
jne 0x7efda
movl (%rbp), %eax
movl 0x8(%rbp), %edi
movslq 0x18(%rbp), %rsi
movq 0x30(%rsp), %r8
movl 0x4(%r8), %ecx
movl 0x10(%r8), %r8d
cmpl %eax, %ecx
cmovgl %ecx, %eax
movslq %eax, %rcx
cmpl %edi, %r8d
cmovgl %r8d, %edi
movslq %edi, %rax
jmp 0x7efdc
xorl %esi, %esi
movslq 0x14(%rbp), %rdi
cmpq %rdi, %r12
jne 0x7f014
movslq 0x4(%rbp), %r9
movslq 0xc(%rbp), %r8
movslq 0x1c(%rbp), %rdx
movq 0x30(%rsp), %r10
movl 0x8(%r10), %edi
movl 0x14(%r10), %r10d
cmpl %r9d, %edi
cmovgel %r9d, %edi
movslq %edi, %r15
cmpl %r8d, %r10d
cmovgel %r8d, %r10d
movslq %r10d, %r14
jmp 0x7f01a
movq %r15, %r9
movq %r14, %r8
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
pushq %r15
pushq %rcx
pushq %r14
pushq %rax
pushq %r9
callq 0x7e70d
addq $0x30, %rsp
movq 0x8(%rsp), %rdi
addq %rax, %rdi
leaq 0x1(%r12), %rax
movslq 0x14(%rbp), %rcx
addq $0x20, %r13
cmpq %rcx, %r12
movq %rax, %r12
jl 0x7eeb0
jmp 0x7ec03
movq %r15, %rdx
subq %rcx, %rdx
movq %r14, %rcx
subq %rax, %rcx
cmpq $0x200, %rdx            # imm = 0x200
jg 0x7f080
cmpq %rcx, %rdx
cmovgq %rdx, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
jmp 0x7f0bc
cmpq %rcx, %rdx
cmovlq %rdx, %rcx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %rdx, %xmm2
mulsd 0x1b25a9(%rip), %xmm2   # 0x231648
addsd 0x1b2269(%rip), %xmm2   # 0x231310
movapd %xmm2, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rdi, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %rdi
jmp 0x7f03c