#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__ frand();
int rand_vector(double *Pi, int size) {
  int i;
  double r = (double)frand();
  double 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    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x0,%al
call   1030 <frand@plt>
cvtsi2sd %rax,%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1194 <rand_vector+0x84>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     1181 <rand_vector+0x71>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119b <rand_vector+0x8b>
jmp    1186 <rand_vector+0x76>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113f <rand_vector+0x2f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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    %rax,%rcx
mov    $0xffffffff,%eax
test   %ebp,%ebp
jle    1158 <rand_vector+0x48>
cvtsi2sd %rcx,%xmm0
mov    %ebp,%ecx
xorpd  %xmm1,%xmm1
xor    %edx,%edx
nopl   0x0(%rax,%rax,1)
addsd  (%rbx,%rdx,8),%xmm1
ucomisd %xmm0,%xmm1
jae    1156 <rand_vector+0x46>
add    $0x1,%rdx
cmp    %rdx,%rcx
jne    1140 <rand_vector+0x30>
jmp    1158 <rand_vector+0x48>
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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