#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 ceil(double);
long lround(double x) {
  long t;
  if (x >= 0.0) {
    t = ceil(x);
    if (t - x > 0.5)
      t -= 1.0;
    return (t);
  } else {
    t = ceil(-x);
    if (t + x > 0.5)
      t -= 1.0;
    return (-t);
  }
}


x86 assembly (clang, -O0):
<lround>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1182 <lround+0x72>
movsd  -0x10(%rbp),%xmm0
call   1030 <ceil@plt>
mov    %rax,-0x18(%rbp)
cvtsi2sdq -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  0xeb0(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1175 <lround+0x65>
cvtsi2sdq -0x18(%rbp),%xmm0
movsd  0xea0(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e9 <lround+0xd9>
movsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
call   1030 <ceil@plt>
mov    %rax,-0x18(%rbp)
cvtsi2sdq -0x18(%rbp),%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  0xe46(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11df <lround+0xcf>
cvtsi2sdq -0x18(%rbp),%xmm0
movsd  0xe36(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
sub    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lround>:
sub    $0x18,%rsp
movapd %xmm0,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
movapd %xmm1,(%rsp)
jae    116d <lround+0x5d>
movapd 0xee1(%rip),%xmm0
xorpd  %xmm1,%xmm0
call   1030 <ceil@plt>
mov    %rax,%rcx
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movapd (%rsp),%xmm1
addsd  %xmm0,%xmm1
ucomisd 0xeac(%rip),%xmm1
addsd  0xeac(%rip),%xmm0
cvttsd2si %xmm0,%rax
cmovbe %rcx,%rax
neg    %rax
add    $0x18,%rsp
ret
movapd %xmm1,%xmm0
call   1030 <ceil@plt>
mov    %rax,%rcx
xorps  %xmm0,%xmm0
cvtsi2sd %rax,%xmm0
movapd %xmm0,%xmm1
subsd  (%rsp),%xmm1
ucomisd 0xe6e(%rip),%xmm1
addsd  0xe6e(%rip),%xmm0
cvttsd2si %xmm0,%rax
cmovbe %rcx,%rax
add    $0x18,%rsp
ret

LLVM IR (-O0):
define i64 @lround(double %0) {
%2 = alloca i64
%3 = alloca double
%4 = alloca i64
store double %0, double* %3
%5 = load double, double* %3
%6 = fcmp oge double %5, 0.000000e+00
br i1 %6, label %7, label %22
7: ; preds = %1
%8 = load double, double* %3
%9 = call i64 @ceil(double %8)
store i64 %9, i64* %4
%10 = load i64, i64* %4
%11 = sitofp i64 %10 to double
%12 = load double, double* %3
%13 = fsub double %11, %12
%14 = fcmp ogt double %13, 5.000000e-01
br i1 %14, label %15, label %20
15: ; preds = %7
%16 = load i64, i64* %4
%17 = sitofp i64 %16 to double
%18 = fsub double %17, 1.000000e+00
%19 = fptosi double %18 to i64
store i64 %19, i64* %4
br label %20
20: ; preds = %15, %7
%21 = load i64, i64* %4
store i64 %21, i64* %2
br label %39
22: ; preds = %1
%23 = load double, double* %3
%24 = fneg double %23
%25 = call i64 @ceil(double %24)
store i64 %25, i64* %4
%26 = load i64, i64* %4
%27 = sitofp i64 %26 to double
%28 = load double, double* %3
%29 = fadd double %27, %28
%30 = fcmp ogt double %29, 5.000000e-01
br i1 %30, label %31, label %36
31: ; preds = %22
%32 = load i64, i64* %4
%33 = sitofp i64 %32 to double
%34 = fsub double %33, 1.000000e+00
%35 = fptosi double %34 to i64
store i64 %35, i64* %4
br label %36
36: ; preds = %31, %22
%37 = load i64, i64* %4
%38 = sub nsw i64 0, %37
store i64 %38, i64* %2
br label %39
39: ; preds = %36, %20
%40 = load i64, i64* %2
ret i64 %40
}

LLVM IR (-O2):
define i64 @lround(double %0) {
%2 = fcmp ult double %0, 0.000000e+00
br i1 %2, label %11, label %3
3: ; preds = %1
%4 = tail call i64 @ceil(double %0)
%5 = sitofp i64 %4 to double
%6 = fsub double %5, %0
%7 = fcmp ogt double %6, 5.000000e-01
%8 = fadd double %5, -1.000000e+00
%9 = fptosi double %8 to i64
%10 = select i1 %7, i64 %9, i64 %4
br label %21
11: ; preds = %1
%12 = fneg double %0
%13 = tail call i64 @ceil(double %12)
%14 = sitofp i64 %13 to double
%15 = fadd double %14, %0
%16 = fcmp ogt double %15, 5.000000e-01
%17 = fadd double %14, -1.000000e+00
%18 = fptosi double %17 to i64
%19 = select i1 %16, i64 %18, i64 %13
%20 = sub nsw i64 0, %19
br label %21
21: ; preds = %11, %3
%22 = phi i64 [ %10, %3 ], [ %20, %11 ]
ret i64 %22
}