#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 */
unsigned int drand48();
unsigned int WalkerSample(const double *Y, unsigned int N,
                          const unsigned int *A) {
  unsigned int i;
  double r;
  i = 1 + (unsigned int)(N * drand48());
  r = drand48();
  if (r > Y[i]) {
    i = A[i];
  }
  return i - 1;
}


x86 assembly (clang, -O0):
<WalkerSample>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
xor    %eax,%eax
mov    %al,-0x29(%rbp)
call   1030 <drand48@plt>
mov    -0x30(%rbp),%ecx
mov    %eax,%edx
mov    -0x29(%rbp),%al
imul   %edx,%ecx
add    $0x1,%ecx
mov    %ecx,-0x1c(%rbp)
call   1030 <drand48@plt>
mov    %eax,%eax
cvtsi2sd %rax,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
ucomisd (%rax,%rcx,8),%xmm0
jbe    1179 <WalkerSample+0x69>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WalkerSample>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
xor    %eax,%eax
call   1030 <drand48@plt>
mov    %eax,%ebx
imul   %ebp,%ebx
add    $0x1,%ebx
xor    %eax,%eax
call   1030 <drand48@plt>
mov    %eax,%eax
cvtsi2sd %rax,%xmm0
ucomisd (%r15,%rbx,8),%xmm0
jbe    1148 <WalkerSample+0x38>
mov    (%r14,%rbx,4),%ebx
add    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @WalkerSample(double* %0, i32 %1, i32* %2) {
%4 = alloca double*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca double
store double* %0, double** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%9 = load i32, i32* %5
%10 = call i32 (...) @drand48()
%11 = mul i32 %9, %10
%12 = add i32 1, %11
store i32 %12, i32* %7
%13 = call i32 (...) @drand48()
%14 = uitofp i32 %13 to double
store double %14, double* %8
%15 = load double, double* %8
%16 = load double*, double** %4
%17 = load i32, i32* %7
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds double, double* %16, i64 %18
%20 = load double, double* %19
%21 = fcmp ogt double %15, %20
br i1 %21, label %22, label %28
22: ; preds = %3
%23 = load i32*, i32** %6
%24 = load i32, i32* %7
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = load i32, i32* %26
store i32 %27, i32* %7
br label %28
28: ; preds = %22, %3
%29 = load i32, i32* %7
%30 = sub i32 %29, 1
ret i32 %30
}

LLVM IR (-O2):
define i32 @WalkerSample(double* nocapture %0, i32 %1, i32* nocapture %2) {
%4 = tail call i32 (...) @drand48()
%5 = mul i32 %4, %1
%6 = add i32 %5, 1
%7 = tail call i32 (...) @drand48()
%8 = uitofp i32 %7 to double
%9 = zext i32 %6 to i64
%10 = getelementptr inbounds double, double* %0, i64 %9
%11 = load double, double* %10
%12 = fcmp olt double %11, %8
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = getelementptr inbounds i32, i32* %2, i64 %9
%15 = load i32, i32* %14
br label %16
16: ; preds = %13, %3
%17 = phi i32 [ %15, %13 ], [ %6, %3 ]
%18 = add i32 %17, -1
ret i32 %18
}