#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 */
int /*<<< orphan*/ assert(int);
double exp(double);
double log(double);
double sqrt(double);
double uniformRandom();
double gammaDev(int shape) {
  double am, e, s, v1, v2, y;
  double gamma;
  assert(shape >= 1);
  if (shape < 6) {
    gamma = 1.0;
    for (int i = 0; i <= shape; i++)
      gamma *= uniformRandom();
    gamma = -(log(gamma));
  } else {
    do {
      do {
        do {
          v1 = uniformRandom();
          v2 = 2.0 * uniformRandom() - 1.0;
        } while (v1 * v1 + v2 * v2 > 1.0);
        y = v2 / v1;
        am = shape - 1;
        s = sqrt(2.0 * am + 1.0);
        gamma = s * y + am;
      } while (gamma <= 0.0);
      e = (1.0 + y * y) * exp(am * log(gamma / am) - s * y);
    } while (uniformRandom() > e);
  }
  return gamma;
}


x86 assembly (clang, -O0):
<gammaDev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x1,-0x4(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpl   $0x6,-0x4(%rbp)
jge    11e0 <gammaDev+0x90>
movsd  0xe8a(%rip),%xmm0
movsd  %xmm0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x4(%rbp),%eax
jg     11b5 <gammaDev+0x65>
mov    $0x0,%al
call   1070 <uniformRandom@plt>
mulsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    118a <gammaDev+0x3a>
movsd  -0x40(%rbp),%xmm0
call   1050 <log@plt>
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x40(%rbp)
jmp    1342 <gammaDev+0x1f2>
jmp    11e5 <gammaDev+0x95>
jmp    11ea <gammaDev+0x9a>
jmp    11ef <gammaDev+0x9f>
mov    $0x0,%al
call   1070 <uniformRandom@plt>
movsd  %xmm0,-0x28(%rbp)
mov    $0x0,%al
call   1070 <uniformRandom@plt>
addsd  %xmm0,%xmm0
movsd  0xdf2(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm2
movsd  -0x30(%rbp),%xmm1
mulsd  -0x30(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  0xdcd(%rip),%xmm1
ucomisd %xmm1,%xmm0
ja     11ef <gammaDev+0x9f>
movsd  -0x30(%rbp),%xmm0
divsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
addsd  %xmm0,%xmm0
movsd  0xd94(%rip),%xmm1
addsd  %xmm1,%xmm0
call   1060 <sqrt@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm2
movsd  -0x10(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x40(%rbp),%xmm0
jae    11ea <gammaDev+0x9a>
movsd  -0x38(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xd46(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x40(%rbp),%xmm0
divsd  -0x10(%rbp),%xmm0
call   1050 <log@plt>
movaps %xmm0,%xmm2
movsd  -0x58(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
mulsd  -0x38(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1040 <exp@plt>
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    $0x0,%al
call   1070 <uniformRandom@plt>
ucomisd -0x18(%rbp),%xmm0
ja     11e5 <gammaDev+0x95>
jmp    1342 <gammaDev+0x1f2>
movsd  -0x40(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gammaDev>:
push   %rbx
sub    $0x40,%rsp
mov    %edi,%ebx
xor    %edi,%edi
test   %ebx,%ebx
setg   %dil
call   1030 <assert@plt>
cmp    $0x6,%ebx
jge    11ad <gammaDev+0x5d>
test   %ebx,%ebx
js     12f4 <gammaDev+0x1a4>
add    $0x1,%ebx
movsd  0xe84(%rip),%xmm0
nopl   0x0(%rax)
movsd  %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1070 <uniformRandom@plt>
movsd  0x10(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm1,0x10(%rsp)
movsd  0x10(%rsp),%xmm0
add    $0xffffffff,%ebx
jne    1180 <gammaDev+0x30>
jmp    12fc <gammaDev+0x1ac>
add    $0xffffffff,%ebx
cvtsi2sd %ebx,%xmm0
movsd  %xmm0,0x20(%rsp)
addsd  %xmm0,%xmm0
addsd  0xe3a(%rip),%xmm0
movsd  %xmm0,0x28(%rsp)
sqrtsd %xmm0,%xmm0
movsd  %xmm0,0x38(%rsp)
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <uniformRandom@plt>
movsd  %xmm0,0x10(%rsp)
xor    %eax,%eax
call   1070 <uniformRandom@plt>
movsd  0x10(%rsp),%xmm4
movapd %xmm0,%xmm3
addsd  %xmm0,%xmm3
addsd  0xdfe(%rip),%xmm3
movapd %xmm3,%xmm0
mulsd  %xmm3,%xmm0
movapd %xmm4,%xmm1
mulsd  %xmm4,%xmm1
addsd  %xmm0,%xmm1
ucomisd 0xdda(%rip),%xmm1
movsd  0xdd2(%rip),%xmm1
ja     11e0 <gammaDev+0x90>
movsd  0x28(%rsp),%xmm0
xorpd  %xmm2,%xmm2
ucomisd %xmm2,%xmm0
xorpd  %xmm0,%xmm0
movsd  0x38(%rsp),%xmm5
jae    1277 <gammaDev+0x127>
movsd  0x28(%rsp),%xmm0
movsd  %xmm3,0x8(%rsp)
call   1060 <sqrt@plt>
movsd  0x10(%rsp),%xmm4
movsd  0x8(%rsp),%xmm3
movsd  0xd91(%rip),%xmm1
movapd %xmm0,%xmm5
xorpd  %xmm0,%xmm0
divsd  %xmm4,%xmm3
mulsd  %xmm3,%xmm5
movapd %xmm5,%xmm2
addsd  0x20(%rsp),%xmm2
ucomisd %xmm2,%xmm0
jae    11e0 <gammaDev+0x90>
mulsd  %xmm3,%xmm3
addsd  %xmm1,%xmm3
movsd  %xmm3,0x8(%rsp)
movapd %xmm2,%xmm0
divsd  0x20(%rsp),%xmm0
movapd %xmm2,0x10(%rsp)
movsd  %xmm5,0x30(%rsp)
call   1050 <log@plt>
mulsd  0x20(%rsp),%xmm0
subsd  0x30(%rsp),%xmm0
call   1040 <exp@plt>
mulsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   1070 <uniformRandom@plt>
movapd 0x10(%rsp),%xmm1
ucomisd 0x8(%rsp),%xmm0
ja     11e0 <gammaDev+0x90>
jmp    130d <gammaDev+0x1bd>
movsd  0xd04(%rip),%xmm0
call   1050 <log@plt>
movapd %xmm0,%xmm1
xorpd  0xd03(%rip),%xmm1
movapd %xmm1,%xmm0
add    $0x40,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define double @gammaDev(i32 %0) {
%2 = alloca i32
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca i32
store i32 %0, i32* %2
%11 = load i32, i32* %2
%12 = icmp sge i32 %11, 1
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* %2
%16 = icmp slt i32 %15, 6
br i1 %16, label %17, label %33
17: ; preds = %1
store double 1.000000e+00, double* %9
store i32 0, i32* %10
br label %18
18: ; preds = %26, %17
%19 = load i32, i32* %10
%20 = load i32, i32* %2
%21 = icmp sle i32 %19, %20
br i1 %21, label %22, label %29
22: ; preds = %18
%23 = call double (...) @uniformRandom()
%24 = load double, double* %9
%25 = fmul double %24, %23
store double %25, double* %9
br label %26
26: ; preds = %22
%27 = load i32, i32* %10
%28 = add nsw i32 %27, 1
store i32 %28, i32* %10
br label %18
29: ; preds = %18
%30 = load double, double* %9
%31 = call double @log(double %30)
%32 = fneg double %31
store double %32, double* %9
br label %86
33: ; preds = %1
br label %34
34: ; preds = %81, %33
br label %35
35: ; preds = %62, %34
br label %36
36: ; preds = %40, %35
%37 = call double (...) @uniformRandom()
store double %37, double* %6
%38 = call double (...) @uniformRandom()
%39 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %38, double -1.000000e+00)
store double %39, double* %7
br label %40
40: ; preds = %36
%41 = load double, double* %6
%42 = load double, double* %6
%43 = load double, double* %7
%44 = load double, double* %7
%45 = fmul double %43, %44
%46 = call double @llvm.fmuladd.f64(double %41, double %42, double %45)
%47 = fcmp ogt double %46, 1.000000e+00
br i1 %47, label %36, label %48
48: ; preds = %40
%49 = load double, double* %7
%50 = load double, double* %6
%51 = fdiv double %49, %50
store double %51, double* %8
%52 = load i32, i32* %2
%53 = sub nsw i32 %52, 1
%54 = sitofp i32 %53 to double
store double %54, double* %3
%55 = load double, double* %3
%56 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %55, double 1.000000e+00)
%57 = call double @sqrt(double %56)
store double %57, double* %5
%58 = load double, double* %5
%59 = load double, double* %8
%60 = load double, double* %3
%61 = call double @llvm.fmuladd.f64(double %58, double %59, double %60)
store double %61, double* %9
br label %62
62: ; preds = %48
%63 = load double, double* %9
%64 = fcmp ole double %63, 0.000000e+00
br i1 %64, label %35, label %65
65: ; preds = %62
%66 = load double, double* %8
%67 = load double, double* %8
%68 = call double @llvm.fmuladd.f64(double %66, double %67, double 1.000000e+00)
%69 = load double, double* %3
%70 = load double, double* %9
%71 = load double, double* %3
%72 = fdiv double %70, %71
%73 = call double @log(double %72)
%74 = load double, double* %5
%75 = load double, double* %8
%76 = fmul double %74, %75
%77 = fneg double %76
%78 = call double @llvm.fmuladd.f64(double %69, double %73, double %77)
%79 = call double @exp(double %78)
%80 = fmul double %68, %79
store double %80, double* %4
br label %81
81: ; preds = %65
%82 = call double (...) @uniformRandom()
%83 = load double, double* %4
%84 = fcmp ogt double %82, %83
br i1 %84, label %34, label %85
85: ; preds = %81
br label %86
86: ; preds = %85, %29
%87 = load double, double* %9
ret double %87
}

LLVM IR (-O2):
define double @gammaDev(i32 %0) {
%2 = icmp sgt i32 %0, 0
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = icmp slt i32 %0, 6
br i1 %5, label %10, label %6
6: ; preds = %1
%7 = add nsw i32 %0, -1
%8 = sitofp i32 %7 to double
%9 = tail call double @llvm.fmuladd.f64(double %8, double 2.000000e+00, double 1.000000e+00)
br label %23
10: ; preds = %1
%11 = icmp slt i32 %0, 0
br i1 %11, label %12, label %16
12: ; preds = %16, %10
%13 = phi double [ 1.000000e+00, %10 ], [ %20, %16 ]
%14 = tail call double @log(double %13)
%15 = fneg double %14
br label %47
16: ; preds = %10, %16
%17 = phi i32 [ %21, %16 ], [ 0, %10 ]
%18 = phi double [ %20, %16 ], [ 1.000000e+00, %10 ]
%19 = tail call double (...) @uniformRandom()
%20 = fmul double %18, %19
%21 = add nuw i32 %17, 1
%22 = icmp eq i32 %17, %0
br i1 %22, label %12, label %16
23: ; preds = %30, %6
%24 = tail call double (...) @uniformRandom()
%25 = tail call double (...) @uniformRandom()
%26 = tail call double @llvm.fmuladd.f64(double %25, double 2.000000e+00, double -1.000000e+00)
%27 = fmul double %26, %26
%28 = tail call double @llvm.fmuladd.f64(double %24, double %24, double %27)
%29 = fcmp ogt double %28, 1.000000e+00
br i1 %29, label %30, label %31
30: ; preds = %23, %36, %31
br label %23
31: ; preds = %23
%32 = fdiv double %26, %24
%33 = tail call double @sqrt(double %9)
%34 = tail call double @llvm.fmuladd.f64(double %33, double %32, double %8)
%35 = fcmp ugt double %34, 0.000000e+00
br i1 %35, label %36, label %30
36: ; preds = %31
%37 = tail call double @llvm.fmuladd.f64(double %32, double %32, double 1.000000e+00)
%38 = fdiv double %34, %8
%39 = tail call double @log(double %38)
%40 = fneg double %33
%41 = fmul double %32, %40
%42 = tail call double @llvm.fmuladd.f64(double %8, double %39, double %41)
%43 = tail call double @exp(double %42)
%44 = fmul double %37, %43
%45 = tail call double (...) @uniformRandom()
%46 = fcmp ogt double %45, %44
br i1 %46, label %30, label %47
47: ; preds = %36, %12
%48 = phi double [ %15, %12 ], [ %34, %36 ]
ret double %48
}