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


x86 assembly (clang, -O0):
<lround>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1185 <lround+0x65>
movsd  -0x8(%rbp),%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
movsd  0xea0(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1180 <lround+0x60>
movsd  -0x10(%rbp),%xmm0
movsd  0xe91(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    11fe <lround+0xde>
movsd  -0x8(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
addsd  -0x8(%rbp),%xmm0
movsd  0xe43(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11dd <lround+0xbd>
movsd  -0x10(%rbp),%xmm0
movsd  0xe34(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1040 <isfinite@plt>
cmp    $0x0,%eax
je     1247 <lround+0x127>
movsd  -0x10(%rbp),%xmm0
mov    0x2db3(%rip),%rax
cvtsi2sdq (%rax),%xmm1
ucomisd %xmm1,%xmm0
ja     1247 <lround+0x127>
movsd  -0x10(%rbp),%xmm1
mov    0x2db0(%rip),%rax
cvtsi2sdq (%rax),%xmm0
ucomisd %xmm1,%xmm0
jbe    1259 <lround+0x139>
mov    0x2d92(%rip),%rax
mov    (%rax),%ecx
mov    0x2d71(%rip),%rax
mov    %ecx,(%rax)
cvttsd2si -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lround>:
sub    $0x18,%rsp
movapd %xmm0,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
movapd %xmm1,(%rsp)
movapd %xmm1,%xmm0
jae    1180 <lround+0x60>
xorpd  0xecd(%rip),%xmm0
call   1030 <ceil@plt>
movapd (%rsp),%xmm3
addsd  %xmm0,%xmm3
movsd  0xeaf(%rip),%xmm1
addsd  %xmm0,%xmm1
movsd  0xe9b(%rip),%xmm2
cmpltsd %xmm3,%xmm2
andpd  %xmm2,%xmm1
andnpd %xmm0,%xmm2
orpd   %xmm1,%xmm2
xorpd  0xe92(%rip),%xmm2
jmp    11a0 <lround+0x80>
call   1030 <ceil@plt>
movapd %xmm0,%xmm2
subsd  (%rsp),%xmm0
ucomisd 0xe6a(%rip),%xmm0
jbe    11a0 <lround+0x80>
addsd  0xe68(%rip),%xmm2
movapd %xmm2,(%rsp)
movapd %xmm2,%xmm0
call   1040 <isfinite@plt>
movapd (%rsp),%xmm1
test   %eax,%eax
je     11e1 <lround+0xc1>
mov    0x2e12(%rip),%rax
xorps  %xmm0,%xmm0
cvtsi2sdq (%rax),%xmm0
ucomisd %xmm0,%xmm1
ja     11e1 <lround+0xc1>
mov    0x2e15(%rip),%rax
xorps  %xmm0,%xmm0
cvtsi2sdq (%rax),%xmm0
ucomisd %xmm1,%xmm0
jbe    11f3 <lround+0xd3>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    0x2dd7(%rip),%rcx
mov    %eax,(%rcx)
cvttsd2si %xmm1,%rax
add    $0x18,%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 @lround(double %0) {
%2 = alloca double
%3 = alloca double
store double %0, double* %2
%4 = load double, double* %2
%5 = fcmp oge double %4, 0.000000e+00
br i1 %5, label %6, label %17
6: ; preds = %1
%7 = load double, double* %2
%8 = call double @llvm.ceil.f64(double %7)
store double %8, double* %3
%9 = load double, double* %3
%10 = load double, double* %2
%11 = fsub double %9, %10
%12 = fcmp ogt double %11, 5.000000e-01
br i1 %12, label %13, label %16
13: ; preds = %6
%14 = load double, double* %3
%15 = fsub double %14, 1.000000e+00
store double %15, double* %3
br label %16
16: ; preds = %13, %6
br label %31
17: ; preds = %1
%18 = load double, double* %2
%19 = fneg double %18
%20 = call double @llvm.ceil.f64(double %19)
store double %20, double* %3
%21 = load double, double* %3
%22 = load double, double* %2
%23 = fadd double %21, %22
%24 = fcmp ogt double %23, 5.000000e-01
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = load double, double* %3
%27 = fsub double %26, 1.000000e+00
store double %27, double* %3
br label %28
28: ; preds = %25, %17
%29 = load double, double* %3
%30 = fneg double %29
store double %30, double* %3
br label %31
31: ; preds = %28, %16
%32 = load double, double* %3
%33 = call i32 @isfinite(double %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %45
35: ; preds = %31
%36 = load double, double* %3
%37 = load i64, i64* @LONG_MAX
%38 = sitofp i64 %37 to double
%39 = fcmp ogt double %36, %38
br i1 %39, label %45, label %40
40: ; preds = %35
%41 = load double, double* %3
%42 = load i64, i64* @LONG_MIN
%43 = sitofp i64 %42 to double
%44 = fcmp olt double %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 double, double* %3
%49 = fptosi double %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 @lround(double %0) {
%2 = fcmp ult double %0, 0.000000e+00
br i1 %2, label %9, label %3
3: ; preds = %1
%4 = tail call double @llvm.ceil.f64(double %0)
%5 = fsub double %4, %0
%6 = fcmp ogt double %5, 5.000000e-01
br i1 %6, label %7, label %17
7: ; preds = %3
%8 = fadd double %4, -1.000000e+00
br label %17
9: ; preds = %1
%10 = fneg double %0
%11 = tail call double @llvm.ceil.f64(double %10)
%12 = fadd double %11, %0
%13 = fcmp ogt double %12, 5.000000e-01
%14 = fadd double %11, -1.000000e+00
%15 = select i1 %13, double %14, double %11
%16 = fneg double %15
br label %17
17: ; preds = %3, %7, %9
%18 = phi double [ %8, %7 ], [ %4, %3 ], [ %16, %9 ]
%19 = tail call i32 @isfinite(double %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 double
%24 = fcmp ogt double %18, %23
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i64, i64* @LONG_MIN
%27 = sitofp i64 %26 to double
%28 = fcmp olt double %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 double %18 to i64
ret i64 %32
}