#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 get_rand_int(int /*<<< orphan*/, int);
scalar_t__ percentage_chance(double);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int test_rand() {
  int i;
  int num;
  int counter;
  double frac;
  int results[(10)];
  printf("\n### RANDOM UTILITY UNIT TEST ###\n");
  srand(time(NULL));
  printf("\nTesting get_rand_int() with %d bins\n", (10));
  for (i = 0; i < (10); i++)
    results[i] = 0;
  for (i = 0; i < (10000000); i++) {
    num = get_rand_int(0, (10));
    results[num]++;
  }
  for (i = 0; i < (10); i++) {
    frac = (double)results[i] / (10000000);
    printf("Bin %d: %lf\n", i, frac);
  }
  printf("Expected fraction: %lf\n", 1.0 / (10));
  printf("\nTesting percentage_chance() with rate %lf\n", (0.5));
  counter = 0;
  for (i = 0; i < (10000000); i++) {
    if (percentage_chance((0.5)))
      counter++;
  }
  printf("Percentage chance fraction: %lf\n", (double)counter / (10000000));
  printf("Expected fraction: %lf\n", (double)(0.5));
  return 0;
}


x86 assembly (clang, -O0):
<test_rand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%edi
call   1050 <srand@plt>
lea    0xebe(%rip),%rdi
mov    $0xa,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
cmpl   $0xa,-0x4(%rbp)
jge    11b4 <test_rand+0x64>
movslq -0x4(%rbp),%rax
movl   $0x0,-0x40(%rbp,%rax,4)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    1190 <test_rand+0x40>
movl   $0x0,-0x4(%rbp)
cmpl   $0x989680,-0x4(%rbp)
jge    11f4 <test_rand+0xa4>
xor    %edi,%edi
mov    $0xa,%esi
call   1030 <get_rand_int@plt>
mov    %eax,-0x8(%rbp)
movslq -0x8(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%ecx
add    $0x1,%ecx
mov    %ecx,-0x40(%rbp,%rax,4)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11bb <test_rand+0x6b>
movl   $0x0,-0x4(%rbp)
cmpl   $0xa,-0x4(%rbp)
jge    1244 <test_rand+0xf4>
movslq -0x4(%rbp),%rax
cvtsi2sdl -0x40(%rbp,%rax,4),%xmm0
movsd  0xdf9(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x4(%rbp),%esi
movsd  -0x18(%rbp),%xmm0
lea    0xe32(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11fb <test_rand+0xab>
lea    0xe23(%rip),%rdi
movsd  0xdb5(%rip),%xmm0
mov    $0x1,%al
call   1040 <printf@plt>
lea    0xe25(%rip),%rdi
movsd  0xd97(%rip),%xmm0
mov    $0x1,%al
call   1040 <printf@plt>
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x4(%rbp)
cmpl   $0x989680,-0x4(%rbp)
jge    12be <test_rand+0x16e>
movsd  0xd6d(%rip),%xmm0
call   1070 <percentage_chance@plt>
cmp    $0x0,%rax
je     12ab <test_rand+0x15b>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12b0 <test_rand+0x160>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    127e <test_rand+0x12e>
cvtsi2sdl -0xc(%rbp),%xmm0
movsd  0xd45(%rip),%xmm1
divsd  %xmm1,%xmm0
lea    0xddc(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
lea    0xd8a(%rip),%rdi
movsd  0xd14(%rip),%xmm0
mov    $0x1,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_rand>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
lea    0xf43(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
lea    0xe91(%rip),%rdi
mov    $0xa,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x20(%rsp)
mov    $0x989680,%ebx
nopl   (%rax)
xor    %edi,%edi
mov    $0xa,%esi
call   1030 <get_rand_int@plt>
cltq
addl   $0x1,(%rsp,%rax,4)
add    $0xffffffff,%ebx
jne    11b0 <test_rand+0x50>
xorps  %xmm0,%xmm0
cvtsi2sdl (%rsp),%xmm0
divsd  0xe29(%rip),%xmm0
lea    0xe60(%rip),%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1,%al
call   1050 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2sdl 0x4(%rsp),%xmm0
divsd  0xe03(%rip),%xmm0
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1,%al
call   1050 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2sdl 0x8(%rsp),%xmm0
divsd  0xde3(%rip),%xmm0
mov    %rbx,%rdi
mov    $0x2,%esi
mov    $0x1,%al
call   1050 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2sdl 0xc(%rsp),%xmm0
divsd  0xdc3(%rip),%xmm0
mov    %rbx,%rdi
mov    $0x3,%esi
mov    $0x1,%al
call   1050 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2sdl 0x10(%rsp),%xmm0
divsd  0xda3(%rip),%xmm0
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x1,%al
call   1050 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2sdl 0x14(%rsp),%xmm0
divsd  0xd83(%rip),%xmm0
mov    %rbx,%rdi
mov    $0x5,%esi
mov    $0x1,%al
call   1050 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2sdl 0x18(%rsp),%xmm0
divsd  0xd63(%rip),%xmm0
mov    %rbx,%rdi
mov    $0x6,%esi
mov    $0x1,%al
call   1050 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2sdl 0x1c(%rsp),%xmm0
divsd  0xd43(%rip),%xmm0
mov    %rbx,%rdi
mov    $0x7,%esi
mov    $0x1,%al
call   1050 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2sdl 0x20(%rsp),%xmm0
divsd  0xd23(%rip),%xmm0
mov    %rbx,%rdi
mov    $0x8,%esi
mov    $0x1,%al
call   1050 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2sdl 0x24(%rsp),%xmm0
divsd  0xd03(%rip),%xmm0
mov    %rbx,%rdi
mov    $0x9,%esi
mov    $0x1,%al
call   1050 <printf@plt>
lea    0xd38(%rip),%rdi
movsd  0xced(%rip),%xmm0
mov    $0x1,%al
call   1050 <printf@plt>
lea    0xd3a(%rip),%rdi
movsd  0xcdf(%rip),%xmm0
mov    $0x1,%al
call   1050 <printf@plt>
mov    $0x989680,%ebx
nopl   (%rax)
movsd  0xcc8(%rip),%xmm0
call   1080 <percentage_chance@plt>
cmp    $0x1,%rax
sbb    $0xffffffff,%ebp
add    $0xffffffff,%ebx
jne    1340 <test_rand+0x1e0>
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
divsd  0xc98(%rip),%xmm0
lea    0xd20(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
lea    0xcce(%rip),%rdi
movsd  0xc8b(%rip),%xmm0
mov    $0x1,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"\0A### RANDOM UTILITY UNIT TEST ###\0A\00"
@.str.1 = [38 x i8] c"\0ATesting get_rand_int() with %d bins\0A\00"
@.str.2 = [13 x i8] c"Bin %d: %lf\0A\00"
@.str.3 = [24 x i8] c"Expected fraction: %lf\0A\00"
@.str.4 = [44 x i8] c"\0ATesting percentage_chance() with rate %lf\0A\00"
@.str.5 = [33 x i8] c"Percentage chance fraction: %lf\0A\00"
define i32 @test_rand() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca double
%5 = alloca [10 x i32]
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @time(i32* null)
%8 = call i32 @srand(i32 %7)
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 10)
store i32 0, i32* %1
br label %10
10: ; preds = %17, %0
%11 = load i32, i32* %1
%12 = icmp slt i32 %11, 10
br i1 %12, label %13, label %20
13: ; preds = %10
%14 = load i32, i32* %1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [10 x i32], [10 x i32]* %5, i64 0, i64 %15
store i32 0, i32* %16
br label %17
17: ; preds = %13
%18 = load i32, i32* %1
%19 = add nsw i32 %18, 1
store i32 %19, i32* %1
br label %10
20: ; preds = %10
store i32 0, i32* %1
br label %21
21: ; preds = %31, %20
%22 = load i32, i32* %1
%23 = icmp slt i32 %22, 10000000
br i1 %23, label %24, label %34
24: ; preds = %21
%25 = call i32 @get_rand_int(i32 0, i32 10)
store i32 %25, i32* %2
%26 = load i32, i32* %2
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [10 x i32], [10 x i32]* %5, i64 0, i64 %27
%29 = load i32, i32* %28
%30 = add nsw i32 %29, 1
store i32 %30, i32* %28
br label %31
31: ; preds = %24
%32 = load i32, i32* %1
%33 = add nsw i32 %32, 1
store i32 %33, i32* %1
br label %21
34: ; preds = %21
store i32 0, i32* %1
br label %35
35: ; preds = %48, %34
%36 = load i32, i32* %1
%37 = icmp slt i32 %36, 10
br i1 %37, label %38, label %51
38: ; preds = %35
%39 = load i32, i32* %1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [10 x i32], [10 x i32]* %5, i64 0, i64 %40
%42 = load i32, i32* %41
%43 = sitofp i32 %42 to double
%44 = fdiv double %43, 1.000000e+07
store double %44, double* %4
%45 = load i32, i32* %1
%46 = load double, double* %4
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %45, double %46)
br label %48
48: ; preds = %38
%49 = load i32, i32* %1
%50 = add nsw i32 %49, 1
store i32 %50, i32* %1
br label %35
51: ; preds = %35
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), double 1.000000e-01)
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), double 5.000000e-01)
store i32 0, i32* %3
store i32 0, i32* %1
br label %54
54: ; preds = %64, %51
%55 = load i32, i32* %1
%56 = icmp slt i32 %55, 10000000
br i1 %56, label %57, label %67
57: ; preds = %54
%58 = call i64 @percentage_chance(double 5.000000e-01)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = load i32, i32* %3
%62 = add nsw i32 %61, 1
store i32 %62, i32* %3
br label %63
63: ; preds = %60, %57
br label %64
64: ; preds = %63
%65 = load i32, i32* %1
%66 = add nsw i32 %65, 1
store i32 %66, i32* %1
br label %54
67: ; preds = %54
%68 = load i32, i32* %3
%69 = sitofp i32 %68 to double
%70 = fdiv double %69, 1.000000e+07
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), double %70)
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), double 5.000000e-01)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [38 x i8] c"\0ATesting get_rand_int() with %d bins\0A\00"
@.str.2 = [13 x i8] c"Bin %d: %lf\0A\00"
@.str.3 = [24 x i8] c"Expected fraction: %lf\0A\00"
@.str.4 = [44 x i8] c"\0ATesting percentage_chance() with rate %lf\0A\00"
@.str.5 = [33 x i8] c"Percentage chance fraction: %lf\0A\00"
@str = [34 x i8] c"\0A### RANDOM UTILITY UNIT TEST ###\00"
define i32 @test_rand() {
%1 = alloca [10 x i32]
%2 = bitcast [10 x i32]* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @time(i32* null)
%5 = tail call i32 @srand(i32 %4)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 10)
br label %7
7: ; preds = %0, %7
%8 = phi i32 [ %14, %7 ], [ 0, %0 ]
%9 = tail call i32 @get_rand_int(i32 0, i32 10)
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 %10
%12 = load i32, i32* %11
%13 = add nsw i32 %12, 1
store i32 %13, i32* %11
%14 = add nuw nsw i32 %8, 1
%15 = icmp eq i32 %14, 10000000
br i1 %15, label %16, label %7
16: ; preds = %7
%17 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 0
%18 = load i32, i32* %17
%19 = sitofp i32 %18 to double
%20 = fdiv double %19, 1.000000e+07
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 0, double %20)
%22 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 1
%23 = load i32, i32* %22
%24 = sitofp i32 %23 to double
%25 = fdiv double %24, 1.000000e+07
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 1, double %25)
%27 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 2
%28 = load i32, i32* %27
%29 = sitofp i32 %28 to double
%30 = fdiv double %29, 1.000000e+07
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 2, double %30)
%32 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 3
%33 = load i32, i32* %32
%34 = sitofp i32 %33 to double
%35 = fdiv double %34, 1.000000e+07
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 3, double %35)
%37 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 4
%38 = load i32, i32* %37
%39 = sitofp i32 %38 to double
%40 = fdiv double %39, 1.000000e+07
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 4, double %40)
%42 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 5
%43 = load i32, i32* %42
%44 = sitofp i32 %43 to double
%45 = fdiv double %44, 1.000000e+07
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 5, double %45)
%47 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 6
%48 = load i32, i32* %47
%49 = sitofp i32 %48 to double
%50 = fdiv double %49, 1.000000e+07
%51 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 6, double %50)
%52 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 7
%53 = load i32, i32* %52
%54 = sitofp i32 %53 to double
%55 = fdiv double %54, 1.000000e+07
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 7, double %55)
%57 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 8
%58 = load i32, i32* %57
%59 = sitofp i32 %58 to double
%60 = fdiv double %59, 1.000000e+07
%61 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 8, double %60)
%62 = getelementptr inbounds [10 x i32], [10 x i32]* %1, i64 0, i64 9
%63 = load i32, i32* %62
%64 = sitofp i32 %63 to double
%65 = fdiv double %64, 1.000000e+07
%66 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 9, double %65)
%67 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), double 1.000000e-01)
%68 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), double 5.000000e-01)
br label %69
69: ; preds = %16, %69
%70 = phi i32 [ 0, %16 ], [ %76, %69 ]
%71 = phi i32 [ 0, %16 ], [ %75, %69 ]
%72 = tail call i64 @percentage_chance(double 5.000000e-01)
%73 = icmp ne i64 %72, 0
%74 = zext i1 %73 to i32
%75 = add nuw nsw i32 %71, %74
%76 = add nuw nsw i32 %70, 1
%77 = icmp eq i32 %76, 10000000
br i1 %77, label %78, label %69
78: ; preds = %69
%79 = sitofp i32 %75 to double
%80 = fdiv double %79, 1.000000e+07
%81 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), double %80)
%82 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), double 5.000000e-01)
ret i32 0
}