// File: /workspace/github_c_makefile_star2_O3/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 0x15839(%rip), %rdi     # 0x5dc7f
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 0x487e7
addq $0x40, %rsp
movl %eax, %r14d
leaq 0x151cd(%rip), %rdi     # 0x5d669
movl %eax, %esi
xorl %eax, %eax
callq 0x20f0
testl %r14d, %r14d
je 0x487d8
movl %r14d, -0x2c(%rbp)
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 0x44304
addq $0x40, %rsp
movl 0x110(%rbp), %esi
cmpl %esi, %eax
je 0x48557
leaq 0x1576d(%rip), %rdi     # 0x5dcaf
movl %eax, %edx
xorl %eax, %eax
callq 0x20f0
leaq 0x15a3c(%rip), %rdi     # 0x5df8e
callq 0x2090
leaq 0x15782(%rip), %rsi     # 0x5dce0
leaq -0x140(%rbp), %rbx
movq %rbx, %rdi
movq -0x38(%rbp), %rdx
xorl %eax, %eax
callq 0x2270
leaq 0xec86(%rip), %rsi      # 0x57200
movq %rbx, %rdi
callq 0x2220
movq %rax, %rbx
leaq 0x1513c(%rip), %rsi     # 0x5d6c8
leaq -0x210(%rbp), %rdx
movq %rax, %rdi
xorl %eax, %eax
callq 0x21a0
movl $0x3, %edi
movl $0x3, %esi
callq 0x4971a
movq %rax, %r13
leaq 0x15740(%rip), %rdi     # 0x5dcf6
movl $0x28, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x2290
movl $0x5, %r15d
leaq 0x1574a(%rip), %r12     # 0x5dd1f
xorl %r14d, %r14d
leal 0x1(%r15), %r8d
leal 0x2(%r15), %r10d
leal 0x3(%r15), %r11d
movq %rbx, %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 0x21a0
addq $0x20, %rsp
incl %r14d
leal 0x4(%r15), %eax
cmpl $0xd, %r15d
movl %eax, %r15d
jne 0x485d8
movl $0xa, %edi
movq %rbx, %rsi
callq 0x2140
movl -0x2c(%rbp), %eax
testl %eax, %eax
jle 0x487b6
movl %eax, %eax
movq %rax, -0x38(%rbp)
leaq 0x15733(%rip), %r12     # 0x5dd6e
xorl %r15d, %r15d
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 %r13, %rdx
movq %rax, %rcx
callq 0x48a71
addq $0xe0, %rsp
movq -0x30f0(%rbp,%r15,8), %rdx
movq %rbx, %rdi
leaq 0x156a4(%rip), %rsi     # 0x5dd4c
xorl %eax, %eax
callq 0x21a0
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 %rbx, %rdi
leaq 0x15662(%rip), %rsi     # 0x5dd57
movb $0x4, %al
callq 0x21a0
xorl %r14d, %r14d
xorps %xmm0, %xmm0
cvtss2sd -0x74(%rbp,%r14,4), %xmm0
movq (%r13,%r14,8), %rax
xorps %xmm1, %xmm1
cvtss2sd (%rax), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x4(%rax), %xmm2
xorps %xmm3, %xmm3
cvtss2sd 0x8(%rax), %xmm3
movq %rbx, %rdi
movq %r12, %rsi
movb $0x4, %al
callq 0x21a0
incq %r14
cmpq $0x3, %r14
jne 0x486ff
movl $0xa, %edi
movq %rbx, %rsi
callq 0x2140
incq %r15
cmpq -0x38(%rbp), %r15
jne 0x4863e
leaq 0x95f3(%rip), %rdi      # 0x51d4f
leaq -0x140(%rbp), %rsi
xorl %eax, %eax
callq 0x20f0
movq %r13, %rdi
movl $0x3, %esi
callq 0x49678
cmpl $0x0, -0x2c(%rbp)
jle 0x487d8
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 0x487ab
callq 0x2050
incq %rbx
cmpq %rbx, -0x38(%rbp)
jne 0x4877f
jmp 0x487d8
leaq 0x9592(%rip), %rdi      # 0x51d4f
leaq -0x140(%rbp), %rsi
xorl %eax, %eax
callq 0x20f0
movq %r13, %rdi
movl $0x3, %esi
callq 0x49678
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq