// File: /workspace/github_c_makefile_star2_O3/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, 0x70(%rsp)
movq %r9, 0x90(%rsp)
movq %rcx, 0x98(%rsp)
movq %rdx, %rbx
movq %rsi, %r12
movl %edi, 0x60(%rsp)
movq 0x260(%rsp), %r13
movq 0x228(%rsp), %r14
movl 0x210(%rsp), %edx
movq 0x230(%rsp), %rsi
xorl %r15d, %r15d
movl %r15d, 0x7c(%rsp)
movl %r15d, 0x78(%rsp)
movl $0x40000000, 0x74(%rsp) # imm = 0x40000000
leaq 0x6c(%rsp), %rbp
leaq 0x68(%rsp), %r8
movq %r14, %rdi
movq %rbp, %rcx
callq 0x108db
movss %xmm0, 0x64(%rsp)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x80(%rsp)
leaq 0x31054(%rip), %rdi     # 0x58eea
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, 0x88(%rsp)
movq %rbx, %rcx
movb $0x1, %al
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd 0x70(%rsp), %xmm0
leaq 0x3104c(%rip), %rsi     # 0x58f0a
movq 0x278(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rdx
movq %r13, %rcx
movl 0x210(%rsp), %r13d
movb $0x1, %al
callq 0x21a0
cvtss2sd (%rbp), %xmm1
leaq 0x68(%rsp), %rax
cvtss2sd (%rax), %xmm2
leaq 0x31025(%rip), %rsi     # 0x58f18
movq %rbx, %rdi
movsd 0x80(%rsp), %xmm0
movb $0x3, %al
callq 0x21a0
leaq 0x411f3(%rip), %rax     # 0x69100
cmpl %r15d, (%rax)
je 0x27fb4
cmpb $0x49, (%r12)
jne 0x27f3f
cmpb $0x4e, 0x1(%r12)
jne 0x27f3f
cmpb $0x4d, 0x2(%r12)
jne 0x27f3f
xorl %edx, %edx
xorps %xmm1, %xmm1
movsd 0x29246(%rip), %xmm2    # 0x51180
xorps %xmm0, %xmm0
jmp 0x27fa3
movq 0x250(%rsp), %rsi
movq 0x248(%rsp), %rdi
movq 0x238(%rsp), %rax
leaq 0x74(%rsp), %rbx
movq %rbx, 0x28(%rsp)
leaq 0x78(%rsp), %r15
movq %r15, 0x20(%rsp)
leaq 0x7c(%rsp), %rbp
movq %rbp, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movl %r13d, %edx
callq 0x2780f
movl %eax, %edx
xorps %xmm0, %xmm0
cvtss2sd (%r15), %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%rbp), %xmm1
xorps %xmm2, %xmm2
cvtss2sd (%rbx), %xmm2
movq 0x278(%rsp), %rbx
leaq 0x30f81(%rip), %rsi     # 0x58f2b
movq %rbx, %rdi
movb $0x3, %al
callq 0x21a0
movq 0x270(%rsp), %rbp
leaq 0x356a1(%rip), %rsi     # 0x5d664
movq %rbx, %rdi
movq 0x88(%rsp), %rdx
xorl %eax, %eax
callq 0x21a0
movq %rbx, %rdi
callq 0x21f0
leaq 0x30f5f(%rip), %rsi     # 0x58f43
leaq 0xa0(%rsp), %rdi
movq %r12, %rdx
movq 0x260(%rsp), %rcx
xorl %eax, %eax
callq 0x2270
testl %r13d, %r13d
jle 0x28030
movss 0x6c(%rsp), %xmm0
movss 0x68(%rsp), %xmm1
movl %r13d, %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 0x28014
movq 0x288(%rsp), %r15
leaq 0xa0(%rsp), %rcx
movq %r14, %rdi
movl %r13d, %esi
movq %rbp, %rdx
movss 0x64(%rsp), %xmm0
callq 0x281c2
movss 0x6c(%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 0x2808c
xorl %eax, %eax
movss (%r15,%rax,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%r15,%rax,4)
incq %rax
cmpq %rax, %rbx
jne 0x28074
cmpl $0x0, 0x60(%rsp)
jle 0x280f1
xorl %r13d, %r13d
movsd 0x30ac2(%rip), %xmm1    # 0x58b60
leaq 0x31483(%rip), %rbp     # 0x59528
leaq 0x328c0(%rip), %r14     # 0x5a96c
xorl %eax, %eax
xorps %xmm0, %xmm0
cvtss2sd (%r15,%r13,4), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x280e9
testl %eax, %eax
jne 0x280d2
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 0x30a77(%rip), %xmm1    # 0x58b60
incq %r13
cmpq %r13, %rbx
jne 0x280ae
leaq 0x30e48(%rip), %rsi     # 0x58f40
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
movq 0x268(%rsp), %rdx
movq 0x260(%rsp), %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x2270
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 0x2824e
movss 0x64(%rsp), %xmm0
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq