// File: /workspace/github_c_makefile_star2_O2/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 0x9813
movl %eax, %ebx
xorl %eax, %eax
cmpl $-0x1, %ebx
je 0x6a77
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 0x9813
cmpl $-0x1, %eax
je 0x6a75
movq 0x80(%rsp), %r8
movq 0x78(%rsp), %rcx
movq 0x70(%rsp), %rsi
testq %rsi, %rsi
je 0x6833
testq %rcx, %rcx
je 0x683c
movq 0x768(%rcx), %rdx
movslq 0xc(%rdx), %rcx
cmpq $-0x1, %rcx
je 0x6a75
movslq 0x10(%rdx), %rdx
cmpq $-0x1, %rdx
je 0x6a75
movq 0x768(%rsi), %rdi
movslq 0xc(%rdi), %rsi
cmpq $-0x1, %rsi
je 0x6a75
movslq 0x10(%rdi), %rdi
cmpq $-0x1, %rdi
je 0x6a75
movq (%r8,%rsi,8), %rsi
movss (%rsi,%rdi,4), %xmm1
movq (%r8,%rcx,8), %rcx
addss (%rcx,%rdx,4), %xmm1
mulss 0x123ab(%rip), %xmm1    # 0x18bdc
jmp 0x6868
movq 0x768(%rcx), %rdx
jmp 0x6843
movq 0x768(%rsi), %rdx
movslq 0xc(%rdx), %rcx
cmpq $-0x1, %rcx
je 0x6a75
movslq 0x10(%rdx), %rdx
cmpq $-0x1, %rdx
je 0x6a75
movq (%r8,%rcx,8), %rcx
movss (%rcx,%rdx,4), %xmm1
movq 0xa0(%rsp), %rsi
movss 0x1d40(%rsi), %xmm0
addss 0x12320(%rip), %xmm0    # 0x18ba0
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 0x6a75
cltq
movq %rax, 0x18(%rsp)
movq (%r14,%rax,8), %rax
mulss 0x1c(%rax), %xmm1
ucomiss %xmm1, %xmm0
movl $0x0, %eax
jbe 0x6a77
movq 0x98(%rsp), %rdx
cmpl $0x0, 0xa8(%rsp)
movss %xmm1, 0x8(%rsp)
movss %xmm2, 0x4(%rsp)
jne 0x6973
movl $0x428, %edi            # imm = 0x428
callq 0x50f4
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
andq $0x0, 0x10(%rax)
movq (%rsi,%rcx,8), %rax
andq $0x0, 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)
pushq $0x1
popq %rax
cmpl $0x0, 0x1d60(%rsi)
je 0x6a77
movq 0x19646(%rip), %rax     # 0x1ffd0
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 0x12249(%rip), %rsi     # 0x18c11
movq %r12, %r8
xorl %eax, %eax
pushq %rbp
pushq %rcx
pushq %r13
pushq %r9
pushq %r15
callq 0x2160
addq $0x30, %rsp
movq 0x195eb(%rip), %rcx     # 0x1ffd0
movq (%rcx), %rdi
cvtss2sd 0x4(%rsp), %xmm0
movq (%r14,%rbx,8), %rax
movq %rcx, %rbx
cvtss2sd 0x1c(%rax), %xmm1
leaq 0x12238(%rip), %r15     # 0x18c39
movq %r15, %rsi
movb $0x2, %al
callq 0x2160
movq (%rbx), %rdi
cvtss2sd 0x8(%rsp), %xmm0
movq 0x18(%rsp), %rax
movq (%r14,%rax,8), %rax
cvtss2sd 0x1c(%rax), %xmm1
movq %r15, %rsi
movb $0x2, %al
callq 0x2160
movq 0x70(%rsp), %rax
testq %rax, %rax
je 0x6a86
movq 0x768(%rax), %rax
movslq 0xc(%rax), %rdx
cmpq $-0x1, %rdx
je 0x6a86
movslq 0x10(%rax), %rcx
cmpq $-0x1, %rcx
je 0x6a86
movq (%rbx), %rdi
movq 0x80(%rsp), %rax
movq (%rax,%rdx,8), %rax
cvtss2sd (%rax,%rcx,4), %xmm0
leaq 0x121dc(%rip), %rsi     # 0x18c48
movb $0x1, %al
callq 0x2160
jmp 0x6a9b
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rcx
leaq 0x121c9(%rip), %rdi     # 0x18c59
pushq $0xf
popq %rsi
pushq $0x1
popq %rdx
callq 0x2250
movq 0x78(%rsp), %rax
testq %rax, %rax
je 0x6ae4
movq 0x768(%rax), %rax
movslq 0xc(%rax), %rdx
cmpq $-0x1, %rdx
je 0x6ae4
movslq 0x10(%rax), %rcx
cmpq $-0x1, %rcx
je 0x6ae4
movq (%rbx), %rdi
movq 0x80(%rsp), %rax
movq (%rax,%rdx,8), %rax
cvtss2sd (%rax,%rcx,4), %xmm0
leaq 0x1216d(%rip), %rsi     # 0x18c48
movb $0x1, %al
callq 0x2160
jmp 0x6af9
movq (%rbx), %rcx
leaq 0x1216b(%rip), %rdi     # 0x18c59
pushq $0xf
popq %rsi
pushq $0x1
popq %rdx
callq 0x2250
movq (%rbx), %rsi
leaq 0x12107(%rip), %rdi     # 0x18c0a
callq 0x20e0
pushq $0x1
popq %rax
jmp 0x6a77