#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 /*<<< orphan*/ int16_t;

/* Variables and functions */
float INT16_MAX;
float INT16_MIN;
float floor(float);
int16_t ConvertSampleFloatToInt16(float sample) {
  float value = floor(32768.0f * sample + 0.5f);
  if (INT16_MIN >= value) {
    value = INT16_MIN;
  }
  if (INT16_MAX <= value) {
    value = INT16_MAX;
  }
  return (int16_t)value;
}


x86 assembly (clang, -O0):
<ConvertSampleFloatToInt16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movss  %xmm0,-0x4(%rbp)
movss  -0x4(%rbp),%xmm0
movss  0xed6(%rip),%xmm1
mulss  %xmm1,%xmm0
movss  0xece(%rip),%xmm1
addss  %xmm1,%xmm0
call   1030 <floor@plt>
movss  %xmm0,-0x8(%rbp)
mov    0x2e8d(%rip),%rax
movss  (%rax),%xmm0
ucomiss -0x8(%rbp),%xmm0
jb     1169 <ConvertSampleFloatToInt16+0x59>
mov    0x2e78(%rip),%rax
movss  (%rax),%xmm0
movss  %xmm0,-0x8(%rbp)
mov    0x2e70(%rip),%rax
movss  (%rax),%xmm1
movss  -0x8(%rbp),%xmm0
ucomiss %xmm1,%xmm0
jb     1192 <ConvertSampleFloatToInt16+0x82>
mov    0x2e57(%rip),%rax
movss  (%rax),%xmm0
movss  %xmm0,-0x8(%rbp)
cvttss2si -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ConvertSampleFloatToInt16>:
push   %rax
mulss  0xee7(%rip),%xmm0
addss  0xee3(%rip),%xmm0
call   1030 <floorf@plt>
mov    0x2eab(%rip),%rax
movss  (%rax),%xmm1
movaps %xmm0,%xmm2
cmpless %xmm1,%xmm2
andps  %xmm2,%xmm1
andnps %xmm0,%xmm2
orps   %xmm1,%xmm2
mov    0x2e97(%rip),%rax
movss  (%rax),%xmm0
movaps %xmm0,%xmm1
cmpless %xmm2,%xmm1
movaps %xmm1,%xmm3
andnps %xmm2,%xmm3
andps  %xmm0,%xmm1
orps   %xmm3,%xmm1
cvttss2si %xmm1,%eax
pop    %rcx
ret

LLVM IR (-O0):
@INT16_MIN = global float 0.000000e+00
@INT16_MAX = global float 0.000000e+00
define i32 @ConvertSampleFloatToInt16(float %0) {
%2 = alloca float
%3 = alloca float
store float %0, float* %2
%4 = load float, float* %2
%5 = call float @llvm.fmuladd.f32(float 3.276800e+04, float %4, float 5.000000e-01)
%6 = call float @floor(float %5)
store float %6, float* %3
%7 = load float, float* @INT16_MIN
%8 = load float, float* %3
%9 = fcmp oge float %7, %8
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load float, float* @INT16_MIN
store float %11, float* %3
br label %12
12: ; preds = %10, %1
%13 = load float, float* @INT16_MAX
%14 = load float, float* %3
%15 = fcmp ole float %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = load float, float* @INT16_MAX
store float %17, float* %3
br label %18
18: ; preds = %16, %12
%19 = load float, float* %3
%20 = fptosi float %19 to i32
ret i32 %20
}

LLVM IR (-O2):
@INT16_MIN = global float 0.000000e+00
@INT16_MAX = global float 0.000000e+00
define i32 @ConvertSampleFloatToInt16(float %0) {
%2 = tail call float @llvm.fmuladd.f32(float %0, float 3.276800e+04, float 5.000000e-01)
%3 = tail call float @llvm.floor.f32(float %2)
%4 = load float, float* @INT16_MIN
%5 = fcmp oge float %4, %3
%6 = select i1 %5, float %4, float %3
%7 = load float, float* @INT16_MAX
%8 = fcmp ole float %7, %6
%9 = select i1 %8, float %7, float %6
%10 = fptosi float %9 to i32
ret i32 %10
}