#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__ signbit(double const);
void float_to_16_int(unsigned total_samples, const float float_samples[],
                     int int_samples[]) {
  for (; total_samples; total_samples--) {
    const register double d = float_samples[0];
    const int value = d * (signbit(d) ? -(-32768) : 32767);
    int_samples[0] = ((((value) > (-32768) ? (value) : (-32768))) < (32767)
                          ? (((value) > (-32768) ? (value) : (-32768)))
                          : (32767));
    float_samples += 1;
    int_samples += 1;
  }
}


x86 assembly (clang, -O0):
<float_to_16_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1219 <float_to_16_int+0x109>
mov    -0x10(%rbp),%rax
movss  (%rax),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1030 <signbit@plt>
movsd  -0x30(%rbp),%xmm0
mov    %rax,%rdx
mov    $0x7fff,%eax
mov    $0x8000,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
cvtsi2sd %eax,%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0xffff8000,-0x24(%rbp)
jle    1192 <float_to_16_int+0x82>
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    119f <float_to_16_int+0x8f>
mov    $0xffff8000,%eax
mov    %eax,-0x34(%rbp)
jmp    119f <float_to_16_int+0x8f>
mov    -0x34(%rbp),%eax
cmp    $0x7fff,%eax
jge    11dd <float_to_16_int+0xcd>
cmpl   $0xffff8000,-0x24(%rbp)
jle    11c5 <float_to_16_int+0xb5>
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    11d2 <float_to_16_int+0xc2>
mov    $0xffff8000,%eax
mov    %eax,-0x38(%rbp)
jmp    11d2 <float_to_16_int+0xc2>
mov    -0x38(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    11ea <float_to_16_int+0xda>
mov    $0x7fff,%eax
mov    %eax,-0x3c(%rbp)
jmp    11ea <float_to_16_int+0xda>
mov    -0x3c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4(%rbp)
jmp    1123 <float_to_16_int+0x13>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<float_to_16_int>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
je     1194 <float_to_16_int+0x84>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebx,%ebx
lea    0xecd(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movss  (%r15,%rbx,4),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
call   1030 <signbit@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
movsd  0x8(%rsp),%xmm1
mulsd  0x0(%r13,%rcx,8),%xmm1
movsd  0xe9e(%rip),%xmm0
maxsd  %xmm1,%xmm0
movsd  0xe9a(%rip),%xmm1
minsd  %xmm0,%xmm1
cvttsd2si %xmm1,%eax
cwtl
mov    %eax,(%r14,%rbx,4)
add    $0x1,%rbx
cmp    %ebx,%r12d
jne    1140 <float_to_16_int+0x30>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @float_to_16_int(i32 %0, float* %1, i32* %2) {
%4 = alloca i32
%5 = alloca float*
%6 = alloca i32*
%7 = alloca double
%8 = alloca i32
store i32 %0, i32* %4
store float* %1, float** %5
store i32* %2, i32** %6
br label %9
9: ; preds = %51, %3
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %54
12: ; preds = %9
%13 = load float*, float** %5
%14 = getelementptr inbounds float, float* %13, i64 0
%15 = load float, float* %14
%16 = fpext float %15 to double
store double %16, double* %7
%17 = load double, double* %7
%18 = load double, double* %7
%19 = call i64 @signbit(double %18)
%20 = icmp ne i64 %19, 0
%21 = zext i1 %20 to i64
%22 = select i1 %20, i32 32768, i32 32767
%23 = sitofp i32 %22 to double
%24 = fmul double %17, %23
%25 = fptosi double %24 to i32
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp sgt i32 %26, -32768
br i1 %27, label %28, label %30
28: ; preds = %12
%29 = load i32, i32* %8
br label %31
30: ; preds = %12
br label %31
31: ; preds = %30, %28
%32 = phi i32 [ %29, %28 ], [ -32768, %30 ]
%33 = icmp slt i32 %32, 32767
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = load i32, i32* %8
%36 = icmp sgt i32 %35, -32768
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = load i32, i32* %8
br label %40
39: ; preds = %34
br label %40
40: ; preds = %39, %37
%41 = phi i32 [ %38, %37 ], [ -32768, %39 ]
br label %43
42: ; preds = %31
br label %43
43: ; preds = %42, %40
%44 = phi i32 [ %41, %40 ], [ 32767, %42 ]
%45 = load i32*, i32** %6
%46 = getelementptr inbounds i32, i32* %45, i64 0
store i32 %44, i32* %46
%47 = load float*, float** %5
%48 = getelementptr inbounds float, float* %47, i64 1
store float* %48, float** %5
%49 = load i32*, i32** %6
%50 = getelementptr inbounds i32, i32* %49, i64 1
store i32* %50, i32** %6
br label %51
51: ; preds = %43
%52 = load i32, i32* %4
%53 = add i32 %52, -1
store i32 %53, i32* %4
br label %9
54: ; preds = %9
ret void
}

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