#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 */
typedef size_t tmpv;
typedef scalar_t__ *dimx;

/* Variables and functions */
double UCHAR_MAX;
double _cubic(double);
unsigned char p3dBicubicInterpolation_8(unsigned char *in_im, int dimx,
                                        int dimy, double x0, double y0) {
  int u0 = (int)x0;
  int v0 = (int)y0;
  int i, j;
  int v;
  double p;
  int u, tmpu, tmpv;
  double q = 0;
  for (j = -1; j <= 2; j++) {
    v = v0 + j;
    p = 0;
    for (i = -1; i <= 2; i++) {
      u = u0 + i;
      tmpu = u;
      tmpv = v;
      if (u < 0)
        tmpu = 0;
      if (u > ((int)dimx - 1))
        tmpu = (int)dimx - 1;
      if (v < 0)
        tmpv = 0;
      if (v > ((int)dimy - 1))
        tmpv = (int)dimy - 1;
      p = p + in_im[((tmpv) * (dimx) + (tmpu))] * _cubic(x0 - u);
    }
    q = q + p * _cubic(y0 - v);
  }
  q = q + 0.5;
  if (q < 0.0)
    q = 0.0;
  if (q >= UCHAR_MAX)
    q = UCHAR_MAX;
  return (unsigned char)q;
}


