#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 */
int IROUND(float);
float floor(float);
int _mesa_round_to_even(float val) {
  int rounded = IROUND(val);
  if (val - floor(val) == 0.5) {
    if (rounded % 2 != 0)
      rounded += val > 0 ? -1 : 1;
  }
  return rounded;
}


x86 assembly (clang, -O0):
<_mesa_round_to_even>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
call   1030 <IROUND@plt>
mov    %eax,-0x8(%rbp)
movss  -0x4(%rbp),%xmm0
movss  %xmm0,-0xc(%rbp)
movss  -0x4(%rbp),%xmm0
call   1040 <floor@plt>
movaps %xmm0,%xmm1
movss  -0xc(%rbp),%xmm0
subss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe9a(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    11ad <_mesa_round_to_even+0x8d>
jp     11ad <_mesa_round_to_even+0x8d>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     11a8 <_mesa_round_to_even+0x88>
movss  -0x4(%rbp),%xmm0
xorps  %xmm1,%xmm1
mov    $0x1,%eax
mov    $0xffffffff,%ecx
ucomiss %xmm1,%xmm0
cmova  %ecx,%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    11ad <_mesa_round_to_even+0x8d>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_mesa_round_to_even>:
push   %rbx
sub    $0x10,%rsp
movss  %xmm0,0xc(%rsp)
call   1040 <IROUND@plt>
mov    %eax,%ebx
movss  0xc(%rsp),%xmm0
call   1030 <floorf@plt>
mov    %ebx,%ecx
and    $0x1,%ecx
neg    %ecx
xorps  %xmm1,%xmm1
xor    %eax,%eax
movss  0xc(%rsp),%xmm2
ucomiss %xmm1,%xmm2
subss  %xmm0,%xmm2
setbe  %al
add    %eax,%eax
add    $0xffffffff,%eax
and    %ecx,%eax
xor    %ecx,%ecx
ucomiss 0xe97(%rip),%xmm2
cmovne %ecx,%eax
cmovp  %ecx,%eax
add    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @_mesa_round_to_even(float %0) {
%2 = alloca float
%3 = alloca i32
store float %0, float* %2
%4 = load float, float* %2
%5 = call i32 @IROUND(float %4)
store i32 %5, i32* %3
%6 = load float, float* %2
%7 = load float, float* %2
%8 = call float @floor(float %7)
%9 = fsub float %6, %8
%10 = fpext float %9 to double
%11 = fcmp oeq double %10, 5.000000e-01
br i1 %11, label %12, label %24
12: ; preds = %1
%13 = load i32, i32* %3
%14 = srem i32 %13, 2
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %12
%17 = load float, float* %2
%18 = fcmp ogt float %17, 0.000000e+00
%19 = zext i1 %18 to i64
%20 = select i1 %18, i32 -1, i32 1
%21 = load i32, i32* %3
%22 = add nsw i32 %21, %20
store i32 %22, i32* %3
br label %23
23: ; preds = %16, %12
br label %24
24: ; preds = %23, %1
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
define i32 @_mesa_round_to_even(float %0) {
%2 = tail call i32 @IROUND(float %0)
%3 = tail call float @llvm.floor.f32(float %0)
%4 = fsub float %0, %3
%5 = fcmp une float %4, 5.000000e-01
%6 = and i32 %2, 1
%7 = icmp eq i32 %6, 0
%8 = select i1 %5, i1 true, i1 %7
%9 = fcmp ogt float %0, 0.000000e+00
%10 = select i1 %9, i32 -1, i32 1
%11 = select i1 %8, i32 0, i32 %10
%12 = add nsw i32 %11, %2
ret i32 %12
}