// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: extract_sub_cigar_err(overlap_region*, long, long, ul_ov_t*)
int64_t extract_sub_cigar_err(overlap_region *z, int64_t s, int64_t e, ul_ov_t *p)
{
    int64_t wk = ovlp_cur_wid(*p), xk = ovlp_cur_xoff(*p), ck = ovlp_cur_coff(*p);
    int64_t min_w = ovlp_min_wid(*p), max_w = ovlp_max_wid(*p);//[min_w, max_w]
    bit_extz_t ez; window_list *m; 
    int64_t ws, we, os, oe, ovlp, err = 0, xl, yl, werr, tot = e - s;
    if(wk < min_w || wk > max_w) wk = min_w;
    for (; wk >= min_w && z->w_list.a[wk].x_start > s; wk--); 
    if(wk < min_w || wk > max_w) return -1;
    for (; wk <= max_w && z->w_list.a[wk].x_end < s; wk++); 
    if(wk < min_w || wk > max_w) return -1;
    //s >= w_list.a[wk].x_start && s <= w_list.a[wk].x_end
    if(wk != ovlp_cur_wid(*p)) {//xk is global, while ck is local
        xk = z->w_list.a[wk].x_start; ck = 0;
    }
    // fprintf(stderr, "[M::%s] wk::%ld, ck::%ld, xk::%ld\n", __func__, wk, ck, xk);
    // fprintf(stderr, "+[M::%s] wk::%ld, ck::%ld, xk::%ld, w::[%d, %d), bound::[%ld, %ld)\n", 
    //     __func__, wk, ck, xk, z->w_list.a[wk].x_start, z->w_list.a[wk].x_end+1, s, e);

    while(wk <= max_w && z->w_list.a[wk].x_start < e) {///[s, e)
        m = &(z->w_list.a[wk]); 
        ws = m->x_start; we = m->x_end+1;
        os = MAX(s, ws); oe = MIN(e, we);
        ovlp = ((oe>os)? (oe-os):0);
        // fprintf(stderr, "-[M::%s] ovlp::%ld, wk::%ld, ck::%ld, xk::%ld, w::[%ld, %ld), bound::[%ld, %ld)\n", 
        // __func__, ovlp, wk, ck, xk, ws, we, s, e);
        if(ovlp) {
            xl = m->x_end+1-m->x_start;
            yl = m->y_end+1-m->y_start;
            if((is_ualn_win((*m))) || (is_est_aln((*m)))) {
                if(is_ualn_win((*m))) { //unmapped
                    werr = gen_err_unaligned(xl, yl);
                } else {
                    werr = m->error;//shared window
                }
                if(ovlp < xl) {
                    werr = (((double)ovlp)/((double)xl))*((double)werr);
                }
                //skip the whole window
                err += werr; xk = m->x_end+1; ck = m->clen;
            } else {
                if(ovlp == xl) {
                    //skip the whole window
                    err += m->error; xk = m->x_end+1; ck = m->clen;
                } else {
                    // if(os == 22694 && oe == 38018) {
                    //     fprintf(stderr, "\n-[M::%s]\tos::%ld\toe::%ld\n", __func__, os, oe);
                    // } 
                    set_bit_extz_t(ez, (*z), wk); 
                    err += retrieve_cigar_err(&ez, os, oe, &xk, &ck);
                }                
            }
        }
        tot -= ovlp; 
        if(xk >= e) break;//[min_w, max_w] && [s, e)
        wk++; if(wk > max_w) break;
        xk = z->w_list.a[wk].x_start; ck = 0;//reset
    }
    assert(!tot);
    ovlp_cur_wid(*p) = wk; ovlp_cur_xoff(*p) = xk; ovlp_cur_coff(*p) = ck;
    return err;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rsi, %r12
movq %rdi, %r13
movl (%rcx), %esi
movl 0x4(%rcx), %r8d
movq %r8, 0x30(%rsp)
movl 0x8(%rcx), %eax
movq %rax, 0x10(%rsp)
movl 0x10(%rcx), %eax
movl 0x14(%rcx), %edi
movq %rdx, %r11
subq %r12, %r11
movl %esi, %r9d
cmpl %edi, %esi
movq %r9, %rbp
cmovaq %rax, %rbp
movl %edi, %r14d
cmpl %eax, %esi
cmovbq %rax, %rbp
movq %rbp, %rsi
shlq $0x5, %rsi
addq $0x20, %rsi
pushq $-0x1
popq %r10
cmpq %rax, %rbp
jl 0x3a3d4
movq 0x58(%r13), %rbx
movslq -0x20(%rbx,%rsi), %rdi
cmpq %r12, %rdi
jle 0x3a3cf
decq %rbp
addq $-0x20, %rsi
jmp 0x3a3b3
cmpq %r14, %rbp
jle 0x3a3e9
movq %r10, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq %rsi, %rbx
cmpq %r14, %rbp
jg 0x3a3d4
movslq -0x1c(%rbx), %rsi
cmpq %r12, %rsi
jge 0x3a403
incq %rbp
addq $0x20, %rbx
jmp 0x3a3ec
cmpq %rax, %rbp
jl 0x3a3d4
cmpq %rbp, %r9
je 0x3a41c
movslq -0x20(%rbx), %r8
movq %r8, 0x30(%rsp)
andq $0x0, 0x10(%rsp)
xorl %r10d, %r10d
movq %rcx, 0x18(%rsp)
movq %rdx, 0x38(%rsp)
cmpq %r14, %rbp
jg 0x3a62d
movl -0x20(%rbx), %r9d
movslq %r9d, %rsi
cmpq %rdx, %rsi
jge 0x3a62d
movq %r10, 0x8(%rsp)
movslq -0x1c(%rbx), %r10
leaq 0x1(%r10), %rax
cmpq %r12, %rsi
cmovleq %r12, %rsi
cmpq %rdx, %rax
cmovlq %rax, %rdx
movq %rdx, %r15
subq %rsi, %r15
movl $0x0, %edi
cmovleq %rdi, %r15
testq %r15, %r15
je 0x3a4d8
movq %r11, 0x28(%rsp)
movl %eax, %r11d
subl %r9d, %r11d
movslq %r11d, %rcx
movl -0x18(%rbx), %edi
movl %edi, 0x20(%rsp)
movl -0x14(%rbx), %edi
movl %edi, 0x24(%rsp)
movswq -0xc(%rbx), %r8
movl -0x4(%rbx), %edi
cmpq $0x7fff, %r8            # imm = 0x7FFF
jne 0x3a4e5
testl %edi, %edi
jne 0x3a4ed
xorl %edi, %edi
cmpw $0x0, -0xe(%rbx)
jns 0x3a4ed
movl 0x24(%rsp), %edx
subl 0x20(%rsp), %edx
incl %edx
movslq %edx, %rdx
cmpl $0x200, %r11d           # imm = 0x200
jg 0x3a58b
cmpq %rdx, %rcx
movq %rcx, %rsi
cmovgq %rcx, %rdx
cvtsi2sd %edx, %xmm0
jmp 0x3a5c3
movq %r8, %rax
movq 0x8(%rsp), %r10
jmp 0x3a601
testl %edi, %edi
je 0x3a57c
movq %rcx, %r11
movl %edi, %ecx
cmpq %r11, %r15
jne 0x3a506
movq 0x8(%rsp), %r10
addq %r8, %r10
movq %rcx, 0x10(%rsp)
jmp 0x3a56d
movl -0x8(%rbx), %eax
addq %rax, %rax
addq 0x70(%r13), %rax
movq %rax, 0xf8(%rsp)
movq %rcx, 0xf0(%rsp)
movq %rcx, 0xe8(%rsp)
movl %r9d, 0x5c(%rsp)
movl %r10d, 0x60(%rsp)
movl 0x20(%rsp), %eax
movl %eax, 0x50(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x54(%rsp)
movl %r8d, 0x6c(%rsp)
movl %r8d, 0x68(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x30(%rsp), %rcx
leaq 0x10(%rsp), %r8
callq 0x3a251
movq 0x8(%rsp), %r10
addq %rax, %r10
movq 0x30(%rsp), %rax
movq 0x18(%rsp), %rcx
movq 0x28(%rsp), %r11
jmp 0x3a601
movq %rcx, %rsi
movq 0x18(%rsp), %rcx
movq 0x8(%rsp), %r10
jmp 0x3a5d2
cmpq %rdx, %rcx
movq %rcx, %rsi
cmovlq %rcx, %rdx
cvtsi2sd %edx, %xmm1
cvtsi2sd %r11d, %xmm2
mulsd 0x1a8ff2(%rip), %xmm2   # 0x1e3598
addsd 0x1a8cf2(%rip), %xmm2   # 0x1e32a0
movapd %xmm2, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
movq 0x18(%rsp), %rcx
movq 0x8(%rsp), %r10
cvttsd2si %xmm0, %r8
cmpq %rsi, %r15
jge 0x3a5f3
cvtsi2sd %r15, %xmm0
cvtsi2sd %r11d, %xmm1
cvtsi2sd %r8, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %r8
movq 0x28(%rsp), %r11
addq %r8, %r10
andq $0x0, 0x10(%rsp)
subq %r15, %r11
movq 0x38(%rsp), %rdx
cmpq %rdx, %rax
jge 0x3a635
cmpq %r14, %rbp
jge 0x3a632
movslq (%rbx), %r8
andq $0x0, 0x10(%rsp)
movq %r8, 0x30(%rsp)
addq $0x20, %rbx
incq %rbp
jmp 0x3a429
movq %r8, %rax
jmp 0x3a635
incq %rbp
testq %r11, %r11
jne 0x3a64b
movl %ebp, (%rcx)
movl %eax, 0x4(%rcx)
movl 0x10(%rsp), %eax
movl %eax, 0x8(%rcx)
jmp 0x3a3d4
leaq 0x1ab465(%rip), %rdi    # 0x1e5ab7
leaq 0x1a904a(%rip), %rsi    # 0x1e36a3
leaq 0x1ab45c(%rip), %rcx    # 0x1e5abc
movl $0x46e7, %edx           # imm = 0x46E7
callq 0x2110