#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 */
typedef int int32_t;

/* Variables and functions */
int32_t clamp32_from_float(float f) {
  static const float scale = (float)(1UL << 31);
  static const float limpos = 1.;
  static const float limneg = -1.;
  if (f <= limneg) {
    return -0x80000000;
  } else if (f >= limpos) {
    return 0x7fffffff;
  }
  f *= scale;
  return f > 0 ? f + 0.5 : f - 0.5;
}


x86 assembly (clang, -O0):
<clamp32_from_float>:
push   %rbp
mov    %rsp,%rbp
movss  %xmm0,-0x8(%rbp)
movss  0xeef(%rip),%xmm0
ucomiss -0x8(%rbp),%xmm0
jb     1127 <clamp32_from_float+0x27>
movl   $0x80000000,-0x4(%rbp)
jmp    11b6 <clamp32_from_float+0xb6>
movss  -0x8(%rbp),%xmm0
movss  0xed0(%rip),%xmm1
ucomiss %xmm1,%xmm0
jb     1149 <clamp32_from_float+0x49>
movl   $0x7fffffff,-0x4(%rbp)
jmp    11b6 <clamp32_from_float+0xb6>
jmp    114e <clamp32_from_float+0x4e>
movss  0xeb2(%rip),%xmm0
mulss  -0x8(%rbp),%xmm0
movss  %xmm0,-0x8(%rbp)
movss  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jbe    1190 <clamp32_from_float+0x90>
movss  -0x8(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe8e(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    11aa <clamp32_from_float+0xaa>
movss  -0x8(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe6f(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<clamp32_from_float>:
mov    $0x80000000,%eax
movss  0xef3(%rip),%xmm1
ucomiss %xmm0,%xmm1
jae    1147 <clamp32_from_float+0x47>
mov    $0x7fffffff,%eax
ucomiss 0xee6(%rip),%xmm0
jae    1147 <clamp32_from_float+0x47>
mulss  0xee0(%rip),%xmm0
xorps  %xmm1,%xmm1
xor    %eax,%eax
ucomiss %xmm1,%xmm0
seta   %al
cvtss2sd %xmm0,%xmm0
lea    0xed2(%rip),%rcx
addsd  (%rcx,%rax,8),%xmm0
cvttsd2si %xmm0,%eax
ret

LLVM IR (-O0):
@clamp32_from_float.scale = internal constant float 0x41E0000000000000
@clamp32_from_float.limpos = internal constant float 1.000000e+00
@clamp32_from_float.limneg = internal constant float -1.000000e+00
define i32 @clamp32_from_float(float %0) {
%2 = alloca i32
%3 = alloca float
store float %0, float* %3
%4 = load float, float* %3
%5 = fcmp ole float %4, -1.000000e+00
br i1 %5, label %6, label %7
6: ; preds = %1
store i32 -2147483648, i32* %2
br label %28
7: ; preds = %1
%8 = load float, float* %3
%9 = fcmp oge float %8, 1.000000e+00
br i1 %9, label %10, label %11
10: ; preds = %7
store i32 2147483647, i32* %2
br label %28
11: ; preds = %7
br label %12
12: ; preds = %11
%13 = load float, float* %3
%14 = fmul float %13, 0x41E0000000000000
store float %14, float* %3
%15 = load float, float* %3
%16 = fcmp ogt float %15, 0.000000e+00
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load float, float* %3
%19 = fpext float %18 to double
%20 = fadd double %19, 5.000000e-01
br label %25
21: ; preds = %12
%22 = load float, float* %3
%23 = fpext float %22 to double
%24 = fsub double %23, 5.000000e-01
br label %25
25: ; preds = %21, %17
%26 = phi double [ %20, %17 ], [ %24, %21 ]
%27 = fptosi double %26 to i32
store i32 %27, i32* %2
br label %28
28: ; preds = %25, %10, %6
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
define i32 @clamp32_from_float(float %0) {
%2 = fcmp ugt float %0, -1.000000e+00
br i1 %2, label %3, label %12
3: ; preds = %1
%4 = fcmp ult float %0, 1.000000e+00
br i1 %4, label %5, label %12
5: ; preds = %3
%6 = fmul float %0, 0x41E0000000000000
%7 = fcmp ogt float %6, 0.000000e+00
%8 = fpext float %6 to double
%9 = select i1 %7, double 5.000000e-01, double -5.000000e-01
%10 = fadd double %9, %8
%11 = fptosi double %10 to i32
br label %12
12: ; preds = %3, %1, %5
%13 = phi i32 [ %11, %5 ], [ -2147483648, %1 ], [ 2147483647, %3 ]
ret i32 %13
}