// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xd0, %rsp
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x54(%rbp)
movl $0x1, -0x54(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x41406(%rip)    # 0x9d9f0
xorps %xmm0, %xmm0
movsd %xmm0, 0x41403(%rip)    # 0x9d9f8
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x5c65e
movq -0x8(%rbp), %rax
movslq -0x60(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
addsd 0x413d3(%rip), %xmm0    # 0x9d9f0
movsd %xmm0, 0x413cb(%rip)    # 0x9d9f0
movq -0x8(%rbp), %rax
movslq -0x60(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq -0x8(%rbp), %rax
movslq -0x60(%rbp), %rcx
mulss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
addsd 0x413ad(%rip), %xmm0    # 0x9d9f8
movsd %xmm0, 0x413a5(%rip)    # 0x9d9f8
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x5c5fc
cvtsi2sdl -0x1c(%rbp), %xmm1
movsd 0x41385(%rip), %xmm0    # 0x9d9f0
divsd %xmm1, %xmm0
movsd %xmm0, 0x41379(%rip)    # 0x9d9f0
movsd 0x41379(%rip), %xmm1    # 0x9d9f8
cvtsi2sdl -0x1c(%rbp), %xmm0
divsd %xmm0, %xmm1
movsd 0x41360(%rip), %xmm0    # 0x9d9f0
movsd 0x41358(%rip), %xmm2    # 0x9d9f0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x41339(%rip)    # 0x9d9f8
movl -0x54(%rbp), %eax
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jl 0x5c90f
movslq -0x58(%rbp), %rax
movq -0x50(%rbp,%rax,8), %rax
movq %rax, -0x68(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x5c719
movq -0x68(%rbp), %rax
movslq -0x60(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x5c6eb
movsd -0x70(%rbp), %xmm0
cvtsi2sdl -0x1c(%rbp), %xmm1
divsd %xmm1, %xmm0
movslq -0x58(%rbp), %rcx
leaq 0x412ce(%rip), %rax     # 0x9da00
movsd %xmm0, (%rax,%rcx,8)
xorps %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x5c781
movq -0x68(%rbp), %rax
movslq -0x60(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq -0x8(%rbp), %rax
movslq -0x60(%rbp), %rcx
mulss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x5c746
movsd -0x70(%rbp), %xmm1
cvtsi2sdl -0x1c(%rbp), %xmm0
divsd %xmm0, %xmm1
movslq -0x58(%rbp), %rcx
leaq 0x41266(%rip), %rax     # 0x9da00
movsd (%rax,%rcx,8), %xmm0
movsd 0x41249(%rip), %xmm2    # 0x9d9f0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movslq -0x58(%rbp), %rcx
leaq 0x4123f(%rip), %rax     # 0x9da10
movsd %xmm0, (%rax,%rcx,8)
movl $0x1, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x58(%rbp), %eax
jl 0x5c8ff
movslq -0x5c(%rbp), %rax
movq -0x50(%rbp,%rax,8), %rax
movq %rax, -0x78(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp)
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x5c840
movq -0x68(%rbp), %rax
movslq -0x60(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq -0x78(%rbp), %rax
movslq -0x60(%rbp), %rcx
mulss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0x70(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x5c805
movsd -0x70(%rbp), %xmm1
cvtsi2sdl -0x1c(%rbp), %xmm0
divsd %xmm0, %xmm1
movss -0x34(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movslq -0x58(%rbp), %rcx
leaq 0x4119e(%rip), %rax     # 0x9da00
mulsd (%rax,%rcx,8), %xmm0
movslq -0x5c(%rbp), %rcx
leaq 0x4118e(%rip), %rax     # 0x9da00
movsd (%rax,%rcx,8), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movslq -0x58(%rbp), %rcx
leaq 0x4117f(%rip), %rax     # 0x9da20
shlq $0x4, %rcx
addq %rcx, %rax
movslq -0x5c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x5c(%rbp), %eax
cmpl -0x58(%rbp), %eax
jle 0x5c8ef
movslq -0x58(%rbp), %rcx
leaq 0x4115c(%rip), %rax     # 0x9da20
shlq $0x4, %rcx
addq %rcx, %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movslq -0x5c(%rbp), %rcx
leaq 0x41141(%rip), %rax     # 0x9da20
shlq $0x4, %rcx
addq %rcx, %rax
movslq -0x58(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x5c8f1
movl -0x5c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x5c7dd
jmp 0x5c901
movl -0x58(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x5c6c5
movsd 0x41109(%rip), %xmm0    # 0x9da20
movsd 0x41119(%rip), %xmm2    # 0x9da38
movsd 0x41101(%rip), %xmm1    # 0x9da28
mulsd 0x41101(%rip), %xmm1    # 0x9da30
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd 0x410da(%rip), %xmm0    # 0x9da38
divsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd 0x410aa(%rip), %xmm0    # 0x9da20
divsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd 0x410a2(%rip), %xmm0    # 0x9da30
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
divsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd 0x4106b(%rip), %xmm0    # 0x9da28
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
divsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0x90(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xc8(%rbp)
movsd 0x40ff9(%rip), %xmm0    # 0x9d9f0
cvtsd2ss %xmm0, %xmm0
movq 0x10(%rbp), %rax
movss %xmm0, (%rax)
movl $0x0, -0x58(%rbp)
cmpl $0x2, -0x58(%rbp)
jge 0x5cb18
movslq -0x58(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, -0xc0(%rbp,%rax,8)
movl $0x0, -0x5c(%rbp)
cmpl $0x2, -0x5c(%rbp)
jge 0x5ca85
movslq -0x58(%rbp), %rcx
leaq -0xa0(%rbp), %rax
shlq $0x4, %rcx
addq %rcx, %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movslq -0x5c(%rbp), %rcx
leaq 0x40fb9(%rip), %rax     # 0x9da10
movsd (%rax,%rcx,8), %xmm2
movslq -0x58(%rbp), %rax
movsd -0xc0(%rbp,%rax,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xc0(%rbp,%rax,8)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x5ca2b
movslq -0x58(%rbp), %rax
movsd -0xc0(%rbp,%rax,8), %xmm0
movslq -0x58(%rbp), %rcx
leaq 0x40f73(%rip), %rax     # 0x9da10
movsd (%rax,%rcx,8), %xmm2
movsd -0xc8(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xc8(%rbp)
movslq -0x58(%rbp), %rcx
leaq 0x40f3b(%rip), %rax     # 0x9da00
movsd (%rax,%rcx,8), %xmm0
movslq -0x58(%rbp), %rax
movsd -0xc0(%rbp,%rax,8), %xmm2
movq 0x10(%rbp), %rax
movss (%rax), %xmm1
cvtss2sd %xmm1, %xmm1
movq %xmm0, %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
xorq %rdx, %rcx
movq %rcx, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rax)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x5ca0a
movsd -0xc0(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x28(%rbp), %rax
movss %xmm0, (%rax)
movsd -0xb8(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x30(%rbp), %rax
movss %xmm0, (%rax)
movsd -0xc8(%rbp), %xmm0
divsd 0x40ea8(%rip), %xmm0    # 0x9d9f8
callq 0x1220
cvtsd2ss %xmm0, %xmm0
addq $0xd0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)