#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 */
float PaConvert_TriangularDither();
void PaConvert_Float32_Int16_Dither(float *sourceBuffer, int sourceStride,
                                    short *targetBuffer, int targetStride,
                                    int numSamples) {
  int i;
  for (i = 0; i < numSamples; i++) {
    float dither = PaConvert_TriangularDither() * (1.0f / ((1 << (15)) - 1));
    float dithered = (*sourceBuffer * (32766.0f)) + dither;
    *targetBuffer = (short)dithered;
    sourceBuffer += sourceStride;
    targetBuffer += targetStride;
  }
}


x86 assembly (clang, -O0):
<PaConvert_Float32_Int16_Dither>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    11c2 <PaConvert_Float32_Int16_Dither+0xb2>
xor    %eax,%eax
call   1030 <PaConvert_TriangularDither@plt>
movss  0xeb4(%rip),%xmm1
mulss  %xmm1,%xmm0
movss  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
movss  -0x28(%rbp),%xmm1
movss  0xe9a(%rip),%xmm2
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x2c(%rbp)
movss  -0x2c(%rbp),%xmm0
cvttss2si %xmm0,%eax
mov    %ax,%cx
mov    -0x18(%rbp),%rax
mov    %cx,(%rax)
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
shl    $0x1,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1131 <PaConvert_Float32_Int16_Dither+0x21>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PaConvert_Float32_Int16_Dither>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %r8d,%r8d
jle    1173 <PaConvert_Float32_Int16_Dither+0x63>
mov    %r8d,%r14d
mov    %rdx,%rbx
mov    %rdi,%rbp
movslq %esi,%r15
movslq %ecx,%r12
shl    $0x2,%r15
add    %r12,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1030 <PaConvert_TriangularDither@plt>
mulss  0xeb1(%rip),%xmm0
movss  0x0(%rbp),%xmm1
mulss  0xea8(%rip),%xmm1
addss  %xmm0,%xmm1
cvttss2si %xmm1,%eax
mov    %ax,(%rbx)
add    %r15,%rbp
add    %r12,%rbx
add    $0xffffffff,%r14d
jne    1140 <PaConvert_Float32_Int16_Dither+0x30>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @PaConvert_Float32_Int16_Dither(float* %0, i32 %1, i16* %2, i32 %3, i32 %4) {
%6 = alloca float*
%7 = alloca i32
%8 = alloca i16*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca float
%13 = alloca float
store float* %0, float** %6
store i32 %1, i32* %7
store i16* %2, i16** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
store i32 0, i32* %11
br label %14
14: ; preds = %36, %5
%15 = load i32, i32* %11
%16 = load i32, i32* %10
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %39
18: ; preds = %14
%19 = call float (...) @PaConvert_TriangularDither()
%20 = fmul float %19, 0x3F00002000000000
store float %20, float* %12
%21 = load float*, float** %6
%22 = load float, float* %21
%23 = load float, float* %12
%24 = call float @llvm.fmuladd.f32(float %22, float 3.276600e+04, float %23)
store float %24, float* %13
%25 = load float, float* %13
%26 = fptosi float %25 to i16
%27 = load i16*, i16** %8
store i16 %26, i16* %27
%28 = load i32, i32* %7
%29 = load float*, float** %6
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds float, float* %29, i64 %30
store float* %31, float** %6
%32 = load i32, i32* %9
%33 = load i16*, i16** %8
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i16, i16* %33, i64 %34
store i16* %35, i16** %8
br label %36
36: ; preds = %18
%37 = load i32, i32* %11
%38 = add nsw i32 %37, 1
store i32 %38, i32* %11
br label %14
39: ; preds = %14
ret void
}

LLVM IR (-O2):
define void @PaConvert_Float32_Int16_Dither(float* nocapture %0, i32 %1, i16* nocapture writeonly %2, i32 %3, i32 %4) {
%6 = icmp sgt i32 %4, 0
br i1 %6, label %7, label %23
7: ; preds = %5
%8 = sext i32 %1 to i64
%9 = sext i32 %3 to i64
br label %10
10: ; preds = %7, %10
%11 = phi float* [ %0, %7 ], [ %19, %10 ]
%12 = phi i16* [ %2, %7 ], [ %20, %10 ]
%13 = phi i32 [ 0, %7 ], [ %21, %10 ]
%14 = tail call float (...) @PaConvert_TriangularDither()
%15 = fmul float %14, 0x3F00002000000000
%16 = load float, float* %11
%17 = tail call float @llvm.fmuladd.f32(float %16, float 3.276600e+04, float %15)
%18 = fptosi float %17 to i16
store i16 %18, i16* %12
%19 = getelementptr inbounds float, float* %11, i64 %8
%20 = getelementptr inbounds i16, i16* %12, i64 %9
%21 = add nuw nsw i32 %13, 1
%22 = icmp eq i32 %21, %4
br i1 %22, label %23, label %10
23: ; preds = %10, %5
ret void
}