#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 */
double fabs(double);
double modf(double, double *);
long int vl_round_d(double x) {
  double intpart, fractpart;
  fractpart = modf(x, &intpart);
  if ((fabs(fractpart) != 0.5) || ((((int)intpart) % 2) != 0))
    return (int)(x + (x >= 0 ? 0.5 : -0.5));
  else
    return (int)intpart;
}


x86 assembly (clang, -O0):
<vl_round_d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
lea    -0x18(%rbp),%rdi
call   1030 <modf@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movaps 0xed4(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  0xeb8(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    116e <vl_round_d+0x5e>
jp     116e <vl_round_d+0x5e>
cvttsd2si -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11b2 <vl_round_d+0xa2>
movsd  -0x10(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
movsd  0xe80(%rip),%xmm4
movsd  0xe80(%rip),%xmm3
xorps  %xmm1,%xmm1
cmplesd %xmm2,%xmm1
movaps %xmm1,%xmm2
andpd  %xmm4,%xmm2
andnpd %xmm3,%xmm1
orpd   %xmm2,%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
cltq
mov    %rax,-0x8(%rbp)
jmp    11bd <vl_round_d+0xad>
cvttsd2si -0x18(%rbp),%eax
cltq
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vl_round_d>:
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
movsd  %xmm0,0x8(%rsp)
call   1030 <modf@plt>
andpd  0xed4(%rip),%xmm0
ucomisd 0xeec(%rip),%xmm0
jne    1142 <vl_round_d+0x32>
jp     1142 <vl_round_d+0x32>
cvttsd2si 0x10(%rsp),%eax
test   $0x1,%al
je     1165 <vl_round_d+0x55>
xorpd  %xmm0,%xmm0
xor    %eax,%eax
movsd  0x8(%rsp),%xmm1
ucomisd %xmm0,%xmm1
setae  %al
lea    0xeb4(%rip),%rcx
addsd  (%rcx,%rax,8),%xmm1
cvttsd2si %xmm1,%eax
cltq
add    $0x18,%rsp
ret

LLVM IR (-O0):
define i64 @vl_round_d(double %0) {
%2 = alloca i64
%3 = alloca double
%4 = alloca double
%5 = alloca double
store double %0, double* %3
%6 = load double, double* %3
%7 = call double @modf(double %6, double* %4)
store double %7, double* %5
%8 = load double, double* %5
%9 = call double @llvm.fabs.f64(double %8)
%10 = fcmp une double %9, 5.000000e-01
br i1 %10, label %16, label %11
11: ; preds = %1
%12 = load double, double* %4
%13 = fptosi double %12 to i32
%14 = srem i32 %13, 2
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %25
16: ; preds = %11, %1
%17 = load double, double* %3
%18 = load double, double* %3
%19 = fcmp oge double %18, 0.000000e+00
%20 = zext i1 %19 to i64
%21 = select i1 %19, double 5.000000e-01, double -5.000000e-01
%22 = fadd double %17, %21
%23 = fptosi double %22 to i32
%24 = sext i32 %23 to i64
store i64 %24, i64* %2
br label %29
25: ; preds = %11
%26 = load double, double* %4
%27 = fptosi double %26 to i32
%28 = sext i32 %27 to i64
store i64 %28, i64* %2
br label %29
29: ; preds = %25, %16
%30 = load i64, i64* %2
ret i64 %30
}

LLVM IR (-O2):
define i64 @vl_round_d(double %0) {
%2 = alloca double
%3 = bitcast double* %2 to i8*
%4 = call double @modf(double %0, double* %2)
%5 = tail call double @llvm.fabs.f64(double %4)
%6 = fcmp une double %5, 5.000000e-01
br i1 %6, label %12, label %7
7: ; preds = %1
%8 = load double, double* %2
%9 = fptosi double %8 to i32
%10 = and i32 %9, 1
%11 = icmp eq i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %7, %1
%13 = fcmp oge double %0, 0.000000e+00
%14 = select i1 %13, double 5.000000e-01, double -5.000000e-01
%15 = fadd double %14, %0
%16 = fptosi double %15 to i32
br label %17
17: ; preds = %7, %12
%18 = phi i32 [ %16, %12 ], [ %9, %7 ]
%19 = sext i32 %18 to i64
ret i64 %19
}