#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 */
scalar_t__ RAND_MAX;
scalar_t__ random();
int random_draw(float *p, int length) {
  double weight = 0;
  double r = (double)random();
  double max = (double)RAND_MAX;
  double target = r / max;
  int ix = -1;
  do {
    ix++;
    weight += p[ix];
  } while (weight < target && ix < length - 1);
  return ix;
}


x86 assembly (clang, -O0):
<random_draw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    $0x0,%al
call   1030 <random@plt>
cvtsi2sd %rax,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    0x2ea9(%rip),%rax
cvtsi2sdq (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
divsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0xffffffff,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
cvtss2sd %xmm0,%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
xor    %eax,%eax
ucomisd %xmm1,%xmm0
mov    %al,-0x35(%rbp)
jbe    11ad <random_draw+0x9d>
mov    -0x34(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    115f <random_draw+0x4f>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_draw>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <random@plt>
cvtsi2sd %rax,%xmm0
mov    0x2eb9(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
add    $0xffffffff,%r14d
movslq %r14d,%rcx
xorpd  %xmm1,%xmm1
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movapd %xmm1,%xmm2
movss  (%rbx,%rbp,4),%xmm1
cvtss2sd %xmm1,%xmm1
addsd  %xmm2,%xmm1
lea    0x1(%rbp),%rax
ucomisd %xmm1,%xmm0
jbe    1173 <random_draw+0x63>
cmp    %rcx,%rbp
mov    %rax,%rbp
jl     1150 <random_draw+0x40>
add    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@RAND_MAX = global i64 0
define i32 @random_draw(float* %0, i32 %1) {
%3 = alloca float*
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca i32
store float* %0, float** %3
store i32 %1, i32* %4
store double 0.000000e+00, double* %5
%10 = call i64 (...) @random()
%11 = sitofp i64 %10 to double
store double %11, double* %6
%12 = load i64, i64* @RAND_MAX
%13 = sitofp i64 %12 to double
store double %13, double* %7
%14 = load double, double* %6
%15 = load double, double* %7
%16 = fdiv double %14, %15
store double %16, double* %8
store i32 -1, i32* %9
br label %17
17: ; preds = %37, %2
%18 = load i32, i32* %9
%19 = add nsw i32 %18, 1
store i32 %19, i32* %9
%20 = load float*, float** %3
%21 = load i32, i32* %9
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds float, float* %20, i64 %22
%24 = load float, float* %23
%25 = fpext float %24 to double
%26 = load double, double* %5
%27 = fadd double %26, %25
store double %27, double* %5
br label %28
28: ; preds = %17
%29 = load double, double* %5
%30 = load double, double* %8
%31 = fcmp olt double %29, %30
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* %9
%34 = load i32, i32* %4
%35 = sub nsw i32 %34, 1
%36 = icmp slt i32 %33, %35
br label %37
37: ; preds = %32, %28
%38 = phi i1 [ false, %28 ], [ %36, %32 ]
br i1 %38, label %17, label %39
39: ; preds = %37
%40 = load i32, i32* %9
ret i32 %40
}

LLVM IR (-O2):
@RAND_MAX = global i64 0
define i32 @random_draw(float* nocapture %0, i32 %1) {
%3 = tail call i64 (...) @random()
%4 = sitofp i64 %3 to double
%5 = load i64, i64* @RAND_MAX
%6 = sitofp i64 %5 to double
%7 = fdiv double %4, %6
%8 = add nsw i32 %1, -1
%9 = sext i32 %8 to i64
br label %10
10: ; preds = %10, %2
%11 = phi i64 [ %13, %10 ], [ -1, %2 ]
%12 = phi double [ %17, %10 ], [ 0.000000e+00, %2 ]
%13 = add nsw i64 %11, 1
%14 = getelementptr inbounds float, float* %0, i64 %13
%15 = load float, float* %14
%16 = fpext float %15 to double
%17 = fadd double %12, %16
%18 = fcmp olt double %17, %7
%19 = icmp slt i64 %13, %9
%20 = select i1 %18, i1 %19, i1 false
br i1 %20, label %10, label %21
21: ; preds = %10
%22 = trunc i64 %13 to i32
ret i32 %22
}