// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: extract_sub_cigar_err_rr(overlap_region*, long, long, ul_ov_t*)
int64_t extract_sub_cigar_err_rr(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 bd = ovlp_bd(*p), s0, e0;
    s0 = ((int64_t)(z->w_list.a[min_w].x_start)) + bd; 
    e0 = ((int64_t)(z->w_list.a[max_w].x_end))+1-bd;
    if(s < s0) s = s0; if(e > e0) e = e0;///exclude boundary
    if(s >= e) return -1;

    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;
    }

    while(wk <= max_w && z->w_list.a[wk].x_start < e) {///[s, e); [min_w, max_w]
        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);
        
        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 {
                    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
movl (%rcx), %ecx
movl 0x4(%rbx), %eax
movq %rax, 0x20(%rsp)
movl 0x8(%rbx), %r8d
movq %r8, 0x8(%rsp)
movl 0x10(%rbx), %r11d
movl 0x14(%rbx), %r13d
movl 0x18(%rbx), %r9d
andl $0x3fffffff, %r9d       # imm = 0x3FFFFFFF
movq 0x58(%rdi), %r8
movq %r11, %r10
shlq $0x5, %r10
movslq (%r8,%r10), %rbp
addq %r9, %rbp
movq %r13, %r10
shlq $0x5, %r10
movslq 0x4(%r8,%r10), %r12
subq %r9, %r12
incq %r12
cmpq %rsi, %rbp
cmovleq %rsi, %rbp
cmpq %rdx, %r12
cmovgeq %rdx, %r12
movq $-0x1, %r9
movq %r12, %r14
subq %rbp, %r14
jle 0x6cace
cmpl %r13d, %ecx
movq %rcx, %rdx
cmovaq %r11, %rdx
cmpl %r11d, %ecx
cmovbq %r11, %rdx
cmpq %r11, %rdx
jb 0x6cace
movq %rdx, %rsi
shlq $0x5, %rsi
leaq (%rsi,%r8), %r15
addq $0x20, %r15
movslq -0x20(%r15), %rsi
cmpq %rsi, %rbp
jge 0x6c82b
leaq -0x1(%rdx), %rsi
addq $-0x20, %r15
cmpq %r11, %rdx
movq %rsi, %rdx
jg 0x6c80d
jmp 0x6cace
cmpq %r13, %rdx
jg 0x6cace
movslq -0x1c(%r15), %rsi
cmpq %rsi, %rbp
jle 0x6c852
leaq 0x1(%rdx), %rsi
addq $0x20, %r15
cmpq %r13, %rdx
movq %rsi, %rdx
jl 0x6c834
jmp 0x6cace
cmpq %r11, %rdx
jl 0x6cace
cmpq %rdx, %rcx
je 0x6c872
movslq -0x20(%r15), %rax
movq %rax, 0x20(%rsp)
movq $0x0, 0x8(%rsp)
cmpq %r13, %rdx
jg 0x6caaf
movl -0x20(%r15), %r11d
movslq %r11d, %rsi
cmpq %rsi, %r12
jle 0x6caaf
movq %rdi, 0x30(%rsp)
xorl %r9d, %r9d
movq %rdx, %r8
movslq -0x1c(%r15), %rdx
movq %rdx, 0x38(%rsp)
leaq 0x1(%rdx), %rcx
cmpq %rsi, %rbp
cmovgq %rbp, %rsi
cmpq %rcx, %r12
movq %rcx, %r10
cmovlq %r12, %r10
movq %r10, %rdx
subq %rsi, %rdx
movl $0x0, %edi
cmovleq %rdi, %rdx
movq %rdx, (%rsp)
testq %rdx, %rdx
je 0x6c933
movq %r8, 0x10(%rsp)
movq %r9, 0x28(%rsp)
movl %ecx, %eax
subl %r11d, %eax
movslq %eax, %rdx
movl -0x18(%r15), %edi
movl %edi, 0x1c(%rsp)
movl -0x14(%r15), %edi
movswq -0xc(%r15), %r9
movl -0x4(%r15), %r8d
cmpq $0x7fff, %r9            # imm = 0x7FFF
jne 0x6c93b
testl %r8d, %r8d
jne 0x6c944
xorl %r8d, %r8d
cmpw $0x0, -0xe(%r15)
jns 0x6c944
movq %rdx, %rsi
subl 0x1c(%rsp), %edi
incl %edi
movslq %edi, %rdx
cmpl $0x200, %eax            # imm = 0x200
jg 0x6ca2b
cmpq %rdx, %rsi
cmovgq %rsi, %rdx
cvtsi2sd %edx, %xmm0
jmp 0x6ca5f
movq %rax, %rcx
jmp 0x6c9d4
testl %r8d, %r8d
je 0x6ca1d
movl %r8d, %eax
cmpq %rdx, (%rsp)
jne 0x6c95f
movq 0x28(%rsp), %rdx
addq %r9, %rdx
movq %rdx, %r9
movq %rax, 0x8(%rsp)
jmp 0x6c9cf
movl -0x8(%r15), %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 %r11d, 0x5c(%rsp)
movq 0x38(%rsp), %rax
movl %eax, 0x60(%rsp)
movl 0x1c(%rsp), %eax
movl %eax, 0x50(%rsp)
movl %edi, 0x54(%rsp)
movl %r9d, 0x6c(%rsp)
movl %r9d, 0x68(%rsp)
leaq 0x40(%rsp), %rdi
movq %r10, %rdx
leaq 0x20(%rsp), %rcx
leaq 0x8(%rsp), %r8
callq 0x6b6d3
movq 0x28(%rsp), %r9
addq %rax, %r9
movq 0x20(%rsp), %rcx
movq 0x10(%rsp), %r8
movq (%rsp), %rdx
subq %rdx, %r14
cmpq %r12, %rcx
jge 0x6cab7
cmpq %r13, %r8
jge 0x6cab4
movl (%r15), %r11d
movslq %r11d, %rsi
movq %rsi, 0x20(%rsp)
movq $0x0, 0x8(%rsp)
addq $0x20, %r15
leaq 0x1(%r8), %rdx
movq %rsi, %rax
movq %rsi, %rcx
cmpq %rsi, %r12
jg 0x6c893
jmp 0x6cab4
movq %rdx, %rsi
movq 0x10(%rsp), %r8
movq (%rsp), %rdx
jmp 0x6ca6d
cmpq %rdx, %rsi
cmovlq %rsi, %rdx
cvtsi2sd %edx, %xmm1
cvtsi2sd %eax, %xmm2
mulsd 0x1c4c06(%rip), %xmm2   # 0x231648
addsd 0x1c48c6(%rip), %xmm2   # 0x231310
movapd %xmm2, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
movq 0x10(%rsp), %r8
movq (%rsp), %rdx
cvttsd2si %xmm0, %r9
cmpq %rsi, %rdx
jge 0x6ca96
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r9, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %r9
movq 0x28(%rsp), %rax
addq %r9, %rax
movq %rax, %r9
movq $0x0, 0x8(%rsp)
jmp 0x6c9d8
xorl %r9d, %r9d
jmp 0x6cabd
incq %r8
movq %rcx, %rax
movq %r8, %rdx
testq %r14, %r14
jne 0x6cae3
movl %edx, (%rbx)
movl %eax, 0x4(%rbx)
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rbx)
movq %r9, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c707d(%rip), %rdi    # 0x233b67
leaq 0x1c4c62(%rip), %rsi    # 0x231753
leaq 0x1c7216(%rip), %rcx    # 0x233d0e
movl $0x484c, %edx           # imm = 0x484C
callq 0x2110