#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);
double log(double);
double pow(double, int);
double rndu();
double rndgamma1(double s) {
  double r, x = 0, small = 1e-37, w;
  static double a, p, uf, ss = 10, d;
  if (s != ss) {
    a = 1 - s;
    p = a / (a + s * exp(-a));
    uf = p * pow(small / a, s);
    d = a * log(a);
    ss = s;
  }
  for (;;) {
    r = rndu();
    if (r > p)
      x = a - log((1 - r) / (1 - p)), w = a * log(x) - d;
    else if (r > uf)
      x = a * pow(r / p, 1 / s), w = x;
    else
      return (0);
    r = rndu();
    if (1 - r <= w && r > 0)
      if (r * (w + 1) >= 1 || -log(r) <= w)
        continue;
    break;
  }
  return (x);
}


x86 assembly (clang, -O0):
<rndgamma1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0xea3(%rip),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
ucomisd 0x2ed1(%rip),%xmm0
jne    1180 <rndgamma1+0x40>
jp     1180 <rndgamma1+0x40>
jmp    1276 <rndgamma1+0x136>
movsd  0xe80(%rip),%xmm0
subsd  -0x10(%rbp),%xmm0
movsd  %xmm0,0x2ebb(%rip)
movsd  0x2eb3(%rip),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  0x2ea6(%rip),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  0x2e8f(%rip),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
call   1040 <exp@plt>
movsd  -0x58(%rbp),%xmm1
movsd  -0x50(%rbp),%xmm2
movaps %xmm0,%xmm3
movsd  -0x48(%rbp),%xmm0
mulsd  %xmm3,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,0x2e55(%rip)
movsd  0x2e4d(%rip),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x28(%rbp),%xmm0
divsd  0x2e33(%rip),%xmm0
cvttsd2si -0x10(%rbp),%edi
call   1060 <pow@plt>
movaps %xmm0,%xmm1
movsd  -0x40(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,0x2e25(%rip)
movsd  0x2e0d(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  0x2e00(%rip),%xmm0
call   1050 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x38(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,0x2dff(%rip)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,0x2dca(%rip)
jmp    127b <rndgamma1+0x13b>
mov    $0x0,%al
call   1030 <rndu@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
ucomisd 0x2dc4(%rip),%xmm0
jbe    132e <rndgamma1+0x1ee>
movsd  0x2dae(%rip),%xmm0
movsd  %xmm0,-0x68(%rbp)
movsd  0xd59(%rip),%xmm0
subsd  -0x18(%rbp),%xmm0
movsd  0xd4c(%rip),%xmm1
subsd  0x2d94(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1050 <log@plt>
movaps %xmm0,%xmm1
movsd  -0x68(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  0x2d6a(%rip),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1050 <log@plt>
movaps %xmm0,%xmm2
movsd  -0x60(%rbp),%xmm0
movsd  0x2d63(%rip),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    13a3 <rndgamma1+0x263>
movsd  -0x18(%rbp),%xmm0
ucomisd 0x2d25(%rip),%xmm0
jbe    1391 <rndgamma1+0x251>
movsd  0x2d07(%rip),%xmm0
movsd  %xmm0,-0x70(%rbp)
movsd  -0x18(%rbp),%xmm0
divsd  0x2cfd(%rip),%xmm0
movsd  0xca5(%rip),%xmm1
divsd  -0x10(%rbp),%xmm1
cvttsd2si %xmm1,%edi
call   1060 <pow@plt>
movaps %xmm0,%xmm1
movsd  -0x70(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    139e <rndgamma1+0x25e>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    144e <rndgamma1+0x30e>
jmp    13a3 <rndgamma1+0x263>
mov    $0x0,%al
call   1030 <rndu@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  0xc51(%rip),%xmm1
subsd  -0x18(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     143f <rndgamma1+0x2ff>
movsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    143f <rndgamma1+0x2ff>
movsd  -0x18(%rbp),%xmm0
movsd  0xc1e(%rip),%xmm1
addsd  -0x30(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xc0d(%rip),%xmm1
ucomisd %xmm1,%xmm0
jae    1435 <rndgamma1+0x2f5>
movsd  -0x18(%rbp),%xmm0
call   1050 <log@plt>
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
movsd  -0x30(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     143a <rndgamma1+0x2fa>
jmp    127b <rndgamma1+0x13b>
jmp    143f <rndgamma1+0x2ff>
jmp    1444 <rndgamma1+0x304>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rndgamma1>:
push   %rbx
sub    $0x20,%rsp
movapd %xmm0,%xmm1
movsd  0x2eef(%rip),%xmm0
ucomisd %xmm1,%xmm0
jne    115d <rndgamma1+0x1d>
jnp    1208 <rndgamma1+0xc8>
movsd  0xe9b(%rip),%xmm2
subsd  %xmm1,%xmm2
movapd %xmm2,0x10(%rsp)
movsd  %xmm2,0x2ed9(%rip)
movapd 0xea1(%rip),%xmm0
xorpd  %xmm2,%xmm0
movsd  %xmm1,0x8(%rsp)
call   1040 <exp@plt>
mulsd  0x8(%rsp),%xmm0
movapd 0x10(%rsp),%xmm1
addsd  %xmm1,%xmm0
divsd  %xmm0,%xmm1
movapd %xmm1,0x10(%rsp)
movsd  %xmm1,0x2ea8(%rip)
movsd  0xe50(%rip),%xmm0
divsd  0x2e90(%rip),%xmm0
cvttsd2si 0x8(%rsp),%edi
call   1060 <pow@plt>
mulsd  0x10(%rsp),%xmm0
movsd  %xmm0,0x2e87(%rip)
movsd  0x2e6f(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
call   1050 <log@plt>
movsd  0x8(%rsp),%xmm1
mulsd  0x10(%rsp),%xmm0
movsd  %xmm0,0x2e68(%rip)
movsd  %xmm1,0x2e38(%rip)
movsd  0xdf0(%rip),%xmm0
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <rndu@plt>
movsd  0x2e29(%rip),%xmm2
ucomisd %xmm2,%xmm0
jbe    12a0 <rndgamma1+0x160>
movsd  0x2e13(%rip),%xmm1
movsd  %xmm1,0x10(%rsp)
movsd  0xdb5(%rip),%xmm3
movapd %xmm3,%xmm1
subsd  %xmm0,%xmm1
movapd %xmm3,%xmm0
subsd  %xmm2,%xmm0
divsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1050 <log@plt>
movsd  0x10(%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  0x2dd6(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
movsd  %xmm1,0x10(%rsp)
movapd %xmm1,%xmm0
call   1050 <log@plt>
mulsd  0x8(%rsp),%xmm0
subsd  0x2dcb(%rip),%xmm0
jmp    12d3 <rndgamma1+0x193>
nop
ucomisd 0x2db8(%rip),%xmm0
jbe    1341 <rndgamma1+0x201>
movsd  0x2d9a(%rip),%xmm1
movsd  %xmm1,0x8(%rsp)
divsd  %xmm2,%xmm0
mov    %ebx,%edi
call   1060 <pow@plt>
mulsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   1030 <rndu@plt>
ucomisd 0xd28(%rip),%xmm0
jbe    134b <rndgamma1+0x20b>
movsd  0xd0e(%rip),%xmm2
movapd %xmm2,%xmm1
subsd  %xmm0,%xmm1
movsd  0x8(%rsp),%xmm3
ucomisd %xmm1,%xmm3
jb     134b <rndgamma1+0x20b>
movapd %xmm3,%xmm1
addsd  %xmm2,%xmm1
mulsd  %xmm0,%xmm1
ucomisd %xmm2,%xmm1
jae    1220 <rndgamma1+0xe0>
movsd  %xmm3,0x8(%rsp)
call   1050 <log@plt>
xorpd  0xcf1(%rip),%xmm0
movsd  0x8(%rsp),%xmm1
ucomisd %xmm0,%xmm1
jae    1220 <rndgamma1+0xe0>
jmp    134b <rndgamma1+0x20b>
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  0x10(%rsp),%xmm0
add    $0x20,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@rndgamma1.a = internal unnamed_addr global double 0.000000e+00
@rndgamma1.p = internal unnamed_addr global double 0.000000e+00
@rndgamma1.uf = internal unnamed_addr global double 0.000000e+00
@rndgamma1.ss = internal unnamed_addr global double 1.000000e+01
@rndgamma1.d = internal unnamed_addr global double 0.000000e+00
define double @rndgamma1(double %0) {
%2 = load double, double* @rndgamma1.ss
%3 = fcmp une double %2, %0
br i1 %3, label %4, label %18
4: ; preds = %1
%5 = fsub double 1.000000e+00, %0
store double %5, double* @rndgamma1.a
%6 = fneg double %5
%7 = tail call double @exp(double %6)
%8 = tail call double @llvm.fmuladd.f64(double %0, double %7, double %5)
%9 = fdiv double %5, %8
store double %9, double* @rndgamma1.p
%10 = load double, double* @rndgamma1.a
%11 = fdiv double 1.000000e-37, %10
%12 = fptosi double %0 to i32
%13 = tail call double @pow(double %11, i32 %12)
%14 = fmul double %9, %13
store double %14, double* @rndgamma1.uf
%15 = load double, double* @rndgamma1.a
%16 = tail call double @log(double %15)
%17 = fmul double %15, %16
store double %17, double* @rndgamma1.d
store double %0, double* @rndgamma1.ss
br label %18
18: ; preds = %4, %1
%19 = fdiv double 1.000000e+00, %0
%20 = fptosi double %19 to i32
br label %21
21: ; preds = %61, %18
%22 = tail call double (...) @rndu()
%23 = load double, double* @rndgamma1.p
%24 = fcmp ogt double %22, %23
br i1 %24, label %25, label %37
25: ; preds = %21
%26 = load double, double* @rndgamma1.a
%27 = fsub double 1.000000e+00, %22
%28 = fsub double 1.000000e+00, %23
%29 = fdiv double %27, %28
%30 = tail call double @log(double %29)
%31 = fsub double %26, %30
%32 = load double, double* @rndgamma1.a
%33 = tail call double @log(double %31)
%34 = load double, double* @rndgamma1.d
%35 = fneg double %34
%36 = tail call double @llvm.fmuladd.f64(double %32, double %33, double %35)
br label %45
37: ; preds = %21
%38 = load double, double* @rndgamma1.uf
%39 = fcmp ogt double %22, %38
br i1 %39, label %40, label %62
40: ; preds = %37
%41 = load double, double* @rndgamma1.a
%42 = fdiv double %22, %23
%43 = tail call double @pow(double %42, i32 %20)
%44 = fmul double %41, %43
br label %45
45: ; preds = %40, %25
%46 = phi double [ %31, %25 ], [ %44, %40 ]
%47 = phi double [ %36, %25 ], [ %44, %40 ]
%48 = tail call double (...) @rndu()
%49 = fsub double 1.000000e+00, %48
%50 = fcmp ole double %49, %47
%51 = fcmp ogt double %48, 0.000000e+00
%52 = and i1 %51, %50
br i1 %52, label %53, label %62
53: ; preds = %45
%54 = fadd double %47, 1.000000e+00
%55 = fmul double %48, %54
%56 = fcmp ult double %55, 1.000000e+00
br i1 %56, label %57, label %61
57: ; preds = %53
%58 = tail call double @log(double %48)
%59 = fneg double %58
%60 = fcmp ult double %47, %59
br i1 %60, label %62, label %61
61: ; preds = %57, %53
br label %21
62: ; preds = %57, %45, %37
%63 = phi double [ 0.000000e+00, %37 ], [ %46, %45 ], [ %46, %57 ]
ret double %63
}