// File: /workspace/github_c_makefile_star2_O3/josch[P]stride/chkchain.c
// Function: CheckChain
int CheckChain(CHAIN *Chain, COMMAND *Cmd)
{

  int Res, HelAlp, HelPI, Hel310, Sheet, Turn, Bound[300][2];
  int i, j, AsnNumb=0, At, SuspCnt, Beg, End;
  float Content;

  if( Cmd->NProcessed && !ChInStr(Cmd->Processed,SpaceToDash(Chain->Id)) ) {
    Chain->Valid = NO;
    return(FAILURE);
  }

  if( Chain->NRes < 5 )
    return(NotValid(Chain,"less than 5 residues"));
    
  if( !Cmd->Stringent ) 
    return(SUCCESS);
  
  for( Res=0; Res<Chain->NRes; Res++ ){
    if( !CheckRes(Chain->Rsd[Res]->ResType) )
      Chain->NonStandRes++;
    for( At=0; At<Chain->Rsd[Res]->NAtom; At++) {
      if( !CheckAtom(Chain->Rsd[Res]->AtomType[At]) ) 
	Chain->NonStandAtom++;
      if(Chain->Rsd[Res]->Coord[At][0] < MIN_X || Chain->Rsd[Res]->Coord[At][0] > MAX_X ||
	 Chain->Rsd[Res]->Coord[At][1] < MIN_Y || Chain->Rsd[Res]->Coord[At][1] > MAX_Y ||
	 Chain->Rsd[Res]->Coord[At][2] < MIN_Z || Chain->Rsd[Res]->Coord[At][2] > MAX_Z ||
	 Chain->Rsd[Res]->Occupancy[At] < MIN_Occupancy || 
	 Chain->Rsd[Res]->Occupancy[At] > MAX_Occupancy ||
	 Chain->Rsd[Res]->TempFactor[At] < MIN_TempFactor || 
	 Chain->Rsd[Res]->TempFactor[At] > MAX_TempFactor )
	break;
    }      
    if( At < Chain->Rsd[Res]->NAtom )
      break;
  }
  
  if( Res < Chain->NRes )
    return(NotValid(Chain,"suspicious coordinates, occupancy or temperature factor"));
  
  if( 100.0*(float)Chain->NonStandRes/(float)Chain->NRes > MAXNONSTAND )
    return(NotValid(Chain,"too many non-standard residues"));
  
  if( Chain->NRes < Cmd->MinLength )
    return(NotValid(Chain,"Short chain"));
  
  if( Chain->NRes > Cmd->MaxLength )
    return(NotValid(Chain,"Long chain"));
  
  if( Chain->Method == XRay && 
     (Chain->Resolution < Cmd->MinResolution || Chain->Resolution > Cmd->MaxResolution ) )
    return(NotValid(Chain,"Resolution out of range"));
  
  if( (int)strlen(Cmd->Cond) != 0 )  {
    
    if( ChInStr(Cmd->Cond,'c') ) {
      for( Res=0; Res<Chain->NRes; Res++ )
	if( FindAtom(Chain,Res,"N",&At) || 
	    FindAtom(Chain,Res,"O",&At) || 
	    FindAtom(Chain,Res,"C",&At) ) 
	  break;
      
      if( Res == Chain->NRes )
	return(NotValid(Chain,"only CA"));
    }
    
    if( Chain->Method == NMR && !ChInStr(Cmd->Cond,'n') )
      return(NotValid(Chain,"NMR chain"));
    
    if( Chain->Method == XRay && !ChInStr(Cmd->Cond,'x') )
      return(NotValid(Chain,"XRay chain"));
    
    if( Chain->Method == Model && !ChInStr(Cmd->Cond,'m') )
      return(NotValid(Chain,"Model chain"));
    
    if( Chain->Published == NO && ChInStr(Cmd->Cond,'p') )
      return(NotValid(Chain,"Not published"));
    
    if( Chain->DsspAssigned == YES && ChInStr(Cmd->Cond,'d') )
      return(NotValid(Chain,"Assigned according to DSSP"));
    
    if( ChInStr(Cmd->Cond,'a') ) {
      
      if( Chain->Valid && Chain->NHelix == 0 && Chain->NSheet == -1 && Chain->NTurn == 0 )
	return(NotValid(Chain,"No assignment"));
      
      if( (Content = SecStrContent(Chain,&HelAlp,&HelPI,&Hel310,&Sheet,&Turn)) < 0.4 || 
	   Content > 0.9 )
	return(NotValid(Chain,"Suspicious content"));
      
      SuspCnt = 0;
      for( Res=1; Res<Chain->NRes-1; Res++ ) {
	if( ( Chain->Rsd[Res]->Prop->PdbAsn != 'H' && Chain->Rsd[Res]->Prop->PdbAsn != 'T' &&
	     Chain->Rsd[Res]->Prop->Phi > -150.0 && Chain->Rsd[Res]->Prop->Phi < 0.0 && 
	     Chain->Rsd[Res]->Prop->Psi > -100.0 && Chain->Rsd[Res]->Prop->Psi < 10.0) )
	  SuspCnt++;
	
      }
      
      if( (float)SuspCnt/(float)Chain->NRes > 0.4 )
	return(NotValid(Chain,"Suspicious assignment"));
      
      for( i=0; i<Chain->NHelix; i++ ) {
	if( !PdbN2SeqN(Chain,Chain->Helix[i]->PDB_ResNumb1,&Beg) ||
	   !PdbN2SeqN(Chain,Chain->Helix[i]->PDB_ResNumb2,&End) ||
	   /*	      !CheckRes(Chain->Helix[i]->PDB_ResNumb1) ||
		      !CheckRes(Chain->Helix[i]->PDB_ResNumb2) || */
	   Chain->Helix[i]->Class > 10 ||
	   Chain->Helix[i]->Class < 1  ||
	   End-Beg > 100 || End-Beg < 0 )
	  break;
	else 
	  if( Chain->Helix[i]->Class == 1 ) {
	    Bound[AsnNumb][0] = Beg;
	    Bound[AsnNumb][1] = End;
	    AsnNumb++;
	  }
      }
      if( i < Chain->NHelix )
	return(NotValid(Chain,"Erraneous helix assignment"));
      
      for( i=0; i<Chain->NSheet; i++ )
	for( j=0; j<Chain->Sheet[i]->NStrand; j++ ) {
	  if( !PdbN2SeqN(Chain,Chain->Sheet[i]->PDB_ResNumb1[j],&Beg) ||
	     !PdbN2SeqN(Chain,Chain->Sheet[i]->PDB_ResNumb2[j],&End) ||
	     /*	        !CheckRes(Chain->Sheet[i]->PDB_ResNumb1[j]) ||
			!CheckRes(Chain->Sheet[i]->PDB_ResNumb2[j]) || */
	     End-Beg > 100 || End-Beg < 0 )
	    break;
	  else
	    if( Chain->Sheet[i]->Sence[j] != 0 ) {
	      Bound[AsnNumb][0] = Beg;
	      Bound[AsnNumb][1] = End;
	      AsnNumb++;
	    }
	  if( j < Chain->Sheet[i]->NStrand )
	    break;
	}
      
      if( i < Chain->NSheet )
	return(NotValid(Chain,"Erraneous sheet assignment"));
      
      for( i=0; i<Chain->NTurn; i++ )
	if( !PdbN2SeqN(Chain,Chain->Turn[i]->PDB_ResNumb1,&Beg) ||
	   !PdbN2SeqN(Chain,Chain->Turn[i]->PDB_ResNumb2,&End) ||
	   End-Beg > 100 || End-Beg < 0 ) 
	  break;
      
      if( i < Chain->NTurn ) 
	NotValid(Chain,"Erraneous turn assignment");
      
      for( i=0; i<AsnNumb-1; i++ ) {
	for( j=i+1; j<AsnNumb; j++ ) {
	  if( Bound[i][0] == Bound[j][0] && Bound[i][1] == Bound[j][1] ) continue;
	  if( (Bound[j][0] > Bound[i][0] && Bound[j][0] < Bound[i][1]) ||
	     (Bound[j][1] > Bound[i][0] && Bound[j][1] < Bound[i][1]) ||
	     (Bound[i][0] > Bound[j][0] && Bound[i][0] < Bound[j][1]) ||
	     (Bound[i][1] > Bound[j][0] && Bound[i][1] < Bound[j][1]) )
	    break;
	}
	if( j < AsnNumb )
	  break;
      }
      
      if( i < AsnNumb-1 )
	return(NotValid(Chain,"Assignment overlap"));
    }
  }

  fprintf(stderr,"ACCEPTED %s %c %4d %7.3f\n",
	  Chain->File,Chain->Id,Chain->NRes,Chain->Resolution);
  return(SUCCESS);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x988, %rsp            # imm = 0x988
movq %rsi, %r14
movq %rdi, %rbx
cmpl $0x0, 0x1cfc(%rsi)
je 0x160c3
leaq 0x1c6d(%r14), %r15
movsbl 0x40(%rbx), %edi
callq 0x50a2
movsbl %al, %esi
movq %r15, %rdi
callq 0x50af
testl %eax, %eax
je 0x161f3
cmpl $0x4, (%rbx)
jle 0x161fc
movl $0x1, %eax
cmpl $0x0, 0x1d84(%r14)
je 0x1620d
movq 0x68(%rbx), %rax
xorl %r15d, %r15d
movq (%rax,%r15,8), %rdi
addq $0xa, %rdi
callq 0x14d3c
testl %eax, %eax
jne 0x160fa
incl 0x8(%rbx)
movl $0x0, 0x8(%rsp)
movq 0x68(%rbx), %rax
movq (%rax,%r15,8), %rdi
movl (%rdi), %edx
testl %edx, %edx
jle 0x161ce
movl $0x190, %r13d           # imm = 0x190
movl $0x10, %ebp
xorl %r12d, %r12d
addq %rbp, %rdi
callq 0x14d7c
testl %eax, %eax
jne 0x16131
incl 0x18(%rbx)
movq 0x68(%rbx), %rax
movq (%rax,%r15,8), %rdi
movss -0x8(%rdi,%r13), %xmm0
movss 0x4c28(%rip), %xmm1     # 0x1ad70
ucomiss %xmm0, %xmm1
movss 0x39b1(%rip), %xmm2     # 0x19b04
ja 0x161ca
ucomiss %xmm2, %xmm0
ja 0x161ca
movss -0x4(%rdi,%r13), %xmm0
ucomiss %xmm0, %xmm1
ja 0x161ca
ucomiss %xmm2, %xmm0
ja 0x161ca
movss (%rdi,%r13), %xmm0
ucomiss %xmm0, %xmm1
ja 0x161ca
ucomiss %xmm2, %xmm0
ja 0x161ca
movss 0x50c(%rdi,%r12,4), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
ja 0x161ca
ucomiss 0x3a0c(%rip), %xmm0     # 0x19ba0
ja 0x161ca
movss 0x638(%rdi,%r12,4), %xmm0
ucomiss %xmm0, %xmm1
ja 0x161ca
ucomiss 0x4bc8(%rip), %xmm0     # 0x1ad74
ja 0x161ca
incq %r12
movl %r12d, 0x8(%rsp)
movslq (%rdi), %rcx
addq $0xc, %r13
addq $0x5, %rbp
cmpq %rcx, %r12
jl 0x16122
movl (%rdi), %edx
jmp 0x161d1
xorl %r12d, %r12d
movslq (%rbx), %rcx
cmpl %edx, %r12d
jl 0x161e5
incq %r15
cmpq %rcx, %r15
jl 0x160e6
cmpl %r15d, %ecx
jle 0x1621f
leaq 0x4bbc(%rip), %rsi      # 0x1adad
jmp 0x16203
movl $0x0, 0x58(%rbx)
jmp 0x1620b
leaq 0x4b95(%rip), %rsi      # 0x1ad98
movq %rbx, %rdi
callq 0x2773
xorl %eax, %eax
addq $0x988, %rsp            # imm = 0x988
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorps %xmm0, %xmm0
cvtsi2ssl 0x8(%rbx), %xmm0
cvtss2sd %xmm0, %xmm0
mulsd 0x4b4d(%rip), %xmm0     # 0x1ad80
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
cvtss2sd %xmm1, %xmm1
divsd %xmm1, %xmm0
ucomisd 0x4b3e(%rip), %xmm0     # 0x1ad88
jbe 0x16255
leaq 0x4b92(%rip), %rsi      # 0x1ade5
jmp 0x16203
cmpl 0x1d00(%r14), %ecx
jge 0x16267
leaq 0x4b9f(%rip), %rsi      # 0x1ae04
jmp 0x16203
cmpl 0x1d04(%r14), %ecx
jle 0x16279
leaq 0x4b99(%rip), %rsi      # 0x1ae10
jmp 0x16203
cmpl $0x0, 0x54(%rbx)
jne 0x162a8
movss 0x50(%rbx), %xmm0
movss 0x1d30(%r14), %xmm1
ucomiss %xmm0, %xmm1
ja 0x1629c
ucomiss 0x1d34(%r14), %xmm0
jbe 0x162a8
leaq 0x4b78(%rip), %rsi      # 0x1ae1b
jmp 0x16203
addq $0x1cd2, %r14           # imm = 0x1CD2
movq %r14, %rdi
callq 0x20b0
testl %eax, %eax
jne 0x162cd
movq %rbx, %rdi
callq 0x27d6
movl $0x1, %eax
jmp 0x1620d
movq %r14, %rdi
movl $0x63, %esi
callq 0x50af
testl %eax, %eax
je 0x1635b
movl (%rbx), %eax
xorl %r15d, %r15d
testl %eax, %eax
jle 0x1634a
xorl %r15d, %r15d
leaq 0x362d(%rip), %rbp      # 0x1991e
leaq 0x8(%rsp), %r13
leaq 0x42e7(%rip), %r12      # 0x1a5e4
movq %rbx, %rdi
movl %r15d, %esi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x4ea0
testl %eax, %eax
jne 0x16348
movq %rbx, %rdi
movl %r15d, %esi
leaq 0x3623(%rip), %rdx      # 0x19942
movq %r13, %rcx
callq 0x4ea0
testl %eax, %eax
jne 0x16348
movq %rbx, %rdi
movl %r15d, %esi
movq %r12, %rdx
movq %r13, %rcx
callq 0x4ea0
testl %eax, %eax
jne 0x16348
incl %r15d
cmpl (%rbx), %r15d
jl 0x162fd
movl (%rbx), %eax
cmpl %eax, %r15d
jne 0x1635b
leaq 0x4add(%rip), %rsi      # 0x1ae33
jmp 0x16203
movl 0x54(%rbx), %eax
cmpl $0x1, %eax
jne 0x16383
movq %r14, %rdi
movl $0x6e, %esi
callq 0x50af
testl %eax, %eax
jne 0x16380
leaq 0x4ac0(%rip), %rsi      # 0x1ae3b
jmp 0x16203
movl 0x54(%rbx), %eax
testl %eax, %eax
jne 0x163a7
movq %r14, %rdi
movl $0x78, %esi
callq 0x50af
testl %eax, %eax
jne 0x163a4
leaq 0x4aa6(%rip), %rsi      # 0x1ae45
jmp 0x16203
movl 0x54(%rbx), %eax
cmpl $0x2, %eax
jne 0x163c9
movq %r14, %rdi
movl $0x6d, %esi
callq 0x50af
testl %eax, %eax
jne 0x163c9
leaq 0x4a8c(%rip), %rsi      # 0x1ae50
jmp 0x16203
cmpl $0x0, 0x5c(%rbx)
jne 0x163e4
movq %r14, %rdi
movl $0x70, %esi
callq 0x50af
testl %eax, %eax
jne 0x16530
cmpl $0x1, 0x60(%rbx)
jne 0x163ff
movq %r14, %rdi
movl $0x64, %esi
callq 0x50af
testl %eax, %eax
jne 0x1653c
movq %r14, %rdi
movl $0x61, %esi
callq 0x50af
testl %eax, %eax
je 0x162bb
cmpl $0x0, 0x58(%rbx)
je 0x16438
cmpl $0x0, 0x1c(%rbx)
jne 0x16438
cmpl $-0x1, 0x20(%rbx)
jne 0x16438
cmpl $0x0, 0x24(%rbx)
jne 0x16438
leaq 0x4a52(%rip), %rsi      # 0x1ae85
jmp 0x16203
leaq 0x1c(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x14(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0xc(%rsp), %r9
movq %rbx, %rdi
callq 0x51fd
cvtss2sd %xmm0, %xmm0
movsd 0x492b(%rip), %xmm1     # 0x1ad90
ucomisd %xmm0, %xmm1
ja 0x16548
ucomisd 0x36e1(%rip), %xmm0     # 0x19b58
ja 0x16548
movl (%rbx), %eax
xorps %xmm0, %xmm0
cmpl $0x3, %eax
jl 0x1650b
leal -0x1(%rax), %ecx
movq 0x68(%rbx), %rdx
xorl %esi, %esi
movl $0x1, %edi
movss 0x48d7(%rip), %xmm0     # 0x1ad78
xorpd %xmm1, %xmm1
movss 0x48cf(%rip), %xmm2     # 0x1ad7c
movss 0x48bb(%rip), %xmm3     # 0x1ad70
movq (%rdx,%rdi,8), %r8
movq 0x768(%r8), %r8
movzbl 0x1d(%r8), %r9d
cmpl $0x48, %r9d
je 0x164fc
cmpl $0x54, %r9d
je 0x164fc
movss (%r8), %xmm4
ucomiss %xmm0, %xmm4
jbe 0x164fc
ucomiss %xmm4, %xmm1
jbe 0x164fc
movss 0x4(%r8), %xmm4
movaps %xmm3, %xmm5
cmpltps %xmm4, %xmm5
cmpltps %xmm2, %xmm4
andps %xmm5, %xmm4
movd %xmm4, %r8d
subl %r8d, %esi
incq %rdi
cmpq %rdi, %rcx
jne 0x164b5
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
ucomisd 0x486e(%rip), %xmm0     # 0x1ad90
jbe 0x16554
leaq 0x497b(%rip), %rsi      # 0x1aea6
jmp 0x16203
leaq 0x4925(%rip), %rsi      # 0x1ae5c
jmp 0x16203
leaq 0x4927(%rip), %rsi      # 0x1ae6a
jmp 0x16203
leaq 0x4944(%rip), %rsi      # 0x1ae93
jmp 0x16203
movl 0x1c(%rbx), %eax
xorl %r13d, %r13d
movl $0x0, %r12d
testl %eax, %eax
jle 0x165fc
movq 0x80(%rbx), %rax
xorl %r13d, %r13d
leaq 0x4(%rsp), %r14
movq %rsp, %r15
xorl %r12d, %r12d
movq (%rax,%r13,8), %rsi
addq $0xc, %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x4e44
testl %eax, %eax
je 0x165f9
movq 0x80(%rbx), %rax
movq (%rax,%r13,8), %rsi
addq $0x12, %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x4e44
testl %eax, %eax
je 0x165f9
movq 0x80(%rbx), %rax
movq (%rax,%r13,8), %rcx
movl 0x1c(%rcx), %ecx
leal -0xb(%rcx), %edx
cmpl $-0xa, %edx
jb 0x165f9
movl (%rsp), %edx
movl 0x4(%rsp), %esi
movl %edx, %edi
subl %esi, %edi
cmpl $0x64, %edi
ja 0x165f9
cmpl $0x1, %ecx
jne 0x165ed
movslq %r12d, %r12
movl %esi, 0x20(%rsp,%r12,8)
movl %edx, 0x24(%rsp,%r12,8)
incl %r12d
incq %r13
movslq 0x1c(%rbx), %rcx
cmpq %rcx, %r13
jl 0x1657d
movl 0x1c(%rbx), %eax
cmpl %eax, %r13d
jge 0x1660d
leaq 0x48b4(%rip), %rsi      # 0x1aebc
jmp 0x16203
movl 0x20(%rbx), %eax
testl %eax, %eax
jle 0x166b1
xorl %r13d, %r13d
leaq 0x4(%rsp), %r14
movl $0x172, %ebp            # imm = 0x172
movq %rsp, %r15
movq 0x88(%rbx), %rcx
movq (%rcx,%r13,8), %rsi
cmpl $0x0, (%rsi)
jle 0x166a2
addq $0xfa, %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x4e44
testl %eax, %eax
je 0x1669f
movq 0x88(%rbx), %rax
movq (%rax,%r13,8), %rsi
addq %rbp, %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x4e44
testl %eax, %eax
je 0x1669f
movl (%rsp), %eax
movl 0x4(%rsp), %ecx
movl %eax, %edx
subl %ecx, %edx
cmpl $0x64, %edx
ja 0x1669f
movq 0x88(%rbx), %rdx
movq (%rdx,%r13,8), %rdx
cmpl $0x0, 0x214(%rdx)
je 0x1669f
movslq %r12d, %r12
movl %ecx, 0x20(%rsp,%r12,8)
movl %eax, 0x24(%rsp,%r12,8)
incl %r12d
movl 0x20(%rbx), %eax
incq %r13
movslq %eax, %rcx
cmpq %rcx, %r13
jl 0x16628
movl 0x24(%rbx), %eax
xorl %r13d, %r13d
testl %eax, %eax
jle 0x1671d
xorl %r13d, %r13d
leaq 0x4(%rsp), %r14
movq %rsp, %r15
movq 0x90(%rbx), %rax
movq (%rax,%r13,8), %rsi
addq $0xc, %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x4e44
testl %eax, %eax
je 0x1671a
movq 0x90(%rbx), %rax
movq (%rax,%r13,8), %rsi
addq $0x12, %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x4e44
testl %eax, %eax
je 0x1671a
movl (%rsp), %eax
subl 0x4(%rsp), %eax
cmpl $0x64, %eax
ja 0x1671a
incq %r13
movslq 0x24(%rbx), %rax
cmpq %rax, %r13
jl 0x166c6
movl 0x24(%rbx), %eax
cmpl %eax, %r13d
jge 0x16731
leaq 0x47ae(%rip), %rsi      # 0x1aed7
movq %rbx, %rdi
callq 0x2773
cmpl $0x2, %r12d
jl 0x162bb
leal -0x1(%r12), %eax
movl %r12d, %ecx
leaq 0x2c(%rsp), %rdx
decq %rcx
xorl %esi, %esi
movl 0x20(%rsp,%rsi,8), %edi
movl 0x24(%rsp,%rsi,8), %r8d
incq %rsi
xorl %r9d, %r9d
movl -0x4(%rdx,%r9,8), %r10d
cmpl %r10d, %edi
jne 0x1676c
cmpl (%rdx,%r9,8), %r8d
je 0x16793
cmpl %edi, %r10d
jle 0x16776
cmpl %r8d, %r10d
jl 0x167ac
movl (%rdx,%r9,8), %r11d
cmpl %edi, %r11d
jle 0x16789
cmpl %r10d, %edi
jg 0x167ac
cmpl %r8d, %r11d
jl 0x167ac
cmpl %r10d, %r8d
jle 0x16793
cmpl %r11d, %r8d
jl 0x167ac
incq %r9
cmpq %r9, %rcx
jne 0x1675c
addq $0x8, %rdx
decq %rcx
cmpq %rax, %rsi
jne 0x1674d
jmp 0x162bb
leaq 0x473e(%rip), %rsi      # 0x1aef1
jmp 0x16203