#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 */
short lrint(double const);
void d2bes_array(const double *src, short *dest, int count, int normalize) {
  unsigned char *ucptr;
  short value;
  double normfact;
  normfact = normalize ? (1.0 * 0x7FFF) : 1.0;
  ucptr = ((unsigned char *)dest) + 2 * count;
  while (--count >= 0) {
    ucptr -= 2;
    value = lrint(src[count] * normfact);
    ucptr[1] = value;
    ucptr[0] = value >> 8;
  };
}


x86 assembly (clang, -O0):
<d2bes_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x18(%rbp),%eax
movsd  0xecf(%rip),%xmm0
movsd  0xecf(%rip),%xmm1
movsd  %xmm1,-0x40(%rbp)
cmp    $0x0,%eax
movsd  %xmm0,-0x38(%rbp)
jne    1156 <d2bes_array+0x46>
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jl     11ce <d2bes_array+0xbe>
mov    -0x20(%rbp),%rax
add    $0xfffffffffffffffe,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mulsd  -0x30(%rbp),%xmm0
call   1030 <lrint@plt>
mov    %ax,-0x22(%rbp)
mov    -0x22(%rbp),%ax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,0x1(%rax)
movswl -0x22(%rbp),%eax
sar    $0x8,%eax
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
jmp    1174 <d2bes_array+0x64>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<d2bes_array>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
test   %ecx,%ecx
sete   %cl
test   %edx,%edx
jle    117d <d2bes_array+0x6d>
mov    %rsi,%r14
mov    %rdi,%r15
mov    %cl,%al
lea    0xecd(%rip),%rcx
movsd  (%rcx,%rax,8),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %edx,%ebx
add    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%eax
movsd  (%r15,%rax,8),%xmm0
mulsd  0x8(%rsp),%xmm0
call   1030 <lrint@plt>
rol    $0x8,%ax
mov    %ax,(%r14,%rbx,2)
lea    -0x1(%rbx),%rax
add    $0x1,%rbx
cmp    $0x1,%rbx
mov    %rax,%rbx
ja     1150 <d2bes_array+0x40>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @d2bes_array(double* %0, i16* %1, i32 %2, i32 %3) {
%5 = alloca double*
%6 = alloca i16*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i16
%11 = alloca double
store double* %0, double** %5
store i16* %1, i16** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i32, i32* %8
%13 = icmp ne i32 %12, 0
%14 = zext i1 %13 to i64
%15 = select i1 %13, double 3.276700e+04, double 1.000000e+00
store double %15, double* %11
%16 = load i16*, i16** %6
%17 = bitcast i16* %16 to i8*
%18 = load i32, i32* %7
%19 = mul nsw i32 2, %18
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
store i8* %21, i8** %9
br label %22
22: ; preds = %26, %4
%23 = load i32, i32* %7
%24 = add nsw i32 %23, -1
store i32 %24, i32* %7
%25 = icmp sge i32 %24, 0
br i1 %25, label %26, label %47
26: ; preds = %22
%27 = load i8*, i8** %9
%28 = getelementptr inbounds i8, i8* %27, i64 -2
store i8* %28, i8** %9
%29 = load double*, double** %5
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds double, double* %29, i64 %31
%33 = load double, double* %32
%34 = load double, double* %11
%35 = fmul double %33, %34
%36 = call signext i16 @lrint(double %35)
store i16 %36, i16* %10
%37 = load i16, i16* %10
%38 = trunc i16 %37 to i8
%39 = load i8*, i8** %9
%40 = getelementptr inbounds i8, i8* %39, i64 1
store i8 %38, i8* %40
%41 = load i16, i16* %10
%42 = sext i16 %41 to i32
%43 = ashr i32 %42, 8
%44 = trunc i32 %43 to i8
%45 = load i8*, i8** %9
%46 = getelementptr inbounds i8, i8* %45, i64 0
store i8 %44, i8* %46
br label %22
47: ; preds = %22
ret void
}

LLVM IR (-O2):
define void @d2bes_array(double* nocapture %0, i16* nocapture writeonly %1, i32 %2, i32 %3) {
%5 = icmp eq i32 %3, 0
%6 = select i1 %5, double 1.000000e+00, double 3.276700e+04
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %28
8: ; preds = %4
%9 = zext i32 %2 to i64
%10 = getelementptr inbounds i16, i16* %1, i64 %9
%11 = bitcast i16* %10 to i8*
%12 = zext i32 %2 to i64
br label %13
13: ; preds = %8, %13
%14 = phi i64 [ %12, %8 ], [ %16, %13 ]
%15 = phi i8* [ %11, %8 ], [ %17, %13 ]
%16 = add nsw i64 %14, -1
%17 = getelementptr inbounds i8, i8* %15, i64 -2
%18 = and i64 %16, 4294967295
%19 = getelementptr inbounds double, double* %0, i64 %18
%20 = load double, double* %19
%21 = fmul double %6, %20
%22 = tail call signext i16 @lrint(double %21)
%23 = trunc i16 %22 to i8
%24 = getelementptr inbounds i8, i8* %15, i64 -1
store i8 %23, i8* %24
%25 = lshr i16 %22, 8
%26 = trunc i16 %25 to i8
store i8 %26, i8* %17
%27 = icmp ugt i64 %14, 1
br i1 %27, label %13, label %28
28: ; preds = %13, %4
ret void
}