// File: /workspace/github_cpp_makefile_star2_O2/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 %r9, -0x10(%rsp)
movq %r8, -0x8(%rsp)
movq %rdx, %r10
movq 0x38(%rsp), %rdx
movq %rcx, %rax
subq %r10, %rax
cvtsi2sd %rax, %xmm1
movq 0x48(%rdi), %r15
testq %rdx, %rdx
je 0x3334d
andq $0x0, (%rdx)
mulsd %xmm0, %xmm1
testq %r15, %r15
je 0x335c2
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
cmpq %r15, %rdx
movq %r15, %rax
cmovgq %rdx, %rax
movq %rdx, %rsi
shlq $0x5, %rsi
orq $0x4, %rsi
cmpq %r15, %rdx
jge 0x333d1
movq 0x58(%rdi), %r8
movslq (%r8,%rsi), %r8
cmpq %r8, %r10
jle 0x333ce
incq %rdx
addq $0x20, %rsi
jmp 0x333b3
movq %rdx, %rax
cmpq %r15, %rax
je 0x335c2
cmpq $-0x1, %rax
movabsq $-0x1, %r9
movq %r9, %rdx
cmovlq %rax, %rdx
movq %rax, %rsi
shlq $0x5, %rsi
testq %rax, %rax
js 0x33414
movq 0x58(%rdi), %r8
movslq (%r8,%rsi), %r8
cmpq %r8, %r10
jge 0x33411
decq %rax
addq $-0x20, %rsi
jmp 0x333f6
movq %rax, %rdx
xorl %esi, %esi
testq %rdx, %rdx
movl $0x0, %eax
movq %rax, -0x20(%rsp)
cmovleq %rsi, %rdx
movq %rdx, %rbx
shlq $0x5, %rbx
movabsq $0x1, %r13
xorl %r11d, %r11d
movq %r9, -0x18(%rsp)
movq %r9, -0x28(%rsp)
cmpq %r15, %rdx
jge 0x33567
movq 0x58(%rdi), %r9
movslq (%r9,%rbx), %r14
cmpq %r14, %rcx
jle 0x33567
movslq 0xc(%r9,%rbx), %rsi
cmpq $-0x1, %rsi
je 0x334a3
movslq 0x4(%r9,%rbx), %rbp
incq %rbp
cmpq %r14, %r10
movq %r14, %rax
cmovgq %r10, %rax
cmpq %rbp, %rcx
movq %rbp, %r12
cmovlq %rcx, %r12
subq %rax, %r12
jle 0x334a3
movq %rbp, %r8
subq %r14, %r8
movswq 0x14(%r9,%rbx), %r9
cmpq %r8, %r12
jne 0x334ab
addq %r9, -0x20(%rsp)
jmp 0x334d7
movq %r11, %r12
jmp 0x33542
cvtsi2sd %r9d, %xmm1
cvtsi2sd %r12, %xmm2
mulsd %xmm1, %xmm2
cvtsi2sd %r8, %xmm1
cvtsi2sdq -0x20(%rsp), %xmm3
divsd %xmm1, %xmm2
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %r8
movq %r8, -0x20(%rsp)
addq %r11, %r12
testw %r9w, %r9w
jg 0x3353f
testq %r13, %r13
je 0x3353f
incq %rsi
subl %r14d, %ebp
movslq %ebp, %r9
movq %rsi, %r8
subq %r9, %r8
movq -0x8(%rsp), %r9
cmpq %r9, %r8
cmovgq %r8, %r9
movq -0x10(%rsp), %r11
cmpq %r11, %rsi
cmovgeq %r11, %rsi
cmpq %r9, %rsi
jle 0x3353f
movq %r9, %r11
subq %r8, %r11
subq %r14, %rax
cmpq %r11, %rax
jne 0x3353f
movq -0x28(%rsp), %rax
cmpq $-0x1, %rax
je 0x33551
xorl %r13d, %r13d
cmpq %r9, %rax
sete %r13b
cmoveq %rsi, %rax
movq %rax, -0x28(%rsp)
jmp 0x33542
xorl %r13d, %r13d
incq %rdx
addq $0x20, %rbx
movq %r12, %r11
jmp 0x33445
movabsq $0x1, %r13
movq %r9, -0x18(%rsp)
movq %rsi, -0x28(%rsp)
jmp 0x33542
subq %r10, %rcx
movq %rcx, %rax
subq %r11, %rax
cvtsi2sd %rax, %xmm1
cvtsi2sdq -0x20(%rsp), %xmm2
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
movq 0x38(%rsp), %rdx
testq %rdx, %rdx
je 0x335c2
testq %r13, %r13
je 0x335c2
cmpq %r11, %rcx
jne 0x335c2
movq -0x18(%rsp), %rsi
cmpq -0x8(%rsp), %rsi
jne 0x335c2
movq -0x28(%rsp), %rdi
movq %rdi, %rax
subq %rsi, %rax
cmpq %rax, %rcx
jne 0x335c2
cmpq -0x10(%rsp), %rdi
jne 0x335c2
movq $0x1, (%rdx)
cvttsd2si %xmm1, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq