// File: /workspace/github_cpp_makefile_star2_O1/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 %r9, (%rsp)
movq 0x58(%r9), %rbx
cmpq $0x0, 0x158(%rsp)
movq %rdi, 0x30(%rsp)
je 0x7f25e
movl 0x8(%rdi), %r15d
movl 0x14(%rdi), %r10d
movslq 0x14(%rbp), %r12
cmpl 0x10(%rbp), %r12d
jge 0x7f2db
xorl %r11d, %r11d
movl 0x4(%rdi), %esi
cmpq %rsi, %r15
jl 0x7f2c6
movl 0x10(%rdi), %ecx
cmpq %rcx, %r10
jl 0x7f2c6
movslq 0x28(%rbp), %r9
movq %r11, %rbx
movq (%rsp), %rdi
movq %r15, %rdx
movq %r10, %r8
jmp 0x7f29e
movl 0x4(%rdi), %r15d
movl 0x10(%rdi), %r14d
movslq 0x10(%rbp), %r12
cmpl 0x14(%rbp), %r12d
jle 0x7f52c
xorl %r11d, %r11d
movq 0x30(%rsp), %rax
movl 0x8(%rax), %edx
cmpq %rdx, %r15
jg 0x7f2c6
movl 0x14(%rax), %r8d
cmpq %r8, %r14
jg 0x7f2c6
movslq 0x24(%rbp), %r9
movq %r11, %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 0x7f054
movq %rbx, %r11
addq $0x20, %rsp
addq %rax, %r11
movq %r11, %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 %r11d, %r11d
movq %rbp, 0x38(%rsp)
movslq 0x4(%rbx,%r13), %rsi
incq %rsi
cmpq %rsi, %r15
jl 0x7f352
movslq 0xc(%rbx,%r13), %rcx
incq %rcx
cmpq %rcx, %r10
jl 0x7f352
movq %r15, %rdx
movq %r11, %r15
movslq 0x14(%rbp), %rax
movq $-0x1, %r9
cmpq %rax, %r12
jne 0x7f31e
movslq 0x24(%rbp), %r9
movq (%rsp), %rdi
movq %r10, %r8
movsd 0x10(%rsp), %xmm0
pushq 0x150(%rsp)
pushq 0x20(%rsp)
pushq 0x30(%rsp)
pushq 0x40(%rsp)
callq 0x7f054
addq $0x20, %rsp
movq %r15, %r11
addq %rax, %r11
movq 0x30(%rsp), %rdi
movslq (%rbx,%r13), %r15
movslq 0x4(%rbx,%r13), %rax
incq %rax
movslq 0x8(%rbx,%r13), %r10
movslq 0xc(%rbx,%r13), %rcx
incq %rcx
movl 0x1c(%rbx,%r13), %edx
cmpw $0x7fff, 0x14(%rbx,%r13) # imm = 0x7FFF
jne 0x7f38b
testl %edx, %edx
jne 0x7f38b
cmpw $0x0, 0x12(%rbx,%r13)
js 0x7f4bb
movq %r11, 0x8(%rsp)
movq (%rsp), %r8
movq 0x58(%r8), %rsi
movl 0x18(%rsi,%r13), %r9d
addq %r9, %r9
addq 0x70(%r8), %r9
movq %r9, 0xf8(%rsp)
movl 0x1c(%rsi,%r13), %r8d
movq %r8, 0xf0(%rsp)
movq %r8, 0xe8(%rsp)
movl (%rsi,%r13), %r8d
movl %r8d, 0x5c(%rsp)
movl 0x4(%rsi,%r13), %r8d
movl %r8d, 0x60(%rsp)
movl 0x8(%rsi,%r13), %r8d
movl %r8d, 0x50(%rsp)
movl 0xc(%rsi,%r13), %r8d
movl %r8d, 0x54(%rsp)
movswl 0x14(%rsi,%r13), %esi
movl %esi, 0x6c(%rsp)
movl %esi, 0x68(%rsp)
movslq 0x10(%rbp), %rsi
cmpq %rsi, %r12
jne 0x7f429
movl (%rbp), %r11d
movl 0x8(%rbp), %r8d
movslq 0x18(%rbp), %rsi
movl 0x4(%rdi), %r9d
movl 0x10(%rdi), %r10d
cmpl %r11d, %r9d
cmovgl %r9d, %r11d
movslq %r11d, %r15
cmpl %r8d, %r10d
cmovgl %r10d, %r8d
movslq %r8d, %r10
jmp 0x7f42b
xorl %esi, %esi
movslq 0x14(%rbp), %r8
cmpq %r8, %r12
jne 0x7f45e
movslq 0x4(%rbp), %r9
movslq 0xc(%rbp), %r8
movslq 0x1c(%rbp), %rdx
movl 0x8(%rdi), %eax
movq %rdi, %r14
movl 0x14(%rdi), %ecx
cmpl %r9d, %eax
cmovgel %r9d, %eax
cltq
cmpl %r8d, %ecx
cmovgel %r8d, %ecx
movslq %ecx, %rcx
jmp 0x7f467
movq %rdi, %r14
movq %rax, %r9
movq %rcx, %r8
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
pushq %rax
pushq %r15
pushq %rcx
pushq %r10
pushq %r9
movq %r15, %rbp
movq %r10, %r15
callq 0x7ecf9
movq %r15, %r10
movq %rbp, %r15
addq $0x30, %rsp
movq 0x8(%rsp), %r11
addq %rax, %r11
movq %r14, %rdi
movq 0x38(%rsp), %rbp
leaq -0x1(%r12), %rax
movslq 0x10(%rbp), %rcx
addq $-0x20, %r13
cmpq %rcx, %r12
movq %rax, %r12
jg 0x7f2ea
jmp 0x7f237
subq %r15, %rax
subq %r10, %rcx
cmpq $0x200, %rax            # imm = 0x200
jg 0x7f4da
cmpq %rcx, %rax
cmovgq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
jmp 0x7f516
cmpq %rcx, %rax
cmovlq %rax, %rcx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %rax, %xmm2
mulsd 0x1a604f(%rip), %xmm2   # 0x225548
addsd 0x1a5d6f(%rip), %xmm2   # 0x225270
movapd %xmm2, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r11, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r11
jmp 0x7f49d
movq %r12, %r13
shlq $0x5, %r13
xorl %r11d, %r11d
movslq (%rbx,%r13), %rdx
cmpq %rdx, %r15
jg 0x7f596
movslq 0x8(%rbx,%r13), %r8
cmpq %r8, %r14
jg 0x7f596
movq %r11, 0x8(%rsp)
movslq 0x10(%rbp), %rax
movq $-0x1, %r9
cmpq %rax, %r12
jne 0x7f562
movslq 0x28(%rbp), %r9
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 0x7f054
addq $0x20, %rsp
movq 0x8(%rsp), %r11
addq %rax, %r11
movslq (%rbx,%r13), %rax
movslq 0x4(%rbx,%r13), %r15
incq %r15
movslq 0x8(%rbx,%r13), %rcx
movslq 0xc(%rbx,%r13), %r14
incq %r14
movl 0x1c(%rbx,%r13), %edx
cmpw $0x7fff, 0x14(%rbx,%r13) # imm = 0x7FFF
jne 0x7f5cf
testl %edx, %edx
jne 0x7f5cf
cmpw $0x0, 0x12(%rbx,%r13)
js 0x7f6e9
movq %r11, 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)
movl (%rsi,%r13), %edi
movl %edi, 0x5c(%rsp)
movl 0x4(%rsi,%r13), %edi
movl %edi, 0x60(%rsp)
movl 0x8(%rsi,%r13), %edi
movl %edi, 0x50(%rsp)
movl 0xc(%rsi,%r13), %edi
movl %edi, 0x54(%rsp)
movswl 0x14(%rsi,%r13), %esi
movl %esi, 0x6c(%rsp)
movl %esi, 0x68(%rsp)
movslq 0x10(%rbp), %rsi
cmpq %rsi, %r12
jne 0x7f669
movl (%rbp), %eax
movl 0x8(%rbp), %ecx
movslq 0x18(%rbp), %rsi
movq 0x30(%rsp), %r8
movl 0x4(%r8), %edi
movl 0x10(%r8), %r8d
cmpl %eax, %edi
cmovgl %edi, %eax
cltq
cmpl %ecx, %r8d
cmovgl %r8d, %ecx
movslq %ecx, %rcx
jmp 0x7f66b
xorl %esi, %esi
movslq 0x14(%rbp), %rdi
cmpq %rdi, %r12
jne 0x7f6a3
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 0x7f6a9
movq %r15, %r9
movq %r14, %r8
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
pushq %r15
pushq %rax
pushq %r14
pushq %rcx
pushq %r9
callq 0x7ecf9
addq $0x30, %rsp
movq 0x8(%rsp), %r11
addq %rax, %r11
leaq 0x1(%r12), %rax
movslq 0x14(%rbp), %rcx
addq $0x20, %r13
cmpq %rcx, %r12
movq %rax, %r12
jl 0x7f536
jmp 0x7f277
movq %r15, %rdx
subq %rax, %rdx
movq %r14, %rax
subq %rcx, %rax
cmpq $0x200, %rdx            # imm = 0x200
jg 0x7f70f
cmpq %rax, %rdx
cmovgq %rdx, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
jmp 0x7f74b
cmpq %rax, %rdx
cmovlq %rdx, %rax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %rdx, %xmm2
mulsd 0x1a5e1a(%rip), %xmm2   # 0x225548
addsd 0x1a5b3a(%rip), %xmm2   # 0x225270
movapd %xmm2, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r11, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r11
jmp 0x7f6cb