#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 INT_MAX;
int INT_MIN;
int ourint(double x) {
  int result;
  double fraction;
  if (x > ((double)INT_MAX) - 0.5)
    return INT_MAX;
  if (x < ((double)INT_MIN) + 0.5)
    return INT_MIN;
  result = x;
  fraction = x - result;
  if (fraction < 0) {
    if (fraction == -0.5)
      return (result & ~0x01);
    if (fraction > -0.5) {
      return result;
    } else {
      return (result - 1);
    }
  } else {
    if (fraction == 0.5)
      return ((result + 1) & ~0x01);
    if (fraction < 0.5) {
      return result;
    } else {
      return (result + 1);
    }
  }
}


x86 assembly (clang, -O0):
<ourint>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
mov    0x2ed3(%rip),%rax
cvtsi2sdl (%rax),%xmm1
movsd  0xedf(%rip),%xmm2
subsd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
jbe    1140 <ourint+0x40>
mov    0x2eb2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <ourint+0x150>
movsd  -0x10(%rbp),%xmm1
mov    0x2e94(%rip),%rax
cvtsi2sdl (%rax),%xmm0
movsd  0xea8(%rip),%xmm2
addsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
jbe    1177 <ourint+0x77>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <ourint+0x150>
cvttsd2si -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
movsd  -0x10(%rbp),%xmm0
cvtsi2sdl -0x14(%rbp),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x20(%rbp),%xmm0
jbe    11fb <ourint+0xfb>
movsd  -0x20(%rbp),%xmm0
movsd  0xe5b(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    11cb <ourint+0xcb>
jp     11cb <ourint+0xcb>
mov    -0x14(%rbp),%eax
and    $0xfffffffe,%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <ourint+0x150>
movsd  -0x20(%rbp),%xmm0
movsd  0xe30(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11ed <ourint+0xed>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <ourint+0x150>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <ourint+0x150>
movsd  -0x20(%rbp),%xmm0
movsd  0xdf8(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    1229 <ourint+0x129>
jp     1229 <ourint+0x129>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
and    $0xfffffffe,%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <ourint+0x150>
movsd  0xdcf(%rip),%xmm0
ucomisd -0x20(%rbp),%xmm0
jbe    1247 <ourint+0x147>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <ourint+0x150>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<ourint>:
mov    0x2ee1(%rip),%rax
mov    (%rax),%eax
cvtsi2sd %eax,%xmm1
addsd  0xeeb(%rip),%xmm1
ucomisd %xmm1,%xmm0
ja     1139 <ourint+0x39>
mov    0x2ebe(%rip),%rax
mov    (%rax),%eax
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
addsd  0xed5(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    113a <ourint+0x3a>
ret
cvttsd2si %xmm0,%eax
cvttpd2dq %xmm0,%xmm1
cvtdq2pd %xmm1,%xmm1
subsd  %xmm1,%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jbe    1164 <ourint+0x64>
ucomisd 0xea4(%rip),%xmm0
jne    1177 <ourint+0x77>
jp     1177 <ourint+0x77>
and    $0xfffffffe,%eax
ret
ucomisd 0xe9c(%rip),%xmm0
jne    117d <ourint+0x7d>
jp     117d <ourint+0x7d>
add    $0x1,%eax
and    $0xfffffffe,%eax
ret
ja     1139 <ourint+0x39>
add    $0xffffffff,%eax
ret
movsd  0xe83(%rip),%xmm1
ucomisd %xmm0,%xmm1
ja     1139 <ourint+0x39>
add    $0x1,%eax
ret

LLVM IR (-O0):
@INT_MAX = global i32 0
@INT_MIN = global i32 0
define i32 @ourint(double %0) {
%2 = alloca i32
%3 = alloca double
%4 = alloca i32
%5 = alloca double
store double %0, double* %3
%6 = load double, double* %3
%7 = load i32, i32* @INT_MAX
%8 = sitofp i32 %7 to double
%9 = fsub double %8, 5.000000e-01
%10 = fcmp ogt double %6, %9
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = load i32, i32* @INT_MAX
store i32 %12, i32* %2
br label %59
13: ; preds = %1
%14 = load double, double* %3
%15 = load i32, i32* @INT_MIN
%16 = sitofp i32 %15 to double
%17 = fadd double %16, 5.000000e-01
%18 = fcmp olt double %14, %17
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = load i32, i32* @INT_MIN
store i32 %20, i32* %2
br label %59
21: ; preds = %13
%22 = load double, double* %3
%23 = fptosi double %22 to i32
store i32 %23, i32* %4
%24 = load double, double* %3
%25 = load i32, i32* %4
%26 = sitofp i32 %25 to double
%27 = fsub double %24, %26
store double %27, double* %5
%28 = load double, double* %5
%29 = fcmp olt double %28, 0.000000e+00
br i1 %29, label %30, label %44
30: ; preds = %21
%31 = load double, double* %5
%32 = fcmp oeq double %31, -5.000000e-01
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i32, i32* %4
%35 = and i32 %34, -2
store i32 %35, i32* %2
br label %59
36: ; preds = %30
%37 = load double, double* %5
%38 = fcmp ogt double %37, -5.000000e-01
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = load i32, i32* %4
store i32 %40, i32* %2
br label %59
41: ; preds = %36
%42 = load i32, i32* %4
%43 = sub nsw i32 %42, 1
store i32 %43, i32* %2
br label %59
44: ; preds = %21
%45 = load double, double* %5
%46 = fcmp oeq double %45, 5.000000e-01
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = load i32, i32* %4
%49 = add nsw i32 %48, 1
%50 = and i32 %49, -2
store i32 %50, i32* %2
br label %59
51: ; preds = %44
%52 = load double, double* %5
%53 = fcmp olt double %52, 5.000000e-01
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = load i32, i32* %4
store i32 %55, i32* %2
br label %59
56: ; preds = %51
%57 = load i32, i32* %4
%58 = add nsw i32 %57, 1
store i32 %58, i32* %2
br label %59
59: ; preds = %56, %54, %47, %41, %39, %33, %19, %11
%60 = load i32, i32* %2
ret i32 %60
}

LLVM IR (-O2):
@INT_MAX = global i32 0
@INT_MIN = global i32 0
define i32 @ourint(double %0) {
%2 = load i32, i32* @INT_MAX
%3 = sitofp i32 %2 to double
%4 = fadd double %3, -5.000000e-01
%5 = fcmp olt double %4, %0
br i1 %5, label %33, label %6
6: ; preds = %1
%7 = load i32, i32* @INT_MIN
%8 = sitofp i32 %7 to double
%9 = fadd double %8, 5.000000e-01
%10 = fcmp ogt double %9, %0
br i1 %10, label %33, label %11
11: ; preds = %6
%12 = fptosi double %0 to i32
%13 = sitofp i32 %12 to double
%14 = fsub double %0, %13
%15 = fcmp olt double %14, 0.000000e+00
br i1 %15, label %16, label %24
16: ; preds = %11
%17 = fcmp oeq double %14, -5.000000e-01
br i1 %17, label %18, label %20
18: ; preds = %16
%19 = and i32 %12, -2
br label %33
20: ; preds = %16
%21 = fcmp ogt double %14, -5.000000e-01
br i1 %21, label %33, label %22
22: ; preds = %20
%23 = add nsw i32 %12, -1
br label %33
24: ; preds = %11
%25 = fcmp oeq double %14, 5.000000e-01
br i1 %25, label %26, label %29
26: ; preds = %24
%27 = add nsw i32 %12, 1
%28 = and i32 %27, -2
br label %33
29: ; preds = %24
%30 = fcmp olt double %14, 5.000000e-01
br i1 %30, label %33, label %31
31: ; preds = %29
%32 = add nsw i32 %12, 1
br label %33
33: ; preds = %29, %20, %6, %1, %31, %26, %22, %18
%34 = phi i32 [ %19, %18 ], [ %23, %22 ], [ %28, %26 ], [ %32, %31 ], [ %2, %1 ], [ %7, %6 ], [ %12, %20 ], [ %12, %29 ]
ret i32 %34
}