// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/site_dynamics.c
// Function: Binding_site_dynamics
void Binding_site_dynamics(struct Normal_Mode NM, struct Reference Ref,
			   struct residue *seq, atom *atoms,
			   char *nameout1, //int mode,
			   char *pdb, char *chain, int Nchain,
			   int Nres, char *SITES)
{
  int NMAX=1000; // Maximum number of sites
  int *site_res[NMAX], nres_site[NMAX];
  char *chain_res[NMAX], *description[NMAX], file_s[200];
  int PRINT_SITE=1;  // Print active site read in the PDB?
  printf("Reading binding sites\n");
  int N_sites=Read_sites(site_res, nres_site, chain_res, description, file_s,
			 NMAX, SITES, pdb, chain, Nchain, seq, Nres,PRINT_SITE);
  printf("%d binding sites read\n", N_sites);
  if(N_sites==0)return;

  // Extract c_alpha
  char SEL[4]="CA"; float mass=0;
  int Nref=Ref.N_ref, iref[Nref], iatom[Nref], atomres[Nref];
  int Na=Extract_atoms(iref, iatom, atomres, seq, &mass, atoms, Ref, SEL);
  if(Na!=Nres){
    printf("WARNING, %d residues but %d representative atoms", Nres, Na);
    printf(" found in Binding_site_dynamics\n");
  }

  char nameout[200]; int i, a, k;
  sprintf(nameout, "%s_sites_dynamics.dat", nameout1);
  FILE *file_out=fopen(nameout, "w");
  fprintf(file_out, "# read binding sites in file %s\n", file_s);
  float lambda[3], d2[3];
  float **v=Allocate_mat2_f(3, 3);
  fprintf(file_out, "# 1=<d>^2 2=<d>^2_x 3=<d>^2_y 4=<d>^2_z "); k=5;
  for(a=0; a<3; a++){
    fprintf(file_out,
	    " %d=projection_%d %d=v%d_x %d=v%d_y %d=v%d_z",
	    k,a,k+1,a,k+2,a,k+3,a); k+=4;
  }
  fprintf(file_out, "\n");
  for(i=0; i<N_sites; i++){
    Eigen_directions(d2, lambda, v,
		     site_res[i], nres_site[i], chain_res[i], NM,
		     atomres, iref);
    fprintf(file_out, "# site %s\n", description[i]);
    float sum=d2[0]+d2[1]+d2[2];
    fprintf(file_out, "%.3g  %.3f %.3f %.3f  ",
	    sum, d2[0]/sum, d2[1]/sum, d2[2]/sum);
    for(a=0; a<3; a++){
      fprintf(file_out, "%.3g  %.3f %.3f %.3f    ",
	      lambda[a], v[a][0], v[a][1], v[a][2]);
    }
    fprintf(file_out, "\n");
  }
  printf("Writing %s\n", nameout);

  Empty_matrix_f(v,3); //modyves: v was never freed
  for(i=0; i<N_sites; i++){
    free(site_res[i]);
    free(chain_res[i]);
    if(description[i])free(description[i]);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x6f48, %rsp           # imm = 0x6F48
movl %r9d, %r12d
movq %r8, %r15
movq %rcx, %r14
movq %rdx, -0x38(%rbp)
movq %rsi, -0x50(%rbp)
movq %rdi, %rbx
movl 0x110(%rbp), %r13d
leaq 0x158d9(%rip), %rdi     # 0x5dcaf
callq 0x2090
subq $0x8, %rsp
leaq -0x5030(%rbp), %rdi
leaq -0x11b0(%rbp), %rsi
leaq -0x6f70(%rbp), %rdx
leaq -0x30f0(%rbp), %rcx
leaq -0x210(%rbp), %r8
movl $0x3e8, %r9d            # imm = 0x3E8
pushq $0x1
pushq %r13
pushq %rbx
pushq %r12
pushq %r15
pushq %r14
pushq 0x118(%rbp)
callq 0x48755
addq $0x40, %rsp
movl %eax, %r14d
leaq 0x1526d(%rip), %rdi     # 0x5d699
movl %eax, %esi
xorl %eax, %eax
callq 0x20f0
movl %r14d, -0x2c(%rbp)
testl %r14d, %r14d
je 0x48746
leaq 0xe0(%rbp), %rcx
leaq -0x68(%rbp), %rax
movl $0x4143, (%rax)         # imm = 0x4143
leaq -0x64(%rbp), %r8
movl $0x0, (%r8)
movl (%rcx), %edx
movq %rsp, %rdi
leaq 0xf(,%rdx,4), %r9
andq $-0x10, %r9
subq %r9, %rdi
movq %rdi, %rsp
movq %rsp, %rsi
subq %r9, %rsi
movq %rsi, %rsp
movq %rsp, %rdx
subq %r9, %rdx
movq %rdx, %rsp
subq $0x40, %rsp
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rax, 0x30(%rsp)
movq %rdi, -0x60(%rbp)
movq %rdx, -0x58(%rbp)
movq %rbx, %rcx
movq -0x50(%rbp), %r9
callq 0x44267
addq $0x40, %rsp
movl 0x110(%rbp), %esi
cmpl %esi, %eax
je 0x484e7
leaq 0x1580d(%rip), %rdi     # 0x5dcdf
movl %eax, %edx
xorl %eax, %eax
callq 0x20f0
leaq 0x15adc(%rip), %rdi     # 0x5dfbe
callq 0x2090
leaq 0x15822(%rip), %rsi     # 0x5dd10
leaq -0x140(%rbp), %rbx
movq %rbx, %rdi
movq -0x38(%rbp), %rdx
xorl %eax, %eax
callq 0x2260
leaq 0xed36(%rip), %rsi      # 0x57240
movq %rbx, %rdi
callq 0x2210
movq %rax, %r13
leaq 0x151dc(%rip), %rsi     # 0x5d6f8
leaq -0x210(%rbp), %rdx
movq %rax, %rdi
xorl %eax, %eax
callq 0x2190
movl $0x3, %edi
movl $0x3, %esi
callq 0x496ae
movq %rax, %rbx
leaq 0x157e0(%rip), %rdi     # 0x5dd26
movl $0x28, %esi
movl $0x1, %edx
movq %r13, %rcx
callq 0x2280
movl $0x5, %r15d
leaq 0x157ea(%rip), %r12     # 0x5dd4f
xorl %r14d, %r14d
leal 0x1(%r15), %r8d
leal 0x2(%r15), %r10d
leal 0x3(%r15), %r11d
movq %r13, %rdi
movq %r12, %rsi
movl %r15d, %edx
movl %r14d, %ecx
movl %r14d, %r9d
xorl %eax, %eax
pushq %r14
pushq %r11
pushq %r14
pushq %r10
callq 0x2190
addq $0x20, %rsp
incl %r14d
leal 0x4(%r15), %eax
cmpl $0xd, %r15d
movl %eax, %r15d
jne 0x48568
movl $0xa, %edi
movq %r13, %rsi
callq 0x2140
movl -0x2c(%rbp), %eax
movl %eax, %r14d
testl %eax, %eax
jle 0x486e8
leaq 0x157d6(%rip), %r12     # 0x5dd9e
xorl %r15d, %r15d
movq %r14, -0x38(%rbp)
movq -0x5030(%rbp,%r15,8), %rax
movl -0x11b0(%rbp,%r15,4), %r8d
subq $0xe0, %rsp
movl $0x1a, %ecx
movq %rsp, %rdi
leaq 0x10(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq -0x60(%rbp), %rcx
movq %rcx, 0xd8(%rsp)
movq -0x58(%rbp), %rcx
movq %rcx, 0xd0(%rsp)
leaq -0x44(%rbp), %rdi
leaq -0x74(%rbp), %rsi
movq %rbx, %rdx
movq %rax, %rcx
callq 0x48a16
addq $0xe0, %rsp
movq -0x30f0(%rbp,%r15,8), %rdx
movq %r13, %rdi
leaq 0x15743(%rip), %rsi     # 0x5dd7c
xorl %eax, %eax
callq 0x2190
movss -0x44(%rbp), %xmm1
movss -0x40(%rbp), %xmm2
movaps %xmm1, %xmm3
addss %xmm2, %xmm3
movss -0x3c(%rbp), %xmm4
addss %xmm4, %xmm3
xorps %xmm0, %xmm0
cvtss2sd %xmm3, %xmm0
divss %xmm3, %xmm1
cvtss2sd %xmm1, %xmm1
divss %xmm3, %xmm2
cvtss2sd %xmm2, %xmm2
divss %xmm3, %xmm4
xorps %xmm3, %xmm3
cvtss2sd %xmm4, %xmm3
movq %r13, %rdi
leaq 0x15701(%rip), %rsi     # 0x5dd87
movb $0x4, %al
callq 0x2190
xorl %r14d, %r14d
xorps %xmm0, %xmm0
cvtss2sd -0x74(%rbp,%r14,4), %xmm0
movq (%rbx,%r14,8), %rax
xorps %xmm1, %xmm1
cvtss2sd (%rax), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x4(%rax), %xmm2
xorps %xmm3, %xmm3
cvtss2sd 0x8(%rax), %xmm3
movq %r13, %rdi
movq %r12, %rsi
movb $0x4, %al
callq 0x2190
incq %r14
cmpq $0x3, %r14
jne 0x48690
movl $0xa, %edi
movq %r13, %rsi
callq 0x2140
incq %r15
movq -0x38(%rbp), %r14
cmpq %r14, %r15
jne 0x485cf
leaq 0x95ec(%rip), %rdi      # 0x51cdb
leaq -0x140(%rbp), %rsi
xorl %eax, %eax
callq 0x20f0
movq %rbx, %rdi
movl $0x3, %esi
callq 0x4960c
cmpl $0x0, -0x2c(%rbp)
jle 0x48746
xorl %ebx, %ebx
movq -0x5030(%rbp,%rbx,8), %rdi
callq 0x2050
movq -0x6f70(%rbp,%rbx,8), %rdi
callq 0x2050
movq -0x30f0(%rbp,%rbx,8), %rdi
testq %rdi, %rdi
je 0x4873e
callq 0x2050
incq %rbx
cmpq %rbx, %r14
jne 0x48712
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq