// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: LeastSquare_advance(trans_idx*, ha_ug_index*, unsigned long)
void LeastSquare_advance(trans_idx* dis, ha_ug_index* idx, uint64_t med)
{
    #define SCAL_RATE 1000
    long double t1=0, t2=0, t3=0, t4=0, x, y;
    uint64_t i, m, ava_size;

    for (i = m = 0; i < dis->n; i++)
    {
        x = ((double)(dis->a[i].beg + dis->a[i].end))/2;
        y = ((double)(dis->a[i].cnt_1))/((double)(dis->a[i].cnt_0 + dis->a[i].cnt_1));
        if(dis->a[i].beg >= med) break;

        t1 += x*x;
        t2 += x;
        t3 += x*y;
        t4 += y;
        m++;
    }

    if(i < dis->n)
    {
        uint64_t beg, end, cnt_0, cnt_1;
        for (beg = dis->a[i].beg, end = dis->a[i].end, cnt_0 = cnt_1 = 0; i < dis->n; i++)
        {
            cnt_0 += dis->a[i].cnt_0;
            cnt_1 += dis->a[i].cnt_1;
            beg = MIN(beg, dis->a[i].beg);
            end = MAX(end, dis->a[i].end);
        }

        x = ((double)(beg + end))/2;
        y = ((double)(cnt_1))/((double)(cnt_0 + cnt_1));

        t1 += x*x;
        t2 += x;
        t3 += x*y;
        t4 += y;
        m++;
    }



    if(t2 > t4)
    {
        idx->frac = t2/t4;
        if(idx->frac > SCAL_RATE) idx->frac = idx->frac / SCAL_RATE;
        if(idx->frac < 1) idx->frac = 1;
    }

    ava_size = m;
    t1 /= (idx->frac*idx->frac);
    t2 /= idx->frac;
    t3 /= idx->frac;
    if((t1*ava_size - t2*t2) != 0)
    {
        idx->a = (t3*ava_size - t2*t4) / (t1*ava_size - t2*t2);
    }
    if((t1*ava_size - t2*t2) != 0)
    {
        idx->b = (t1*t4 - t2*t3) / (t1*ava_size - t2*t2);
    }
}

// Assembly:
pushq %rbx
movq 0x8(%rdi), %rcx
fldz
movabsq $0x20, %r8
xorl %eax, %eax
movq 0xe08db(%rip), %xmm1    # 0x1e3270
movapd 0xe08e3(%rip), %xmm0    # 0x1e3280
movsd 0xe0ba3(%rip), %xmm2    # 0x1e3548
fld %st(0)
fld %st(1)
fld %st(2)
movq %rcx, %r10
subq %rax, %r10
je 0x102b1e
movq (%rdi), %rbx
movq -0x20(%rbx,%r8), %r9
cmpq %rdx, %r9
jae 0x102a57
movq (%rbx,%r8), %r10
movq %r10, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm0, %xmm3
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
addq -0x8(%rbx,%r8), %r10
movq %r10, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm0, %xmm3
movapd %xmm3, %xmm5
unpckhpd %xmm3, %xmm5            # xmm5 = xmm5[1],xmm3[1]
addsd %xmm3, %xmm5
divsd %xmm5, %xmm4
movsd %xmm4, -0x30(%rsp)
fldl -0x30(%rsp)
addq -0x18(%rbx,%r8), %r9
movq %r9, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm0, %xmm3
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
mulsd %xmm2, %xmm4
movsd %xmm4, -0x28(%rsp)
fldl -0x28(%rsp)
fld %st(0)
fmul %st(1), %st
faddp %st, %st(3)
fadd %st, %st(3)
fmul %st(1), %st
faddp %st, %st(4)
faddp %st, %st(4)
incq %rax
addq $0x28, %r8
jmp 0x1029ab
leaq (%rbx,%r8), %r11
movq -0x18(%rbx,%r8), %rdi
xorl %ecx, %ecx
xorl %edx, %edx
subq $0x1, %r10
jb 0x102a8d
addq -0x8(%r11), %rcx
addq (%r11), %rdx
movq -0x20(%r11), %r8
movq -0x18(%r11), %rbx
cmpq %r8, %r9
cmovaeq %r8, %r9
cmpq %rbx, %rdi
cmovbeq %rbx, %rdi
addq $0x28, %r11
jmp 0x102a64
addq %r9, %rdi
movq %rdi, %xmm2
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
subpd %xmm0, %xmm2
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addsd %xmm2, %xmm3
mulsd 0xe0a97(%rip), %xmm3    # 0x1e3548
movsd %xmm3, -0x18(%rsp)
fldl -0x18(%rsp)
movq %rdx, %xmm2
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
subpd %xmm0, %xmm2
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addsd %xmm2, %xmm3
addq %rdx, %rcx
movq %rcx, %xmm2
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
subpd %xmm0, %xmm2
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
addsd %xmm2, %xmm0
divsd %xmm0, %xmm3
movsd %xmm3, -0x20(%rsp)
fldl -0x20(%rsp)
fld %st(1)
fmul %st(2), %st
faddp %st, %st(3)
fxch %st(3)
fadd %st(1), %st
fxch %st(1)
fmul %st(3), %st
faddp %st, %st(4)
fxch %st(4)
faddp %st, %st(2)
incq %rax
movq %rax, %rcx
fxch %st(1)
fxch %st(3)
fxch %st(1)
fxch %st(1)
fucomi %st(3), %st
jbe 0x102b52
fld %st(0)
fdiv %st(4), %st
flds 0xea632(%rip)           # 0x1ed160
fxch %st(1)
fucomi %st(1), %st
fdiv %st, %st(1)
fcmovnbe %st(1), %st
fstp %st(1)
fld %st(0)
fstpt 0xa0(%rsi)
fld1
fucomi %st(1), %st
jbe 0x102b5a
fstp %st(1)
fld %st(0)
fstpt 0xa0(%rsi)
jmp 0x102b58
fldt 0xa0(%rsi)
fldz
fstp %st(0)
fld %st(0)
fmul %st(1), %st
fdivrp %st, %st(3)
fdivr %st, %st(1)
movq %rcx, -0x38(%rsp)
xorl %eax, %eax
testq %rcx, %rcx
sets %al
fildll -0x38(%rsp)
leaq 0xea5ac(%rip), %rcx     # 0x1ed128
fadds (%rcx,%rax,4)
fld %st(2)
fchs
fxch %st(3)
fmul %st, %st(0)
fld %st(4)
fstpt -0xc(%rsp)
fxch %st(4)
fmul %st(1), %st
fsubp %st, %st(4)
fldz
fxch %st(4)
fucomi %st(4), %st
fstp %st(4)
jne 0x102b9f
jnp 0x102bdf
fxch %st(4)
fdivp %st, %st(1)
fld %st(4)
fmul %st(2), %st
fxch %st(4)
fmul %st(1), %st
faddp %st, %st(4)
fxch %st(3)
fdiv %st(2), %st
fstpt 0x80(%rsi)
fmulp %st, %st(2)
fldt -0xc(%rsp)
fmulp %st, %st(3)
fxch %st(2)
faddp %st, %st(1)
fdivp %st, %st(1)
fstpt 0x90(%rsi)
fldz
fldz
fldz
fldz
fldz
fldz
fxch %st(1)
fxch %st(3)
fxch %st(2)
fxch %st(1)
fstp %st(5)
fstp %st(3)
fstp %st(0)
fstp %st(1)
fstp %st(0)
fstp %st(0)
popq %rbx
retq