// File: /workspace/github_cpp_makefile_star2_O3/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 %rcx, %rbx
movq %rdx, %r11
movq %rsi, %r12
movl (%rcx), %r8d
movl 0x4(%rcx), %eax
movq %rax, 0x28(%rsp)
movl 0x8(%rcx), %ecx
movq %rcx, 0x8(%rsp)
movl 0x10(%rbx), %esi
movl 0x14(%rbx), %r9d
movl %r8d, %ecx
cmpl %r9d, %r8d
movq %rcx, %rdx
cmovaq %rsi, %rdx
cmpl %esi, %r8d
cmovbq %rsi, %rdx
movq $-0x1, %r14
cmpq %rsi, %rdx
jb 0x6bb21
movl %r9d, %r13d
movq %r11, %r15
subq %r12, %r15
movq 0x58(%rdi), %r9
movq %rdx, %r8
shlq $0x5, %r8
leaq (%r8,%r9), %rbp
addq $0x20, %rbp
movslq -0x20(%rbp), %r8
cmpq %r12, %r8
jle 0x6b87f
leaq -0x1(%rdx), %r8
addq $-0x20, %rbp
cmpq %rsi, %rdx
movq %r8, %rdx
jg 0x6b861
jmp 0x6bb21
cmpq %r13, %rdx
jg 0x6bb21
movslq -0x1c(%rbp), %r8
cmpq %r12, %r8
jge 0x6b8a6
leaq 0x1(%rdx), %r8
addq $0x20, %rbp
cmpq %r13, %rdx
movq %r8, %rdx
jl 0x6b888
jmp 0x6bb21
cmpq %rsi, %rdx
jl 0x6bb21
cmpq %rdx, %rcx
je 0x6b8c6
movslq -0x20(%rbp), %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0x8(%rsp)
cmpq %r13, %rdx
jg 0x6bb02
movl -0x20(%rbp), %r9d
movslq %r9d, %rsi
cmpq %r11, %rsi
jge 0x6bb02
movq %rdi, 0x30(%rsp)
xorl %r14d, %r14d
movq %r11, 0x10(%rsp)
movq %rdx, %r8
movslq -0x1c(%rbp), %rdx
movq %rdx, 0x38(%rsp)
leaq 0x1(%rdx), %rcx
cmpq %r12, %rsi
cmovleq %r12, %rsi
cmpq %r11, %rcx
movq %r11, %r10
cmovlq %rcx, %r10
movq %r10, %rdx
subq %rsi, %rdx
movl $0x0, %edi
cmovleq %rdi, %rdx
movq %rdx, (%rsp)
testq %rdx, %rdx
je 0x6b98a
movq %r8, 0x18(%rsp)
movl %ecx, %eax
subl %r9d, %eax
movslq %eax, %rdx
movl -0x18(%rbp), %edi
movl %edi, 0x24(%rsp)
movl -0x14(%rbp), %r11d
movswq -0xc(%rbp), %rdi
movl -0x4(%rbp), %r8d
cmpq $0x7fff, %rdi           # imm = 0x7FFF
jne 0x6b992
testl %r8d, %r8d
jne 0x6b99b
xorl %r8d, %r8d
cmpw $0x0, -0xe(%rbp)
jns 0x6b99b
movq %rdx, %rsi
subl 0x24(%rsp), %r11d
incl %r11d
movslq %r11d, %rdx
cmpl $0x200, %eax            # imm = 0x200
jg 0x6ba7e
cmpq %rdx, %rsi
cmovgq %rsi, %rdx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
jmp 0x6bab5
movq %rax, %rcx
jmp 0x6ba21
testl %r8d, %r8d
je 0x6ba6b
movl %r8d, %eax
cmpq %rdx, (%rsp)
jne 0x6b9ae
addq %rdi, %r14
movq %rax, 0x8(%rsp)
jmp 0x6ba17
movl -0x8(%rbp), %ecx
addq %rcx, %rcx
movq 0x30(%rsp), %r8
addq 0x70(%r8), %rcx
movq %rcx, 0xf8(%rsp)
movq %rax, 0xf0(%rsp)
movq %rax, 0xe8(%rsp)
movl %r9d, 0x5c(%rsp)
movq 0x38(%rsp), %rax
movl %eax, 0x60(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x50(%rsp)
movl %r11d, 0x54(%rsp)
movl %edi, 0x6c(%rsp)
movl %edi, 0x68(%rsp)
leaq 0x40(%rsp), %rdi
movq %r10, %rdx
leaq 0x28(%rsp), %rcx
leaq 0x8(%rsp), %r8
callq 0x6b6d3
addq %rax, %r14
movq 0x28(%rsp), %rcx
movq 0x10(%rsp), %r11
movq 0x18(%rsp), %r8
movq (%rsp), %rdx
subq %rdx, %r15
cmpq %r11, %rcx
jge 0x6bb0a
cmpq %r13, %r8
jge 0x6bb07
movl (%rbp), %r9d
movslq %r9d, %rsi
movq %rsi, 0x28(%rsp)
movq $0x0, 0x8(%rsp)
addq $0x20, %rbp
leaq 0x1(%r8), %rdx
movq %rsi, %rax
movq %rsi, %rcx
cmpq %r11, %rsi
jl 0x6b8ec
jmp 0x6bb07
movq %rdx, %rsi
movq 0x10(%rsp), %r11
movq 0x18(%rsp), %r8
movq (%rsp), %rdx
jmp 0x6bac8
cmpq %rdx, %rsi
cmovlq %rsi, %rdx
cvtsi2sd %edx, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
mulsd 0x1c5bb0(%rip), %xmm2   # 0x231648
addsd 0x1c5870(%rip), %xmm2   # 0x231310
movapd %xmm2, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
movq 0x10(%rsp), %r11
movq 0x18(%rsp), %r8
movq (%rsp), %rdx
cvttsd2si %xmm0, %rdi
cmpq %rsi, %rdx
jge 0x6baf1
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %rdi, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rdi
addq %rdi, %r14
movq $0x0, 0x8(%rsp)
jmp 0x6ba25
xorl %r14d, %r14d
jmp 0x6bb10
incq %r8
movq %rcx, %rax
movq %r8, %rdx
testq %r15, %r15
jne 0x6bb36
movl %edx, (%rbx)
movl %eax, 0x4(%rbx)
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rbx)
movq %r14, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c802a(%rip), %rdi    # 0x233b67
leaq 0x1c5c0f(%rip), %rsi    # 0x231753
leaq 0x1c8021(%rip), %rcx    # 0x233b6c
movl $0x46e7, %edx           # imm = 0x46E7
callq 0x2110