#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__ RAND_MAX;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ rand();
int _random_choice(const double *p, const int N) {
  double dR = (double)rand();
  double dM = (double)RAND_MAX;
  double r = dR / (dM + 1.0);
  double s = 0.0;
  int i;
  for (i = 0; i < N; i++) {
    s += p[i];
    if (s >= r) {
      return (i);
    }
  }
  printf("ERROR: random select method could not select anything. Probably p is "
         "not normalized.\n");
  for (i = 0; i < N; i++) {
    printf("%i \t %f\n", i, p[i]);
  }
  exit(1);
}


x86 assembly (clang, -O0):
<_random_choice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1050 <rand@plt>
cvtsi2sd %rax,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    0x2e91(%rip),%rax
cvtsi2sdq (%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xe92(%rip),%xmm1
addsd  -0x20(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11da <_random_choice+0xaa>
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
ucomisd -0x28(%rbp),%xmm0
jb     11c7 <_random_choice+0x97>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret
jmp    11cc <_random_choice+0x9c>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    118b <_random_choice+0x5b>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1227 <_random_choice+0xf7>
mov    -0x34(%rbp),%esi
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xe4c(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11ef <_random_choice+0xbf>
mov    $0x1,%edi
call   1040 <exit@plt>

x86 assembly (clang, -O2):
<_random_choice>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r15
xor    %eax,%eax
call   1060 <rand@plt>
test   %ebp,%ebp
jle    11e0 <_random_choice+0xa0>
cvtsi2sd %rax,%xmm0
mov    0x2e81(%rip),%rax
cvtsi2sdq (%rax),%xmm1
addsd  0xe8c(%rip),%xmm1
divsd  %xmm1,%xmm0
mov    %ebp,%ebx
xorpd  %xmm1,%xmm1
xor    %eax,%eax
addsd  (%r15,%rax,8),%xmm1
ucomisd %xmm0,%xmm1
jae    11d5 <_random_choice+0x95>
add    $0x1,%rax
cmp    %rax,%rbx
jne    1180 <_random_choice+0x40>
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
test   %ebp,%ebp
jle    11cb <_random_choice+0x8b>
xor    %ebp,%ebp
lea    0xe5a(%rip),%r14
xchg   %ax,%ax
movsd  (%r15,%rbp,8),%xmm0
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x1,%al
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11b0 <_random_choice+0x70>
mov    $0x1,%edi
call   1050 <exit@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe2a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@RAND_MAX = global i64 0
@.str = [86 x i8] c"ERROR: random select method could not select anything. Probably p is not normalized.\0A\00"
@.str.1 = [9 x i8] c"%i \09 %f\0A\00"
define i32 @_random_choice(double* %0, i32 %1) {
%3 = alloca double*
%4 = alloca i32
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca i32
store double* %0, double** %3
store i32 %1, i32* %4
%10 = call i64 (...) @rand()
%11 = sitofp i64 %10 to double
store double %11, double* %5
%12 = load i64, i64* @RAND_MAX
%13 = sitofp i64 %12 to double
store double %13, double* %6
%14 = load double, double* %5
%15 = load double, double* %6
%16 = fadd double %15, 1.000000e+00
%17 = fdiv double %14, %16
store double %17, double* %7
store double 0.000000e+00, double* %8
store i32 0, i32* %9
br label %18
18: ; preds = %36, %2
%19 = load i32, i32* %9
%20 = load i32, i32* %4
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %39
22: ; preds = %18
%23 = load double*, double** %3
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds double, double* %23, i64 %25
%27 = load double, double* %26
%28 = load double, double* %8
%29 = fadd double %28, %27
store double %29, double* %8
%30 = load double, double* %8
%31 = load double, double* %7
%32 = fcmp oge double %30, %31
br i1 %32, label %33, label %35
33: ; preds = %22
%34 = load i32, i32* %9
ret i32 %34
35: ; preds = %22
br label %36
36: ; preds = %35
%37 = load i32, i32* %9
%38 = add nsw i32 %37, 1
store i32 %38, i32* %9
br label %18
39: ; preds = %18
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %9
br label %41
41: ; preds = %53, %39
%42 = load i32, i32* %9
%43 = load i32, i32* %4
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %56
45: ; preds = %41
%46 = load i32, i32* %9
%47 = load double*, double** %3
%48 = load i32, i32* %9
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds double, double* %47, i64 %49
%51 = load double, double* %50
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %46, double %51)
br label %53
53: ; preds = %45
%54 = load i32, i32* %9
%55 = add nsw i32 %54, 1
store i32 %55, i32* %9
br label %41
56: ; preds = %41
%57 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@RAND_MAX = global i64 0
@.str.1 = [9 x i8] c"%i \09 %f\0A\00"
@str = [85 x i8] c"ERROR: random select method could not select anything. Probably p is not normalized.\00"
define i32 @_random_choice(double* nocapture %0, i32 %1) {
%3 = tail call i64 (...) @rand()
%4 = sitofp i64 %3 to double
%5 = load i64, i64* @RAND_MAX
%6 = sitofp i64 %5 to double
%7 = fadd double %6, 1.000000e+00
%8 = fdiv double %4, %7
%9 = icmp sgt i32 %1, 0
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([85 x i8], [85 x i8]* @str, i64 0, i64 0))
br label %38
12: ; preds = %2
%13 = zext i32 %1 to i64
br label %14
14: ; preds = %12, %23
%15 = phi i64 [ 0, %12 ], [ %24, %23 ]
%16 = phi double [ 0.000000e+00, %12 ], [ %19, %23 ]
%17 = getelementptr inbounds double, double* %0, i64 %15
%18 = load double, double* %17
%19 = fadd double %16, %18
%20 = fcmp ult double %19, %8
br i1 %20, label %23, label %21
21: ; preds = %14
%22 = trunc i64 %15 to i32
ret i32 %22
23: ; preds = %14
%24 = add nuw nsw i64 %15, 1
%25 = icmp eq i64 %24, %13
br i1 %25, label %26, label %14
26: ; preds = %23
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([85 x i8], [85 x i8]* @str, i64 0, i64 0))
br i1 %9, label %28, label %38
28: ; preds = %26
%29 = zext i32 %1 to i64
br label %30
30: ; preds = %28, %30
%31 = phi i64 [ 0, %28 ], [ %36, %30 ]
%32 = getelementptr inbounds double, double* %0, i64 %31
%33 = load double, double* %32
%34 = trunc i64 %31 to i32
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %34, double %33)
%36 = add nuw nsw i64 %31, 1
%37 = icmp eq i64 %36, %29
br i1 %37, label %38, label %30
38: ; preds = %30, %10, %26
%39 = tail call i32 @exit(i32 1)
unreachable
}