// File: /workspace/github_cpp_makefile_star2_O2/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, 0x18(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, %r13
movq %r9, 0x10(%rsp)
movq 0x58(%r9), %rbx
cmpq $0x0, 0x158(%rsp)
movq %rdi, 0x8(%rsp)
je 0x4bb1a
movl 0x8(%rdi), %r15d
movl 0x14(%rdi), %r10d
movslq 0x14(%r13), %rbp
movq %rbp, %r12
shlq $0x5, %r12
xorl %r11d, %r11d
movq %r13, 0x38(%rsp)
movslq 0x10(%r13), %rax
cmpq %rax, %rbp
jl 0x4bae9
movslq 0x4(%rbx,%r12), %rax
incq %rax
movslq 0xc(%rbx,%r12), %rcx
incq %rcx
cmpq %rax, %r15
jl 0x4b956
cmpq %rcx, %r10
jl 0x4b956
movq %r11, %r14
movslq 0x14(%r13), %rdx
pushq $-0x1
popq %r9
cmpq %rdx, %rbp
jne 0x4b910
movslq 0x24(%r13), %r9
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movq %r15, %rdx
movq %r10, %r8
movsd 0x18(%rsp), %xmm0
pushq 0x150(%rsp)
pushq 0x28(%rsp)
pushq 0x38(%rsp)
pushq 0x48(%rsp)
callq 0x4b6f5
addq $0x20, %rsp
movq %r14, %r11
addq %rax, %r11
movslq 0x4(%rbx,%r12), %rax
movslq 0xc(%rbx,%r12), %rcx
incq %rax
incq %rcx
movslq (%rbx,%r12), %r15
movslq 0x8(%rbx,%r12), %r10
movl 0x1c(%rbx,%r12), %edx
cmpw $0x7fff, 0x14(%rbx,%r12) # imm = 0x7FFF
jne 0x4b97f
testl %edx, %edx
jne 0x4b97f
cmpw $0x0, 0x12(%rbx,%r12)
js 0x4ba87
movq 0x10(%rsp), %r8
movq 0x58(%r8), %rsi
movl 0x18(%rsi,%r12), %edi
addq %rdi, %rdi
addq 0x70(%r8), %rdi
movq %rdi, 0xf8(%rsp)
movl 0x1c(%rsi,%r12), %edi
movq %rdi, 0xf0(%rsp)
movq %rdi, 0xe8(%rsp)
movq (%rsi,%r12), %rdi
movq %rdi, 0x5c(%rsp)
movq 0x8(%rsi,%r12), %rdi
movq %rdi, 0x50(%rsp)
movswl 0x14(%rsi,%r12), %esi
movl %esi, 0x6c(%rsp)
movl %esi, 0x68(%rsp)
movslq 0x10(%r13), %rsi
cmpq %rsi, %rbp
jne 0x4ba0a
movl (%r13), %edi
movl 0x8(%r13), %r8d
movslq 0x18(%r13), %rsi
movq 0x8(%rsp), %r10
movl 0x4(%r10), %r9d
movl 0x10(%r10), %r10d
cmpl %edi, %r9d
cmovgl %r9d, %edi
movslq %edi, %r15
cmpl %r8d, %r10d
cmovgl %r10d, %r8d
movslq %r8d, %r10
jmp 0x4ba0c
xorl %esi, %esi
movslq 0x14(%r13), %rdi
movq %r11, %r14
cmpq %rdi, %rbp
jne 0x4ba44
movslq 0x4(%r13), %r9
movslq 0xc(%r13), %r8
movslq 0x1c(%r13), %rdx
movq 0x8(%rsp), %rcx
movl 0x8(%rcx), %eax
movl 0x14(%rcx), %ecx
cmpl %r9d, %eax
cmovgel %r9d, %eax
cltq
cmpl %r8d, %ecx
cmovgel %r8d, %ecx
movslq %ecx, %rcx
jmp 0x4ba4a
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, %r13
movq %r10, %r15
callq 0x4b45e
movq %r15, %r10
movq %r13, %r15
addq $0x30, %rsp
movq %rax, %r11
addq %r14, %r11
movq 0x38(%rsp), %r13
decq %rbp
addq $-0x20, %r12
jmp 0x4b8d5
subq %r15, %rax
subq %r10, %rcx
cmpq $0x200, %rax            # imm = 0x200
jg 0x4baa3
cmpq %rcx, %rax
cmovgq %rax, %rcx
cvtsi2sd %rcx, %xmm0
jmp 0x4bad9
cmpq %rcx, %rax
cmovlq %rax, %rcx
cvtsi2sd %rcx, %xmm1
cvtsi2sd %rax, %xmm2
mulsd 0x197adc(%rip), %xmm2   # 0x1e3598
addsd 0x1977dc(%rip), %xmm2   # 0x1e32a0
movapd %xmm2, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
cvtsi2sd %r11, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r11
jmp 0x4ba7b
movq 0x8(%rsp), %rax
movl 0x4(%rax), %esi
cmpq %rsi, %r15
jl 0x4bd90
movl 0x10(%rax), %ecx
cmpq %rcx, %r10
jl 0x4bd90
movslq 0x28(%r13), %r9
movq 0x10(%rsp), %rdi
movq %r15, %rdx
movq %r10, %r8
jmp 0x4bd65
movl 0x4(%rdi), %r15d
movl 0x10(%rdi), %r14d
movslq 0x10(%r13), %r10
movq %r10, %rbp
shlq $0x5, %rbp
xorl %r11d, %r11d
movslq 0x14(%r13), %rax
cmpq %rax, %r10
jg 0x4bd40
movslq (%rbx,%rbp), %rcx
movslq 0x8(%rbx,%rbp), %rax
cmpq %rcx, %r15
movq %r10, 0x38(%rsp)
jg 0x4bbb0
cmpq %rax, %r14
jg 0x4bbb0
movq %r11, %r12
movslq 0x10(%r13), %rdx
pushq $-0x1
popq %r9
cmpq %rdx, %r10
jne 0x4bb69
movslq 0x28(%r13), %r9
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %rcx, %rdx
movq %r14, %rcx
movq %rax, %r8
movsd 0x18(%rsp), %xmm0
pushq 0x150(%rsp)
pushq 0x28(%rsp)
pushq 0x38(%rsp)
pushq 0x48(%rsp)
callq 0x4b6f5
addq $0x20, %rsp
movq %r12, %r11
addq %rax, %r11
movslq (%rbx,%rbp), %rcx
movslq 0x8(%rbx,%rbp), %rax
movq 0x38(%rsp), %r10
movslq 0x4(%rbx,%rbp), %r15
incq %r15
movslq 0xc(%rbx,%rbp), %r14
incq %r14
movl 0x1c(%rbx,%rbp), %edx
cmpw $0x7fff, 0x14(%rbx,%rbp) # imm = 0x7FFF
jne 0x4bbdd
testl %edx, %edx
jne 0x4bbdd
cmpw $0x0, 0x12(%rbx,%rbp)
js 0x4bcd7
movq 0x10(%rsp), %r8
movq 0x58(%r8), %rsi
movl 0x18(%rsi,%rbp), %edi
addq %rdi, %rdi
addq 0x70(%r8), %rdi
movq %rdi, 0xf8(%rsp)
movl 0x1c(%rsi,%rbp), %edi
movq %rdi, 0xf0(%rsp)
movq %rdi, 0xe8(%rsp)
movq (%rsi,%rbp), %rdi
movq %rdi, 0x5c(%rsp)
movq 0x8(%rsi,%rbp), %rdi
movq %rdi, 0x50(%rsp)
movswl 0x14(%rsi,%rbp), %esi
movl %esi, 0x6c(%rsp)
movl %esi, 0x68(%rsp)
movslq 0x10(%r13), %rsi
cmpq %rsi, %r10
jne 0x4bc63
movl (%r13), %eax
movl 0x8(%r13), %edi
movslq 0x18(%r13), %rsi
movq 0x8(%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 0x4bc65
xorl %esi, %esi
movslq 0x14(%r13), %rdi
movq %r11, %r12
cmpq %rdi, %r10
jne 0x4bca0
movslq 0x4(%r13), %r9
movslq 0xc(%r13), %r8
movslq 0x1c(%r13), %rdx
movq 0x8(%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 0x4bca6
movq %r15, %r9
movq %r14, %r8
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
pushq %r15
pushq %rcx
pushq %r14
pushq %rax
pushq %r9
callq 0x4b45e
addq $0x30, %rsp
movq %rax, %r11
addq %r12, %r11
movq 0x38(%rsp), %r10
incq %r10
addq $0x20, %rbp
jmp 0x4bb30
movq %r15, %rdx
subq %rcx, %rdx
movq %r14, %rcx
subq %rax, %rcx
cmpq $0x200, %rdx            # imm = 0x200
jg 0x4bcfa
cmpq %rcx, %rdx
cmovgq %rdx, %rcx
cvtsi2sd %rcx, %xmm0
jmp 0x4bd30
cmpq %rcx, %rdx
cmovlq %rdx, %rcx
cvtsi2sd %rcx, %xmm1
cvtsi2sd %rdx, %xmm2
mulsd 0x197885(%rip), %xmm2   # 0x1e3598
addsd 0x197585(%rip), %xmm2   # 0x1e32a0
movapd %xmm2, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
cvtsi2sd %r11, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r11
jmp 0x4bccb
movq 0x8(%rsp), %rax
movl 0x8(%rax), %edx
cmpq %rdx, %r15
jg 0x4bd90
movl 0x14(%rax), %r8d
cmpq %r8, %r14
jg 0x4bd90
movslq 0x24(%r13), %r9
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rcx
movsd 0x18(%rsp), %xmm0
pushq 0x150(%rsp)
pushq 0x28(%rsp)
pushq 0x38(%rsp)
pushq 0x48(%rsp)
movq %r11, %rbx
callq 0x4b6f5
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