#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;
typedef int int16_t;

/* Variables and functions */
void resampler_float_to_s16(int16_t *restrict out, const float *restrict in,
                            size_t samples) {
  for (int i = 0; i < (int)samples; i++) {
    int32_t temp = in[i] + 0.5;
    if (temp > 0x7FFE)
      out[i] = 0x7FFE;
    else if (temp < -0x7FFF)
      out[i] = -0x7FFF;
    else
      out[i] = (int16_t)temp;
  }
}


x86 assembly (clang, -O0):
<resampler_float_to_s16>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
jge    11b2 <resampler_float_to_s16+0xb2>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xec1(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x7ffe,-0x20(%rbp)
jle    116b <resampler_float_to_s16+0x6b>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movl   $0x7ffe,(%rax,%rcx,4)
jmp    119f <resampler_float_to_s16+0x9f>
cmpl   $0xffff8001,-0x20(%rbp)
jge    118c <resampler_float_to_s16+0x8c>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movl   $0xffff8001,(%rax,%rcx,4)
jmp    119a <resampler_float_to_s16+0x9a>
mov    -0x20(%rbp),%edx
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    119f <resampler_float_to_s16+0x9f>
jmp    11a4 <resampler_float_to_s16+0xa4>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1117 <resampler_float_to_s16+0x17>
pop    %rbp
ret

x86 assembly (clang, -O2):
<resampler_float_to_s16>:
test   %edx,%edx
jle    1165 <resampler_float_to_s16+0x65>
mov    %edx,%eax
xor    %ecx,%ecx
movsd  0xef0(%rip),%xmm0
jmp    1130 <resampler_float_to_s16+0x30>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movl   $0x7ffe,(%rdi,%rcx,4)
add    $0x1,%rcx
cmp    %rcx,%rax
je     1165 <resampler_float_to_s16+0x65>
movss  (%rsi,%rcx,4),%xmm1
cvtss2sd %xmm1,%xmm1
addsd  %xmm0,%xmm1
cvttsd2si %xmm1,%edx
cmp    $0x7fff,%edx
jge    1120 <resampler_float_to_s16+0x20>
cmp    $0xffff8000,%edx
jg     1160 <resampler_float_to_s16+0x60>
movl   $0xffff8001,(%rdi,%rcx,4)
jmp    1127 <resampler_float_to_s16+0x27>
nopw   0x0(%rax,%rax,1)
mov    %edx,(%rdi,%rcx,4)
jmp    1127 <resampler_float_to_s16+0x27>
ret

LLVM IR (-O0):
define void @resampler_float_to_s16(i32* noalias %0, float* noalias %1, i64 %2) {
%4 = alloca i32*
%5 = alloca float*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store float* %1, float** %5
store i64 %2, i64* %6
store i32 0, i32* %7
br label %9
9: ; preds = %46, %3
%10 = load i32, i32* %7
%11 = load i64, i64* %6
%12 = trunc i64 %11 to i32
%13 = icmp slt i32 %10, %12
br i1 %13, label %14, label %49
14: ; preds = %9
%15 = load float*, float** %5
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds float, float* %15, i64 %17
%19 = load float, float* %18
%20 = fpext float %19 to double
%21 = fadd double %20, 5.000000e-01
%22 = fptosi double %21 to i32
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp sgt i32 %23, 32766
br i1 %24, label %25, label %30
25: ; preds = %14
%26 = load i32*, i32** %4
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
store i32 32766, i32* %29
br label %45
30: ; preds = %14
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, -32767
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i32*, i32** %4
%35 = load i32, i32* %7
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
store i32 -32767, i32* %37
br label %44
38: ; preds = %30
%39 = load i32, i32* %8
%40 = load i32*, i32** %4
%41 = load i32, i32* %7
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32, i32* %40, i64 %42
store i32 %39, i32* %43
br label %44
44: ; preds = %38, %33
br label %45
45: ; preds = %44, %25
br label %46
46: ; preds = %45
%47 = load i32, i32* %7
%48 = add nsw i32 %47, 1
store i32 %48, i32* %7
br label %9
49: ; preds = %9
ret void
}

LLVM IR (-O2):
define void @resampler_float_to_s16(i32* noalias nocapture writeonly %0, float* noalias nocapture %1, i64 %2) {
%4 = trunc i64 %2 to i32
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = and i64 %2, 4294967295
br label %9
8: ; preds = %24, %3
ret void
9: ; preds = %6, %24
%10 = phi i64 [ 0, %6 ], [ %25, %24 ]
%11 = getelementptr inbounds float, float* %1, i64 %10
%12 = load float, float* %11
%13 = fpext float %12 to double
%14 = fadd double %13, 5.000000e-01
%15 = fptosi double %14 to i32
%16 = icmp sgt i32 %15, 32766
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = getelementptr inbounds i32, i32* %0, i64 %10
store i32 32766, i32* %18
br label %24
19: ; preds = %9
%20 = icmp slt i32 %15, -32767
%21 = getelementptr inbounds i32, i32* %0, i64 %10
br i1 %20, label %22, label %23
22: ; preds = %19
store i32 -32767, i32* %21
br label %24
23: ; preds = %19
store i32 %15, i32* %21
br label %24
24: ; preds = %22, %23, %17
%25 = add nuw nsw i64 %10, 1
%26 = icmp eq i64 %25, %7
br i1 %26, label %8, label %9
}