#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ add_grad(int, int, double, double *, double *);
int /*<<< orphan*/ calc_delta(double *, double *, double *, double *, double *,
                              int *);
double sqrt(double);
double ff_bond_quad(int m, int n, double *cor, int *pairs, double *lengths,
                    double amp, double *unitcell, double *unitcell_reciproke,
                    int *unitcell_active, double *gradient) {
  int b, i, j;
  double result, d, tmp;
  double delta[3];
  result = 0.0;
  for (b = 0; b < m; b++) {
    i = pairs[2 * b];
    j = pairs[2 * b + 1];
    calc_delta(cor + 3 * i, cor + 3 * j, delta, unitcell, unitcell_reciproke,
               unitcell_active);
    tmp = delta[0];
    d = tmp * tmp;
    tmp = delta[1];
    d += tmp * tmp;
    tmp = delta[2];
    d += tmp * tmp;
    d = sqrt(d);
    tmp = d - lengths[b];
    result += amp * tmp * tmp;
    if (gradient != NULL) {
      tmp = 2 * amp * tmp / d;
      add_grad(i, j, tmp, delta, gradient);
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<ff_bond_quad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
movsd  %xmm0,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12d8 <ff_bond_quad+0x1a8>
mov    -0x18(%rbp),%rax
mov    -0x34(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x34(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
imul   $0x3,-0x38(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
imul   $0x3,-0x3c(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
lea    -0x70(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    0x10(%rbp),%r8
mov    0x18(%rbp),%r9
call   1030 <calc_delta@plt>
movsd  -0x70(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x58(%rbp),%xmm0
mulsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x68(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x58(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm2
movsd  -0x50(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x58(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm2
movsd  -0x50(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
call   1050 <sqrt@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
subsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x28(%rbp),%xmm0
mulsd  -0x58(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm2
movsd  -0x48(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
cmpq   $0x0,0x20(%rbp)
je     12c5 <ff_bond_quad+0x195>
movsd  0xd67(%rip),%xmm0
mulsd  -0x28(%rbp),%xmm0
mulsd  -0x58(%rbp),%xmm0
divsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x3c(%rbp),%esi
movsd  -0x58(%rbp),%xmm0
lea    -0x70(%rbp),%rdx
mov    0x20(%rbp),%rcx
call   1040 <add_grad@plt>
jmp    12ca <ff_bond_quad+0x19a>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    116e <ff_bond_quad+0x3e>
movsd  -0x48(%rbp),%xmm0
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ff_bond_quad>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r9,0x28(%rsp)
mov    %r8,0x20(%rsp)
test   %edi,%edi
jle    1298 <ff_bond_quad+0x168>
mov    %rcx,%r12
mov    %rdx,%rbx
mov    0xa0(%rsp),%rax
movapd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
mov    %edi,%ecx
mov    %rcx,0x10(%rsp)
xor    %r15d,%r15d
xorpd  %xmm1,%xmm1
movsd  %xmm0,0x18(%rsp)
jmp    11bf <ff_bond_quad+0x8f>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movapd %xmm2,%xmm1
movsd  0x18(%rsp),%xmm0
mulsd  %xmm0,%xmm1
mulsd  %xmm2,%xmm1
movsd  0x38(%rsp),%xmm2
addsd  %xmm1,%xmm2
movapd %xmm2,%xmm1
add    $0x1,%r15
cmp    %r15,0x10(%rsp)
je     129c <ff_bond_quad+0x16c>
movsd  %xmm1,0x38(%rsp)
mov    %rax,%r14
movslq (%r12,%r15,8),%r13
movslq 0x4(%r12,%r15,8),%rbp
lea    0x0(,%r13,2),%rax
add    %r13,%rax
lea    (%rbx,%rax,8),%rdi
lea    0x0(%rbp,%rbp,2),%rax
lea    (%rbx,%rax,8),%rsi
lea    0x40(%rsp),%rdx
mov    0x28(%rsp),%rcx
mov    0x90(%rsp),%r8
mov    0x98(%rsp),%r9
call   1030 <calc_delta@plt>
movsd  0x40(%rsp),%xmm0
movsd  0x48(%rsp),%xmm1
mulsd  %xmm0,%xmm0
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0x50(%rsp),%xmm0
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
ucomisd 0xdca(%rip),%xmm0
jb     1240 <ff_bond_quad+0x110>
sqrtsd %xmm0,%xmm0
jmp    1245 <ff_bond_quad+0x115>
xchg   %ax,%ax
call   1050 <sqrt@plt>
movapd %xmm0,%xmm2
mov    0x20(%rsp),%rax
subsd  (%rax,%r15,8),%xmm2
mov    %r14,%rax
test   %r14,%r14
je     1190 <ff_bond_quad+0x60>
movsd  0x8(%rsp),%xmm1
mulsd  %xmm2,%xmm1
divsd  %xmm0,%xmm1
mov    %r13d,%edi
mov    %ebp,%esi
movapd %xmm1,%xmm0
lea    0x40(%rsp),%rdx
mov    %r14,%rcx
movsd  %xmm2,0x30(%rsp)
call   1040 <add_grad@plt>
movsd  0x30(%rsp),%xmm2
mov    %r14,%rax
jmp    1190 <ff_bond_quad+0x60>
xorpd  %xmm1,%xmm1
movapd %xmm1,%xmm0
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @ff_bond_quad(i32 %0, i32 %1, double* %2, i32* %3, double* %4, double %5, double* %6, double* %7, i32* %8, double* %9) {
%11 = alloca i32
%12 = alloca i32
%13 = alloca double*
%14 = alloca i32*
%15 = alloca double*
%16 = alloca double
%17 = alloca double*
%18 = alloca double*
%19 = alloca i32*
%20 = alloca double*
%21 = alloca i32
%22 = alloca i32
%23 = alloca i32
%24 = alloca double
%25 = alloca double
%26 = alloca double
%27 = alloca [3 x double]
store i32 %0, i32* %11
store i32 %1, i32* %12
store double* %2, double** %13
store i32* %3, i32** %14
store double* %4, double** %15
store double %5, double* %16
store double* %6, double** %17
store double* %7, double** %18
store i32* %8, i32** %19
store double* %9, double** %20
store double 0.000000e+00, double* %24
store i32 0, i32* %21
br label %28
28: ; preds = %109, %10
%29 = load i32, i32* %21
%30 = load i32, i32* %11
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %112
32: ; preds = %28
%33 = load i32*, i32** %14
%34 = load i32, i32* %21
%35 = mul nsw i32 2, %34
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %33, i64 %36
%38 = load i32, i32* %37
store i32 %38, i32* %22
%39 = load i32*, i32** %14
%40 = load i32, i32* %21
%41 = mul nsw i32 2, %40
%42 = add nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %39, i64 %43
%45 = load i32, i32* %44
store i32 %45, i32* %23
%46 = load double*, double** %13
%47 = load i32, i32* %22
%48 = mul nsw i32 3, %47
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds double, double* %46, i64 %49
%51 = load double*, double** %13
%52 = load i32, i32* %23
%53 = mul nsw i32 3, %52
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds double, double* %51, i64 %54
%56 = getelementptr inbounds [3 x double], [3 x double]* %27, i64 0, i64 0
%57 = load double*, double** %17
%58 = load double*, double** %18
%59 = load i32*, i32** %19
%60 = call i32 @calc_delta(double* %50, double* %55, double* %56, double* %57, double* %58, i32* %59)
%61 = getelementptr inbounds [3 x double], [3 x double]* %27, i64 0, i64 0
%62 = load double, double* %61
store double %62, double* %26
%63 = load double, double* %26
%64 = load double, double* %26
%65 = fmul double %63, %64
store double %65, double* %25
%66 = getelementptr inbounds [3 x double], [3 x double]* %27, i64 0, i64 1
%67 = load double, double* %66
store double %67, double* %26
%68 = load double, double* %26
%69 = load double, double* %26
%70 = load double, double* %25
%71 = call double @llvm.fmuladd.f64(double %68, double %69, double %70)
store double %71, double* %25
%72 = getelementptr inbounds [3 x double], [3 x double]* %27, i64 0, i64 2
%73 = load double, double* %72
store double %73, double* %26
%74 = load double, double* %26
%75 = load double, double* %26
%76 = load double, double* %25
%77 = call double @llvm.fmuladd.f64(double %74, double %75, double %76)
store double %77, double* %25
%78 = load double, double* %25
%79 = call double @sqrt(double %78)
store double %79, double* %25
%80 = load double, double* %25
%81 = load double*, double** %15
%82 = load i32, i32* %21
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds double, double* %81, i64 %83
%85 = load double, double* %84
%86 = fsub double %80, %85
store double %86, double* %26
%87 = load double, double* %16
%88 = load double, double* %26
%89 = fmul double %87, %88
%90 = load double, double* %26
%91 = load double, double* %24
%92 = call double @llvm.fmuladd.f64(double %89, double %90, double %91)
store double %92, double* %24
%93 = load double*, double** %20
%94 = icmp ne double* %93, null
br i1 %94, label %95, label %108
95: ; preds = %32
%96 = load double, double* %16
%97 = fmul double 2.000000e+00, %96
%98 = load double, double* %26
%99 = fmul double %97, %98
%100 = load double, double* %25
%101 = fdiv double %99, %100
store double %101, double* %26
%102 = load i32, i32* %22
%103 = load i32, i32* %23
%104 = load double, double* %26
%105 = getelementptr inbounds [3 x double], [3 x double]* %27, i64 0, i64 0
%106 = load double*, double** %20
%107 = call i32 @add_grad(i32 %102, i32 %103, double %104, double* %105, double* %106)
br label %108
108: ; preds = %95, %32
br label %109
109: ; preds = %108
%110 = load i32, i32* %21
%111 = add nsw i32 %110, 1
store i32 %111, i32* %21
br label %28
112: ; preds = %28
%113 = load double, double* %24
ret double %113
}

LLVM IR (-O2):
define double @ff_bond_quad(i32 %0, i32 %1, double* %2, i32* nocapture %3, double* nocapture %4, double %5, double* %6, double* %7, i32* %8, double* %9) {
%11 = alloca [3 x double]
%12 = bitcast [3 x double]* %11 to i8*
%13 = icmp sgt i32 %0, 0
br i1 %13, label %14, label %56
14: ; preds = %10
%15 = getelementptr inbounds [3 x double], [3 x double]* %11, i64 0, i64 0
%16 = getelementptr inbounds [3 x double], [3 x double]* %11, i64 0, i64 1
%17 = getelementptr inbounds [3 x double], [3 x double]* %11, i64 0, i64 2
%18 = icmp eq double* %9, null
%19 = fmul double %5, 2.000000e+00
%20 = zext i32 %0 to i64
br label %21
21: ; preds = %14, %53
%22 = phi i64 [ 0, %14 ], [ %54, %53 ]
%23 = phi double [ 0.000000e+00, %14 ], [ %48, %53 ]
%24 = shl nuw nsw i64 %22, 1
%25 = getelementptr inbounds i32, i32* %3, i64 %24
%26 = load i32, i32* %25
%27 = or i64 %24, 1
%28 = getelementptr inbounds i32, i32* %3, i64 %27
%29 = load i32, i32* %28
%30 = mul nsw i32 %26, 3
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds double, double* %2, i64 %31
%33 = mul nsw i32 %29, 3
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds double, double* %2, i64 %34
%36 = call i32 @calc_delta(double* %32, double* %35, double* %15, double* %6, double* %7, i32* %8)
%37 = load double, double* %15
%38 = fmul double %37, %37
%39 = load double, double* %16
%40 = call double @llvm.fmuladd.f64(double %39, double %39, double %38)
%41 = load double, double* %17
%42 = call double @llvm.fmuladd.f64(double %41, double %41, double %40)
%43 = call double @sqrt(double %42)
%44 = getelementptr inbounds double, double* %4, i64 %22
%45 = load double, double* %44
%46 = fsub double %43, %45
%47 = fmul double %46, %5
%48 = call double @llvm.fmuladd.f64(double %47, double %46, double %23)
br i1 %18, label %53, label %49
49: ; preds = %21
%50 = fmul double %19, %46
%51 = fdiv double %50, %43
%52 = call i32 @add_grad(i32 %26, i32 %29, double %51, double* %15, double* %9)
br label %53
53: ; preds = %21, %49
%54 = add nuw nsw i64 %22, 1
%55 = icmp eq i64 %54, %20
br i1 %55, label %56, label %21
56: ; preds = %53, %10
%57 = phi double [ 0.000000e+00, %10 ], [ %48, %53 ]
ret double %57
}