// File: /workspace/github_c_makefile_star2_O1/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
movl %r9d, %ebp
movq %r8, 0x8(%rsp)
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, (%rsp)
movq %rdi, %r13
testl %r9d, %r9d
jle 0x487af
movl %ebp, %ebx
xorl %r15d, %r15d
movl $0x640, %edi            # imm = 0x640
callq 0x21d0
movq %rax, (%r13,%r15,8)
movl $0x190, %edi            # imm = 0x190
callq 0x21d0
movq %rax, (%r14,%r15,8)
movq $0x0, (%r12,%r15,8)
incq %r15
cmpq %r15, %rbx
jne 0x48782
movq %r13, 0x18(%rsp)
movl 0x88(%rsp), %eax
movq 0x70(%rsp), %r13
xorl %r15d, %r15d
movq 0x60(%rsp), %rbx
cmpb $0x0, (%rbx)
movq %r12, 0x10(%rsp)
je 0x4881e
movq 0x18(%rsp), %rdi
movq %r14, %rsi
movq (%rsp), %rdx
movq %r12, %rcx
movq %rbx, %r8
movq %r13, %r9
pushq $0x190                  # imm = 0x190
movq %rbp, %r12
pushq %rbp
movq %rax, %rbx
pushq %rax
pushq 0x98(%rsp)
callq 0x48c5e
addq $0x20, %rsp
testl %eax, %eax
je 0x48824
movl %eax, %ebp
movq 0x8(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x2080
movl %ebp, %r15d
jmp 0x48824
movq %rax, %rbx
movq %rbp, %r12
testl %r15d, %r15d
movq (%rsp), %rdx
movq 0x18(%rsp), %rdi
jne 0x488c3
movl 0x90(%rsp), %ebp
movq 0x68(%rsp), %r8
movq %r14, %rsi
movq 0x10(%rsp), %rcx
movq %r13, %r9
pushq $0x190                  # imm = 0x190
pushq %r12
movq %r8, %r12
pushq %rbx
pushq 0x98(%rsp)
callq 0x48fcd
addq $0x20, %rsp
movl %eax, %r15d
testl %eax, %eax
je 0x4887c
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x2080
testl %r15d, %r15d
sete %al
testl %ebp, %ebp
sete %cl
orb %al, %cl
jne 0x488bf
subq $0x8, %rsp
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rsi
movq 0x18(%rsp), %rdx
movl %r15d, %ecx
movq 0x88(%rsp), %r8
movq %r12, %r9
pushq %r13
callq 0x48239
movq %rbx, %rdx
addq $0x10, %rsp
jmp 0x488c3
movq (%rsp), %rdx
xorl %r12d, %r12d
testl %r15d, %r15d
je 0x48a04
movl %r15d, %ebp
movl %r15d, %r10d
movq 0x10(%rsp), %r15
movq %r10, 0x20(%rsp)
jle 0x48935
xorl %ebx, %ebx
movq (%r15,%rbx,8), %rax
testq %rax, %rax
je 0x488fd
movzbl (%rax), %ecx
testl %ecx, %ecx
je 0x4892d
cmpl $0xa, %ecx
je 0x4892a
incq %rax
jmp 0x488ec
movl $0x14, %edi
callq 0x21d0
movq %rax, (%r15,%rbx,8)
leal 0x1(%rbx), %edx
movq %rax, %rdi
leaq 0x1549f(%rip), %rsi     # 0x5ddb7
xorl %eax, %eax
callq 0x2260
movq 0x20(%rsp), %r10
movq (%rsp), %rdx
jmp 0x4892d
movb $0x0, (%rax)
incq %rbx
cmpq %r10, %rbx
jne 0x488e3
testl %ebp, %ebp
jle 0x48a04
movl 0x78(%rsp), %ebp
movl %ebp, %ebx
xorl %r15d, %r15d
xorl %r12d, %r12d
movslq (%rdx,%r15,4), %rax
testq %rax, %rax
jle 0x489f8
movq (%r14,%r15,8), %rcx
xorl %r9d, %r9d
xorl %esi, %esi
testl %ebp, %ebp
jle 0x4897d
movb (%rcx,%r9), %dil
xorl %r8d, %r8d
cmpb (%r13,%r8), %dil
je 0x4897b
incq %r8
cmpq %r8, %rbx
jne 0x4896a
jmp 0x4897d
incl %esi
incq %r9
cmpq %rax, %r9
jne 0x4895f
cmpl %eax, %esi
jne 0x489f8
movl %r12d, 0x8(%rsp)
movl %r12d, %ecx
cmpq %rcx, %r15
je 0x489f0
movslq 0x8(%rsp), %r12
movl %eax, (%rdx,%r12,4)
movq 0x10(%rsp), %rax
movq (%rax,%r12,8), %rdi
movq (%rax,%r15,8), %rsi
callq 0x2080
movq 0x20(%rsp), %r10
movq 0x18(%rsp), %rdi
movq (%rsp), %rdx
cmpl $0x0, (%rdx,%r15,4)
jle 0x489f0
xorl %eax, %eax
movq (%rdi,%r15,8), %rcx
movl (%rcx,%rax,4), %ecx
movq (%rdi,%r12,8), %rsi
movl %ecx, (%rsi,%rax,4)
movq (%r14,%r15,8), %rcx
movb (%rcx,%rax), %cl
movq (%r14,%r12,8), %rsi
movb %cl, (%rsi,%rax)
incq %rax
movslq (%rdx,%r15,4), %rcx
cmpq %rcx, %rax
jl 0x489c8
movl 0x8(%rsp), %r12d
incl %r12d
incq %r15
cmpq %r10, %r15
jne 0x48949
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq