// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: cal_estimate_err_hc(overlap_region*, long, long, long, long, long, double, long*)
int64_t cal_estimate_err_hc(overlap_region *z, int64_t wl, int64_t qs, int64_t qe, int64_t ts, int64_t te, double e_rate, int64_t *exact)
{
    int64_t k, ws, we, wid, os, oe, ovlp, tot, cov_l, est = (qe-qs)*e_rate, wn = z->w_list.n, exa = 1, ots, ote, q[2], t[2]; 
    if(exact) (*exact) = 0;
    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, ots = ote = -1; 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 == (we-ws)) {
            tot += z->w_list.a[k].error;
        } else {
            tot += ((double)z->w_list.a[k].error)*((double)ovlp)/((double)(we-ws));
        }
        if(z->w_list.a[k].error > 0) exa = 0;
        if(exa) {
            q[0] = os - ws; q[1] = we - oe; 
            we = z->w_list.a[k].y_end+1; ws = we - (z->w_list.a[k].x_end+1-z->w_list.a[k].x_start);
            os = MAX(ts, ws); oe = MIN(te, we);
	        ovlp = ((oe>os)? (oe-os):0);
            t[0] = os - ws; t[1] = we - oe; 
            if((ovlp) && (q[0] == t[0]) && (t[0] == t[0])) {
                if(ote == -1) {
                    ots = os; ote = oe; 
                } else if(ote == os) {
                    ote = oe; 
                } else {
                    exa = 0;
                }
            } else {
                exa = 0;
            }
        }
    }
    tot += ((qe-qs)-cov_l)*e_rate; 
    if((exact) && exa && (((qe-qs) == cov_l))) {
        if(((qe-qs) == (ote - ots)) && (ots == ts) && (ote == te)) (*exact) = 1;
    }
    return tot;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, %r10
movq 0x38(%rsp), %r12
movq %rcx, %rax
subq %rdx, %rax
cvtsi2sd %rax, %xmm1
movq 0x48(%rdi), %r15
testq %r12, %r12
je 0x63fad
movq $0x0, (%r12)
mulsd %xmm0, %xmm1
testq %r15, %r15
je 0x6423a
movl 0x8(%rdi), %eax
movl 0x4(%rdi), %r11d
cmpq %r10, %r11
cmovgq %r11, %r10
incl %eax
cmpq %rcx, %rax
cmovlq %rax, %rcx
movq %r10, %rax
cqto
idivq %rsi
movq %rdx, %rbx
movq %r11, %rax
xorl %edx, %edx
idivq %rsi
addq %r11, %rbx
subq %rbx, %rdx
leaq (%rdx,%r10), %rax
cqto
idivq %rsi
leaq -0x1(%r15), %rdx
cmpq %r15, %rax
cmovlq %rax, %rdx
leaq 0x1(%rdx), %rax
cmpq %rax, %r15
cmovgq %r15, %rax
movq 0x58(%rdi), %rsi
movq %rdx, %r11
shlq $0x5, %r11
addq %r11, %rsi
addq $0x4, %rsi
movslq (%rsi), %r11
cmpq %r11, %r10
jle 0x64032
incq %rdx
addq $0x20, %rsi
cmpq %r15, %rdx
jl 0x6401b
movq %rax, %rdx
cmpq %r15, %rdx
je 0x6423a
testq %rdx, %rdx
js 0x6406a
movq %rdx, %rax
shlq $0x5, %rax
addq 0x58(%rdi), %rax
movslq (%rax), %rsi
cmpq %rsi, %r10
jge 0x6406a
leaq -0x1(%rdx), %rsi
addq $-0x20, %rax
testq %rdx, %rdx
movq %rsi, %rdx
jg 0x6404b
movq $-0x1, %rdx
xorl %r14d, %r14d
testq %rdx, %rdx
cmovleq %r14, %rdx
subq %rdx, %r15
jle 0x641bb
movq %r9, -0x8(%rsp)
movq 0x58(%rdi), %rax
shlq $0x5, %rdx
leaq (%rdx,%rax), %rdi
addq $0x14, %rdi
movl $0x1, %ebx
movq $-0x1, -0x18(%rsp)
xorl %r13d, %r13d
movq $-0x1, -0x10(%rsp)
xorl %r9d, %r9d
movslq -0x14(%rdi), %rbp
cmpq %rbp, %rcx
jle 0x641d1
movslq -0x8(%rdi), %r12
cmpq $-0x1, %r12
je 0x640fa
movslq -0x10(%rdi), %rsi
incq %rsi
cmpq %rbp, %r10
movq %rbp, %rdx
cmovgq %r10, %rdx
cmpq %rsi, %rcx
movq %rsi, %r14
cmovlq %rcx, %r14
subq %rdx, %r14
jle 0x640fa
movq %rsi, %rax
subq %rbp, %rax
movswq (%rdi), %r11
cmpq %rax, %r14
jne 0x64102
addq %r11, %r9
jmp 0x64133
movq %r13, %r14
jmp 0x64198
xorps %xmm1, %xmm1
cvtsi2sd %r11d, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r14, %xmm2
mulsd %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
xorps %xmm3, %xmm3
cvtsi2sd %r9, %xmm3
divsd %xmm1, %xmm2
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %r9
addq %r13, %r14
cmpw $0x0, (%rdi)
jg 0x64196
testq %rbx, %rbx
je 0x64196
incq %r12
subl %ebp, %esi
movslq %esi, %rsi
movq %r12, %rax
subq %rsi, %rax
cmpq %r8, %rax
movq %r8, %rsi
cmovgq %rax, %rsi
movq -0x8(%rsp), %r11
cmpq %r11, %r12
cmovgeq %r11, %r12
cmpq %rsi, %r12
jle 0x64196
movq %rsi, %r11
subq %rax, %r11
subq %rbp, %rdx
cmpq %r11, %rdx
jne 0x64196
movq -0x18(%rsp), %rax
cmpq $-0x1, %rax
je 0x641aa
xorl %ebx, %ebx
cmpq %rsi, %rax
sete %bl
cmoveq %r12, %rax
movq %rax, -0x18(%rsp)
jmp 0x64198
xorl %ebx, %ebx
addq $0x20, %rdi
movq %r14, %r13
decq %r15
jne 0x640af
jmp 0x641d4
movl $0x1, %ebx
movq %rsi, -0x10(%rsp)
movq %r12, -0x18(%rsp)
jmp 0x64198
movq $-0x1, %rdi
movb $0x1, %dl
xorpd %xmm2, %xmm2
movq $-0x1, %rsi
jmp 0x641f6
movq %r13, %r14
xorps %xmm2, %xmm2
cvtsi2sd %r9, %xmm2
testq %rbx, %rbx
setne %dl
movq -0x8(%rsp), %r9
movq 0x38(%rsp), %r12
movq -0x18(%rsp), %rsi
movq -0x10(%rsp), %rdi
subq %r10, %rcx
movq %rcx, %rax
subq %r14, %rax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
testq %r12, %r12
je 0x6423a
testb %dl, %dl
je 0x6423a
cmpq %r14, %rcx
jne 0x6423a
cmpq %r8, %rdi
jne 0x6423a
movq %rsi, %rax
subq %rdi, %rax
cmpq %rax, %rcx
jne 0x6423a
cmpq %r9, %rsi
jne 0x6423a
movq $0x1, (%r12)
cvttsd2si %xmm1, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq