// File: /workspace/github_c_makefile_star2_O2/josch[P]stride/contact_order.c
// Function: ContactOrder
void ContactOrder(CHAIN **Chain, int NChain, COMMAND *Cmd )
{

  register int Res1, Res2, At1, At2;
  int Cn, From, To, NumberOfContacts;
  char PDB_Start[RES_FIELD], PDB_End[RES_FIELD];
  double CO;
  
  for( Cn=0; Cn<NChain; Cn++ ) {
    
    if( !Chain[Cn]->Valid )
      continue;
    NumberOfContacts = 0;
    CO = 0.0;
    if( !strcmp(Cmd->FirstResidue,"") ) {
      From = 0;
      strcpy(PDB_Start,Chain[Cn]->Rsd[0]->PDB_ResNumb);
    }
    else { 
      if( !PdbN2SeqN(Chain[Cn],Cmd->FirstResidue,&From) ) {
	fprintf(stderr,"Residue %s does not exist in %s chain %c\n",
		Cmd->FirstResidue,Chain[Cn]->PdbIdent,SpaceToDash(Chain[Cn]->Id));
	exit(1);
      }
      strcpy(PDB_Start,Cmd->FirstResidue);
    }
    if( !strcmp(Cmd->LastResidue,"") ) {
      To = Chain[Cn]->NRes;
      strcpy(PDB_End,Chain[Cn]->Rsd[Chain[Cn]->NRes-1]->PDB_ResNumb);
    }
    else {
      if( !PdbN2SeqN(Chain[Cn],Cmd->LastResidue,&To) ) {
	fprintf(stderr,"Residue %s does not exist in %s chain %c\n",
		Cmd->LastResidue,Chain[Cn]->PdbIdent,SpaceToDash(Chain[Cn]->Id));
	exit(1);
      }
      strcpy(PDB_End,Cmd->LastResidue);
    }
    for( Res1=From; Res1<To-1; Res1++ ) {
      for( At1=0; At1<Chain[Cn]->Rsd[Res1]->NAtom; At1++ ) {
	if( !strcmp(Chain[Cn]->Rsd[Res1]->AtomType[At1],"H") )
	  continue;
	for( Res2 = Res1+1; Res2<To; Res2++ ) {
	  for( At2=0; At2<Chain[Cn]->Rsd[Res2]->NAtom; At2++ ) {
	    if( !strcmp(Chain[Cn]->Rsd[Res2]->AtomType[At2],"H") )
	      continue;
	    if( Dist(Chain[Cn]->Rsd[Res1]->Coord[At1],
		     Chain[Cn]->Rsd[Res2]->Coord[At2]) < CO_DIST) {
	      CO += abs(Res1-Res2);
	      NumberOfContacts++;
	    }
	  }
	}
      }
    }
    CO = 100.0*CO/NumberOfContacts;
    CO /= (To-From+1);
    
    printf("%s %c %d ( %s ) %d ( %s ) %5.1f\n",
	   Chain[Cn]->PdbIdent,SpaceToDash(Chain[Cn]->Id),From,PDB_Start,To-1,PDB_End,CO);
  }
  exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %rbx
movq %rdi, 0x18(%rsp)
leaq 0x1ce6(%rdx), %r14
addq $0x1cec, %rbx           # imm = 0x1CEC
xorl %ecx, %ecx
testl %esi, %esi
cmovlel %ecx, %esi
movq %rsi, 0x48(%rsp)
leaq 0x32(%rsp), %r15
leaq 0x2c(%rsp), %r12
movq %rbx, 0x40(%rsp)
movq %r14, 0x38(%rsp)
cmpq 0x48(%rsp), %rcx
je 0x17a4e
movq 0x18(%rsp), %rax
movq (%rax,%rcx,8), %rdi
cmpl $0x0, 0x58(%rdi)
je 0x17a46
movq %rcx, 0x10(%rsp)
cmpb $0x0, (%r14)
je 0x17816
movq %r14, %rsi
leaq 0xc(%rsp), %rdx
callq 0x4720
movq %r14, %rsi
testl %eax, %eax
jne 0x17826
jmp 0x17a55
andl $0x0, 0xc(%rsp)
movq 0x68(%rdi), %rax
movq (%rax), %rsi
addq $0x4, %rsi
movq %r15, %rdi
callq 0x2070
cmpb $0x0, (%rbx)
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
je 0x17868
movq %rbx, %rsi
leaq 0x8(%rsp), %rdx
callq 0x4720
testl %eax, %eax
je 0x17a91
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2070
movl 0x8(%rsp), %r15d
jmp 0x17885
movslq (%rdi), %r15
movl %r15d, 0x8(%rsp)
movq 0x68(%rdi), %rax
movq -0x8(%rax,%r15,8), %rsi
addq $0x4, %rsi
movq %r12, %rdi
callq 0x2070
movslq 0xc(%rsp), %r12
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movl $0x0, 0x28(%rsp)
movq 0x10(%rsp), %rdi
leal -0x1(%r15), %eax
cltq
cmpq %rax, %r12
jge 0x179b7
movq 0x18(%rsp), %rax
movq (%rax,%rdi,8), %rax
xorl %r14d, %r14d
movq 0x68(%rax), %rcx
movq (%rcx,%r12,8), %rcx
movslq (%rcx), %rdx
cmpq %rdx, %r14
jge 0x179af
leaq (%r14,%r14,4), %rdx
cmpb $0x48, 0x10(%rcx,%rdx)
jne 0x178f3
addq %rdx, %rcx
addq $0x10, %rcx
cmpb $0x0, 0x1(%rcx)
movq %r12, %rbx
jne 0x17998
jmp 0x179a7
movq %r12, %rbx
jmp 0x17998
movl %r12d, %ecx
subl %ebx, %ecx
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
cvtsi2sd %edx, %xmm0
movsd %xmm0, 0x50(%rsp)
movl $0x188, %r15d           # imm = 0x188
pushq $0x11
popq %rbp
xorl %r13d, %r13d
movq 0x68(%rax), %rcx
movq (%rcx,%rbx,8), %rsi
movslq (%rsi), %rdx
cmpq %rdx, %r13
jge 0x17993
cmpb $0x48, -0x1(%rsi,%rbp)
jne 0x1793a
cmpb $0x0, (%rsi,%rbp)
je 0x1797d
movq (%rcx,%r12,8), %rax
imulq $0xc, %r14, %rcx
leaq (%rax,%rcx), %rdi
addq $0x188, %rdi            # imm = 0x188
addq %r15, %rsi
callq 0x3abc
movss 0x275b(%rip), %xmm1     # 0x1a0b8
ucomiss %xmm0, %xmm1
jbe 0x17978
movsd 0x20(%rsp), %xmm0
addsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
incl 0x28(%rsp)
movq 0x10(%rsp), %rdi
incq %r13
movq 0x18(%rsp), %rax
movq (%rax,%rdi,8), %rax
addq $0xc, %r15
addq $0x5, %rbp
jmp 0x1791d
movl 0x8(%rsp), %r15d
incq %rbx
movslq %r15d, %rcx
cmpq %rcx, %rbx
jl 0x178fb
incq %r14
jmp 0x178bb
incq %r12
jmp 0x178a0
movsd 0x20(%rsp), %xmm1
mulsd 0x236b(%rip), %xmm1     # 0x19d30
cvtsi2sdl 0x28(%rsp), %xmm0
subl 0xc(%rsp), %r15d
divsd %xmm0, %xmm1
incl %r15d
cvtsi2sd %r15d, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
movq 0x18(%rsp), %rax
movq (%rax,%rdi,8), %r13
movsbl 0x40(%r13), %edi
addq $0xb0, %r13
callq 0x4969
movsbl %al, %edx
movl 0xc(%rsp), %ecx
movl 0x8(%rsp), %r9d
decl %r9d
leaq 0x2c(%rsp), %r12
movq %r12, (%rsp)
leaq 0x26c7(%rip), %rdi      # 0x1a0e6
movq %r13, %rsi
leaq 0x32(%rsp), %r15
movq %r15, %r8
movsd 0x20(%rsp), %xmm0
movb $0x1, %al
callq 0x20d0
movq 0x10(%rsp), %rcx
movq 0x40(%rsp), %rbx
movq 0x38(%rsp), %r14
incq %rcx
jmp 0x177d4
xorl %edi, %edi
callq 0x2240
movq 0x8594(%rip), %rax      # 0x1fff0
movq (%rax), %rbx
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %r15
movsbl 0x40(%r15), %edi
addq $0xb0, %r15
callq 0x4969
movsbl %al, %r8d
leaq 0x2633(%rip), %rsi      # 0x1a0bc
movq %rbx, %rdi
movq %r14, %rdx
jmp 0x17acb
movq 0x8558(%rip), %rax      # 0x1fff0
movq (%rax), %r14
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %r15
movsbl 0x40(%r15), %edi
addq $0xb0, %r15
callq 0x4969
movsbl %al, %r8d
leaq 0x25f7(%rip), %rsi      # 0x1a0bc
movq %r14, %rdi
movq %rbx, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2160
pushq $0x1
popq %rdi
callq 0x2240
nopl (%rax)