// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/site_dynamics.c
// Function: Read_sites
int Read_sites(int **site_res, int *nres_site, char **chain_res,
	       char **description, char *file_site,
	       int NMAX, char *SITES, char *pdb, char *chain, int Nchain,
	       struct residue *seq, int Nres, int PRINT_SITE)
{
  int SMAX=400, N_sites=0, i;
  for(i=0; i<NMAX; i++){
    site_res[i]=malloc(SMAX*sizeof(int));
    chain_res[i]=malloc(SMAX*sizeof(char));
    description[i]=NULL;
  }
  if(SITES[0]!='\0'){
    N_sites=Read_sites_file(site_res, chain_res, nres_site, description,
			    SITES, chain, seq, Nres, NMAX, SMAX);
    if(N_sites)strcpy(file_site, SITES);
  }
  if(N_sites==0){
    N_sites=Read_sites_PDB(site_res, chain_res, nres_site, description,
			   pdb, chain, seq, Nres, NMAX, SMAX);
    if(N_sites)strcpy(file_site, pdb);
    // Print site
    if(PRINT_SITE && N_sites)
      Print_sites(site_res, nres_site, description, N_sites,
		  seq, pdb, chain);
  }
  if(N_sites==0)return(0);
  for(i=0; i<N_sites; i++){
    if(description[i]==NULL){
      description[i]=malloc(20*sizeof(char));
      sprintf(description[i], "site%d", i+1);
    }else{
      char *ptr=description[i];
      while(*ptr!='\0'){
	if(*ptr=='\n'){*ptr='\0'; break;} ptr++;
      } 
    }
  }

  // Eliminate sites that are not in the selected chains 
  int S=0;
  for(i=0; i<N_sites; i++){
    if(nres_site[i]<=0)continue;
    int n=0;
    for(int j=0; j<nres_site[i]; j++){
      char chj=chain_res[i][j]; int nc;
      for(nc=0; nc<Nchain; nc++){
	if(chj==chain[nc]){n++; break;}
      }
    }
    if(n==nres_site[i]){
      if(S!=i){
	nres_site[S]=nres_site[i];
	strcpy(description[S], description[i]);
	for(int j=0; j<nres_site[i]; j++){
	  site_res[S][j]=site_res[i][j];
	  chain_res[S][j]=chain_res[i][j];
	}
      }
      S++;
    }
  }
  return(S);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %rdx, %r14
movq %rsi, 0x8(%rsp)
movq %rdi, %r12
movl 0x78(%rsp), %ebp
movq 0x70(%rsp), %r13
xorl %ebx, %ebx
testl %r9d, %r9d
movl $0x0, %r15d
movq %r9, 0x20(%rsp)
cmovgl %r9d, %r15d
cmpq %rbx, %r15
je 0x42945
movl $0x640, %edi            # imm = 0x640
callq 0x21d0
movq %rax, (%r12,%rbx,8)
movl $0x190, %edi            # imm = 0x190
callq 0x21d0
movq %rax, (%r14,%rbx,8)
movq 0x10(%rsp), %rax
andq $0x0, (%rax,%rbx,8)
incq %rbx
jmp 0x42915
movq 0x60(%rsp), %rbx
cmpb $0x0, (%rbx)
je 0x4299f
movq %r12, %rdi
movq %r14, %rsi
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %rbx, %r8
movq %r13, %r9
pushq $0x190                  # imm = 0x190
pushq 0x28(%rsp)
movl 0x98(%rsp), %eax
pushq %rax
pushq 0x98(%rsp)
callq 0x42dae
addq $0x20, %rsp
testl %eax, %eax
je 0x4299f
movl %eax, %r15d
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x2080
jmp 0x42a2f
movq %r12, %rdi
movq %r14, %rsi
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq 0x68(%rsp), %rbx
movq %rbx, %r8
movq %r13, %r9
pushq $0x190                  # imm = 0x190
pushq 0x28(%rsp)
movl 0x98(%rsp), %eax
pushq %rax
pushq 0x98(%rsp)
callq 0x43117
addq $0x20, %rsp
testl %eax, %eax
je 0x42b5b
movl %eax, %r15d
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x2080
cmpl $0x0, 0x90(%rsp)
je 0x42a2f
subq $0x8, %rsp
movq %r12, %rdi
movq %rbx, %r9
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movq 0x18(%rsp), %rdx
movl %r15d, %ecx
movq 0x88(%rsp), %r8
pushq %r13
callq 0x4240e
movq %rbx, %rsi
addq $0x10, %rsp
jmp 0x42a34
movq 0x8(%rsp), %rsi
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
xorl %ebx, %ebx
cmpq %r15, %rbx
je 0x42a97
movq 0x10(%rsp), %rax
movq (%rax,%rbx,8), %rax
testq %rax, %rax
je 0x42a63
movzbl (%rax), %ecx
testl %ecx, %ecx
je 0x42a92
cmpl $0xa, %ecx
je 0x42a8f
incq %rax
jmp 0x42a52
pushq $0x14
popq %rdi
callq 0x21d0
movq 0x10(%rsp), %rcx
movq %rax, (%rcx,%rbx,8)
leal 0x1(%rbx), %edx
movq %rax, %rdi
leaq 0x15276(%rip), %rsi     # 0x57cf7
xorl %eax, %eax
callq 0x2260
movq 0x8(%rsp), %rsi
jmp 0x42a92
movb $0x0, (%rax)
incq %rbx
jmp 0x42a3f
xorl %ebx, %ebx
testl %ebp, %ebp
cmovlel %ebx, %ebp
xorl %eax, %eax
movq %r15, 0x20(%rsp)
cmpq %r15, %rbx
je 0x42b5d
movslq (%rsi,%rbx,4), %r10
testq %r10, %r10
jle 0x42b04
movq (%r14,%rbx,8), %rcx
xorl %edx, %edx
xorl %esi, %esi
cmpq %r10, %rdx
je 0x42ae5
movb (%rcx,%rdx), %dil
xorl %r8d, %r8d
cmpq %r8, %rbp
je 0x42ae0
leaq 0x1(%r8), %r9
cmpb (%r13,%r8), %dil
movq %r9, %r8
jne 0x42acb
incl %esi
incq %rdx
jmp 0x42abf
cmpl %r10d, %esi
jne 0x42aff
movl %eax, %ecx
cmpq %rcx, %rbx
movq 0x8(%rsp), %rsi
jne 0x42b09
incl %eax
movq 0x20(%rsp), %r15
jmp 0x42b04
movq 0x8(%rsp), %rsi
incq %rbx
jmp 0x42aa5
movslq %eax, %r15
movl %r10d, (%rsi,%r15,4)
movq 0x10(%rsp), %rcx
movq (%rcx,%r15,8), %rdi
movq (%rcx,%rbx,8), %rsi
movl %eax, 0x18(%rsp)
callq 0x2080
movl 0x18(%rsp), %eax
movq 0x8(%rsp), %rsi
xorl %edi, %edi
movslq (%rsi,%rbx,4), %rcx
cmpq %rcx, %rdi
jge 0x42af6
movq (%r12,%rbx,8), %rcx
movl (%rcx,%rdi,4), %ecx
movq (%r12,%r15,8), %rdx
movl %ecx, (%rdx,%rdi,4)
movq (%r14,%rbx,8), %rcx
movb (%rcx,%rdi), %cl
movq (%r14,%r15,8), %rdx
movb %cl, (%rdx,%rdi)
incq %rdi
jmp 0x42b31
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq