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


x86 assembly (clang, -O0):
<round>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jb     1175 <round+0x65>
movsd  -0x8(%rbp),%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
movsd  0xeb0(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1170 <round+0x60>
movsd  -0x10(%rbp),%xmm0
movsd  0xea1(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    11ee <round+0xde>
movsd  -0x8(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
addsd  -0x8(%rbp),%xmm0
movsd  0xe53(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11cd <round+0xbd>
movsd  -0x10(%rbp),%xmm0
movsd  0xe44(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<round>:
sub    $0x18,%rsp
movapd %xmm0,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
movapd %xmm1,(%rsp)
movapd %xmm1,%xmm0
jae    1177 <round+0x67>
xorpd  0xedd(%rip),%xmm0
call   1030 <ceil@plt>
movapd %xmm0,%xmm1
movapd (%rsp),%xmm3
addsd  %xmm0,%xmm3
movsd  0xebb(%rip),%xmm2
addsd  %xmm0,%xmm2
movsd  0xea7(%rip),%xmm0
cmpltsd %xmm3,%xmm0
andpd  %xmm0,%xmm2
andnpd %xmm1,%xmm0
orpd   %xmm2,%xmm0
xorpd  0xe9e(%rip),%xmm0
add    $0x18,%rsp
ret
call   1030 <ceil@plt>
movapd %xmm0,%xmm1
subsd  (%rsp),%xmm1
ucomisd 0xe73(%rip),%xmm1
jbe    1172 <round+0x62>
addsd  0xe71(%rip),%xmm0
add    $0x18,%rsp
ret

LLVM IR (-O0):
define double @round(double %0) {
%2 = alloca double
%3 = alloca double
store double %0, double* %2
%4 = load double, double* %2
%5 = fcmp oge double %4, 0.000000e+00
br i1 %5, label %6, label %17
6: ; preds = %1
%7 = load double, double* %2
%8 = call double @llvm.ceil.f64(double %7)
store double %8, double* %3
%9 = load double, double* %3
%10 = load double, double* %2
%11 = fsub double %9, %10
%12 = fcmp ogt double %11, 5.000000e-01
br i1 %12, label %13, label %16
13: ; preds = %6
%14 = load double, double* %3
%15 = fsub double %14, 1.000000e+00
store double %15, double* %3
br label %16
16: ; preds = %13, %6
br label %31
17: ; preds = %1
%18 = load double, double* %2
%19 = fneg double %18
%20 = call double @llvm.ceil.f64(double %19)
store double %20, double* %3
%21 = load double, double* %3
%22 = load double, double* %2
%23 = fadd double %21, %22
%24 = fcmp ogt double %23, 5.000000e-01
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = load double, double* %3
%27 = fsub double %26, 1.000000e+00
store double %27, double* %3
br label %28
28: ; preds = %25, %17
%29 = load double, double* %3
%30 = fneg double %29
store double %30, double* %3
br label %31
31: ; preds = %28, %16
%32 = load double, double* %3
ret double %32
}

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