// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/nma.c
// Function: Output_modes
float Output_modes(int N,           //degrees of freedom
		   char *model,     // INM, ANM or TNM
		   char *REF_ATM,   // Reference atoms
		   atom *atoms, int N_atoms,
		   struct residue *seq, int N_res,
		   struct axe *axes, int N_axes,
		   float *B_CA, float *B_CA_exp,
		   int *weight, float *dist_CA,
		   float ***aniso_pred, float ***aniso_exp,
		   int N_MODES, float cutoff,
		   char *inter, char *prot_name,
		   FILE *file_B, FILE *file_sum,
		   float **eigen_vector, float *eigen_value, float *eigen_B,
		   float *Cart_collectivity, float *Tors_collectivity,
		   float **Cart_mode, int *atom_ref, int N_ref)
{
  char outfile[200], string[100]; int n_aniso=0;
  float r, slope, offset, dot_aniso=0, ov_aniso=0, delta_aniso=2.;
  float slope_calc, slope_dist, offset2, r2, chi;
  int neg=0, i;
  int nca=N_res;

  // B factors comparison
  // Model 1: B_exp = slope*B_calc + offset
  // r=Corr_coeff(B_CA, B_CA_exp, weight, nca, &slope, &offset, &newnorm);
  r=Corr_coeff(B_CA, B_CA_exp, nca, &slope, &offset);
  printf("cc(B, %s %s)=  %6.3f   ref: %s\n", model, inter, r, REF_ATM);

  fprintf(file_sum, "%3s\t%3s\t%4.1f", model, inter, cutoff);
  fprintf(file_sum, "\t%6.3f\t%8.5f\t%5.3f", r, slope, offset);
  if(FIT_ROT){
    // Model 2: B_exp = slope*B_calc + offset
    Bivariate_fit(nca, B_CA_exp, B_CA, dist_CA, weight,
		  &slope_calc, &slope_dist, &offset2, &r2, &chi);
    fprintf(file_sum, "\t%6.3f\t%8.5f\t%8.5f\t%5.3f ",
	    r2, slope_calc, slope_dist, offset2);
  }
  //fprintf(file_sum, "%8s  %3d  %4.1f", prot_name, N, cutoff);
  // Anisotropic B factors
  if(ANISOU){
    if(strncmp(model, "INM", 3)!=0){
      n_aniso=Compare_anisou(aniso_pred, aniso_exp, nca, model, inter,
			     file_aniso, prot_name, N, weight,
			     &dot_aniso, &ov_aniso, &delta_aniso);
    }
    fprintf(file_sum, "\t%3d\t%.3f\t%6.3f\t%.4f",
	    n_aniso, ov_aniso, dot_aniso, delta_aniso);
  }
  fprintf(file_sum, "\t%s\n", REF_ATM);
  fflush(file_sum);

  // Print isotropic B factors
  sprintf(string, "%s_%s", model, inter);
  Rescale(B_CA, nca, slope, offset);
  Print_B(B_CA, nca, file_B, string, r);

  // Normalize eigenvalues
  if(slope>0)for(i=0; i<N; i++)eigen_value[i]/=slope;
  // for(i=0; i<N; i++)eigen_value[i]*=100;
  // Print negative eigenvalues, if any
  for(i=0; i<N; i++){
    if(eigen_value[i]<-0.001){
      if(neg==0){printf("Negative eigenvalues:\n"); neg=1;}
      printf("%.4f\n", eigen_value[i]);
    }
  }

  // Print normal modes
  sprintf(outfile, "%s_%s_%s", prot_name, inter, model);
  Print_modes_old(eigen_value, eigen_vector, eigen_B,
		  Cart_collectivity, Tors_collectivity, N, N_MODES,
		  atoms, N_atoms, nca, seq, axes, N_axes, outfile, model,
		  Cart_mode, atom_ref, N_ref);

  return(r);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movss %xmm0, 0x7c(%rsp)
movq %r9, 0x90(%rsp)
movq %rcx, 0x98(%rsp)
movq %rdx, %rbx
movq %rsi, %r12
movl %edi, 0x60(%rsp)
movq 0x278(%rsp), %r15
movq 0x260(%rsp), %r13
movq 0x228(%rsp), %r14
movl 0x210(%rsp), %edx
movq 0x230(%rsp), %rsi
xorl %ebp, %ebp
movl %ebp, 0x74(%rsp)
movl %ebp, 0x70(%rsp)
movl $0x40000000, 0x6c(%rsp) # imm = 0x40000000
leaq 0x64(%rsp), %rcx
leaq 0x78(%rsp), %r8
movq %r14, %rdi
callq 0x10d91
movss %xmm0, 0x68(%rsp)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x80(%rsp)
leaq 0x30bda(%rip), %rdi     # 0x58f39
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, 0x88(%rsp)
movq %rbx, %rcx
movb $0x1, %al
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd 0x7c(%rsp), %xmm0
leaq 0x30bd2(%rip), %rsi     # 0x58f59
movq %r15, %rdi
movq %r12, %rdx
movq %r13, %rcx
movb $0x1, %al
callq 0x2190
leaq 0x64(%rsp), %rax
cvtss2sd (%rax), %xmm1
movl 0x210(%rsp), %ebx
leaq 0x78(%rsp), %rax
cvtss2sd (%rax), %xmm2
leaq 0x30bb0(%rip), %rsi     # 0x58f67
movq %r15, %rdi
movsd 0x80(%rsp), %xmm0
movb $0x3, %al
callq 0x2190
leaq 0x40d2f(%rip), %rax     # 0x69100
cmpl %ebp, (%rax)
je 0x28463
leaq 0x30b9a(%rip), %rsi     # 0x58f7a
movl $0x3, %edx
movq %r12, %rdi
callq 0x2070
testl %eax, %eax
je 0x28435
movq 0x250(%rsp), %rsi
movq 0x248(%rsp), %rdi
movq 0x238(%rsp), %rax
leaq 0x6c(%rsp), %rcx
movq %rcx, 0x28(%rsp)
leaq 0x70(%rsp), %rcx
movq %rcx, 0x20(%rsp)
leaq 0x74(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movl %ebx, %edx
callq 0x27dcc
movl %eax, %ebp
xorps %xmm0, %xmm0
cvtss2sd 0x70(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x74(%rsp), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x6c(%rsp), %xmm2
leaq 0x30b27(%rip), %rsi     # 0x58f7e
movq %r15, %rdi
movl %ebp, %edx
movb $0x3, %al
callq 0x2190
movq 0x270(%rsp), %rbp
leaq 0x35222(%rip), %rsi     # 0x5d694
movq %r15, %rdi
movq 0x88(%rsp), %rdx
xorl %eax, %eax
callq 0x2190
movq %r15, %rdi
callq 0x21e0
leaq 0x30b03(%rip), %rsi     # 0x58f96
leaq 0xa0(%rsp), %rdi
movq %r12, %rdx
movq 0x260(%rsp), %rcx
xorl %eax, %eax
callq 0x2260
testl %ebx, %ebx
jle 0x284dd
movss 0x64(%rsp), %xmm0
movss 0x78(%rsp), %xmm1
movl %ebx, %eax
xorl %ecx, %ecx
movss (%r14,%rcx,4), %xmm2
mulss %xmm0, %xmm2
addss %xmm1, %xmm2
movss %xmm2, (%r14,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x284c1
movq 0x288(%rsp), %r15
leaq 0xa0(%rsp), %rcx
movq %r14, %rdi
movl %ebx, %esi
movq %rbp, %rdx
movss 0x68(%rsp), %xmm0
callq 0x2866e
movss 0x64(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
setbe %al
movl 0x60(%rsp), %edx
testl %edx, %edx
setle %cl
orb %al, %cl
movl %edx, %ebx
jne 0x28538
xorl %eax, %eax
movss (%r15,%rax,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%r15,%rax,4)
incq %rax
cmpq %rax, %rbx
jne 0x28520
cmpl $0x0, 0x60(%rsp)
jle 0x2859d
xorl %r13d, %r13d
movsd 0x30646(%rip), %xmm1    # 0x58b90
leaq 0x3102e(%rip), %rbp     # 0x5957f
leaq 0x32470(%rip), %r14     # 0x5a9c8
xorl %eax, %eax
xorps %xmm0, %xmm0
cvtss2sd (%r15,%r13,4), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x28595
testl %eax, %eax
jne 0x28575
movq %rbp, %rdi
callq 0x2090
xorps %xmm0, %xmm0
cvtss2sd (%r15,%r13,4), %xmm0
movq %r14, %rdi
movb $0x1, %al
callq 0x20f0
movl $0x1, %eax
movsd 0x305fb(%rip), %xmm1    # 0x58b90
incq %r13
cmpq %r13, %rbx
jne 0x2855a
leaq 0x309ef(%rip), %rsi     # 0x58f93
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
movq 0x268(%rsp), %rdx
movq 0x260(%rsp), %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x2260
movl 0x2b8(%rsp), %eax
movl %eax, 0x58(%rsp)
movq 0x2b0(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x2a8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq %r12, 0x40(%rsp)
movq %rbx, 0x38(%rsp)
movq 0x218(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x258(%rsp), %eax
movl %eax, (%rsp)
movq %r15, %rdi
movq 0x280(%rsp), %rsi
movq 0x290(%rsp), %rdx
movq 0x298(%rsp), %rcx
movq 0x2a0(%rsp), %r8
movl 0x60(%rsp), %r9d
callq 0x286fa
movss 0x68(%rsp), %xmm0
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq