#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 */
float bilinear_interpolate(float, float, float *, int, int);
int /*<<< orphan*/ printf(char *, float, ...);
int main(int argc, char **argv) {
  int dim = 4;
  float v_x[] = {1, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0};
  float v_y[] = {1, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0};
  float x = -1.5, y = 1.5;
  printf(
      "testing wrap case @ (%.2f, %.2f) in the x grid, expect x wrap to 3.5\n",
      x, y);
  float v = bilinear_interpolate(x, y, v_x, dim, dim + 1);
  printf("interpolated v: %.2f\n", v);
  x = -2.5 - dim - 1;
  printf(
      "\ntesting wrap case @ (%.2f, %.2f) in the x grid expect x wrap to 2.5\n",
      x, y);
  v = bilinear_interpolate(x, y, v_x, dim, dim + 1);
  printf("interpolated v: %.2f\n", v);
  x = 5.5;
  y = 0;
  printf("\ntesting wrap case @ (%.2f, %.2f) in x grid expect x wrap to 0.5\n",
         x, y);
  v = bilinear_interpolate(x, y, v_x, dim, dim + 1);
  printf("ninterpolated v: %.2f\n", v);
  x = 6.5 + dim + 1;
  printf(
      "\ntesting wrap case @ (%.2f, %.2f) in the x grid expect x wrap to 1.5\n",
      x, y);
  v = bilinear_interpolate(x, y, v_x, dim, dim + 1);
  printf("interpolated v: %.2f\n", v);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x4,-0x14(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
movss  0xebc(%rip),%xmm0
movss  %xmm0,-0x70(%rbp)
movss  0xeb3(%rip),%xmm0
movss  %xmm0,-0x68(%rbp)
movss  0xea2(%rip),%xmm0
movss  %xmm0,-0x50(%rbp)
movss  0xe95(%rip),%xmm0
movss  %xmm0,-0x38(%rbp)
movss  0xe88(%rip),%xmm0
movss  %xmm0,-0x34(%rbp)
lea    -0xc0(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1050 <memset@plt>
movss  0xe68(%rip),%xmm0
movss  %xmm0,-0xc0(%rbp)
ff
movss  0xe5c(%rip),%xmm0
movss  %xmm0,-0xb8(%rbp)
ff
movss  0xe48(%rip),%xmm0
movss  %xmm0,-0xa4(%rbp)
ff
movss  0xe38(%rip),%xmm0
movss  %xmm0,-0x90(%rbp)
ff
movss  0xe24(%rip),%xmm0
movss  %xmm0,-0xc4(%rbp)
ff
movss  0xe10(%rip),%xmm0
movss  %xmm0,-0xc8(%rbp)
ff
movss  -0xc4(%rbp),%xmm0
ff
movss  -0xc8(%rbp),%xmm1
ff
cvtss2sd %xmm1,%xmm1
lea    0xdfd(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
movss  -0xc4(%rbp),%xmm0
ff
movss  -0xc8(%rbp),%xmm1
ff
lea    -0x70(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1030 <bilinear_interpolate@plt>
movss  %xmm0,-0xcc(%rbp)
ff
movss  -0xcc(%rbp),%xmm0
ff
lea    0xe8c(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
cvtsi2sdl -0x14(%rbp),%xmm1
movsd  0xd8d(%rip),%xmm0
subsd  %xmm1,%xmm0
movsd  0xd71(%rip),%xmm1
subsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0xc4(%rbp)
ff
movss  -0xc4(%rbp),%xmm0
ff
movss  -0xc8(%rbp),%xmm1
ff
cvtss2sd %xmm1,%xmm1
lea    0xdb8(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
movss  -0xc4(%rbp),%xmm0
ff
movss  -0xc8(%rbp),%xmm1
ff
lea    -0x70(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1030 <bilinear_interpolate@plt>
movss  %xmm0,-0xcc(%rbp)
ff
movss  -0xcc(%rbp),%xmm0
ff
lea    0xe01(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
movss  0xd0f(%rip),%xmm0
movss  %xmm0,-0xc4(%rbp)
ff
xorps  %xmm0,%xmm0
movss  %xmm0,-0xc8(%rbp)
ff
movss  -0xc4(%rbp),%xmm0
ff
movss  -0xc8(%rbp),%xmm1
ff
cvtss2sd %xmm1,%xmm1
lea    0xd81(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
movss  -0xc4(%rbp),%xmm0
ff
movss  -0xc8(%rbp),%xmm1
ff
lea    -0x70(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1030 <bilinear_interpolate@plt>
movss  %xmm0,-0xcc(%rbp)
ff
movss  -0xcc(%rbp),%xmm0
ff
lea    0xd83(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
cvtsi2sdl -0x14(%rbp),%xmm1
movsd  0xc7d(%rip),%xmm0
addsd  %xmm1,%xmm0
movsd  0xc69(%rip),%xmm1
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0xc4(%rbp)
ff
movss  -0xc4(%rbp),%xmm0
ff
movss  -0xc8(%rbp),%xmm1
ff
cvtss2sd %xmm1,%xmm1
lea    0xd4f(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
movss  -0xc4(%rbp),%xmm0
ff
movss  -0xc8(%rbp),%xmm1
ff
lea    -0x70(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1030 <bilinear_interpolate@plt>
movss  %xmm0,-0xcc(%rbp)
ff
movss  -0xcc(%rbp),%xmm0
ff
lea    0xcf9(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x58,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x3f800000,(%rsp)
movl   $0x40000000,0x8(%rsp)
40
movl   $0x3f800000,0x20(%rsp)
3f
movabs $0x3f8000003f800000,%rax
mov    %rax,0x38(%rsp)
lea    0xeb1(%rip),%rdi
movss  0xe89(%rip),%xmm0
movsd  0xe99(%rip),%xmm1
mov    $0x2,%al
call   1040 <printf@plt>
movss  0xe76(%rip),%xmm1
mov    %rsp,%rbx
movss  0xe67(%rip),%xmm0
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x5,%edx
call   1030 <bilinear_interpolate@plt>
lea    0xf3d(%rip),%r14
mov    %r14,%rdi
mov    $0x1,%al
call   1040 <printf@plt>
lea    0xea3(%rip),%rdi
movss  0xe3d(%rip),%xmm0
movsd  0xe45(%rip),%xmm1
mov    $0x2,%al
call   1040 <printf@plt>
movss  0xe26(%rip),%xmm0
movss  0xe1a(%rip),%xmm1
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x5,%edx
call   1030 <bilinear_interpolate@plt>
mov    %r14,%rdi
mov    $0x1,%al
call   1040 <printf@plt>
lea    0xe9f(%rip),%rdi
movss  0xdf7(%rip),%xmm0
xorps  %xmm1,%xmm1
mov    $0x2,%al
call   1040 <printf@plt>
xorps  %xmm1,%xmm1
movss  0xde2(%rip),%xmm0
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x5,%edx
call   1030 <bilinear_interpolate@plt>
lea    0xeab(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
lea    0xeb4(%rip),%rdi
movss  0xdb7(%rip),%xmm0
xorps  %xmm1,%xmm1
mov    $0x2,%al
call   1040 <printf@plt>
xorps  %xmm1,%xmm1
movss  0xda2(%rip),%xmm0
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x5,%edx
call   1030 <bilinear_interpolate@plt>
mov    %r14,%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [70 x i8] c"testing wrap case @ (%.2f, %.2f) in the x grid, expect x wrap to 3.5\0A\00"
@.str.1 = [22 x i8] c"interpolated v: %.2f\0A\00"
@.str.2 = [70 x i8] c"\0Atesting wrap case @ (%.2f, %.2f) in the x grid expect x wrap to 2.5\0A\00"
@.str.3 = [66 x i8] c"\0Atesting wrap case @ (%.2f, %.2f) in x grid expect x wrap to 0.5\0A\00"
@.str.4 = [23 x i8] c"ninterpolated v: %.2f\0A\00"
@.str.5 = [70 x i8] c"\0Atesting wrap case @ (%.2f, %.2f) in the x grid expect x wrap to 1.5\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [20 x float]
%8 = alloca [20 x float]
%9 = alloca float
%10 = alloca float
%11 = alloca float
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 4, i32* %6
%12 = bitcast [20 x float]* %7 to i8*
%13 = bitcast i8* %12 to [20 x float]*
%14 = getelementptr inbounds [20 x float], [20 x float]* %13, i32 0, i32 0
store float 1.000000e+00, float* %14
%15 = getelementptr inbounds [20 x float], [20 x float]* %13, i32 0, i32 2
store float 2.000000e+00, float* %15
%16 = getelementptr inbounds [20 x float], [20 x float]* %13, i32 0, i32 8
store float 1.000000e+00, float* %16
%17 = getelementptr inbounds [20 x float], [20 x float]* %13, i32 0, i32 14
store float 1.000000e+00, float* %17
%18 = getelementptr inbounds [20 x float], [20 x float]* %13, i32 0, i32 15
store float 1.000000e+00, float* %18
%19 = bitcast [20 x float]* %8 to i8*
%20 = bitcast i8* %19 to [20 x float]*
%21 = getelementptr inbounds [20 x float], [20 x float]* %20, i32 0, i32 0
store float 1.000000e+00, float* %21
%22 = getelementptr inbounds [20 x float], [20 x float]* %20, i32 0, i32 2
store float 2.000000e+00, float* %22
%23 = getelementptr inbounds [20 x float], [20 x float]* %20, i32 0, i32 7
store float 1.000000e+00, float* %23
%24 = getelementptr inbounds [20 x float], [20 x float]* %20, i32 0, i32 12
store float 1.000000e+00, float* %24
store float -1.500000e+00, float* %9
store float 1.500000e+00, float* %10
%25 = load float, float* %9
%26 = load float, float* %10
%27 = fpext float %26 to double
%28 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str, i64 0, i64 0), float %25, double %27)
%29 = load float, float* %9
%30 = load float, float* %10
%31 = getelementptr inbounds [20 x float], [20 x float]* %7, i64 0, i64 0
%32 = load i32, i32* %6
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
%35 = call float @bilinear_interpolate(float %29, float %30, float* %31, i32 %32, i32 %34)
store float %35, float* %11
%36 = load float, float* %11
%37 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), float %36)
%38 = load i32, i32* %6
%39 = sitofp i32 %38 to double
%40 = fsub double -2.500000e+00, %39
%41 = fsub double %40, 1.000000e+00
%42 = fptrunc double %41 to float
store float %42, float* %9
%43 = load float, float* %9
%44 = load float, float* %10
%45 = fpext float %44 to double
%46 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.2, i64 0, i64 0), float %43, double %45)
%47 = load float, float* %9
%48 = load float, float* %10
%49 = getelementptr inbounds [20 x float], [20 x float]* %7, i64 0, i64 0
%50 = load i32, i32* %6
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 1
%53 = call float @bilinear_interpolate(float %47, float %48, float* %49, i32 %50, i32 %52)
store float %53, float* %11
%54 = load float, float* %11
%55 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), float %54)
store float 5.500000e+00, float* %9
store float 0.000000e+00, float* %10
%56 = load float, float* %9
%57 = load float, float* %10
%58 = fpext float %57 to double
%59 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.3, i64 0, i64 0), float %56, double %58)
%60 = load float, float* %9
%61 = load float, float* %10
%62 = getelementptr inbounds [20 x float], [20 x float]* %7, i64 0, i64 0
%63 = load i32, i32* %6
%64 = load i32, i32* %6
%65 = add nsw i32 %64, 1
%66 = call float @bilinear_interpolate(float %60, float %61, float* %62, i32 %63, i32 %65)
store float %66, float* %11
%67 = load float, float* %11
%68 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), float %67)
%69 = load i32, i32* %6
%70 = sitofp i32 %69 to double
%71 = fadd double 6.500000e+00, %70
%72 = fadd double %71, 1.000000e+00
%73 = fptrunc double %72 to float
store float %73, float* %9
%74 = load float, float* %9
%75 = load float, float* %10
%76 = fpext float %75 to double
%77 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.5, i64 0, i64 0), float %74, double %76)
%78 = load float, float* %9
%79 = load float, float* %10
%80 = getelementptr inbounds [20 x float], [20 x float]* %7, i64 0, i64 0
%81 = load i32, i32* %6
%82 = load i32, i32* %6
%83 = add nsw i32 %82, 1
%84 = call float @bilinear_interpolate(float %78, float %79, float* %80, i32 %81, i32 %83)
store float %84, float* %11
%85 = load float, float* %11
%86 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), float %85)
ret i32 0
}

