// File: /workspace/github_c_makefile_star2_O2/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 $0x128, %rsp            # imm = 0x128
movq %rsi, %rbx
leaq 0x34(%rsp), %rsi
callq 0xd804
movq %rax, 0x48(%rsp)
testq %rax, %rax
je 0xc378
cmpl $0x0, 0x58b5f(%rip)     # 0x64ea0
jne 0xc348
callq 0xec8d
movzbl (%rbx), %eax
cmpl $0x2a, %eax
je 0xc35b
cmpl $0x30, %eax
jne 0xc380
cmpb $0x31, 0x1(%rbx)
jne 0xc380
leaq 0x43b75(%rip), %rdi     # 0x4fed7
callq 0x2090
xorl %r15d, %r15d
movq $0x0, 0x18(%rsp)
xorl %r14d, %r14d
jmp 0xc3e3
xorl %r13d, %r13d
jmp 0xc715
pushq $-0x1
popq %r12
cmpb $0x0, 0x1(%rbx,%r12)
leaq 0x1(%r12), %r12
jne 0xc384
cmpl $0x2, %r12d
pushq $0x1
popq %rax
movl %eax, %r15d
cmovael %r12d, %r15d
leaq (,%r15,4), %rdi
callq 0x21d0
movq %rax, %r14
cmpq $0x1, %r12
adcq $0x0, %r12
xorl %eax, %eax
cmpq %rax, %r12
je 0xc3c8
andl $0x0, (%r14,%rax,4)
incq %rax
jmp 0xc3b9
leaq 0x4382f(%rip), %rdi     # 0x4fbfe
movl %r15d, %esi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x20f0
movb $0x1, %al
movq %rax, 0x18(%rsp)
leaq 0x24(%rsp), %rax
movl $0x787878, (%rax)       # imm = 0x787878
leaq 0x58abb(%rip), %rax     # 0x64eb0
movq %rax, 0x40(%rsp)
movb $0x2e, %bpl
movl $0x2710, %r12d          # imm = 0x2710
leaq 0x60(%rsp), %r13
movl $0x0, 0x14(%rsp)
movq $0x0, 0x38(%rsp)
xorl %ecx, %ecx
movb %bpl, 0xa(%rsp)
movl %r12d, 0x2c(%rsp)
movl %ecx, 0x10(%rsp)
testl %r15d, %r15d
movl $0x0, %ebp
cmovgl %r15d, %ebp
movq %r13, %rdi
movl $0xc8, %esi
movq 0x48(%rsp), %rdx
callq 0x2170
testq %rax, %rax
je 0xc65e
movl 0x60(%rsp), %r12d
cmpl $0x4d4f5441, %r12d      # imm = 0x4D4F5441
je 0xc4c9
movq %r13, %rdi
leaq 0x437b9(%rip), %rsi     # 0x4fc21
pushq $0x6
popq %rdx
callq 0x2200
testl %eax, %eax
je 0xc4b6
cmpl $0x0, 0x14(%rsp)
setne %r12b
movq %r13, %rdi
leaq 0x437a1(%rip), %rsi     # 0x4fc28
pushq $0x3
popq %rdx
callq 0x2200
testl %eax, %eax
sete %al
andb %r12b, %al
cmpb $0x1, %al
je 0xc506
movq %r13, %rdi
leaq 0x43787(%rip), %rsi     # 0x4fc2c
pushq $0x6
popq %rdx
callq 0x2200
testl %eax, %eax
jne 0xc435
jmp 0xc65e
cmpl $0x0, 0x10(%rsp)
movl $0x0, 0x10(%rsp)
je 0xc435
movb 0x75(%rsp), %al
cmpb $0x20, %al
jne 0xc4d8
movq 0x40(%rsp), %rax
movb (%rax), %al
cmpb $0x0, 0x18(%rsp)
je 0xc62d
movzbl (%rbx), %ecx
testb $-0x21, %cl
jne 0xc4f1
movb %al, (%rbx)
movb $0x0, 0x1(%rbx)
xorl %ecx, %ecx
cmpq %rcx, %rbp
je 0xc518
leaq 0x1(%rcx), %rdx
cmpb (%rbx,%rcx), %al
movq %rdx, %rcx
jne 0xc4f3
jmp 0xc524
incq 0x40(%rsp)
movl $0x0, 0x10(%rsp)
jmp 0xc435
pushq $0x1
popq %rax
movl %eax, 0x10(%rsp)
jmp 0xc435
movl $0x1, -0x4(%r14,%rdx,4)
leaq 0x57b2c(%rip), %rax     # 0x64060
cmpl $0x0, (%rax)
je 0xc5fe
movb 0x71(%rsp), %al
movb %al, 0xb(%rsp)
movzwl 0x72(%rsp), %eax
movw %ax, 0xc(%rsp)
movb $0x0, 0xe(%rsp)
cmpl $0x4d4f5441, %r12d      # imm = 0x4D4F5441
je 0xc59b
leaq 0xb(%rsp), %rdi
leaq 0x436ca(%rip), %rsi     # 0x4fc33
pushq $0x3
popq %rdx
callq 0x2200
pushq $0x1
popq %rcx
testl %eax, %eax
je 0xc425
leaq 0xb(%rsp), %rdi
leaq 0x436af(%rip), %rsi     # 0x4fc37
pushq $0x3
popq %rdx
callq 0x2200
pushq $0x1
popq %rcx
testl %eax, %eax
je 0xc425
movb 0x7a(%rsp), %bpl
movb $0x20, 0x7a(%rsp)
leaq 0x76(%rsp), %rdi
leaq 0x4368a(%rip), %rsi     # 0x4fc3b
leaq 0x30(%rsp), %rdx
leaq 0x5c(%rsp), %rcx
leaq 0x58(%rsp), %r8
leaq 0x54(%rsp), %r9
xorl %eax, %eax
callq 0x21f0
movl 0x30(%rsp), %r12d
cmpl 0x2c(%rsp), %r12d
jne 0xc643
cmpb 0xa(%rsp), %bpl
jne 0xc643
leaq 0xb(%rsp), %rdi
leaq 0x24(%rsp), %rsi
pushq $0x3
popq %rdx
callq 0x2070
pushq $0x1
popq %rcx
testl %eax, %eax
je 0xc425
jmp 0xc643
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 0x434e6(%rip), %xmm0    # 0x4fb00
movmskps %xmm0, %eax
pushq $0x1
popq %rcx
testl %eax, %eax
jne 0xc425
jmp 0xc53d
xorl %ecx, %ecx
cmpb 0x38(%rsp), %al
setne %cl
addl %ecx, %r15d
movq %rax, 0x38(%rsp)
jmp 0xc52d
incl 0x14(%rsp)
leaq 0x24(%rsp), %rdi
leaq 0xb(%rsp), %rsi
callq 0x2080
pushq $0x1
popq %rcx
jmp 0xc41b
movq 0x48(%rsp), %rdi
callq 0x20c0
leaq 0x57a89(%rip), %rax     # 0x640f8
cmpl $0x0, (%rax)
movl 0x14(%rsp), %r13d
je 0xc68a
leaq 0x435c7(%rip), %rdi     # 0x4fc47
movl %r13d, %esi
xorl %eax, %eax
callq 0x20f0
cmpl $0x0, 0x34(%rsp)
je 0xc696
callq 0xea57
cmpb $0x0, 0x18(%rsp)
je 0xc6e0
movslq %r15d, %rdi
callq 0x21d0
movq %rax, %r12
xorl %r13d, %r13d
xorl %r15d, %r15d
cmpq %r13, %rbp
je 0xc72a
cmpl $0x0, (%r14,%r13,4)
movb (%rbx,%r13), %al
je 0xc6ca
movslq %r15d, %r15
movb %al, (%r12,%r15)
incl %r15d
jmp 0xc6db
movsbl %al, %esi
leaq 0x43581(%rip), %rdi     # 0x4fc55
xorl %eax, %eax
callq 0x20f0
incq %r13
jmp 0xc6ae
xorl %r12d, %r12d
movslq %r15d, %rdx
movb $0x0, (%rbx,%rdx)
leaq 0x43581(%rip), %rdi     # 0x4fc72
movl %r13d, %esi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x20f0
cmpb $0x0, 0x18(%rsp)
je 0xc715
movq %r12, %rdi
callq 0x2050
movq %r14, %rdi
callq 0x2050
movl %r13d, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
testl %r15d, %r15d
movl $0x0, %ecx
cmovgl %r15d, %ecx
movl 0x14(%rsp), %r13d
cmpq %rax, %rcx
je 0xc6e3
movb (%r12,%rax), %dl
movb %dl, (%rbx,%rax)
incq %rax
jmp 0xc73d