#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 ighmm_rand_uniform_cont(int, double, float);
int sample(int seed, double *dist, int N) {
  double total = dist[N - 1];
  double rn = ighmm_rand_uniform_cont(seed, total, 0.0f);
  int i;
  if (rn <= dist[0])
    return 0;
  for (i = 1; i < N; i++) {
    if (dist[i - 1] < rn && rn <= dist[i])
      return i;
  }
  return N - 1;
}


x86 assembly (clang, -O0):
<sample>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
movsd  -0x20(%rbp),%xmm0
xorps  %xmm1,%xmm1
call   1030 <ighmm_rand_uniform_cont@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm1
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
ucomisd %xmm1,%xmm0
jb     1171 <sample+0x61>
movl   $0x0,-0x4(%rbp)
jmp    11e8 <sample+0xd8>
movl   $0x1,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11df <sample+0xcf>
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11cc <sample+0xbc>
movsd  -0x28(%rbp),%xmm1
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
ucomisd %xmm1,%xmm0
jb     11cc <sample+0xbc>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e8 <sample+0xd8>
jmp    11d1 <sample+0xc1>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1178 <sample+0x68>
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):
<sample>:
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
movslq %edx,%rax
movsd  -0x8(%rsi,%rax,8),%xmm0
xorps  %xmm1,%xmm1
call   1030 <ighmm_rand_uniform_cont@plt>
movsd  (%rbx),%xmm1
xor    %eax,%eax
ucomisd %xmm0,%xmm1
jae    1188 <sample+0x78>
lea    -0x1(%r14),%eax
cmp    $0x2,%r14d
jl     1188 <sample+0x78>
mov    %r14d,%edx
mov    $0x2,%ecx
sub    %rdx,%rcx
mov    $0x1,%edx
ucomisd %xmm1,%xmm0
jbe    116b <sample+0x5b>
nopw   0x0(%rax,%rax,1)
movsd  (%rbx,%rdx,8),%xmm1
ucomisd %xmm0,%xmm1
jae    1186 <sample+0x76>
lea    (%rcx,%rdx,1),%rsi
cmp    $0x1,%rsi
je     1188 <sample+0x78>
movsd  (%rbx,%rdx,8),%xmm1
add    $0x1,%rdx
ucomisd %xmm1,%xmm0
ja     1160 <sample+0x50>
jmp    116b <sample+0x5b>
mov    %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @sample(i32 %0, double* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca double*
%7 = alloca i32
%8 = alloca double
%9 = alloca double
%10 = alloca i32
store i32 %0, i32* %5
store double* %1, double** %6
store i32 %2, i32* %7
%11 = load double*, double** %6
%12 = load i32, i32* %7
%13 = sub nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds double, double* %11, i64 %14
%16 = load double, double* %15
store double %16, double* %8
%17 = load i32, i32* %5
%18 = load double, double* %8
%19 = call double @ighmm_rand_uniform_cont(i32 %17, double %18, float 0.000000e+00)
store double %19, double* %9
%20 = load double, double* %9
%21 = load double*, double** %6
%22 = getelementptr inbounds double, double* %21, i64 0
%23 = load double, double* %22
%24 = fcmp ole double %20, %23
br i1 %24, label %25, label %26
25: ; preds = %3
store i32 0, i32* %4
br label %57
26: ; preds = %3
store i32 1, i32* %10
br label %27
27: ; preds = %51, %26
%28 = load i32, i32* %10
%29 = load i32, i32* %7
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %54
31: ; preds = %27
%32 = load double*, double** %6
%33 = load i32, i32* %10
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds double, double* %32, i64 %35
%37 = load double, double* %36
%38 = load double, double* %9
%39 = fcmp olt double %37, %38
br i1 %39, label %40, label %50
40: ; preds = %31
%41 = load double, double* %9
%42 = load double*, double** %6
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds double, double* %42, i64 %44
%46 = load double, double* %45
%47 = fcmp ole double %41, %46
br i1 %47, label %48, label %50
48: ; preds = %40
%49 = load i32, i32* %10
store i32 %49, i32* %4
br label %57
50: ; preds = %40, %31
br label %51
51: ; preds = %50
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 1
store i32 %53, i32* %10
br label %27
54: ; preds = %27
%55 = load i32, i32* %7
%56 = sub nsw i32 %55, 1
store i32 %56, i32* %4
br label %57
57: ; preds = %54, %48, %25
%58 = load i32, i32* %4
ret i32 %58
}

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