// File: /workspace/github_c_makefile_star2_O3/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 0x3acae(%rip), %rdi     # 0x5824f
callq 0x2090
movl 0x4b8d4(%rip), %esi     # 0x68e80
leaq 0x3a889(%rip), %rdi     # 0x57e3c
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x20f0
leaq 0x3acb2(%rip), %rdi     # 0x58276
callq 0x2090
leaq 0x3a89a(%rip), %rdi     # 0x57e6a
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
leaq 0x3acac(%rip), %rdi     # 0x5828c
callq 0x2090
leaq 0x3acd7(%rip), %rdi     # 0x582c3
callq 0x2090
leaq 0x3acff(%rip), %rdi     # 0x582f7
callq 0x2090
leaq 0x3ad32(%rip), %rdi     # 0x58336
callq 0x2090
movl %ebp, %esi
testl %ebp, %ebp
jle 0x1d964
movl 0x4b867(%rip), %edx     # 0x68e80
movapd 0x33a6f(%rip), %xmm2    # 0x51090
xorl %ebp, %ebp
xorl %r13d, %r13d
movl %esi, 0x8(%rsp)
movl $0x42c80000, 0x10(%rbx) # imm = 0x42C80000
testq %r12, %r12
je 0x1d6d2
testq %r15, %r15
je 0x1d6d2
cmpl $0x2, %edx
je 0x1d6d2
movslq 0x8(%rbx), %rax
movslq (%r15,%rax,4), %rax
imulq $0xc8, %rax, %r13
addq (%r12,%rax,8), %r13
xorps %xmm0, %xmm0
cvtsd2ss 0x20(%rbx), %xmm0
movq %r13, %rdi
callq 0x1c59a
cvtss2sd %xmm0, %xmm0
movsd 0x28(%rbx), %xmm1
movq %rbx, %rdi
movsd %xmm0, (%rsp)
movl %r14d, %esi
callq 0x1e00c
cmpl $0x1, %eax
sbbl $-0x1, %ebp
xorps %xmm0, %xmm0
ucomisd 0x48(%rbx), %xmm0
jbe 0x1d6c8
leaq 0x4ba35(%rip), %rax     # 0x690d4
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
leaq 0x4af3f(%rip), %rax     # 0x685ec
movl (%rax), %esi
movsd 0x20(%rbx), %xmm1
divsd 0x20(%r13), %xmm1
leaq 0x3a7df(%rip), %rdi     # 0x57ea0
movb $0x2, %al
callq 0x20f0
movsd (%rsp), %xmm0
jmp 0x1d7da
movq $0x0, 0x38(%rbx)
leaq 0x4b9f7(%rip), %rax     # 0x690d8
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
movsd %xmm0, 0x48(%rbx)
leaq 0x4aef8(%rip), %rax     # 0x685ec
cmpl $0x0, (%rax)
mulsd 0x39c69(%rip), %xmm0    # 0x57368
movsd %xmm0, (%rsp)
je 0x1d755
movsd 0x20(%rbx), %xmm0
movsd 0x5bbcd(%rip), %xmm1    # 0x792e0
xorpd %xmm2, %xmm1
callq 0x20a0
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rbx)
leaq 0x4b937(%rip), %rax     # 0x69068
xorps %xmm0, %xmm0
cvtss2sd (%rax), %xmm0
leaq 0x4b995(%rip), %rax     # 0x690d4
movss (%rax), %xmm1
xorps 0x338e6(%rip), %xmm1    # 0x51030
cvtss2sd %xmm1, %xmm1
callq 0x20a0
jmp 0x1d79f
movsd 0x5bb83(%rip), %xmm0    # 0x792e0
xorpd %xmm2, %xmm0
mulsd 0x20(%rbx), %xmm0
callq 0x22b0
movsd (%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x40(%rbx)
leaq 0x4b954(%rip), %rax     # 0x690d4
movss (%rax), %xmm0
xorps 0x338a5(%rip), %xmm0    # 0x51030
leaq 0x4b8d6(%rip), %rax     # 0x69068
mulss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x22b0
mulsd %xmm0, %xmm0
movsd 0x48(%rbx), %xmm1
xorpd 0x338e0(%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 0x1c59a
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0xb8(%rbx)
xorps %xmm0, %xmm0
cvtsd2ss 0x20(%rbx), %xmm0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1d161
xorps %xmm2, %xmm2
cvtss2sd %xmm0, %xmm2
movsd 0x28(%rbx), %xmm0
divsd %xmm2, %xmm0
movsd 0x3393c(%rip), %xmm1    # 0x51148
subsd %xmm0, %xmm1
andpd 0x39ad8(%rip), %xmm1    # 0x572f0
ucomisd 0x39b50(%rip), %xmm1    # 0x57370
ja 0x1da02
cmpl $0x2, 0x4b653(%rip)     # 0x68e80
jne 0x1d85a
leaq 0x4b832(%rip), %rax     # 0x69068
movss (%rax), %xmm0
movss %xmm0, 0x10(%rbx)
movss %xmm0, 0x14(%rbx)
movl $0x2, %edx
movl 0x8(%rsp), %esi
movapd 0x3383b(%rip), %xmm2    # 0x51090
jmp 0x1d92f
movsd 0x20(%rbx), %xmm0
movsd 0x20(%r13), %xmm1
movapd %xmm0, %xmm2
minsd %xmm1, %xmm2
maxsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
cvtsd2ss %xmm2, %xmm2
leaq 0x4b7e8(%rip), %rax     # 0x69068
movss (%rax), %xmm0
movss %xmm0, (%rsp)
movaps %xmm1, %xmm0
subss %xmm2, %xmm0
divss 0x3387c(%rip), %xmm0    # 0x51114
addss %xmm0, %xmm2
subss %xmm0, %xmm1
movq %rbx, %rdi
movq %r13, %rsi
movss %xmm2, 0x10(%rsp)
movaps %xmm2, %xmm0
movss %xmm1, 0x14(%rsp)
callq 0x1e421
movsd 0x70(%rbx), %xmm0
ucomisd %xmm0, %xmm0
movl 0x4b5b7(%rip), %edx     # 0x68e80
jp 0x1d98f
movsd 0x78(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1d98f
movsd 0x80(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1d98f
movsd 0x88(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1d98f
movsd 0x90(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1d98f
movsd 0x98(%rbx), %xmm0
ucomisd %xmm0, %xmm0
jp 0x1d98f
movl 0x8(%rsp), %esi
movapd 0x33766(%rip), %xmm2    # 0x51090
movss (%rsp), %xmm0
movss %xmm0, 0x18(%rbx)
addq $0xc8, %rbx
incl %r14d
cmpl %r14d, %esi
jne 0x1d62a
leaq 0x3a678(%rip), %rdi     # 0x57fc6
xorl %eax, %eax
callq 0x20f0
testl %ebp, %ebp
je 0x1d980
leaq 0x3a683(%rip), %rdi     # 0x57fe3
movl %ebp, %esi
jmp 0x1d96b
leaq 0x3a65b(%rip), %rdi     # 0x57fc6
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
leaq 0x3a5cc(%rip), %rdi     # 0x57f62
movl %r14d, %esi
xorl %eax, %eax
callq 0x20f0
xorps %xmm0, %xmm0
cvtss2sd 0x10(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x14(%rsp), %xmm1
leaq 0x3a5ca(%rip), %rdi     # 0x57f83
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 0x3a5b3(%rip), %rdi     # 0x57fa4
movb $0x6, %al
callq 0x20f0
movl $0x8, %edi
callq 0x2280
movsd 0x20(%rbx), %xmm0
leaq 0x3a4ba(%rip), %rdi     # 0x57ec8
movl %r14d, %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 0x3a4b5(%rip), %rdi     # 0x57eed
movb $0x3, %al
callq 0x20f0
xorps %xmm0, %xmm0
cvtsd2ss 0x20(%rbx), %xmm0
movq %rbx, %rdi
callq 0x1c59a
cvtss2sd %xmm0, %xmm0
leaq 0x3a4b6(%rip), %rdi     # 0x57f10
movb $0x1, %al
callq 0x20f0
testq %r13, %r13
je 0x1d9f8
movl 0x4b414(%rip), %esi     # 0x68e80
movsd 0x20(%r13), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x10(%r13), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x14(%r13), %xmm2
cvtss2sd 0x18(%r13), %xmm3
leaq 0x3a489(%rip), %rdi     # 0x57f1a
movb $0x4, %al
callq 0x20f0
movsd 0xa0(%r13), %xmm0
movsd 0xa8(%r13), %xmm1
movsd 0xb0(%r13), %xmm2
leaq 0x3ede2(%rip), %rdi     # 0x5c89c
movb $0x3, %al
callq 0x20f0
xorps %xmm0, %xmm0
cvtsd2ss 0x20(%rbx), %xmm0
movq %r13, %rdi
callq 0x1c59a
cvtss2sd %xmm0, %xmm0
leaq 0x3a479(%rip), %rdi     # 0x57f55
movb $0x1, %al
callq 0x20f0
jmp 0x1d9f8