#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_Int8_Dither(float *sourceBuffer, int sourceStride,
                                   char *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 * (126.0f)) + dither;
    long samp = (long)dithered;
    *targetBuffer = (char)samp;
    sourceBuffer += sourceStride;
    targetBuffer += targetStride;
  }
}


x86 assembly (clang, -O0):
<PaConvert_Float32_Int8_Dither>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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    11c1 <PaConvert_Float32_Int8_Dither+0xb1>
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)
mov    -0x38(%rbp),%rax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%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
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1131 <PaConvert_Float32_Int8_Dither+0x21>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PaConvert_Float32_Int8_Dither>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %r8d,%r8d
jle    1163 <PaConvert_Float32_Int8_Dither+0x53>
mov    %r8d,%r14d
mov    %rdx,%rbx
mov    %rdi,%rbp
movslq %esi,%r15
movslq %ecx,%r12
shl    $0x2,%r15
xor    %eax,%eax
call   1030 <PaConvert_TriangularDither@plt>
mulss  0xec1(%rip),%xmm0
movss  0x0(%rbp),%xmm1
mulss  0xeb8(%rip),%xmm1
addss  %xmm0,%xmm1
cvttss2si %xmm1,%rax
mov    %al,(%rbx)
add    %r12,%rbx
add    %r15,%rbp
add    $0xffffffff,%r14d
jne    1130 <PaConvert_Float32_Int8_Dither+0x20>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @PaConvert_Float32_Int8_Dither(float* %0, i32 %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca float*
%7 = alloca i32
%8 = alloca i8*
%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 i8* %2, i8** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
store i32 0, i32* %11
br label %15
15: ; preds = %39, %5
%16 = load i32, i32* %11
%17 = load i32, i32* %10
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %42
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 1.260000e+02, 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 = trunc i64 %28 to i8
%30 = load i8*, i8** %8
store i8 %29, i8* %30
%31 = load i32, i32* %7
%32 = load float*, float** %6
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds float, float* %32, i64 %33
store float* %34, float** %6
%35 = load i32, i32* %9
%36 = load i8*, i8** %8
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %8
br label %39
39: ; preds = %19
%40 = load i32, i32* %11
%41 = add nsw i32 %40, 1
store i32 %41, i32* %11
br label %15
42: ; preds = %15
ret void
}

LLVM IR (-O2):
define void @PaConvert_Float32_Int8_Dither(float* nocapture %0, i32 %1, i8* nocapture writeonly %2, i32 %3, i32 %4) {
%6 = icmp sgt i32 %4, 0
br i1 %6, label %7, label %24
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 ], [ %20, %10 ]
%12 = phi i8* [ %2, %7 ], [ %21, %10 ]
%13 = phi i32 [ 0, %7 ], [ %22, %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 1.260000e+02, float %15)
%18 = fptosi float %17 to i64
%19 = trunc i64 %18 to i8
store i8 %19, i8* %12
%20 = getelementptr inbounds float, float* %11, i64 %8
%21 = getelementptr inbounds i8, i8* %12, i64 %9
%22 = add nuw nsw i32 %13, 1
%23 = icmp eq i32 %22, %4
br i1 %23, label %24, label %10
24: ; preds = %10, %5
ret void
}