#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 */
float bow_random_01();
int random_index_from_multinomial(float *p, int size) {
  float r = bow_random_01();
  int k;
  float sum;
  for (k = -1, sum = 0; sum < r; sum += p[k])
    k++;
  return k;
}


x86 assembly (clang, -O0):
<random_index_from_multinomial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1030 <bow_random_01@plt>
movss  %xmm0,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x18(%rbp)
movss  -0x18(%rbp),%xmm1
movss  -0x10(%rbp),%xmm0
ucomiss %xmm1,%xmm0
jbe    1172 <random_index_from_multinomial+0x62>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
addss  -0x18(%rbp),%xmm0
movss  %xmm0,-0x18(%rbp)
jmp    113a <random_index_from_multinomial+0x2a>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_index_from_multinomial>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <bow_random_01@plt>
mov    $0xffffffff,%eax
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jbe    1140 <random_index_from_multinomial+0x30>
mov    $0xffffffff,%eax
nopl   (%rax)
addss  (%rbx),%xmm1
add    $0x1,%eax
add    $0x4,%rbx
ucomiss %xmm1,%xmm0
ja     1130 <random_index_from_multinomial+0x20>
pop    %rbx
ret

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

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