// File: /workspace/github_c_makefile_star2_O2/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, %r13d
movq %r8, 0x58(%rsp)
movq %rcx, 0x60(%rsp)
movl %edx, %ebx
movq %rsi, 0x50(%rsp)
movq %rdi, %r14
movslq %r9d, %r12
leaq (,%r12,4), %rbp
movq %rbp, %rdi
callq 0x21d0
movq %rax, 0x10(%rsp)
movq %rbp, %rdi
callq 0x21d0
movq %rax, 0x48(%rsp)
movslq %ebx, %r15
shlq $0x2, %r15
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x18(%rsp)
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x40(%rsp)
movq %rbp, %rdi
callq 0x21d0
movq %rax, 0x20(%rsp)
movq %rbp, %rdi
callq 0x21d0
movq %rax, 0x28(%rsp)
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x30(%rsp)
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x38(%rsp)
leaq 0x2d024(%rip), %rsi     # 0x52ea9
xorl %ebp, %ebp
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq 0x178(%rsp), %rdx
movq 0x180(%rsp), %rcx
xorl %eax, %eax
callq 0x2260
leaq 0x2b2a3(%rip), %rsi     # 0x51150
movq %r15, %rdi
callq 0x2210
movq %rax, 0x68(%rsp)
leaq 0x25de6(%rip), %rdi     # 0x4bca7
movq %r15, %rsi
xorl %eax, %eax
callq 0x20f0
testl %r12d, %r12d
movl $0x0, %eax
movl %r13d, 0x4(%rsp)
cmovgl %r13d, %eax
movq %rax, 0x8(%rsp)
cmpq %rbp, 0x8(%rsp)
je 0x25eff
movq 0x10(%rsp), %rax
andl $0x0, (%rax,%rbp,4)
movq 0x48(%rsp), %rax
andl $0x0, (%rax,%rbp,4)
incq %rbp
jmp 0x25ee1
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %r12d
cmovgl %ebx, %r12d
movq 0x40(%rsp), %rcx
movq 0x18(%rsp), %rdx
cmpq %rax, %r12
je 0x25f29
andl $0x0, (%rdx,%rax,4)
andl $0x0, (%rcx,%rax,4)
incq %rax
jmp 0x25f17
xorl %r15d, %r15d
cmpq %r15, 0x8(%rsp)
je 0x25f62
movq 0x50(%rsp), %rax
movq (%rax,%r15,8), %rdi
movq 0x170(%rsp), %rsi
movl %ebx, %edx
callq 0xfd9c
movq 0x58(%rsp), %rax
movq (%rax,%r15,8), %rdi
movl 0x4(%rsp), %esi
callq 0xfd4e
incq %r15
jmp 0x25f2c
xorl %r15d, %r15d
cmpq %r15, %r12
je 0x25f94
movq (%r14,%r15,8), %rdi
movq 0x170(%rsp), %rsi
movl %ebx, %edx
callq 0xfd9c
movq 0x60(%rsp), %rax
movq (%rax,%r15,8), %rdi
movl 0x4(%rsp), %esi
callq 0xfd4e
incq %r15
jmp 0x25f65
xorl %ebp, %ebp
cmpq 0x8(%rsp), %rbp
je 0x26010
movq 0x50(%rsp), %rax
movq (%rax,%rbp,8), %r15
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x2600b
movq (%r14,%r13,8), %rsi
movq %r15, %rdi
movq 0x170(%rsp), %rdx
movl %ebx, %ecx
callq 0xfd1e
mulss %xmm0, %xmm0
movq 0x10(%rsp), %rax
ucomiss (%rax,%rbp,4), %xmm0
jbe 0x25fe6
movq 0x10(%rsp), %rax
movss %xmm0, (%rax,%rbp,4)
movq 0x20(%rsp), %rax
movl %r13d, (%rax,%rbp,4)
movq 0x18(%rsp), %rax
ucomiss (%rax,%r13,4), %xmm0
jbe 0x26006
movq 0x18(%rsp), %rax
movss %xmm0, (%rax,%r13,4)
movq 0x30(%rsp), %rax
movl %ebp, (%rax,%r13,4)
incq %r13
jmp 0x25fa9
incq %rbp
jmp 0x25f96
xorl %r14d, %r14d
movq 0x40(%rsp), %r13
movq 0x48(%rsp), %rbp
cmpq 0x8(%rsp), %r14
je 0x26088
movq 0x58(%rsp), %rax
movq (%rax,%r14,8), %rbx
xorl %r15d, %r15d
cmpq %r15, %r12
je 0x26083
movq 0x60(%rsp), %rax
movq (%rax,%r15,8), %rsi
movq %rbx, %rdi
movl 0x4(%rsp), %edx
callq 0xfcf3
mulss %xmm0, %xmm0
ucomiss (%rbp,%r14,4), %xmm0
jbe 0x26066
movss %xmm0, (%rbp,%r14,4)
movq 0x28(%rsp), %rax
movl %r15d, (%rax,%r14,4)
ucomiss (%r13,%r15,4), %xmm0
jbe 0x2607e
movss %xmm0, (%r13,%r15,4)
movq 0x38(%rsp), %rax
movl %r14d, (%rax,%r15,4)
incq %r15
jmp 0x26030
incq %r14
jmp 0x2601d
leaq 0x2ce2c(%rip), %rdi     # 0x52ebb
pushq $0x27
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
movq 0x68(%rsp), %r15
movq %r15, %rcx
callq 0x2280
leaq 0x2ce37(%rip), %rdi     # 0x52ee3
pushq $0x21
popq %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2280
xorl %ebx, %ebx
leaq 0x2ce42(%rip), %r14     # 0x52f05
cmpq %rbx, 0x8(%rsp)
je 0x260f0
movq 0x10(%rsp), %rax
cvtss2sd (%rax,%rbx,4), %xmm0
movq 0x20(%rsp), %rax
movl (%rax,%rbx,4), %ecx
movq %r15, %rdi
movq %r14, %rsi
movl %ebx, %edx
movb $0x1, %al
callq 0x2190
incq %rbx
jmp 0x260c3
leaq 0x2bf54(%rip), %rdi     # 0x5204b
pushq $0x2
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2280
leaq 0x2ce05(%rip), %rdi     # 0x52f14
pushq $0x27
popq %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2280
leaq 0x2cdbf(%rip), %rdi     # 0x52ee3
pushq $0x21
popq %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2280
xorl %ebx, %ebx
leaq 0x2ce01(%rip), %r14     # 0x52f3c
cmpq %rbx, 0x8(%rsp)
je 0x26164
cvtss2sd (%rbp,%rbx,4), %xmm0
movq 0x28(%rsp), %rax
movl (%rax,%rbx,4), %ecx
movq %r15, %rdi
movq %r14, %rsi
movl %ebx, %edx
movb $0x1, %al
callq 0x2190
incq %rbx
jmp 0x2613b
leaq 0x2bee0(%rip), %rdi     # 0x5204b
pushq $0x2
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2280
leaq 0x2cdc7(%rip), %rdi     # 0x52f4a
pushq $0x27
popq %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2280
leaq 0x2cdda(%rip), %rdi     # 0x52f72
pushq $0x22
popq %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2280
xorl %ebx, %ebx
leaq 0x2cd8d(%rip), %r14     # 0x52f3c
cmpq %rbx, %r12
je 0x261da
movq 0x18(%rsp), %rax
cvtss2sd (%rax,%rbx,4), %xmm0
movq 0x30(%rsp), %rax
movl (%rax,%rbx,4), %ecx
movq %r15, %rdi
movq %r14, %rsi
movl %ebx, %edx
movb $0x1, %al
callq 0x2190
incq %rbx
jmp 0x261af
leaq 0x2be6a(%rip), %rdi     # 0x5204b
pushq $0x2
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2280
leaq 0x2cd9c(%rip), %rdi     # 0x52f95
pushq $0x27
popq %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2280
leaq 0x2cd64(%rip), %rdi     # 0x52f72
pushq $0x22
popq %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x2280
xorl %ebx, %ebx
leaq 0x2cd17(%rip), %r14     # 0x52f3c
cmpq %rbx, %r12
je 0x2624d
cvtss2sd (%r13,%rbx,4), %xmm0
movq 0x38(%rsp), %rax
movl (%rax,%rbx,4), %ecx
movq %r15, %rdi
movq %r14, %rsi
movl %ebx, %edx
movb $0x1, %al
callq 0x2190
incq %rbx
jmp 0x26225
leaq 0x2bdf7(%rip), %rdi     # 0x5204b
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x2280
movq %r15, %rdi
callq 0x20c0
movq 0x10(%rsp), %rdi
callq 0x2050
movq 0x20(%rsp), %rdi
callq 0x2050
movq %rbp, %rdi
callq 0x2050
movq 0x28(%rsp), %rdi
callq 0x2050
movq 0x18(%rsp), %rdi
callq 0x2050
movq 0x30(%rsp), %rdi
callq 0x2050
movq %r13, %rdi
callq 0x2050
movq 0x38(%rsp), %rdi
callq 0x2050
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq