#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 */
size_t index_3D_to_1D(int, int, int, int, int, int);
double lagrange_basis(int, double, double const *, int);
double lagrange_interp_3D(double x, double y, double z, const double x_values[],
                          int num_x, const double y_values[], int num_y,
                          const double z_values[], int num_z,
                          const double f_values[]) {
  double out = 0;
  double lx, ly, lz, f;
  for (int i = 0; i < num_x; i++) {
    lx = lagrange_basis(i, x, x_values, num_x);
    for (int j = 0; j < num_y; j++) {
      ly = lagrange_basis(i, y, y_values, num_y);
      for (int k = 0; k < num_z; k++) {
        f = f_values[index_3D_to_1D(i, num_x, j, num_y, k, num_z)];
        lz = lagrange_basis(i, z, z_values, num_z);
        out += f * lx * ly * lz;
      }
    }
  }
  return out;
}


x86 assembly (clang, -O0):
<lagrange_interp_3D>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    0x10(%rbp),%rax
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
movsd  %xmm2,-0x18(%rbp)
mov    %rdi,-0x20(%rbp)
mov    %esi,-0x24(%rbp)
mov    %rdx,-0x30(%rbp)
mov    %ecx,-0x34(%rbp)
mov    %r8,-0x40(%rbp)
mov    %r9d,-0x44(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x50(%rbp)
movl   $0x0,-0x74(%rbp)
mov    -0x74(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1272 <lagrange_interp_3D+0x152>
mov    -0x74(%rbp),%edi
movsd  -0x8(%rbp),%xmm0
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <lagrange_basis@plt>
movsd  %xmm0,-0x58(%rbp)
movl   $0x0,-0x78(%rbp)
mov    -0x78(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    125f <lagrange_interp_3D+0x13f>
mov    -0x74(%rbp),%edi
movsd  -0x10(%rbp),%xmm0
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1040 <lagrange_basis@plt>
movsd  %xmm0,-0x60(%rbp)
movl   $0x0,-0x7c(%rbp)
mov    -0x7c(%rbp),%eax
cmp    -0x44(%rbp),%eax
jge    124c <lagrange_interp_3D+0x12c>
mov    0x10(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x74(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    -0x78(%rbp),%edx
mov    -0x34(%rbp),%ecx
mov    -0x7c(%rbp),%r8d
mov    -0x44(%rbp),%r9d
call   1030 <index_3D_to_1D@plt>
mov    %rax,%rcx
mov    -0x88(%rbp),%rax
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x70(%rbp)
mov    -0x74(%rbp),%edi
movsd  -0x18(%rbp),%xmm0
mov    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1040 <lagrange_basis@plt>
movsd  %xmm0,-0x68(%rbp)
movsd  -0x70(%rbp),%xmm0
mulsd  -0x58(%rbp),%xmm0
mulsd  -0x60(%rbp),%xmm0
movsd  -0x68(%rbp),%xmm2
movsd  -0x50(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x7c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7c(%rbp)
jmp    11bb <lagrange_interp_3D+0x9b>
jmp    1251 <lagrange_interp_3D+0x131>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    118f <lagrange_interp_3D+0x6f>
jmp    1264 <lagrange_interp_3D+0x144>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    1163 <lagrange_interp_3D+0x43>
movsd  -0x50(%rbp),%xmm0
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lagrange_interp_3D>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rdx,0x30(%rsp)
mov    %rdi,0x18(%rsp)
movsd  %xmm1,0x28(%rsp)
movsd  %xmm0,0x10(%rsp)
test   %esi,%esi
jle    1285 <lagrange_interp_3D+0x165>
mov    %r9d,%ebx
mov    %ecx,%r15d
mov    %esi,%edx
movsd  %xmm2,0x38(%rsp)
mov    %r8,0x20(%rsp)
xorpd  %xmm0,%xmm0
xor    %ebp,%ebp
mov    %esi,0x8(%rsp)
mov    %ecx,0xc(%rsp)
mov    0x20(%rsp),%r12
jmp    1194 <lagrange_interp_3D+0x74>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  (%rsp),%xmm0
add    $0x1,%ebp
mov    0x8(%rsp),%edx
cmp    %edx,%ebp
je     1289 <lagrange_interp_3D+0x169>
movsd  %xmm0,(%rsp)
mov    %ebp,%edi
movsd  0x10(%rsp),%xmm0
mov    0x18(%rsp),%rsi
call   1040 <lagrange_basis@plt>
test   %r15d,%r15d
jle    1180 <lagrange_interp_3D+0x60>
movsd  %xmm0,0x40(%rsp)
xor    %eax,%eax
movsd  (%rsp),%xmm0
jmp    11d5 <lagrange_interp_3D+0xb5>
nop
movsd  (%rsp),%xmm0
mov    %r13d,%eax
add    $0x1,%eax
mov    0xc(%rsp),%r15d
cmp    %r15d,%eax
je     1185 <lagrange_interp_3D+0x65>
mov    %eax,%r13d
movsd  %xmm0,(%rsp)
mov    %ebp,%edi
movsd  0x28(%rsp),%xmm0
mov    0x30(%rsp),%rsi
mov    %r15d,%edx
call   1040 <lagrange_basis@plt>
test   %ebx,%ebx
jle    11c0 <lagrange_interp_3D+0xa0>
movaps %xmm0,%xmm1
xor    %r14d,%r14d
mov    0x8(%rsp),%r15d
movsd  (%rsp),%xmm0
movsd  %xmm1,0x48(%rsp)
nopl   0x0(%rax)
movsd  %xmm0,(%rsp)
mov    %ebp,%edi
mov    %r15d,%esi
mov    %r13d,%edx
mov    0xc(%rsp),%ecx
mov    %r14d,%r8d
mov    %ebx,%r9d
call   1030 <index_3D_to_1D@plt>
mov    0x90(%rsp),%rcx
movsd  (%rcx,%rax,8),%xmm0
mulsd  0x40(%rsp),%xmm0
movsd  %xmm0,0x50(%rsp)
mov    %ebp,%edi
movsd  0x38(%rsp),%xmm0
mov    %r12,%rsi
mov    %ebx,%edx
call   1040 <lagrange_basis@plt>
movsd  0x48(%rsp),%xmm1
mov    %r13d,%eax
movsd  0x50(%rsp),%xmm2
mulsd  %xmm1,%xmm2
mulsd  %xmm0,%xmm2
movsd  (%rsp),%xmm0
addsd  %xmm2,%xmm0
add    $0x1,%r14d
cmp    %r14d,%ebx
jne    1210 <lagrange_interp_3D+0xf0>
jmp    11c8 <lagrange_interp_3D+0xa8>
xorpd  %xmm0,%xmm0
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @lagrange_interp_3D(double %0, double %1, double %2, double* %3, i32 %4, double* %5, i32 %6, double* %7, i32 %8, double* %9) {
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double*
%15 = alloca i32
%16 = alloca double*
%17 = alloca i32
%18 = alloca double*
%19 = alloca i32
%20 = alloca double*
%21 = alloca double
%22 = alloca double
%23 = alloca double
%24 = alloca double
%25 = alloca double
%26 = alloca i32
%27 = alloca i32
%28 = alloca i32
store double %0, double* %11
store double %1, double* %12
store double %2, double* %13
store double* %3, double** %14
store i32 %4, i32* %15
store double* %5, double** %16
store i32 %6, i32* %17
store double* %7, double** %18
store i32 %8, i32* %19
store double* %9, double** %20
store double 0.000000e+00, double* %21
store i32 0, i32* %26
br label %29
29: ; preds = %85, %10
%30 = load i32, i32* %26
%31 = load i32, i32* %15
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %88
33: ; preds = %29
%34 = load i32, i32* %26
%35 = load double, double* %11
%36 = load double*, double** %14
%37 = load i32, i32* %15
%38 = call double @lagrange_basis(i32 %34, double %35, double* %36, i32 %37)
store double %38, double* %22
store i32 0, i32* %27
br label %39
39: ; preds = %81, %33
%40 = load i32, i32* %27
%41 = load i32, i32* %17
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %84
43: ; preds = %39
%44 = load i32, i32* %26
%45 = load double, double* %12
%46 = load double*, double** %16
%47 = load i32, i32* %17
%48 = call double @lagrange_basis(i32 %44, double %45, double* %46, i32 %47)
store double %48, double* %23
store i32 0, i32* %28
br label %49
49: ; preds = %77, %43
%50 = load i32, i32* %28
%51 = load i32, i32* %19
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %80
53: ; preds = %49
%54 = load double*, double** %20
%55 = load i32, i32* %26
%56 = load i32, i32* %15
%57 = load i32, i32* %27
%58 = load i32, i32* %17
%59 = load i32, i32* %28
%60 = load i32, i32* %19
%61 = call i64 @index_3D_to_1D(i32 %55, i32 %56, i32 %57, i32 %58, i32 %59, i32 %60)
%62 = getelementptr inbounds double, double* %54, i64 %61
%63 = load double, double* %62
store double %63, double* %25
%64 = load i32, i32* %26
%65 = load double, double* %13
%66 = load double*, double** %18
%67 = load i32, i32* %19
%68 = call double @lagrange_basis(i32 %64, double %65, double* %66, i32 %67)
store double %68, double* %24
%69 = load double, double* %25
%70 = load double, double* %22
%71 = fmul double %69, %70
%72 = load double, double* %23
%73 = fmul double %71, %72
%74 = load double, double* %24
%75 = load double, double* %21
%76 = call double @llvm.fmuladd.f64(double %73, double %74, double %75)
store double %76, double* %21
br label %77
77: ; preds = %53
%78 = load i32, i32* %28
%79 = add nsw i32 %78, 1
store i32 %79, i32* %28
br label %49
80: ; preds = %49
br label %81
81: ; preds = %80
%82 = load i32, i32* %27
%83 = add nsw i32 %82, 1
store i32 %83, i32* %27
br label %39
84: ; preds = %39
br label %85
85: ; preds = %84
%86 = load i32, i32* %26
%87 = add nsw i32 %86, 1
store i32 %87, i32* %26
br label %29
88: ; preds = %29
%89 = load double, double* %21
ret double %89
}

LLVM IR (-O2):
define double @lagrange_interp_3D(double %0, double %1, double %2, double* %3, i32 %4, double* %5, i32 %6, double* %7, i32 %8, double* nocapture %9) {
%11 = icmp sgt i32 %4, 0
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = icmp sgt i32 %6, 0
%14 = icmp sgt i32 %8, 0
br label %17
15: ; preds = %21, %10
%16 = phi double [ 0.000000e+00, %10 ], [ %22, %21 ]
ret double %16
17: ; preds = %12, %21
%18 = phi i32 [ 0, %12 ], [ %23, %21 ]
%19 = phi double [ 0.000000e+00, %12 ], [ %22, %21 ]
%20 = tail call double @lagrange_basis(i32 %18, double %0, double* %3, i32 %4)
br i1 %13, label %25, label %21
21: ; preds = %29, %17
%22 = phi double [ %19, %17 ], [ %30, %29 ]
%23 = add nuw nsw i32 %18, 1
%24 = icmp eq i32 %23, %4
br i1 %24, label %15, label %17
25: ; preds = %17, %29
%26 = phi i32 [ %31, %29 ], [ 0, %17 ]
%27 = phi double [ %30, %29 ], [ %19, %17 ]
%28 = tail call double @lagrange_basis(i32 %18, double %1, double* %5, i32 %6)
br i1 %14, label %33, label %29
29: ; preds = %33, %25
%30 = phi double [ %27, %25 ], [ %42, %33 ]
%31 = add nuw nsw i32 %26, 1
%32 = icmp eq i32 %31, %6
br i1 %32, label %21, label %25
33: ; preds = %25, %33
%34 = phi i32 [ %43, %33 ], [ 0, %25 ]
%35 = phi double [ %42, %33 ], [ %27, %25 ]
%36 = tail call i64 @index_3D_to_1D(i32 %18, i32 %4, i32 %26, i32 %6, i32 %34, i32 %8)
%37 = getelementptr inbounds double, double* %9, i64 %36
%38 = load double, double* %37
%39 = tail call double @lagrange_basis(i32 %18, double %2, double* %7, i32 %8)
%40 = fmul double %20, %38
%41 = fmul double %28, %40
%42 = tail call double @llvm.fmuladd.f64(double %41, double %39, double %35)
%43 = add nuw nsw i32 %34, 1
%44 = icmp eq i32 %43, %8
br i1 %44, label %29, label %33
}