#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 round(float);
float nearest_neighbor_interpolator(float *x, int w, int h, float p, float q) {
  int ip = round(p);
  int iq = round(q);
  if (ip < 0)
    ip = 0;
  if (iq < 0)
    iq = 0;
  if (ip >= w)
    ip = w - 1;
  if (iq >= h)
    iq = h - 1;
  return x[w * iq + ip];
}


x86 assembly (clang, -O0):
<nearest_neighbor_interpolator>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movss  %xmm0,-0x14(%rbp)
movss  %xmm1,-0x18(%rbp)
movss  -0x14(%rbp),%xmm0
call   1030 <round@plt>
mov    %eax,-0x1c(%rbp)
movss  -0x18(%rbp),%xmm0
call   1030 <round@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1157 <nearest_neighbor_interpolator+0x47>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1168 <nearest_neighbor_interpolator+0x58>
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jl     117d <nearest_neighbor_interpolator+0x6d>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x10(%rbp),%eax
jl     1192 <nearest_neighbor_interpolator+0x82>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
imul   -0x20(%rbp),%ecx
add    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
movss  (%rax,%rcx,4),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nearest_neighbor_interpolator>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movss  %xmm1,0x4(%rsp)
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%r14
call   1030 <round@plt>
mov    %eax,%ebp
movss  0x4(%rsp),%xmm0
call   1030 <round@plt>
xor    %ecx,%ecx
test   %ebp,%ebp
cmovs  %ecx,%ebp
test   %eax,%eax
cmovns %eax,%ecx
lea    -0x1(%rbx),%eax
cmp    %ebx,%ebp
cmovl  %ebp,%eax
lea    -0x1(%r15),%edx
cmp    %r15d,%ecx
cmovl  %ecx,%edx
imul   %ebx,%edx
add    %eax,%edx
movslq %edx,%rax
movss  (%r14,%rax,4),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define float @nearest_neighbor_interpolator(float* %0, i32 %1, i32 %2, float %3, float %4) {
%6 = alloca float*
%7 = alloca i32
%8 = alloca i32
%9 = alloca float
%10 = alloca float
%11 = alloca i32
%12 = alloca i32
store float* %0, float** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store float %3, float* %9
store float %4, float* %10
%13 = load float, float* %9
%14 = call i32 @round(float %13)
store i32 %14, i32* %11
%15 = load float, float* %10
%16 = call i32 @round(float %15)
store i32 %16, i32* %12
%17 = load i32, i32* %11
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %5
store i32 0, i32* %11
br label %20
20: ; preds = %19, %5
%21 = load i32, i32* %12
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 0, i32* %12
br label %24
24: ; preds = %23, %20
%25 = load i32, i32* %11
%26 = load i32, i32* %7
%27 = icmp sge i32 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* %7
%30 = sub nsw i32 %29, 1
store i32 %30, i32* %11
br label %31
31: ; preds = %28, %24
%32 = load i32, i32* %12
%33 = load i32, i32* %8
%34 = icmp sge i32 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* %8
%37 = sub nsw i32 %36, 1
store i32 %37, i32* %12
br label %38
38: ; preds = %35, %31
%39 = load float*, float** %6
%40 = load i32, i32* %7
%41 = load i32, i32* %12
%42 = mul nsw i32 %40, %41
%43 = load i32, i32* %11
%44 = add nsw i32 %42, %43
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds float, float* %39, i64 %45
%47 = load float, float* %46
ret float %47
}

LLVM IR (-O2):
define float @nearest_neighbor_interpolator(float* nocapture %0, i32 %1, i32 %2, float %3, float %4) {
%6 = tail call i32 @round(float %3)
%7 = tail call i32 @round(float %4)
%8 = icmp sgt i32 %6, 0
%9 = select i1 %8, i32 %6, i32 0
%10 = icmp sgt i32 %7, 0
%11 = select i1 %10, i32 %7, i32 0
%12 = icmp slt i32 %9, %1
%13 = add nsw i32 %1, -1
%14 = select i1 %12, i32 %9, i32 %13
%15 = icmp slt i32 %11, %2
%16 = add nsw i32 %2, -1
%17 = select i1 %15, i32 %11, i32 %16
%18 = mul nsw i32 %17, %1
%19 = add nsw i32 %18, %14
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds float, float* %0, i64 %20
%22 = load float, float* %21
ret float %22
}