#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 exp(double);
int floor(double);
int log(double);
double pow(double, double);
double runif();
double rgamma(double shape, double rate) {
  double scale = 1.0 / rate;
  int shape_int = floor(shape);
  double s = 0;
  for (int i = 0; i < shape_int; i++) {
    s = s - log(runif());
  }
  if (shape_int == shape) {
    return scale * s;
  } else {
    double U, V, W, xi, eta;
    double delta = shape - shape_int;
    do {
      U = runif();
      V = runif();
      W = runif();
      if (U <= exp(1) / (exp(1) + delta)) {
        xi = pow(V, 1 / delta);
        eta = W * pow(xi, delta - 1);
      } else {
        xi = 1 - log(V);
        eta = W * exp(-xi);
      }
    } while (eta > pow(xi, delta - 1) * exp(-xi));
    return scale * (xi + s);
  }
}


x86 assembly (clang, -O0):
<rgamma>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
movsd  0xe93(%rip),%xmm0
divsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
call   1070 <floor@plt>
mov    %eax,-0x24(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11d5 <rgamma+0x85>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x70(%rbp)
mov    $0x0,%al
call   1030 <runif@plt>
call   1050 <log@plt>
movsd  -0x70(%rbp),%xmm0
cvtsi2sd %eax,%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1193 <rgamma+0x43>
cvtsi2sdl -0x24(%rbp),%xmm0
ucomisd -0x10(%rbp),%xmm0
jne    11ff <rgamma+0xaf>
jp     11ff <rgamma+0xaf>
movsd  -0x20(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    13c9 <rgamma+0x279>
movsd  -0x10(%rbp),%xmm0
cvtsi2sdl -0x24(%rbp),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    $0x0,%al
call   1030 <runif@plt>
movsd  %xmm0,-0x40(%rbp)
mov    $0x0,%al
call   1030 <runif@plt>
movsd  %xmm0,-0x48(%rbp)
mov    $0x0,%al
call   1030 <runif@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x80(%rbp)
movsd  0xdb8(%rip),%xmm0
call   1040 <exp@plt>
movsd  %xmm0,-0x78(%rbp)
movsd  0xda6(%rip),%xmm0
call   1040 <exp@plt>
movsd  -0x80(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x78(%rbp),%xmm0
addsd  -0x68(%rbp),%xmm2
divsd  %xmm2,%xmm0
ucomisd %xmm1,%xmm0
jb     12dc <rgamma+0x18c>
movsd  -0x48(%rbp),%xmm0
movsd  0xd74(%rip),%xmm1
divsd  -0x68(%rbp),%xmm1
call   1060 <pow@plt>
movsd  %xmm0,-0x58(%rbp)
movsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x88(%rbp)
ff
movsd  -0x58(%rbp),%xmm0
movsd  -0x68(%rbp),%xmm1
movsd  0xd46(%rip),%xmm2
subsd  %xmm2,%xmm1
call   1060 <pow@plt>
movaps %xmm0,%xmm1
movsd  -0x88(%rbp),%xmm0
ff
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x60(%rbp)
jmp    133a <rgamma+0x1ea>
movsd  -0x48(%rbp),%xmm0
call   1050 <log@plt>
mov    %eax,%ecx
mov    $0x1,%eax
sub    %ecx,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x90(%rbp)
ff
movsd  -0x58(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
call   1040 <exp@plt>
movaps %xmm0,%xmm1
movsd  -0x90(%rbp),%xmm0
ff
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x60(%rbp)
jmp    133f <rgamma+0x1ef>
movsd  -0x60(%rbp),%xmm0
movsd  %xmm0,-0x98(%rbp)
ff
movsd  -0x58(%rbp),%xmm0
movsd  -0x68(%rbp),%xmm1
movsd  0xca2(%rip),%xmm2
subsd  %xmm2,%xmm1
call   1060 <pow@plt>
movsd  %xmm0,-0xa0(%rbp)
ff
movsd  -0x58(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
call   1040 <exp@plt>
movsd  -0xa0(%rbp),%xmm1
ff
movaps %xmm0,%xmm2
movsd  -0x98(%rbp),%xmm0
ff
mulsd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
ja     1212 <rgamma+0xc2>
movsd  -0x20(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm1
addsd  -0x30(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rgamma>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
movsd  %xmm1,0x38(%rsp)
movsd  %xmm0,(%rsp)
call   1070 <floor@plt>
mov    %eax,%ebx
xorpd  %xmm1,%xmm1
test   %eax,%eax
jle    11a8 <rgamma+0x58>
mov    %ebx,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsd  %xmm1,0x10(%rsp)
xor    %eax,%eax
call   1030 <runif@plt>
call   1050 <log@plt>
movsd  0x10(%rsp),%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
subsd  %xmm0,%xmm1
add    $0xffffffff,%ebp
jne    1180 <rgamma+0x30>
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
movsd  (%rsp),%xmm2
ucomisd %xmm2,%xmm0
jne    11c0 <rgamma+0x70>
jnp    130c <rgamma+0x1bc>
movsd  %xmm1,0x10(%rsp)
subsd  %xmm0,%xmm2
movsd  0xe2e(%rip),%xmm1
movapd %xmm2,%xmm0
addsd  %xmm1,%xmm0
divsd  %xmm0,%xmm1
movsd  %xmm1,0x48(%rsp)
movsd  0xe1c(%rip),%xmm0
divsd  %xmm2,%xmm0
movsd  %xmm0,0x40(%rsp)
addsd  0xe12(%rip),%xmm2
movsd  %xmm2,(%rsp)
jmp    1284 <rgamma+0x134>
nopl   0x0(%rax,%rax,1)
movsd  0x8(%rsp),%xmm0
call   1050 <log@plt>
mov    $0x1,%ecx
sub    %eax,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
movapd %xmm0,0x20(%rsp)
xorpd  0xde9(%rip),%xmm0
movapd %xmm0,0x50(%rsp)
call   1040 <exp@plt>
movsd  (%rsp),%xmm1
movsd  0x18(%rsp),%xmm2
mulsd  %xmm0,%xmm2
movsd  %xmm2,0x18(%rsp)
movaps 0x20(%rsp),%xmm0
call   1060 <pow@plt>
movsd  %xmm0,0x8(%rsp)
movapd 0x50(%rsp),%xmm0
call   1040 <exp@plt>
mulsd  0x8(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
ucomisd %xmm0,%xmm1
jbe    1300 <rgamma+0x1b0>
xor    %eax,%eax
call   1030 <runif@plt>
movsd  %xmm0,0x20(%rsp)
xor    %eax,%eax
call   1030 <runif@plt>
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   1030 <runif@plt>
movapd %xmm0,%xmm1
movsd  0x48(%rsp),%xmm0
ucomisd 0x20(%rsp),%xmm0
movsd  %xmm1,0x18(%rsp)
jb     1210 <rgamma+0xc0>
movsd  0x8(%rsp),%xmm0
movsd  0x40(%rsp),%xmm1
call   1060 <pow@plt>
movapd %xmm0,0x20(%rsp)
movsd  (%rsp),%xmm1
call   1060 <pow@plt>
movsd  (%rsp),%xmm1
movapd 0x20(%rsp),%xmm2
xorpd  0xd2b(%rip),%xmm2
movapd %xmm2,0x50(%rsp)
jmp    1247 <rgamma+0xf7>
movsd  0x10(%rsp),%xmm1
addsd  0x20(%rsp),%xmm1
movsd  0xcf4(%rip),%xmm0
divsd  0x38(%rsp),%xmm0
mulsd  %xmm1,%xmm0
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret

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

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