// File: /workspace/github_c_makefile_star2_O3/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 $0x68, %rsp
movq %rdi, 0x18(%rsp)
testl %esi, %esi
jle 0x182dc
movq %rdx, %rbx
leaq 0x1ce6(%rdx), %r14
addq $0x1cec, %rbx           # imm = 0x1CEC
movl %esi, %eax
movq %rax, 0x58(%rsp)
xorl %ecx, %ecx
leaq 0x42(%rsp), %r15
movq %rbx, 0x50(%rsp)
movq %r14, 0x48(%rsp)
movq 0x18(%rsp), %rax
movq (%rax,%rcx,8), %rdi
cmpl $0x0, 0x58(%rdi)
je 0x182ce
movq %rcx, 0x10(%rsp)
cmpb $0x0, (%r14)
je 0x18033
movq %r14, %rsi
leaq 0xc(%rsp), %rdx
callq 0x4e44
movq %r14, %rsi
testl %eax, %eax
jne 0x18046
jmp 0x182e3
movl $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), %rbp
je 0x1809b
movq %rbp, %rdi
movq %rbx, %rsi
leaq 0x8(%rsp), %rdx
callq 0x4e44
testl %eax, %eax
je 0x1831f
leaq 0x32(%rsp), %rdi
movq %rbx, %rsi
callq 0x2070
movl 0x8(%rsp), %r12d
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rdi
movq (%rax,%rdi,8), %rbp
jmp 0x180c0
movslq (%rbp), %r12
movl %r12d, 0x8(%rsp)
movq 0x68(%rbp), %rax
movq -0x8(%rax,%r12,8), %rsi
addq $0x4, %rsi
leaq 0x32(%rsp), %rdi
callq 0x2070
movq 0x10(%rsp), %rdi
movl 0xc(%rsp), %eax
leal -0x1(%r12), %ecx
cmpl %ecx, %eax
jge 0x18262
movslq %eax, %r8
movl $0x0, 0x24(%rsp)
xorpd %xmm1, %xmm1
leaq 0x1(%r8), %rax
movq %rax, 0x38(%rsp)
movq 0x68(%rbp), %rax
movq (%rax,%r8,8), %rax
cmpl $0x0, (%rax)
jle 0x18225
xorl %r14d, %r14d
leaq (%r14,%r14,4), %rdx
movb $0x1, %cl
cmpb $0x48, 0x10(%rax,%rdx)
jne 0x18118
addq %rdx, %rax
addq $0x10, %rax
cmpb $0x0, 0x1(%rax)
setne %cl
testb %cl, %cl
je 0x1820e
movslq %r12d, %rax
movq 0x38(%rsp), %rcx
movq %rcx, %r15
cmpq %rax, %rcx
jge 0x1820e
movq 0x68(%rbp), %rax
movq (%rax,%r15,8), %rsi
cmpl $0x0, (%rsi)
jle 0x181ff
movl %r8d, %ecx
subl %r15d, %ecx
movl %ecx, %edx
negl %edx
cmovsl %ecx, %edx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
movsd %xmm0, 0x60(%rsp)
movl $0x188, %r12d           # imm = 0x188
movl $0x11, %r13d
xorl %ebx, %ebx
cmpb $0x48, -0x1(%rsi,%r13)
jne 0x1817c
cmpb $0x0, (%rsi,%r13)
je 0x181d2
movsd %xmm1, 0x28(%rsp)
movq %r8, %rbp
movq (%rax,%r8,8), %rax
leaq (%r14,%r14,2), %rcx
leaq (%rax,%rcx,4), %rdi
addq $0x188, %rdi            # imm = 0x188
addq %r12, %rsi
callq 0x40c3
movss 0x2f68(%rip), %xmm1     # 0x1b110
ucomiss %xmm0, %xmm1
jbe 0x181c4
movsd 0x28(%rsp), %xmm1
addsd 0x60(%rsp), %xmm1
incl 0x24(%rsp)
movq 0x10(%rsp), %rdi
jmp 0x181cf
movq 0x10(%rsp), %rdi
movsd 0x28(%rsp), %xmm1
movq %rbp, %r8
incq %rbx
movq 0x18(%rsp), %rax
movq (%rax,%rdi,8), %rbp
movq 0x68(%rbp), %rax
movq (%rax,%r15,8), %rsi
movslq (%rsi), %rcx
addq $0xc, %r12
addq $0x5, %r13
cmpq %rcx, %rbx
jl 0x1816d
movl 0x8(%rsp), %r12d
incq %r15
movslq %r12d, %rax
cmpq %rax, %r15
jl 0x18134
incq %r14
movq 0x68(%rbp), %rax
movq (%rax,%r8,8), %rax
movslq (%rax), %rcx
cmpq %rcx, %r14
jl 0x180fd
leal -0x1(%r12), %eax
cltq
movq 0x38(%rsp), %rcx
movq %rcx, %r8
cmpq %rax, %rcx
jl 0x180e0
movl 0xc(%rsp), %eax
mulsd 0x2b37(%rip), %xmm1     # 0x1ad80
xorps %xmm0, %xmm0
cvtsi2sdl 0x24(%rsp), %xmm0
divsd %xmm0, %xmm1
movq 0x50(%rsp), %rbx
movq 0x48(%rsp), %r14
jmp 0x1826a
movsd 0x2e9e(%rip), %xmm1     # 0x1b108
subl %eax, %r12d
incl %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
movsbl 0x40(%rbp), %edi
addq $0xb0, %rbp
callq 0x50a2
movsbl %al, %edx
movl 0xc(%rsp), %ecx
movl 0x8(%rsp), %r9d
decl %r9d
leaq 0x32(%rsp), %rax
movq %rax, (%rsp)
leaq 0x2e8d(%rip), %rdi      # 0x1b13e
movq %rbp, %rsi
leaq 0x42(%rsp), %r15
movq %r15, %r8
movsd 0x28(%rsp), %xmm0
movb $0x1, %al
callq 0x20d0
movq 0x10(%rsp), %rcx
incq %rcx
cmpq 0x58(%rsp), %rcx
jne 0x17ffc
xorl %edi, %edi
callq 0x2230
movq 0x8d06(%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 0x50a2
movsbl %al, %r8d
leaq 0x2dfd(%rip), %rsi      # 0x1b114
movq %rbx, %rdi
movq %r14, %rdx
jmp 0x18359
movq 0x8cca(%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 0x50a2
movsbl %al, %r8d
leaq 0x2dc1(%rip), %rsi      # 0x1b114
movq %r14, %rdi
movq %rbx, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2160
movl $0x1, %edi
callq 0x2230
nopl (%rax)