// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/read.c
// Function: Count_residues
int Count_residues(char *pdb_name, char *chain_to_read){
  int N_res=0, Compression;
  FILE *file_in=Open_compressed_file(pdb_name, &Compression);
  if(file_in==NULL)return(0);
  if(ini_modres==0)Ini_modres(); //modyves: parameters were global static variables

  char string[200];
  int hetatm=0;
  int i, res_num, res_num_old=10000;
  char  chain=' ', chain_old='\0';
  char res_type[5], res_type_old[5], icode=' ', icode_old='.';
  float x, y, z;
  int nchains=0, ic, ini_chain=0, readall=0, *rchain=NULL;
  char *xchain=NULL;//modyves: there were two things called "chain"

  char *chain_pdb=CHAIN_CODE;

  // Count chains to read
  if((chain_to_read[0]=='*')||
     ((chain_to_read[0]=='0')&&(chain_to_read[1]=='1'))){
    readall=1;
    printf("Counting residues in ALL chains\n");
  }else{
    i=0; while(chain_to_read[i]!='\0')i++;
    nchains=i;  if(nchains==0)nchains=1;
    rchain=(int *)malloc(nchains*sizeof(int));
    for(i=0; i<nchains; i++)rchain[i]=0;
    printf("Counting residues in %d chains %s\n",
	   nchains, chain_to_read);
  }

  strcpy(res_type_old,"xxx");
  while(fgets(string, sizeof(string), file_in)!=NULL){

    if(strncmp(string,"ATOM", 4)==0){
      /* Standard residue or DNA basis */
      hetatm=0; if(ini_chain==0)ini_chain=1;
    }else if(strncmp(string,"HETATM", 6)==0){
      if(ini_chain==0)continue;
      hetatm=1;
      /* Cofactor or exotic residue */
    }else if((strncmp(string,"TER",3)==0)&&(N_res>0)){
      chain_pdb++; ini_chain=0; continue;
    }else if(strncmp(string,"ENDMDL", 6)==0){
      break;                                    /* end model */
    }else{
      continue;
    }

    chain=string[21];
    if(chain==' ')chain=*chain_pdb;
    if(readall){
      if(chain!=chain_old){
	//chain_to_read[nchains]=chain;
	nchains++; chain_old=chain;
      }
    }else{
      if((chain_to_read[0]==' ')||(chain_to_read[0]=='\0')){
	chain_to_read[0]=chain; chain_to_read[1]='\0';
      }
      for(ic=0; ic<nchains; ic++){
	if(chain==chain_to_read[ic]){rchain[ic]=1; goto read;}
      }
      continue;
    }

  read:    /* Omit hydrogen if HYD=0 */
    if(HYD==0){
      if((string[13]=='H')||(string[12]=='H') ||
	 (string[13]=='D') ||(string[12]=='D'))continue;
    }

    /* Read residue; check if water molecule */
    res_type[0]=string[17]; res_type[1]=string[18]; res_type[2]=string[19];
    res_type[3]='\0';
    if((hetatm==1)&&((strncmp(res_type,"HOH",3)==0)||
		     (strncmp(res_type,"DOD",3)==0)))continue;
    icode=string[26]; string[26]=' ';
    sscanf(string+22,"%d %f %f %f", &res_num, &x, &y, &z);

    /* New residue */
    if((res_num!=res_num_old)||(icode!=icode_old)||
       (strncmp(res_type, res_type_old, 3)!=0)){
      N_res++;
      strcpy(res_type_old, res_type); icode_old=icode; res_num_old=res_num;
    }
  }
  //N_res++;
  fclose(file_in);
  if(Verbose)printf("%3d residues\n", N_res);
  if(Compression)Delete_tmp_file();
  if(readall==0){
    // Count how many chains have been read
    int nc=0;
    xchain=(char *)malloc(nchains*sizeof(char)); //modyves
    for(i=0; i<nchains; i++){
      if(rchain[i]){xchain[nc]=chain_to_read[i]; nc++;}//modyves
      else{printf("WARNING, chain %c not found\n", chain_to_read[i]);}
    }
    for(i=0; i<nc; i++)chain_to_read[i]=xchain[i];
    nchains=nc;//modyves
  }
  chain_to_read[nchains]='\0';
  printf("Counting %3d residues in %d chains: %s\n",N_res, nchains, chain_to_read);
  if(readall==0)//modyves
  	{free(xchain);xchain=NULL; 
   	 free(rchain);rchain=NULL;
  	}
  return(N_res);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rsi, %rbx
leaq 0x3c(%rsp), %rsi
callq 0xe1e8
testq %rax, %rax
je 0xcbd1
movq %rax, %r12
cmpl $0x0, 0x5d30b(%rip)     # 0x69ea0
jne 0xcb9c
callq 0xf9b0
movzbl (%rbx), %eax
cmpl $0x2a, %eax
je 0xcbaf
cmpl $0x30, %eax
jne 0xcbd8
cmpb $0x31, 0x1(%rbx)
jne 0xcbd8
leaq 0x493f6(%rip), %rdi     # 0x55fac
callq 0x2090
xorl %edx, %edx
movq $0x0, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
jmp 0xcc42
xorl %ebp, %ebp
jmp 0xcff9
movq $-0x1, %r13
cmpb $0x0, 0x1(%rbx,%r13)
leaq 0x1(%r13), %r13
jne 0xcbdf
cmpl $0x2, %r13d
movl $0x1, %r14d
cmovael %r13d, %r14d
leaq (,%r14,4), %rdi
callq 0x21d0
cmpq $0x1, %r13
adcq $0x0, %r13
shlq $0x2, %r13
movq %rax, 0x18(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x2110
leaq 0x4903b(%rip), %rdi     # 0x55c66
movl %r14d, %esi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x20f0
movq %r14, %rdx
movb $0x1, %al
movq %rax, 0x20(%rsp)
leaq 0x2c(%rsp), %rax
movl $0x787878, (%rax)       # imm = 0x787878
leaq 0x5d25c(%rip), %rax     # 0x69eb0
movq %rax, 0x48(%rsp)
movb $0x2e, %al
movl $0x2710, %r14d          # imm = 0x2710
leaq 0x60(%rsp), %r13
movl $0x41544548, %r15d      # imm = 0x41544548
xorl %ebp, %ebp
movq $0x0, 0x40(%rsp)
xorl %ecx, %ecx
movb %al, 0xa(%rsp)
movl %r14d, 0x34(%rsp)
movl %ecx, 0x4(%rsp)
movq %rdx, 0x10(%rsp)
movl %edx, %r14d
movq %r13, %rdi
movl $0xc8, %esi
movq %r12, %rdx
callq 0x2170
testq %rax, %rax
je 0xcef7
movl 0x60(%rsp), %eax
cmpl $0x4d4f5441, %eax       # imm = 0x4D4F5441
je 0xcd23
movl 0x60(%rsp), %ecx
xorl %r15d, %ecx
movzwl 0x64(%rsp), %edx
xorl $0x4d54, %edx           # imm = 0x4D54
orl %ecx, %edx
je 0xcd10
testl %ebp, %ebp
setne %al
movl 0x60(%rsp), %ecx
movl $0x4554, %edx           # imm = 0x4554
xorl %edx, %ecx
movzbl 0x62(%rsp), %edx
xorl $0x52, %edx
orw %cx, %dx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
je 0xcd73
movl 0x60(%rsp), %eax
movl $0x4d444e45, %ecx       # imm = 0x4D444E45
xorl %ecx, %eax
movzwl 0x64(%rsp), %ecx
xorl $0x4c44, %ecx           # imm = 0x4C44
orl %eax, %ecx
jne 0xcc8e
jmp 0xcef7
cmpl $0x0, 0x4(%rsp)
movl $0x0, 0x4(%rsp)
je 0xcc8e
movb 0x75(%rsp), %cl
cmpb $0x20, %cl
jne 0xcd33
movq 0x48(%rsp), %rcx
movb (%rcx), %cl
cmpb $0x0, 0x20(%rsp)
je 0xce41
movzbl (%rbx), %edx
testb $-0x21, %dl
jne 0xcd4c
movb %cl, (%rbx)
movb $0x0, 0x1(%rbx)
movl $0x1, 0x4(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0xcc8e
xorl %edx, %edx
cmpb (%rbx,%rdx), %cl
je 0xcd85
incq %rdx
cmpq %rdx, %r14
jne 0xcd61
jmp 0xcc8e
incq 0x48(%rsp)
movl $0x0, 0x4(%rsp)
jmp 0xcc8e
movq 0x18(%rsp), %rcx
movl $0x1, (%rcx,%rdx,4)
movq 0x10(%rsp), %rdx
leaq 0x5c2c3(%rip), %rcx     # 0x69060
cmpl $0x0, (%rcx)
jne 0xcde3
movb 0x6c(%rsp), %cl
movq %rdx, %r8
movb 0x6d(%rsp), %dl
cmpb $0x48, %dl
sete %sil
cmpb $0x48, %cl
sete %dil
orb %sil, %dil
cmpb $0x44, %dl
sete %dl
cmpb $0x44, %cl
sete %cl
orb %dl, %cl
movq %r8, %rdx
orb %dil, %cl
movl $0x1, 0x4(%rsp)
cmpb $0x1, %cl
je 0xcc86
movb 0x71(%rsp), %cl
movb %cl, 0xb(%rsp)
movzwl 0x72(%rsp), %ecx
movw %cx, 0xc(%rsp)
movb $0x0, 0xe(%rsp)
cmpl $0x4d4f5441, %eax       # imm = 0x4D4F5441
je 0xce5e
movzwl 0xb(%rsp), %eax
xorl $0x4f48, %eax           # imm = 0x4F48
movzbl 0xd(%rsp), %ecx
xorl $0x48, %ecx
movl $0x1, 0x4(%rsp)
orw %ax, %cx
je 0xcc86
movzwl 0xb(%rsp), %eax
xorl $0x4f44, %eax           # imm = 0x4F44
movzbl 0xd(%rsp), %ecx
xorl $0x44, %ecx
orw %ax, %cx
je 0xcc86
jmp 0xce5e
xorl %edx, %edx
cmpb 0x40(%rsp), %cl
setne %dl
movq 0x10(%rsp), %rsi
addl %edx, %esi
movq %rsi, %rdx
movq %rcx, 0x40(%rsp)
jmp 0xcd96
movb 0x7a(%rsp), %al
movb %al, 0x4(%rsp)
movb $0x20, 0x7a(%rsp)
leaq 0x76(%rsp), %rdi
leaq 0x48e2c(%rip), %rsi     # 0x55ca3
movq %rdx, 0x10(%rsp)
leaq 0x38(%rsp), %rdx
leaq 0x5c(%rsp), %rcx
leaq 0x58(%rsp), %r8
leaq 0x54(%rsp), %r9
xorl %eax, %eax
callq 0x21f0
movl 0x38(%rsp), %r14d
cmpl 0x34(%rsp), %r14d
jne 0xced3
movb 0x4(%rsp), %al
cmpb 0xa(%rsp), %al
jne 0xced3
movl $0x3, %edx
leaq 0xb(%rsp), %rdi
leaq 0x2c(%rsp), %rsi
callq 0x2070
movq 0x10(%rsp), %rdx
movl $0x1, %ecx
testl %eax, %eax
je 0xcc82
incl %ebp
leaq 0x2c(%rsp), %rdi
leaq 0xb(%rsp), %rsi
callq 0x2080
movq 0x10(%rsp), %rdx
movl $0x1, %ecx
movb 0x4(%rsp), %al
jmp 0xcc79
movq %r12, %rdi
callq 0x20c0
leaq 0x5c1f2(%rip), %rax     # 0x690f8
cmpl $0x0, (%rax)
je 0xcf1b
leaq 0x48d9d(%rip), %rdi     # 0x55caf
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
cmpl $0x0, 0x3c(%rsp)
je 0xcf4a
leaq 0x48ec5(%rip), %rsi     # 0x55dee
leaq 0x48ea0(%rip), %rdx     # 0x55dd0
leaq 0x130(%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0x2260
movq %r12, %rdi
callq 0x20e0
xorl %r13d, %r13d
cmpb $0x0, 0x20(%rsp)
je 0xcfbe
movq 0x10(%rsp), %r15
movslq %r15d, %rdi
callq 0x21d0
movq %rax, %r12
testl %r15d, %r15d
movq 0x18(%rsp), %rcx
jle 0xcfa9
xorl %r15d, %r15d
xorl %r13d, %r13d
cmpl $0x0, (%rcx,%r15,4)
movb (%rbx,%r15), %al
je 0xcf8b
movslq %r13d, %r13
movb %al, (%r12,%r13)
incl %r13d
jmp 0xcfa1
movsbl %al, %esi
leaq 0x48d28(%rip), %rdi     # 0x55cbd
xorl %eax, %eax
callq 0x20f0
movq 0x18(%rsp), %rcx
incq %r15
cmpq %r15, %r14
jne 0xcf74
testl %r13d, %r13d
jle 0xcfbc
movl %r13d, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x21a0
jmp 0xcfc6
xorl %r12d, %r12d
movq 0x10(%rsp), %r13
movslq %r13d, %rdx
movb $0x0, (%rbx,%rdx)
leaq 0x48d06(%rip), %rdi     # 0x55cda
movl %ebp, %esi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x20f0
cmpb $0x0, 0x20(%rsp)
je 0xcff9
movq %r12, %rdi
callq 0x2050
movq 0x18(%rsp), %rdi
callq 0x2050
movl %ebp, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq