#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_2D_to_1D(int, int, int, int);
double lagrange_basis(int, double, double const *, int);
double lagrange_interp_2D(double x, double y, const double x_values[],
                          int num_x, const double y_values[], int num_y,
                          const double z_values[]) {
  double out = 0;
  double lx, ly, z;
  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(j, y, y_values, num_y);
      z = z_values[index_2D_to_1D(i, num_x, j, num_y)];
      out += z * lx * ly;
    }
  }
  return out;
}


x86 assembly (clang, -O0):
<lagrange_interp_2D>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
mov    %rdx,-0x28(%rbp)
mov    %ecx,-0x2c(%rbp)
mov    %r8,-0x38(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1210 <lagrange_interp_2D+0xf0>
mov    -0x5c(%rbp),%edi
movsd  -0x8(%rbp),%xmm0
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <lagrange_basis@plt>
movsd  %xmm0,-0x48(%rbp)
movl   $0x0,-0x60(%rbp)
mov    -0x60(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    11fd <lagrange_interp_2D+0xdd>
mov    -0x60(%rbp),%edi
movsd  -0x10(%rbp),%xmm0
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <lagrange_basis@plt>
movsd  %xmm0,-0x50(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x5c(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    -0x60(%rbp),%edx
mov    -0x2c(%rbp),%ecx
call   1040 <index_2D_to_1D@plt>
mov    %rax,%rcx
mov    -0x68(%rbp),%rax
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x58(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
movsd  -0x50(%rbp),%xmm2
movsd  -0x40(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x60(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60(%rbp)
jmp    117f <lagrange_interp_2D+0x5f>
jmp    1202 <lagrange_interp_2D+0xe2>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1153 <lagrange_interp_2D+0x33>
movsd  -0x40(%rbp),%xmm0
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lagrange_interp_2D>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,0x20(%rsp)
movsd  %xmm0,0x18(%rsp)
test   %esi,%esi
jle    121b <lagrange_interp_2D+0xfb>
mov    %r8,%r14
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %esi,%r13d
xorps  %xmm0,%xmm0
xor    %ebx,%ebx
movsd  %xmm1,0x10(%rsp)
jmp    1172 <lagrange_interp_2D+0x52>
nopl   0x0(%rax)
movsd  0x8(%rsp),%xmm0
add    $0x1,%ebx
cmp    %r13d,%ebx
je     121e <lagrange_interp_2D+0xfe>
movsd  %xmm0,0x8(%rsp)
mov    %ebx,%edi
movsd  0x18(%rsp),%xmm0
mov    0x20(%rsp),%rsi
mov    %r13d,%edx
call   1030 <lagrange_basis@plt>
test   %ebp,%ebp
jle    1160 <lagrange_interp_2D+0x40>
movaps %xmm0,%xmm3
xor    %r12d,%r12d
movsd  0x10(%rsp),%xmm1
movsd  0x8(%rsp),%xmm0
movsd  %xmm3,0x28(%rsp)
nopl   0x0(%rax)
movsd  %xmm0,0x8(%rsp)
mov    %r12d,%edi
movaps %xmm1,%xmm0
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <lagrange_basis@plt>
movsd  %xmm0,0x30(%rsp)
mov    %ebx,%edi
mov    %r13d,%esi
mov    %r12d,%edx
mov    %ebp,%ecx
call   1040 <index_2D_to_1D@plt>
movsd  0x28(%rsp),%xmm3
movsd  0x10(%rsp),%xmm1
movsd  (%r14,%rax,8),%xmm0
mulsd  %xmm3,%xmm0
mulsd  0x30(%rsp),%xmm0
movsd  0x8(%rsp),%xmm2
addsd  %xmm0,%xmm2
movsd  %xmm2,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
add    $0x1,%r12d
cmp    %r12d,%ebp
jne    11b0 <lagrange_interp_2D+0x90>
jmp    1166 <lagrange_interp_2D+0x46>
xorps  %xmm0,%xmm0
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @lagrange_interp_2D(double %0, double %1, double* %2, i32 %3, double* %4, i32 %5, double* %6) {
%8 = alloca double
%9 = alloca double
%10 = alloca double*
%11 = alloca i32
%12 = alloca double*
%13 = alloca i32
%14 = alloca double*
%15 = alloca double
%16 = alloca double
%17 = alloca double
%18 = alloca double
%19 = alloca i32
%20 = alloca i32
store double %0, double* %8
store double %1, double* %9
store double* %2, double** %10
store i32 %3, i32* %11
store double* %4, double** %12
store i32 %5, i32* %13
store double* %6, double** %14
store double 0.000000e+00, double* %15
store i32 0, i32* %19
br label %21
21: ; preds = %59, %7
%22 = load i32, i32* %19
%23 = load i32, i32* %11
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %62
25: ; preds = %21
%26 = load i32, i32* %19
%27 = load double, double* %8
%28 = load double*, double** %10
%29 = load i32, i32* %11
%30 = call double @lagrange_basis(i32 %26, double %27, double* %28, i32 %29)
store double %30, double* %16
store i32 0, i32* %20
br label %31
31: ; preds = %55, %25
%32 = load i32, i32* %20
%33 = load i32, i32* %13
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %58
35: ; preds = %31
%36 = load i32, i32* %20
%37 = load double, double* %9
%38 = load double*, double** %12
%39 = load i32, i32* %13
%40 = call double @lagrange_basis(i32 %36, double %37, double* %38, i32 %39)
store double %40, double* %17
%41 = load double*, double** %14
%42 = load i32, i32* %19
%43 = load i32, i32* %11
%44 = load i32, i32* %20
%45 = load i32, i32* %13
%46 = call i64 @index_2D_to_1D(i32 %42, i32 %43, i32 %44, i32 %45)
%47 = getelementptr inbounds double, double* %41, i64 %46
%48 = load double, double* %47
store double %48, double* %18
%49 = load double, double* %18
%50 = load double, double* %16
%51 = fmul double %49, %50
%52 = load double, double* %17
%53 = load double, double* %15
%54 = call double @llvm.fmuladd.f64(double %51, double %52, double %53)
store double %54, double* %15
br label %55
55: ; preds = %35
%56 = load i32, i32* %20
%57 = add nsw i32 %56, 1
store i32 %57, i32* %20
br label %31
58: ; preds = %31
br label %59
59: ; preds = %58
%60 = load i32, i32* %19
%61 = add nsw i32 %60, 1
store i32 %61, i32* %19
br label %21
62: ; preds = %21
%63 = load double, double* %15
ret double %63
}

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