// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/anharmonic_tnm.c
// Function: Energy_covalent
double Energy_covalent(float *r, atom *atoms, int natoms,
		       struct bond *bonds)
{
  int i, j;
  if(INI_COV==0){
    INI_COV=1;
    int nmax=2*natoms;
    Int_list_cov=malloc(nmax*sizeof(struct interaction));
    struct interaction *Int_cov=Int_list_cov; int n=0;
    struct bond *bond=bonds;
    for(i=0; i<natoms; i++){
      if(bond->previous && bond->len<0){
	Int_cov->i1=bond->i_atom; Int_cov->i2=bond->previous->i_atom;
	if(Dist_thr(Int_cov, atoms)){
	  Int_cov->sec_der=E_COV_FACT; Int_cov++; n++;
	}
	if(bond->previous->previous && bond->angle<0){
	  Int_cov->i1=bond->i_atom;
	  Int_cov->i2=bond->previous->previous->i_atom;
	  if(Dist_thr(Int_cov, atoms)){
	    Int_cov->sec_der=E_COV_FACT; Int_cov++; n++;
	  }
	}
	if(n>nmax){printf("ERROR too many covalent bonds\n"); exit(8);}
      }
      bond++;
    }
    printf("%d covalent bonds stored for %d atoms\n", n, natoms);
    N_cov=n;
    Compute_sec_der(Int_list_cov, N_cov, NULL);
    double E_rep_tmp=E_repulsion; E_repulsion*=E_COV_FACT;
    Assign_interactions_SB(Int_list_cov, N_cov, NULL, NULL);
    E_repulsion=E_rep_tmp;

    Ene_cov_ini=0; Int_cov=Int_list_cov;
    for(i=0; i<N_cov; i++){
      Ene_cov_ini+=Compute_V(Int_cov, Int_cov->r0);
      Int_cov++;
    }
  } // End initialization of the energy

  double Ene_cov=0; struct interaction *Int_cov=Int_list_cov;
  for(i=0; i<N_cov; i++){
    double d2=0, d;
    float *r1=r+3*Int_cov->i1, *r2=r+3*Int_cov->i2;
    for(j=0; j<3; j++){d=r1[j]-r2[j]; d*=d; d2+=d;}
    Ene_cov+=Compute_V(Int_cov, sqrt(d2));
    Int_cov++;
  }
  return(Ene_cov-Ene_cov_ini);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
cmpl $0x0, 0x59f05(%rip)     # 0x74290
je 0x1a448
xorl %r15d, %r15d
movq 0x59f55(%rip), %r14     # 0x742f0
xorpd %xmm0, %xmm0
cmpl 0x59f52(%rip), %r15d    # 0x742f8
jge 0x1a431
movsd %xmm0, 0x8(%rsp)
imull $0x3, (%r14), %eax
cltq
leaq (%rbx,%rax,4), %rax
imull $0x3, 0x4(%r14), %ecx
movslq %ecx, %rcx
leaq (%rbx,%rcx,4), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x1a3ef
movss (%rax,%rdx,4), %xmm1
subss (%rcx,%rdx,4), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rdx
jmp 0x1a3ce
ucomisd 0x30c59(%rip), %xmm0    # 0x4b050
jb 0x1a3ff
sqrtsd %xmm0, %xmm0
jmp 0x1a404
callq 0x2290
cvtsd2ss %xmm0, %xmm0
movq %r14, %rdi
callq 0x1ac14
cvtss2sd %xmm0, %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
addq $0xc8, %r14
incl %r15d
jmp 0x1a39f
subsd 0x59ec7(%rip), %xmm0    # 0x74300
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rcx, %r15
movl %edx, %r14d
movq %rsi, %r12
movl $0x1, 0x59e35(%rip)     # 0x74290
leal (%r14,%r14), %eax
movl %eax, 0x8(%rsp)
cltq
imulq $0xc8, %rax, %rdi
callq 0x21d0
movq %rax, %r13
movq %rax, 0x59e75(%rip)     # 0x742f0
xorl %ebp, %ebp
testl %r14d, %r14d
movq %r14, %rax
movl $0x0, %r14d
movq %rax, 0x10(%rsp)
cmovgl %eax, %r14d
addq $0x20, %r15
subl $0x1, %r14d
jb 0x1a54a
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x1a53e
cmpl $0x0, 0x14(%r15)
jns 0x1a53e
movl (%r15), %ecx
movl %ecx, (%r13)
movl 0x20(%rax), %eax
movl %eax, 0x4(%r13)
movq %r13, %rdi
movq %r12, %rsi
callq 0x1bb08
testl %eax, %eax
je 0x1a4ec
movsd 0x499ab(%rip), %xmm0    # 0x63e88
movsd %xmm0, 0x28(%r13)
addq $0xc8, %r13
incl %ebp
movq 0x8(%r15), %rax
movq 0x28(%rax), %rax
testq %rax, %rax
je 0x1a534
cmpl $0x0, 0x18(%r15)
jns 0x1a534
movl (%r15), %ecx
movl %ecx, (%r13)
movl 0x20(%rax), %eax
movl %eax, 0x4(%r13)
movq %r13, %rdi
movq %r12, %rsi
callq 0x1bb08
testl %eax, %eax
je 0x1a534
movsd 0x49963(%rip), %xmm0    # 0x63e88
movsd %xmm0, 0x28(%r13)
addq $0xc8, %r13
incl %ebp
cmpl 0x8(%rsp), %ebp
jg 0x1a60a
addq $0xc0, %r15
jmp 0x1a496
leaq 0x375cf(%rip), %rdi     # 0x51b20
xorl %r15d, %r15d
movl %ebp, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x20f0
movl %ebp, 0x59d90(%rip)     # 0x742f8
movq 0x59d81(%rip), %rdi     # 0x742f0
movl %ebp, %esi
xorl %edx, %edx
callq 0x16de2
leaq 0x49b59(%rip), %r14     # 0x640d8
movss (%r14), %xmm0
movss %xmm0, 0x8(%rsp)
cvtss2sd %xmm0, %xmm0
mulsd 0x498f2(%rip), %xmm0    # 0x63e88
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14)
movq 0x59d4a(%rip), %rdi     # 0x742f0
movl 0x59d4c(%rip), %esi     # 0x742f8
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1bb7e
movss 0x8(%rsp), %xmm0
movss %xmm0, (%r14)
andq $0x0, 0x59d38(%rip)     # 0x74300
movq 0x59d21(%rip), %r14     # 0x742f0
cmpl 0x59d22(%rip), %r15d    # 0x742f8
jge 0x1a391
cvtsd2ss 0x20(%r14), %xmm0
movq %r14, %rdi
callq 0x1ac14
cvtss2sd %xmm0, %xmm0
addsd 0x59d0a(%rip), %xmm0    # 0x74300
movsd %xmm0, 0x59d02(%rip)    # 0x74300
addq $0xc8, %r14
incl %r15d
jmp 0x1a5cf
leaq 0x37b80(%rip), %rdi     # 0x52191
callq 0x2090
pushq $0x8
popq %rdi
callq 0x2270