#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 floor(double);
long lrint(double x) {
  if (ceil(x + 0.5) == floor(x + 0.5)) {
    if (x < 1 && x > -1)
      return 0;
    return (int)ceil(x) % 2 == 0 ? ceil(x) : floor(x);
  } else
    return x >= 0 ? floor(x + 0.5) : ceil(x - 0.5);
}


x86 assembly (clang, -O0):
<lrint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  0xecb(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
call   1030 <ceil@plt>
mov    %rax,-0x18(%rbp)
movsd  0xeb5(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
call   1040 <floor@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
cmp    %rcx,%rax
jne    11e5 <lrint+0xc5>
movsd  0xe9b(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    119c <lrint+0x7c>
movsd  -0x10(%rbp),%xmm0
movsd  0xe8b(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    119c <lrint+0x7c>
movq   $0x0,-0x8(%rbp)
jmp    1234 <lrint+0x114>
movsd  -0x10(%rbp),%xmm0
call   1030 <ceil@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11ca <lrint+0xaa>
movsd  -0x10(%rbp),%xmm0
call   1030 <ceil@plt>
mov    %rax,-0x20(%rbp)
jmp    11d8 <lrint+0xb8>
movsd  -0x10(%rbp),%xmm0
call   1040 <floor@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1234 <lrint+0x114>
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1212 <lrint+0xf2>
movsd  0xe01(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
call   1040 <floor@plt>
mov    %rax,-0x28(%rbp)
jmp    122c <lrint+0x10c>
movsd  -0x10(%rbp),%xmm0
movsd  0xde1(%rip),%xmm1
subsd  %xmm1,%xmm0
call   1030 <ceil@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lrint>:
push   %rbx
sub    $0x10,%rsp
movsd  0xed3(%rip),%xmm1
movsd  %xmm0,(%rsp)
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movapd %xmm1,%xmm0
call   1030 <ceil@plt>
mov    %rax,%rbx
movsd  0x8(%rsp),%xmm0
call   1040 <floor@plt>
cmp    %rax,%rbx
jne    1181 <lrint+0x61>
movsd  0xeb0(%rip),%xmm0
movsd  (%rsp),%xmm1
ucomisd %xmm1,%xmm0
jbe    11a4 <lrint+0x84>
movapd %xmm1,%xmm0
ucomisd 0xea1(%rip),%xmm1
jbe    11a4 <lrint+0x84>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
movsd  0x8(%rsp),%xmm2
xorpd  %xmm0,%xmm0
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
jae    11c1 <lrint+0xa1>
movapd %xmm1,%xmm0
addsd  0xe66(%rip),%xmm0
jmp    11b7 <lrint+0x97>
movsd  (%rsp),%xmm0
call   1030 <ceil@plt>
movsd  (%rsp),%xmm0
test   $0x1,%al
jne    11c5 <lrint+0xa5>
add    $0x10,%rsp
pop    %rbx
jmp    1030 <ceil@plt>
movapd %xmm2,%xmm0
add    $0x10,%rsp
pop    %rbx
jmp    1040 <floor@plt>

LLVM IR (-O0):
define i64 @lrint(double %0) {
%2 = alloca i64
%3 = alloca double
store double %0, double* %3
%4 = load double, double* %3
%5 = fadd double %4, 5.000000e-01
%6 = call i64 @ceil(double %5)
%7 = load double, double* %3
%8 = fadd double %7, 5.000000e-01
%9 = call i64 @floor(double %8)
%10 = icmp eq i64 %6, %9
br i1 %10, label %11, label %32
11: ; preds = %1
%12 = load double, double* %3
%13 = fcmp olt double %12, 1.000000e+00
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load double, double* %3
%16 = fcmp ogt double %15, -1.000000e+00
br i1 %16, label %17, label %18
17: ; preds = %14
store i64 0, i64* %2
br label %45
18: ; preds = %14, %11
%19 = load double, double* %3
%20 = call i64 @ceil(double %19)
%21 = trunc i64 %20 to i32
%22 = srem i32 %21, 2
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load double, double* %3
%26 = call i64 @ceil(double %25)
br label %30
27: ; preds = %18
%28 = load double, double* %3
%29 = call i64 @floor(double %28)
br label %30
30: ; preds = %27, %24
%31 = phi i64 [ %26, %24 ], [ %29, %27 ]
store i64 %31, i64* %2
br label %45
32: ; preds = %1
%33 = load double, double* %3
%34 = fcmp oge double %33, 0.000000e+00
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = load double, double* %3
%37 = fadd double %36, 5.000000e-01
%38 = call i64 @floor(double %37)
br label %43
39: ; preds = %32
%40 = load double, double* %3
%41 = fsub double %40, 5.000000e-01
%42 = call i64 @ceil(double %41)
br label %43
43: ; preds = %39, %35
%44 = phi i64 [ %38, %35 ], [ %42, %39 ]
store i64 %44, i64* %2
br label %45
45: ; preds = %43, %30, %17
%46 = load i64, i64* %2
ret i64 %46
}

LLVM IR (-O2):
define i64 @lrint(double %0) {
%2 = fadd double %0, 5.000000e-01
%3 = tail call i64 @ceil(double %2)
%4 = tail call i64 @floor(double %2)
%5 = icmp eq i64 %3, %4
br i1 %5, label %6, label %18
6: ; preds = %1
%7 = fcmp olt double %0, 1.000000e+00
%8 = fcmp ogt double %0, -1.000000e+00
%9 = and i1 %7, %8
br i1 %9, label %25, label %10
10: ; preds = %6
%11 = tail call i64 @ceil(double %0)
%12 = and i64 %11, 1
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i64 @ceil(double %0)
br label %25
16: ; preds = %10
%17 = tail call i64 @floor(double %0)
br label %25
18: ; preds = %1
%19 = fcmp ult double %0, 0.000000e+00
br i1 %19, label %22, label %20
20: ; preds = %18
%21 = tail call i64 @floor(double %2)
br label %25
22: ; preds = %18
%23 = fadd double %0, -5.000000e-01
%24 = tail call i64 @ceil(double %23)
br label %25
25: ; preds = %20, %22, %14, %16, %6
%26 = phi i64 [ 0, %6 ], [ %15, %14 ], [ %17, %16 ], [ %21, %20 ], [ %24, %22 ]
ret i64 %26
}