#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 */
double const lagrange_basis(int, double, double const *, int);
double lagrange_interp_1D(double x, const double x_values[], int num_x,
                          const double y_values[]) {
  double out = 0;
  for (int j = 0; j < num_x; j++) {
    out += y_values[j] * lagrange_basis(j, x, x_values, num_x);
  }
  return out;
}


x86 assembly (clang, -O0):
<lagrange_interp_1D>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1191 <lagrange_interp_1D+0x81>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
movsd  -0x8(%rbp),%xmm0
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <lagrange_basis@plt>
movaps %xmm0,%xmm2
movsd  -0x38(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1137 <lagrange_interp_1D+0x27>
movsd  -0x28(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lagrange_interp_1D>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %esi,%esi
jle    117f <lagrange_interp_1D+0x6f>
mov    %rdx,%r14
mov    %rdi,%r15
mov    %esi,%r12d
xorpd  %xmm1,%xmm1
xor    %ebx,%ebx
movsd  %xmm0,(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  %xmm1,0x10(%rsp)
movsd  (%r14,%rbx,8),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %ebx,%edi
movsd  (%rsp),%xmm0
mov    %r15,%rsi
mov    %r12d,%edx
call   1030 <lagrange_basis@plt>
movsd  0x10(%rsp),%xmm1
mulsd  0x8(%rsp),%xmm0
addsd  %xmm0,%xmm1
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1140 <lagrange_interp_1D+0x30>
jmp    1183 <lagrange_interp_1D+0x73>
xorpd  %xmm1,%xmm1
movapd %xmm1,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @lagrange_interp_1D(double %0, double* %1, i32 %2, double* %3) {
%5 = alloca double
%6 = alloca double*
%7 = alloca i32
%8 = alloca double*
%9 = alloca double
%10 = alloca i32
store double %0, double* %5
store double* %1, double** %6
store i32 %2, i32* %7
store double* %3, double** %8
store double 0.000000e+00, double* %9
store i32 0, i32* %10
br label %11
11: ; preds = %28, %4
%12 = load i32, i32* %10
%13 = load i32, i32* %7
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %31
15: ; preds = %11
%16 = load double*, double** %8
%17 = load i32, i32* %10
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds double, double* %16, i64 %18
%20 = load double, double* %19
%21 = load i32, i32* %10
%22 = load double, double* %5
%23 = load double*, double** %6
%24 = load i32, i32* %7
%25 = call double @lagrange_basis(i32 %21, double %22, double* %23, i32 %24)
%26 = load double, double* %9
%27 = call double @llvm.fmuladd.f64(double %20, double %25, double %26)
store double %27, double* %9
br label %28
28: ; preds = %15
%29 = load i32, i32* %10
%30 = add nsw i32 %29, 1
store i32 %30, i32* %10
br label %11
31: ; preds = %11
%32 = load double, double* %9
ret double %32
}

LLVM IR (-O2):
define double @lagrange_interp_1D(double %0, double* %1, i32 %2, double* nocapture %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %8
6: ; preds = %4
%7 = zext i32 %2 to i64
br label %10
8: ; preds = %10, %4
%9 = phi double [ 0.000000e+00, %4 ], [ %17, %10 ]
ret double %9
10: ; preds = %6, %10
%11 = phi i64 [ 0, %6 ], [ %18, %10 ]
%12 = phi double [ 0.000000e+00, %6 ], [ %17, %10 ]
%13 = getelementptr inbounds double, double* %3, i64 %11
%14 = load double, double* %13
%15 = trunc i64 %11 to i32
%16 = tail call double @lagrange_basis(i32 %15, double %0, double* %1, i32 %2)
%17 = tail call double @llvm.fmuladd.f64(double %14, double %16, double %12)
%18 = add nuw nsw i64 %11, 1
%19 = icmp eq i64 %18, %7
br i1 %19, label %8, label %10
}