// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movss %xmm0, 0x14(%rsp)
movq %r9, %r15
movq %r8, %r12
movl %ecx, %ebp
movl %edx, 0x10(%rsp)
movq %rsi, %r13
movq %rdi, %r14
movq %r8, %rsi
movq %r9, %rdx
callq 0xa3cd
movl %eax, %ebx
xorl %eax, %eax
cmpl $-0x1, %ebx
je 0x74e6
movq %r13, 0x20(%rsp)
movl %ebp, 0xc(%rsp)
movq 0x68(%rsp), %rbp
movq 0x60(%rsp), %r13
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0xa3cd
cmpl $-0x1, %eax
je 0x74e4
movq 0x80(%rsp), %r8
movq 0x78(%rsp), %rcx
movq 0x70(%rsp), %rsi
testq %rsi, %rsi
je 0x7291
testq %rcx, %rcx
je 0x729a
movq 0x768(%rcx), %rdx
movslq 0xc(%rdx), %rcx
cmpq $-0x1, %rcx
je 0x74e4
movslq 0x10(%rdx), %rdx
cmpq $-0x1, %rdx
je 0x74e4
movq 0x768(%rsi), %rdi
movslq 0xc(%rdi), %rsi
cmpq $-0x1, %rsi
je 0x74e4
movslq 0x10(%rdi), %rdi
cmpq $-0x1, %rdi
je 0x74e4
movq (%r8,%rsi,8), %rsi
movss (%rsi,%rdi,4), %xmm1
movq (%r8,%rcx,8), %rcx
addss (%rcx,%rdx,4), %xmm1
mulss 0x1294d(%rip), %xmm1    # 0x19bdc
jmp 0x72c6
movq 0x768(%rcx), %rdx
jmp 0x72a1
movq 0x768(%rsi), %rdx
movslq 0xc(%rdx), %rcx
cmpq $-0x1, %rcx
je 0x74e4
movslq 0x10(%rdx), %rdx
cmpq $-0x1, %rdx
je 0x74e4
movq (%r8,%rcx,8), %rcx
movss (%rcx,%rdx,4), %xmm1
movq 0xa0(%rsp), %rsi
movss 0x1d40(%rsi), %xmm0
addss 0x128c2(%rip), %xmm0    # 0x19ba0
mulss 0x1d44(%rsi), %xmm1
addss %xmm0, %xmm1
movslq %ebx, %rbx
movq (%r14,%rbx,8), %rcx
movss 0x1c(%rcx), %xmm2
mulss %xmm1, %xmm2
movss 0x14(%rsp), %xmm0
ucomiss %xmm2, %xmm0
jbe 0x74e4
cltq
movq %rax, 0x18(%rsp)
movq (%r14,%rax,8), %rax
mulss 0x1c(%rax), %xmm1
ucomiss %xmm1, %xmm0
movl $0x0, %eax
jbe 0x74e6
movq 0x98(%rsp), %rdx
cmpl $0x0, 0xa8(%rsp)
movss %xmm1, 0x8(%rsp)
movss %xmm2, 0x4(%rsp)
jne 0x73d1
movl $0x428, %edi            # imm = 0x428
callq 0x5880
movq 0x90(%rsp), %rdx
movslq (%rdx), %rcx
movq 0x88(%rsp), %rsi
movq %rax, (%rsi,%rcx,8)
movl $0x1, 0x20(%rax)
movq (%r14,%rbx,8), %rax
movslq (%rdx), %rcx
movq (%rsi,%rcx,8), %rdx
movq %rax, (%rdx)
movq 0x18(%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 0x98(%rsp), %rsi
callq 0x2070
movq 0xa0(%rsp), %rsi
movq 0x98(%rsp), %rdx
movq 0x90(%rsp), %rax
incl (%rax)
movl $0x1, %eax
cmpl $0x0, 0x1d60(%rsi)
je 0x74e6
movq 0x19be6(%rip), %rax     # 0x20fd0
movq (%rax), %rdi
movslq 0x10(%rsp), %rax
movq 0x20(%rsp), %rsi
movq (%rsi,%rax,8), %rax
movsbl 0x40(%rax), %ecx
addq $0x4, %r12
movslq 0xc(%rsp), %rax
movq (%rsi,%rax,8), %rax
movsbl 0x40(%rax), %r9d
addq $0x4, %r15
addq $0x4, %r13
addq $0x4, %rbp
subq $0x8, %rsp
leaq 0x127e9(%rip), %rsi     # 0x19c11
movq %r12, %r8
xorl %eax, %eax
pushq %rbp
pushq %rcx
pushq %r13
pushq %r9
pushq %r15
callq 0x2160
addq $0x30, %rsp
movq 0x19b8b(%rip), %rcx     # 0x20fd0
movq (%rcx), %rdi
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rsp), %xmm0
movq (%r14,%rbx,8), %rax
movq %rcx, %rbx
xorps %xmm1, %xmm1
cvtss2sd 0x1c(%rax), %xmm1
leaq 0x127d2(%rip), %r15     # 0x19c39
movq %r15, %rsi
movb $0x2, %al
callq 0x2160
movq (%rbx), %rdi
xorps %xmm0, %xmm0
cvtss2sd 0x8(%rsp), %xmm0
movq 0x18(%rsp), %rax
movq (%r14,%rax,8), %rax
xorps %xmm1, %xmm1
cvtss2sd 0x1c(%rax), %xmm1
movq %r15, %rsi
movb $0x2, %al
callq 0x2160
movq 0x70(%rsp), %rax
testq %rax, %rax
je 0x74f5
movq 0x768(%rax), %rax
movslq 0xc(%rax), %rdx
cmpq $-0x1, %rdx
je 0x74f5
movslq 0x10(%rax), %rcx
cmpq $-0x1, %rcx
je 0x74f5
movq (%rbx), %rdi
movq 0x80(%rsp), %rax
movq (%rax,%rdx,8), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rcx,4), %xmm0
leaq 0x1276d(%rip), %rsi     # 0x19c48
movb $0x1, %al
callq 0x2160
jmp 0x750e
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rcx
leaq 0x1275a(%rip), %rdi     # 0x19c59
movl $0xf, %esi
movl $0x1, %edx
callq 0x2240
movq 0x78(%rsp), %rax
testq %rax, %rax
je 0x755a
movq 0x768(%rax), %rax
movslq 0xc(%rax), %rdx
cmpq $-0x1, %rdx
je 0x755a
movslq 0x10(%rax), %rcx
cmpq $-0x1, %rcx
je 0x755a
movq (%rbx), %rdi
movq 0x80(%rsp), %rax
movq (%rax,%rdx,8), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rcx,4), %xmm0
leaq 0x126f7(%rip), %rsi     # 0x19c48
movb $0x1, %al
callq 0x2160
jmp 0x7573
movq (%rbx), %rcx
leaq 0x126f5(%rip), %rdi     # 0x19c59
movl $0xf, %esi
movl $0x1, %edx
callq 0x2240
movq (%rbx), %rsi
leaq 0x1268d(%rip), %rdi     # 0x19c0a
callq 0x20e0
movl $0x1, %eax
jmp 0x74e6