#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 /*<<< orphan*/ ERANGE;
scalar_t__ LONG_MAX;
scalar_t__ LONG_MIN;
long double ceill(long double);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ isfinite(long double);
long lroundl(long double x) {
  long double res;
  if (x >= 0.0L) {
    res = ceill(x);
    if (res - x > 0.5L)
      res -= 1.0;
  } else {
    res = ceill(-x);
    if (res + x > 0.5L)
      res -= 1.0L;
    res = -res;
  }
  if (!isfinite(res) || res > (long double)LONG_MAX ||
      res < (long double)LONG_MIN) {
    errno = ERANGE;
  }
  return (long)res;
}


x86 assembly (clang, -O0):
<lroundl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
fldt   0x10(%rbp)
fstpt  -0x10(%rbp)
fldt   -0x10(%rbp)
fldz
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jb     1184 <lroundl+0x64>
jmp    1144 <lroundl+0x24>
fldt   -0x10(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
call   1040 <ceill@plt>
fstpt  -0x20(%rbp)
fldt   -0x20(%rbp)
fldt   -0x10(%rbp)
fsubrp %st,%st(1)
flds   0xe9e(%rip)
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jbe    117f <lroundl+0x5f>
jmp    1173 <lroundl+0x53>
fldt   -0x20(%rbp)
fld1
fchs
faddp  %st,%st(1)
fstpt  -0x20(%rbp)
jmp    11c9 <lroundl+0xa9>
fldt   -0x10(%rbp)
fchs
mov    %rsp,%rax
fstpt  (%rax)
call   1040 <ceill@plt>
fstpt  -0x20(%rbp)
fldt   -0x20(%rbp)
fldt   -0x10(%rbp)
faddp  %st,%st(1)
flds   0xe5c(%rip)
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jbe    11c1 <lroundl+0xa1>
jmp    11b5 <lroundl+0x95>
fldt   -0x20(%rbp)
fld1
fchs
faddp  %st,%st(1)
fstpt  -0x20(%rbp)
fldt   -0x20(%rbp)
fchs
fstpt  -0x20(%rbp)
fldt   -0x20(%rbp)
mov    %rsp,%rax
fstpt  (%rax)
call   1030 <isfinite@plt>
cmp    $0x0,%eax
je     1227 <lroundl+0x107>
fldt   -0x20(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
fildll -0x28(%rbp)
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
ja     1227 <lroundl+0x107>
jmp    1204 <lroundl+0xe4>
fldt   -0x20(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
fildll -0x30(%rbp)
fucomip %st(1),%st
fstp   %st(0)
jbe    1239 <lroundl+0x119>
jmp    1227 <lroundl+0x107>
mov    0x2db2(%rip),%rax
mov    (%rax),%ecx
mov    0x2d91(%rip),%rax
mov    %ecx,(%rax)
fldt   -0x20(%rbp)
fnstcw -0x3a(%rbp)
movzwl -0x3a(%rbp),%eax
or     $0xc00,%eax
mov    %ax,-0x3c(%rbp)
fldcw  -0x3c(%rbp)
fistpll -0x38(%rbp)
fldcw  -0x3a(%rbp)
mov    -0x38(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lroundl>:
sub    $0x38,%rsp
fldt   0x40(%rsp)
fldz
fxch   %st(1)
fucomi %st(1),%st
fstp   %st(1)
fld    %st(0)
fstpt  0x14(%rsp)
jae    1164 <lroundl+0x44>
fchs
fstpt  (%rsp)
call   1040 <ceill@plt>
fldt   0x14(%rsp)
fadd   %st(1),%st
flds   0xeb2(%rip)
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
fld1
fchs
fadd   %st(1),%st
fxch   %st(1)
fcmovnbe %st(1),%st
fstp   %st(1)
fchs
jmp    1186 <lroundl+0x66>
fstpt  (%rsp)
call   1040 <ceill@plt>
fldt   0x14(%rsp)
fsubr  %st(1),%st
flds   0xe88(%rip)
fxch   %st(1)
fucomip %st(1),%st
fstp   %st(0)
jbe    1186 <lroundl+0x66>
fld1
fchs
faddp  %st,%st(1)
fld    %st(0)
fstpt  0x14(%rsp)
fstpt  (%rsp)
call   1030 <isfinite@plt>
fldt   0x14(%rsp)
test   %eax,%eax
je     11ce <lroundl+0xae>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x30(%rsp)
fildll 0x30(%rsp)
fxch   %st(1)
fucomi %st(1),%st
fstp   %st(1)
ja     11ce <lroundl+0xae>
mov    0x2e2a(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x28(%rsp)
fildll 0x28(%rsp)
fucomip %st(1),%st
jbe    11e0 <lroundl+0xc0>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    0x2dea(%rip),%rcx
mov    %eax,(%rcx)
fnstcw 0x10(%rsp)
movzwl 0x10(%rsp),%eax
or     $0xc00,%eax
mov    %ax,0x12(%rsp)
fldcw  0x12(%rsp)
fistpll 0x20(%rsp)
fldcw  0x10(%rsp)
mov    0x20(%rsp),%rax
add    $0x38,%rsp
ret

LLVM IR (-O0):
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@ERANGE = global i32 0
@errno = global i32 0
define i64 @lroundl(x86_fp80 %0) {
%2 = alloca x86_fp80
%3 = alloca x86_fp80
store x86_fp80 %0, x86_fp80* %2
%4 = load x86_fp80, x86_fp80* %2
%5 = fcmp oge x86_fp80 %4, 0xK00000000000000000000
br i1 %5, label %6, label %17
6: ; preds = %1
%7 = load x86_fp80, x86_fp80* %2
%8 = call x86_fp80 @llvm.ceil.f80(x86_fp80 %7)
store x86_fp80 %8, x86_fp80* %3
%9 = load x86_fp80, x86_fp80* %3
%10 = load x86_fp80, x86_fp80* %2
%11 = fsub x86_fp80 %9, %10
%12 = fcmp ogt x86_fp80 %11, 0xK3FFE8000000000000000
br i1 %12, label %13, label %16
13: ; preds = %6
%14 = load x86_fp80, x86_fp80* %3
%15 = fsub x86_fp80 %14, 0xK3FFF8000000000000000
store x86_fp80 %15, x86_fp80* %3
br label %16
16: ; preds = %13, %6
br label %31
17: ; preds = %1
%18 = load x86_fp80, x86_fp80* %2
%19 = fneg x86_fp80 %18
%20 = call x86_fp80 @llvm.ceil.f80(x86_fp80 %19)
store x86_fp80 %20, x86_fp80* %3
%21 = load x86_fp80, x86_fp80* %3
%22 = load x86_fp80, x86_fp80* %2
%23 = fadd x86_fp80 %21, %22
%24 = fcmp ogt x86_fp80 %23, 0xK3FFE8000000000000000
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = load x86_fp80, x86_fp80* %3
%27 = fsub x86_fp80 %26, 0xK3FFF8000000000000000
store x86_fp80 %27, x86_fp80* %3
br label %28
28: ; preds = %25, %17
%29 = load x86_fp80, x86_fp80* %3
%30 = fneg x86_fp80 %29
store x86_fp80 %30, x86_fp80* %3
br label %31
31: ; preds = %28, %16
%32 = load x86_fp80, x86_fp80* %3
%33 = call i32 @isfinite(x86_fp80 %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %45
35: ; preds = %31
%36 = load x86_fp80, x86_fp80* %3
%37 = load i64, i64* @LONG_MAX
%38 = sitofp i64 %37 to x86_fp80
%39 = fcmp ogt x86_fp80 %36, %38
br i1 %39, label %45, label %40
40: ; preds = %35
%41 = load x86_fp80, x86_fp80* %3
%42 = load i64, i64* @LONG_MIN
%43 = sitofp i64 %42 to x86_fp80
%44 = fcmp olt x86_fp80 %41, %43
br i1 %44, label %45, label %47
45: ; preds = %40, %35, %31
%46 = load i32, i32* @ERANGE
store i32 %46, i32* @errno
br label %47
47: ; preds = %45, %40
%48 = load x86_fp80, x86_fp80* %3
%49 = fptosi x86_fp80 %48 to i64
ret i64 %49
}

LLVM IR (-O2):
@LONG_MAX = global i64 0
@LONG_MIN = global i64 0
@ERANGE = global i32 0
@errno = global i32 0
define i64 @lroundl(x86_fp80 %0) {
%2 = fcmp ult x86_fp80 %0, 0xK00000000000000000000
br i1 %2, label %9, label %3
3: ; preds = %1
%4 = tail call x86_fp80 @llvm.ceil.f80(x86_fp80 %0)
%5 = fsub x86_fp80 %4, %0
%6 = fcmp ogt x86_fp80 %5, 0xK3FFE8000000000000000
br i1 %6, label %7, label %17
7: ; preds = %3
%8 = fadd x86_fp80 %4, 0xKBFFF8000000000000000
br label %17
9: ; preds = %1
%10 = fneg x86_fp80 %0
%11 = tail call x86_fp80 @llvm.ceil.f80(x86_fp80 %10)
%12 = fadd x86_fp80 %11, %0
%13 = fcmp ogt x86_fp80 %12, 0xK3FFE8000000000000000
%14 = fadd x86_fp80 %11, 0xKBFFF8000000000000000
%15 = select i1 %13, x86_fp80 %14, x86_fp80 %11
%16 = fneg x86_fp80 %15
br label %17
17: ; preds = %3, %7, %9
%18 = phi x86_fp80 [ %8, %7 ], [ %4, %3 ], [ %16, %9 ]
%19 = tail call i32 @isfinite(x86_fp80 %18)
%20 = icmp eq i32 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %17
%22 = load i64, i64* @LONG_MAX
%23 = sitofp i64 %22 to x86_fp80
%24 = fcmp ogt x86_fp80 %18, %23
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i64, i64* @LONG_MIN
%27 = sitofp i64 %26 to x86_fp80
%28 = fcmp olt x86_fp80 %18, %27
br i1 %28, label %29, label %31
29: ; preds = %25, %21, %17
%30 = load i32, i32* @ERANGE
store i32 %30, i32* @errno
br label %31
31: ; preds = %29, %25
%32 = fptosi x86_fp80 %18 to i64
ret i64 %32
}