// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/anharmonic_tnm.c
// Function: Energy_anharmonic_new
float Energy_anharmonic_new(struct inter_new *New_inter,
			    int *N_int_new, int N_int_max,
			    int *nc_nat, int *nc_nonat,
			    float *Ene_nat_attr, float *Ene_nat_rep,
			    float *Ene_nonat,
			    float *coord, atom *atoms, int natoms,
			    struct residue *seq, int nres,
			    struct interaction *Int_list, int N_int,
			    struct interaction **Int_KB, int NA,
			    struct axe *axe, int naxes,
			    double *delta_phi, int K_tors, int sec_der)
{

  if(INI_ENERGY==0){
    Initialize_energy(Int_list, N_int, coord, atoms, natoms, nres);
    INI_ENERGY=1;
  }

  *N_int_new=0; *nc_nat=0; *nc_nonat=0;
  *Ene_nat_attr=0; *Ene_nat_rep=0; *Ene_nonat=0;
  for(int i=0; i<N_int_all; i++){
    int i1=Int_list_all[i].i1, i2=Int_list_all[i].i2;
    float d=Min_res_dist(coord, atoms, &i1, &i2, -1, natoms);
    if(d>=C_THR)continue;
    int n=nat_res1_res2[atoms[i1].res][atoms[i2].res];
    if(d<=0){
      printf("ERROR, d(%d,%d)= %.2g n=%d\n", i1, i2, d, n);
      exit(8);
    }

    struct interaction *Int_k;
    float f0;
    if(n>=0){
      Int_k=Int_list+n;
      f0=Compute_V(Int_k, d);
      if(f0>0){(*Ene_nat_rep)+=f0;}
      else{(*Ene_nat_attr)+=f0;}
      (*nc_nat)++;
      if(f0< (Int_k->E_min-0.00001) || n>=N_int){
	printf("ERROR in Energy_anharmonic_new, inter. %d (nat=%d/%d) "
	       "res1=%d res2=%d d=%.4g d0= %.4g V(d)=%.4g < V(d0)=%.4g\n",
	       i,n,N_int,atoms[i1].res,atoms[i2].res,d,Int_k->r0,
	       f0,Int_k->E_min);
	exit(8);
      }

    }else{
      int a=seq[atoms[i1].res].i_aa, b=seq[atoms[i2].res].i_aa;
      Int_k=Int_KB[a]+b;
      f0=Compute_V(Int_k, d);
      (*Ene_nonat)+=f0; //-Int_KB[a][b].E_min;
      (*nc_nonat)++;
    }
    if(New_inter){
      struct inter_new *Int=New_inter+(*N_int_new);
      Int->i1=i1; Int->i2=i2;
      Int->nat=n;
      Int->f0=f0;
      Int->r=d;
      Int->f1=Compute_V1(Int_k, d);
      float *r1=coord+3*i1, *r2=coord+3*i2;
      for(int j=0; j<3; j++){
	Int->rj[j]=r2[j];
	Int->rji[j]=r2[j]-r1[j];
      }
      Vector_product(Int->crij, r1, r2);
      if(sec_der){
	Int->f2=Compute_V2(Int_k, d, 1); // do not assume it is minimum!
      }
    }
    (*N_int_new)++;
    if(*N_int_new > N_int_max){
      printf("ERROR, > %d interactions in Energy_anharmonic_new "
	     "Increase N_int_max\n", *N_int_new); exit(8);
    }
  }
  if(K_tors==0){return(*Ene_nat_attr+*Ene_nat_rep+*Ene_nonat);}

  // Compute_torsional energy
  double E_phi=0, E_psi=0, E_omg=0, E_chi=0, E_BA=0, E_BL=0, E_BT=0;
  struct axe *ax=axe;
  for(int i=0; i<naxes; i++){
    float d2=delta_phi[i]*delta_phi[i];
    if(ax->type=='f'){E_phi+=d2;}
    else if(ax->type=='p'){E_psi+=d2;}
    else if(ax->type=='l'){E_BL+=d2;}
    else if(ax->type=='a'){E_BA+=d2;}
    else if(ax->type=='t'){E_BT+=d2;}
    else if(ax->type=='o'){E_omg+=d2;}
    else if(ax->type=='s'){E_chi+=d2;}
    ax++;
  }
  double E_tors=K_PHI*E_phi;
  if(E_psi)E_tors+=K_PSI*E_psi;
  if(E_omg)E_tors+=K_OMEGA*E_omg;
  if(E_chi)E_tors+=K_CHI*E_chi;
  if(E_BL)E_tors+=K_BL*E_BL;
  if(E_BA)E_tors+=K_BA*E_BA;
  if(E_BT)E_tors+=K_PHI*E_BT;
  return(*Ene_nat_attr+*Ene_nat_rep+*Ene_nonat+0.5*E_tors);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %rax
movq %r8, %r10
movq %rcx, %r11
movl %edx, 0x24(%rsp)
movq %rsi, %rbp
movq %rdi, 0x30(%rsp)
movl 0xb0(%rsp), %r15d
movq 0xa8(%rsp), %r13
movq 0x98(%rsp), %r12
movq 0x90(%rsp), %r14
cmpl $0x0, 0x5938e(%rip)     # 0x7428c
jne 0x1af56
movl 0xc0(%rsp), %r9d
movq 0xc8(%rsp), %rdi
movl 0xd0(%rsp), %esi
movq 0xa0(%rsp), %rdx
movq %r13, %rcx
movl %r15d, %r8d
movq %r10, %r15
movq %r11, %r13
movq %rax, %rbx
callq 0x1aaea
movq %rbx, %rax
movq %r13, %r11
movq 0xa8(%rsp), %r13
movq %r15, %r10
movl 0xb0(%rsp), %r15d
movl $0x1, 0x59336(%rip)     # 0x7428c
andl $0x0, (%rbp)
movq %r11, 0x50(%rsp)
andl $0x0, (%r11)
movq %r10, 0x48(%rsp)
andl $0x0, (%r10)
movq %rax, 0x18(%rsp)
andl $0x0, (%rax)
andl $0x0, (%r14)
andl $0x0, (%r12)
xorl %ecx, %ecx
movq %rbp, 0x38(%rsp)
movslq 0x59331(%rip), %rax     # 0x742bc
cmpq %rax, %rcx
jge 0x1b217
movq 0x59325(%rip), %rax     # 0x742c0
movq %rcx, 0x40(%rsp)
imulq $0xc8, %rcx, %rcx
movl (%rax,%rcx), %edx
movl %edx, 0xc(%rsp)
movl 0x4(%rax,%rcx), %eax
movl %eax, 0x8(%rsp)
movq 0xa0(%rsp), %rdi
movq %r13, %rsi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
pushq $-0x1
popq %r8
movl %r15d, %r9d
callq 0x1a8f8
leaq 0x4908a(%rip), %rax     # 0x64068
ucomiss (%rax), %xmm0
jae 0x1b20a
movaps %xmm0, %xmm1
movq %r12, %rbx
movq 0x592dc(%rip), %rcx     # 0x742d0
movslq 0xc(%rsp), %r15
imulq $0x70, %r15, %rbp
movslq 0x64(%r13,%rbp), %rax
movq (%rcx,%rax,8), %rdx
movslq 0x8(%rsp), %rcx
movq %rcx, 0x28(%rsp)
imulq $0x70, %rcx, %r12
movslq 0x64(%r13,%r12), %rcx
movl (%rdx,%rcx,4), %r13d
xorps %xmm0, %xmm0
ucomiss %xmm1, %xmm0
jae 0x1b427
testl %r13d, %r13d
movss %xmm1, 0x4(%rsp)
js 0x1b0bf
imulq $0xc8, %r13, %r14
movq 0xc8(%rsp), %rax
leaq (%rax,%r14), %rdi
movq %rdi, 0x10(%rsp)
movaps %xmm1, %xmm0
callq 0x1ac14
ucomiss 0x2ffad(%rip), %xmm0    # 0x4b00c
movq 0x18(%rsp), %rax
cmovaq 0x90(%rsp), %rax
movss (%rax), %xmm1
addss %xmm0, %xmm1
movss %xmm1, (%rax)
movq 0x50(%rsp), %rax
incl (%rax)
cvtss2sd %xmm0, %xmm2
movq 0xc8(%rsp), %rax
movsd 0xb8(%rax,%r14), %xmm3
movapd %xmm3, %xmm1
addsd 0x361f6(%rip), %xmm1    # 0x51298
ucomisd %xmm2, %xmm1
ja 0x1b456
cmpl 0xd0(%rsp), %r13d
jge 0x1b456
movq %rbx, %r12
jmp 0x1b110
shlq $0x5, %rax
movq 0xb8(%rsp), %rdx
movswq 0x14(%rdx,%rax), %rax
shlq $0x5, %rcx
movswq 0x14(%rdx,%rcx), %rcx
imulq $0xc8, %rcx, %rdi
movq 0xd8(%rsp), %rcx
addq (%rcx,%rax,8), %rdi
movq %rdi, 0x10(%rsp)
movaps %xmm1, %xmm0
callq 0x1ac14
movaps %xmm0, %xmm1
movq %rbx, %r12
addss (%rbx), %xmm1
movss %xmm1, (%rbx)
movq 0x48(%rsp), %rax
incl (%rax)
cmpq $0x0, 0x30(%rsp)
je 0x1b1da
movq 0x38(%rsp), %rax
movslq (%rax), %rax
imulq $0x44, %rax, %r14
movq 0x30(%rsp), %r12
leaq (%r12,%r14), %rbp
movl %r15d, (%r12,%r14)
movq 0x28(%rsp), %rbx
movl %ebx, 0x4(%r12,%r14)
movl %r13d, 0x40(%r12,%r14)
movss %xmm0, 0x8(%r12,%r14)
movss 0x4(%rsp), %xmm0
movss %xmm0, 0x18(%r12,%r14)
movq 0x10(%rsp), %rdi
callq 0x1b4a6
movss %xmm0, 0xc(%r12,%r14)
leal (%r15,%r15,2), %eax
cltq
movq 0xa0(%rsp), %rcx
leaq (%rcx,%rax,4), %rsi
leal (%rbx,%rbx,2), %eax
cltq
leaq (%rcx,%rax,4), %rdx
xorl %eax, %eax
cmpq $0x3, %rax
je 0x1b1a7
movss (%rdx,%rax,4), %xmm0
movss %xmm0, 0x1c(%rbp,%rax,4)
subss (%rsi,%rax,4), %xmm0
movss %xmm0, 0x28(%rbp,%rax,4)
incq %rax
jmp 0x1b186
leaq 0x34(%rbp), %rdi
callq 0xf8a4
cmpl $0x0, 0x108(%rsp)
movq 0x98(%rsp), %r12
je 0x1b1da
movq 0x10(%rsp), %rdi
movss 0x4(%rsp), %xmm0
pushq $0x1
popq %rsi
callq 0x1b776
movss %xmm0, 0x10(%rbp)
movq 0x38(%rsp), %rbp
movl (%rbp), %eax
leal 0x1(%rax), %esi
movl %esi, (%rbp)
cmpl 0x24(%rsp), %eax
movq 0x90(%rsp), %r14
movq 0xa8(%rsp), %r13
movl 0xb0(%rsp), %r15d
jge 0x1b446
movq 0x40(%rsp), %rcx
incq %rcx
jmp 0x1af84
cmpl $0x0, 0x100(%rsp)
je 0x1b404
xorl %eax, %eax
movl 0xf0(%rsp), %ecx
movq %rcx, %rdi
testl %edi, %edi
cmovlel %eax, %edi
xorpd %xmm6, %xmm6
leaq 0x360fb(%rip), %rcx     # 0x5133c
xorpd %xmm5, %xmm5
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
xorpd %xmm0, %xmm0
movq 0xe8(%rsp), %r8
cmpq %rax, %rdi
je 0x1b2d1
movq 0xf8(%rsp), %rdx
movsd (%rdx,%rax,8), %xmm7
mulsd %xmm7, %xmm7
cvtsd2ss %xmm7, %xmm7
movzbl (%r8), %edx
leal -0x66(%rdx), %esi
cmpl $0xe, %esi
ja 0x1b29a
movslq (%rcx,%rsi,4), %rdx
addq %rcx, %rdx
cvtss2sd %xmm7, %xmm7
jmpq *%rdx
addsd %xmm7, %xmm6
jmp 0x1b2c5
cmpl $0x61, %edx
jne 0x1b2c5
cvtss2sd %xmm7, %xmm7
addsd %xmm7, %xmm1
jmp 0x1b2c5
addsd %xmm7, %xmm4
jmp 0x1b2c5
addsd %xmm7, %xmm0
jmp 0x1b2c5
addsd %xmm7, %xmm5
jmp 0x1b2c5
addsd %xmm7, %xmm2
jmp 0x1b2c5
addsd %xmm7, %xmm3
addq $0x158, %r8             # imm = 0x158
incq %rax
jmp 0x1b261
leaq 0x48dc8(%rip), %rax     # 0x640a0
movsd (%rax), %xmm7
mulsd %xmm7, %xmm6
leaq 0x48db1(%rip), %rax     # 0x64098
movsd (%rax), %xmm9
mulsd %xmm5, %xmm9
addsd %xmm6, %xmm9
xorpd %xmm8, %xmm8
cmpneqsd %xmm8, %xmm5
movapd %xmm5, %xmm10
andnpd %xmm6, %xmm10
andpd %xmm9, %xmm5
orpd %xmm10, %xmm5
leaq 0x48d74(%rip), %rax     # 0x64090
movsd (%rax), %xmm6
mulsd %xmm4, %xmm6
addsd %xmm5, %xmm6
cmpneqsd %xmm8, %xmm4
movapd %xmm4, %xmm9
andnpd %xmm5, %xmm9
andpd %xmm6, %xmm4
orpd %xmm9, %xmm4
leaq 0x48d60(%rip), %rax     # 0x640a8
movsd (%rax), %xmm5
mulsd %xmm3, %xmm5
addsd %xmm4, %xmm5
cmpneqsd %xmm8, %xmm3
movapd %xmm3, %xmm6
andnpd %xmm4, %xmm6
andpd %xmm5, %xmm3
orpd %xmm6, %xmm3
leaq 0x48d47(%rip), %rax     # 0x640b8
movsd (%rax), %xmm4
mulsd %xmm2, %xmm4
addsd %xmm3, %xmm4
cmpneqsd %xmm8, %xmm2
movapd %xmm2, %xmm5
andnpd %xmm3, %xmm5
andpd %xmm4, %xmm2
orpd %xmm5, %xmm2
leaq 0x48d16(%rip), %rax     # 0x640b0
movsd (%rax), %xmm3
mulsd %xmm1, %xmm3
addsd %xmm2, %xmm3
cmpneqsd %xmm8, %xmm1
movapd %xmm1, %xmm4
andnpd %xmm2, %xmm4
andpd %xmm3, %xmm1
orpd %xmm4, %xmm1
mulsd %xmm0, %xmm7
addsd %xmm1, %xmm7
cmpneqsd %xmm8, %xmm0
movapd %xmm0, %xmm2
andnpd %xmm1, %xmm2
andpd %xmm7, %xmm0
orpd %xmm2, %xmm0
movq 0x18(%rsp), %rax
movss (%rax), %xmm1
addss (%r14), %xmm1
addss (%r12), %xmm1
mulsd 0x2fc7a(%rip), %xmm0    # 0x4b070
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
jmp 0x1b418
movq 0x18(%rsp), %rax
movss (%rax), %xmm0
addss (%r14), %xmm0
addss (%r12), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cvtss2sd %xmm1, %xmm0
leaq 0x3643d(%rip), %rdi     # 0x5186f
movl %r15d, %esi
movq 0x28(%rsp), %rdx
movl %r13d, %ecx
movb $0x1, %al
callq 0x20f0
jmp 0x1b49e
leaq 0x364ac(%rip), %rdi     # 0x518f9
xorl %eax, %eax
callq 0x20f0
jmp 0x1b49e
movq 0xa8(%rsp), %rcx
leaq (%rcx,%rbp), %rax
addq $0x64, %rax
leaq 0x64(%rcx,%r12), %rcx
movl (%rax), %r8d
movl (%rcx), %r9d
cvtss2sd 0x4(%rsp), %xmm0
movq 0x10(%rsp), %rax
movsd 0x20(%rax), %xmm1
leaq 0x36403(%rip), %rdi     # 0x5188b
movq 0x40(%rsp), %rsi
movl %r13d, %edx
movl 0xd0(%rsp), %ecx
movb $0x4, %al
callq 0x20f0
pushq $0x8
popq %rdi
callq 0x2270