#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 ceil(double);
double floor(double);
double fmod(double, double);
double round_per_R5RS(double x) {
  double fl = floor(x);
  double ce = ceil(x);
  double dfl = x - fl;
  double dce = ce - x;
  if (dfl > dce) {
    return ce;
  } else if (dfl < dce) {
    return fl;
  } else {
    if (fmod(fl, 2.0) == 0.0) {
      return fl;
    } else {
      return ce;
    }
  }
}


x86 assembly (clang, -O0):
<round_per_R5RS>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1050 <floor@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
ucomisd -0x30(%rbp),%xmm0
jbe    1198 <round_per_R5RS+0x68>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f9 <round_per_R5RS+0xc9>
movsd  -0x28(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11bb <round_per_R5RS+0x8b>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f9 <round_per_R5RS+0xc9>
movsd  -0x18(%rbp),%xmm0
movsd  0xe38(%rip),%xmm1
call   1040 <fmod@plt>
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    11ef <round_per_R5RS+0xbf>
jp     11ef <round_per_R5RS+0xbf>
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f9 <round_per_R5RS+0xc9>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<round_per_R5RS>:
sub    $0x28,%rsp
movsd  %xmm0,(%rsp)
call   1050 <floor@plt>
movaps %xmm0,0x10(%rsp)
movsd  (%rsp),%xmm0
call   1030 <ceil@plt>
movsd  (%rsp),%xmm3
movapd 0x10(%rsp),%xmm2
movapd %xmm0,%xmm1
subsd  %xmm3,%xmm0
subsd  %xmm2,%xmm3
ucomisd %xmm0,%xmm3
ja     11a8 <round_per_R5RS+0x78>
ucomisd %xmm3,%xmm0
jbe    1176 <round_per_R5RS+0x46>
movapd %xmm2,%xmm1
jmp    11a8 <round_per_R5RS+0x78>
movapd %xmm1,(%rsp)
movsd  0xe7d(%rip),%xmm1
movapd %xmm2,%xmm0
call   1040 <fmod@plt>
xorpd  %xmm1,%xmm1
cmpeqsd %xmm0,%xmm1
movapd 0x10(%rsp),%xmm0
andpd  %xmm1,%xmm0
andnpd (%rsp),%xmm1
orpd   %xmm0,%xmm1
movapd %xmm1,%xmm0
add    $0x28,%rsp
ret

LLVM IR (-O0):
define double @round_per_R5RS(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
store double %0, double* %3
%8 = load double, double* %3
%9 = call double @llvm.floor.f64(double %8)
store double %9, double* %4
%10 = load double, double* %3
%11 = call double @llvm.ceil.f64(double %10)
store double %11, double* %5
%12 = load double, double* %3
%13 = load double, double* %4
%14 = fsub double %12, %13
store double %14, double* %6
%15 = load double, double* %5
%16 = load double, double* %3
%17 = fsub double %15, %16
store double %17, double* %7
%18 = load double, double* %6
%19 = load double, double* %7
%20 = fcmp ogt double %18, %19
br i1 %20, label %21, label %23
21: ; preds = %1
%22 = load double, double* %5
store double %22, double* %2
br label %37
23: ; preds = %1
%24 = load double, double* %6
%25 = load double, double* %7
%26 = fcmp olt double %24, %25
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = load double, double* %4
store double %28, double* %2
br label %37
29: ; preds = %23
%30 = load double, double* %4
%31 = call double @fmod(double %30, double 2.000000e+00)
%32 = fcmp oeq double %31, 0.000000e+00
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = load double, double* %4
store double %34, double* %2
br label %37
35: ; preds = %29
%36 = load double, double* %5
store double %36, double* %2
br label %37
37: ; preds = %35, %33, %27, %21
%38 = load double, double* %2
ret double %38
}

LLVM IR (-O2):
define double @round_per_R5RS(double %0) {
%2 = tail call double @llvm.floor.f64(double %0)
%3 = tail call double @llvm.ceil.f64(double %0)
%4 = fsub double %0, %2
%5 = fsub double %3, %0
%6 = fcmp ogt double %4, %5
br i1 %6, label %13, label %7
7: ; preds = %1
%8 = fcmp olt double %4, %5
br i1 %8, label %13, label %9
9: ; preds = %7
%10 = tail call double @fmod(double %2, double 2.000000e+00)
%11 = fcmp oeq double %10, 0.000000e+00
%12 = select i1 %11, double %2, double %3
br label %13
13: ; preds = %9, %7, %1
%14 = phi double [ %3, %1 ], [ %2, %7 ], [ %12, %9 ]
ret double %14
}