LLVM IR (-O2):
@.str = [70 x i8] c"testing wrap case @ (%.2f, %.2f) in the x grid, expect x wrap to 3.5\0A\00"
@.str.1 = [22 x i8] c"interpolated v: %.2f\0A\00"
@.str.2 = [70 x i8] c"\0Atesting wrap case @ (%.2f, %.2f) in the x grid expect x wrap to 2.5\0A\00"
@.str.3 = [66 x i8] c"\0Atesting wrap case @ (%.2f, %.2f) in x grid expect x wrap to 0.5\0A\00"
@.str.4 = [23 x i8] c"ninterpolated v: %.2f\0A\00"
@.str.5 = [70 x i8] c"\0Atesting wrap case @ (%.2f, %.2f) in the x grid expect x wrap to 1.5\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [20 x float]
%4 = bitcast [20 x float]* %3 to i8*
%5 = getelementptr inbounds [20 x float], [20 x float]* %3, i64 0, i64 0
store float 1.000000e+00, float* %5
%6 = getelementptr inbounds [20 x float], [20 x float]* %3, i64 0, i64 2
store float 2.000000e+00, float* %6
%7 = getelementptr inbounds [20 x float], [20 x float]* %3, i64 0, i64 8
store float 1.000000e+00, float* %7
%8 = getelementptr inbounds [20 x float], [20 x float]* %3, i64 0, i64 14
store float 1.000000e+00, float* %8
%9 = getelementptr inbounds [20 x float], [20 x float]* %3, i64 0, i64 15
store float 1.000000e+00, float* %9
%10 = tail call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str, i64 0, i64 0), float -1.500000e+00, double 1.500000e+00)
%11 = call float @bilinear_interpolate(float -1.500000e+00, float 1.500000e+00, float* %5, i32 4, i32 5)
%12 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), float %11)
%13 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.2, i64 0, i64 0), float -7.500000e+00, double 1.500000e+00)
%14 = call float @bilinear_interpolate(float -7.500000e+00, float 1.500000e+00, float* %5, i32 4, i32 5)
%15 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), float %14)
%16 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.3, i64 0, i64 0), float 5.500000e+00, double 0.000000e+00)
%17 = call float @bilinear_interpolate(float 5.500000e+00, float 0.000000e+00, float* %5, i32 4, i32 5)
%18 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), float %17)
%19 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.5, i64 0, i64 0), float 1.150000e+01, double 0.000000e+00)
%20 = call float @bilinear_interpolate(float 1.150000e+01, float 0.000000e+00, float* %5, i32 4, i32 5)
%21 = call i32 (i8*, float, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), float %20)
ret i32 0
}