#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 */
scalar_t__ lrintf(float);
void src_float_to_short_array(const float *in, short *out, int len) {
  while (len) {
    float scaled_value;
    len--;
    scaled_value = in[len] * 32768.f;
    if (scaled_value >= 32767.f)
      out[len] = 32767;
    else if (scaled_value <= -32768.f)
      out[len] = -32768;
    else
      out[len] = (short)(lrintf(scaled_value));
  }
}


x86 assembly (clang, -O0):
<src_float_to_short_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11c1 <src_float_to_short_array+0xb1>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movss  0xebe(%rip),%xmm0
mulss  (%rax,%rcx,4),%xmm0
movss  %xmm0,-0x18(%rbp)
movss  -0x18(%rbp),%xmm0
movss  0xea3(%rip),%xmm1
ucomiss %xmm1,%xmm0
jb     1179 <src_float_to_short_array+0x69>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movw   $0x7fff,(%rax,%rcx,2)
jmp    11bc <src_float_to_short_array+0xac>
movss  0xe87(%rip),%xmm0
ucomiss -0x18(%rbp),%xmm0
jb     119e <src_float_to_short_array+0x8e>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movw   $0x8000,(%rax,%rcx,2)
jmp    11b7 <src_float_to_short_array+0xa7>
movss  -0x18(%rbp),%xmm0
call   1030 <lrintf@plt>
mov    %ax,%dx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dx,(%rax,%rcx,2)
jmp    11bc <src_float_to_short_array+0xac>
jmp    1123 <src_float_to_short_array+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<src_float_to_short_array>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     11a9 <src_float_to_short_array+0x99>
mov    %edx,%r15d
movslq %edx,%rax
lea    (%rsi,%rax,2),%r14
add    $0xfffffffffffffffe,%r14
neg    %r15d
lea    (%rdi,%rax,4),%r12
add    $0xfffffffffffffffc,%r12
xor    %ebx,%ebx
movss  0xebd(%rip),%xmm1
movss  0xeb9(%rip),%xmm2
movss  0xeb5(%rip),%xmm3
jmp    116e <src_float_to_short_array+0x5e>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ax,(%r14,%rbx,2)
add    $0xffffffffffffffff,%rbx
cmp    %ebx,%r15d
je     11a9 <src_float_to_short_array+0x99>
movss  (%r12,%rbx,4),%xmm0
mulss  %xmm1,%xmm0
mov    $0x7fff,%ax
ucomiss %xmm2,%xmm0
jae    1160 <src_float_to_short_array+0x50>
mov    $0x8000,%ax
ucomiss %xmm0,%xmm3
jae    1160 <src_float_to_short_array+0x50>
call   1030 <lrintf@plt>
movss  0xe71(%rip),%xmm3
movss  0xe65(%rip),%xmm2
movss  0xe59(%rip),%xmm1
jmp    1160 <src_float_to_short_array+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @src_float_to_short_array(float* %0, i16* %1, i32 %2) {
%4 = alloca float*
%5 = alloca i16*
%6 = alloca i32
%7 = alloca float
store float* %0, float** %4
store i16* %1, i16** %5
store i32 %2, i32* %6
br label %8
8: ; preds = %44, %3
%9 = load i32, i32* %6
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %45
11: ; preds = %8
%12 = load i32, i32* %6
%13 = add nsw i32 %12, -1
store i32 %13, i32* %6
%14 = load float*, float** %4
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds float, float* %14, i64 %16
%18 = load float, float* %17
%19 = fmul float %18, 3.276800e+04
store float %19, float* %7
%20 = load float, float* %7
%21 = fcmp oge float %20, 3.276700e+04
br i1 %21, label %22, label %27
22: ; preds = %11
%23 = load i16*, i16** %5
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i16, i16* %23, i64 %25
store i16 32767, i16* %26
br label %44
27: ; preds = %11
%28 = load float, float* %7
%29 = fcmp ole float %28, -3.276800e+04
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i16*, i16** %5
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i16, i16* %31, i64 %33
store i16 -32768, i16* %34
br label %43
35: ; preds = %27
%36 = load float, float* %7
%37 = call i64 @lrintf(float %36)
%38 = trunc i64 %37 to i16
%39 = load i16*, i16** %5
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i16, i16* %39, i64 %41
store i16 %38, i16* %42
br label %43
43: ; preds = %35, %30
br label %44
44: ; preds = %43, %22
br label %8
45: ; preds = %8
ret void
}

LLVM IR (-O2):
define void @src_float_to_short_array(float* nocapture %0, i16* nocapture writeonly %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %24, label %5
5: ; preds = %3
%6 = sext i32 %2 to i64
br label %7
7: ; preds = %5, %19
%8 = phi i64 [ %6, %5 ], [ %9, %19 ]
%9 = add nsw i64 %8, -1
%10 = getelementptr inbounds float, float* %0, i64 %9
%11 = load float, float* %10
%12 = fmul float %11, 3.276800e+04
%13 = fcmp ult float %12, 3.276700e+04
br i1 %13, label %14, label %19
14: ; preds = %7
%15 = fcmp ugt float %12, -3.276800e+04
br i1 %15, label %16, label %19
16: ; preds = %14
%17 = tail call i64 @lrintf(float %12)
%18 = trunc i64 %17 to i16
br label %19
19: ; preds = %14, %7, %16
%20 = phi i16 [ %18, %16 ], [ 32767, %7 ], [ -32768, %14 ]
%21 = getelementptr inbounds i16, i16* %1, i64 %9
store i16 %20, i16* %21
%22 = trunc i64 %9 to i32
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %7
24: ; preds = %19, %3
ret void
}