#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 */
struct reb_simulation {
  int dummy;
};

/* Variables and functions */
double exp(double);
double log(double);
double pow(double, int);
double reb_random_uniform(struct reb_simulation *, int, int);
double reb_random_powerlaw(struct reb_simulation *r, double min, double max,
                           double slope) {
  double y = reb_random_uniform(r, 0., 1.);
  if (slope == -1)
    return exp(y * log(max / min) + log(min));
  else
    return pow((pow(max, slope + 1.) - pow(min, slope + 1.)) * y +
                   pow(min, slope + 1.),
               1. / (slope + 1.));
}


x86 assembly (clang, -O0):
<reb_random_powerlaw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
movsd  %xmm1,-0x20(%rbp)
movsd  %xmm2,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1060 <reb_random_uniform@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  0xe83(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    11d9 <reb_random_powerlaw+0x99>
jp     11d9 <reb_random_powerlaw+0x99>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x20(%rbp),%xmm0
divsd  -0x18(%rbp),%xmm0
call   1040 <log@plt>
movsd  %xmm0,-0x40(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1040 <log@plt>
movsd  -0x40(%rbp),%xmm2
movaps %xmm0,%xmm1
movsd  -0x38(%rbp),%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   1030 <exp@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    1286 <reb_random_powerlaw+0x146>
movsd  -0x20(%rbp),%xmm0
movsd  0xe22(%rip),%xmm1
addsd  -0x28(%rbp),%xmm1
cvttsd2si %xmm1,%edi
call   1050 <pow@plt>
movsd  %xmm0,-0x58(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xe02(%rip),%xmm1
addsd  -0x28(%rbp),%xmm1
cvttsd2si %xmm1,%edi
call   1050 <pow@plt>
movaps %xmm0,%xmm1
movsd  -0x58(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xdcc(%rip),%xmm1
addsd  -0x28(%rbp),%xmm1
cvttsd2si %xmm1,%edi
call   1050 <pow@plt>
movsd  -0x50(%rbp),%xmm2
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  0xda1(%rip),%xmm2
addsd  -0x28(%rbp),%xmm2
movsd  0xd94(%rip),%xmm1
divsd  %xmm2,%xmm1
cvttsd2si %xmm1,%edi
call   1050 <pow@plt>
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reb_random_powerlaw>:
push   %rbx
sub    $0x20,%rsp
movsd  %xmm2,0x8(%rsp)
movsd  %xmm1,(%rsp)
movsd  %xmm0,0x10(%rsp)
xor    %esi,%esi
mov    $0x1,%edx
call   1060 <reb_random_uniform@plt>
movsd  %xmm0,0x18(%rsp)
movsd  0x8(%rsp),%xmm1
ucomisd 0xe8a(%rip),%xmm1
jne    11b5 <reb_random_powerlaw+0x75>
jp     11b5 <reb_random_powerlaw+0x75>
movsd  (%rsp),%xmm0
divsd  0x10(%rsp),%xmm0
call   1040 <log@plt>
movsd  %xmm0,0x8(%rsp)
movsd  0x10(%rsp),%xmm0
call   1040 <log@plt>
movsd  0x18(%rsp),%xmm1
mulsd  0x8(%rsp),%xmm1
addsd  %xmm1,%xmm0
add    $0x20,%rsp
pop    %rbx
jmp    1030 <exp@plt>
movapd %xmm1,%xmm0
addsd  0xe47(%rip),%xmm0
movsd  %xmm0,0x8(%rsp)
cvttsd2si %xmm0,%ebx
movsd  (%rsp),%xmm0
mov    %ebx,%edi
call   1050 <pow@plt>
movsd  %xmm0,(%rsp)
movsd  0x10(%rsp),%xmm0
mov    %ebx,%edi
call   1050 <pow@plt>
movsd  (%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  0x10(%rsp),%xmm0
mov    %ebx,%edi
call   1050 <pow@plt>
movsd  (%rsp),%xmm1
mulsd  0x18(%rsp),%xmm1
addsd  %xmm1,%xmm0
movsd  0xded(%rip),%xmm1
divsd  0x8(%rsp),%xmm1
cvttsd2si %xmm1,%edi
add    $0x20,%rsp
pop    %rbx
jmp    1050 <pow@plt>

LLVM IR (-O0):
%struct.reb_simulation = type { i32 }
define double @reb_random_powerlaw(%struct.reb_simulation* %0, double %1, double %2, double %3) {
%5 = alloca double
%6 = alloca %struct.reb_simulation*
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
store %struct.reb_simulation* %0, %struct.reb_simulation** %6
store double %1, double* %7
store double %2, double* %8
store double %3, double* %9
%11 = load %struct.reb_simulation*, %struct.reb_simulation** %6
%12 = call double @reb_random_uniform(%struct.reb_simulation* %11, i32 0, i32 1)
store double %12, double* %10
%13 = load double, double* %9
%14 = fcmp oeq double %13, -1.000000e+00
br i1 %14, label %15, label %25
15: ; preds = %4
%16 = load double, double* %10
%17 = load double, double* %8
%18 = load double, double* %7
%19 = fdiv double %17, %18
%20 = call double @log(double %19)
%21 = load double, double* %7
%22 = call double @log(double %21)
%23 = call double @llvm.fmuladd.f64(double %16, double %20, double %22)
%24 = call double @exp(double %23)
store double %24, double* %5
br label %49
25: ; preds = %4
%26 = load double, double* %8
%27 = load double, double* %9
%28 = fadd double %27, 1.000000e+00
%29 = fptosi double %28 to i32
%30 = call double @pow(double %26, i32 %29)
%31 = load double, double* %7
%32 = load double, double* %9
%33 = fadd double %32, 1.000000e+00
%34 = fptosi double %33 to i32
%35 = call double @pow(double %31, i32 %34)
%36 = fsub double %30, %35
%37 = load double, double* %10
%38 = load double, double* %7
%39 = load double, double* %9
%40 = fadd double %39, 1.000000e+00
%41 = fptosi double %40 to i32
%42 = call double @pow(double %38, i32 %41)
%43 = call double @llvm.fmuladd.f64(double %36, double %37, double %42)
%44 = load double, double* %9
%45 = fadd double %44, 1.000000e+00
%46 = fdiv double 1.000000e+00, %45
%47 = fptosi double %46 to i32
%48 = call double @pow(double %43, i32 %47)
store double %48, double* %5
br label %49
49: ; preds = %25, %15
%50 = load double, double* %5
ret double %50
}

LLVM IR (-O2):
%struct.reb_simulation = type { i32 }
define double @reb_random_powerlaw(%struct.reb_simulation* %0, double %1, double %2, double %3) {
%5 = tail call double @reb_random_uniform(%struct.reb_simulation* %0, i32 0, i32 1)
%6 = fcmp oeq double %3, -1.000000e+00
br i1 %6, label %7, label %13
7: ; preds = %4
%8 = fdiv double %2, %1
%9 = tail call double @log(double %8)
%10 = tail call double @log(double %1)
%11 = tail call double @llvm.fmuladd.f64(double %5, double %9, double %10)
%12 = tail call double @exp(double %11)
br label %24
13: ; preds = %4
%14 = fadd double %3, 1.000000e+00
%15 = fptosi double %14 to i32
%16 = tail call double @pow(double %2, i32 %15)
%17 = tail call double @pow(double %1, i32 %15)
%18 = fsub double %16, %17
%19 = tail call double @pow(double %1, i32 %15)
%20 = tail call double @llvm.fmuladd.f64(double %18, double %5, double %19)
%21 = fdiv double 1.000000e+00, %14
%22 = fptosi double %21 to i32
%23 = tail call double @pow(double %20, i32 %22)
br label %24
24: ; preds = %13, %7
%25 = phi double [ %12, %7 ], [ %23, %13 ]
ret double %25
}