#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 */
double next_gauss();
int addNoise(int sample, double noise) {
  int byteNoise = 0;
  int noiseSample = 0;
  byteNoise = (int)(noise * next_gauss());
  noiseSample = sample + byteNoise;
  noiseSample = (noiseSample < 0 ? 0 : (noiseSample > 255 ? 255 : noiseSample));
  return noiseSample;
}


x86 assembly (clang, -O0):
<addNoise>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movsd  %xmm0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    $0x0,%al
call   1030 <next_gauss@plt>
movaps %xmm0,%xmm1
movsd  -0x20(%rbp),%xmm0
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    116f <addNoise+0x5f>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    1195 <addNoise+0x85>
cmpl   $0xff,-0x18(%rbp)
jle    1189 <addNoise+0x79>
mov    $0xff,%eax
mov    %eax,-0x28(%rbp)
jmp    118f <addNoise+0x7f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addNoise>:
push   %rbp
push   %rbx
push   %rax
movsd  %xmm0,(%rsp)
mov    %edi,%ebx
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <next_gauss@plt>
mulsd  (%rsp),%xmm0
cvttsd2si %xmm0,%ecx
add    %ebx,%ecx
cmp    $0xff,%ecx
mov    $0xff,%eax
cmovl  %ecx,%eax
test   %eax,%eax
cmovs  %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @addNoise(i32 %0, double %1) {
%3 = alloca i32
%4 = alloca double
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store double %1, double* %4
store i32 0, i32* %5
store i32 0, i32* %6
%7 = load double, double* %4
%8 = call double (...) @next_gauss()
%9 = fmul double %7, %8
%10 = fptosi double %9 to i32
store i32 %10, i32* %5
%11 = load i32, i32* %3
%12 = load i32, i32* %5
%13 = add nsw i32 %11, %12
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
br label %25
17: ; preds = %2
%18 = load i32, i32* %6
%19 = icmp sgt i32 %18, 255
br i1 %19, label %20, label %21
20: ; preds = %17
br label %23
21: ; preds = %17
%22 = load i32, i32* %6
br label %23
23: ; preds = %21, %20
%24 = phi i32 [ 255, %20 ], [ %22, %21 ]
br label %25
25: ; preds = %23, %16
%26 = phi i32 [ 0, %16 ], [ %24, %23 ]
store i32 %26, i32* %6
%27 = load i32, i32* %6
ret i32 %27
}

LLVM IR (-O2):
define i32 @addNoise(i32 %0, double %1) {
%3 = tail call double (...) @next_gauss()
%4 = fmul double %3, %1
%5 = fptosi double %4 to i32
%6 = add nsw i32 %5, %0
%7 = icmp slt i32 %6, 255
%8 = select i1 %7, i32 %6, i32 255
%9 = icmp sgt i32 %8, 0
%10 = select i1 %9, i32 %8, i32 0
ret i32 %10
}