// File: /workspace/github_c_makefile_star2_O1/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, %r14
movq %rdx, %r15
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x3a5ad(%rip), %rdi     # 0x5828f
callq 0x2090
movl 0x4b193(%rip), %esi     # 0x68e80
leaq 0x3a188(%rip), %rdi     # 0x57e7c
movl $0x0, 0x8(%rsp)
xorl %eax, %eax
callq 0x20f0
leaq 0x3a5ac(%rip), %rdi     # 0x582b6
callq 0x2090
leaq 0x3a194(%rip), %rdi     # 0x57eaa
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
leaq 0x3a5a6(%rip), %rdi     # 0x582cc
callq 0x2090
leaq 0x3a5d1(%rip), %rdi     # 0x58303
callq 0x2090
leaq 0x3a5f9(%rip), %rdi     # 0x58337
callq 0x2090
leaq 0x3a62c(%rip), %rdi     # 0x58376
callq 0x2090
movl %ebp, %r13d
testl %ebp, %ebp
jle 0x1e081
xorl %ebp, %ebp
movapd 0x3332c(%rip), %xmm2    # 0x51090
movl $0x0, 0x8(%rsp)
xorl %r12d, %r12d
movl $0x42c80000, 0x10(%rbx) # imm = 0x42C80000
testq %r15, %r15
je 0x1de23
testq %r14, %r14
je 0x1de23
cmpl $0x2, 0x4b0f1(%rip)     # 0x68e80
je 0x1de23
movslq 0x8(%rbx), %rax
movslq (%r14,%rax,4), %rax
imulq $0xc8, %rax, %r12
addq (%r15,%rax,8), %r12
xorps %xmm0, %xmm0
cvtsd2ss 0x20(%rbx), %xmm0
movq %r12, %rdi
callq 0x1ccef
cvtss2sd %xmm0, %xmm0
movsd 0x28(%rbx), %xmm1
movq %rbx, %rdi
movsd %xmm0, (%rsp)
movl %ebp, %esi
callq 0x1e752
cmpl $0x1, %eax
movl 0x8(%rsp), %eax
sbbl $-0x1, %eax
xorps %xmm0, %xmm0
ucomisd 0x48(%rbx), %xmm0
movl %eax, 0x8(%rsp)
jbe 0x1de19
leaq 0x4b2e5(%rip), %rax     # 0x690d4
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
leaq 0x4a7ef(%rip), %rax     # 0x685ec
movl (%rax), %esi
movsd 0x20(%rbx), %xmm1
divsd 0x20(%r12), %xmm1
leaq 0x3a0ce(%rip), %rdi     # 0x57ee0
movb $0x2, %al
callq 0x20f0
movsd (%rsp), %xmm0
jmp 0x1df2b
movq $0x0, 0x38(%rbx)
leaq 0x4b2a6(%rip), %rax     # 0x690d8
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
movsd %xmm0, 0x48(%rbx)
leaq 0x4a7a7(%rip), %rax     # 0x685ec
cmpl $0x0, (%rax)
mulsd 0x39548(%rip), %xmm0    # 0x57398
movsd %xmm0, (%rsp)
je 0x1dea6
movsd 0x20(%rbx), %xmm0
movsd 0x5b47c(%rip), %xmm1    # 0x792e0
xorpd %xmm2, %xmm1
callq 0x20a0
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rbx)
leaq 0x4b1e6(%rip), %rax     # 0x69068
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
leaq 0x4b244(%rip), %rax     # 0x690d4
movss (%rax), %xmm1
xorps 0x38a95(%rip), %xmm1    # 0x56930
cvtss2sd %xmm1, %xmm1
callq 0x20a0
jmp 0x1def0
movsd 0x5b432(%rip), %xmm0    # 0x792e0
xorpd %xmm2, %xmm0
mulsd 0x20(%rbx), %xmm0
callq 0x22a0
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rbx)
leaq 0x4b203(%rip), %rax     # 0x690d4
movss (%rax), %xmm0
xorps 0x38a54(%rip), %xmm0    # 0x56930
leaq 0x4b185(%rip), %rax     # 0x69068
mulss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x22a0
mulsd %xmm0, %xmm0
movsd 0x48(%rbx), %xmm1
xorpd 0x3318f(%rip), %xmm1    # 0x51090
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd 0x40(%rbx), %xmm0
subsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rbx)
xorps %xmm0, %xmm0
cvtsd2ss 0x20(%rbx), %xmm0
movq %rbx, %rdi
callq 0x1ccef
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xb8(%rbx)
xorps %xmm0, %xmm0
cvtsd2ss 0x20(%rbx), %xmm0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1d8bb
xorps %xmm2, %xmm2
cvtss2sd %xmm0, %xmm2
movsd 0x28(%rbx), %xmm0
divsd %xmm2, %xmm0
movsd 0x330e3(%rip), %xmm1    # 0x51040
subsd %xmm0, %xmm1
andpd 0x393c7(%rip), %xmm1    # 0x57330
ucomisd 0x3942f(%rip), %xmm1    # 0x573a0
ja 0x1e13d
cmpl $0x2, 0x4af02(%rip)     # 0x68e80
jne 0x1dfa2
leaq 0x4b0e1(%rip), %rax     # 0x69068
movss (%rax), %xmm1
movss %xmm1, 0x10(%rbx)
movss %xmm1, 0x14(%rbx)
movapd 0x330f3(%rip), %xmm2    # 0x51090
jmp 0x1e06a
movsd 0x20(%rbx), %xmm0
movsd 0x20(%r12), %xmm1
movapd %xmm0, %xmm2
minsd %xmm1, %xmm2
maxsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm2, %xmm0
leaq 0x4b09c(%rip), %rax     # 0x69068
movss (%rax), %xmm2
movss %xmm2, (%rsp)
movaps %xmm1, %xmm2
subss %xmm0, %xmm2
divss 0x33024(%rip), %xmm2    # 0x51008
addss %xmm2, %xmm0
subss %xmm2, %xmm1
movq %rbx, %rdi
movq %r12, %rsi
movss %xmm0, 0x10(%rsp)
movss %xmm1, 0x14(%rsp)
callq 0x1eb51
movss (%rsp), %xmm1
movsd 0x70(%rbx), %xmm0
ucomisd %xmm0, %xmm0
movapd 0x33077(%rip), %xmm2    # 0x51090
jp 0x1e0c5
movsd 0x78(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1e0c5
movsd 0x80(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1e0c5
movsd 0x88(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1e0c5
movsd 0x90(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1e0c5
movsd 0x98(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1e0c5
movss %xmm1, 0x18(%rbx)
addq $0xc8, %rbx
incl %ebp
cmpl %ebp, %r13d
jne 0x1dd6f
leaq 0x39f7e(%rip), %rdi     # 0x58006
movl %r13d, %esi
xorl %eax, %eax
callq 0x20f0
movl 0x8(%rsp), %esi
testl %esi, %esi
je 0x1e0b6
leaq 0x39f82(%rip), %rdi     # 0x58023
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 0x4adb5(%rip), %edx     # 0x68e80
leaq 0x39ed0(%rip), %rdi     # 0x57fa2
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x14(%rsp), %xmm1
leaq 0x39ecf(%rip), %rdi     # 0x57fc3
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 0x39eb8(%rip), %rdi     # 0x57fe4
movb $0x6, %al
callq 0x20f0
movl $0x8, %edi
callq 0x2270
movsd 0x20(%rbx), %xmm0
leaq 0x39dbf(%rip), %rdi     # 0x57f08
movl %ebp, %esi
movb $0x1, %al
movsd %xmm2, 0x8(%rsp)
callq 0x20f0
movsd 0x28(%rbx), %xmm0
movapd %xmm0, %xmm2
movsd 0x8(%rsp), %xmm1
divsd %xmm1, %xmm2
leaq 0x39dbb(%rip), %rdi     # 0x57f2d
movb $0x3, %al
callq 0x20f0
xorps %xmm0, %xmm0
cvtsd2ss 0x20(%rbx), %xmm0
movq %rbx, %rdi
callq 0x1ccef
cvtss2sd %xmm0, %xmm0
leaq 0x39dbc(%rip), %rdi     # 0x57f50
movb $0x1, %al
callq 0x20f0
testq %r12, %r12
je 0x1e133
movl 0x4acda(%rip), %esi     # 0x68e80
movsd 0x20(%r12), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x10(%r12), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x14(%r12), %xmm2
cvtss2sd 0x18(%r12), %xmm3
leaq 0x39d8b(%rip), %rdi     # 0x57f5a
movb $0x4, %al
callq 0x20f0
movsd 0xa0(%r12), %xmm0
movsd 0xa8(%r12), %xmm1
movsd 0xb0(%r12), %xmm2
leaq 0x3e6e1(%rip), %rdi     # 0x5c8dc
movb $0x3, %al
callq 0x20f0
xorps %xmm0, %xmm0
cvtsd2ss 0x20(%rbx), %xmm0
movq %r12, %rdi
callq 0x1ccef
cvtss2sd %xmm0, %xmm0
leaq 0x39d78(%rip), %rdi     # 0x57f95
movb $0x1, %al
callq 0x20f0
jmp 0x1e133