#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 drand48();
int random_sample(int arraylen, double arraysum, double *array) {
  double sampled = arraysum * drand48();
  double cumsum = 0;
  for (int i = 0; i < arraylen; ++i) {
    cumsum += array[i];
    if (sampled < cumsum)
      return i;
  }
  return arraylen - 1;
}


x86 assembly (clang, -O0):
<random_sample>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    $0x0,%al
call   1030 <drand48@plt>
movaps %xmm0,%xmm1
movsd  -0x38(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11aa <random_sample+0x9a>
mov    -0x18(%rbp),%rax
movslq -0x2c(%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
jbe    1197 <random_sample+0x87>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b3 <random_sample+0xa3>
jmp    119c <random_sample+0x8c>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1155 <random_sample+0x45>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_sample>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
movsd  %xmm0,0x8(%rsp)
mov    %edi,%r14d
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <drand48@plt>
test   %r14d,%r14d
jle    117d <random_sample+0x6d>
xorpd  %xmm1,%xmm1
addsd  (%rbx),%xmm1
mulsd  0x8(%rsp),%xmm0
mov    $0x1,%cl
ucomisd %xmm0,%xmm1
ja     118f <random_sample+0x7f>
mov    %r14d,%eax
lea    -0x1(%rax),%rcx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    %rbp,%rcx
je     1183 <random_sample+0x73>
addsd  0x8(%rbx,%rbp,8),%xmm1
add    $0x1,%rbp
ucomisd %xmm0,%xmm1
jbe    1160 <random_sample+0x50>
cmp    %rax,%rbp
setb   %cl
jmp    118f <random_sample+0x7f>
add    $0xffffffff,%r14d
jmp    1199 <random_sample+0x89>
add    $0x1,%rbp
cmp    %rax,%rbp
setb   %cl
mov    %eax,%ebp
add    $0xffffffff,%r14d
test   %cl,%cl
cmovne %ebp,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @random_sample(i32 %0, double %1, double* nocapture %2) {
%4 = tail call double (...) @drand48()
%5 = fmul double %4, %1
%6 = icmp sgt i32 %0, 0
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = add nsw i32 %0, -1
br label %35
9: ; preds = %3
%10 = zext i32 %0 to i64
%11 = zext i32 %0 to i64
%12 = load double, double* %2
%13 = fadd double %12, 0.000000e+00
%14 = fcmp olt double %5, %13
br i1 %14, label %30, label %20
15: ; preds = %20
%16 = getelementptr inbounds double, double* %2, i64 %23
%17 = load double, double* %16
%18 = fadd double %21, %17
%19 = fcmp olt double %5, %18
br i1 %19, label %25, label %20
20: ; preds = %9, %15
%21 = phi double [ %18, %15 ], [ %13, %9 ]
%22 = phi i64 [ %23, %15 ], [ 0, %9 ]
%23 = add nuw nsw i64 %22, 1
%24 = icmp eq i64 %23, %11
br i1 %24, label %28, label %15
25: ; preds = %15
%26 = icmp ult i64 %23, %10
%27 = trunc i64 %23 to i32
br label %30
28: ; preds = %20
%29 = icmp ult i64 %23, %10
br label %30
30: ; preds = %28, %9, %25
%31 = phi i32 [ %27, %25 ], [ 0, %9 ], [ %0, %28 ]
%32 = phi i1 [ %26, %25 ], [ true, %9 ], [ %29, %28 ]
%33 = add nsw i32 %0, -1
%34 = select i1 %32, i32 %31, i32 %33
br label %35
35: ; preds = %30, %7
%36 = phi i32 [ %8, %7 ], [ %34, %30 ]
ret i32 %36
}