#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 */
int drand48();
int floor(int);
int /*<<< orphan*/ printf(char *, int);
int sample(int *weights, int num_weights) {
  int total_weight = weights[num_weights - 1];
  int s = floor(drand48() * total_weight);
  int i;
  for (i = 0; i < num_weights; i++) {
    if (s < weights[i]) {
      return i;
    }
  }
  printf("DOH! s = %f\n", s);
  return 0;
}


x86 assembly (clang, -O0):
<sample>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <drand48@plt>
mov    %eax,%edi
imul   -0x18(%rbp),%edi
call   1040 <floor@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ac <sample+0x7c>
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x20(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jge    1199 <sample+0x69>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c2 <sample+0x92>
jmp    119e <sample+0x6e>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    116e <sample+0x3e>
mov    -0x1c(%rbp),%esi
lea    0xe4a(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sample>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %rdi,%rbx
movslq %esi,%r12
mov    -0x4(%rdi,%r12,4),%ebp
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <drand48@plt>
imul   %eax,%ebp
mov    %ebp,%edi
call   1040 <floor@plt>
test   %r12d,%r12d
jle    117e <sample+0x4e>
mov    %r15d,%edx
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    (%rbx,%rcx,4),%eax
jl     118e <sample+0x5e>
add    $0x1,%rcx
cmp    %rcx,%rdx
jne    1170 <sample+0x40>
lea    0xe7b(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
jmp    1191 <sample+0x61>
mov    %ecx,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"DOH! s = %f\0A\00"
define i32 @sample(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%9 = load i32*, i32** %4
%10 = load i32, i32* %5
%11 = sub nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i32, i32* %9, i64 %12
%14 = load i32, i32* %13
store i32 %14, i32* %6
%15 = call i32 (...) @drand48()
%16 = load i32, i32* %6
%17 = mul nsw i32 %15, %16
%18 = call i32 @floor(i32 %17)
store i32 %18, i32* %7
store i32 0, i32* %8
br label %19
19: ; preds = %34, %2
%20 = load i32, i32* %8
%21 = load i32, i32* %5
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %37
23: ; preds = %19
%24 = load i32, i32* %7
%25 = load i32*, i32** %4
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
%29 = load i32, i32* %28
%30 = icmp slt i32 %24, %29
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = load i32, i32* %8
store i32 %32, i32* %3
br label %40
33: ; preds = %23
br label %34
34: ; preds = %33
%35 = load i32, i32* %8
%36 = add nsw i32 %35, 1
store i32 %36, i32* %8
br label %19
37: ; preds = %19
%38 = load i32, i32* %7
%39 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %38)
store i32 0, i32* %3
br label %40
40: ; preds = %37, %31
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [13 x i8] c"DOH! s = %f\0A\00"
define i32 @sample(i32* nocapture %0, i32 %1) {
%3 = add nsw i32 %1, -1
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i32, i32* %0, i64 %4
%6 = load i32, i32* %5
%7 = tail call i32 (...) @drand48()
%8 = mul nsw i32 %7, %6
%9 = tail call i32 @floor(i32 %8)
%10 = icmp sgt i32 %1, 0
br i1 %10, label %11, label %21
11: ; preds = %2
%12 = zext i32 %1 to i64
br label %13
13: ; preds = %11, %18
%14 = phi i64 [ 0, %11 ], [ %19, %18 ]
%15 = getelementptr inbounds i32, i32* %0, i64 %14
%16 = load i32, i32* %15
%17 = icmp slt i32 %9, %16
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = add nuw nsw i64 %14, 1
%20 = icmp eq i64 %19, %12
br i1 %20, label %21, label %13
21: ; preds = %18, %2
%22 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %25
23: ; preds = %13
%24 = trunc i64 %14 to i32
br label %25
25: ; preds = %23, %21
%26 = phi i32 [ 0, %21 ], [ %24, %23 ]
ret i32 %26
}