#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 RAND_MAX;
scalar_t__ rand();
int rand_event(double *P, int N) {
  const double eps = 1e-9;
  int k;
  double r = ((double)rand()) / (RAND_MAX);
  for (k = 1; k <= N; k++) {
    r -= P[k - 1];
    if (r < eps)
      return k - 1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<rand_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movsd  0xed9(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    $0x0,%al
call   1030 <rand@plt>
cvtsi2sd %rax,%xmm0
mov    0x2ea9(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     11af <rand_event+0x9f>
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm1
movsd  -0x30(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  0xe7d(%rip),%xmm0
ucomisd -0x30(%rbp),%xmm0
jbe    119c <rand_event+0x8c>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11b6 <rand_event+0xa6>
jmp    11a1 <rand_event+0x91>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    114f <rand_event+0x3f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_event>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <rand@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %ebp,%ebp
jle    1168 <rand_event+0x58>
cvtsi2sd %rcx,%xmm0
mov    0x2eb1(%rip),%rcx
divsd  (%rcx),%xmm0
mov    %ebp,%edx
xor    %ecx,%ecx
movsd  0xeb9(%rip),%xmm1
nopw   0x0(%rax,%rax,1)
subsd  (%rbx,%rcx,8),%xmm0
ucomisd %xmm0,%xmm1
ja     1166 <rand_event+0x56>
add    $0x1,%rcx
cmp    %rcx,%rdx
jne    1150 <rand_event+0x40>
jmp    1168 <rand_event+0x58>
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@RAND_MAX = global double 0.000000e+00
define i32 @rand_event(double* nocapture %0, i32 %1) {
%3 = tail call i64 (...) @rand()
%4 = icmp slt i32 %1, 1
br i1 %4, label %24, label %5
5: ; preds = %2
%6 = sitofp i64 %3 to double
%7 = load double, double* @RAND_MAX
%8 = fdiv double %6, %7
%9 = add nuw i32 %1, 1
%10 = zext i32 %9 to i64
br label %14
11: ; preds = %14
%12 = add nuw nsw i64 %15, 1
%13 = icmp eq i64 %12, %10
br i1 %13, label %24, label %14
14: ; preds = %5, %11
%15 = phi i64 [ 1, %5 ], [ %12, %11 ]
%16 = phi double [ %8, %5 ], [ %20, %11 ]
%17 = add nsw i64 %15, -1
%18 = getelementptr inbounds double, double* %0, i64 %17
%19 = load double, double* %18
%20 = fsub double %16, %19
%21 = fcmp olt double %20, 1.000000e-09
br i1 %21, label %22, label %11
22: ; preds = %14
%23 = trunc i64 %17 to i32
br label %24
24: ; preds = %11, %22, %2
%25 = phi i32 [ -1, %2 ], [ %23, %22 ], [ -1, %11 ]
ret i32 %25
}