#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 ranf();
int sample_Mult(double *th, int lo, int hi) {
  double s;
  int i;
  s = th[hi - lo + 1];
  s = ranf() * s;
  for (i = 0; i <= hi - lo; i++) {
    s -= th[i];
    if (s < 0) {
      return i + lo;
    }
  }
  return lo;
}


x86 assembly (clang, -O0):
<sample_Mult>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    $0x0,%al
call   1030 <ranf@plt>
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    -0x14(%rbp),%ecx
cmp    %ecx,%eax
jg     11af <sample_Mult+0x9f>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
movsd  -0x20(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x20(%rbp),%xmm0
jbe    119c <sample_Mult+0x8c>
mov    -0x24(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b5 <sample_Mult+0xa5>
jmp    11a1 <sample_Mult+0x91>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1154 <sample_Mult+0x44>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sample_Mult>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%rbp
sub    %esi,%ebx
movslq %ebx,%r15
movsd  0x8(%rdi,%r15,8),%xmm0
movsd  %xmm0,(%rsp)
xor    %eax,%eax
call   1030 <ranf@plt>
mov    %r14d,%eax
test   %r15d,%r15d
js     117d <sample_Mult+0x6d>
movsd  (%rsp),%xmm1
mulsd  %xmm0,%xmm1
add    $0x1,%ebx
xor    %ecx,%ecx
xorpd  %xmm0,%xmm0
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
subsd  0x0(%rbp,%rcx,8),%xmm1
ucomisd %xmm1,%xmm0
ja     1177 <sample_Mult+0x67>
add    $0x1,%rcx
cmp    %rcx,%rbx
jne    1160 <sample_Mult+0x50>
jmp    117d <sample_Mult+0x6d>
add    %ecx,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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