double round(x)
double x;
{
  double f;
  if (x > 0.0) {
    f = floor(x);
    x = f + (x - f >= 0.5);
  } else if (x < 0.0) {
    f = ceil(x);
    x = f - (f - x >= 0.5);
  }
  return x;
}


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
jbe    1183 <round+0x63>
movsd  -0x8(%rbp),%xmm0
call   1040 <floor@plt>
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm1
subsd  -0x10(%rbp),%xmm1
movsd  0xe9b(%rip),%xmm2
ucomisd %xmm2,%xmm1
setae  %al
and    $0x1,%al
movzbl %al,%eax
cvtsi2sd %eax,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11d5 <round+0xb5>
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    11d0 <round+0xb0>
movsd  -0x8(%rbp),%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
subsd  -0x8(%rbp),%xmm1
movsd  0xe49(%rip),%xmm2
ucomisd %xmm2,%xmm1
setae  %al
and    $0x1,%al
movzbl %al,%eax
cvtsi2sd %eax,%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11d5 <round+0xb5>
movsd  -0x8(%rbp),%xmm0
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<round>:
sub    $0x18,%rsp
movapd %xmm0,%xmm2
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm2
jbe    1168 <round+0x48>
movapd %xmm2,%xmm0
movapd %xmm2,(%rsp)
call   1040 <floor@plt>
movapd (%rsp),%xmm2
subsd  %xmm0,%xmm2
movsd  0xeaf(%rip),%xmm1
cmplesd %xmm2,%xmm1
movsd  0xeaa(%rip),%xmm2
andpd  %xmm1,%xmm2
addsd  %xmm0,%xmm2
jmp    11a6 <round+0x86>
ucomisd %xmm2,%xmm0
jbe    11a6 <round+0x86>
movapd %xmm2,%xmm0
movapd %xmm2,(%rsp)
call   1030 <ceil@plt>
movapd %xmm0,%xmm1
subsd  (%rsp),%xmm1
movsd  0xe73(%rip),%xmm2
cmplesd %xmm1,%xmm2
movsd  0xe6e(%rip),%xmm1
andpd  %xmm2,%xmm1
subsd  %xmm1,%xmm0
movapd %xmm0,%xmm2
movapd %xmm2,%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 ogt double %4, 0.000000e+00
br i1 %5, label %6, label %17
6: ; preds = %1
%7 = load double, double* %2
%8 = call double @llvm.floor.f64(double %7)
store double %8, double* %3
%9 = load double, double* %3
%10 = load double, double* %2
%11 = load double, double* %3
%12 = fsub double %10, %11
%13 = fcmp oge double %12, 5.000000e-01
%14 = zext i1 %13 to i32
%15 = sitofp i32 %14 to double
%16 = fadd double %9, %15
store double %16, double* %2
br label %32
17: ; preds = %1
%18 = load double, double* %2
%19 = fcmp olt double %18, 0.000000e+00
br i1 %19, label %20, label %31
20: ; preds = %17
%21 = load double, double* %2
%22 = call double @llvm.ceil.f64(double %21)
store double %22, double* %3
%23 = load double, double* %3
%24 = load double, double* %3
%25 = load double, double* %2
%26 = fsub double %24, %25
%27 = fcmp oge double %26, 5.000000e-01
%28 = zext i1 %27 to i32
%29 = sitofp i32 %28 to double
%30 = fsub double %23, %29
store double %30, double* %2
br label %31
31: ; preds = %20, %17
br label %32
32: ; preds = %31, %6
%33 = load double, double* %2
ret double %33
}

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