// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/anharmonic_tnm.c
// Function: Assign_interactions_SB
void Assign_interactions_SB(struct interaction *Int_list, int N_int,
			    struct interaction **Int_KB, int *aa_seq)
{
  printf("Assigning structure-based interactions\n");
  printf("--> NPAR  = %8d        number of parameters: ",NPAR);
  printf("2 (Morse) 3 (barrier)\n");
  printf("--> N_int = %8d        number of native interactions\n",N_int);
  printf("--> Int_list[]         list of all native interactions\n");
  printf("--> Int_KB[][]         knowledge-based interactions\n");
  printf("--> aa_seq[]           a.a. type ID of each residue in the seq\n");
  printf("--> OUTPUT: Int_list gets filled up with energy parameters\n");

  int n, n_err=0; struct interaction *Int=Int_list, *I_KB=NULL;
  for(n=0; n<N_int; n++){
    double V0, r3;
    Int->r1=100;
    if(NPAR==2 || aa_seq==NULL || Int_KB==NULL){
      Int->A1=0;
      Int->A4=E_repulsion;
      double fc;
      if(POW){
	Int->A2=-2*Int->A4*pow(Int->r0, -lambda_2);
	fc=pow(C_THR, -lambda);
      }else{
	Int->A2=-2*Int->A4*exp(-lambda_2*Int->r0);
	fc=exp(-lambda*C_THR);
      }
      double fc2=fc*fc;
      Int->A0=-Int->A4*fc2*fc2-Int->A2*fc2;
      V0=Compute_V(Int, Int->r0);
    }else{
      int a=aa_seq[Int->res1], b=aa_seq[Int->res1];
      I_KB=Int_KB[a]+b;
      V0=Compute_V(I_KB, Int->r0);
      if(Determine_Ak(Int, V0, Int->sec_der, n))n_err++;
      if(Int->A4<0){
	printf("POWER= %d lambda= %.2f (rSB/rKB)= %.3g\n",
	       POW, lambda, Int->r0/I_KB->r0);
      }
    }
    Int->E_min=V0;
    double kk=Compute_V2(Int, Int->r0, 0);
    if(fabs(1-Int->sec_der/kk)>0.001){
      printf("ERROR in Assign_int_SB n=%d r= %.2f ", n, Int->r0);
      printf("k= %.4g (old) %.4g (new) /= %.3f  ", 
	     Int->sec_der, kk, Int->sec_der/kk);
      printf("V0= %.3g\n", Compute_V(Int, Int->r0));
      if(I_KB){
	printf("Knowledge-based: NPAR=%d r0= %.3g r1=%.2g r2=%.2g r3=%.2g\n",
	       NPAR, I_KB->r0, I_KB->r1, I_KB->r2, I_KB->r3);
	printf("b= %.3g %.3g %.3g\n", I_KB->b3, I_KB->b4, I_KB->b5);
	printf("V_KB=  %.3g\n",Compute_V(I_KB, Int->r0));
      }
      exit(8);
    }
    if(NPAR==2){
      // Interpolation is not a good idea
      //r3=(Int->r0+C_THR)*0.5; if(r3>C1_THR)r3=C1_THR;
      r3=C_THR;
      Int->r1=r3; Int->r2=r3;
    }else{
      // Interpolate structure based and knowledge based
      float r1, r2;
      if(Int->r0<I_KB->r0){r1=Int->r0; r2=I_KB->r0;}
      else{r1=I_KB->r0; r2=Int->r0;}
      //r3=(r2+C_THR)*0.5; if(r3>C1_THR)r3=C1_THR;
      r3=C_THR;
      float d=(r2-r1)/3; r1+=d; r2-=d;
      Interpolation(Int, I_KB, r1, r2);
      if(isnan(Int->a0) || isnan(Int->a1) || isnan(Int->a2) ||
	 isnan(Int->a3) || isnan(Int->a4) || isnan(Int->a5)){
	printf("ERROR in Assign_SB, n=%d KB= %d\n", n, NPAR);
	printf("Interpolation r1= %.2f r2= %.2f\n", r1, r2);
	printf("a= %.3g %.3g %.3g %.3g %.3g %.3g\n",
	       Int->a0, Int->a1, Int->a2, Int->a3, Int->a4, Int->a5);
	exit(8);
      }
    }
    Int->r3=r3;
    /*Interpolation(Int, NULL, r3, C_THR);
    if(isnan(Int->b3) || isnan(Int->b4) || isnan(Int->b5)){
      printf("ERROR in Assign_SB, n=%d KB= %d\n", n, NPAR);
      printf("Interpolation r3= %.2f r4= %.2f\n", r3, C_THR);
      printf("b= %.3g %.3g %.3g\n", Int->b3, Int->b4, Int->b5);
      exit(8);
      }*/
    Int++;
  }
  printf("%d SB interactions assigned\n", N_int);
  if(n_err){
    printf("ERRORS in SB in %d pairs of residues/atoms\n", n_err);
    //exit(8);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r15
movq %rdx, %r12
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x36611(%rip), %rdi     # 0x521af
callq 0x2090
movl 0x482d7(%rip), %esi     # 0x63e80
leaq 0x361ec(%rip), %rdi     # 0x51d9c
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x20f0
leaq 0x36615(%rip), %rdi     # 0x521d6
callq 0x2090
leaq 0x361fd(%rip), %rdi     # 0x51dca
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
leaq 0x3660f(%rip), %rdi     # 0x521ec
callq 0x2090
leaq 0x3663a(%rip), %rdi     # 0x52223
callq 0x2090
leaq 0x36662(%rip), %rdi     # 0x52257
callq 0x2090
leaq 0x36695(%rip), %rdi     # 0x52296
callq 0x2090
testl %ebp, %ebp
movl $0x0, %r13d
movl %ebp, 0xc(%rsp)
cmovgl %ebp, %r13d
movapd 0x2f4a2(%rip), %xmm2    # 0x4b0c0
movl $0x0, 0x8(%rsp)
xorl %ebp, %ebp
cmpl %ebp, %r13d
je 0x1bf1c
movl $0x42c80000, 0x10(%rbx) # imm = 0x42C80000
testq %r12, %r12
je 0x1bcd8
testq %r15, %r15
je 0x1bcd8
cmpl $0x2, 0x4822f(%rip)     # 0x63e80
je 0x1bcd8
movslq 0x8(%rbx), %rax
movslq (%r15,%rax,4), %rax
imulq $0xc8, %rax, %r14
addq (%r12,%rax,8), %r14
cvtsd2ss 0x20(%rbx), %xmm0
movq %r14, %rdi
callq 0x1ac14
cvtss2sd %xmm0, %xmm0
movsd 0x28(%rbx), %xmm1
movq %rbx, %rdi
movsd %xmm0, (%rsp)
movl %ebp, %esi
callq 0x1c5ab
cmpl $0x1, %eax
sbbl $-0x1, 0x8(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x48(%rbx), %xmm0
jbe 0x1bcce
leaq 0x4842c(%rip), %rax     # 0x640d4
cvtss2sd (%rax), %xmm0
leaq 0x47939(%rip), %rax     # 0x635ec
movl (%rax), %esi
movsd 0x20(%rbx), %xmm1
divsd 0x20(%r14), %xmm1
leaq 0x36139(%rip), %rdi     # 0x51e00
movb $0x2, %al
callq 0x20f0
movsd (%rsp), %xmm0
jmp 0x1bdd4
andq $0x0, 0x38(%rbx)
leaq 0x483f4(%rip), %rax     # 0x640d8
cvtss2sd (%rax), %xmm0
movsd %xmm0, 0x48(%rbx)
leaq 0x478f8(%rip), %rax     # 0x635ec
cmpl $0x0, (%rax)
mulsd 0x355c9(%rip), %xmm0    # 0x512c8
movsd %xmm0, (%rsp)
je 0x1bd52
movsd 0x20(%rbx), %xmm0
movsd 0x585cd(%rip), %xmm1    # 0x742e0
xorpd %xmm2, %xmm1
callq 0x20a0
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rbx)
leaq 0x48337(%rip), %rax     # 0x64068
cvtss2sd (%rax), %xmm0
leaq 0x48398(%rip), %rax     # 0x640d4
movss (%rax), %xmm1
xorps 0x34b19(%rip), %xmm1    # 0x50860
cvtss2sd %xmm1, %xmm1
callq 0x20a0
jmp 0x1bd9c
movsd 0x58586(%rip), %xmm0    # 0x742e0
xorpd %xmm2, %xmm0
mulsd 0x20(%rbx), %xmm0
callq 0x22a0
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rbx)
leaq 0x48357(%rip), %rax     # 0x640d4
movss (%rax), %xmm0
xorps 0x34ad8(%rip), %xmm0    # 0x50860
leaq 0x482d9(%rip), %rax     # 0x64068
mulss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x22a0
mulsd %xmm0, %xmm0
movsd 0x48(%rbx), %xmm1
xorpd 0x2f313(%rip), %xmm1    # 0x4b0c0
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd 0x40(%rbx), %xmm0
subsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rbx)
cvtsd2ss 0x20(%rbx), %xmm0
movq %rbx, %rdi
callq 0x1ac14
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xb8(%rbx)
cvtsd2ss 0x20(%rbx), %xmm0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1b776
cvtss2sd %xmm0, %xmm2
movsd 0x28(%rbx), %xmm0
divsd %xmm2, %xmm0
movsd 0x2f248(%rip), %xmm1    # 0x4b048
subsd %xmm0, %xmm1
andpd 0x35434(%rip), %xmm1    # 0x51240
ucomisd 0x354bc(%rip), %xmm1    # 0x512d0
ja 0x1bfd1
cmpl $0x2, 0x4805f(%rip)     # 0x63e80
jne 0x1be45
leaq 0x4823e(%rip), %rax     # 0x64068
movss (%rax), %xmm1
movss %xmm1, 0x10(%rbx)
movss %xmm1, 0x14(%rbx)
movapd 0x2f280(%rip), %xmm2    # 0x4b0c0
jmp 0x1bf09
movsd 0x20(%rbx), %xmm0
movsd 0x20(%r14), %xmm1
movapd %xmm0, %xmm2
minsd %xmm1, %xmm2
maxsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
cvtsd2ss %xmm2, %xmm0
leaq 0x481fd(%rip), %rax     # 0x64068
movss (%rax), %xmm2
movss %xmm2, (%rsp)
movaps %xmm1, %xmm2
subss %xmm0, %xmm2
divss 0x2f185(%rip), %xmm2    # 0x4b008
addss %xmm2, %xmm0
subss %xmm2, %xmm1
movq %rbx, %rdi
movq %r14, %rsi
movss %xmm0, 0x10(%rsp)
movss %xmm1, 0x14(%rsp)
callq 0x1c998
movss (%rsp), %xmm1
movsd 0x70(%rbx), %xmm0
ucomisd %xmm0, %xmm0
movapd 0x2f208(%rip), %xmm2    # 0x4b0c0
jp 0x1bf61
movsd 0x78(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1bf61
movsd 0x80(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1bf61
movsd 0x88(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1bf61
movsd 0x90(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1bf61
movsd 0x98(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1bf61
movss %xmm1, 0x18(%rbx)
addq $0xc8, %rbx
incl %ebp
jmp 0x1bc28
leaq 0x36003(%rip), %rdi     # 0x51f26
movl 0xc(%rsp), %esi
xorl %eax, %eax
callq 0x20f0
movl 0x8(%rsp), %esi
testl %esi, %esi
je 0x1bf52
leaq 0x36006(%rip), %rdi     # 0x51f43
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20f0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x47f19(%rip), %edx     # 0x63e80
leaq 0x35f54(%rip), %rdi     # 0x51ec2
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
cvtss2sd 0x10(%rsp), %xmm0
cvtss2sd 0x14(%rsp), %xmm1
leaq 0x35f59(%rip), %rdi     # 0x51ee3
movb $0x2, %al
callq 0x20f0
movsd 0x70(%rbx), %xmm0
movsd 0x78(%rbx), %xmm1
movsd 0x80(%rbx), %xmm2
movsd 0x88(%rbx), %xmm3
movsd 0x90(%rbx), %xmm4
movsd 0x98(%rbx), %xmm5
leaq 0x35f42(%rip), %rdi     # 0x51f04
movb $0x6, %al
callq 0x20f0
pushq $0x8
popq %rdi
callq 0x2270
movsd 0x20(%rbx), %xmm0
leaq 0x35e4b(%rip), %rdi     # 0x51e28
movl %ebp, %esi
movb $0x1, %al
movsd %xmm2, (%rsp)
callq 0x20f0
movsd 0x28(%rbx), %xmm0
movapd %xmm0, %xmm2
movsd (%rsp), %xmm1
divsd %xmm1, %xmm2
leaq 0x35e49(%rip), %rdi     # 0x51e4d
movb $0x3, %al
callq 0x20f0
cvtsd2ss 0x20(%rbx), %xmm0
movq %rbx, %rdi
callq 0x1ac14
cvtss2sd %xmm0, %xmm0
leaq 0x35e4d(%rip), %rdi     # 0x51e70
movb $0x1, %al
callq 0x20f0
testq %r14, %r14
je 0x1bfc9
movl 0x47e4b(%rip), %esi     # 0x63e80
movsd 0x20(%r14), %xmm0
cvtss2sd 0x10(%r14), %xmm1
cvtss2sd 0x14(%r14), %xmm2
cvtss2sd 0x18(%r14), %xmm3
leaq 0x35e26(%rip), %rdi     # 0x51e7a
movb $0x4, %al
callq 0x20f0
movsd 0xa0(%r14), %xmm0
movsd 0xa8(%r14), %xmm1
movsd 0xb0(%r14), %xmm2
leaq 0x3a787(%rip), %rdi     # 0x56804
movb $0x3, %al
callq 0x20f0
cvtsd2ss 0x20(%rbx), %xmm0
movq %r14, %rdi
callq 0x1ac14
cvtss2sd %xmm0, %xmm0
leaq 0x35e19(%rip), %rdi     # 0x51eb5
movb $0x1, %al
callq 0x20f0
jmp 0x1bfc9