// File: /workspace/github_cpp_makefile_star2_O1/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 $0x108, %rsp            # imm = 0x108
movq %rcx, %rbx
movl (%rcx), %r15d
movl 0x4(%rcx), %eax
movq %rax, (%rsp)
movl 0x8(%rcx), %eax
movq %rax, 0x8(%rsp)
movl 0x10(%rcx), %r8d
movl 0x14(%rcx), %r12d
movl 0x18(%rcx), %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
movq 0x58(%rdi), %rcx
movq %r8, %r9
shlq $0x5, %r9
movslq (%rcx,%r9), %rbp
addq %rax, %rbp
movq %r12, %r9
shlq $0x5, %r9
movslq 0x4(%rcx,%r9), %r13
subq %rax, %r13
incq %r13
cmpq %rsi, %rbp
cmovleq %rsi, %rbp
cmpq %rdx, %r13
cmovgeq %rdx, %r13
movq $-0x1, %rax
movq %r13, %r14
subq %rbp, %r14
jle 0x6cfdf
movq %rdi, %r11
cmpl %r12d, %r15d
movq %r15, %rdi
cmovaq %r8, %rdi
cmpl %r8d, %r15d
cmovbq %r8, %rdi
cmpq %r8, %rdi
setb %dl
jb 0x6cd22
movq %rdi, %r9
shlq $0x5, %r9
movslq (%rcx,%r9), %rsi
cmpq %rsi, %rbp
jge 0x6cd22
leaq -0x1(%r8), %rsi
addq %rcx, %r9
addq $-0x20, %r9
cmpq %r8, %rdi
setle %dl
jle 0x6cd1f
decq %rdi
movslq (%r9), %r10
addq $-0x20, %r9
cmpq %r10, %rbp
jl 0x6cd06
jmp 0x6cd22
movq %rsi, %rdi
testb %dl, %dl
jne 0x6cfdf
cmpq %r12, %rdi
jg 0x6cfdf
movq %rdi, %rdx
shlq $0x5, %rdx
movslq 0x4(%rcx,%rdx), %rsi
cmpq %rsi, %rbp
jle 0x6cd6a
leaq 0x1(%r12), %rsi
leaq (%rdx,%rcx), %r9
addq $0x24, %r9
cmpq %r12, %rdi
setge %dl
jge 0x6cd6e
incq %rdi
movslq (%r9), %r10
addq $0x20, %r9
cmpq %r10, %rbp
jg 0x6cd51
jmp 0x6cd71
xorl %edx, %edx
jmp 0x6cd71
movq %rsi, %rdi
cmpq %r8, %rdi
setl %sil
orb %dl, %sil
jne 0x6cfdf
cmpq %r15, %rdi
je 0x6cd9e
movq %rdi, %rdx
shlq $0x5, %rdx
movslq (%rcx,%rdx), %rdx
movq %rdx, (%rsp)
movq $0x0, 0x8(%rsp)
cmpq %r12, %rdi
jg 0x6cfc9
movq %rdi, %rdx
shlq $0x5, %rdx
movl (%rcx,%rdx), %r8d
movslq %r8d, %rsi
cmpq %rsi, %r13
jle 0x6cfc9
leaq (%rdx,%rcx), %r15
addq $0x20, %r15
movq $0x0, 0x10(%rsp)
movq %r11, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movq %rdi, %r9
movslq -0x1c(%r15), %rax
incq %rax
cmpq %rsi, %rbp
cmovgq %rbp, %rsi
cmpq %rax, %r13
movq %rax, %rdx
cmovlq %r13, %rdx
movq %rdx, %r14
subq %rsi, %r14
movl $0x0, %ecx
cmovleq %rcx, %r14
testq %r14, %r14
je 0x6cf82
movq %r9, 0x18(%rsp)
movl %eax, %ecx
subl %r8d, %ecx
movslq %ecx, %rdi
movl -0x14(%r15), %r10d
subl -0x18(%r15), %r10d
movswq -0xc(%r15), %r9
cmpq $0x7fff, %r9            # imm = 0x7FFF
jne 0x6ce4b
cmpl $0x0, -0x4(%r15)
jne 0x6ce42
cmpw $0x0, -0xe(%r15)
js 0x6cecd
cmpl $0x7fff, %r9d           # imm = 0x7FFF
je 0x6ce52
cmpl $0x0, -0x4(%r15)
je 0x6cecd
cmpq %rdi, %r14
je 0x6cf6b
movl -0x8(%r15), %eax
addq %rax, %rax
addq 0x70(%r11), %rax
movq %rax, 0xe8(%rsp)
movl -0x4(%r15), %eax
movq %rax, 0xe0(%rsp)
movq %rax, 0xd8(%rsp)
movl %r8d, 0x4c(%rsp)
movl -0x1c(%r15), %eax
movl %eax, 0x50(%rsp)
movl -0x18(%r15), %eax
movl %eax, 0x40(%rsp)
movl -0x14(%r15), %eax
movl %eax, 0x44(%rsp)
movswl -0xc(%r15), %eax
movl %eax, 0x5c(%rsp)
movl %eax, 0x58(%rsp)
leaq 0x30(%rsp), %rdi
movq %rsp, %rcx
leaq 0x8(%rsp), %r8
callq 0x6baf1
movq 0x20(%rsp), %r11
addq %rax, 0x10(%rsp)
jmp 0x6cf7d
cmpl $0x7fff, %r9d           # imm = 0x7FFF
jne 0x6cf42
cmpl $0x0, -0x4(%r15)
jne 0x6cf42
cmpw $0x0, -0xe(%r15)
jns 0x6cf42
incl %r10d
movslq %r10d, %rdx
cmpl $0x200, %ecx            # imm = 0x200
jg 0x6cf03
cmpq %rdx, %rdi
cmovgq %rdi, %rdx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
jmp 0x6cf3d
cmpq %rdx, %rdi
cmovlq %rdi, %rdx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
mulsd 0x1b8628(%rip), %xmm2   # 0x225548
addsd 0x1b8348(%rip), %xmm2   # 0x225270
movapd %xmm2, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm2, %xmm0
orpd %xmm1, %xmm0
cvttsd2si %xmm0, %r9
cmpq %rdi, %r14
jge 0x6cf6b
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r9, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %r9
addq %r9, 0x10(%rsp)
movq %rax, (%rsp)
movl -0x4(%r15), %eax
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %r9
movq 0x28(%rsp), %rax
subq %r14, %rax
movq %rax, %r14
cmpq %r13, (%rsp)
jge 0x6cfbf
cmpq %r12, %r9
jge 0x6cfbc
movl (%r15), %r8d
movslq %r8d, %rsi
movq %rsi, (%rsp)
movq $0x0, 0x8(%rsp)
addq $0x20, %r15
leaq 0x1(%r9), %rdi
cmpq %rsi, %r13
jg 0x6cdd4
incq %r9
movq %r9, %rdi
movq 0x10(%rsp), %rax
jmp 0x6cfcb
xorl %eax, %eax
testq %r14, %r14
jne 0x6cff1
movl %edi, (%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 0x1baa5f(%rip), %rdi    # 0x227a57
leaq 0x1b8644(%rip), %rsi    # 0x225643
leaq 0x1babf8(%rip), %rcx    # 0x227bfe
movl $0x484c, %edx           # imm = 0x484C
callq 0x2110