// File: /workspace/github_c_makefile_star2_O1/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 $0x88, %rsp
movq %rdi, 0x18(%rsp)
testl %esi, %esi
jle 0x1849c
movq %rdx, %rbx
leaq 0x1ce6(%rdx), %r14
addq $0x1cec, %rbx           # imm = 0x1CEC
movl %esi, %r15d
xorl %ecx, %ecx
leaq 0x5a(%rsp), %r13
leaq 0x54(%rsp), %rbp
movq %rbx, 0x70(%rsp)
movq %r14, 0x68(%rsp)
movq %r15, 0x60(%rsp)
movq 0x18(%rsp), %rax
movq (%rax,%rcx,8), %rdi
cmpl $0x0, 0x58(%rdi)
je 0x18490
movq %rcx, 0x10(%rsp)
cmpb $0x0, (%r14)
je 0x181a4
movq %r14, %rsi
leaq 0x24(%rsp), %rdx
callq 0x4f1c
movq %r14, %rsi
testl %eax, %eax
jne 0x181b7
jmp 0x184a3
movl $0x0, 0x24(%rsp)
movq 0x68(%rdi), %rax
movq (%rax), %rsi
addq $0x4, %rsi
movq %r13, %rdi
callq 0x2070
cmpb $0x0, (%rbx)
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
je 0x181eb
movq %rbx, %rsi
leaq 0xc(%rsp), %rdx
callq 0x4f1c
movq %rbx, %rsi
testl %eax, %eax
jne 0x18201
jmp 0x184df
movl (%rdi), %eax
movl %eax, 0xc(%rsp)
movq 0x68(%rdi), %rax
movslq (%rdi), %rcx
movq -0x8(%rax,%rcx,8), %rsi
addq $0x4, %rsi
movq %rbp, %rdi
callq 0x2070
movslq 0x24(%rsp), %rsi
movl 0xc(%rsp), %eax
leal -0x1(%rax), %ecx
movsd 0x2ee3(%rip), %xmm1     # 0x1b100
cmpl %ecx, %esi
movq 0x10(%rsp), %rdx
jge 0x1842c
movl $0x0, 0x34(%rsp)
xorps %xmm1, %xmm1
leaq 0x1(%rsi), %rax
movq %rax, 0x40(%rsp)
movq 0x18(%rsp), %rax
movq (%rax,%rdx,8), %rax
movq 0x68(%rax), %rax
movq (%rax,%rsi,8), %rax
cmpl $0x0, (%rax)
jle 0x183e4
leaq 0x1(%rsi), %rcx
movq %rcx, 0x78(%rsp)
xorl %edi, %edi
movq %rsi, 0x38(%rsp)
movsd %xmm1, 0x28(%rsp)
movq %rdi, 0x48(%rsp)
leaq (%rdi,%rdi,4), %rcx
leaq (%rax,%rcx), %rdi
addq $0x10, %rdi
leaq 0x295a(%rip), %rsi      # 0x1abe0
callq 0x2150
testl %eax, %eax
je 0x183af
movslq 0xc(%rsp), %rax
movq 0x40(%rsp), %rbx
cmpq %rax, 0x78(%rsp)
movq 0x10(%rsp), %rdx
movsd 0x28(%rsp), %xmm1
jge 0x183ba
movq 0x18(%rsp), %rax
movq (%rax,%rdx,8), %rax
movq 0x68(%rax), %rbp
movq (%rbp,%rbx,8), %r12
cmpl $0x0, (%r12)
jle 0x1839c
movq 0x38(%rsp), %rax
subl %ebx, %eax
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x80(%rsp)
movl $0x188, %r15d           # imm = 0x188
movl $0x10, %r13d
xorl %r14d, %r14d
movsd %xmm1, 0x28(%rsp)
leaq (%r12,%r13), %rdi
leaq 0x28d2(%rip), %rsi      # 0x1abe0
callq 0x2150
testl %eax, %eax
je 0x18367
movq 0x38(%rsp), %rax
movq (%rbp,%rax,8), %rax
movq 0x48(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,4), %rdi
addq $0x188, %rdi            # imm = 0x188
addq %r15, %r12
movq %r12, %rsi
callq 0x40d5
movss 0x2dc0(%rip), %xmm1     # 0x1b108
ucomiss %xmm0, %xmm1
jbe 0x18367
movsd 0x28(%rsp), %xmm1
addsd 0x80(%rsp), %xmm1
incl 0x34(%rsp)
movq 0x10(%rsp), %rdx
jmp 0x18372
movq 0x10(%rsp), %rdx
movsd 0x28(%rsp), %xmm1
incq %r14
movq 0x18(%rsp), %rax
movq (%rax,%rdx,8), %rax
movq 0x68(%rax), %rbp
movq (%rbp,%rbx,8), %r12
movslq (%r12), %rax
addq $0xc, %r15
addq $0x5, %r13
cmpq %rax, %r14
jl 0x182fd
incq %rbx
movslq 0xc(%rsp), %rax
cmpq %rax, %rbx
jl 0x182b3
jmp 0x183ba
movq 0x10(%rsp), %rdx
movsd 0x28(%rsp), %xmm1
movq 0x48(%rsp), %rdi
incq %rdi
movq 0x18(%rsp), %rax
movq (%rax,%rdx,8), %rax
movq 0x68(%rax), %rax
movq 0x38(%rsp), %rsi
movq (%rax,%rsi,8), %rax
movslq (%rax), %rcx
cmpq %rcx, %rdi
jl 0x18268
movslq 0xc(%rsp), %rax
leaq -0x1(%rax), %rcx
movq 0x40(%rsp), %rdi
movq %rdi, %rsi
cmpq %rcx, %rdi
jl 0x18235
mulsd 0x298a(%rip), %xmm1     # 0x1ad90
xorps %xmm0, %xmm0
cvtsi2sdl 0x34(%rsp), %xmm0
divsd %xmm0, %xmm1
movq 0x70(%rsp), %rbx
movq 0x68(%rsp), %r14
movq 0x60(%rsp), %r15
leaq 0x5a(%rsp), %r13
leaq 0x54(%rsp), %rbp
subl 0x24(%rsp), %eax
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
movq 0x18(%rsp), %rax
movq (%rax,%rdx,8), %r12
movsbl 0x40(%r12), %edi
addq $0xb0, %r12
callq 0x517a
movsbl %al, %edx
movl 0x24(%rsp), %ecx
movl 0xc(%rsp), %r9d
decl %r9d
movq %rbp, (%rsp)
leaq 0x2cbe(%rip), %rdi      # 0x1b136
movq %r12, %rsi
movq %r13, %r8
movsd 0x28(%rsp), %xmm0
movb $0x1, %al
callq 0x20d0
movq 0x10(%rsp), %rcx
incq %rcx
cmpq %r15, %rcx
jne 0x1816d
xorl %edi, %edi
callq 0x2230
movq 0x8b46(%rip), %rax      # 0x20ff0
movq (%rax), %rbx
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %r15
movsbl 0x40(%r15), %edi
addq $0xb0, %r15
callq 0x517a
movsbl %al, %r8d
leaq 0x2c35(%rip), %rsi      # 0x1b10c
movq %rbx, %rdi
movq %r14, %rdx
jmp 0x18519
movq 0x8b0a(%rip), %rax      # 0x20ff0
movq (%rax), %r14
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
movq (%rax,%rcx,8), %r15
movsbl 0x40(%r15), %edi
addq $0xb0, %r15
callq 0x517a
movsbl %al, %r8d
leaq 0x2bf9(%rip), %rsi      # 0x1b10c
movq %r14, %rdi
movq %rbx, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2160
movl $0x1, %edi
callq 0x2230
nopl (%rax)