#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 floor(float);
void truncate_float_samples(float *samples, int num_samples, int bits) {
  int isample, imin = -(1 << (bits - 1)), imax = (1 << (bits - 1)) - 1;
  float scalar = (float)(1 << (bits - 1));
  while (num_samples--) {
    if (*samples >= 1.0)
      isample = imax;
    else if (*samples <= -1.0)
      isample = imin;
    else
      isample = floor(*samples * scalar);
    *samples++ = isample / scalar;
  }
}


x86 assembly (clang, -O0):
<truncate_float_samples>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%eax
shl    %cl,%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
mov    $0x1,%eax
shl    %cl,%eax
cvtsi2ss %eax,%xmm0
movss  %xmm0,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
je     1203 <truncate_float_samples+0xf3>
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe77(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     119e <truncate_float_samples+0x8e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11e1 <truncate_float_samples+0xd1>
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
cvtss2sd %xmm0,%xmm1
movsd  0xe56(%rip),%xmm0
ucomisd %xmm1,%xmm0
jb     11c7 <truncate_float_samples+0xb7>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11dc <truncate_float_samples+0xcc>
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
mulss  -0x20(%rbp),%xmm0
call   1030 <floor@plt>
mov    %eax,-0x14(%rbp)
jmp    11e1 <truncate_float_samples+0xd1>
cvtsi2ssl -0x14(%rbp),%xmm0
divss  -0x20(%rbp),%xmm0
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x8(%rbp)
movss  %xmm0,(%rax)
jmp    1161 <truncate_float_samples+0x51>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<truncate_float_samples>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    -0x1(%rdx),%ecx
mov    $0x1,%r12d
shl    %cl,%r12d
test   %esi,%esi
je     11af <truncate_float_samples+0x9f>
mov    %rdi,%rbx
mov    %r12d,%r14d
neg    %r14d
cvtsi2ss %r12d,%xmm1
add    $0xffffffff,%r12d
mov    %esi,%r15d
xor    %ebp,%ebp
movss  0xeb1(%rip),%xmm2
movss  0xead(%rip),%xmm3
movss  %xmm1,0xc(%rsp)
jmp    1179 <truncate_float_samples+0x69>
nop
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
divss  %xmm1,%xmm0
movss  %xmm0,(%rbx,%rbp,4)
add    $0x1,%rbp
cmp    %ebp,%r15d
je     11af <truncate_float_samples+0x9f>
movss  (%rbx,%rbp,4),%xmm0
ucomiss %xmm2,%xmm0
mov    %r12d,%eax
jae    1160 <truncate_float_samples+0x50>
ucomiss %xmm0,%xmm3
mov    %r14d,%eax
jae    1160 <truncate_float_samples+0x50>
mulss  %xmm1,%xmm0
call   1030 <floor@plt>
movss  0xe65(%rip),%xmm3
movss  0xe59(%rip),%xmm2
movss  0xc(%rsp),%xmm1
jmp    1160 <truncate_float_samples+0x50>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @truncate_float_samples(float* %0, i32 %1, i32 %2) {
%4 = alloca float*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca float
store float* %0, float** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = load i32, i32* %6
%12 = sub nsw i32 %11, 1
%13 = shl i32 1, %12
%14 = sub nsw i32 0, %13
store i32 %14, i32* %8
%15 = load i32, i32* %6
%16 = sub nsw i32 %15, 1
%17 = shl i32 1, %16
%18 = sub nsw i32 %17, 1
store i32 %18, i32* %9
%19 = load i32, i32* %6
%20 = sub nsw i32 %19, 1
%21 = shl i32 1, %20
%22 = sitofp i32 %21 to float
store float %22, float* %10
br label %23
23: ; preds = %48, %3
%24 = load i32, i32* %5
%25 = add nsw i32 %24, -1
store i32 %25, i32* %5
%26 = icmp ne i32 %24, 0
br i1 %26, label %27, label %55
27: ; preds = %23
%28 = load float*, float** %4
%29 = load float, float* %28
%30 = fpext float %29 to double
%31 = fcmp oge double %30, 1.000000e+00
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = load i32, i32* %9
store i32 %33, i32* %7
br label %48
34: ; preds = %27
%35 = load float*, float** %4
%36 = load float, float* %35
%37 = fpext float %36 to double
%38 = fcmp ole double %37, -1.000000e+00
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = load i32, i32* %8
store i32 %40, i32* %7
br label %47
41: ; preds = %34
%42 = load float*, float** %4
%43 = load float, float* %42
%44 = load float, float* %10
%45 = fmul float %43, %44
%46 = call i32 @floor(float %45)
store i32 %46, i32* %7
br label %47
47: ; preds = %41, %39
br label %48
48: ; preds = %47, %32
%49 = load i32, i32* %7
%50 = sitofp i32 %49 to float
%51 = load float, float* %10
%52 = fdiv float %50, %51
%53 = load float*, float** %4
%54 = getelementptr inbounds float, float* %53, i32 1
store float* %54, float** %4
store float %52, float* %53
br label %23
55: ; preds = %23
ret void
}

LLVM IR (-O2):
define void @truncate_float_samples(float* nocapture %0, i32 %1, i32 %2) {
%4 = add nsw i32 %2, -1
%5 = shl nuw i32 1, %4
%6 = sub nsw i32 0, %5
%7 = add nsw i32 %5, -1
%8 = sitofp i32 %5 to float
%9 = icmp eq i32 %1, 0
br i1 %9, label %27, label %10
10: ; preds = %3, %21
%11 = phi i32 [ %13, %21 ], [ %1, %3 ]
%12 = phi float* [ %25, %21 ], [ %0, %3 ]
%13 = add nsw i32 %11, -1
%14 = load float, float* %12
%15 = fcmp ult float %14, 1.000000e+00
br i1 %15, label %16, label %21
16: ; preds = %10
%17 = fcmp ugt float %14, -1.000000e+00
br i1 %17, label %18, label %21
18: ; preds = %16
%19 = fmul float %14, %8
%20 = tail call i32 @floor(float %19)
br label %21
21: ; preds = %16, %10, %18
%22 = phi i32 [ %20, %18 ], [ %7, %10 ], [ %6, %16 ]
%23 = sitofp i32 %22 to float
%24 = fdiv float %23, %8
%25 = getelementptr inbounds float, float* %12, i64 1
store float %24, float* %12
%26 = icmp eq i32 %13, 0
br i1 %26, label %27, label %10
27: ; preds = %21, %3
ret void
}