// File: /workspace/github_c_makefile_star2_O2/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 $0x998, %rsp            # imm = 0x998
movq %rsi, %r14
movq %rdi, %rbx
cmpl $0x0, 0x1cfc(%rsi)
je 0x15b13
leaq 0x1c6d(%r14), %r15
movsbl 0x40(%rbx), %edi
callq 0x4969
movsbl %al, %esi
movq %r15, %rdi
callq 0x4974
testl %eax, %eax
je 0x15c46
movl (%rbx), %edx
cmpl $0x4, %edx
jg 0x15b26
leaq 0x4227(%rip), %rsi      # 0x19d48
jmp 0x161fe
pushq $0x1
popq %rax
cmpl $0x0, 0x1d84(%r14)
je 0x16208
movq %r14, 0x10(%rsp)
xorl %r15d, %r15d
pushq $0x10
popq %r14
movslq %edx, %rcx
cmpq %rcx, %r15
jge 0x15c35
movq 0x68(%rbx), %rax
movq (%rax,%r15,8), %rdi
addq $0xa, %rdi
callq 0x14838
testl %eax, %eax
jne 0x15b67
incl 0x8(%rbx)
movq 0x68(%rbx), %rax
movq (%rax,%r15,8), %rdi
movl $0x190, %ebp            # imm = 0x190
movq %r14, %r12
xorl %eax, %eax
movq %rax, %r13
movl %r13d, 0xc(%rsp)
movslq (%rdi), %rax
cmpq %rax, %r13
jge 0x15c26
addq %r12, %rdi
callq 0x1487c
testl %eax, %eax
jne 0x15b9c
incl 0x18(%rbx)
movq 0x68(%rbx), %rax
movq (%rax,%r15,8), %rdi
movss -0x8(%rdi,%rbp), %xmm0
movss 0x416e(%rip), %xmm1     # 0x19d20
ucomiss %xmm0, %xmm1
movss 0x2f47(%rip), %xmm2     # 0x18b04
ja 0x15c26
ucomiss %xmm2, %xmm0
ja 0x15c26
movss -0x4(%rdi,%rbp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x15c26
ucomiss %xmm2, %xmm0
ja 0x15c26
movss (%rdi,%rbp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x15c26
ucomiss %xmm2, %xmm0
ja 0x15c26
movss 0x50c(%rdi,%r13,4), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
ja 0x15c26
ucomiss 0x2fa4(%rip), %xmm0     # 0x18ba0
ja 0x15c26
movss 0x638(%rdi,%r13,4), %xmm0
ucomiss %xmm0, %xmm1
ja 0x15c26
leaq 0x1(%r13), %rax
addq $0xc, %rbp
addq $0x5, %r12
ucomiss 0x4104(%rip), %xmm0     # 0x19d24
jbe 0x15b79
movl (%rbx), %edx
cmpl %r13d, (%rdi)
jg 0x15c35
incq %r15
jmp 0x15b43
cmpl %r15d, %edx
jle 0x15c4f
leaq 0x411c(%rip), %rsi      # 0x19d5d
jmp 0x161fe
andl $0x0, 0x58(%rbx)
jmp 0x16206
cvtsi2ssl 0x8(%rbx), %xmm0
cvtss2sd %xmm0, %xmm0
mulsd 0x40d0(%rip), %xmm0     # 0x19d30
cvtsi2ss %edx, %xmm1
cvtss2sd %xmm1, %xmm1
divsd %xmm1, %xmm0
ucomisd 0x40c4(%rip), %xmm0     # 0x19d38
jbe 0x15c82
leaq 0x4118(%rip), %rsi      # 0x19d95
jmp 0x161fe
movq 0x10(%rsp), %r14
cmpl 0x1d00(%r14), %edx
jge 0x15c9c
leaq 0x411d(%rip), %rsi      # 0x19db4
jmp 0x161fe
cmpl 0x1d04(%r14), %edx
jle 0x15cb1
leaq 0x4114(%rip), %rsi      # 0x19dc0
jmp 0x161fe
cmpl $0x0, 0x54(%rbx)
je 0x15dd0
addq $0x1cd2, %r14           # imm = 0x1CD2
movq %r14, %rdi
callq 0x20b0
testl %eax, %eax
je 0x161c1
pushq $0x63
popq %rsi
movq %r14, %rdi
callq 0x4974
testl %eax, %eax
je 0x15d55
xorl %r15d, %r15d
leaq 0x2c33(%rip), %r12      # 0x1891e
leaq 0xc(%rsp), %r13
leaq 0x2c4b(%rip), %rbp      # 0x18942
cmpl (%rbx), %r15d
jge 0x15d44
movq %rbx, %rdi
movl %r15d, %esi
movq %r12, %rdx
movq %r13, %rcx
callq 0x4777
testl %eax, %eax
jne 0x15d44
movq %rbx, %rdi
movl %r15d, %esi
movq %rbp, %rdx
movq %r13, %rcx
callq 0x4777
testl %eax, %eax
jne 0x15d44
movq %rbx, %rdi
movl %r15d, %esi
leaq 0x3881(%rip), %rdx      # 0x195b4
movq %r13, %rcx
callq 0x4777
testl %eax, %eax
jne 0x15d44
incl %r15d
jmp 0x15cf7
cmpl %r15d, (%rbx)
jne 0x15d55
leaq 0x4093(%rip), %rsi      # 0x19de3
jmp 0x161fe
movl 0x54(%rbx), %eax
cmpl $0x1, %eax
jne 0x15d73
pushq $0x6e
popq %rsi
movq %r14, %rdi
callq 0x4974
testl %eax, %eax
je 0x15f0f
movl 0x54(%rbx), %eax
testl %eax, %eax
jne 0x15d8d
pushq $0x78
popq %rsi
movq %r14, %rdi
callq 0x4974
testl %eax, %eax
je 0x15f59
movl 0x54(%rbx), %eax
cmpl $0x2, %eax
jne 0x15da5
pushq $0x6d
popq %rsi
movq %r14, %rdi
callq 0x4974
testl %eax, %eax
je 0x15f4d
cmpl $0x0, 0x5c(%rbx)
je 0x15ef0
cmpl $0x1, 0x60(%rbx)
jne 0x15dfd
pushq $0x64
popq %rsi
movq %r14, %rdi
callq 0x4974
testl %eax, %eax
je 0x15dfd
leaq 0x404f(%rip), %rsi      # 0x19e1a
jmp 0x161fe
movss 0x50(%rbx), %xmm0
movss 0x1d30(%r14), %xmm1
ucomiss %xmm0, %xmm1
ja 0x15df1
ucomiss 0x1d34(%r14), %xmm0
jbe 0x15cbb
leaq 0x3fd3(%rip), %rsi      # 0x19dcb
jmp 0x161fe
pushq $0x61
popq %rsi
movq %r14, %rdi
callq 0x4974
testl %eax, %eax
je 0x161c1
cmpl $0x0, 0x58(%rbx)
je 0x15e2c
cmpl $0x0, 0x1c(%rbx)
jne 0x15e2c
cmpl $-0x1, 0x20(%rbx)
jne 0x15e2c
cmpl $0x0, 0x24(%rbx)
je 0x161f7
leaq 0x2c(%rsp), %rsi
leaq 0x28(%rsp), %rdx
leaq 0x24(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x1c(%rsp), %r9
movq %rbx, %rdi
callq 0x4aaf
cvtss2sd %xmm0, %xmm0
movsd 0x3ee7(%rip), %xmm1     # 0x19d40
ucomisd %xmm0, %xmm1
ja 0x15f1b
ucomisd 0x2ced(%rip), %xmm0     # 0x18b58
ja 0x15f1b
movslq (%rbx), %rax
movq %rax, %rcx
decq %rcx
xorl %edx, %edx
pushq $0x1
popq %rsi
movss 0x3ea1(%rip), %xmm0     # 0x19d28
xorpd %xmm1, %xmm1
movss 0x3e99(%rip), %xmm2     # 0x19d2c
movss 0x3e85(%rip), %xmm3     # 0x19d20
cmpq %rcx, %rsi
jge 0x15f27
movq 0x68(%rbx), %rdi
movq (%rdi,%rsi,8), %rdi
movq 0x768(%rdi), %rdi
movzbl 0x1d(%rdi), %r8d
cmpl $0x48, %r8d
je 0x15eeb
cmpl $0x54, %r8d
je 0x15eeb
movss (%rdi), %xmm4
ucomiss %xmm0, %xmm4
jbe 0x15eeb
ucomiss %xmm4, %xmm1
jbe 0x15eeb
movss 0x4(%rdi), %xmm4
movaps %xmm3, %xmm5
cmpltps %xmm4, %xmm5
cmpltps %xmm2, %xmm4
andps %xmm5, %xmm4
movd %xmm4, %edi
subl %edi, %edx
incq %rsi
jmp 0x15e9b
pushq $0x70
popq %rsi
movq %r14, %rdi
callq 0x4974
testl %eax, %eax
je 0x15daf
leaq 0x3f02(%rip), %rsi      # 0x19e0c
jmp 0x161fe
leaq 0x3ed5(%rip), %rsi      # 0x19deb
jmp 0x161fe
leaq 0x3f21(%rip), %rsi      # 0x19e43
jmp 0x161fe
cvtsi2ss %edx, %xmm0
cvtsi2ss %eax, %xmm1
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
ucomisd 0x3e01(%rip), %xmm0     # 0x19d40
jbe 0x15f65
leaq 0x3f0e(%rip), %rsi      # 0x19e56
jmp 0x161fe
leaq 0x3eac(%rip), %rsi      # 0x19e00
jmp 0x161fe
leaq 0x3e95(%rip), %rsi      # 0x19df5
jmp 0x161fe
xorl %r13d, %r13d
leaq 0x8(%rsp), %r14
leaq 0x4(%rsp), %r15
xorl %r12d, %r12d
movslq 0x1c(%rbx), %rax
cmpq %rax, %r13
jge 0x16002
movq 0x80(%rbx), %rax
movq (%rax,%r13,8), %rsi
addq $0xc, %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x4720
testl %eax, %eax
je 0x16002
movq 0x80(%rbx), %rax
movq (%rax,%r13,8), %rsi
addq $0x12, %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x4720
testl %eax, %eax
je 0x16002
movq 0x80(%rbx), %rax
movq (%rax,%r13,8), %rax
movl 0x1c(%rax), %eax
leal -0xb(%rax), %ecx
cmpl $-0xa, %ecx
jb 0x16002
movl 0x4(%rsp), %ecx
movl 0x8(%rsp), %edx
movl %ecx, %esi
subl %edx, %esi
cmpl $0x64, %esi
ja 0x16002
cmpl $0x1, %eax
jne 0x15ffa
movslq %r12d, %r12
movl %edx, 0x30(%rsp,%r12,8)
movl %ecx, 0x34(%rsp,%r12,8)
incl %r12d
incq %r13
jmp 0x15f75
cmpl %r13d, 0x1c(%rbx)
jle 0x16014
leaq 0x3e5d(%rip), %rsi      # 0x19e6c
jmp 0x161fe
xorl %r13d, %r13d
leaq 0x8(%rsp), %r14
movslq 0x20(%rbx), %rax
cmpq %rax, %r13
jge 0x160d5
movq 0x88(%rbx), %rax
movq (%rax,%r13,8), %rax
movl (%rax), %ecx
movl $0x172, %r15d           # imm = 0x172
xorl %ebp, %ebp
movslq %ecx, %rcx
cmpq %rcx, %rbp
jge 0x160cd
leaq (%rax,%r15), %rsi
addq $-0x78, %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x4720
testl %eax, %eax
je 0x160cd
movq 0x88(%rbx), %rax
movq (%rax,%r13,8), %rsi
addq %r15, %rsi
movq %rbx, %rdi
leaq 0x4(%rsp), %rdx
callq 0x4720
testl %eax, %eax
je 0x160cd
movl 0x4(%rsp), %ecx
movl 0x8(%rsp), %edx
movl %ecx, %eax
subl %edx, %eax
cmpl $0x64, %eax
ja 0x160cd
movq 0x88(%rbx), %rax
movq (%rax,%r13,8), %rax
cmpl $0x0, 0x214(%rax,%rbp,4)
je 0x160b6
movslq %r12d, %r12
movl %edx, 0x30(%rsp,%r12,8)
movl %ecx, 0x34(%rsp,%r12,8)
incl %r12d
movslq (%rax), %rcx
leaq 0x1(%rbp), %rdx
addq $0x6, %r15
cmpq %rcx, %rbp
movq %rdx, %rbp
jge 0x1603e
incq %r13
jmp 0x1601c
xorl %r13d, %r13d
leaq 0x8(%rsp), %r14
leaq 0x4(%rsp), %r15
movslq 0x24(%rbx), %rax
cmpq %rax, %r13
jge 0x16139
movq 0x90(%rbx), %rax
movq (%rax,%r13,8), %rsi
addq $0xc, %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x4720
testl %eax, %eax
je 0x16139
movq 0x90(%rbx), %rax
movq (%rax,%r13,8), %rsi
addq $0x12, %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x4720
testl %eax, %eax
je 0x16139
movl 0x4(%rsp), %eax
subl 0x8(%rsp), %eax
cmpl $0x64, %eax
ja 0x16139
incq %r13
jmp 0x160e2
cmpl %r13d, 0x24(%rbx)
jle 0x1614e
leaq 0x3d41(%rip), %rsi      # 0x19e87
movq %rbx, %rdi
callq 0x1621a
cmpl $0x2, %r12d
pushq $0x1
popq %rax
cmovgel %r12d, %eax
decl %eax
pushq $0x1
popq %rcx
xorl %edx, %edx
cmpq %rax, %rdx
je 0x161c1
leaq 0x1(%rdx), %rsi
movq %rcx, %rdi
cmpl %edi, %r12d
jle 0x161b9
movl 0x30(%rsp,%rdx,8), %r10d
movl 0x34(%rsp,%rdx,8), %r8d
movl 0x30(%rsp,%rdi,8), %r9d
cmpl %r9d, %r10d
jne 0x1618c
cmpl 0x34(%rsp,%rdi,8), %r8d
je 0x161b4
cmpl %r10d, %r9d
jle 0x16196
cmpl %r8d, %r9d
jl 0x161ee
movl 0x34(%rsp,%rdi,8), %r11d
cmpl %r10d, %r11d
jle 0x161aa
cmpl %r9d, %r10d
jg 0x161ee
cmpl %r8d, %r11d
jl 0x161ee
cmpl %r9d, %r8d
jle 0x161b4
cmpl %r11d, %r8d
jl 0x161ee
incq %rdi
jmp 0x1616c
incq %rcx
movq %rsi, %rdx
jmp 0x16160
movq 0x9e28(%rip), %rax      # 0x1fff0
movq (%rax), %rdi
movq 0x48(%rbx), %rdx
movsbl 0x40(%rbx), %ecx
movl (%rbx), %r8d
cvtss2sd 0x50(%rbx), %xmm0
leaq 0x3cd2(%rip), %rsi      # 0x19eb4
movb $0x1, %al
callq 0x2160
pushq $0x1
popq %rax
jmp 0x16208
leaq 0x3cac(%rip), %rsi      # 0x19ea1
jmp 0x161fe
leaq 0x3c37(%rip), %rsi      # 0x19e35
movq %rbx, %rdi
callq 0x1621a
xorl %eax, %eax
addq $0x998, %rsp            # imm = 0x998
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq