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


x86 assembly (clang, -O0):
<roulette_wheel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    $0x0,%al
call   1030 <genrand_real1@plt>
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1185 <roulette_wheel+0x75>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
ucomisd -0x20(%rbp),%xmm0
jb     1172 <roulette_wheel+0x62>
jmp    1185 <roulette_wheel+0x75>
jmp    1177 <roulette_wheel+0x67>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    113a <roulette_wheel+0x2a>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<roulette_wheel>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <genrand_real1@plt>
test   %ebp,%ebp
jle    1147 <roulette_wheel+0x37>
mov    %ebp,%eax
xorpd  %xmm1,%xmm1
xor    %ebx,%ebx
xchg   %ax,%ax
addsd  (%r14,%rbx,8),%xmm1
ucomisd %xmm0,%xmm1
jae    1147 <roulette_wheel+0x37>
add    $0x1,%rbx
cmp    %rbx,%rax
jne    1130 <roulette_wheel+0x20>
mov    %eax,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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