x86 assembly (clang, -O0):
<p3dBicubicInterpolation_8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
movsd  %xmm1,-0x20(%rbp)
cvttsd2si -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
cvttsd2si -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x58(%rbp)
movl   $0xffffffff,-0x30(%rbp)
cmpl   $0x2,-0x30(%rbp)
jg     1297 <p3dBicubicInterpolation_8+0x187>
mov    -0x28(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
cmpl   $0x2,-0x2c(%rbp)
jg     1248 <p3dBicubicInterpolation_8+0x138>
mov    -0x24(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11a0 <p3dBicubicInterpolation_8+0x90>
movl   $0x0,-0x48(%rbp)
mov    -0x44(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    11ba <p3dBicubicInterpolation_8+0xaa>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11cb <p3dBicubicInterpolation_8+0xbb>
movl   $0x0,-0x4c(%rbp)
mov    -0x34(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jle    11e5 <p3dBicubicInterpolation_8+0xd5>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4c(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x4c(%rbp),%ecx
imul   -0xc(%rbp),%ecx
add    -0x48(%rbp),%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x18(%rbp),%xmm0
cvtsi2sdl -0x44(%rbp),%xmm1
subsd  %xmm1,%xmm0
call   1030 <_cubic@plt>
movsd  -0x68(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x60(%rbp),%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1170 <p3dBicubicInterpolation_8+0x60>
movsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x78(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x70(%rbp)
movsd  -0x20(%rbp),%xmm0
cvtsi2sdl -0x34(%rbp),%xmm1
subsd  %xmm1,%xmm0
call   1030 <_cubic@plt>
movsd  -0x78(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x70(%rbp),%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    114e <p3dBicubicInterpolation_8+0x3e>
movsd  0xd61(%rip),%xmm0
addsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x58(%rbp),%xmm0
jbe    12bf <p3dBicubicInterpolation_8+0x1af>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x58(%rbp),%xmm0
mov    0x2d25(%rip),%rax
ucomisd (%rax),%xmm0
jb     12e5 <p3dBicubicInterpolation_8+0x1d5>
mov    0x2d14(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x58(%rbp)
cvttsd2si -0x58(%rbp),%eax
movzbl %al,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<p3dBicubicInterpolation_8>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rdi,%rbx
cvttsd2si %xmm0,%eax
movsd  %xmm1,0x40(%rsp)
cvttsd2si %xmm1,%ecx
mov    %ecx,0x1c(%rsp)
lea    -0x1(%rsi),%r8d
xor    %r12d,%r12d
mov    %eax,%r9d
add    $0xffffffff,%r9d
mov    $0x0,%ecx
cmovns %r9d,%ecx
test   %eax,%eax
mov    $0x0,%ebp
cmovns %eax,%ebp
mov    %rdx,%rdi
mov    %rdx,0x38(%rsp)
add    $0xffffffff,%edx
mov    %edx,0x18(%rsp)
cmp    %esi,%eax
cmovg  %r8d,%ecx
mov    %rcx,0x28(%rsp)
cvtsi2sd %r9d,%xmm2
movapd %xmm0,%xmm3
cmovge %r8d,%ebp
mov    %rbp,0x20(%rsp)
cvttpd2dq %xmm0,%xmm1
cvtdq2pd %xmm1,%xmm1
mov    %eax,%edx
add    $0x1,%edx
mov    $0x0,%r14d
cmovns %edx,%r14d
subsd  %xmm2,%xmm3
movapd %xmm3,0x70(%rsp)
movapd %xmm0,%xmm2
cmp    %esi,%edx
cmovge %r8d,%r14d
subsd  %xmm1,%xmm2
movapd %xmm2,0x60(%rsp)
xorps  %xmm1,%xmm1
cvtsi2sd %edx,%xmm1
movapd %xmm0,%xmm2
subsd  %xmm1,%xmm2
movapd %xmm2,0x50(%rsp)
add    $0x2,%eax
cmovns %eax,%r12d
mov    %rsi,0x30(%rsp)
cmp    %esi,%eax
cmovge %r8d,%r12d
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
subsd  %xmm1,%xmm0
movapd %xmm0,0x80(%rsp)
mov    $0xffffffff,%r15d
xorpd  %xmm1,%xmm1
nopl   (%rax)
movsd  %xmm1,0x48(%rsp)
mov    0x1c(%rsp),%r13d
add    %r15d,%r13d
mov    $0x0,%ebp
cmovns %r13d,%ebp
cmp    0x38(%rsp),%r13d
cmovge 0x18(%rsp),%ebp
imul   0x30(%rsp),%ebp
mov    0x28(%rsp),%rax
add    %ebp,%eax
cltq
movzbl (%rbx,%rax,1),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x8(%rsp)
movapd 0x70(%rsp),%xmm0
call   1030 <_cubic@plt>
mulsd  0x8(%rsp),%xmm0
addsd  0xdaf(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    0x20(%rsp),%rax
add    %ebp,%eax
cltq
movzbl (%rbx,%rax,1),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x10(%rsp)
movapd 0x60(%rsp),%xmm0
call   1030 <_cubic@plt>
mulsd  0x10(%rsp),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
lea    (%r14,%rbp,1),%eax
cltq
movzbl (%rbx,%rax,1),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x10(%rsp)
movapd 0x50(%rsp),%xmm0
call   1030 <_cubic@plt>
mulsd  0x10(%rsp),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
add    %r12d,%ebp
movslq %ebp,%rax
movzbl (%rbx,%rax,1),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x10(%rsp)
movapd 0x80(%rsp),%xmm0
call   1030 <_cubic@plt>
mulsd  0x10(%rsp),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
xorps  %xmm1,%xmm1
cvtsi2sd %r13d,%xmm1
movsd  0x40(%rsp),%xmm0
subsd  %xmm1,%xmm0
call   1030 <_cubic@plt>
movsd  0x48(%rsp),%xmm1
mulsd  0x8(%rsp),%xmm0
addsd  %xmm0,%xmm1
add    $0x1,%r15d
cmp    $0x3,%r15d
jne    1200 <p3dBicubicInterpolation_8+0xf0>
addsd  0xcc2(%rip),%xmm1
xorpd  %xmm0,%xmm0
maxsd  %xmm1,%xmm0
mov    0x2ca3(%rip),%rax
movsd  (%rax),%xmm1
movapd %xmm1,%xmm2
cmplesd %xmm0,%xmm2
movapd %xmm2,%xmm3
andnpd %xmm0,%xmm3
andpd  %xmm1,%xmm2
orpd   %xmm3,%xmm2
cvttsd2si %xmm2,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@UCHAR_MAX = global double 0.000000e+00
define zeroext i8 @p3dBicubicInterpolation_8(i8* %0, i32 %1, i32 %2, double %3, double %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca double
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca double
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store double %3, double* %9
store double %4, double* %10
%21 = load double, double* %9
%22 = fptosi double %21 to i32
store i32 %22, i32* %11
%23 = load double, double* %10
%24 = fptosi double %23 to i32
store i32 %24, i32* %12
store double 0.000000e+00, double* %20
store i32 -1, i32* %14
br label %25
25: ; preds = %95, %5
%26 = load i32, i32* %14
%27 = icmp sle i32 %26, 2
br i1 %27, label %28, label %98
28: ; preds = %25
%29 = load i32, i32* %12
%30 = load i32, i32* %14
%31 = add nsw i32 %29, %30
store i32 %31, i32* %15
store double 0.000000e+00, double* %16
store i32 -1, i32* %13
br label %32
32: ; preds = %83, %28
%33 = load i32, i32* %13
%34 = icmp sle i32 %33, 2
br i1 %34, label %35, label %86
35: ; preds = %32
%36 = load i32, i32* %11
%37 = load i32, i32* %13
%38 = add nsw i32 %36, %37
store i32 %38, i32* %17
%39 = load i32, i32* %17
store i32 %39, i32* %18
%40 = load i32, i32* %15
store i32 %40, i32* %19
%41 = load i32, i32* %17
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %35
store i32 0, i32* %18
br label %44
44: ; preds = %43, %35
%45 = load i32, i32* %17
%46 = load i32, i32* %7
%47 = sub nsw i32 %46, 1
%48 = icmp sgt i32 %45, %47
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = load i32, i32* %7
%51 = sub nsw i32 %50, 1
store i32 %51, i32* %18
br label %52
52: ; preds = %49, %44
%53 = load i32, i32* %15
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %52
store i32 0, i32* %19
br label %56
56: ; preds = %55, %52
%57 = load i32, i32* %15
%58 = load i32, i32* %8
%59 = sub nsw i32 %58, 1
%60 = icmp sgt i32 %57, %59
br i1 %60, label %61, label %64
61: ; preds = %56
%62 = load i32, i32* %8
%63 = sub nsw i32 %62, 1
store i32 %63, i32* %19
br label %64
64: ; preds = %61, %56
%65 = load double, double* %16
%66 = load i8*, i8** %6
%67 = load i32, i32* %19
%68 = load i32, i32* %7
%69 = mul nsw i32 %67, %68
%70 = load i32, i32* %18
%71 = add nsw i32 %69, %70
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %66, i64 %72
%74 = load i8, i8* %73
%75 = zext i8 %74 to i32
%76 = sitofp i32 %75 to double
%77 = load double, double* %9
%78 = load i32, i32* %17
%79 = sitofp i32 %78 to double
%80 = fsub double %77, %79
%81 = call double @_cubic(double %80)
%82 = call double @llvm.fmuladd.f64(double %76, double %81, double %65)
store double %82, double* %16
br label %83
83: ; preds = %64
%84 = load i32, i32* %13
%85 = add nsw i32 %84, 1
store i32 %85, i32* %13
br label %32
86: ; preds = %32
%87 = load double, double* %20
%88 = load double, double* %16
%89 = load double, double* %10
%90 = load i32, i32* %15
%91 = sitofp i32 %90 to double
%92 = fsub double %89, %91
%93 = call double @_cubic(double %92)
%94 = call double @llvm.fmuladd.f64(double %88, double %93, double %87)
store double %94, double* %20
br label %95
95: ; preds = %86
%96 = load i32, i32* %14
%97 = add nsw i32 %96, 1
store i32 %97, i32* %14
br label %25
98: ; preds = %25
%99 = load double, double* %20
%100 = fadd double %99, 5.000000e-01
store double %100, double* %20
%101 = load double, double* %20
%102 = fcmp olt double %101, 0.000000e+00
br i1 %102, label %103, label %104
103: ; preds = %98
store double 0.000000e+00, double* %20
br label %104
104: ; preds = %103, %98
%105 = load double, double* %20
%106 = load double, double* @UCHAR_MAX
%107 = fcmp oge double %105, %106
br i1 %107, label %108, label %110
108: ; preds = %104
%109 = load double, double* @UCHAR_MAX
store double %109, double* %20
br label %110
110: ; preds = %108, %104
%111 = load double, double* %20
%112 = fptoui double %111 to i8
ret i8 %112
}

LLVM IR (-O2):
@UCHAR_MAX = global double 0.000000e+00
define zeroext i8 @p3dBicubicInterpolation_8(i8* nocapture %0, i32 %1, i32 %2, double %3, double %4) {
%6 = fptosi double %3 to i32
%7 = fptosi double %4 to i32
%8 = add nsw i32 %1, -1
%9 = add nsw i32 %2, -1
%10 = add nsw i32 %6, -1
%11 = icmp sgt i32 %10, 0
%12 = select i1 %11, i32 %10, i32 0
%13 = icmp sgt i32 %6, %1
%14 = select i1 %13, i32 %8, i32 %12
%15 = sitofp i32 %10 to double
%16 = fsub double %3, %15
%17 = icmp sgt i32 %6, 0
%18 = select i1 %17, i32 %6, i32 0
%19 = icmp slt i32 %6, %1
%20 = select i1 %19, i32 %18, i32 %8
%21 = sitofp i32 %6 to double
%22 = fsub double %3, %21
%23 = add nsw i32 %6, 1
%24 = icmp sgt i32 %23, 0
%25 = select i1 %24, i32 %23, i32 0
%26 = icmp slt i32 %23, %1
%27 = select i1 %26, i32 %25, i32 %8
%28 = sitofp i32 %23 to double
%29 = fsub double %3, %28
%30 = add nsw i32 %6, 2
%31 = icmp sgt i32 %30, 0
%32 = select i1 %31, i32 %30, i32 0
%33 = icmp slt i32 %30, %1
%34 = select i1 %33, i32 %32, i32 %8
%35 = sitofp i32 %30 to double
%36 = fsub double %3, %35
br label %37
37: ; preds = %5, %37
%38 = phi double [ 0.000000e+00, %5 ], [ %77, %37 ]
%39 = phi i32 [ -1, %5 ], [ %78, %37 ]
%40 = add nsw i32 %39, %7
%41 = icmp sgt i32 %40, 0
%42 = select i1 %41, i32 %40, i32 0
%43 = icmp slt i32 %40, %2
%44 = select i1 %43, i32 %42, i32 %9
%45 = mul nsw i32 %44, %1
%46 = add nsw i32 %14, %45
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %0, i64 %47
%49 = load i8, i8* %48
%50 = uitofp i8 %49 to double
%51 = tail call double @_cubic(double %16)
%52 = tail call double @llvm.fmuladd.f64(double %50, double %51, double 0.000000e+00)
%53 = add nsw i32 %20, %45
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %0, i64 %54
%56 = load i8, i8* %55
%57 = uitofp i8 %56 to double
%58 = tail call double @_cubic(double %22)
%59 = tail call double @llvm.fmuladd.f64(double %57, double %58, double %52)
%60 = add nsw i32 %27, %45
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %0, i64 %61
%63 = load i8, i8* %62
%64 = uitofp i8 %63 to double
%65 = tail call double @_cubic(double %29)
%66 = tail call double @llvm.fmuladd.f64(double %64, double %65, double %59)
%67 = add nsw i32 %34, %45
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %0, i64 %68
%70 = load i8, i8* %69
%71 = uitofp i8 %70 to double
%72 = tail call double @_cubic(double %36)
%73 = tail call double @llvm.fmuladd.f64(double %71, double %72, double %66)
%74 = sitofp i32 %40 to double
%75 = fsub double %4, %74
%76 = tail call double @_cubic(double %75)
%77 = tail call double @llvm.fmuladd.f64(double %73, double %76, double %38)
%78 = add nsw i32 %39, 1
%79 = icmp eq i32 %78, 3
br i1 %79, label %80, label %37
80: ; preds = %37
%81 = fadd double %77, 5.000000e-01
%82 = fcmp olt double %81, 0.000000e+00
%83 = select i1 %82, double 0.000000e+00, double %81
%84 = load double, double* @UCHAR_MAX
%85 = fcmp oge double %83, %84
%86 = select i1 %85, double %84, double %83
%87 = fptoui double %86 to i8
ret i8 %87
}