// File: /workspace/github_c_makefile_star2_O2/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, %r14d
movq %r8, %r15
movq %rcx, %rbx
movq %rdx, -0x50(%rbp)
movq %rsi, -0x48(%rbp)
movq %rdi, %r12
movl 0x110(%rbp), %r13d
leaq 0x15665(%rip), %rdi     # 0x57bef
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 %r12
pushq %r14
pushq %r15
pushq %rbx
pushq 0x118(%rbp)
callq 0x428d5
addq $0x40, %rsp
movl %eax, %ebx
leaq 0x14ffa(%rip), %rdi     # 0x575d9
movl %eax, %esi
xorl %eax, %eax
callq 0x20f0
testl %ebx, %ebx
je 0x428c6
movq %rbx, -0x30(%rbp)
leaq 0xe0(%rbp), %rcx
leaq -0x68(%rbp), %rax
movl $0x4143, (%rax)         # imm = 0x4143
leaq -0x64(%rbp), %r8
andl $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 %r12, %rcx
movq -0x48(%rbp), %r9
callq 0x3eb54
addq $0x40, %rsp
movl 0x110(%rbp), %esi
cmpl %esi, %eax
je 0x42696
leaq 0x1559e(%rip), %rdi     # 0x57c1f
movl %eax, %edx
xorl %eax, %eax
callq 0x20f0
leaq 0x1586d(%rip), %rdi     # 0x57efe
callq 0x2090
leaq 0x155b3(%rip), %rsi     # 0x57c50
leaq -0x140(%rbp), %rbx
movq %rbx, %rdi
movq -0x50(%rbp), %rdx
xorl %eax, %eax
callq 0x2260
leaq 0xea97(%rip), %rsi      # 0x51150
movq %rbx, %rdi
callq 0x2210
movq %rax, %r13
leaq 0x14f6d(%rip), %rsi     # 0x57638
leaq -0x210(%rbp), %rdx
movq %rax, %rdi
xorl %eax, %eax
callq 0x2190
pushq $0x3
popq %rdi
movl %edi, %esi
callq 0x4379e
movq %rax, %r12
leaq 0x15576(%rip), %rdi     # 0x57c66
pushq $0x28
popq %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x2280
pushq $0x5
popq %r14
leaq 0x15586(%rip), %r15     # 0x57c8f
xorl %ebx, %ebx
cmpl $0x11, %r14d
je 0x42744
leal 0x1(%r14), %r8d
leal 0x2(%r14), %r10d
leal 0x3(%r14), %r11d
movq %r13, %rdi
movq %r15, %rsi
movl %r14d, %edx
movl %ebx, %ecx
movl %ebx, %r9d
xorl %eax, %eax
pushq %rbx
pushq %r11
pushq %rbx
pushq %r10
callq 0x2190
addq $0x20, %rsp
incl %ebx
addl $0x4, %r14d
jmp 0x4270b
pushq $0xa
popq %rdi
movq %r13, %rsi
callq 0x2140
xorl %eax, %eax
movq -0x30(%rbp), %r15
testl %r15d, %r15d
cmovlel %eax, %r15d
leaq 0x1557b(%rip), %r14     # 0x57cde
xorl %ebx, %ebx
movq %r15, -0x30(%rbp)
cmpq %r15, %rbx
je 0x42873
movq -0x5030(%rbp,%rbx,8), %rax
movl -0x11b0(%rbp,%rbx,4), %r8d
subq $0xe0, %rsp
pushq $0x1a
popq %rcx
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 -0x3c(%rbp), %rdi
leaq -0x74(%rbp), %rsi
movq %r12, %rdx
movq %rax, %rcx
callq 0x42b6c
addq $0xe0, %rsp
movq -0x30f0(%rbp,%rbx,8), %rdx
movq %r13, %rdi
leaq 0x154e2(%rip), %rsi     # 0x57cbc
xorl %eax, %eax
callq 0x2190
movss -0x3c(%rbp), %xmm1
movss -0x38(%rbp), %xmm2
movaps %xmm1, %xmm3
addss %xmm2, %xmm3
movss -0x34(%rbp), %xmm4
addss %xmm4, %xmm3
cvtss2sd %xmm3, %xmm0
divss %xmm3, %xmm1
cvtss2sd %xmm1, %xmm1
divss %xmm3, %xmm2
cvtss2sd %xmm2, %xmm2
divss %xmm3, %xmm4
cvtss2sd %xmm4, %xmm3
movq %r13, %rdi
leaq 0x154a6(%rip), %rsi     # 0x57cc7
movb $0x4, %al
callq 0x2190
xorl %r15d, %r15d
cmpq $0x3, %r15
je 0x4285c
cvtss2sd -0x74(%rbp,%r15,4), %xmm0
movq (%r12,%r15,8), %rax
cvtss2sd (%rax), %xmm1
cvtss2sd 0x4(%rax), %xmm2
cvtss2sd 0x8(%rax), %xmm3
movq %r13, %rdi
movq %r14, %rsi
movb $0x4, %al
callq 0x2190
incq %r15
jmp 0x4282b
pushq $0xa
popq %rdi
movq %r13, %rsi
callq 0x2140
incq %rbx
movq -0x30(%rbp), %r15
jmp 0x42769
leaq 0x942d(%rip), %rdi      # 0x4bca7
xorl %ebx, %ebx
leaq -0x140(%rbp), %rsi
xorl %eax, %eax
callq 0x20f0
pushq $0x3
popq %rsi
movq %r12, %rdi
callq 0x436f0
cmpq %rbx, %r15
je 0x428c6
movq -0x5030(%rbp,%rbx,8), %rdi
callq 0x2050
movq -0x6f70(%rbp,%rbx,8), %rdi
callq 0x2050
movq -0x30f0(%rbp,%rbx,8), %rdi
callq 0x2050
incq %rbx
jmp 0x42895
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq