#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 RAND_MAX;
scalar_t__ rand();
scalar_t__ round(int);
void fdither(float *x, int N, int scale) {
  for (int i = 0; i < N; i++) {
    float r = ((float)rand()) / RAND_MAX;
    x[i] += round(scale * 2 * r - 1);
  }
}


x86 assembly (clang, -O0):
<fdither>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ae <fdither+0x8e>
mov    $0x0,%al
call   1040 <rand@plt>
cvtsi2ss %rax,%xmm0
mov    0x2e90(%rip),%rax
divss  (%rax),%xmm0
movss  %xmm0,-0x18(%rbp)
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
cvtsi2ss %eax,%xmm0
movss  -0x18(%rbp),%xmm1
mulss  %xmm1,%xmm0
movss  0xe84(%rip),%xmm1
addss  %xmm1,%xmm0
cvttss2si %xmm0,%edi
call   1030 <round@plt>
cvtsi2ss %rax,%xmm0
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
addss  (%rax,%rcx,4),%xmm0
movss  %xmm0,(%rax,%rcx,4)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1139 <fdither+0x19>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdither>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
jle    1198 <fdither+0x78>
mov    %rdi,%r12
add    %edx,%edx
cvtsi2ss %edx,%xmm0
movss  %xmm0,0x4(%rsp)
mov    %esi,%r14d
mov    0x2ea3(%rip),%r15
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
divss  (%r15),%xmm0
mulss  0x4(%rsp),%xmm0
addss  0xe8e(%rip),%xmm0
cvttss2si %xmm0,%edi
call   1030 <round@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  (%r12,%rbx,4),%xmm0
movss  %xmm0,(%r12,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1150 <fdither+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@RAND_MAX = global float 0.000000e+00
define void @fdither(float* %0, i32 %1, i32 %2) {
%4 = alloca float*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca float
store float* %0, float** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %32, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %5
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %35
13: ; preds = %9
%14 = call i64 (...) @rand()
%15 = sitofp i64 %14 to float
%16 = load float, float* @RAND_MAX
%17 = fdiv float %15, %16
store float %17, float* %8
%18 = load i32, i32* %6
%19 = mul nsw i32 %18, 2
%20 = sitofp i32 %19 to float
%21 = load float, float* %8
%22 = call float @llvm.fmuladd.f32(float %20, float %21, float -1.000000e+00)
%23 = fptosi float %22 to i32
%24 = call i64 @round(i32 %23)
%25 = sitofp i64 %24 to float
%26 = load float*, float** %4
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds float, float* %26, i64 %28
%30 = load float, float* %29
%31 = fadd float %30, %25
store float %31, float* %29
br label %32
32: ; preds = %13
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %9
35: ; preds = %9
ret void
}

LLVM IR (-O2):
@RAND_MAX = global float 0.000000e+00
define void @fdither(float* nocapture %0, i32 %1, i32 %2) {
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = shl nsw i32 %2, 1
%7 = sitofp i32 %6 to float
%8 = zext i32 %1 to i64
br label %10
9: ; preds = %10, %3
ret void
10: ; preds = %5, %10
%11 = phi i64 [ 0, %5 ], [ %23, %10 ]
%12 = tail call i64 (...) @rand()
%13 = sitofp i64 %12 to float
%14 = load float, float* @RAND_MAX
%15 = fdiv float %13, %14
%16 = tail call float @llvm.fmuladd.f32(float %7, float %15, float -1.000000e+00)
%17 = fptosi float %16 to i32
%18 = tail call i64 @round(i32 %17)
%19 = sitofp i64 %18 to float
%20 = getelementptr inbounds float, float* %0, i64 %11
%21 = load float, float* %20
%22 = fadd float %21, %19
store float %22, float* %20
%23 = add nuw nsw i64 %11, 1
%24 = icmp eq i64 %23, %8
br i1 %24, label %9, label %10
}