#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_dm_quad(int n, double *cor, double *dm0, double *dmk, double amp,
                  double *unitcell, double *unitcell_reciproke,
                  int *unitcell_active, double *gradient) {
  int i, j;
  double d, d0, k, tmp, result;
  double delta[3];
  result = 0.0;
  for (i = 0; i < n; i++) {
    for (j = 0; j < i; j++) {
      d0 = dm0[i * n + j];
      k = dmk[i * n + j];
      if (d0 > 0) {
        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 - d0);
        result += amp * k * tmp * tmp;
        if (gradient != NULL) {
          tmp = 2 * amp * k * tmp / d;
          add_grad(i, j, tmp, delta, gradient);
        }
      }
    }
  }
  return result;
}


x86 assembly (clang, -O0):
<ff_dm_quad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movsd  %xmm0,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x68(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1324 <ff_dm_quad+0x1f4>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    1311 <ff_dm_quad+0x1e1>
mov    -0x18(%rbp),%rax
mov    -0x3c(%rbp),%ecx
imul   -0x4(%rbp),%ecx
add    -0x40(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x3c(%rbp),%ecx
imul   -0x4(%rbp),%ecx
add    -0x40(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x50(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    12fe <ff_dm_quad+0x1ce>
mov    -0x10(%rbp),%rdi
imul   $0x3,-0x3c(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
imul   $0x3,-0x40(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
lea    -0x80(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    -0x38(%rbp),%r8
mov    0x10(%rbp),%r9
call   1030 <calc_delta@plt>
movsd  -0x80(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x60(%rbp),%xmm0
mulsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x78(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x60(%rbp),%xmm0
movsd  -0x60(%rbp),%xmm2
movsd  -0x48(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x70(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x60(%rbp),%xmm0
movsd  -0x60(%rbp),%xmm2
movsd  -0x48(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
call   1050 <sqrt@plt>
movsd  %xmm0,-0x48(%rbp)
movsd  -0x48(%rbp),%xmm0
subsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x28(%rbp),%xmm0
mulsd  -0x58(%rbp),%xmm0
mulsd  -0x60(%rbp),%xmm0
movsd  -0x60(%rbp),%xmm2
movsd  -0x68(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x68(%rbp)
cmpq   $0x0,0x18(%rbp)
je     12f9 <ff_dm_quad+0x1c9>
movsd  0xd38(%rip),%xmm0
mulsd  -0x28(%rbp),%xmm0
mulsd  -0x58(%rbp),%xmm0
mulsd  -0x60(%rbp),%xmm0
divsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    -0x3c(%rbp),%edi
mov    -0x40(%rbp),%esi
movsd  -0x60(%rbp),%xmm0
lea    -0x80(%rbp),%rdx
mov    0x18(%rbp),%rcx
call   1040 <add_grad@plt>
jmp    12fe <ff_dm_quad+0x1ce>
jmp    1303 <ff_dm_quad+0x1d3>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1181 <ff_dm_quad+0x51>
jmp    1316 <ff_dm_quad+0x1e6>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    116e <ff_dm_quad+0x3e>
movsd  -0x68(%rbp),%xmm0
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ff_dm_quad>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %r9,0x60(%rsp)
mov    %r8,0x58(%rsp)
mov    %rcx,0x50(%rsp)
mov    %rsi,0x28(%rsp)
test   %edi,%edi
jle    1350 <ff_dm_quad+0x220>
movapd %xmm0,%xmm2
mov    %rdx,%r12
mov    0xd8(%rsp),%rbp
addsd  %xmm2,%xmm0
movsd  %xmm0,0x48(%rsp)
mov    %edi,%eax
mov    %rax,0x38(%rsp)
lea    0x0(,%rax,8),%rax
mov    %rax,0x30(%rsp)
xorpd  %xmm0,%xmm0
xor    %esi,%esi
xor    %r13d,%r13d
xor    %eax,%eax
xorpd  %xmm1,%xmm1
movsd  %xmm2,0x18(%rsp)
jmp    11da <ff_dm_quad+0xaa>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movapd %xmm1,%xmm3
mov    0x10(%rsp),%rax
add    $0x1,%rax
mov    0x40(%rsp),%r13
add    0x30(%rsp),%r13
add    $0x3,%rsi
movapd %xmm3,%xmm1
cmp    0x38(%rsp),%rax
je     1354 <ff_dm_quad+0x224>
test   %rax,%rax
mov    %r13,0x40(%rsp)
mov    %rax,0x10(%rsp)
je     11b0 <ff_dm_quad+0x80>
lea    (%rax,%rax,2),%eax
mov    0x28(%rsp),%rcx
lea    (%rcx,%rax,8),%rax
mov    %rax,0x68(%rsp)
xor    %r15d,%r15d
xor    %r14d,%r14d
mov    %rsi,0x20(%rsp)
jmp    1229 <ff_dm_quad+0xf9>
nopw   0x0(%rax,%rax,1)
movapd %xmm1,%xmm3
add    $0x3,%r14
add    $0x1,%r15d
add    $0x8,%r13
movapd %xmm3,%xmm1
cmp    %r14,%rsi
je     11b4 <ff_dm_quad+0x84>
movsd  (%r12,%r13,1),%xmm3
ucomisd %xmm0,%xmm3
jbe    1210 <ff_dm_quad+0xe0>
movsd  %xmm3,0x70(%rsp)
movsd  %xmm1,0x8(%rsp)
mov    %rbp,%rbx
mov    0x50(%rsp),%rax
movsd  (%rax,%r13,1),%xmm0
movsd  %xmm0,0x78(%rsp)
mov    %r14d,%eax
mov    0x28(%rsp),%rcx
lea    (%rcx,%rax,8),%rsi
mov    0x68(%rsp),%rdi
lea    0x80(%rsp),%rdx
mov    0x58(%rsp),%rcx
mov    0x60(%rsp),%r8
mov    0xd0(%rsp),%r9
call   1030 <calc_delta@plt>
movsd  0x80(%rsp),%xmm0
movsd  0x88(%rsp),%xmm1
mulsd  %xmm0,%xmm0
mulsd  %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0x90(%rsp),%xmm0
mulsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
ucomisd 0xd44(%rip),%xmm0
jb     12d0 <ff_dm_quad+0x1a0>
sqrtsd %xmm0,%xmm0
jmp    12d5 <ff_dm_quad+0x1a5>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
call   1050 <sqrt@plt>
movsd  0x18(%rsp),%xmm2
mov    %rbx,%rbp
movapd %xmm0,%xmm1
subsd  0x70(%rsp),%xmm1
movsd  0x78(%rsp),%xmm4
movapd %xmm4,%xmm3
mulsd  %xmm2,%xmm3
mulsd  %xmm1,%xmm3
mulsd  %xmm1,%xmm3
addsd  0x8(%rsp),%xmm3
test   %rbx,%rbx
je     1342 <ff_dm_quad+0x212>
mulsd  0x48(%rsp),%xmm4
mulsd  %xmm1,%xmm4
divsd  %xmm0,%xmm4
mov    0x10(%rsp),%rdi
mov    %r15d,%esi
movapd %xmm4,%xmm0
lea    0x80(%rsp),%rdx
mov    %rbp,%rcx
movsd  %xmm3,0x8(%rsp)
call   1040 <add_grad@plt>
movsd  0x8(%rsp),%xmm3
mov    %rbx,%rbp
xorpd  %xmm0,%xmm0
mov    0x20(%rsp),%rsi
jmp    1214 <ff_dm_quad+0xe4>
xorpd  %xmm3,%xmm3
movapd %xmm3,%xmm0
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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