// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/nma.c
// Function: Compare_anisou
int Compare_anisou(float ***aniso_pred, float ***aniso_exp, int nca,
		   char *model, char *inter, char *file_name, char *prot_name,
		   int N, int *weight, float *dot_aniso, float *ov_aniso,
		   float *delta_aniso)
{
  //FILE *file_out=fopen(file_name, "a");
  int ica, i, j, n_sum=0;
  float sum_overlap=0, sum_dot=0, sum_delta=0;
  float delta, overlap, dot, dd;
  float **e_vec1=Allocate_mat2_f(3,3), e_val1[3];
  float **e_vec2=Allocate_mat2_f(3,3), e_val2[3];
  float **e_vec12=Allocate_mat2_f(3,3), e_val12[3];
  float **u_12=Allocate_mat2_f(3,3);

  for(ica=0; ica<nca; ica++){
    if(weight[ica]){
      float **u1=aniso_exp[ica],  norm1=0;
      float **u2=aniso_pred[ica], norm2=0;
      float prod_lambda=1.0, prod_lambda_12=1.0;
      float isotropy;

      // Exclude quasi-spherical distributions, isotropy >= 0.5
      f_Diagonalize(3, u1, e_val1, e_vec1, 1);
      isotropy=e_val1[2]/e_val1[0]; //printf("%.2f\n", anisotropy);
      if(isotropy >= 0.5)continue;
      n_sum++;

      /****************  Overlap *********************/
      // Normalize by the trace
      for(i=0; i<3; i++)norm1+=u1[i][i];
      for(i=0; i<3; i++)norm2+=u2[i][i];
      for(i=0; i<3; i++)for(j=i; j<3; j++){
	  u1[i][j]/=norm1; u2[i][j]/=norm2;
	  u_12[i][j]=u1[i][j]+u2[i][j];
	}
      for(i=0; i<3; i++)for(j=0; j<i; j++){
	  u1[i][j]=u1[j][i]; u2[i][j]=u2[j][i]; u_12[i][j]=u_12[j][i];
	}

      for(i=0; i<3; i++)e_val1[i]/=norm1;
      f_Diagonalize(3, u2, e_val2, e_vec2, 1);
      f_Diagonalize(3, u_12, e_val12, e_vec12, 1);
      for(i=0; i<3; i++){
	prod_lambda*=(e_val1[i]*e_val2[i]);
	prod_lambda_12*=e_val12[i];
      }
      overlap=sqrt(8.*sqrt(prod_lambda)/prod_lambda_12);
      sum_overlap += overlap;
      // printf("overlap: %.3f\n", overlap);

      /*****   dot product   **************/
      /*
	dot=0; dot_norm=0;
	for(i=0; i<3; i++){
	float w=e_val1[i]*e_val2[i];
	dot+=w*Scalar_product_3(e_vec1[i], e_vec2[i]);
	dot_norm+=w;
	}
	dot /= dot_norm;
      */
      dot=Scalar_product_3(e_vec1[0], e_vec2[0]);
      if(dot<0){dot=-dot;} sum_dot += dot;

      /***********   delta   **************/
      delta=0;           // delta = sqrt(sum_ij(U1_ij-U2_ij)^2/9)
      for(i=0; i<3; i++){
	dd=u1[i][i]-u2[i][i]; delta+=dd*dd;
	for(j=0; j<i; j++){dd=u1[i][j]-u2[i][j]; delta+=2*dd*dd;}
      }
      sum_delta+=delta;

      /*
      //fprintf(file_out, "%6.3f %6.3f\n", overlap, dot);
      for(i=0; i<3; i++)for(j=0; j<=i; j++){
      fprintf(file_out, "%6.3f %6.3f ", u1[i][j], u2[i][j]);
      }
      fprintf(file_out, "\n");
      */
    }
  }
  *ov_aniso=sum_overlap/n_sum;
  *dot_aniso=sum_dot/n_sum;
  *delta_aniso=sqrt(sum_delta/(9*n_sum));

  /*
    fprintf(file_out, "%6.3f  %s %8s %6.3f %2.0f %8s %3d\n",
	  sum_overlap/n_sum, model, inter, sum_dot/n_sum, n_sum, prot_name, N);
  fclose(file_out);
  */

  // Clean
  Empty_matrix_f(e_vec1, 3);
  Empty_matrix_f(e_vec2, 3);
  Empty_matrix_f(e_vec12, 3);
  Empty_matrix_f(u_12, 3);

  return(n_sum);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edx, %ebp
movq %rsi, 0x48(%rsp)
movq %rdi, 0x40(%rsp)
movl $0x3, %edi
movl $0x3, %esi
callq 0x496ae
movq %rax, %rbx
movl $0x3, %edi
movl $0x3, %esi
callq 0x496ae
movq %rax, 0x20(%rsp)
movl $0x3, %edi
movl $0x3, %esi
callq 0x496ae
movq %rax, 0x38(%rsp)
movl $0x3, %edi
movl $0x3, %esi
callq 0x496ae
movq %rax, %r12
testl %ebp, %ebp
jle 0x28120
movl %ebp, %r15d
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
xorl %r13d, %r13d
movq $0x0, 0x18(%rsp)
movss %xmm0, 0x8(%rsp)
movq 0xb0(%rsp), %rax
cmpl $0x0, (%rax,%r13,4)
je 0x28112
movss %xmm0, 0x14(%rsp)
movq 0x48(%rsp), %rax
movq (%rax,%r13,8), %r14
movq 0x40(%rsp), %rax
movq (%rax,%r13,8), %rbp
movl $0x3, %edi
movq %r14, %rsi
leaq 0x2c(%rsp), %rdx
movq %rbx, %rcx
movl $0x1, %r8d
callq 0x4a144
movss 0x34(%rsp), %xmm0
divss 0x2c(%rsp), %xmm0
ucomiss 0x2dccb(%rip), %xmm0    # 0x55b7c
jae 0x2801a
xorps %xmm0, %xmm0
xorl %eax, %eax
movq (%r14,%rax,8), %rcx
addss (%rcx,%rax,4), %xmm0
incq %rax
cmpq $0x3, %rax
jne 0x27ebc
xorps %xmm1, %xmm1
xorl %eax, %eax
movq (%rbp,%rax,8), %rcx
addss (%rcx,%rax,4), %xmm1
incq %rax
cmpq $0x3, %rax
jne 0x27ed3
xorl %eax, %eax
movq (%r14,%rax,8), %rcx
movq (%rbp,%rax,8), %rdx
movq (%r12,%rax,8), %rsi
movq %rax, %rdi
movss (%rcx,%rdi,4), %xmm2
divss %xmm0, %xmm2
movss %xmm2, (%rcx,%rdi,4)
movss (%rdx,%rdi,4), %xmm2
divss %xmm1, %xmm2
movss %xmm2, (%rdx,%rdi,4)
addss (%rcx,%rdi,4), %xmm2
movss %xmm2, (%rsi,%rdi,4)
incq %rdi
cmpq $0x3, %rdi
jne 0x27ef8
incq %rax
cmpq $0x3, %rax
jne 0x27ee8
xorl %eax, %eax
testq %rax, %rax
je 0x27f7c
movq (%r14,%rax,8), %rcx
movq (%rbp,%rax,8), %rdx
movq (%r12,%rax,8), %rsi
xorl %edi, %edi
movq (%r14,%rdi,8), %r8
movss (%r8,%rax,4), %xmm1
movss %xmm1, (%rcx,%rdi,4)
movq (%rbp,%rdi,8), %r8
movss (%r8,%rax,4), %xmm1
movss %xmm1, (%rdx,%rdi,4)
movq (%r12,%rdi,8), %r8
movss (%r8,%rax,4), %xmm1
movss %xmm1, (%rsi,%rdi,4)
incq %rdi
cmpq %rdi, %rax
jne 0x27f46
incq %rax
cmpq $0x3, %rax
jne 0x27f32
xorl %eax, %eax
movss 0x2c(%rsp,%rax,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, 0x2c(%rsp,%rax,4)
incq %rax
cmpq $0x3, %rax
jne 0x27f87
movl $0x3, %edi
movq %rbp, %rsi
leaq 0x5c(%rsp), %rdx
movq 0x20(%rsp), %rcx
movl $0x1, %r8d
callq 0x4a144
movl $0x3, %edi
movq %r12, %rsi
leaq 0x50(%rsp), %rdx
movq 0x38(%rsp), %rcx
movl $0x1, %r8d
callq 0x4a144
xorl %eax, %eax
movss 0x2902c(%rip), %xmm0    # 0x51010
movaps %xmm0, %xmm2
movss 0x2c(%rsp,%rax,4), %xmm1
mulss 0x5c(%rsp,%rax,4), %xmm1
mulss %xmm1, %xmm0
mulss 0x50(%rsp,%rax,4), %xmm2
incq %rax
cmpq $0x3, %rax
jne 0x27fe7
cvtss2sd %xmm0, %xmm0
ucomisd 0x2904e(%rip), %xmm0    # 0x51060
jb 0x28025
sqrtsd %xmm0, %xmm0
jmp 0x28036
movss 0x14(%rsp), %xmm0
jmp 0x28112
movss %xmm2, 0x10(%rsp)
callq 0x2290
movss 0x10(%rsp), %xmm2
mulsd 0x2f37a(%rip), %xmm0    # 0x573b8
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
divsd %xmm1, %xmm0
ucomisd 0x2900f(%rip), %xmm0    # 0x51060
jb 0x28059
sqrtsd %xmm0, %xmm0
jmp 0x2805e
callq 0x2290
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x10(%rsp)
movq (%rbx), %rdi
movq 0x20(%rsp), %rax
movq (%rax), %rsi
callq 0x108be
movaps %xmm0, %xmm1
xorps 0x2e8ae(%rip), %xmm1    # 0x56930
maxss %xmm0, %xmm1
xorps %xmm4, %xmm4
xorl %eax, %eax
movaps %xmm4, %xmm2
movq (%r14,%rax,8), %rcx
movss (%rcx,%rax,4), %xmm4
movq (%rbp,%rax,8), %rdx
subss (%rdx,%rax,4), %xmm4
mulss %xmm4, %xmm4
addss %xmm2, %xmm4
testq %rax, %rax
je 0x280d1
xorl %esi, %esi
movss (%rcx,%rsi,4), %xmm2
subss (%rdx,%rsi,4), %xmm2
movaps %xmm2, %xmm3
addss %xmm2, %xmm3
mulss %xmm2, %xmm3
addss %xmm3, %xmm4
incq %rsi
cmpq %rsi, %rax
jne 0x280b0
incq %rax
cmpq $0x3, %rax
jne 0x2808b
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
movss 0xc(%rsp), %xmm0
addss 0x10(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
movss 0x8(%rsp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x8(%rsp)
movss 0x14(%rsp), %xmm0
addss %xmm4, %xmm0
incq %r13
cmpq %r15, %r13
jne 0x27e58
jmp 0x2813b
movq $0x0, 0x18(%rsp)
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
movss %xmm1, 0x8(%rsp)
movss %xmm1, 0xc(%rsp)
movq 0x18(%rsp), %r15
xorps %xmm1, %xmm1
cvtsi2ss %r15d, %xmm1
movq 0xc8(%rsp), %r14
movss 0xc(%rsp), %xmm2
divss %xmm1, %xmm2
movq 0xc0(%rsp), %rax
movss %xmm2, (%rax)
movss 0x8(%rsp), %xmm2
divss %xmm1, %xmm2
leal (%r15,%r15,8), %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movq 0xb8(%rsp), %rax
movss %xmm2, (%rax)
divss %xmm1, %xmm0
ucomiss 0x28e7a(%rip), %xmm0    # 0x5100c
jb 0x2819a
sqrtss %xmm0, %xmm0
jmp 0x2819f
callq 0x21c0
movss %xmm0, (%r14)
movq %rbx, %rdi
movl $0x3, %esi
callq 0x4960c
movq 0x20(%rsp), %rdi
movl $0x3, %esi
callq 0x4960c
movq 0x38(%rsp), %rdi
movl $0x3, %esi
callq 0x4960c
movq %r12, %rdi
movl $0x3, %esi
callq 0x4960c
movl %r15d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq