// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/nma.c
// Function: Compare_modes
void Compare_modes(float **Cart_mode_ANM, float **Cart_mode_TNM, int N_Cart,
		   float **Tors_mode_ANM, float **Tors_mode_TNM, int N_tors,
		   float *mass_coord, char *prot_name, char *inter)
{
  // Compare torsional and cartesian normal modes
  int ia, i;
  float q;
  float *Ovlp_cart_max_TNM=malloc(N_tors*sizeof(float));
  float *Ovlp_tors_max_TNM=malloc(N_tors*sizeof(float));
  float *Ovlp_cart_max_ANM=malloc(N_Cart*sizeof(float));
  float *Ovlp_tors_max_ANM=malloc(N_Cart*sizeof(float));
  int *mode_cart_max_TNM=malloc(N_tors*sizeof(int));
  int *mode_tors_max_TNM=malloc(N_tors*sizeof(int));
  int *mode_cart_max_ANM=malloc(N_Cart*sizeof(int));
  int *mode_tors_max_ANM=malloc(N_Cart*sizeof(int));

  FILE *file_out; char outfile[200];

  sprintf(outfile, "%s_%s_compare.dat", prot_name, inter);
  file_out=fopen(outfile, "w");
  printf("Writing %s\n", outfile);

  for(i=0; i<N_tors; i++){
    Ovlp_cart_max_TNM[i]=0;
    Ovlp_tors_max_TNM[i]=0;
  }
  for(i=0; i<N_Cart; i++){
    Ovlp_cart_max_ANM[i]=0;
    Ovlp_tors_max_ANM[i]=0;
  }
  // Normalize
  for(i=0; i<N_tors; i++){
    Normalize_vector_weighted(Cart_mode_TNM[i], mass_coord, N_Cart);
    Normalize_vector(Tors_mode_TNM[i], N_tors);
  }
  for(i=0; i<N_Cart; i++){
    Normalize_vector_weighted(Cart_mode_ANM[i], mass_coord, N_Cart);
    Normalize_vector(Tors_mode_ANM[i], N_tors);
  }

  for(ia=0; ia<N_tors; ia++){
    float *Ca=Cart_mode_TNM[ia];
    for(i=0; i<N_Cart; i++){
      q=Scalar_product_weighted(Ca, Cart_mode_ANM[i], mass_coord, N_Cart);
      q*=q;
      if(q > Ovlp_cart_max_TNM[ia]){
	Ovlp_cart_max_TNM[ia]=q; mode_cart_max_TNM[ia]=i;
      }
      if(q > Ovlp_cart_max_ANM[i] ){
	Ovlp_cart_max_ANM[i]=q; mode_cart_max_ANM[i]=ia;
      }
    }
  }
  for(ia=0; ia<N_tors; ia++){
    float *Ta=Tors_mode_TNM[ia];
    for(i=0; i<N_Cart; i++){
      q=Scalar_product(Ta, Tors_mode_ANM[i],N_tors); q*=q;
      if(q > Ovlp_tors_max_TNM[ia]){
	Ovlp_tors_max_TNM[ia]=q; mode_tors_max_TNM[ia]=i;
      }
      if(q > Ovlp_tors_max_ANM[i] ){
	Ovlp_tors_max_ANM[i]=q; mode_tors_max_ANM[i]=ia;
      }
    }
  }

  fprintf(file_out, "# Max. coeff Cartesian TNM - ANM modes\n");
  fprintf(file_out, "# TNM_mode  Max.coeff^2 ANM_mode\n");
  for(i=0; i<N_tors; i++)
    fprintf(file_out, "%3d  %.3f %3d\n",
	    i, Ovlp_cart_max_TNM[i], mode_cart_max_TNM[i]);
  fprintf(file_out, "&\n");

  fprintf(file_out, "# Max. coeff Torsional TNM - ANM modes\n");
  fprintf(file_out, "# TNM_mode  Max.coeff^2 ANM_mode\n");
  for(i=0; i<N_tors; i++)
    fprintf(file_out, "%3d\t%.3f\t%3d\n",
	    i, Ovlp_tors_max_TNM[i], mode_tors_max_TNM[i]);
  fprintf(file_out, "&\n");

  fprintf(file_out, "# Max. coeff Cartesian ANM - TNM modes\n");
  fprintf(file_out, "# ANM_mode  Max.coeff^2  TNM_mode\n");
  for(i=0; i<N_Cart; i++)
    fprintf(file_out, "%3d\t%.3f\t%3d\n",
	    i, Ovlp_cart_max_ANM[i], mode_cart_max_ANM[i]);
  fprintf(file_out, "&\n");

  fprintf(file_out, "# Max. coeff Torsional ANM - TNM modes\n");
  fprintf(file_out, "# ANM_mode  Max.coeff^2  TNM_mode\n");
  for(i=0; i<N_Cart; i++)
    fprintf(file_out, "%3d\t%.3f\t%3d\n",
	    i, Ovlp_tors_max_ANM[i], mode_tors_max_ANM[i]);
  fprintf(file_out, "&\n");

  fclose(file_out);
  free(Ovlp_cart_max_TNM); free(mode_cart_max_TNM);
  free(Ovlp_tors_max_TNM); free(mode_tors_max_TNM);
  free(Ovlp_cart_max_ANM); free(mode_cart_max_ANM);
  free(Ovlp_tors_max_ANM); free(mode_tors_max_ANM);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %r9d, %r12d
movq %r8, 0x58(%rsp)
movq %rcx, 0x30(%rsp)
movl %edx, %r14d
movq %rsi, 0x50(%rsp)
movq %rdi, %rbx
movslq %r9d, %r13
leaq (,%r13,4), %r15
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x20(%rsp)
movq %r15, %rdi
callq 0x21d0
movq %rax, %rbp
movl %r14d, 0x4(%rsp)
movslq %r14d, %r14
shlq $0x2, %r14
movq %r14, %rdi
callq 0x21d0
movq %rax, 0x18(%rsp)
movq %r14, %rdi
callq 0x21d0
movq %rax, 0x48(%rsp)
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x38(%rsp)
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x28(%rsp)
movq %r14, %rdi
callq 0x21d0
movq %rax, 0x40(%rsp)
movq %r14, %rdi
callq 0x21d0
movq %rax, %r15
leaq 0x30397(%rip), %rsi     # 0x58f9c
leaq 0x70(%rsp), %r14
movq %r14, %rdi
movq 0x178(%rsp), %rdx
movq 0x180(%rsp), %rcx
xorl %eax, %eax
callq 0x2260
leaq 0x2e615(%rip), %rsi     # 0x57240
movq %r14, %rdi
callq 0x2210
movq %rax, 0x8(%rsp)
leaq 0x2909c(%rip), %rdi     # 0x51cdb
movq %r14, %rsi
xorl %eax, %eax
callq 0x20f0
movl %r12d, (%rsp)
movl %r12d, %r12d
testl %r13d, %r13d
movq 0x20(%rsp), %rdi
jle 0x28c79
leaq (,%r12,4), %r14
xorl %esi, %esi
movq %r14, %rdx
callq 0x2110
movq %rbp, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x2110
movq %rbp, 0x68(%rsp)
movq %r12, 0x10(%rsp)
movq %r15, 0x60(%rsp)
movl 0x4(%rsp), %ebp
movl %ebp, %r13d
testl %ebp, %ebp
jle 0x28cb9
leaq (,%r13,4), %r14
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x2110
movq 0x48(%rsp), %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x2110
movq 0x170(%rsp), %r14
cmpl $0x0, (%rsp)
movq 0x10(%rsp), %rax
movq 0x50(%rsp), %r12
jle 0x28d05
xorl %r15d, %r15d
movq (%r12,%r15,8), %rdi
movq %r14, %rsi
movl %ebp, %edx
movq %rax, %rbp
callq 0x10d14
movq 0x58(%rsp), %rax
movq (%rax,%r15,8), %rdi
movl (%rsp), %esi
callq 0x10ca9
movq %rbp, %rax
movl 0x4(%rsp), %ebp
incq %r15
cmpq %r15, %rax
jne 0x28cd4
testl %ebp, %ebp
movq 0x30(%rsp), %r12
jle 0x28d33
xorl %r15d, %r15d
movq (%rbx,%r15,8), %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x10d14
movq (%r12,%r15,8), %rdi
movl (%rsp), %esi
callq 0x10ca9
incq %r15
cmpq %r15, %r13
jne 0x28d11
cmpl $0x0, (%rsp)
jle 0x28daa
xorl %r12d, %r12d
cmpl $0x0, 0x4(%rsp)
jle 0x28da0
movq 0x50(%rsp), %rax
movq (%rax,%r12,8), %r15
xorl %ebp, %ebp
movq (%rbx,%rbp,8), %rsi
movq %r15, %rdi
movq %r14, %rdx
movl %r13d, %ecx
callq 0x10c76
mulss %xmm0, %xmm0
movq 0x20(%rsp), %rax
ucomiss (%rax,%r12,4), %xmm0
movq 0x38(%rsp), %rcx
jbe 0x28d7f
movss %xmm0, (%rax,%r12,4)
movl %ebp, (%rcx,%r12,4)
movq 0x18(%rsp), %rax
ucomiss (%rax,%rbp,4), %xmm0
movq 0x40(%rsp), %rcx
jbe 0x28d98
movss %xmm0, (%rax,%rbp,4)
movl %r12d, (%rcx,%rbp,4)
incq %rbp
cmpq %rbp, %r13
jne 0x28d4e
incq %r12
cmpq 0x10(%rsp), %r12
jne 0x28d3c
cmpl $0x0, (%rsp)
movq 0x48(%rsp), %r12
movq 0x68(%rsp), %rbp
movq 0x30(%rsp), %rcx
jle 0x28e2d
xorl %r14d, %r14d
cmpl $0x0, 0x4(%rsp)
jle 0x28e23
movq 0x58(%rsp), %rax
movq (%rax,%r14,8), %rbx
xorl %r15d, %r15d
movq (%rcx,%r15,8), %rsi
movq %rbx, %rdi
movl (%rsp), %edx
callq 0x10c48
mulss %xmm0, %xmm0
ucomiss (%rbp,%r14,4), %xmm0
jbe 0x28e00
movss %xmm0, (%rbp,%r14,4)
movq 0x28(%rsp), %rax
movl %r15d, (%rax,%r14,4)
ucomiss (%r12,%r15,4), %xmm0
jbe 0x28e16
movss %xmm0, (%r12,%r15,4)
movq 0x60(%rsp), %rax
movl %r14d, (%rax,%r15,4)
incq %r15
cmpq %r15, %r13
movq 0x30(%rsp), %rcx
jne 0x28dd5
incq %r14
cmpq 0x10(%rsp), %r14
jne 0x28dc2
leaq 0x3017a(%rip), %rdi     # 0x58fae
movl $0x27, %esi
movl $0x1, %edx
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x2280
leaq 0x30184(%rip), %rdi     # 0x58fd6
movl $0x21, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2280
cmpl $0x0, (%rsp)
movq 0x20(%rsp), %r15
movq 0x38(%rsp), %r14
jle 0x28ea2
xorl %ebx, %ebx
xorps %xmm0, %xmm0
cvtss2sd (%r15,%rbx,4), %xmm0
movl (%r14,%rbx,4), %ecx
movq 0x8(%rsp), %rdi
leaq 0x30169(%rip), %rsi     # 0x58ff8
movl %ebx, %edx
movb $0x1, %al
callq 0x2190
incq %rbx
cmpq %rbx, 0x10(%rsp)
jne 0x28e76
leaq 0x2f282(%rip), %rdi     # 0x5812b
movl $0x2, %esi
movl $0x1, %edx
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x2280
leaq 0x30140(%rip), %rdi     # 0x59007
movl $0x27, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2280
leaq 0x300f6(%rip), %rdi     # 0x58fd6
movl $0x21, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2280
cmpl $0x0, (%rsp)
movq 0x28(%rsp), %r15
jle 0x28f2e
xorl %ebx, %ebx
leaq 0x30129(%rip), %r14     # 0x5902f
xorps %xmm0, %xmm0
cvtss2sd (%rbp,%rbx,4), %xmm0
movl (%r15,%rbx,4), %ecx
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
movb $0x1, %al
callq 0x2190
incq %rbx
cmpq %rbx, 0x10(%rsp)
jne 0x28f06
leaq 0x2f1f6(%rip), %rdi     # 0x5812b
movl $0x2, %esi
movl $0x1, %edx
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x2280
leaq 0x300ea(%rip), %rdi     # 0x5903d
movl $0x27, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2280
leaq 0x300f9(%rip), %rdi     # 0x59065
movl $0x22, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2280
cmpl $0x0, 0x4(%rsp)
movq 0x18(%rsp), %r15
movq 0x40(%rsp), %r14
jle 0x28fbb
xorl %ebx, %ebx
xorps %xmm0, %xmm0
cvtss2sd (%r15,%rbx,4), %xmm0
movl (%r14,%rbx,4), %ecx
movq 0x8(%rsp), %rdi
leaq 0x30085(%rip), %rsi     # 0x5902f
movl %ebx, %edx
movb $0x1, %al
callq 0x2190
incq %rbx
cmpq %rbx, %r13
jne 0x28f91
leaq 0x2f169(%rip), %rdi     # 0x5812b
movl $0x2, %esi
movl $0x1, %edx
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x2280
leaq 0x300a8(%rip), %rdi     # 0x59088
movl $0x27, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2280
leaq 0x3006c(%rip), %rdi     # 0x59065
movl $0x22, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2280
cmpl $0x0, 0x4(%rsp)
movq 0x60(%rsp), %r15
jle 0x29046
xorl %ebx, %ebx
leaq 0x3000f(%rip), %r14     # 0x5902f
xorps %xmm0, %xmm0
cvtss2sd (%r12,%rbx,4), %xmm0
movl (%r15,%rbx,4), %ecx
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
movb $0x1, %al
callq 0x2190
incq %rbx
cmpq %rbx, %r13
jne 0x29020
leaq 0x2f0de(%rip), %rdi     # 0x5812b
movl $0x2, %esi
movl $0x1, %edx
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x2280
movq %rbx, %rdi
callq 0x20c0
movq 0x20(%rsp), %rdi
callq 0x2050
movq 0x38(%rsp), %rdi
callq 0x2050
movq %rbp, %rdi
callq 0x2050
movq 0x28(%rsp), %rdi
callq 0x2050
movq 0x18(%rsp), %rdi
callq 0x2050
movq 0x40(%rsp), %rdi
callq 0x2050
movq %r12, %rdi
callq 0x2050
movq %r15, %rdi
callq 0x2050
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq