// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/nma.c
// Function: Bivariate_fit
int Bivariate_fit(int n, float *z, float *x, float *y, int *w,
		  float *a, float *b, float *c, float *r, float *chi)
{
  // fit: z_i = a*x_i + b*y_i + c
  // Returns a, b, c, correlation coefficient and normalized sum of residuals

  int i, norm=0;
  float xy_ave=0, xz_ave=0, yz_ave=0;
  float x2_ave=0, y2_ave=0, z2_ave=0;
  float x_ave=0, y_ave=0, z_ave=0, chi1;

  if(n<3){
    printf("Too few points (%d), the fit can not be performed!\n\n", n);
    return(0);
  }

  for(i=0; i<n; i++){
    if(w[i]){
      x_ave+=x[i]; y_ave+=y[i]; z_ave+=z[i]; norm++;
      x2_ave+=x[i]*x[i]; y2_ave+=y[i]*y[i]; z2_ave+=z[i]*z[i];
      xy_ave+=x[i]*y[i]; xz_ave+=x[i]*z[i]; yz_ave+=y[i]*z[i];
    }
  }

  xy_ave=(float)norm*xy_ave-(x_ave*y_ave);
  xz_ave=(float)norm*xz_ave-(x_ave*z_ave);
  yz_ave=(float)norm*yz_ave-(y_ave*z_ave);
  x2_ave=(float)norm*x2_ave-(x_ave*x_ave);
  y2_ave=(float)norm*y2_ave-(y_ave*y_ave);
  z2_ave=(float)norm*z2_ave-(z_ave*z_ave);

  *a=(y2_ave*xz_ave-xy_ave*yz_ave)/(x2_ave*y2_ave-xy_ave*xy_ave);
  *b=(x2_ave*yz_ave-xy_ave*xz_ave)/(x2_ave*y2_ave-xy_ave*xy_ave);
  *c=z_ave-(*a)*x_ave-(*b)*y_ave; *c/=(float)norm;
  *r=((*a)*xz_ave+(*b)*yz_ave)/
    sqrt(z2_ave*((*a)*(*a)*x2_ave+(*b)*(*b)*y2_ave+ 2*(*a)*(*b)*xy_ave));
  (*chi)=0;
  for(i=0; i<n; i++){
    chi1= z[i]-(*a)*x[i]-(*b)*y[i]-(*c);
    (*chi) += chi1*chi1;
  }
  (*chi)/=z2_ave;
  return(norm);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r12
movl %edi, %esi
cmpl $0x3, %edi
jge 0x27078
leaq 0x31de4(%rip), %rdi     # 0x58e4e
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x20f0
jmp 0x272db
movq %r9, %rbx
movq %rcx, %r14
movq %rdx, %r15
movq 0x78(%rsp), %rcx
movq 0x70(%rsp), %r9
movq 0x68(%rsp), %rdx
movq 0x60(%rsp), %rdi
movl %esi, %r13d
xorps %xmm3, %xmm3
xorl %eax, %eax
xorps %xmm1, %xmm1
xorl %ebp, %ebp
xorps %xmm2, %xmm2
xorps %xmm0, %xmm0
xorps %xmm12, %xmm12
cmpl $0x0, (%r8,%rax,4)
je 0x270fe
movss (%r15,%rax,4), %xmm4
movss (%r14,%rax,4), %xmm5
movaps %xmm5, %xmm6
unpcklps %xmm4, %xmm6            # xmm6 = xmm6[0],xmm4[0],xmm6[1],xmm4[1]
addps %xmm6, %xmm2
movss (%r12,%rax,4), %xmm7
addss %xmm7, %xmm1
incl %ebp
mulps %xmm6, %xmm6
addps %xmm6, %xmm3
movaps %xmm7, %xmm6
shufps $0x0, %xmm7, %xmm6      # xmm6 = xmm6[0,0],xmm7[0,0]
movaps %xmm4, %xmm8
unpcklps %xmm7, %xmm8            # xmm8 = xmm8[0],xmm7[0],xmm8[1],xmm7[1]
mulps %xmm6, %xmm8
addps %xmm8, %xmm12
unpcklps %xmm4, %xmm7            # xmm7 = xmm7[0],xmm4[0],xmm7[1],xmm4[1]
shufps $0x0, %xmm5, %xmm5      # xmm5 = xmm5[0,0,0,0]
mulps %xmm7, %xmm5
addps %xmm5, %xmm0
incq %rax
cmpq %rax, %r13
jne 0x270ac
xorps %xmm6, %xmm6
cvtsi2ss %ebp, %xmm6
movaps %xmm2, %xmm7
shufps $0x55, %xmm2, %xmm7     # xmm7 = xmm7[1,1],xmm2[1,1]
movaps 0x29f15(%rip), %xmm5    # 0x51030
xorps %xmm7, %xmm5
mulss %xmm1, %xmm5
movaps %xmm6, %xmm4
mulss %xmm12, %xmm4
addss %xmm5, %xmm4
movaps %xmm2, %xmm8
shufps $0x0, %xmm2, %xmm8      # xmm8 = xmm8[0,0],xmm2[0,0]
movss %xmm1, %xmm8            # xmm8 = xmm1[0],xmm8[1,2,3]
mulps %xmm2, %xmm8
movaps %xmm6, %xmm5
shufps $0x0, %xmm6, %xmm5      # xmm5 = xmm5[0,0],xmm6[0,0]
mulps %xmm5, %xmm0
subps %xmm8, %xmm0
movaps %xmm2, %xmm8
mulps %xmm2, %xmm8
mulps %xmm3, %xmm5
subps %xmm8, %xmm5
movaps %xmm0, %xmm3
shufps $0x55, %xmm0, %xmm3     # xmm3 = xmm3[1,1],xmm0[1,1]
movaps %xmm0, %xmm8
mulps %xmm3, %xmm8
movaps %xmm5, %xmm9
shufps $0x0, %xmm5, %xmm9      # xmm9 = xmm9[0,0],xmm5[0,0]
movss %xmm4, %xmm9            # xmm9 = xmm4[0],xmm9[1,2,3]
mulps %xmm5, %xmm9
subps %xmm8, %xmm9
movaps %xmm9, %xmm10
shufps $0x55, %xmm9, %xmm10    # xmm10 = xmm10[1,1],xmm9[1,1]
divss %xmm10, %xmm9
movss %xmm9, (%rbx)
movaps %xmm4, %xmm9
mulss %xmm3, %xmm9
movaps %xmm5, %xmm8
shufps $0x55, %xmm5, %xmm8     # xmm8 = xmm8[1,1],xmm5[1,1]
movaps %xmm8, %xmm11
mulss %xmm0, %xmm11
subss %xmm9, %xmm11
movaps %xmm1, %xmm9
mulss %xmm1, %xmm9
shufps $0x55, %xmm12, %xmm12   # xmm12 = xmm12[1,1,1,1]
mulss %xmm6, %xmm12
divss %xmm10, %xmm11
movss %xmm11, (%rdi)
mulss (%rbx), %xmm7
subss %xmm9, %xmm12
subss %xmm7, %xmm1
mulss %xmm2, %xmm11
subss %xmm11, %xmm1
divss %xmm6, %xmm1
movss %xmm1, (%rdx)
movss (%rbx), %xmm1
movss (%rdi), %xmm2
mulss %xmm2, %xmm0
mulss %xmm1, %xmm4
addss %xmm0, %xmm4
cvtss2sd %xmm4, %xmm4
movaps %xmm1, %xmm0
mulss %xmm1, %xmm0
addss %xmm1, %xmm1
mulss %xmm2, %xmm1
mulss %xmm2, %xmm2
mulss %xmm5, %xmm2
mulss %xmm8, %xmm0
addss %xmm2, %xmm0
mulss %xmm3, %xmm1
addss %xmm0, %xmm1
mulss %xmm12, %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2724c
sqrtsd %xmm0, %xmm0
jmp 0x2727d
movaps %xmm12, 0x10(%rsp)
movsd %xmm4, 0x8(%rsp)
callq 0x22a0
movsd 0x8(%rsp), %xmm4
movq 0x70(%rsp), %r9
movaps 0x10(%rsp), %xmm12
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rdx
movq 0x78(%rsp), %rcx
divsd %xmm0, %xmm4
xorps %xmm0, %xmm0
cvtsd2ss %xmm4, %xmm0
movss %xmm0, (%r9)
movl $0x0, (%rcx)
xorps %xmm0, %xmm0
xorl %eax, %eax
movss (%r12,%rax,4), %xmm1
movss (%rbx), %xmm2
mulss (%r15,%rax,4), %xmm2
subss %xmm2, %xmm1
movss (%rdi), %xmm2
mulss (%r14,%rax,4), %xmm2
subss %xmm2, %xmm1
subss (%rdx), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
movss %xmm0, (%rcx)
incq %rax
cmpq %rax, %r13
jne 0x27298
divss %xmm12, %xmm0
movss %xmm0, (%rcx)
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq