// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: cal_estimate_err(overlap_region*, long, long, long, double)
int64_t cal_estimate_err(overlap_region *z, int64_t wl, int64_t qs, int64_t qe, double e_rate)
{
    int64_t k, ws, we, wid, os, oe, ovlp, tot, cov_l, est = (qe-qs)*e_rate, wn = z->w_list.n; 
    if(!wn) return est;
    if(qs < z->x_pos_s) qs = z->x_pos_s;
    if(qe > z->x_pos_e+1) qe = z->x_pos_e+1;
    ws = qs/wl; ws *= wl; wid = get_win_id_by_s(z, ws, wl, NULL);
    if(wid>=wn) wid = wn-1;
    for (k = wid;k < wn && qs > z->w_list.a[k].x_end; k++); if(k == wn) return est;
    ///qs <= z->w_list.a[k].x_end
    for (;k>=0 && qs < z->w_list.a[k].x_start; k--); if(k < 0) k = 0;
    ///qs >= z->w_list.a[k].x_start

    for (tot = cov_l = 0; k < wn && z->w_list.a[k].x_start < qe; k++) {
        if(z->w_list.a[k].y_end == -1) continue;
        ws = z->w_list.a[k].x_start; we = z->w_list.a[k].x_end+1;
        os = MAX(qs, ws); oe = MIN(qe, we);
	    ovlp = ((oe>os)? (oe-os):0);
        if(!ovlp) continue;
        cov_l += ovlp;
        // if(!ovlp) {
        //     fprintf(stderr, "\n[M::%s::] utg%.6dl(%c), q::[%ld, %ld), w::[%ld, %ld), z::::[%d, %d)\n", 
        //         __func__, (int32_t)z->y_id+1, "+-"[z->y_pos_strand], qs, qe, ws, we, z->x_pos_s, z->x_pos_e+1);
        // }
        if(ovlp == (we-ws)) {
            tot += z->w_list.a[k].error;
        } else {
            tot += ((double)z->w_list.a[k].error)*((double)ovlp)/((double)(we-ws));
        }
    }
    tot += ((qe-qs)-cov_l)*e_rate; 
    return tot;
}

// Assembly:
movq %rcx, %rax
subq %rdx, %rax
cvtsi2sd %rax, %xmm1
mulsd %xmm0, %xmm1
movq 0x48(%rdi), %r11
testq %r11, %r11
je 0x33313
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdx, %r8
movl 0x8(%rdi), %eax
movl 0x4(%rdi), %r9d
cmpq %rdx, %r9
cmovgq %r9, %r8
incl %eax
cmpq %rcx, %rax
cmovlq %rax, %rcx
movq %r8, %rax
cqto
idivq %rsi
movq %rdx, %r10
movq %r9, %rax
xorl %edx, %edx
idivq %rsi
addq %r9, %r10
subq %r10, %rdx
leaq (%rdx,%r8), %rax
cqto
idivq %rsi
leaq -0x1(%r11), %rsi
cmpq %r11, %rax
cmovlq %rax, %rsi
cmpq %r11, %rsi
movq %r11, %rdx
cmovgq %rsi, %rdx
movq %rsi, %rax
shlq $0x5, %rax
orq $0x4, %rax
cmpq %r11, %rsi
jge 0x33224
movq 0x58(%rdi), %r9
movslq (%r9,%rax), %r9
cmpq %r9, %r8
jle 0x33221
incq %rsi
addq $0x20, %rax
jmp 0x33206
movq %rsi, %rdx
cmpq %r11, %rdx
je 0x3330c
cmpq $-0x1, %rdx
movabsq $-0x1, %rax
cmovlq %rdx, %rax
movq %rdx, %rsi
shlq $0x5, %rsi
testq %rdx, %rdx
js 0x33264
movq 0x58(%rdi), %r9
movslq (%r9,%rsi), %r9
cmpq %r9, %r8
jge 0x33261
decq %rdx
addq $-0x20, %rsi
jmp 0x33246
movq %rdx, %rax
xorl %edx, %edx
testq %rax, %rax
cmovleq %rdx, %rax
movq %rax, %rsi
shlq $0x5, %rsi
xorl %r9d, %r9d
cmpq %r11, %rax
jge 0x332f4
movq 0x58(%rdi), %rbx
movslq (%rbx,%rsi), %r14
cmpq %r14, %rcx
jle 0x332f4
cmpl $-0x1, 0xc(%rbx,%rsi)
je 0x332eb
movslq 0x4(%rbx,%rsi), %r10
incq %r10
cmpq %r14, %r8
movq %r14, %r12
cmovgq %r8, %r12
cmpq %r10, %rcx
movq %r10, %r15
cmovlq %rcx, %r15
subq %r12, %r15
jle 0x332eb
addq %r15, %r9
subq %r14, %r10
movswq 0x14(%rbx,%rsi), %rbx
cmpq %r10, %r15
jne 0x332c7
addq %rbx, %rdx
jmp 0x332eb
cvtsi2sd %ebx, %xmm1
cvtsi2sd %r15, %xmm2
mulsd %xmm1, %xmm2
cvtsi2sd %r10, %xmm1
cvtsi2sd %rdx, %xmm3
divsd %xmm1, %xmm2
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %rdx
incq %rax
addq $0x20, %rsi
jmp 0x33277
addq %r9, %r8
subq %r8, %rcx
cvtsi2sd %rcx, %xmm1
cvtsi2sd %rdx, %xmm2
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
popq %rbx
popq %r12
popq %r14
popq %r15
cvttsd2si %xmm1, %rax
retq