#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 drand();
int rand_vector(double *Pi, int size) {
  int i;
  double r = drand();
  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 <drand@plt>
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    118f <rand_vector+0x7f>
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     117c <rand_vector+0x6c>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1196 <rand_vector+0x86>
jmp    1181 <rand_vector+0x71>
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    $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 <drand@plt>
mov    $0xffffffff,%eax
test   %ebp,%ebp
jle    1148 <rand_vector+0x38>
mov    %ebp,%ecx
xorpd  %xmm1,%xmm1
xor    %edx,%edx
addsd  (%rbx,%rdx,8),%xmm1
ucomisd %xmm0,%xmm1
jae    1146 <rand_vector+0x36>
add    $0x1,%rdx
cmp    %rdx,%rcx
jne    1130 <rand_vector+0x20>
jmp    1148 <rand_vector+0x38>
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 double (...) @drand()
store double %9, double* %7
store double 0.000000e+00, double* %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 double*, double** %4
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds double, double* %15, i64 %17
%19 = load double, double* %18
%20 = load double, double* %8
%21 = fadd double %20, %19
store double %21, double* %8
%22 = load double, double* %7
%23 = load double, double* %8
%24 = fcmp ole double %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(double* nocapture %0, i32 %1) {
%3 = tail call double (...) @drand()
%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 double [ 0.000000e+00, %5 ], [ %12, %14 ]
%10 = getelementptr inbounds double, double* %0, i64 %8
%11 = load double, double* %10
%12 = fadd double %9, %11
%13 = fcmp ugt double %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
}