// File: /workspace/github_cpp_makefile_star2_O2/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), %eax
movl 0x4(%rcx), %ecx
movq %rcx, 0x30(%rsp)
movl 0x8(%rbx), %r8d
movq %r8, 0x18(%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), %r14
addq %r9, %r14
movq %r13, %r10
shlq $0x5, %r10
movslq 0x4(%r8,%r10), %r12
subq %r9, %r12
incq %r12
cmpq %rsi, %r14
cmovleq %rsi, %r14
cmpq %rdx, %r12
cmovgeq %rdx, %r12
pushq $-0x1
popq %r10
movq %r12, %rsi
subq %r14, %rsi
jle 0x3b2a0
cmpl %r13d, %eax
movq %rax, %rbp
cmovaq %r11, %rbp
cmpl %r11d, %eax
cmovbq %r11, %rbp
movq %rbp, %rdx
shlq $0x5, %rdx
leaq (%rdx,%r8), %r15
addq $0x20, %r15
cmpq %r11, %rbp
jl 0x3b2a0
movslq -0x20(%r15), %rdx
cmpq %rdx, %r14
jge 0x3b29b
decq %rbp
addq $-0x20, %r15
jmp 0x3b284
cmpq %r13, %rbp
jle 0x3b2b5
movq %r10, %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq %r13, %rbp
jg 0x3b2a0
movslq -0x1c(%r15), %rdx
cmpq %rdx, %r14
jle 0x3b2cc
incq %rbp
addq $0x20, %r15
jmp 0x3b2b5
cmpq %r11, %rbp
jl 0x3b2a0
cmpq %rbp, %rax
je 0x3b2e5
movslq -0x20(%r15), %rcx
movq %rcx, 0x30(%rsp)
andq $0x0, 0x18(%rsp)
xorl %r10d, %r10d
movq %rdi, 0x38(%rsp)
cmpq %r13, %rbp
jg 0x3b505
movq %rsi, (%rsp)
movl -0x20(%r15), %r9d
movslq %r9d, %rsi
cmpq %rsi, %r12
jle 0x3b50a
movq %r10, 0x10(%rsp)
movslq -0x1c(%r15), %r10
leaq 0x1(%r10), %rax
cmpq %rsi, %r14
cmovgq %r14, %rsi
cmpq %rax, %r12
movq %rax, %rdx
cmovlq %r12, %rdx
movq %rdx, %r11
subq %rsi, %r11
movl $0x0, %edi
cmovleq %rdi, %r11
testq %r11, %r11
je 0x3b3a6
movq %r11, 0x8(%rsp)
movl %eax, %ecx
subl %r9d, %ecx
movslq %ecx, %rdi
movq %rdi, 0x28(%rsp)
movl -0x18(%r15), %edi
movl %edi, 0x24(%rsp)
movl -0x14(%r15), %r11d
movswq -0xc(%r15), %r8
movl -0x4(%r15), %edi
cmpq $0x7fff, %r8            # imm = 0x7FFF
jne 0x3b3b7
testl %edi, %edi
jne 0x3b3bf
xorl %edi, %edi
cmpw $0x0, -0xe(%r15)
jns 0x3b3bf
subl 0x24(%rsp), %r11d
incl %r11d
movslq %r11d, %rdx
cmpl $0x200, %ecx            # imm = 0x200
jg 0x3b469
movq 0x28(%rsp), %rdi
cmpq %rdx, %rdi
cmovgq %rdi, %rdx
cvtsi2sd %edx, %xmm0
jmp 0x3b4a2
movq %rcx, %rax
movq 0x10(%rsp), %r10
movq (%rsp), %rsi
jmp 0x3b4de
testl %edi, %edi
je 0x3b454
movl %edi, %ecx
movq 0x28(%rsp), %rdi
cmpq %rdi, 0x8(%rsp)
jne 0x3b3dc
movq 0x10(%rsp), %r10
addq %r8, %r10
movq %rcx, 0x18(%rsp)
jmp 0x3b446
movl -0x8(%r15), %eax
addq %rax, %rax
movq 0x38(%rsp), %rdi
addq 0x70(%rdi), %rax
movq %rax, 0xf8(%rsp)
movq %rcx, 0xf0(%rsp)
movq %rcx, 0xe8(%rsp)
movl %r9d, 0x5c(%rsp)
movl %r10d, 0x60(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x50(%rsp)
movl %r11d, 0x54(%rsp)
movl %r8d, 0x6c(%rsp)
movl %r8d, 0x68(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x30(%rsp), %rcx
leaq 0x18(%rsp), %r8
callq 0x3a251
movq 0x10(%rsp), %r10
addq %rax, %r10
movq 0x30(%rsp), %rax
movq (%rsp), %rsi
movq 0x8(%rsp), %r11
jmp 0x3b4de
movq 0x10(%rsp), %r10
movq (%rsp), %rsi
movq 0x8(%rsp), %r11
movq 0x28(%rsp), %rdi
jmp 0x3b4b5
movq 0x28(%rsp), %rdi
cmpq %rdx, %rdi
cmovlq %rdi, %rdx
cvtsi2sd %edx, %xmm1
cvtsi2sd %ecx, %xmm2
mulsd 0x1a8113(%rip), %xmm2   # 0x1e3598
addsd 0x1a7e13(%rip), %xmm2   # 0x1e32a0
movapd %xmm2, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
movq 0x10(%rsp), %r10
movq (%rsp), %rsi
movq 0x8(%rsp), %r11
cvttsd2si %xmm0, %r8
cmpq %rdi, %r11
jge 0x3b4d5
cvtsi2sd %r11, %xmm0
cvtsi2sd %ecx, %xmm1
cvtsi2sd %r8, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %r8
addq %r8, %r10
andq $0x0, 0x18(%rsp)
subq %r11, %rsi
cmpq %r12, %rax
jge 0x3b516
cmpq %r13, %rbp
jge 0x3b513
movslq (%r15), %rcx
andq $0x0, 0x18(%rsp)
movq %rcx, 0x30(%rsp)
addq $0x20, %r15
incq %rbp
jmp 0x3b2ed
movq %rcx, %rax
jmp 0x3b516
movq %rcx, %rax
movq (%rsp), %rsi
jmp 0x3b516
incq %rbp
testq %rsi, %rsi
jne 0x3b52c
movl %ebp, (%rbx)
movl %eax, 0x4(%rbx)
movl 0x18(%rsp), %eax
movl %eax, 0x8(%rbx)
jmp 0x3b2a0
leaq 0x1aa584(%rip), %rdi    # 0x1e5ab7
leaq 0x1a8169(%rip), %rsi    # 0x1e36a3
leaq 0x1aa71d(%rip), %rcx    # 0x1e5c5e
movl $0x484c, %edx           # imm = 0x484C
callq 0x2110