// File: /workspace/github_cpp_makefile_star2_O3/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 0x63aa5
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
leaq 0x1(%rsi), %rdx
cmpq %rdx, %r11
cmovgq %r11, %rdx
movq 0x58(%rdi), %rax
movq %rsi, %rdi
shlq $0x5, %rdi
addq %rax, %rdi
addq $0x4, %rdi
movslq (%rdi), %r9
cmpq %r9, %r8
jle 0x639b1
incq %rsi
addq $0x20, %rdi
cmpq %r11, %rsi
jl 0x6399a
movq %rdx, %rsi
cmpq %r11, %rsi
je 0x63aa4
testq %rsi, %rsi
js 0x639e8
movq %rsi, %rdx
shlq $0x5, %rdx
addq %rax, %rdx
movslq (%rdx), %rdi
cmpq %rdi, %r8
jge 0x639e8
leaq -0x1(%rsi), %rdi
addq $-0x20, %rdx
testq %rsi, %rsi
movq %rdi, %rsi
jg 0x639c9
movq $-0x1, %rsi
xorl %edx, %edx
testq %rsi, %rsi
cmovleq %rdx, %rsi
xorpd %xmm2, %xmm2
subq %rsi, %r11
jle 0x63a8e
shlq $0x5, %rsi
addq %rsi, %rax
xorl %edx, %edx
xorl %esi, %esi
movslq (%rax), %r9
cmpq %r9, %rcx
jle 0x63a86
cmpl $-0x1, 0xc(%rax)
je 0x63a7d
movslq 0x4(%rax), %rdi
incq %rdi
cmpq %r9, %r8
movq %r9, %rbx
cmovgq %r8, %rbx
cmpq %rdi, %rcx
movq %rdi, %r10
cmovlq %rcx, %r10
subq %rbx, %r10
jle 0x63a7d
addq %r10, %rdx
subq %r9, %rdi
movswq 0x14(%rax), %r9
cmpq %rdi, %r10
jne 0x63a4c
addq %r9, %rsi
jmp 0x63a7d
xorps %xmm1, %xmm1
cvtsi2sd %r9d, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r10, %xmm2
mulsd %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtsi2sd %rdi, %xmm1
xorps %xmm3, %xmm3
cvtsi2sd %rsi, %xmm3
divsd %xmm1, %xmm2
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %rsi
addq $0x20, %rax
decq %r11
jne 0x63a09
xorps %xmm2, %xmm2
cvtsi2sd %rsi, %xmm2
addq %rdx, %r8
subq %r8, %rcx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
popq %rbx
cvttsd2si %xmm1, %rax
retq