#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 ceil(double);
double floor(double);
double rint(double x) {
  if (ceil(x + 0.5) == floor(x + 0.5)) {
    int a;
    a = (int)ceil(x);
    if (a % 2 == 0)
      return ceil(x);
    else
      return floor(x);
  } else {
    if (x >= 0)
      return floor(x + 0.5);
    else
      return ceil(x - 0.5);
  }
}


x86 assembly (clang, -O0):
<rint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  0xecb(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  0xeb4(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
call   1040 <floor@plt>
movaps %xmm0,%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jne    11bb <rint+0x9b>
jp     11bb <rint+0x9b>
movsd  -0x10(%rbp),%xmm0
call   1030 <ceil@plt>
cvttsd2si %xmm0,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11a7 <rint+0x87>
movsd  -0x10(%rbp),%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    1204 <rint+0xe4>
movsd  -0x10(%rbp),%xmm0
call   1040 <floor@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    1204 <rint+0xe4>
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     11e9 <rint+0xc9>
movsd  0xe2b(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
call   1040 <floor@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    1204 <rint+0xe4>
movsd  -0x10(%rbp),%xmm0
movsd  0xe0a(%rip),%xmm1
subsd  %xmm1,%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rint>:
sub    $0x18,%rsp
movsd  0xed4(%rip),%xmm1
movsd  %xmm0,(%rsp)
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movapd %xmm1,%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,0x10(%rsp)
movsd  0x8(%rsp),%xmm0
call   1040 <floor@plt>
movsd  0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
jne    1184 <rint+0x64>
jp     1184 <rint+0x64>
movsd  (%rsp),%xmm0
call   1030 <ceil@plt>
cvttsd2si %xmm0,%eax
test   $0x1,%al
je     11a9 <rint+0x89>
movsd  (%rsp),%xmm0
call   1040 <floor@plt>
add    $0x18,%rsp
ret
xorpd  %xmm1,%xmm1
movsd  (%rsp),%xmm2
ucomisd %xmm1,%xmm2
jae    11a9 <rint+0x89>
movsd  (%rsp),%xmm0
addsd  0xe68(%rip),%xmm0
add    $0x18,%rsp
jmp    1030 <ceil@plt>
add    $0x18,%rsp
ret

LLVM IR (-O0):
define double @rint(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca i32
store double %0, double* %3
%5 = load double, double* %3
%6 = fadd double %5, 5.000000e-01
%7 = call double @llvm.ceil.f64(double %6)
%8 = load double, double* %3
%9 = fadd double %8, 5.000000e-01
%10 = call double @llvm.floor.f64(double %9)
%11 = fcmp oeq double %7, %10
br i1 %11, label %12, label %25
12: ; preds = %1
%13 = load double, double* %3
%14 = call double @llvm.ceil.f64(double %13)
%15 = fptosi double %14 to i32
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = srem i32 %16, 2
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load double, double* %3
%21 = call double @llvm.ceil.f64(double %20)
store double %21, double* %2
br label %36
22: ; preds = %12
%23 = load double, double* %3
%24 = call double @llvm.floor.f64(double %23)
store double %24, double* %2
br label %36
25: ; preds = %1
%26 = load double, double* %3
%27 = fcmp oge double %26, 0.000000e+00
br i1 %27, label %28, label %32
28: ; preds = %25
%29 = load double, double* %3
%30 = fadd double %29, 5.000000e-01
%31 = call double @llvm.floor.f64(double %30)
store double %31, double* %2
br label %36
32: ; preds = %25
%33 = load double, double* %3
%34 = fsub double %33, 5.000000e-01
%35 = call double @llvm.ceil.f64(double %34)
store double %35, double* %2
br label %36
36: ; preds = %32, %28, %22, %19
%37 = load double, double* %2
ret double %37
}

LLVM IR (-O2):
define double @rint(double %0) {
%2 = fadd double %0, 5.000000e-01
%3 = tail call double @llvm.ceil.f64(double %2)
%4 = tail call double @llvm.floor.f64(double %2)
%5 = fcmp oeq double %3, %4
br i1 %5, label %6, label %13
6: ; preds = %1
%7 = tail call double @llvm.ceil.f64(double %0)
%8 = fptosi double %7 to i32
%9 = and i32 %8, 1
%10 = icmp eq i32 %9, 0
%11 = tail call double @llvm.floor.f64(double %0)
%12 = select i1 %10, double %7, double %11
br label %18
13: ; preds = %1
%14 = fcmp ult double %0, 0.000000e+00
br i1 %14, label %15, label %18
15: ; preds = %13
%16 = fadd double %0, -5.000000e-01
%17 = tail call double @llvm.ceil.f64(double %16)
br label %18
18: ; preds = %13, %15, %6
%19 = phi double [ %12, %6 ], [ %17, %15 ], [ %4, %13 ]
ret double %19
}