#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 frand();
int rand_vector(float *Pi, int size) {
  int i;
  float r = frand();
  float sum = 0;
  for (i = 0; i < size; i++) {
    sum += Pi[i];
    if (r <= sum) {
      return i;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<rand_vector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x0,%al
call   1030 <frand@plt>
movss  %xmm0,-0x1c(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    118e <rand_vector+0x7e>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movss  (%rax,%rcx,4),%xmm0
addss  -0x20(%rbp),%xmm0
movss  %xmm0,-0x20(%rbp)
movss  -0x1c(%rbp),%xmm1
movss  -0x20(%rbp),%xmm0
ucomiss %xmm1,%xmm0
jb     117b <rand_vector+0x6b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1195 <rand_vector+0x85>
jmp    1180 <rand_vector+0x70>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113a <rand_vector+0x2a>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_vector>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <frand@plt>
mov    $0xffffffff,%eax
test   %ebp,%ebp
jle    1147 <rand_vector+0x37>
mov    %ebp,%ecx
xorps  %xmm1,%xmm1
xor    %edx,%edx
nop
addss  (%rbx,%rdx,4),%xmm1
ucomiss %xmm0,%xmm1
jae    1145 <rand_vector+0x35>
add    $0x1,%rdx
cmp    %rdx,%rcx
jne    1130 <rand_vector+0x20>
jmp    1147 <rand_vector+0x37>
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @rand_vector(float* nocapture %0, i32 %1) {
%3 = tail call float (...) @frand()
%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 float [ 0.000000e+00, %5 ], [ %12, %14 ]
%10 = getelementptr inbounds float, float* %0, i64 %8
%11 = load float, float* %10
%12 = fadd float %9, %11
%13 = fcmp ugt float %3, %12
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 [ -1, %2 ], [ %18, %17 ], [ -1, %14 ]
ret i32 %20
}