#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_ClipDither(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;
    {
      samp = ((samp) < (-0x80)) ? -0x80 : (((samp) < (0x7F)) ? (0x7F) : (samp));
    };
    *targetBuffer = (char)samp;
    sourceBuffer += sourceStride;
    targetBuffer += targetStride;
  }
}


x86 assembly (clang, -O0):
<PaConvert_Float32_Int8_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    120d <PaConvert_Float32_Int8_ClipDither+0xfd>
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   $0xffffffffffffff80,-0x38(%rbp)
jge    119c <PaConvert_Float32_Int8_ClipDither+0x8c>
mov    $0xffffffffffffff80,%rax
mov    %rax,-0x40(%rbp)
jmp    11c5 <PaConvert_Float32_Int8_ClipDither+0xb5>
cmpq   $0x7f,-0x38(%rbp)
jge    11b5 <PaConvert_Float32_Int8_ClipDither+0xa5>
mov    $0x7f,%eax
mov    %rax,-0x48(%rbp)
jmp    11bd <PaConvert_Float32_Int8_ClipDither+0xad>
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    %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_ClipDither+0x21>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PaConvert_Float32_Int8_ClipDither>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %r8d,%r8d
jle    118f <PaConvert_Float32_Int8_ClipDither+0x7f>
mov    %r8d,%r14d
mov    %rdx,%rbx
mov    %rdi,%rbp
movslq %esi,%r15
movslq %ecx,%r12
shl    $0x2,%r15
jmp    114e <PaConvert_Float32_Int8_ClipDither+0x3e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %dl,(%rbx)
add    %r12,%rbx
add    %r15,%rbp
add    $0xffffffff,%r14d
je     118f <PaConvert_Float32_Int8_ClipDither+0x7f>
xor    %eax,%eax
call   1030 <PaConvert_TriangularDither@plt>
mulss  0xea3(%rip),%xmm0
movss  0x0(%rbp),%xmm1
mulss  0xe9a(%rip),%xmm1
addss  %xmm0,%xmm1
cvttss2si %xmm1,%rax
mov    %eax,%ecx
cmp    $0x7f,%rax
jg     1180 <PaConvert_Float32_Int8_ClipDither+0x70>
mov    $0x7f,%ecx
mov    $0x80,%edx
cmp    $0xffffffffffffff80,%rax
jl     1140 <PaConvert_Float32_Int8_ClipDither+0x30>
mov    %ecx,%edx
jmp    1140 <PaConvert_Float32_Int8_ClipDither+0x30>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @PaConvert_Float32_Int8_ClipDither(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 = %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 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 = icmp slt i64 %28, -128
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, 127
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 [ 127, %34 ], [ %36, %35 ]
br label %39
39: ; preds = %37, %30
%40 = phi i64 [ -128, %30 ], [ %38, %37 ]
store i64 %40, i64* %14
%41 = load i64, i64* %14
%42 = trunc i64 %41 to i8
%43 = load i8*, i8** %8
store i8 %42, i8* %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 i8*, i8** %8
%50 = sext i32 %48 to i64
%51 = getelementptr inbounds i8, i8* %49, i64 %50
store i8* %51, i8** %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_Int8_ClipDither(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 %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 i8* [ %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 1.260000e+02, float %15)
%18 = fptosi float %17 to i64
%19 = icmp slt i64 %18, -128
%20 = icmp sgt i64 %18, 127
%21 = select i1 %20, i64 %18, i64 127
%22 = trunc i64 %21 to i8
%23 = select i1 %19, i8 -128, i8 %22
store i8 %23, i8* %12
%24 = getelementptr inbounds float, float* %11, i64 %8
%25 = getelementptr inbounds i8, i8* %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
}