#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 */
long double ceill(long double);
int fegetround();
long double floorl(long double);
double fmodl(long double, int);
long double rintl(long double x) {
  const int rounding = fegetround();
  if (rounding == 0) {
    return fmodl(x, 1) < 0.5 ? floorl(x) : ceill(x);
  }
  if (rounding == 1024) {
    return floorl(x);
  }
  if (rounding == 2048) {
    return ceill(x);
  }
  return fmodl(x, 1) == 0.0 ? x : (x < 0.0 ? ceill(x) : floorl(x));
}


x86 assembly (clang, -O0):
<rintl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
fldt   0x10(%rbp)
fstpt  -0x20(%rbp)
mov    $0x0,%al
call   1040 <fegetround@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11b9 <rintl+0x79>
fldt   -0x20(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
mov    $0x1,%edi
call   1030 <fmodl@plt>
movaps %xmm0,%xmm1
movsd  0xe81(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    119e <rintl+0x5e>
fldt   -0x20(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
call   1060 <floorl@plt>
fstpt  -0x30(%rbp)
jmp    11ae <rintl+0x6e>
fldt   -0x20(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
call   1050 <ceill@plt>
fstpt  -0x30(%rbp)
fldt   -0x30(%rbp)
fstpt  -0x10(%rbp)
jmp    1272 <rintl+0x132>
cmpl   $0x400,-0x24(%rbp)
jne    11db <rintl+0x9b>
fldt   -0x20(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
call   1060 <floorl@plt>
fstpt  -0x10(%rbp)
jmp    1272 <rintl+0x132>
cmpl   $0x800,-0x24(%rbp)
jne    11fd <rintl+0xbd>
fldt   -0x20(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
call   1050 <ceill@plt>
fstpt  -0x10(%rbp)
jmp    1272 <rintl+0x132>
fldt   -0x20(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
mov    $0x1,%edi
call   1030 <fmodl@plt>
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    122d <rintl+0xed>
jp     122d <rintl+0xed>
fldt   -0x20(%rbp)
fstpt  -0x3c(%rbp)
jmp    126c <rintl+0x12c>
fldt   -0x20(%rbp)
fldz
fucomip %st(1),%st
fstp   %st(0)
jbe    1256 <rintl+0x116>
jmp    1241 <rintl+0x101>
fldt   -0x20(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
call   1050 <ceill@plt>
fstpt  -0x48(%rbp)
jmp    1266 <rintl+0x126>
fldt   -0x20(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
call   1060 <floorl@plt>
fstpt  -0x48(%rbp)
fldt   -0x48(%rbp)
fstpt  -0x3c(%rbp)
fldt   -0x3c(%rbp)
fstpt  -0x10(%rbp)
fldt   -0x10(%rbp)
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rintl>:
sub    $0x28,%rsp
fldt   0x30(%rsp)
fstpt  0x1c(%rsp)
xor    %eax,%eax
call   1040 <fegetround@plt>
cmp    $0x800,%eax
je     1195 <rintl+0x55>
cmp    $0x400,%eax
je     1184 <rintl+0x44>
test   %eax,%eax
jne    11a6 <rintl+0x66>
fldt   0x1c(%rsp)
fstpt  (%rsp)
mov    $0x1,%edi
call   1030 <fmodl@plt>
movsd  0xe82(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    1195 <rintl+0x55>
fldt   0x1c(%rsp)
fstpt  (%rsp)
call   1060 <floorl@plt>
add    $0x28,%rsp
ret
fldt   0x1c(%rsp)
fstpt  (%rsp)
call   1050 <ceill@plt>
add    $0x28,%rsp
ret
fldt   0x1c(%rsp)
fstpt  (%rsp)
mov    $0x1,%edi
call   1030 <fmodl@plt>
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11cc <rintl+0x8c>
jp     11cc <rintl+0x8c>
fldt   0x1c(%rsp)
add    $0x28,%rsp
ret
fldz
fldt   0x1c(%rsp)
fxch   %st(1)
fucomip %st(1),%st
jbe    1188 <rintl+0x48>
jmp    1199 <rintl+0x59>

LLVM IR (-O0):
define x86_fp80 @rintl(x86_fp80 %0) {
%2 = alloca x86_fp80
%3 = alloca x86_fp80
%4 = alloca i32
store x86_fp80 %0, x86_fp80* %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 x86_fp80, x86_fp80* %3
%10 = call double @fmodl(x86_fp80 %9, i32 1)
%11 = fcmp olt double %10, 5.000000e-01
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load x86_fp80, x86_fp80* %3
%14 = call x86_fp80 @llvm.floor.f80(x86_fp80 %13)
br label %18
15: ; preds = %8
%16 = load x86_fp80, x86_fp80* %3
%17 = call x86_fp80 @llvm.ceil.f80(x86_fp80 %16)
br label %18
18: ; preds = %15, %12
%19 = phi x86_fp80 [ %14, %12 ], [ %17, %15 ]
store x86_fp80 %19, x86_fp80* %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 x86_fp80, x86_fp80* %3
%25 = call x86_fp80 @llvm.floor.f80(x86_fp80 %24)
store x86_fp80 %25, x86_fp80* %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 x86_fp80, x86_fp80* %3
%31 = call x86_fp80 @llvm.ceil.f80(x86_fp80 %30)
store x86_fp80 %31, x86_fp80* %2
br label %51
32: ; preds = %26
%33 = load x86_fp80, x86_fp80* %3
%34 = call double @fmodl(x86_fp80 %33, i32 1)
%35 = fcmp oeq double %34, 0.000000e+00
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = load x86_fp80, x86_fp80* %3
br label %49
38: ; preds = %32
%39 = load x86_fp80, x86_fp80* %3
%40 = fcmp olt x86_fp80 %39, 0xK00000000000000000000
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load x86_fp80, x86_fp80* %3
%43 = call x86_fp80 @llvm.ceil.f80(x86_fp80 %42)
br label %47
44: ; preds = %38
%45 = load x86_fp80, x86_fp80* %3
%46 = call x86_fp80 @llvm.floor.f80(x86_fp80 %45)
br label %47
47: ; preds = %44, %41
%48 = phi x86_fp80 [ %43, %41 ], [ %46, %44 ]
br label %49
49: ; preds = %47, %36
%50 = phi x86_fp80 [ %37, %36 ], [ %48, %47 ]
store x86_fp80 %50, x86_fp80* %2
br label %51
51: ; preds = %49, %29, %23, %18
%52 = load x86_fp80, x86_fp80* %2
ret x86_fp80 %52
}

LLVM IR (-O2):
define x86_fp80 @rintl(x86_fp80 %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 @fmodl(x86_fp80 %0, i32 1)
%5 = fcmp olt double %4, 5.000000e-01
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call x86_fp80 @llvm.floor.f80(x86_fp80 %0)
br label %23
8: ; preds = %3
%9 = tail call x86_fp80 @llvm.ceil.f80(x86_fp80 %0)
br label %23
10: ; preds = %1
%11 = tail call x86_fp80 @llvm.floor.f80(x86_fp80 %0)
br label %23
12: ; preds = %1
%13 = tail call x86_fp80 @llvm.ceil.f80(x86_fp80 %0)
br label %23
14: ; preds = %1
%15 = tail call double @fmodl(x86_fp80 %0, i32 1)
%16 = fcmp oeq double %15, 0.000000e+00
br i1 %16, label %23, label %17
17: ; preds = %14
%18 = fcmp olt x86_fp80 %0, 0xK00000000000000000000
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = tail call x86_fp80 @llvm.ceil.f80(x86_fp80 %0)
br label %23
21: ; preds = %17
%22 = tail call x86_fp80 @llvm.floor.f80(x86_fp80 %0)
br label %23
23: ; preds = %21, %19, %14, %6, %8, %12, %10
%24 = phi x86_fp80 [ %11, %10 ], [ %13, %12 ], [ %7, %6 ], [ %9, %8 ], [ %20, %19 ], [ %22, %21 ], [ %0, %14 ]
ret x86_fp80 %24
}