#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);
int fegetround();
double floor(double);
double fmod(double, int);
double rint(double x) {
  const int rounding = fegetround();
  if (rounding == 0) {
    return fmod(x, 1) < 0.5 ? floor(x) : ceil(x);
  }
  if (rounding == 1024) {
    return floor(x);
  }
  if (rounding == 2048) {
    return ceil(x);
  }
  return fmod(x, 1) == 0.0 ? x : (x < 0.0 ? ceil(x) : floor(x));
}


x86 assembly (clang, -O0):
<rint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    $0x0,%al
call   1050 <fegetround@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11b7 <rint+0x77>
movsd  -0x10(%rbp),%xmm0
mov    $0x1,%edi
call   1040 <fmod@plt>
movaps %xmm0,%xmm1
movsd  0xe85(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    1199 <rint+0x59>
movsd  -0x10(%rbp),%xmm0
call   1060 <floor@plt>
movsd  %xmm0,-0x20(%rbp)
jmp    11a8 <rint+0x68>
movsd  -0x10(%rbp),%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    126f <rint+0x12f>
cmpl   $0x400,-0x14(%rbp)
jne    11d8 <rint+0x98>
movsd  -0x10(%rbp),%xmm0
call   1060 <floor@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    126f <rint+0x12f>
cmpl   $0x800,-0x14(%rbp)
jne    11f9 <rint+0xb9>
movsd  -0x10(%rbp),%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    126f <rint+0x12f>
movsd  -0x10(%rbp),%xmm0
mov    $0x1,%edi
call   1040 <fmod@plt>
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    122a <rint+0xea>
jp     122a <rint+0xea>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    1265 <rint+0x125>
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    124c <rint+0x10c>
movsd  -0x10(%rbp),%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x30(%rbp)
jmp    125b <rint+0x11b>
movsd  -0x10(%rbp),%xmm0
call   1060 <floor@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rint>:
push   %rax
movsd  %xmm0,(%rsp)
xor    %eax,%eax
call   1050 <fegetround@plt>
cmp    $0x800,%eax
je     1187 <rint+0x47>
cmp    $0x400,%eax
je     117c <rint+0x3c>
test   %eax,%eax
jne    1192 <rint+0x52>
movsd  (%rsp),%xmm0
mov    $0x1,%edi
call   1040 <fmod@plt>
movsd  0xe8a(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    1187 <rint+0x47>
movsd  (%rsp),%xmm0
pop    %rax
jmp    1060 <floor@plt>
movsd  (%rsp),%xmm0
pop    %rax
jmp    1030 <ceil@plt>
movsd  (%rsp),%xmm0
mov    $0x1,%edi
call   1040 <fmod@plt>
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11b4 <rint+0x74>
jp     11b4 <rint+0x74>
movsd  (%rsp),%xmm0
pop    %rax
ret
movsd  (%rsp),%xmm0
ucomisd %xmm0,%xmm1
jbe    11c5 <rint+0x85>
pop    %rax
jmp    1030 <ceil@plt>
pop    %rax
jmp    1060 <floor@plt>

LLVM IR (-O0):
define double @rint(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca i32
store double %0, double* %3
%5 = call i32 (...) @fegetround()
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %20
8: ; preds = %1
%9 = load double, double* %3
%10 = call double @fmod(double %9, i32 1)
%11 = fcmp olt double %10, 5.000000e-01
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load double, double* %3
%14 = call double @llvm.floor.f64(double %13)
br label %18
15: ; preds = %8
%16 = load double, double* %3
%17 = call double @llvm.ceil.f64(double %16)
br label %18
18: ; preds = %15, %12
%19 = phi double [ %14, %12 ], [ %17, %15 ]
store double %19, double* %2
br label %51
20: ; preds = %1
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, 1024
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load double, double* %3
%25 = call double @llvm.floor.f64(double %24)
store double %25, double* %2
br label %51
26: ; preds = %20
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 2048
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load double, double* %3
%31 = call double @llvm.ceil.f64(double %30)
store double %31, double* %2
br label %51
32: ; preds = %26
%33 = load double, double* %3
%34 = call double @fmod(double %33, i32 1)
%35 = fcmp oeq double %34, 0.000000e+00
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = load double, double* %3
br label %49
38: ; preds = %32
%39 = load double, double* %3
%40 = fcmp olt double %39, 0.000000e+00
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load double, double* %3
%43 = call double @llvm.ceil.f64(double %42)
br label %47
44: ; preds = %38
%45 = load double, double* %3
%46 = call double @llvm.floor.f64(double %45)
br label %47
47: ; preds = %44, %41
%48 = phi double [ %43, %41 ], [ %46, %44 ]
br label %49
49: ; preds = %47, %36
%50 = phi double [ %37, %36 ], [ %48, %47 ]
store double %50, double* %2
br label %51
51: ; preds = %49, %29, %23, %18
%52 = load double, double* %2
ret double %52
}

LLVM IR (-O2):
define double @rint(double %0) {
%2 = tail call i32 (...) @fegetround()
switch i32 %2, label %14 [
i32 0, label %3
i32 1024, label %10
i32 2048, label %12
]
3: ; preds = %1
%4 = tail call double @fmod(double %0, i32 1)
%5 = fcmp olt double %4, 5.000000e-01
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call double @llvm.floor.f64(double %0)
br label %23
8: ; preds = %3
%9 = tail call double @llvm.ceil.f64(double %0)
br label %23
10: ; preds = %1
%11 = tail call double @llvm.floor.f64(double %0)
br label %23
12: ; preds = %1
%13 = tail call double @llvm.ceil.f64(double %0)
br label %23
14: ; preds = %1
%15 = tail call double @fmod(double %0, i32 1)
%16 = fcmp oeq double %15, 0.000000e+00
br i1 %16, label %23, label %17
17: ; preds = %14
%18 = fcmp olt double %0, 0.000000e+00
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = tail call double @llvm.ceil.f64(double %0)
br label %23
21: ; preds = %17
%22 = tail call double @llvm.floor.f64(double %0)
br label %23
23: ; preds = %21, %19, %14, %6, %8, %12, %10
%24 = phi double [ %11, %10 ], [ %13, %12 ], [ %7, %6 ], [ %9, %8 ], [ %20, %19 ], [ %22, %21 ], [ %0, %14 ]
ret double %24
}