#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 *calloc(unsigned int, int);
int /*<<< orphan*/ error(char *);
int log(double);
double *normrnd(int, int /*<<< orphan*/, int);
double pow(double, int);
double rnd();
int sqrt(int);
double *gammarnd(unsigned n, double shape, double rate) {
  if (shape <= 0 || rate <= 0)
    error("Both parameters must be larger than 0.");
  double *arr = calloc(n, sizeof(double));
  double orig_shape, a1, a2, u, v;
  double *x;
  orig_shape = shape;
  if (shape < 1.)
    shape += 1.;
  a1 = shape - 1. / 3.;
  a2 = 1. / sqrt(9. * a1);
  for (int i = 0; i < n; i++) {
    do {
      do {
        x = normrnd(1, 0, 1);
        v = 1. + a2 * *x;
      } while (v <= 0);
      v = v * v * v;
      u = rnd();
    } while (u > 1. - 0.331 * ((*x) * (*x)) * ((*x) * (*x)) &&
             log(u) > 0.5 * (*x) * (*x) + a1 * (1. - v + log(v)));
    if (shape == orig_shape)
      arr[i] = a1 * v / rate;
    else
      arr[i] = pow(u, 1. / orig_shape) * a1 * v / rate;
  }
  return arr;
}


x86 assembly (clang, -O0):
<gammarnd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jae    11a4 <gammarnd+0x34>
xorps  %xmm0,%xmm0
ucomisd -0x18(%rbp),%xmm0
jb     11b0 <gammarnd+0x40>
lea    0xe7d(%rip),%rdi
call   1050 <error@plt>
mov    -0x4(%rbp),%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xe2d(%rip),%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    11f0 <gammarnd+0x80>
movsd  0xe1a(%rip),%xmm0
addsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xe13(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xdfa(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1080 <sqrt@plt>
cvtsi2sd %eax,%xmm1
movsd  0xdd8(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    1435 <gammarnd+0x2c5>
jmp    1249 <gammarnd+0xd9>
jmp    124e <gammarnd+0xde>
mov    $0x1,%edx
xor    %esi,%esi
mov    %edx,%edi
call   1090 <normrnd@plt>
mov    %rax,-0x50(%rbp)
movsd  -0x38(%rbp),%xmm0
mov    -0x50(%rbp),%rax
movsd  (%rax),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xd87(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x48(%rbp),%xmm0
jae    124e <gammarnd+0xde>
movsd  -0x48(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    $0x0,%al
call   1030 <rnd@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
mov    -0x50(%rbp),%rax
movsd  (%rax),%xmm2
mov    -0x50(%rbp),%rax
mulsd  (%rax),%xmm2
movsd  0xd4b(%rip),%xmm1
mulsd  %xmm2,%xmm1
mov    -0x50(%rbp),%rax
movsd  (%rax),%xmm2
mov    -0x50(%rbp),%rax
mulsd  (%rax),%xmm2
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm1
movsd  0xcfc(%rip),%xmm2
addsd  %xmm2,%xmm1
xor    %eax,%eax
ucomisd %xmm1,%xmm0
mov    %al,-0x55(%rbp)
jbe    13a9 <gammarnd+0x239>
movsd  -0x40(%rbp),%xmm0
call   1060 <log@plt>
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    -0x50(%rbp),%rax
movsd  0xcea(%rip),%xmm0
mulsd  (%rax),%xmm0
movsd  %xmm0,-0x70(%rbp)
mov    -0x50(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x68(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x78(%rbp)
movsd  0xca2(%rip),%xmm0
subsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x80(%rbp)
movsd  -0x48(%rbp),%xmm0
call   1060 <log@plt>
movsd  -0x80(%rbp),%xmm4
movsd  -0x78(%rbp),%xmm2
movsd  -0x70(%rbp),%xmm1
movsd  -0x68(%rbp),%xmm3
movsd  -0x60(%rbp),%xmm0
cvtsi2sd %eax,%xmm5
addsd  %xmm5,%xmm4
mulsd  %xmm4,%xmm2
mulsd  %xmm3,%xmm1
addsd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
seta   %al
mov    %al,-0x55(%rbp)
mov    -0x55(%rbp),%al
test   $0x1,%al
jne    1249 <gammarnd+0xd9>
movsd  -0x10(%rbp),%xmm0
ucomisd -0x28(%rbp),%xmm0
jne    13eb <gammarnd+0x27b>
jp     13eb <gammarnd+0x27b>
movsd  -0x30(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
divsd  -0x18(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x54(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
jmp    1422 <gammarnd+0x2b2>
movsd  -0x40(%rbp),%xmm0
movsd  0xc08(%rip),%xmm1
divsd  -0x28(%rbp),%xmm1
cvttsd2si %xmm1,%edi
call   1070 <pow@plt>
mulsd  -0x30(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
divsd  -0x18(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x54(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
jmp    1427 <gammarnd+0x2b7>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1238 <gammarnd+0xc8>
mov    -0x20(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gammarnd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
movsd  %xmm1,0x18(%rsp)
mov    %edi,%r14d
xorpd  %xmm1,%xmm1
movapd %xmm0,0x20(%rsp)
ucomisd %xmm0,%xmm1
jae    119d <gammarnd+0x2d>
ucomisd 0x18(%rsp),%xmm1
jb     11a9 <gammarnd+0x39>
lea    0xe8c(%rip),%rdi
call   1050 <error@plt>
mov    %r14d,%edi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,%r12
movsd  0xe3f(%rip),%xmm3
movapd 0x20(%rsp),%xmm1
movapd %xmm1,%xmm0
addsd  %xmm3,%xmm0
movapd %xmm1,%xmm2
cmpltsd %xmm3,%xmm2
andpd  %xmm2,%xmm0
andnpd %xmm1,%xmm2
orpd   %xmm0,%xmm2
movsd  0xe1c(%rip),%xmm1
movapd %xmm2,0x60(%rsp)
addsd  %xmm2,%xmm1
movsd  0xe12(%rip),%xmm0
movsd  %xmm1,0x8(%rsp)
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1080 <sqrt@plt>
movsd  0xde7(%rip),%xmm1
test   %r14d,%r14d
je     13bc <gammarnd+0x24c>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movapd %xmm1,%xmm2
divsd  %xmm0,%xmm2
movsd  %xmm2,0x58(%rsp)
movapd %xmm1,%xmm0
divsd  0x20(%rsp),%xmm0
cvttsd2si %xmm0,%r15d
mov    %r14d,%r14d
xor    %ebx,%ebx
jmp    1290 <gammarnd+0x120>
movapd %xmm2,%xmm0
mov    %r15d,%edi
call   1070 <pow@plt>
mulsd  0x8(%rsp),%xmm0
movsd  0x10(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
divsd  0x18(%rsp),%xmm0
movsd  %xmm0,(%r12,%rbx,8)
add    $0x1,%rbx
cmp    %r14,%rbx
je     13bc <gammarnd+0x24c>
cs nopw 0x0(%rax,%rax,1)
mov    $0x1,%edi
xor    %esi,%esi
mov    $0x1,%edx
call   1090 <normrnd@plt>
movsd  (%rax),%xmm0
mulsd  0x58(%rsp),%xmm0
addsd  0xd4d(%rip),%xmm0
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
jae    1290 <gammarnd+0x120>
mov    %rax,%rbp
movapd %xmm0,%xmm1
mulsd  %xmm0,%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm1,0x10(%rsp)
xor    %eax,%eax
call   1030 <rnd@plt>
movapd %xmm0,%xmm2
movsd  0x0(%rbp),%xmm0
mulsd  %xmm0,%xmm0
movapd %xmm0,%xmm1
mulsd  0xd26(%rip),%xmm1
mulsd  %xmm0,%xmm1
addsd  0xd02(%rip),%xmm1
ucomisd %xmm1,%xmm2
jbe    1393 <gammarnd+0x223>
movapd %xmm2,%xmm0
movsd  %xmm2,0x50(%rsp)
call   1060 <log@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x48(%rsp)
movsd  0x0(%rbp),%xmm0
movsd  %xmm0,0x40(%rsp)
mulsd  0xce9(%rip),%xmm0
movsd  %xmm0,0x38(%rsp)
movsd  0xcbb(%rip),%xmm1
movsd  0x10(%rsp),%xmm0
subsd  %xmm0,%xmm1
movsd  %xmm1,0x30(%rsp)
call   1060 <log@plt>
movsd  0x50(%rsp),%xmm2
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
addsd  0x30(%rsp),%xmm0
mulsd  0x8(%rsp),%xmm0
movsd  0x38(%rsp),%xmm1
mulsd  0x40(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  0x48(%rsp),%xmm0
ucomisd %xmm1,%xmm0
ja     1290 <gammarnd+0x120>
movapd 0x60(%rsp),%xmm0
ucomisd 0x20(%rsp),%xmm0
jne    124d <gammarnd+0xdd>
jp     124d <gammarnd+0xdd>
movsd  0x10(%rsp),%xmm0
mulsd  0x8(%rsp),%xmm0
jmp    126d <gammarnd+0xfd>
mov    %r12,%rax
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [39 x i8] c"Both parameters must be larger than 0.\00"
define double* @gammarnd(i32 %0, double %1, double %2) {
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca double*
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double*
%14 = alloca i32
store i32 %0, i32* %4
store double %1, double* %5
store double %2, double* %6
%15 = load double, double* %5
%16 = fcmp ole double %15, 0.000000e+00
br i1 %16, label %20, label %17
17: ; preds = %3
%18 = load double, double* %6
%19 = fcmp ole double %18, 0.000000e+00
br i1 %19, label %20, label %22
20: ; preds = %17, %3
%21 = call i32 @error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = load i32, i32* %4
%24 = call double* @calloc(i32 %23, i32 8)
store double* %24, double** %7
%25 = load double, double* %5
store double %25, double* %8
%26 = load double, double* %5
%27 = fcmp olt double %26, 1.000000e+00
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load double, double* %5
%30 = fadd double %29, 1.000000e+00
store double %30, double* %5
br label %31
31: ; preds = %28, %22
%32 = load double, double* %5
%33 = fsub double %32, 0x3FD5555555555555
store double %33, double* %9
%34 = load double, double* %9
%35 = fmul double 9.000000e+00, %34
%36 = fptosi double %35 to i32
%37 = call i32 @sqrt(i32 %36)
%38 = sitofp i32 %37 to double
%39 = fdiv double 1.000000e+00, %38
store double %39, double* %10
store i32 0, i32* %14
br label %40
40: ; preds = %130, %31
%41 = load i32, i32* %14
%42 = load i32, i32* %4
%43 = icmp ult i32 %41, %42
br i1 %43, label %44, label %133
44: ; preds = %40
br label %45
45: ; preds = %97, %44
br label %46
46: ; preds = %52, %45
%47 = call double* @normrnd(i32 1, i32 0, i32 1)
store double* %47, double** %13
%48 = load double, double* %10
%49 = load double*, double** %13
%50 = load double, double* %49
%51 = call double @llvm.fmuladd.f64(double %48, double %50, double 1.000000e+00)
store double %51, double* %12
br label %52
52: ; preds = %46
%53 = load double, double* %12
%54 = fcmp ole double %53, 0.000000e+00
br i1 %54, label %46, label %55
55: ; preds = %52
%56 = load double, double* %12
%57 = load double, double* %12
%58 = fmul double %56, %57
%59 = load double, double* %12
%60 = fmul double %58, %59
store double %60, double* %12
%61 = call double (...) @rnd()
store double %61, double* %11
br label %62
62: ; preds = %55
%63 = load double, double* %11
%64 = load double*, double** %13
%65 = load double, double* %64
%66 = load double*, double** %13
%67 = load double, double* %66
%68 = fmul double %65, %67
%69 = fmul double 3.310000e-01, %68
%70 = load double*, double** %13
%71 = load double, double* %70
%72 = load double*, double** %13
%73 = load double, double* %72
%74 = fmul double %71, %73
%75 = fneg double %69
%76 = call double @llvm.fmuladd.f64(double %75, double %74, double 1.000000e+00)
%77 = fcmp ogt double %63, %76
br i1 %77, label %78, label %97
78: ; preds = %62
%79 = load double, double* %11
%80 = call i32 @log(double %79)
%81 = sitofp i32 %80 to double
%82 = load double*, double** %13
%83 = load double, double* %82
%84 = fmul double 5.000000e-01, %83
%85 = load double*, double** %13
%86 = load double, double* %85
%87 = load double, double* %9
%88 = load double, double* %12
%89 = fsub double 1.000000e+00, %88
%90 = load double, double* %12
%91 = call i32 @log(double %90)
%92 = sitofp i32 %91 to double
%93 = fadd double %89, %92
%94 = fmul double %87, %93
%95 = call double @llvm.fmuladd.f64(double %84, double %86, double %94)
%96 = fcmp ogt double %81, %95
br label %97
97: ; preds = %78, %62
%98 = phi i1 [ false, %62 ], [ %96, %78 ]
br i1 %98, label %45, label %99
99: ; preds = %97
%100 = load double, double* %5
%101 = load double, double* %8
%102 = fcmp oeq double %100, %101
br i1 %102, label %103, label %113
103: ; preds = %99
%104 = load double, double* %9
%105 = load double, double* %12
%106 = fmul double %104, %105
%107 = load double, double* %6
%108 = fdiv double %106, %107
%109 = load double*, double** %7
%110 = load i32, i32* %14
%111 = sext i32 %110 to i64
%112 = getelementptr inbounds double, double* %109, i64 %111
store double %108, double* %112
br label %129
113: ; preds = %99
%114 = load double, double* %11
%115 = load double, double* %8
%116 = fdiv double 1.000000e+00, %115
%117 = fptosi double %116 to i32
%118 = call double @pow(double %114, i32 %117)
%119 = load double, double* %9
%120 = fmul double %118, %119
%121 = load double, double* %12
%122 = fmul double %120, %121
%123 = load double, double* %6
%124 = fdiv double %122, %123
%125 = load double*, double** %7
%126 = load i32, i32* %14
%127 = sext i32 %126 to i64
%128 = getelementptr inbounds double, double* %125, i64 %127
store double %124, double* %128
br label %129
129: ; preds = %113, %103
br label %130
130: ; preds = %129
%131 = load i32, i32* %14
%132 = add nsw i32 %131, 1
store i32 %132, i32* %14
br label %40
133: ; preds = %40
%134 = load double*, double** %7
ret double* %134
}

LLVM IR (-O2):
@.str = [39 x i8] c"Both parameters must be larger than 0.\00"
define noalias double* @gammarnd(i32 %0, double %1, double %2) {
%4 = fcmp ole double %1, 0.000000e+00
%5 = fcmp ole double %2, 0.000000e+00
%6 = or i1 %4, %5
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @error(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %3, %7
%10 = tail call double* @calloc(i32 %0, i32 8)
%11 = fcmp olt double %1, 1.000000e+00
%12 = fadd double %1, 1.000000e+00
%13 = select i1 %11, double %12, double %1
%14 = fadd double %13, 0xBFD5555555555555
%15 = fmul double %14, 9.000000e+00
%16 = fptosi double %15 to i32
%17 = tail call i32 @sqrt(i32 %16)
%18 = sitofp i32 %17 to double
%19 = fdiv double 1.000000e+00, %18
%20 = icmp eq i32 %0, 0
br i1 %20, label %28, label %21
21: ; preds = %9
%22 = fcmp oeq double %13, %1
%23 = fdiv double 1.000000e+00, %1
%24 = fptosi double %23 to i32
%25 = zext i32 %0 to i64
br label %26
26: ; preds = %21, %63
%27 = phi i64 [ 0, %21 ], [ %67, %63 ]
br label %29
28: ; preds = %63, %9
ret double* %10
29: ; preds = %34, %26
%30 = tail call double* @normrnd(i32 1, i32 0, i32 1)
%31 = load double, double* %30
%32 = tail call double @llvm.fmuladd.f64(double %19, double %31, double 1.000000e+00)
%33 = fcmp ugt double %32, 0.000000e+00
br i1 %33, label %35, label %34
34: ; preds = %29, %44
br label %29
35: ; preds = %29
%36 = fmul double %32, %32
%37 = fmul double %32, %36
%38 = tail call double (...) @rnd()
%39 = load double, double* %30
%40 = fmul double %39, %39
%41 = fmul double %40, -3.310000e-01
%42 = tail call double @llvm.fmuladd.f64(double %41, double %40, double 1.000000e+00)
%43 = fcmp ogt double %38, %42
br i1 %43, label %44, label %56
44: ; preds = %35
%45 = tail call i32 @log(double %38)
%46 = sitofp i32 %45 to double
%47 = load double, double* %30
%48 = fmul double %47, 5.000000e-01
%49 = fsub double 1.000000e+00, %37
%50 = tail call i32 @log(double %37)
%51 = sitofp i32 %50 to double
%52 = fadd double %49, %51
%53 = fmul double %14, %52
%54 = tail call double @llvm.fmuladd.f64(double %48, double %47, double %53)
%55 = fcmp olt double %54, %46
br i1 %55, label %34, label %56
56: ; preds = %35, %44
br i1 %22, label %57, label %59
57: ; preds = %56
%58 = fmul double %14, %37
br label %63
59: ; preds = %56
%60 = tail call double @pow(double %38, i32 %24)
%61 = fmul double %14, %60
%62 = fmul double %37, %61
br label %63
63: ; preds = %57, %59
%64 = phi double [ %58, %57 ], [ %62, %59 ]
%65 = fdiv double %64, %2
%66 = getelementptr inbounds double, double* %10, i64 %27
store double %65, double* %66
%67 = add nuw nsw i64 %27, 1
%68 = icmp eq i64 %67, %25
br i1 %68, label %28, label %26
}