#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 select_random_index(double *pi, int m, char *sel, double maxp) {
  double u1, u2, q;
  int p = 0;
  while (1) {
    u1 = drand48();
    u2 = drand48();
    p = 1 + (int)(m - 1) * u1;
    q = maxp * u2;
    if (q <= pi[p]) {
      if (!sel[p]) {
        sel[p] = 1;
        return p;
      } else {
        int j;
        for (j = p + 1; j != p; j++) {
          if (j > m)
            j = 1;
          if (!sel[j]) {
            sel[j] = 1;
            return j;
          }
        }
        return 0;
      }
    }
  }
}


x86 assembly (clang, -O0):
<select_random_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x44(%rbp)
mov    $0x0,%al
call   1030 <drand48@plt>
movsd  %xmm0,-0x30(%rbp)
mov    $0x0,%al
call   1030 <drand48@plt>
movsd  %xmm0,-0x38(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm0
movsd  -0x30(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xe9e(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x44(%rbp)
movsd  -0x28(%rbp),%xmm0
mulsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm1
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
ucomisd %xmm1,%xmm0
jb     1231 <select_random_index+0x121>
mov    -0x20(%rbp),%rax
movslq -0x44(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
jne    11c1 <select_random_index+0xb1>
mov    -0x20(%rbp),%rax
movslq -0x44(%rbp),%rcx
movb   $0x1,(%rax,%rcx,1)
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1236 <select_random_index+0x126>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
je     1225 <select_random_index+0x115>
mov    -0x48(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    11e9 <select_random_index+0xd9>
movl   $0x1,-0x48(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x48(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
jne    1212 <select_random_index+0x102>
mov    -0x20(%rbp),%rax
movslq -0x48(%rbp),%rcx
movb   $0x1,(%rax,%rcx,1)
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1236 <select_random_index+0x126>
jmp    1217 <select_random_index+0x107>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    11ca <select_random_index+0xba>
movl   $0x0,-0x4(%rbp)
jmp    1236 <select_random_index+0x126>
jmp    112f <select_random_index+0x1f>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<select_random_index>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
movsd  %xmm0,0x10(%rsp)
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbx
lea    -0x1(%r15),%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x8(%rsp)
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <drand48@plt>
movsd  %xmm0,0x18(%rsp)
xor    %eax,%eax
call   1030 <drand48@plt>
movsd  0x18(%rsp),%xmm1
mulsd  0x8(%rsp),%xmm1
addsd  0xe98(%rip),%xmm1
cvttsd2si %xmm1,%eax
mulsd  0x10(%rsp),%xmm0
movslq %eax,%rcx
movsd  (%rbx,%rcx,8),%xmm1
ucomisd %xmm0,%xmm1
jb     1140 <select_random_index+0x30>
cmpb   $0x0,(%r14,%rcx,1)
je     11b0 <select_random_index+0xa0>
mov    $0x1,%ecx
mov    %eax,%esi
xchg   %ax,%ax
lea    0x1(%rsi),%edx
cmp    %eax,%edx
je     11b9 <select_random_index+0xa9>
cmp    %r15d,%esi
cmovge %ecx,%edx
movslq %edx,%rdi
cmpb   $0x0,(%r14,%rdi,1)
mov    %edx,%esi
jne    1190 <select_random_index+0x80>
add    %rdi,%r14
mov    %edx,%eax
jmp    11b3 <select_random_index+0xa3>
add    %rcx,%r14
movb   $0x1,(%r14)
jmp    11bb <select_random_index+0xab>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @select_random_index(double* %0, i32 %1, i8* %2, double %3) {
%5 = alloca i32
%6 = alloca double*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca i32
%14 = alloca i32
store double* %0, double** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store double %3, double* %9
store i32 0, i32* %13
br label %15
15: ; preds = %4, %77
%16 = call double (...) @drand48()
store double %16, double* %10
%17 = call double (...) @drand48()
store double %17, double* %11
%18 = load i32, i32* %7
%19 = sub nsw i32 %18, 1
%20 = sitofp i32 %19 to double
%21 = load double, double* %10
%22 = call double @llvm.fmuladd.f64(double %20, double %21, double 1.000000e+00)
%23 = fptosi double %22 to i32
store i32 %23, i32* %13
%24 = load double, double* %9
%25 = load double, double* %11
%26 = fmul double %24, %25
store double %26, double* %12
%27 = load double, double* %12
%28 = load double*, double** %6
%29 = load i32, i32* %13
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds double, double* %28, i64 %30
%32 = load double, double* %31
%33 = fcmp ole double %27, %32
br i1 %33, label %34, label %77
34: ; preds = %15
%35 = load i8*, i8** %8
%36 = load i32, i32* %13
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i8, i8* %38
%40 = icmp ne i8 %39, 0
br i1 %40, label %47, label %41
41: ; preds = %34
%42 = load i8*, i8** %8
%43 = load i32, i32* %13
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 1, i8* %45
%46 = load i32, i32* %13
store i32 %46, i32* %5
br label %78
47: ; preds = %34
%48 = load i32, i32* %13
%49 = add nsw i32 %48, 1
store i32 %49, i32* %14
br label %50
50: ; preds = %73, %47
%51 = load i32, i32* %14
%52 = load i32, i32* %13
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %76
54: ; preds = %50
%55 = load i32, i32* %14
%56 = load i32, i32* %7
%57 = icmp sgt i32 %55, %56
br i1 %57, label %58, label %59
58: ; preds = %54
store i32 1, i32* %14
br label %59
59: ; preds = %58, %54
%60 = load i8*, i8** %8
%61 = load i32, i32* %14
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
%64 = load i8, i8* %63
%65 = icmp ne i8 %64, 0
br i1 %65, label %72, label %66
66: ; preds = %59
%67 = load i8*, i8** %8
%68 = load i32, i32* %14
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i8, i8* %67, i64 %69
store i8 1, i8* %70
%71 = load i32, i32* %14
store i32 %71, i32* %5
br label %78
72: ; preds = %59
br label %73
73: ; preds = %72
%74 = load i32, i32* %14
%75 = add nsw i32 %74, 1
store i32 %75, i32* %14
br label %50
76: ; preds = %50
store i32 0, i32* %5
br label %78
77: ; preds = %15
br label %15
78: ; preds = %76, %66, %41
%79 = load i32, i32* %5
ret i32 %79
}

LLVM IR (-O2):
define i32 @select_random_index(double* nocapture %0, i32 %1, i8* nocapture %2, double %3) {
%5 = add nsw i32 %1, -1
%6 = sitofp i32 %5 to double
br label %7
7: ; preds = %7, %4
%8 = tail call double (...) @drand48()
%9 = tail call double (...) @drand48()
%10 = tail call double @llvm.fmuladd.f64(double %6, double %8, double 1.000000e+00)
%11 = fptosi double %10 to i32
%12 = fmul double %9, %3
%13 = sext i32 %11 to i64
%14 = getelementptr inbounds double, double* %0, i64 %13
%15 = load double, double* %14
%16 = fcmp ugt double %12, %15
br i1 %16, label %7, label %17
17: ; preds = %7
%18 = getelementptr inbounds i8, i8* %2, i64 %13
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %34, label %21
21: ; preds = %17, %25
%22 = phi i32 [ %27, %25 ], [ %11, %17 ]
%23 = add nsw i32 %22, 1
%24 = icmp eq i32 %23, %11
br i1 %24, label %37, label %25
25: ; preds = %21
%26 = icmp slt i32 %22, %1
%27 = select i1 %26, i32 %23, i32 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %2, i64 %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %21
32: ; preds = %25
%33 = getelementptr inbounds i8, i8* %2, i64 %28
br label %34
34: ; preds = %17, %32
%35 = phi i8* [ %33, %32 ], [ %18, %17 ]
%36 = phi i32 [ %27, %32 ], [ %11, %17 ]
store i8 1, i8* %35
br label %37
37: ; preds = %21, %34
%38 = phi i32 [ %36, %34 ], [ 0, %21 ]
ret i32 %38
}