// File: /workspace/github_c_makefile_star2_O1/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, 0x5cec6(%rip)     # 0x79290
jne 0x1c592
movq %rcx, %r15
movl %edx, %r14d
movq %rsi, %r12
movl $0x1, 0x5cead(%rip)     # 0x79290
leal (%r14,%r14), %eax
movl %eax, 0x8(%rsp)
cltq
imulq $0xc8, %rax, %rdi
callq 0x21d0
movq %rax, 0x5cef0(%rip)     # 0x792f0
testl %r14d, %r14d
movq %r14, 0x10(%rsp)
jle 0x1c4c7
movq %rax, %r13
addq $0x20, %r15
xorl %ebp, %ebp
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x1c4b5
cmpl $0x0, 0x14(%r15)
jns 0x1c4b5
movl (%r15), %ecx
movl %ecx, (%r13)
movl 0x20(%rax), %eax
movl %eax, 0x4(%r13)
movq %r13, %rdi
movq %r12, %rsi
callq 0x1dc49
testl %eax, %eax
je 0x1c463
movsd 0x4ca34(%rip), %xmm0    # 0x68e88
movsd %xmm0, 0x28(%r13)
addq $0xc8, %r13
incl %ebp
movq 0x8(%r15), %rax
movq 0x28(%rax), %rax
testq %rax, %rax
je 0x1c4ab
cmpl $0x0, 0x18(%r15)
jns 0x1c4ab
movl (%r15), %ecx
movl %ecx, (%r13)
movl 0x20(%rax), %eax
movl %eax, 0x4(%r13)
movq %r13, %rdi
movq %r12, %rsi
callq 0x1dc49
testl %eax, %eax
je 0x1c4ab
movsd 0x4c9ec(%rip), %xmm0    # 0x68e88
movsd %xmm0, 0x28(%r13)
addq $0xc8, %r13
incl %ebp
cmpl 0x8(%rsp), %ebp
jg 0x1c656
addq $0xc0, %r15
decl %r14d
jne 0x1c417
jmp 0x1c4c9
xorl %ebp, %ebp
leaq 0x3b730(%rip), %rdi     # 0x57c00
xorl %r15d, %r15d
movl %ebp, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x20f0
movl %ebp, 0x5ce11(%rip)     # 0x792f8
movq 0x5ce02(%rip), %rdi     # 0x792f0
movl %ebp, %esi
xorl %edx, %edx
callq 0x18a1d
leaq 0x4cbda(%rip), %r14     # 0x690d8
movss (%r14), %xmm0
movss %xmm0, 0x8(%rsp)
cvtss2sd %xmm0, %xmm0
mulsd 0x4c973(%rip), %xmm0    # 0x68e88
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14)
movq 0x5cdcb(%rip), %rdi     # 0x792f0
movl 0x5cdcd(%rip), %esi     # 0x792f8
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1dcc2
movss 0x8(%rsp), %xmm0
movss %xmm0, (%r14)
movq $0x0, 0x5cdb6(%rip) # 0x79300
cmpl $0x0, 0x5cda7(%rip)     # 0x792f8
jle 0x1c592
movq 0x5cd96(%rip), %r14     # 0x792f0
xorps %xmm0, %xmm0
cvtsd2ss 0x20(%r14), %xmm0
movq %r14, %rdi
callq 0x1ccef
cvtss2sd %xmm0, %xmm0
addsd 0x5cd89(%rip), %xmm0    # 0x79300
movsd %xmm0, 0x5cd81(%rip)    # 0x79300
addq $0xc8, %r14
incl %r15d
cmpl 0x5cd68(%rip), %r15d    # 0x792f8
jl 0x1c55a
cmpl $0x0, 0x5cd5f(%rip)     # 0x792f8
jle 0x1c63b
xorl %r15d, %r15d
movq 0x5cd47(%rip), %r14     # 0x792f0
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movslq (%r14), %rax
leaq (%rax,%rax,2), %rax
leaq (%rbx,%rax,4), %rax
movslq 0x4(%r14), %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rbx,%rcx,4), %rcx
xorl %edx, %edx
xorpd %xmm0, %xmm0
movss (%rax,%rdx,4), %xmm1
subss (%rcx,%rdx,4), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rdx
cmpq $0x3, %rdx
jne 0x1c5d0
ucomisd 0x34a69(%rip), %xmm0    # 0x51060
jb 0x1c5ff
sqrtsd %xmm0, %xmm0
jmp 0x1c604
callq 0x2290
cvtsd2ss %xmm0, %xmm0
movq %r14, %rdi
callq 0x1ccef
cvtss2sd %xmm0, %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
addq $0xc8, %r14
incl %r15d
cmpl 0x5ccc5(%rip), %r15d    # 0x792f8
jl 0x1c5ad
jmp 0x1c63f
xorpd %xmm0, %xmm0
subsd 0x5ccb9(%rip), %xmm0    # 0x79300
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3bc14(%rip), %rdi     # 0x58271
callq 0x2090
movl $0x8, %edi
callq 0x2270