// File: /workspace/github_cpp_makefile_star2_O1/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 $0x108, %rsp            # imm = 0x108
movq %rcx, %rbx
movq %rdx, %r11
movq %rsi, %r15
movl (%rcx), %r8d
movl 0x4(%rcx), %eax
movq %rax, (%rsp)
movl 0x8(%rcx), %eax
movq %rax, 0x8(%rsp)
movl 0x10(%rcx), %edx
movl 0x14(%rcx), %esi
movl %r8d, %ecx
cmpl %esi, %r8d
movq %rcx, %r12
cmovaq %rdx, %r12
cmpl %edx, %r8d
cmovbq %rdx, %r12
cmpq %rdx, %r12
setb %bpl
jb 0x6bc95
movq 0x58(%rdi), %r9
movq %r12, %r10
shlq $0x5, %r10
movslq (%r9,%r10), %r8
cmpq %r15, %r8
jle 0x6bc95
leaq -0x1(%rdx), %r8
addq %r10, %r9
addq $-0x20, %r9
cmpq %rdx, %r12
setle %bpl
jle 0x6bc92
decq %r12
movslq (%r9), %r10
addq $-0x20, %r9
cmpq %r15, %r10
jg 0x6bc78
jmp 0x6bc95
movq %r8, %r12
movq $-0x1, %rax
testb %bpl, %bpl
jne 0x6bf8d
movl %esi, %ebp
cmpq %rbp, %r12
jg 0x6bf8d
movq 0x58(%rdi), %rsi
movq %r12, %r8
shlq $0x5, %r8
movslq 0x4(%rsi,%r8), %r9
cmpq %r15, %r9
jge 0x6bcea
leaq 0x1(%rbp), %r10
addq %rsi, %r8
addq $0x24, %r8
cmpq %rbp, %r12
setge %sil
jge 0x6bcee
incq %r12
movslq (%r8), %r9
addq $0x20, %r8
cmpq %r15, %r9
jl 0x6bcd0
jmp 0x6bcf1
xorl %esi, %esi
jmp 0x6bcf1
movq %r10, %r12
cmpq %rdx, %r12
setl %dl
orb %sil, %dl
jne 0x6bf8d
cmpq %rcx, %r12
je 0x6bd21
movq 0x58(%rdi), %rcx
movq %r12, %rdx
shlq $0x5, %rdx
movslq (%rcx,%rdx), %rcx
movq %rcx, (%rsp)
movq $0x0, 0x8(%rsp)
movq %r11, %r14
subq %r15, %r14
cmpq %rbp, %r12
jg 0x6bf76
movq 0x58(%rdi), %rdx
movq %r12, %r8
shlq $0x5, %r8
movl (%rdx,%r8), %ecx
movslq %ecx, %rsi
cmpq %r11, %rsi
jge 0x6bf76
movq %rdi, 0x20(%rsp)
leaq (%r8,%rdx), %r13
addq $0x20, %r13
movq $0x0, 0x10(%rsp)
movq %r11, 0x28(%rsp)
movq %r12, %r8
movslq -0x1c(%r13), %rax
incq %rax
cmpq %r15, %rsi
cmovleq %r15, %rsi
cmpq %r11, %rax
movq %r11, %rdx
cmovlq %rax, %rdx
movq %rdx, %r12
subq %rsi, %r12
movl $0x0, %edi
cmovleq %rdi, %r12
testq %r12, %r12
je 0x6bf36
movq %r8, 0x18(%rsp)
movl %eax, %edi
subl %ecx, %edi
movslq %edi, %r8
movl -0x14(%r13), %r10d
subl -0x18(%r13), %r10d
movswq -0xc(%r13), %r9
cmpq $0x7fff, %r9            # imm = 0x7FFF
jne 0x6bdd3
cmpl $0x0, -0x4(%r13)
jne 0x6bdca
cmpw $0x0, -0xe(%r13)
js 0x6bdf6
cmpl $0x7fff, %r9d           # imm = 0x7FFF
je 0x6bdda
cmpl $0x0, -0x4(%r13)
je 0x6bdf6
cmpq %r8, %r12
jne 0x6be3f
addq %r9, 0x10(%rsp)
movq %rax, (%rsp)
movl -0x4(%r13), %eax
movq %rax, 0x8(%rsp)
jmp 0x6beb0
cmpl $0x7fff, %r9d           # imm = 0x7FFF
jne 0x6bef6
cmpl $0x0, -0x4(%r13)
jne 0x6bef6
cmpw $0x0, -0xe(%r13)
jns 0x6bef6
incl %r10d
movslq %r10d, %rcx
cmpl $0x200, %edi            # imm = 0x200
jg 0x6beb7
cmpq %rcx, %r8
cmovgq %r8, %rcx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
jmp 0x6bef1
movl -0x8(%r13), %eax
addq %rax, %rax
movq 0x20(%rsp), %rdi
addq 0x70(%rdi), %rax
movq %rax, 0xe8(%rsp)
movl -0x4(%r13), %eax
movq %rax, 0xe0(%rsp)
movq %rax, 0xd8(%rsp)
movl %ecx, 0x4c(%rsp)
movl -0x1c(%r13), %eax
movl %eax, 0x50(%rsp)
movl -0x18(%r13), %eax
movl %eax, 0x40(%rsp)
movl -0x14(%r13), %eax
movl %eax, 0x44(%rsp)
movswl -0xc(%r13), %eax
movl %eax, 0x5c(%rsp)
movl %eax, 0x58(%rsp)
leaq 0x30(%rsp), %rdi
movq %rsp, %rcx
leaq 0x8(%rsp), %r8
callq 0x6baf1
movq 0x28(%rsp), %r11
addq %rax, 0x10(%rsp)
movq 0x18(%rsp), %r8
jmp 0x6bf36
cmpq %rcx, %r8
cmovlq %r8, %rcx
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %edi, %xmm2
mulsd 0x1b9674(%rip), %xmm2   # 0x225548
addsd 0x1b9394(%rip), %xmm2   # 0x225270
movapd %xmm2, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
cvttsd2si %xmm0, %r9
cmpq %r8, %r12
jge 0x6bf1f
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %edi, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r9, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %r9
movq 0x18(%rsp), %r8
addq %r9, 0x10(%rsp)
movq %rax, (%rsp)
movl -0x4(%r13), %eax
movq %rax, 0x8(%rsp)
subq %r12, %r14
cmpq %r11, (%rsp)
jge 0x6bf6c
cmpq %rbp, %r8
jge 0x6bf69
movl (%r13), %ecx
movslq %ecx, %rsi
movq %rsi, (%rsp)
movq $0x0, 0x8(%rsp)
addq $0x20, %r13
leaq 0x1(%r8), %r12
cmpq %r11, %rsi
jl 0x6bd66
incq %r8
movq %r8, %r12
movq 0x10(%rsp), %rax
jmp 0x6bf78
xorl %eax, %eax
testq %r14, %r14
jne 0x6bf9f
movl %r12d, (%rbx)
movl (%rsp), %ecx
movl %ecx, 0x4(%rbx)
movl 0x8(%rsp), %ecx
movl %ecx, 0x8(%rbx)
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1bbab1(%rip), %rdi    # 0x227a57
leaq 0x1b9696(%rip), %rsi    # 0x225643
leaq 0x1bbaa8(%rip), %rcx    # 0x227a5c
movl $0x46e7, %edx           # imm = 0x46E7
callq 0x2110