// File: /workspace/github_c_makefile_star2_O3/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 0xe1e3
testq %rax, %rax
je 0xcc0a
movq %rax, %r12
cmpl $0x0, 0x5d2d2(%rip)     # 0x69ea0
jne 0xcbd5
callq 0xf66a
movzbl (%rbx), %eax
cmpl $0x2a, %eax
je 0xcbe8
cmpl $0x30, %eax
jne 0xcc11
cmpb $0x31, 0x1(%rbx)
jne 0xcc11
leaq 0x49398(%rip), %rdi     # 0x55f87
callq 0x2090
xorl %edx, %edx
movq $0x0, 0x28(%rsp)
movq $0x0, 0x18(%rsp)
jmp 0xcc7b
xorl %ebp, %ebp
jmp 0xd036
movq $-0x1, %r13
cmpb $0x0, 0x1(%rbx,%r13)
leaq 0x1(%r13), %r13
jne 0xcc18
cmpl $0x2, %r13d
movl $0x1, %r14d
cmovael %r13d, %r14d
leaq (,%r14,4), %rdi
callq 0x21e0
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 0x4904a(%rip), %rdi     # 0x55cae
movl %r14d, %esi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x20f0
movq %r14, %rdx
movb $0x1, %al
movq %rax, 0x28(%rsp)
leaq 0x20(%rsp), %rax
movl $0x787878, (%rax)       # imm = 0x787878
leaq 0x5d223(%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 0xcf27
movl 0x60(%rsp), %eax
cmpl $0x4d4f5441, %eax       # imm = 0x4D4F5441
je 0xcd5c
movl 0x60(%rsp), %ecx
xorl %r15d, %ecx
movzwl 0x64(%rsp), %edx
xorl $0x4d54, %edx           # imm = 0x4D54
orl %ecx, %edx
je 0xcd49
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 0xcdac
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 0xccc7
jmp 0xcf27
cmpl $0x0, 0x4(%rsp)
movl $0x0, 0x4(%rsp)
je 0xccc7
movb 0x75(%rsp), %cl
cmpb $0x20, %cl
jne 0xcd6c
movq 0x48(%rsp), %rcx
movb (%rcx), %cl
cmpb $0x0, 0x28(%rsp)
je 0xce71
movzbl (%rbx), %edx
testb $-0x21, %dl
jne 0xcd85
movb %cl, (%rbx)
movb $0x0, 0x1(%rbx)
movl $0x1, 0x4(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0xccc7
xorl %edx, %edx
cmpb (%rbx,%rdx), %cl
je 0xcdbe
incq %rdx
cmpq %rdx, %r14
jne 0xcd9a
jmp 0xccc7
incq 0x48(%rsp)
movl $0x0, 0x4(%rsp)
jmp 0xccc7
movq 0x18(%rsp), %rcx
movl $0x1, (%rcx,%rdx,4)
movq 0x10(%rsp), %rdx
leaq 0x5c28a(%rip), %rcx     # 0x69060
cmpl $0x0, (%rcx)
je 0xce3d
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 0xce8e
movzwl 0xb(%rsp), %eax
xorl $0x4f48, %eax           # imm = 0x4F48
movzbl 0xd(%rsp), %ecx
xorl $0x48, %ecx
movl $0x1, 0x4(%rsp)
orw %ax, %cx
je 0xccbf
movzwl 0xb(%rsp), %eax
xorl $0x4f44, %eax           # imm = 0x4F44
movzbl 0xd(%rsp), %ecx
xorl $0x44, %ecx
orw %ax, %cx
je 0xccbf
jmp 0xce8e
movd 0x6c(%rsp), %xmm0
punpcklbw %xmm0, %xmm0    # xmm0 = xmm0[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7]
pshuflw $0x5, %xmm0, %xmm0      # xmm0 = xmm0[1,1,0,0,4,5,6,7]
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
pcmpeqb 0x48d57(%rip), %xmm0    # 0x55bb0
movmskps %xmm0, %ecx
movl $0x1, 0x4(%rsp)
testl %ecx, %ecx
jne 0xccbf
jmp 0xcddb
xorl %edx, %edx
cmpb 0x40(%rsp), %cl
setne %dl
movq 0x10(%rsp), %rsi
addl %edx, %esi
movq %rsi, %rdx
movq %rcx, 0x40(%rsp)
jmp 0xcdcf
movb 0x7a(%rsp), %al
movb %al, 0x4(%rsp)
movb $0x20, 0x7a(%rsp)
leaq 0x76(%rsp), %rdi
leaq 0x48e44(%rip), %rsi     # 0x55ceb
movq %rdx, 0x10(%rsp)
leaq 0x38(%rsp), %rdx
leaq 0x5c(%rsp), %rcx
leaq 0x58(%rsp), %r8
leaq 0x54(%rsp), %r9
xorl %eax, %eax
callq 0x2200
movl 0x38(%rsp), %r14d
cmpl 0x34(%rsp), %r14d
jne 0xcf03
movb 0x4(%rsp), %al
cmpb 0xa(%rsp), %al
jne 0xcf03
movl $0x3, %edx
leaq 0xb(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x2070
movq 0x10(%rsp), %rdx
movl $0x1, %ecx
testl %eax, %eax
je 0xccbb
incl %ebp
leaq 0x20(%rsp), %rdi
leaq 0xb(%rsp), %rsi
callq 0x2080
movq 0x10(%rsp), %rdx
movl $0x1, %ecx
movb 0x4(%rsp), %al
jmp 0xccb2
movq %r12, %rdi
callq 0x20c0
leaq 0x5c1c2(%rip), %rax     # 0x690f8
cmpl $0x0, (%rax)
je 0xcf4b
leaq 0x48db5(%rip), %rdi     # 0x55cf7
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
cmpl $0x0, 0x3c(%rsp)
je 0xcf7a
leaq 0x48edd(%rip), %rsi     # 0x55e36
leaq 0x48eb8(%rip), %rdx     # 0x55e18
leaq 0x130(%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0x2270
movq %r12, %rdi
callq 0x20e0
movq 0x10(%rsp), %rdx
movslq %edx, %rdi
cmpb $0x0, 0x28(%rsp)
je 0xcfee
movq %rdx, %r15
callq 0x21e0
movq %rax, %r12
testl %r15d, %r15d
jle 0xd007
xorl %r15d, %r15d
xorl %r13d, %r13d
movq 0x18(%rsp), %rcx
cmpl $0x0, (%rcx,%r15,4)
movb (%rbx,%r15), %al
je 0xcfbb
movslq %r13d, %r13
movb %al, (%r12,%r13)
incl %r13d
jmp 0xcfd1
movsbl %al, %esi
leaq 0x48d40(%rip), %rdi     # 0x55d05
xorl %eax, %eax
callq 0x20f0
movq 0x18(%rsp), %rcx
incq %r15
cmpq %r15, %r14
jne 0xcfa4
testl %r13d, %r13d
jle 0xd00a
movl %r13d, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x21b0
jmp 0xd00a
movb $0x0, (%rbx,%rdi)
leaq 0x48d29(%rip), %rdi     # 0x55d22
movl %ebp, %esi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x20f0
jmp 0xd036
xorl %r13d, %r13d
movslq %r13d, %rdx
movb $0x0, (%rbx,%rdx)
leaq 0x48d0a(%rip), %rdi     # 0x55d22
movl %ebp, %esi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x20f0
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