#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 */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
void float_to_s32(const float *in, char *out, const size_t samples) {
  size_t i;
  const int S32_MAX = (1 << 23) - 1;
  const int S32_MIN = -(1 << 23);
  assert(in != NULL);
  assert(out != NULL);
  for (i = 0; i < samples; i++) {
    int32_t *out_val = (int32_t *)(out + i * sizeof(int32_t));
    float f = in[i] * S32_MAX;
    if (f >= S32_MAX)
      *out_val = S32_MAX;
    else if (f <= S32_MIN)
      *out_val = S32_MIN;
    else {
      *out_val = (int32_t)f << 8;
    }
  }
}


x86 assembly (clang, -O0):
<float_to_s32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x7fffff,-0x24(%rbp)
movl   $0xff800000,-0x28(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1208 <float_to_s32+0xf8>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movss  0xe71(%rip),%xmm0
mulss  (%rax,%rcx,4),%xmm0
movss  %xmm0,-0x34(%rbp)
movss  -0x34(%rbp),%xmm0
movss  0xe5a(%rip),%xmm1
ucomiss %xmm1,%xmm0
jb     11be <float_to_s32+0xae>
mov    -0x30(%rbp),%rax
movl   $0x7fffff,(%rax)
jmp    11f2 <float_to_s32+0xe2>
movss  0xe3e(%rip),%xmm0
ucomiss -0x34(%rbp),%xmm0
jb     11df <float_to_s32+0xcf>
mov    -0x30(%rbp),%rax
movl   $0xff800000,(%rax)
jmp    11ed <float_to_s32+0xdd>
cvttss2si -0x34(%rbp),%ecx
shl    $0x8,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
jmp    11f2 <float_to_s32+0xe2>
jmp    11f7 <float_to_s32+0xe7>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    115e <float_to_s32+0x4e>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<float_to_s32>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1030 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1030 <assert@plt>
test   %r14,%r14
je     127b <float_to_s32+0x16b>
cmp    $0x4,%r14
jae    1150 <float_to_s32+0x40>
xor    %eax,%eax
jmp    1281 <float_to_s32+0x171>
mov    %r14,%rax
and    $0xfffffffffffffffc,%rax
lea    -0x4(%rax),%rcx
mov    %rcx,%rdx
shr    $0x2,%rdx
add    $0x1,%rdx
test   %rcx,%rcx
je     12d3 <float_to_s32+0x1c3>
mov    %rdx,%rsi
and    $0xfffffffffffffffe,%rsi
xor    %ecx,%ecx
movaps 0xe81(%rip),%xmm0
movaps 0xe8a(%rip),%xmm1
movdqa 0xe92(%rip),%xmm2
movaps 0xe9b(%rip),%xmm3
cs nopw 0x0(%rax,%rax,1)
nop
movups (%rbx,%rcx,4),%xmm4
mulps  %xmm0,%xmm4
movaps %xmm0,%xmm5
cmpnleps %xmm4,%xmm5
cvttps2dq %xmm4,%xmm6
cmpleps %xmm1,%xmm4
pslld  $0x8,%xmm6
andps  %xmm5,%xmm4
pand   %xmm5,%xmm6
pandn  %xmm2,%xmm5
por    %xmm6,%xmm5
movaps %xmm4,%xmm6
andnps %xmm5,%xmm6
andps  %xmm3,%xmm4
orps   %xmm6,%xmm4
movups %xmm4,(%r15,%rcx,4)
movups 0x10(%rbx,%rcx,4),%xmm4
mulps  %xmm0,%xmm4
movaps %xmm0,%xmm5
cmpnleps %xmm4,%xmm5
cvttps2dq %xmm4,%xmm6
cmpleps %xmm1,%xmm4
pslld  $0x8,%xmm6
andps  %xmm5,%xmm4
pand   %xmm5,%xmm6
pandn  %xmm2,%xmm5
por    %xmm6,%xmm5
movaps %xmm4,%xmm6
andnps %xmm5,%xmm6
andps  %xmm3,%xmm4
orps   %xmm6,%xmm4
movups %xmm4,0x10(%r15,%rcx,4)
add    $0x8,%rcx
add    $0xfffffffffffffffe,%rsi
jne    11a0 <float_to_s32+0x90>
test   $0x1,%dl
je     1276 <float_to_s32+0x166>
movups (%rbx,%rcx,4),%xmm0
movaps 0xdca(%rip),%xmm1
mulps  %xmm1,%xmm0
cmpnleps %xmm0,%xmm1
cvttps2dq %xmm0,%xmm2
cmpleps 0xdc7(%rip),%xmm0
02
pslld  $0x8,%xmm2
andps  %xmm1,%xmm0
pand   %xmm1,%xmm2
pandn  0xdc3(%rip),%xmm1
por    %xmm2,%xmm1
movaps %xmm0,%xmm2
andnps %xmm1,%xmm2
andps  0xdc2(%rip),%xmm0
orps   %xmm2,%xmm0
movups %xmm0,(%r15,%rcx,4)
cmp    %r14,%rax
jne    1281 <float_to_s32+0x171>
pop    %rbx
pop    %r14
pop    %r15
ret
movss  0xdb7(%rip),%xmm0
movss  0xdb3(%rip),%xmm1
jmp    12ad <float_to_s32+0x19d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ecx,(%r15,%rax,4)
add    $0x1,%rax
cmp    %rax,%r14
je     127b <float_to_s32+0x16b>
movss  (%rbx,%rax,4),%xmm2
mulss  %xmm0,%xmm2
mov    $0x7fffff,%ecx
ucomiss %xmm0,%xmm2
jae    12a0 <float_to_s32+0x190>
mov    $0xff800000,%ecx
ucomiss %xmm2,%xmm1
jae    12a0 <float_to_s32+0x190>
cvttss2si %xmm2,%ecx
shl    $0x8,%ecx
jmp    12a0 <float_to_s32+0x190>
xor    %ecx,%ecx
test   $0x1,%dl
jne    122b <float_to_s32+0x11b>
jmp    1276 <float_to_s32+0x166>

LLVM IR (-O0):
define void @float_to_s32(float* %0, i8* %1, i64 %2) {
%4 = alloca float*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca float
store float* %0, float** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 8388607, i32* %8
store i32 -8388608, i32* %9
%12 = load float*, float** %4
%13 = icmp ne float* %12, null
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i8*, i8** %5
%17 = icmp ne i8* %16, null
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
store i64 0, i64* %7
br label %20
20: ; preds = %51, %3
%21 = load i64, i64* %7
%22 = load i64, i64* %6
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %54
24: ; preds = %20
%25 = load i8*, i8** %5
%26 = load i64, i64* %7
%27 = mul i64 %26, 4
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = bitcast i8* %28 to i32*
store i32* %29, i32** %10
%30 = load float*, float** %4
%31 = load i64, i64* %7
%32 = getelementptr inbounds float, float* %30, i64 %31
%33 = load float, float* %32
%34 = fmul float %33, 0x415FFFFFC0000000
store float %34, float* %11
%35 = load float, float* %11
%36 = fcmp oge float %35, 0x415FFFFFC0000000
br i1 %36, label %37, label %39
37: ; preds = %24
%38 = load i32*, i32** %10
store i32 8388607, i32* %38
br label %50
39: ; preds = %24
%40 = load float, float* %11
%41 = fcmp ole float %40, 0xC160000000000000
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = load i32*, i32** %10
store i32 -8388608, i32* %43
br label %49
44: ; preds = %39
%45 = load float, float* %11
%46 = fptosi float %45 to i32
%47 = shl i32 %46, 8
%48 = load i32*, i32** %10
store i32 %47, i32* %48
br label %49
49: ; preds = %44, %42
br label %50
50: ; preds = %49, %37
br label %51
51: ; preds = %50
%52 = load i64, i64* %7
%53 = add i64 %52, 1
store i64 %53, i64* %7
br label %20
54: ; preds = %20
ret void
}

LLVM IR (-O2):
define void @float_to_s32(float* %0, i8* writeonly %1, i64 %2) {
%4 = icmp ne float* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ne i8* %1, null
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp eq i64 %2, 0
br i1 %10, label %97, label %11
11: ; preds = %3
%12 = icmp ult i64 %2, 4
br i1 %12, label %77, label %13
13: ; preds = %11
%14 = and i64 %2, -4
%15 = add i64 %14, -4
%16 = lshr exact i64 %15, 2
%17 = add nuw nsw i64 %16, 1
%18 = and i64 %17, 1
%19 = icmp eq i64 %15, 0
br i1 %19, label %57, label %20
20: ; preds = %13
%21 = and i64 %17, 9223372036854775806
br label %22
22: ; preds = %22, %20
%23 = phi i64 [ 0, %20 ], [ %54, %22 ]
%24 = phi i64 [ 0, %20 ], [ %55, %22 ]
%25 = shl i64 %23, 2
%26 = getelementptr inbounds i8, i8* %1, i64 %25
%27 = getelementptr inbounds float, float* %0, i64 %23
%28 = bitcast float* %27 to <4 x float>*
%29 = load <4 x float>, <4 x float>* %28
%30 = fmul <4 x float> %29, <float 0x415FFFFFC0000000, float 0x415FFFFFC0000000, float 0x415FFFFFC0000000, float 0x415FFFFFC0000000>
%31 = fcmp ult <4 x float> %30, <float 0x415FFFFFC0000000, float 0x415FFFFFC0000000, float 0x415FFFFFC0000000, float 0x415FFFFFC0000000>
%32 = fcmp ole <4 x float> %30, <float 0xC160000000000000, float 0xC160000000000000, float 0xC160000000000000, float 0xC160000000000000>
%33 = fptosi <4 x float> %30 to <4 x i32>
%34 = shl <4 x i32> %33, <i32 8, i32 8, i32 8, i32 8>
%35 = and <4 x i1> %31, %32
%36 = select <4 x i1> %31, <4 x i32> %34, <4 x i32> <i32 8388607, i32 8388607, i32 8388607, i32 8388607>
%37 = select <4 x i1> %35, <4 x i32> <i32 -8388608, i32 -8388608, i32 -8388608, i32 -8388608>, <4 x i32> %36
%38 = bitcast i8* %26 to <4 x i32>*
store <4 x i32> %37, <4 x i32>* %38
%39 = or i64 %23, 4
%40 = shl i64 %39, 2
%41 = getelementptr inbounds i8, i8* %1, i64 %40
%42 = getelementptr inbounds float, float* %0, i64 %39
%43 = bitcast float* %42 to <4 x float>*
%44 = load <4 x float>, <4 x float>* %43
%45 = fmul <4 x float> %44, <float 0x415FFFFFC0000000, float 0x415FFFFFC0000000, float 0x415FFFFFC0000000, float 0x415FFFFFC0000000>
%46 = fcmp ult <4 x float> %45, <float 0x415FFFFFC0000000, float 0x415FFFFFC0000000, float 0x415FFFFFC0000000, float 0x415FFFFFC0000000>
%47 = fcmp ole <4 x float> %45, <float 0xC160000000000000, float 0xC160000000000000, float 0xC160000000000000, float 0xC160000000000000>
%48 = fptosi <4 x float> %45 to <4 x i32>
%49 = shl <4 x i32> %48, <i32 8, i32 8, i32 8, i32 8>
%50 = and <4 x i1> %46, %47
%51 = select <4 x i1> %46, <4 x i32> %49, <4 x i32> <i32 8388607, i32 8388607, i32 8388607, i32 8388607>
%52 = select <4 x i1> %50, <4 x i32> <i32 -8388608, i32 -8388608, i32 -8388608, i32 -8388608>, <4 x i32> %51
%53 = bitcast i8* %41 to <4 x i32>*
store <4 x i32> %52, <4 x i32>* %53
%54 = add nuw i64 %23, 8
%55 = add i64 %24, 2
%56 = icmp eq i64 %55, %21
br i1 %56, label %57, label %22
57: ; preds = %22, %13
%58 = phi i64 [ 0, %13 ], [ %54, %22 ]
%59 = icmp eq i64 %18, 0
br i1 %59, label %75, label %60
60: ; preds = %57
%61 = shl i64 %58, 2
%62 = getelementptr inbounds i8, i8* %1, i64 %61
%63 = getelementptr inbounds float, float* %0, i64 %58
%64 = bitcast float* %63 to <4 x float>*
%65 = load <4 x float>, <4 x float>* %64
%66 = fmul <4 x float> %65, <float 0x415FFFFFC0000000, float 0x415FFFFFC0000000, float 0x415FFFFFC0000000, float 0x415FFFFFC0000000>
%67 = fcmp ult <4 x float> %66, <float 0x415FFFFFC0000000, float 0x415FFFFFC0000000, float 0x415FFFFFC0000000, float 0x415FFFFFC0000000>
%68 = fcmp ole <4 x float> %66, <float 0xC160000000000000, float 0xC160000000000000, float 0xC160000000000000, float 0xC160000000000000>
%69 = fptosi <4 x float> %66 to <4 x i32>
%70 = shl <4 x i32> %69, <i32 8, i32 8, i32 8, i32 8>
%71 = and <4 x i1> %67, %68
%72 = select <4 x i1> %67, <4 x i32> %70, <4 x i32> <i32 8388607, i32 8388607, i32 8388607, i32 8388607>
%73 = select <4 x i1> %71, <4 x i32> <i32 -8388608, i32 -8388608, i32 -8388608, i32 -8388608>, <4 x i32> %72
%74 = bitcast i8* %62 to <4 x i32>*
store <4 x i32> %73, <4 x i32>* %74
br label %75
75: ; preds = %57, %60
%76 = icmp eq i64 %14, %2
br i1 %76, label %97, label %77
77: ; preds = %11, %75
%78 = phi i64 [ 0, %11 ], [ %14, %75 ]
br label %79
79: ; preds = %77, %93
%80 = phi i64 [ %95, %93 ], [ %78, %77 ]
%81 = shl i64 %80, 2
%82 = getelementptr inbounds i8, i8* %1, i64 %81
%83 = bitcast i8* %82 to i32*
%84 = getelementptr inbounds float, float* %0, i64 %80
%85 = load float, float* %84
%86 = fmul float %85, 0x415FFFFFC0000000
%87 = fcmp ult float %86, 0x415FFFFFC0000000
br i1 %87, label %88, label %93
88: ; preds = %79
%89 = fcmp ugt float %86, 0xC160000000000000
br i1 %89, label %90, label %93
90: ; preds = %88
%91 = fptosi float %86 to i32
%92 = shl i32 %91, 8
br label %93
93: ; preds = %88, %79, %90
%94 = phi i32 [ %92, %90 ], [ 8388607, %79 ], [ -8388608, %88 ]
store i32 %94, i32* %83
%95 = add nuw i64 %80, 1
%96 = icmp eq i64 %95, %2
br i1 %96, label %97, label %79
97: ; preds = %93, %75, %3
ret void
}