// File: /workspace/github_c_makefile_star2_O1/josch[P]stride/sheet.c
// Function: Link
int Link(HBOND **HBond, CHAIN **Chain, int Cn1, int Cn2, RESIDUE *Res1_1, 
	 RESIDUE *Res1_2, RESIDUE *Res2_2, RESIDUE *Res2_1, RESIDUE *CRes1, 
	 RESIDUE *CRes2, float **PhiPsiMap, PATTERN **Pattern, int *NumPat, 
	 char *Text, float Treshold, COMMAND *Cmd, int Test)
{

  int BondNumber1, BondNumber2, Flag = 0;
  static char *Result[2] = {" NO \n"," YES \n"};
  float Prob1, Prob2, Conf, Coeff;

  if( (BondNumber1 = FindPolInt(HBond,Res1_1,Res1_2)) == ERR ) 
    return(FAILURE);

  if( (BondNumber2 = FindPolInt(HBond,Res2_2,Res2_1)) == ERR ) 
    return(FAILURE);

  if( CRes1 == NULL ) {
    if( CRes2->Prop->PhiZn == ERR || CRes2->Prop->PsiZn == ERR )
      return(FAILURE);
    Conf = PhiPsiMap[CRes2->Prop->PhiZn][CRes2->Prop->PsiZn];
  }
  else
  if( CRes2 == NULL ) {
    if( CRes1->Prop->PhiZn == ERR || CRes1->Prop->PsiZn == ERR )
      return(FAILURE);
    Conf = PhiPsiMap[CRes1->Prop->PhiZn][CRes1->Prop->PsiZn];
  }
  else {
    if( CRes2->Prop->PhiZn == ERR || CRes2->Prop->PsiZn == ERR ||
        CRes1->Prop->PhiZn == ERR || CRes1->Prop->PsiZn == ERR )
      return(FAILURE);
    Conf = 
      0.5*(PhiPsiMap[CRes1->Prop->PhiZn][CRes1->Prop->PsiZn]+
	   PhiPsiMap[CRes2->Prop->PhiZn][CRes2->Prop->PsiZn]);
  }
  Coeff = 1+Cmd->C1_E+Cmd->C2_E*Conf;
  Prob1 = HBond[BondNumber1]->Energy*Coeff;
  Prob2 = HBond[BondNumber2]->Energy*Coeff;

  if( Prob1 < Treshold && Prob2 < Treshold ) {

    if( !Test ) {
      Pattern[*NumPat] = (PATTERN *)ckalloc(sizeof(PATTERN));
      Pattern[*NumPat]->ExistPattern = YES;
      Pattern[*NumPat]->Hb1 = HBond[BondNumber1];
      Pattern[*NumPat]->Hb2 = HBond[BondNumber2];
      Pattern[*NumPat]->Nei1 = NULL;
      Pattern[*NumPat]->Nei2 = NULL;
      strcpy(Pattern[*NumPat]->Type,Text);
      (*NumPat)++;
    }
    Flag = 1;
  }

  if( Cmd->Info && Flag ) {
    fprintf(stdout,"%s %c: %3s %c: %3s | %c: %3s %c: %3s | ",
	    Text,
	    Chain[Cn1]->Id,Res1_1->PDB_ResNumb,
	    Chain[Cn2]->Id,Res1_2->PDB_ResNumb,
	    Chain[Cn2]->Id,Res2_2->PDB_ResNumb,
	    Chain[Cn1]->Id,Res2_1->PDB_ResNumb);
    fprintf(stdout,"%8.6f %6.4f | ", Prob1,HBond[BondNumber1]->Energy);
    fprintf(stdout,"%8.6f %6.4f | ", Prob2,HBond[BondNumber2]->Energy);

    if( CRes1 != NULL && 
        CRes1->Prop->PhiZn != ERR && CRes1->Prop->PsiZn != ERR )
      fprintf(stdout,"%6.4f %2d %2d | ",   
	      PhiPsiMap[CRes1->Prop->PhiZn][CRes1->Prop->PsiZn],
	      CRes1->Prop->PhiZn,CRes1->Prop->PsiZn);
    else
      fprintf(stdout,"000000 00 00 | ");

    if( CRes2 != NULL &&
        CRes2->Prop->PhiZn != ERR && CRes2->Prop->PsiZn != ERR )
      fprintf(stdout,"%6.4f %2d %2d | ",   
	      PhiPsiMap[CRes2->Prop->PhiZn][CRes2->Prop->PsiZn],
	      CRes2->Prop->PhiZn,CRes2->Prop->PsiZn);
    else
      fprintf(stdout,"000000 00 00 | ");
    
    fprintf(stdout,"%s",Result[Flag]);
  }
  
  return(Flag);
  
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movaps %xmm0, 0x10(%rsp)
movq %r9, %r15
movq %r8, %r12
movl %ecx, %ebp
movl %edx, 0xc(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, %r14
movq %r8, %rsi
movq %r9, %rdx
callq 0xa4c5
xorl %ebx, %ebx
cmpl $-0x1, %eax
je 0x76b4
movl %eax, %r13d
movl %ebp, 0x8(%rsp)
movq 0x88(%rsp), %rbp
movq 0x80(%rsp), %rsi
movq %r14, %rdi
movq %rbp, %rdx
callq 0xa4c5
cmpl $-0x1, %eax
je 0x76b4
movq 0xa0(%rsp), %r8
movq 0x98(%rsp), %rcx
movq 0x90(%rsp), %r10
testq %r10, %r10
je 0x7389
testq %rcx, %rcx
je 0x7392
movq 0x768(%rcx), %rdx
movslq 0xc(%rdx), %rcx
cmpq $-0x1, %rcx
je 0x76b4
movslq 0x10(%rdx), %rdx
cmpq $-0x1, %rdx
je 0x76b4
movq 0x768(%r10), %rdi
movslq 0xc(%rdi), %rsi
cmpq $-0x1, %rsi
je 0x76b4
movslq 0x10(%rdi), %rdi
cmpq $-0x1, %rdi
je 0x76b4
movq (%r8,%rsi,8), %rsi
movss (%rsi,%rdi,4), %xmm2
movq (%r8,%rcx,8), %rcx
addss (%rcx,%rdx,4), %xmm2
mulss 0x12845(%rip), %xmm2    # 0x19bcc
jmp 0x73be
movq 0x768(%rcx), %rdx
jmp 0x7399
movq 0x768(%r10), %rdx
movslq 0xc(%rdx), %rcx
cmpq $-0x1, %rcx
je 0x76b4
movslq 0x10(%rdx), %rdx
cmpq $-0x1, %rdx
je 0x76b4
movq (%r8,%rcx,8), %rcx
movss (%rcx,%rdx,4), %xmm2
movq 0xc0(%rsp), %rsi
movss 0x1d40(%rsi), %xmm0
addss 0x127ba(%rip), %xmm0    # 0x19b90
movq 0xb8(%rsp), %rdx
mulss 0x1d44(%rsi), %xmm2
addss %xmm0, %xmm2
movslq %r13d, %rcx
movq %rcx, 0x20(%rsp)
movq (%r14,%rcx,8), %rcx
movss 0x1c(%rcx), %xmm4
mulss %xmm2, %xmm4
cltq
movq %rax, 0x28(%rsp)
movq (%r14,%rax,8), %rax
mulss 0x1c(%rax), %xmm2
movaps %xmm4, %xmm0
movaps 0x10(%rsp), %xmm3
cmpltps %xmm3, %xmm0
movaps %xmm2, %xmm1
cmpltps %xmm3, %xmm1
andps %xmm0, %xmm1
movd %xmm1, %r13d
xorl %ebx, %ebx
testb $0x1, %r13b
je 0x74f2
movl $0x1, %ebx
cmpl $0x0, 0xc8(%rsp)
jne 0x74f2
movl $0x428, %edi            # imm = 0x428
movaps %xmm2, 0x10(%rsp)
movaps %xmm4, 0x30(%rsp)
callq 0x595c
movq 0xb0(%rsp), %rdx
movslq (%rdx), %rcx
movq 0xa8(%rsp), %rsi
movq %rax, (%rsi,%rcx,8)
movl $0x1, 0x20(%rax)
movq 0x20(%rsp), %rax
movq (%r14,%rax,8), %rax
movslq (%rdx), %rcx
movq (%rsi,%rcx,8), %rdx
movq %rax, (%rdx)
movq 0x28(%rsp), %rax
movq (%r14,%rax,8), %rax
movq (%rsi,%rcx,8), %rdx
movq %rax, 0x8(%rdx)
movq (%rsi,%rcx,8), %rax
xorl %edx, %edx
movq %rdx, 0x10(%rax)
movq (%rsi,%rcx,8), %rax
movq %rdx, 0x18(%rax)
movq (%rsi,%rcx,8), %rdi
addq $0x24, %rdi
movq 0xb8(%rsp), %rsi
callq 0x2070
movq 0xc0(%rsp), %rsi
movq 0xb8(%rsp), %rdx
movaps 0x30(%rsp), %xmm4
movaps 0x10(%rsp), %xmm2
movq 0x90(%rsp), %r10
movq 0xb0(%rsp), %rax
incl (%rax)
cmpl $0x0, 0x1d60(%rsi)
sete %al
notb %r13b
orb %al, %r13b
testb $0x1, %r13b
jne 0x76b4
movq 0x19abd(%rip), %r13     # 0x20fd0
movq (%r13), %rdi
movslq 0xc(%rsp), %rax
movq 0x40(%rsp), %rsi
movq (%rsi,%rax,8), %rax
movsbl 0x40(%rax), %ecx
addq $0x4, %r12
movslq 0x8(%rsp), %rax
movq (%rsi,%rax,8), %rax
movsbl 0x40(%rax), %r9d
addq $0x4, %r15
movq 0x80(%rsp), %r11
addq $0x4, %r11
addq $0x4, %rbp
subq $0x8, %rsp
leaq 0x126a7(%rip), %rsi     # 0x19c01
movq %r12, %r8
xorl %eax, %eax
pushq %rbp
movq %r13, %rbp
pushq %rcx
pushq %r11
pushq %r9
pushq %r15
movq %r10, %r12
movaps %xmm2, 0x40(%rsp)
movaps %xmm4, 0x60(%rsp)
callq 0x2160
addq $0x30, %rsp
movq (%r13), %rdi
xorps %xmm0, %xmm0
cvtss2sd 0x30(%rsp), %xmm0
movq 0x20(%rsp), %rax
movq (%r14,%rax,8), %rax
xorps %xmm1, %xmm1
cvtss2sd 0x1c(%rax), %xmm1
leaq 0x12684(%rip), %r15     # 0x19c29
movq %r15, %rsi
movb $0x2, %al
callq 0x2160
movq (%r13), %rdi
xorps %xmm0, %xmm0
cvtss2sd 0x10(%rsp), %xmm0
movq 0x28(%rsp), %rax
movq (%r14,%rax,8), %rax
xorps %xmm1, %xmm1
cvtss2sd 0x1c(%rax), %xmm1
movq %r15, %rsi
movb $0x2, %al
callq 0x2160
testq %r12, %r12
je 0x7620
movq 0x768(%r12), %rax
movslq 0xc(%rax), %rdx
cmpq $-0x1, %rdx
je 0x7620
movslq 0x10(%rax), %rcx
cmpq $-0x1, %rcx
je 0x7620
movq (%rbp), %rdi
movq 0xa0(%rsp), %rax
movq (%rax,%rdx,8), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rcx,4), %xmm0
leaq 0x12621(%rip), %rsi     # 0x19c38
movb $0x1, %al
callq 0x2160
jmp 0x763a
movq (%rbp), %rcx
leaq 0x1261e(%rip), %rdi     # 0x19c49
movl $0xf, %esi
movl $0x1, %edx
callq 0x2240
movq 0x98(%rsp), %r13
testq %r13, %r13
je 0x768a
movq 0x768(%r13), %rax
movslq 0xc(%rax), %rdx
cmpq $-0x1, %rdx
je 0x768a
movslq 0x10(%rax), %rcx
cmpq $-0x1, %rcx
je 0x768a
movq (%rbp), %rdi
movq 0xa0(%rsp), %rax
movq (%rax,%rdx,8), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rcx,4), %xmm0
leaq 0x125b7(%rip), %rsi     # 0x19c38
movb $0x1, %al
callq 0x2160
jmp 0x76a4
movq (%rbp), %rcx
leaq 0x125b4(%rip), %rdi     # 0x19c49
movl $0xf, %esi
movl $0x1, %edx
callq 0x2240
movq (%rbp), %rsi
leaq 0x1254b(%rip), %rdi     # 0x19bfa
callq 0x20e0
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq