#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_ClipDither(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;
    long samp = (long)dithered;
    {
      samp = ((samp) < (-0x8000)) ? -0x8000
                                  : (((samp) < (0x7FFF)) ? (0x7FFF) : (samp));
    };
    *targetBuffer = (short)samp;
    sourceBuffer += sourceStride;
    targetBuffer += targetStride;
  }
}


x86 assembly (clang, -O0):
<PaConvert_Float32_Int16_ClipDither>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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    1219 <PaConvert_Float32_Int16_ClipDither+0x109>
mov    $0x0,%al
call   1030 <PaConvert_TriangularDither@plt>
movss  0xeb8(%rip),%xmm1
mulss  %xmm1,%xmm0
movss  %xmm0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
movss  (%rax),%xmm0
movss  -0x28(%rbp),%xmm1
movss  0xe96(%rip),%xmm2
mulss  %xmm2,%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x2c(%rbp)
cvttss2si -0x2c(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0xffffffffffff8000,-0x38(%rbp)
ff
jge    119f <PaConvert_Float32_Int16_ClipDither+0x8f>
mov    $0xffffffffffff8000,%rax
mov    %rax,-0x40(%rbp)
jmp    11cb <PaConvert_Float32_Int16_ClipDither+0xbb>
cmpq   $0x7fff,-0x38(%rbp)
jge    11bb <PaConvert_Float32_Int16_ClipDither+0xab>
mov    $0x7fff,%eax
mov    %rax,-0x48(%rbp)
jmp    11c3 <PaConvert_Float32_Int16_ClipDither+0xb3>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
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_ClipDither+0x21>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PaConvert_Float32_Int16_ClipDither>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %r8d,%r8d
jle    1194 <PaConvert_Float32_Int16_ClipDither+0x84>
mov    %r8d,%r14d
mov    %rdx,%rbx
mov    %rdi,%rbp
movslq %esi,%r15
movslq %ecx,%r12
shl    $0x2,%r15
add    %r12,%r12
jmp    114f <PaConvert_Float32_Int16_ClipDither+0x3f>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %dx,(%rbx)
add    %r15,%rbp
add    %r12,%rbx
add    $0xffffffff,%r14d
je     1194 <PaConvert_Float32_Int16_ClipDither+0x84>
xor    %eax,%eax
call   1030 <PaConvert_TriangularDither@plt>
mulss  0xea2(%rip),%xmm0
movss  0x0(%rbp),%xmm1
mulss  0xe99(%rip),%xmm1
addss  %xmm0,%xmm1
cvttss2si %xmm1,%rax
mov    %eax,%ecx
cmp    $0x8000,%rax
jge    1183 <PaConvert_Float32_Int16_ClipDither+0x73>
mov    $0x7fff,%ecx
mov    $0x8000,%edx
cmp    $0xffffffffffff8000,%rax
jl     1140 <PaConvert_Float32_Int16_ClipDither+0x30>
mov    %ecx,%edx
jmp    1140 <PaConvert_Float32_Int16_ClipDither+0x30>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @PaConvert_Float32_Int16_ClipDither(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
%14 = alloca i64
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 %15
15: ; preds = %52, %5
%16 = load i32, i32* %11
%17 = load i32, i32* %10
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %55
19: ; preds = %15
%20 = call float (...) @PaConvert_TriangularDither()
%21 = fmul float %20, 0x3F00002000000000
store float %21, float* %12
%22 = load float*, float** %6
%23 = load float, float* %22
%24 = load float, float* %12
%25 = call float @llvm.fmuladd.f32(float %23, float 3.276600e+04, float %24)
store float %25, float* %13
%26 = load float, float* %13
%27 = fptosi float %26 to i64
store i64 %27, i64* %14
%28 = load i64, i64* %14
%29 = icmp slt i64 %28, -32768
br i1 %29, label %30, label %31
30: ; preds = %19
br label %39
31: ; preds = %19
%32 = load i64, i64* %14
%33 = icmp slt i64 %32, 32767
br i1 %33, label %34, label %35
34: ; preds = %31
br label %37
35: ; preds = %31
%36 = load i64, i64* %14
br label %37
37: ; preds = %35, %34
%38 = phi i64 [ 32767, %34 ], [ %36, %35 ]
br label %39
39: ; preds = %37, %30
%40 = phi i64 [ -32768, %30 ], [ %38, %37 ]
store i64 %40, i64* %14
%41 = load i64, i64* %14
%42 = trunc i64 %41 to i16
%43 = load i16*, i16** %8
store i16 %42, i16* %43
%44 = load i32, i32* %7
%45 = load float*, float** %6
%46 = sext i32 %44 to i64
%47 = getelementptr inbounds float, float* %45, i64 %46
store float* %47, float** %6
%48 = load i32, i32* %9
%49 = load i16*, i16** %8
%50 = sext i32 %48 to i64
%51 = getelementptr inbounds i16, i16* %49, i64 %50
store i16* %51, i16** %8
br label %52
52: ; preds = %39
%53 = load i32, i32* %11
%54 = add nsw i32 %53, 1
store i32 %54, i32* %11
br label %15
55: ; preds = %15
ret void
}

LLVM IR (-O2):
define void @PaConvert_Float32_Int16_ClipDither(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 %28
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 ], [ %24, %10 ]
%12 = phi i16* [ %2, %7 ], [ %25, %10 ]
%13 = phi i32 [ 0, %7 ], [ %26, %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 i64
%19 = icmp slt i64 %18, -32768
%20 = icmp sgt i64 %18, 32767
%21 = select i1 %20, i64 %18, i64 32767
%22 = trunc i64 %21 to i16
%23 = select i1 %19, i16 -32768, i16 %22
store i16 %23, i16* %12
%24 = getelementptr inbounds float, float* %11, i64 %8
%25 = getelementptr inbounds i16, i16* %12, i64 %9
%26 = add nuw nsw i32 %13, 1
%27 = icmp eq i32 %26, %4
br i1 %27, label %28, label %10
28: ; preds = %10, %5
ret void
}