// File: /workspace/github_cpp_makefile_star2_O1/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 0x63f79
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), %rdx
cmpq %r11, %rax
cmovlq %rax, %rdx
leaq 0x1(%rdx), %rax
cmpq %rax, %r11
cmovgq %r11, %rax
movq 0x58(%rdi), %rsi
movq %rdx, %r9
shlq $0x5, %r9
addq %r9, %rsi
addq $0x4, %rsi
movslq (%rsi), %r9
cmpq %r9, %r8
jle 0x63e83
incq %rdx
addq $0x20, %rsi
cmpq %r11, %rdx
jl 0x63e6c
movq %rax, %rdx
cmpq %r11, %rdx
je 0x63f78
testq %rdx, %rdx
js 0x63ebb
movq %rdx, %rax
shlq $0x5, %rax
addq 0x58(%rdi), %rax
movslq (%rax), %rsi
cmpq %rsi, %r8
jge 0x63ebb
leaq -0x1(%rdx), %rsi
addq $-0x20, %rax
testq %rdx, %rdx
movq %rsi, %rdx
jg 0x63e9c
movq $-0x1, %rdx
xorl %eax, %eax
testq %rdx, %rdx
cmovleq %rax, %rdx
xorpd %xmm2, %xmm2
subq %rdx, %r11
jle 0x63f62
shlq $0x5, %rdx
addq 0x58(%rdi), %rdx
xorl %eax, %eax
xorl %esi, %esi
movslq (%rdx), %r9
cmpq %r9, %rcx
jle 0x63f5a
cmpl $-0x1, 0xc(%rdx)
je 0x63f51
movslq 0x4(%rdx), %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 0x63f51
addq %r10, %rax
subq %r9, %rdi
movswq 0x14(%rdx), %r9
cmpq %rdi, %r10
jne 0x63f20
addq %r9, %rsi
jmp 0x63f51
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, %rdx
decq %r11
jne 0x63edd
xorps %xmm2, %xmm2
cvtsi2sd %rsi, %xmm2
addq %rax, %r8
subq %r8, %rcx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
popq %rbx
cvttsd2si %xmm1, %rax
retq