#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 lrint(double);
void src_float_to_short_array(const float *in, short *out, int len) {
  double scaled_value;
  while (len) {
    len--;
    scaled_value = in[len] * (8.0 * 0x10000000);
    if (scaled_value >= (1.0 * 0x7FFFFFFF)) {
      out[len] = 32767;
      continue;
    };
    if (scaled_value <= (-8.0 * 0x10000000)) {
      out[len] = -32768;
      continue;
    };
    out[len] = (short)(lrint(scaled_value) >> 16);
  };
}


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     11c9 <src_float_to_short_array+0xb9>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xeb9(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  0xe9b(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     1182 <src_float_to_short_array+0x72>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movw   $0x7fff,(%rax,%rcx,2)
jmp    1123 <src_float_to_short_array+0x13>
movsd  0xe86(%rip),%xmm0
ucomisd -0x20(%rbp),%xmm0
jb     11a8 <src_float_to_short_array+0x98>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movw   $0x8000,(%rax,%rcx,2)
jmp    1123 <src_float_to_short_array+0x13>
movsd  -0x20(%rbp),%xmm0
call   1030 <lrint@plt>
sar    $0x10,%eax
mov    %ax,%dx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dx,(%rax,%rcx,2)
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     11b2 <src_float_to_short_array+0xa2>
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
movsd  0xebd(%rip),%xmm1
movsd  0xebd(%rip),%xmm2
movsd  0xebd(%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     11b2 <src_float_to_short_array+0xa2>
movss  (%r12,%rbx,4),%xmm0
cvtss2sd %xmm0,%xmm0
mulsd  %xmm1,%xmm0
mov    $0x7fff,%ax
ucomisd %xmm2,%xmm0
jae    1160 <src_float_to_short_array+0x50>
mov    $0x8000,%ax
ucomisd %xmm0,%xmm3
jae    1160 <src_float_to_short_array+0x50>
call   1030 <lrint@plt>
movsd  0xe73(%rip),%xmm3
movsd  0xe63(%rip),%xmm2
movsd  0xe53(%rip),%xmm1
shr    $0x10,%eax
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 double
store float* %0, float** %4
store i16* %1, i16** %5
store i32 %2, i32* %6
br label %8
8: ; preds = %36, %31, %23, %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 = fpext float %18 to double
%20 = fmul double %19, 0x41E0000000000000
store double %20, double* %7
%21 = load double, double* %7
%22 = fcmp oge double %21, 0x41DFFFFFFFC00000
br i1 %22, label %23, label %28
23: ; preds = %11
%24 = load i16*, i16** %5
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i16, i16* %24, i64 %26
store i16 32767, i16* %27
br label %8
28: ; preds = %11
%29 = load double, double* %7
%30 = fcmp ole double %29, 0xC1E0000000000000
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i16*, i16** %5
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i16, i16* %32, i64 %34
store i16 -32768, i16* %35
br label %8
36: ; preds = %28
%37 = load double, double* %7
%38 = call i32 @lrint(double %37)
%39 = ashr i32 %38, 16
%40 = trunc i32 %39 to i16
%41 = load i16*, i16** %5
%42 = load i32, i32* %6
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i16, i16* %41, i64 %43
store i16 %40, i16* %44
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 %26, label %5
5: ; preds = %3
%6 = sext i32 %2 to i64
br label %7
7: ; preds = %5, %15
%8 = phi i64 [ %6, %5 ], [ %9, %15 ]
%9 = add nsw i64 %8, -1
%10 = getelementptr inbounds float, float* %0, i64 %9
%11 = load float, float* %10
%12 = fpext float %11 to double
%13 = fmul double %12, 0x41E0000000000000
%14 = fcmp ult double %13, 0x41DFFFFFFFC00000
br i1 %14, label %20, label %15
15: ; preds = %7, %20, %22
%16 = phi i16 [ %25, %22 ], [ -32768, %20 ], [ 32767, %7 ]
%17 = getelementptr inbounds i16, i16* %1, i64 %9
store i16 %16, i16* %17
%18 = trunc i64 %9 to i32
%19 = icmp eq i32 %18, 0
br i1 %19, label %26, label %7
20: ; preds = %7
%21 = fcmp ugt double %13, 0xC1E0000000000000
br i1 %21, label %22, label %15
22: ; preds = %20
%23 = tail call i32 @lrint(double %13)
%24 = lshr i32 %23, 16
%25 = trunc i32 %24 to i16
br label %15
26: ; preds = %15, %3
ret void
}