// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/Fit.c
// Function: Fit_2
float Fit_2(float *Y, float *X0, float *X1, int N,
	    float *A0, float *A1, float *bias, int first)
{
  float mu1=0;
  // 0: do not allow offset 1: do not penalize offset 0.5: penalize

  // Fit Y=XA with X=(2*n) setting bias b=0
  // and evaluating it as b=<Y>-<X>A
  // A=<X^T X>^{-1}<X^T Y>
  float *x[2]; x[0]=X0; x[1]=X1;
  int amax=0, a, b, i;
  if(1){ // Compute also for variable X1, which is the same at all calls
    amax=1;
    Y1=0; Y2=0; for(i=0; i<N; i++){Y1+=Y[i]; Y2+=Y[i]*Y[i];}
    Y1/=N; Y2=(Y2/N-Y1*Y1); 
  }


  for(a=amax; a>=0; a--){
    float *xa=x[a];
    double s=0; for(i=0; i<N; i++){s+=xa[i];} X_ave[a]=s/N;
    //s=0; for(i=0; i<N; i++){s+=xa[i]*xa[i];}
    //X_dev[a]=sqrt(s/N-X_ave[a]*X_ave[a]);
    s=0; for(i=0; i<N; i++){s+=xa[i]*Y[i];}
    corr_y[a]=s/N-X_ave[a]*Y1;
    for(b=1; b>=a; b--){
      float *xb=x[b];
      s=0; for(i=0; i<N; i++){s+=xa[i]*xb[i];}
      Corr[a][b]=s/N-mu1*X_ave[a]*X_ave[b];
      if(b>a)Corr[b][a]=Corr[a][b];
    }
  }

  double Corr_inv[2][2];
  double det=Corr[0][0]*Corr[1][1]-Corr[0][1]*Corr[1][0];
  Corr_inv[0][0]=Corr[1][1]/det;
  Corr_inv[1][1]=Corr[0][0]/det;
  Corr_inv[0][1]=-Corr[1][0]/det;
  Corr_inv[1][0]=-Corr[0][1]/det;

  double A[2], r2=0; *bias=Y1;
  for(a=0; a<2; a++){
    A[a]=0; for(b=0; b<2; b++)A[a]+=Corr_inv[a][b]*corr_y[b];
    r2+=A[a]*corr_y[a];
    (*bias)-=X_ave[a]*A[a];
  }
  *A0=A[0];
  *A1=A[1];

  return(sqrt(r2/Y2));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, 0x18(%rsp)
movq %rsi, (%rsp)
movq %rdx, 0x8(%rsp)
xorl %edx, %edx
testl %ecx, %ecx
movl $0x0, %esi
cmovgl %ecx, %esi
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
cmpq %rdx, %rsi
je 0x3b9af
movss (%rdi,%rdx,4), %xmm0
cvtss2sd %xmm0, %xmm3
addsd %xmm3, %xmm2
movsd %xmm2, 0x3a09a(%rip)    # 0x75a30
mulss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
addsd %xmm0, %xmm1
movsd %xmm1, 0x3a08e(%rip)    # 0x75a38
incq %rdx
jmp 0x3b97c
cvtsi2sd %ecx, %xmm0
divsd %xmm0, %xmm2
movsd %xmm2, 0x3a071(%rip)    # 0x75a30
divsd %xmm0, %xmm1
movapd %xmm2, %xmm3
mulsd %xmm2, %xmm3
subsd %xmm3, %xmm1
movsd %xmm1, 0x3a061(%rip)    # 0x75a38
pushq $0x1
popq %r9
leaq 0x3a05e(%rip), %rcx     # 0x75a40
leaq 0x3a067(%rip), %rdx     # 0x75a50
movsd 0x1b247(%rip), %xmm3    # 0x56c38
leaq 0x3a068(%rip), %r11     # 0x75a60
movl %r9d, %ebx
testl %ebx, %ebx
js 0x3badd
movl %ebx, %r14d
movq (%rsp,%r14,8), %r15
xorpd %xmm4, %xmm4
xorl %eax, %eax
cmpq %rax, %rsi
je 0x3ba24
cvtss2sd (%r15,%rax,4), %xmm5
addsd %xmm5, %xmm4
incq %rax
jmp 0x3ba10
divsd %xmm0, %xmm4
movsd %xmm4, (%rcx,%r14,8)
xorpd %xmm5, %xmm5
xorl %eax, %eax
cmpq %rax, %rsi
je 0x3ba51
movss (%r15,%rax,4), %xmm6
mulss (%rdi,%rax,4), %xmm6
cvtss2sd %xmm6, %xmm6
addsd %xmm6, %xmm5
incq %rax
jmp 0x3ba34
divsd %xmm0, %xmm5
movapd %xmm4, %xmm6
mulsd %xmm2, %xmm6
subsd %xmm6, %xmm5
movsd %xmm5, (%rdx,%r14,8)
mulsd %xmm3, %xmm4
movq %r14, %r12
shlq $0x4, %r12
addq %r11, %r12
movl %r9d, %ebp
cmpl %ebx, %ebp
jl 0x3bad6
movl %ebp, %r13d
movq (%rsp,%r13,8), %rax
xorpd %xmm5, %xmm5
xorl %r10d, %r10d
cmpq %r10, %rsi
je 0x3baa8
movss (%r15,%r10,4), %xmm6
mulss (%rax,%r10,4), %xmm6
cvtss2sd %xmm6, %xmm6
addsd %xmm6, %xmm5
incq %r10
jmp 0x3ba8a
divsd %xmm0, %xmm5
movsd (%rcx,%r13,8), %xmm6
mulsd %xmm4, %xmm6
addsd %xmm5, %xmm6
movsd %xmm6, (%r12,%r13,8)
cmpl %ebx, %ebp
jle 0x3bad2
shlq $0x4, %r13
addq %r11, %r13
movsd %xmm6, (%r13,%r14,8)
decl %ebp
jmp 0x3ba78
decl %ebx
jmp 0x3b9fb
movsd 0x39f7b(%rip), %xmm0    # 0x75a60
movsd 0x39f8b(%rip), %xmm3    # 0x75a78
movsd 0x39f73(%rip), %xmm4    # 0x75a68
movsd 0x39f73(%rip), %xmm5    # 0x75a70
movapd 0xf5bb(%rip), %xmm6     # 0x4b0c0
movapd %xmm5, %xmm7
mulsd %xmm4, %xmm7
xorpd %xmm6, %xmm4
unpcklpd %xmm0, %xmm4            # xmm4 = xmm4[0],xmm0[0]
mulsd %xmm3, %xmm0
subsd %xmm7, %xmm0
xorpd %xmm6, %xmm5
unpcklpd %xmm5, %xmm3            # xmm3 = xmm3[0],xmm5[0]
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
divpd %xmm0, %xmm3
leaq 0x30(%rsp), %rsi
movapd %xmm3, (%rsi)
divpd %xmm0, %xmm4
movapd %xmm4, 0x10(%rsi)
xorpd %xmm0, %xmm0
xorl %edi, %edi
cvtsd2ss %xmm2, %xmm2
cmpq $0x2, %rdi
je 0x3bb98
xorpd %xmm3, %xmm3
xorl %eax, %eax
cmpq $0x10, %rax
je 0x3bb6f
movsd (%rsi,%rax), %xmm4
mulsd (%rax,%rdx), %xmm4
addsd %xmm4, %xmm3
addq $0x8, %rax
jmp 0x3bb55
movsd %xmm3, 0x20(%rsp,%rdi,8)
movsd (%rdx,%rdi,8), %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm0
mulsd (%rcx,%rdi,8), %xmm3
cvtss2sd %xmm2, %xmm2
subsd %xmm3, %xmm2
incq %rdi
addq $0x10, %rsi
jmp 0x3bb45
movq 0x90(%rsp), %rax
movss %xmm2, (%rax)
cvtsd2ss 0x20(%rsp), %xmm2
movss %xmm2, (%r8)
cvtsd2ss 0x28(%rsp), %xmm2
movq 0x18(%rsp), %rax
movss %xmm2, (%rax)
divsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x3bbd2
sqrtsd %xmm0, %xmm0
jmp 0x3bbd7
callq 0x2290
cvtsd2ss %xmm0, %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq