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


x86 assembly (clang, -O0):
<double_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     1215 <double_to_16_int+0x105>
mov    -0x10(%rbp),%rax
movsd  (%rax),%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    118e <double_to_16_int+0x7e>
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    119b <double_to_16_int+0x8b>
mov    $0xffff8000,%eax
mov    %eax,-0x34(%rbp)
jmp    119b <double_to_16_int+0x8b>
mov    -0x34(%rbp),%eax
cmp    $0x7fff,%eax
jge    11d9 <double_to_16_int+0xc9>
cmpl   $0xffff8000,-0x24(%rbp)
jle    11c1 <double_to_16_int+0xb1>
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
jmp    11ce <double_to_16_int+0xbe>
mov    $0xffff8000,%eax
mov    %eax,-0x38(%rbp)
jmp    11ce <double_to_16_int+0xbe>
mov    -0x38(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    11e6 <double_to_16_int+0xd6>
mov    $0x7fff,%eax
mov    %eax,-0x3c(%rbp)
jmp    11e6 <double_to_16_int+0xd6>
mov    -0x3c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
add    $0x8,%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 <double_to_16_int+0x13>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<double_to_16_int>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
je     1190 <double_to_16_int+0x80>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebx,%ebx
lea    0xecd(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  (%r15,%rbx,8),%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  0xea2(%rip),%xmm0
maxsd  %xmm1,%xmm0
movsd  0xe9e(%rip),%xmm1
minsd  %xmm0,%xmm1
cvttsd2si %xmm1,%eax
cwtl
mov    %eax,(%r14,%rbx,4)
add    $0x1,%rbx
cmp    %ebx,%r12d
jne    1140 <double_to_16_int+0x30>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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