// File: /workspace/github_c_makefile_star2_O3/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, 0x5d5df(%rip)     # 0x79290
je 0x1bcbe
movl 0x5d63f(%rip), %eax     # 0x792f8
jmp 0x1be86
movq %rcx, %r15
movl %edx, %r14d
movq %rsi, %r12
movl $0x1, 0x5d5bf(%rip)     # 0x79290
leal (%r14,%r14), %eax
movl %eax, 0x8(%rsp)
cltq
imulq $0xc8, %rax, %rdi
callq 0x21e0
movq %rax, 0x5d602(%rip)     # 0x792f0
testl %r14d, %r14d
movq %r14, 0x10(%rsp)
jle 0x1bdb5
movq %rax, %r13
addq $0x20, %r15
xorl %ebp, %ebp
movq 0x8(%r15), %rax
testq %rax, %rax
je 0x1bda3
cmpl $0x0, 0x14(%r15)
jns 0x1bda3
movl (%r15), %ecx
movl %ecx, (%r13)
movl 0x20(%rax), %eax
movl %eax, 0x4(%r13)
movq %r13, %rdi
movq %r12, %rsi
callq 0x1d508
testl %eax, %eax
je 0x1bd51
movsd 0x4d146(%rip), %xmm0    # 0x68e88
movsd %xmm0, 0x28(%r13)
addq $0xc8, %r13
incl %ebp
movq 0x8(%r15), %rax
movq 0x28(%rax), %rax
testq %rax, %rax
je 0x1bd99
cmpl $0x0, 0x18(%r15)
jns 0x1bd99
movl (%r15), %ecx
movl %ecx, (%r13)
movl 0x20(%rax), %eax
movl %eax, 0x4(%r13)
movq %r13, %rdi
movq %r12, %rsi
callq 0x1d508
testl %eax, %eax
je 0x1bd99
movsd 0x4d0fe(%rip), %xmm0    # 0x68e88
movsd %xmm0, 0x28(%r13)
addq $0xc8, %r13
incl %ebp
cmpl 0x8(%rsp), %ebp
jg 0x1bf45
addq $0xc0, %r15
decl %r14d
jne 0x1bd05
jmp 0x1bdb7
xorl %ebp, %ebp
leaq 0x3be02(%rip), %rdi     # 0x57bc0
xorl %r15d, %r15d
movl %ebp, %esi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x20f0
movl %ebp, 0x5d523(%rip)     # 0x792f8
movq 0x5d514(%rip), %rdi     # 0x792f0
movl %ebp, %esi
xorl %edx, %edx
callq 0x182fb
leaq 0x4d2ec(%rip), %r14     # 0x690d8
movss (%r14), %xmm0
movss %xmm0, 0x8(%rsp)
cvtss2sd %xmm0, %xmm0
mulsd 0x4d085(%rip), %xmm0    # 0x68e88
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14)
movq 0x5d4dd(%rip), %rdi     # 0x792f0
movl 0x5d4df(%rip), %esi     # 0x792f8
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1d581
movss 0x8(%rsp), %xmm0
movss %xmm0, (%r14)
movq $0x0, 0x5d4c8(%rip) # 0x79300
cmpl $0x0, 0x5d4b9(%rip)     # 0x792f8
jle 0x1bf2a
movq 0x5d4a4(%rip), %r14     # 0x792f0
xorps %xmm0, %xmm0
cvtsd2ss 0x20(%r14), %xmm0
movq %r14, %rdi
callq 0x1c59a
cvtss2sd %xmm0, %xmm0
addsd 0x5d497(%rip), %xmm0    # 0x79300
movsd %xmm0, 0x5d48f(%rip)    # 0x79300
addq $0xc8, %r14
incl %r15d
movl 0x5d477(%rip), %eax     # 0x792f8
cmpl %eax, %r15d
jl 0x1be4c
testl %eax, %eax
jle 0x1bf2a
xorl %r15d, %r15d
movq 0x5d458(%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 0x1bebf
ucomisd 0x35292(%rip), %xmm0    # 0x51178
jb 0x1beee
sqrtsd %xmm0, %xmm0
jmp 0x1bef3
callq 0x22a0
cvtsd2ss %xmm0, %xmm0
movq %r14, %rdi
callq 0x1c59a
cvtss2sd %xmm0, %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
addq $0xc8, %r14
incl %r15d
cmpl 0x5d3d6(%rip), %r15d    # 0x792f8
jl 0x1be9c
jmp 0x1bf2e
xorpd %xmm0, %xmm0
subsd 0x5d3ca(%rip), %xmm0    # 0x79300
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3c2e5(%rip), %rdi     # 0x58231
callq 0x2090
movl $0x8, %edi
callq 0x2280