#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 fabs(double);
double floor(double);
unsigned int double_to_closest_int(double value) {
  double ceil_value;
  double floor_value;
  double abs_diff1;
  double abs_diff2;
  ceil_value = ceil(value);
  floor_value = floor(value);
  abs_diff1 = fabs(value - ceil_value);
  abs_diff2 = fabs(value - floor_value);
  if (abs_diff1 < abs_diff2) {
    return ((unsigned int)ceil_value);
  } else {
    return ((unsigned int)floor_value);
  }
}


x86 assembly (clang, -O0):
<double_to_closest_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1030 <ceil@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1040 <floor@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
movaps 0xea4(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
movaps 0xe8a(%rip),%xmm1
pand   %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11a1 <double_to_closest_int+0x81>
cvttsd2si -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11aa <double_to_closest_int+0x8a>
cvttsd2si -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<double_to_closest_int>:
sub    $0x28,%rsp
movaps %xmm0,(%rsp)
call   1030 <ceil@plt>
movaps %xmm0,0x10(%rsp)
movapd (%rsp),%xmm0
call   1040 <floor@plt>
movapd (%rsp),%xmm2
unpcklpd %xmm2,%xmm2
movapd 0x10(%rsp),%xmm3
movapd %xmm3,%xmm1
unpcklpd %xmm0,%xmm1
subpd  %xmm1,%xmm2
andpd  0xea1(%rip),%xmm2
pshufd $0xee,%xmm2,%xmm1
cmpltsd %xmm1,%xmm2
andpd  %xmm2,%xmm3
andnpd %xmm0,%xmm2
orpd   %xmm3,%xmm2
cvttsd2si %xmm2,%rax
add    $0x28,%rsp
ret

LLVM IR (-O0):
define i32 @double_to_closest_int(double %0) {
%2 = alloca i32
%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.ceil.f64(double %8)
store double %9, double* %4
%10 = load double, double* %3
%11 = call double @llvm.floor.f64(double %10)
store double %11, double* %5
%12 = load double, double* %3
%13 = load double, double* %4
%14 = fsub double %12, %13
%15 = call double @llvm.fabs.f64(double %14)
store double %15, double* %6
%16 = load double, double* %3
%17 = load double, double* %5
%18 = fsub double %16, %17
%19 = call double @llvm.fabs.f64(double %18)
store double %19, double* %7
%20 = load double, double* %6
%21 = load double, double* %7
%22 = fcmp olt double %20, %21
br i1 %22, label %23, label %26
23: ; preds = %1
%24 = load double, double* %4
%25 = fptoui double %24 to i32
store i32 %25, i32* %2
br label %29
26: ; preds = %1
%27 = load double, double* %5
%28 = fptoui double %27 to i32
store i32 %28, i32* %2
br label %29
29: ; preds = %26, %23
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
define i32 @double_to_closest_int(double %0) {
%2 = tail call double @llvm.ceil.f64(double %0)
%3 = tail call double @llvm.floor.f64(double %0)
%4 = insertelement <2 x double> poison, double %0, i64 0
%5 = shufflevector <2 x double> %4, <2 x double> poison, <2 x i32> zeroinitializer
%6 = insertelement <2 x double> poison, double %2, i64 0
%7 = insertelement <2 x double> %6, double %3, i64 1
%8 = fsub <2 x double> %5, %7
%9 = call <2 x double> @llvm.fabs.v2f64(<2 x double> %8)
%10 = extractelement <2 x double> %9, i64 0
%11 = extractelement <2 x double> %9, i64 1
%12 = fcmp olt double %10, %11
%13 = select i1 %12, double %2, double %3
%14 = fptoui double %13 to i32
ret i32 %14
}