#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 */
typedef int int64_t;

/* Variables and functions */
double const ceil(double const);
double const floor(double const);
scalar_t__ round(double const);
scalar_t__ trunc(double const);
void round_l_d(const double *source, int64_t *dest) {
  double remainder = *source - floor(*source);
  if (remainder == 0.5) {
    if (*source < 0) {
      *dest = (int64_t)trunc(*source) % 2 != 0 ? floor(*source) : ceil(*source);
    } else {
      *dest = (int64_t)trunc(*source) % 2 != 0 ? ceil(*source) : floor(*source);
    }
  } else {
    *dest = (int64_t)round(*source);
  }
}


x86 assembly (clang, -O0):
<round_l_d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
call   1060 <floor@plt>
movaps %xmm0,%xmm1
movsd  -0x20(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xe78(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    125f <round_l_d+0x11f>
jp     125f <round_l_d+0x11f>
mov    -0x8(%rbp),%rax
xorps  %xmm0,%xmm0
ucomisd (%rax),%xmm0
jbe    1204 <round_l_d+0xc4>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
call   1040 <trunc@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11de <round_l_d+0x9e>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
call   1060 <floor@plt>
movsd  %xmm0,-0x28(%rbp)
jmp    11f0 <round_l_d+0xb0>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
cvttsd2si %xmm0,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    125a <round_l_d+0x11a>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
call   1040 <trunc@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1239 <round_l_d+0xf9>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x30(%rbp)
jmp    124b <round_l_d+0x10b>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
call   1060 <floor@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
cvttsd2si %xmm0,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
jmp    1274 <round_l_d+0x134>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
call   1050 <round@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<round_l_d>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
movsd  (%rdi),%xmm0
movsd  %xmm0,(%rsp)
call   1060 <floor@plt>
movsd  (%rsp),%xmm2
movapd %xmm2,%xmm1
subsd  %xmm0,%xmm1
movapd %xmm2,%xmm0
ucomisd 0xe8f(%rip),%xmm1
jne    1194 <round_l_d+0x54>
jp     1194 <round_l_d+0x54>
call   1040 <trunc@plt>
xorpd  %xmm0,%xmm0
ucomisd (%rsp),%xmm0
movsd  (%rbx),%xmm0
jbe    119b <round_l_d+0x5b>
test   $0x1,%al
jne    119f <round_l_d+0x5f>
call   1030 <ceil@plt>
jmp    11a4 <round_l_d+0x64>
call   1050 <round@plt>
jmp    11a8 <round_l_d+0x68>
test   $0x1,%al
jne    118d <round_l_d+0x4d>
call   1060 <floor@plt>
cvttsd2si %xmm0,%eax
mov    %eax,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @round_l_d(double* %0, i32* %1) {
%3 = alloca double*
%4 = alloca i32*
%5 = alloca double
store double* %0, double** %3
store i32* %1, i32** %4
%6 = load double*, double** %3
%7 = load double, double* %6
%8 = load double*, double** %3
%9 = load double, double* %8
%10 = call double @floor(double %9)
%11 = fsub double %7, %10
store double %11, double* %5
%12 = load double, double* %5
%13 = fcmp oeq double %12, 5.000000e-01
br i1 %13, label %14, label %57
14: ; preds = %2
%15 = load double*, double** %3
%16 = load double, double* %15
%17 = fcmp olt double %16, 0.000000e+00
br i1 %17, label %18, label %37
18: ; preds = %14
%19 = load double*, double** %3
%20 = load double, double* %19
%21 = call i64 @trunc(double %20)
%22 = trunc i64 %21 to i32
%23 = srem i32 %22, 2
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = load double*, double** %3
%27 = load double, double* %26
%28 = call double @floor(double %27)
br label %33
29: ; preds = %18
%30 = load double*, double** %3
%31 = load double, double* %30
%32 = call double @ceil(double %31)
br label %33
33: ; preds = %29, %25
%34 = phi double [ %28, %25 ], [ %32, %29 ]
%35 = fptosi double %34 to i32
%36 = load i32*, i32** %4
store i32 %35, i32* %36
br label %56
37: ; preds = %14
%38 = load double*, double** %3
%39 = load double, double* %38
%40 = call i64 @trunc(double %39)
%41 = trunc i64 %40 to i32
%42 = srem i32 %41, 2
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %37
%45 = load double*, double** %3
%46 = load double, double* %45
%47 = call double @ceil(double %46)
br label %52
48: ; preds = %37
%49 = load double*, double** %3
%50 = load double, double* %49
%51 = call double @floor(double %50)
br label %52
52: ; preds = %48, %44
%53 = phi double [ %47, %44 ], [ %51, %48 ]
%54 = fptosi double %53 to i32
%55 = load i32*, i32** %4
store i32 %54, i32* %55
br label %56
56: ; preds = %52, %33
br label %63
57: ; preds = %2
%58 = load double*, double** %3
%59 = load double, double* %58
%60 = call i64 @round(double %59)
%61 = trunc i64 %60 to i32
%62 = load i32*, i32** %4
store i32 %61, i32* %62
br label %63
63: ; preds = %57, %56
ret void
}

LLVM IR (-O2):
define void @round_l_d(double* nocapture %0, i32* nocapture writeonly %1) {
%3 = load double, double* %0
%4 = tail call double @llvm.floor.f64(double %3)
%5 = fsub double %3, %4
%6 = fcmp oeq double %5, 5.000000e-01
br i1 %6, label %7, label %29
7: ; preds = %2
%8 = fcmp olt double %3, 0.000000e+00
%9 = tail call i64 @trunc(double %3)
%10 = and i64 %9, 1
%11 = icmp eq i64 %10, 0
%12 = load double, double* %0
br i1 %8, label %13, label %21
13: ; preds = %7
br i1 %11, label %16, label %14
14: ; preds = %13
%15 = tail call double @llvm.floor.f64(double %12)
br label %18
16: ; preds = %13
%17 = tail call double @llvm.ceil.f64(double %12)
br label %18
18: ; preds = %16, %14
%19 = phi double [ %15, %14 ], [ %17, %16 ]
%20 = fptosi double %19 to i32
br label %32
21: ; preds = %7
br i1 %11, label %24, label %22
22: ; preds = %21
%23 = tail call double @llvm.ceil.f64(double %12)
br label %26
24: ; preds = %21
%25 = tail call double @llvm.floor.f64(double %12)
br label %26
26: ; preds = %24, %22
%27 = phi double [ %23, %22 ], [ %25, %24 ]
%28 = fptosi double %27 to i32
br label %32
29: ; preds = %2
%30 = tail call i64 @round(double %3)
%31 = trunc i64 %30 to i32
br label %32
32: ; preds = %18, %26, %29
%33 = phi i32 [ %20, %18 ], [ %28, %26 ], [ %31, %29 ]
store i32 %33, i32* %1
ret void
}