#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 random_double2(double);
int roulette_wheel2(const double *array, int len, double tot) {
  double accum = 0.0;
  double rnum = random_double2(tot);
  int i = 0;
  for (; i < len; i++) {
    accum += array[i];
    if (accum >= rnum)
      break;
  }
  return i;
}


x86 assembly (clang, -O0):
<roulette_wheel2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
call   1030 <random_double2@plt>
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    118d <roulette_wheel2+0x7d>
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
ucomisd -0x28(%rbp),%xmm0
jb     117a <roulette_wheel2+0x6a>
jmp    118d <roulette_wheel2+0x7d>
jmp    117f <roulette_wheel2+0x6f>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1142 <roulette_wheel2+0x32>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<roulette_wheel2>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <random_double2@plt>
test   %ebp,%ebp
jle    1148 <roulette_wheel2+0x38>
mov    %ebp,%ecx
xorpd  %xmm1,%xmm1
xor    %eax,%eax
nopl   0x0(%rax)
addsd  (%rbx,%rax,8),%xmm1
ucomisd %xmm0,%xmm1
jae    114a <roulette_wheel2+0x3a>
add    $0x1,%rax
cmp    %rax,%rcx
jne    1130 <roulette_wheel2+0x20>
mov    %ecx,%eax
jmp    114a <roulette_wheel2+0x3a>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @roulette_wheel2(double* %0, i32 %1, double %2) {
%4 = alloca double*
%5 = alloca i32
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca i32
store double* %0, double** %4
store i32 %1, i32* %5
store double %2, double* %6
store double 0.000000e+00, double* %7
%10 = load double, double* %6
%11 = call double @random_double2(double %10)
store double %11, double* %8
store i32 0, i32* %9
br label %12
12: ; preds = %29, %3
%13 = load i32, i32* %9
%14 = load i32, i32* %5
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %32
16: ; preds = %12
%17 = load double*, double** %4
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds double, double* %17, i64 %19
%21 = load double, double* %20
%22 = load double, double* %7
%23 = fadd double %22, %21
store double %23, double* %7
%24 = load double, double* %7
%25 = load double, double* %8
%26 = fcmp oge double %24, %25
br i1 %26, label %27, label %28
27: ; preds = %16
br label %32
28: ; preds = %16
br label %29
29: ; preds = %28
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
br label %12
32: ; preds = %27, %12
%33 = load i32, i32* %9
ret i32 %33
}

LLVM IR (-O2):
define i32 @roulette_wheel2(double* nocapture %0, i32 %1, double %2) {
%4 = tail call double @random_double2(double %2)
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %20
6: ; preds = %3
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %6, %15
%9 = phi i64 [ 0, %6 ], [ %16, %15 ]
%10 = phi double [ 0.000000e+00, %6 ], [ %13, %15 ]
%11 = getelementptr inbounds double, double* %0, i64 %9
%12 = load double, double* %11
%13 = fadd double %10, %12
%14 = fcmp ult double %13, %4
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = add nuw nsw i64 %9, 1
%17 = icmp eq i64 %16, %7
br i1 %17, label %20, label %8
18: ; preds = %8
%19 = trunc i64 %9 to i32
br label %20
20: ; preds = %15, %18, %3
%21 = phi i32 [ 0, %3 ], [ %19, %18 ], [ %1, %15 ]
ret i32 %21
}