// File: /workspace/github_cpp_makefile_star2_O1/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 %r14
pushq %rbx
movq 0x8(%rdi), %rcx
fldz
testq %rcx, %rcx
setne %r9b
je 0x14404d
fstp %st(0)
movq (%rdi), %r8
movq (%r8), %r10
fldz
cmpq %rdx, %r10
jae 0x14405a
fstp %st(0)
addq $0x28, %r8
leaq -0x1(%rcx), %r9
fldz
xorl %eax, %eax
movq 0xe12af(%rip), %xmm0    # 0x225240
movapd 0xe12b7(%rip), %xmm1    # 0x225250
movsd 0xe154f(%rip), %xmm2    # 0x2254f0
fld %st(0)
fld %st(1)
fld %st(2)
movq -0x8(%r8), %r11
movq %r11, %xmm3
punpckldq %xmm0, %xmm3    # xmm3 = xmm3[0],xmm0[0],xmm3[1],xmm0[1]
subpd %xmm1, %xmm3
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addq -0x10(%r8), %r11
addsd %xmm3, %xmm4
movq %r11, %xmm3
punpckldq %xmm0, %xmm3    # xmm3 = xmm3[0],xmm0[0],xmm3[1],xmm0[1]
subpd %xmm1, %xmm3
movapd %xmm3, %xmm5
unpckhpd %xmm3, %xmm5            # xmm5 = xmm5[1],xmm3[1]
addsd %xmm3, %xmm5
divsd %xmm5, %xmm4
movsd %xmm4, -0x20(%rsp)
fldl -0x20(%rsp)
addq -0x20(%r8), %r10
movq %r10, %xmm3
punpckldq %xmm0, %xmm3    # xmm3 = xmm3[0],xmm0[0],xmm3[1],xmm0[1]
subpd %xmm1, %xmm3
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
mulsd %xmm2, %xmm4
movsd %xmm4, -0x18(%rsp)
fldl -0x18(%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)
cmpq %rax, %r9
je 0x14416b
movq (%r8), %r10
addq $0x28, %r8
incq %rax
cmpq %rdx, %r10
jb 0x143fa7
cmpq %rcx, %rax
setb %r9b
jmp 0x14406a
xorl %ecx, %ecx
fld %st(0)
fld %st(1)
fld %st(2)
jmp 0x14416b
xorl %eax, %eax
fld %st(0)
fld %st(1)
fld %st(2)
fxch %st(3)
fxch %st(1)
fxch %st(2)
fxch %st(1)
testb %r9b, %r9b
je 0x1440be
movq (%rdi), %rdx
leaq (%rax,%rax,4), %rdi
movq (%rdx,%rdi,8), %r8
movq 0x8(%rdx,%rdi,8), %r9
cmpq %rcx, %rax
jae 0x1440c6
leaq (%rdx,%rdi,8), %r10
addq $0x20, %r10
xorl %edi, %edi
xorl %edx, %edx
movq %rax, %r11
addq -0x8(%r10), %rdx
addq (%r10), %rdi
movq -0x20(%r10), %rbx
movq -0x18(%r10), %r14
cmpq %rbx, %r8
cmovaeq %rbx, %r8
cmpq %r14, %r9
cmovbeq %r14, %r9
incq %r11
addq $0x28, %r10
cmpq %rcx, %r11
jb 0x144093
jmp 0x1440ca
movq %rax, %rcx
jmp 0x14416b
xorl %edx, %edx
xorl %edi, %edi
addq %r9, %r8
movq %r8, %xmm0
movq 0xe1166(%rip), %xmm1    # 0x225240
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xe116a(%rip), %xmm2    # 0x225250
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
mulsd 0xe13f2(%rip), %xmm3    # 0x2254f0
movsd %xmm3, -0x28(%rsp)
fldl -0x28(%rsp)
movq %rdi, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
addq %rdi, %rdx
movq %rdx, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm3
movsd %xmm3, -0x30(%rsp)
fldl -0x30(%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
fldz
jbe 0x1441a1
fstp %st(0)
fld %st(0)
fdiv %st(4), %st
flds 0xeaea1(%rip)           # 0x22f020
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
fstp %st(1)
jbe 0x1441a1
fstpt 0xa0(%rsi)
fldz
fstp %st(0)
fldt 0xa0(%rsi)
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 0xeae1f(%rip), %rcx     # 0x22efe8
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 0x1441ec
jnp 0x14422c
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
popq %r14
retq