#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 normal_rand(double, double);
unsigned char addnoise(int sym, double amp, double gain, double offset,
                       int clip) {
  int sample;
  sample = offset + gain * normal_rand(sym ? amp : -amp, 1.0);
  if (sample < 0)
    sample = 0;
  else if (sample > clip)
    sample = clip;
  return sample;
}


x86 assembly (clang, -O0):
<addnoise>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  %xmm2,-0x20(%rbp)
mov    %esi,-0x24(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     115a <addnoise+0x4a>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
jmp    117b <addnoise+0x6b>
movsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  0xe78(%rip),%xmm1
call   1030 <normal_rand@plt>
movsd  -0x38(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x30(%rbp),%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11bf <addnoise+0xaf>
movl   $0x0,-0x28(%rbp)
jmp    11d6 <addnoise+0xc6>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jle    11d1 <addnoise+0xc1>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    11d6 <addnoise+0xc6>
mov    -0x28(%rbp),%eax
movzbl %al,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addnoise>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
movsd  %xmm2,0x8(%rsp)
movsd  %xmm1,(%rsp)
test   %edi,%edi
jne    112e <addnoise+0x1e>
xorpd  0xed2(%rip),%xmm0
movsd  0xeda(%rip),%xmm1
call   1030 <normal_rand@plt>
movsd  (%rsp),%xmm1
mulsd  %xmm0,%xmm1
addsd  0x8(%rsp),%xmm1
cvttsd2si %xmm1,%ecx
cmp    %ebx,%ecx
cmovle %ecx,%ebx
xor    %eax,%eax
test   %ecx,%ecx
cmovns %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define zeroext i8 @addnoise(i32 %0, double %1, double %2, double %3, i32 %4) {
%6 = alloca i32
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store double %1, double* %7
store double %2, double* %8
store double %3, double* %9
store i32 %4, i32* %10
%12 = load double, double* %9
%13 = load double, double* %8
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %5
%17 = load double, double* %7
br label %21
18: ; preds = %5
%19 = load double, double* %7
%20 = fneg double %19
br label %21
21: ; preds = %18, %16
%22 = phi double [ %17, %16 ], [ %20, %18 ]
%23 = call double @normal_rand(double %22, double 1.000000e+00)
%24 = call double @llvm.fmuladd.f64(double %13, double %23, double %12)
%25 = fptosi double %24 to i32
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %21
store i32 0, i32* %11
br label %36
29: ; preds = %21
%30 = load i32, i32* %11
%31 = load i32, i32* %10
%32 = icmp sgt i32 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = load i32, i32* %10
store i32 %34, i32* %11
br label %35
35: ; preds = %33, %29
br label %36
36: ; preds = %35, %28
%37 = load i32, i32* %11
%38 = trunc i32 %37 to i8
ret i8 %38
}

LLVM IR (-O2):
define zeroext i8 @addnoise(i32 %0, double %1, double %2, double %3, i32 %4) {
%6 = icmp eq i32 %0, 0
%7 = fneg double %1
%8 = select i1 %6, double %7, double %1
%9 = tail call double @normal_rand(double %8, double 1.000000e+00)
%10 = tail call double @llvm.fmuladd.f64(double %2, double %9, double %3)
%11 = fptosi double %10 to i32
%12 = icmp slt i32 %11, 0
%13 = icmp sgt i32 %11, %4
%14 = select i1 %13, i32 %4, i32 %11
%15 = trunc i32 %14 to i8
%16 = select i1 %12, i8 0, i8 %15
ret i8 %16
}