#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 ruspma_rand_decimal();
int categorical_draw(double *probs, int length) {
  double compare_prob = ruspma_rand_decimal();
  double culm_prob = 0.0;
  int i;
  for (i = 0; i != length; ++i) {
    culm_prob += probs[i];
    if (culm_prob > compare_prob)
      return i;
  }
  return length - 1;
}


x86 assembly (clang, -O0):
<categorical_draw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x0,%al
call   1030 <ruspma_rand_decimal@plt>
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     118b <categorical_draw+0x7b>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
ucomisd -0x20(%rbp),%xmm0
jbe    1178 <categorical_draw+0x68>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1194 <categorical_draw+0x84>
jmp    117d <categorical_draw+0x6d>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    113a <categorical_draw+0x2a>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<categorical_draw>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <ruspma_rand_decimal@plt>
test   %ebp,%ebp
je     1144 <categorical_draw+0x34>
mov    %ebp,%eax
xorpd  %xmm1,%xmm1
xor    %ecx,%ecx
nopl   0x0(%rax,%rax,1)
addsd  (%rbx,%rcx,8),%xmm1
ucomisd %xmm0,%xmm1
ja     1149 <categorical_draw+0x39>
add    $0x1,%rcx
cmp    %rcx,%rax
jne    1130 <categorical_draw+0x20>
add    $0xffffffff,%ebp
jmp    114b <categorical_draw+0x3b>
mov    %ecx,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @categorical_draw(double* nocapture %0, i32 %1) {
%3 = tail call double (...) @ruspma_rand_decimal()
%4 = icmp eq i32 %1, 0
br i1 %4, label %17, label %5
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 ogt double %12, %3
br i1 %13, label %19, label %14
14: ; preds = %7
%15 = add nuw nsw i64 %8, 1
%16 = icmp eq i64 %15, %6
br i1 %16, label %17, label %7
17: ; preds = %14, %2
%18 = add nsw i32 %1, -1
br label %21
19: ; preds = %7
%20 = trunc i64 %8 to i32
br label %21
21: ; preds = %19, %17
%22 = phi i32 [ %18, %17 ], [ %20, %19 ]
ret i32 %22
}