#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 uniformGenerator();
int getWeightedIntRandom(double *weights) {
  double temp, partialSum;
  int returnValue = 0;
  partialSum = weights[0];
  temp = uniformGenerator();
  while (temp < partialSum) {
    returnValue++;
    partialSum += weights[returnValue];
  }
  returnValue--;
  return (returnValue);
}


x86 assembly (clang, -O0):
<getWeightedIntRandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    $0x0,%al
call   1030 <uniformGenerator@plt>
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1175 <getWeightedIntRandom+0x65>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    113c <getWeightedIntRandom+0x2c>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getWeightedIntRandom>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movsd  (%rdi),%xmm0
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   1030 <uniformGenerator@plt>
movsd  0x8(%rsp),%xmm1
mov    $0xffffffff,%eax
ucomisd %xmm0,%xmm1
jbe    1161 <getWeightedIntRandom+0x51>
add    $0x8,%rbx
mov    $0xffffffff,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
addsd  (%rbx),%xmm1
add    $0x1,%eax
add    $0x8,%rbx
ucomisd %xmm0,%xmm1
ja     1150 <getWeightedIntRandom+0x40>
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
define i32 @getWeightedIntRandom(double* nocapture %0) {
%2 = load double, double* %0
%3 = tail call double (...) @uniformGenerator()
%4 = fcmp olt double %3, %2
br i1 %4, label %5, label %16
5: ; preds = %1, %5
%6 = phi i64 [ %8, %5 ], [ 0, %1 ]
%7 = phi double [ %11, %5 ], [ %2, %1 ]
%8 = add nuw nsw i64 %6, 1
%9 = getelementptr inbounds double, double* %0, i64 %8
%10 = load double, double* %9
%11 = fadd double %7, %10
%12 = fcmp olt double %3, %11
br i1 %12, label %5, label %13
13: ; preds = %5
%14 = trunc i64 %8 to i32
%15 = add i32 %14, -1
br label %16
16: ; preds = %13, %1
%17 = phi i32 [ -1, %1 ], [ %15, %13 ]
ret i32 %17
}