// File: /workspace/github_cpp_makefile_star2_O3/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 0x63ad9
movq $0x0, (%r12)
mulsd %xmm0, %xmm1
testq %r15, %r15
je 0x63d5a
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), %rsi
cmpq %rsi, %r15
cmovgq %r15, %rsi
movq 0x58(%rdi), %rax
movq %rdx, %rdi
shlq $0x5, %rdi
addq %rax, %rdi
addq $0x4, %rdi
movslq (%rdi), %r11
cmpq %r11, %r10
jle 0x63b5e
incq %rdx
addq $0x20, %rdi
cmpq %r15, %rdx
jl 0x63b47
movq %rsi, %rdx
cmpq %r15, %rdx
je 0x63d5a
testq %rdx, %rdx
js 0x63b95
movq %rdx, %rsi
shlq $0x5, %rsi
addq %rax, %rsi
movslq (%rsi), %rdi
cmpq %rdi, %r10
jge 0x63b95
leaq -0x1(%rdx), %rdi
addq $-0x20, %rsi
testq %rdx, %rdx
movq %rdi, %rdx
jg 0x63b76
movq $-0x1, %rdx
xorl %r14d, %r14d
testq %rdx, %rdx
cmovleq %r14, %rdx
subq %rdx, %r15
jle 0x63cdb
movq %r9, -0x8(%rsp)
shlq $0x5, %rdx
addq %rdx, %rax
movl $0x1, %ebx
movq $-0x1, -0x18(%rsp)
xorl %r13d, %r13d
movq $-0x1, -0x10(%rsp)
xorl %r9d, %r9d
movslq (%rax), %rbp
cmpq %rbp, %rcx
jle 0x63cf1
movslq 0xc(%rax), %r12
cmpq $-0x1, %r12
je 0x63c1c
movslq 0x4(%rax), %rdi
incq %rdi
cmpq %rbp, %r10
movq %rbp, %rsi
cmovgq %r10, %rsi
cmpq %rdi, %rcx
movq %rdi, %r14
cmovlq %rcx, %r14
subq %rsi, %r14
jle 0x63c1c
movq %rdi, %r11
subq %rbp, %r11
movswq 0x14(%rax), %rdx
cmpq %r11, %r14
jne 0x63c24
addq %rdx, %r9
jmp 0x63c54
movq %r13, %r14
jmp 0x63cb8
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r14, %xmm2
mulsd %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtsi2sd %r11, %xmm1
xorps %xmm3, %xmm3
cvtsi2sd %r9, %xmm3
divsd %xmm1, %xmm2
addsd %xmm2, %xmm3
cvttsd2si %xmm3, %r9
addq %r13, %r14
testw %dx, %dx
jg 0x63cb6
testq %rbx, %rbx
je 0x63cb6
incq %r12
subl %ebp, %edi
movslq %edi, %rdx
movq %r12, %rdi
subq %rdx, %rdi
cmpq %r8, %rdi
movq %r8, %rdx
cmovgq %rdi, %rdx
movq -0x8(%rsp), %r11
cmpq %r11, %r12
cmovgeq %r11, %r12
cmpq %rdx, %r12
jle 0x63cb6
movq %rdx, %r11
subq %rdi, %r11
subq %rbp, %rsi
cmpq %r11, %rsi
jne 0x63cb6
movq -0x18(%rsp), %rsi
cmpq $-0x1, %rsi
je 0x63cca
xorl %ebx, %ebx
cmpq %rdx, %rsi
sete %bl
cmoveq %r12, %rsi
movq %rsi, -0x18(%rsp)
jmp 0x63cb8
xorl %ebx, %ebx
addq $0x20, %rax
movq %r14, %r13
decq %r15
jne 0x63bd1
jmp 0x63cf4
movl $0x1, %ebx
movq %rdx, -0x10(%rsp)
movq %r12, -0x18(%rsp)
jmp 0x63cb8
movq $-0x1, %rdi
movb $0x1, %al
xorpd %xmm2, %xmm2
movq $-0x1, %rsi
jmp 0x63d16
movq %r13, %r14
xorps %xmm2, %xmm2
cvtsi2sd %r9, %xmm2
testq %rbx, %rbx
setne %al
movq -0x8(%rsp), %r9
movq 0x38(%rsp), %r12
movq -0x18(%rsp), %rsi
movq -0x10(%rsp), %rdi
subq %r10, %rcx
movq %rcx, %rdx
subq %r14, %rdx
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
testq %r12, %r12
je 0x63d5a
testb %al, %al
je 0x63d5a
cmpq %r14, %rcx
jne 0x63d5a
cmpq %r8, %rdi
jne 0x63d5a
movq %rsi, %rax
subq %rdi, %rax
cmpq %rax, %rcx
jne 0x63d5a
cmpq %r9, %rsi
jne 0x63d5a
movq $0x1, (%r12)
cvttsd2si %xmm1, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq