#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*/ EDOM;
double LONG_MAX;
double LONG_MIN;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ isfinite(double);
double round_internal(double);
long lround(double x) {
  if (!isfinite(x) || !isfinite(x = round_internal(x)) || (x > LONG_MAX) ||
      (x < LONG_MIN))
    errno = EDOM;
  return (long)(x);
}


x86 assembly (clang, -O0):
<lround>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1040 <isfinite@plt>
cmp    $0x0,%eax
je     118d <lround+0x6d>
movsd  -0x8(%rbp),%xmm0
call   1030 <round_internal@plt>
movsd  %xmm0,-0x8(%rbp)
call   1040 <isfinite@plt>
cmp    $0x0,%eax
je     118d <lround+0x6d>
movsd  -0x8(%rbp),%xmm0
mov    0x2e67(%rip),%rax
ucomisd (%rax),%xmm0
ja     118d <lround+0x6d>
movsd  -0x8(%rbp),%xmm1
mov    0x2e61(%rip),%rax
movsd  (%rax),%xmm0
ucomisd %xmm1,%xmm0
jbe    119f <lround+0x7f>
mov    0x2e54(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2b(%rip),%rax
mov    %ecx,(%rax)
cvttsd2si -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lround>:
push   %rax
movsd  %xmm0,(%rsp)
call   1040 <isfinite@plt>
test   %eax,%eax
je     116c <lround+0x4c>
movsd  (%rsp),%xmm0
call   1030 <round_internal@plt>
movsd  %xmm0,(%rsp)
call   1040 <isfinite@plt>
test   %eax,%eax
je     116c <lround+0x4c>
mov    0x2e82(%rip),%rax
movsd  (%rsp),%xmm1
ucomisd (%rax),%xmm1
ja     1171 <lround+0x51>
mov    0x2e80(%rip),%rax
movsd  (%rax),%xmm0
ucomisd %xmm1,%xmm0
jbe    1183 <lround+0x63>
jmp    1171 <lround+0x51>
movsd  (%rsp),%xmm1
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    0x2e47(%rip),%rcx
mov    %eax,(%rcx)
cvttsd2si %xmm1,%rax
pop    %rcx
ret

LLVM IR (-O0):
@LONG_MAX = global double 0.000000e+00
@LONG_MIN = global double 0.000000e+00
@EDOM = global i32 0
@errno = global i32 0
define i64 @lround(double %0) {
%2 = alloca double
store double %0, double* %2
%3 = load double, double* %2
%4 = call i32 @isfinite(double %3)
%5 = icmp ne i32 %4, 0
br i1 %5, label %6, label %19
6: ; preds = %1
%7 = load double, double* %2
%8 = call double @round_internal(double %7)
store double %8, double* %2
%9 = call i32 @isfinite(double %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %6
%12 = load double, double* %2
%13 = load double, double* @LONG_MAX
%14 = fcmp ogt double %12, %13
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load double, double* %2
%17 = load double, double* @LONG_MIN
%18 = fcmp olt double %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15, %11, %6, %1
%20 = load i32, i32* @EDOM
store i32 %20, i32* @errno
br label %21
21: ; preds = %19, %15
%22 = load double, double* %2
%23 = fptosi double %22 to i64
ret i64 %23
}

LLVM IR (-O2):
@LONG_MAX = global double 0.000000e+00
@LONG_MIN = global double 0.000000e+00
@EDOM = global i32 0
@errno = global i32 0
define i64 @lround(double %0) {
%2 = tail call i32 @isfinite(double %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %14, label %4
4: ; preds = %1
%5 = tail call double @round_internal(double %0)
%6 = tail call i32 @isfinite(double %5)
%7 = icmp eq i32 %6, 0
%8 = load double, double* @LONG_MAX
%9 = fcmp ogt double %5, %8
%10 = select i1 %7, i1 true, i1 %9
%11 = load double, double* @LONG_MIN
%12 = fcmp olt double %5, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %14, label %17
14: ; preds = %4, %1
%15 = phi double [ %5, %4 ], [ %0, %1 ]
%16 = load i32, i32* @EDOM
store i32 %16, i32* @errno
br label %17
17: ; preds = %4, %14
%18 = phi double [ %15, %14 ], [ %5, %4 ]
%19 = fptosi double %18 to i64
ret i64 %19
}