#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, int /*<<< orphan*/);
int samplebinsearch(int seed, double *distribution, int N) {
  double total = distribution[N - 1];
  double rn = ighmm_rand_uniform_cont(seed, total, 0);
  int l = 0;
  int r = N - 1;
  int m;
  while (1) {
    m = (l + r) >> 1;
    if (distribution[m] < rn) {
      if (l == m)
        return r;
      else
        l = m;
    } else if (distribution[m] > rn) {
      if (l == m)
        return l;
      else
        r = m;
    } else {
      return m;
    }
  }
}


x86 assembly (clang, -O0):
<samplebinsearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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
xor    %esi,%esi
call   1030 <ighmm_rand_uniform_cont@plt>
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x30(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
movsd  -0x28(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11a7 <samplebinsearch+0x97>
mov    -0x2c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jne    119c <samplebinsearch+0x8c>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f6 <samplebinsearch+0xe6>
mov    -0x34(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    11f1 <samplebinsearch+0xe1>
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
ucomisd -0x28(%rbp),%xmm0
jbe    11e1 <samplebinsearch+0xd1>
mov    -0x2c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jne    11d6 <samplebinsearch+0xc6>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f6 <samplebinsearch+0xe6>
mov    -0x34(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    11ec <samplebinsearch+0xdc>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f6 <samplebinsearch+0xe6>
jmp    11f1 <samplebinsearch+0xe1>
jmp    115d <samplebinsearch+0x4d>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<samplebinsearch>:
push   %r15
push   %r14
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
movslq %edx,%rax
add    $0xffffffff,%ebx
movsd  -0x8(%rsi,%rax,8),%xmm0
xor    %r15d,%r15d
xor    %esi,%esi
call   1030 <ighmm_rand_uniform_cont@plt>
lea    (%rbx,%r15,1),%eax
sar    %eax
movslq %eax,%rcx
movsd  (%r14,%rcx,8),%xmm1
ucomisd %xmm1,%xmm0
ja     1154 <samplebinsearch+0x44>
ucomisd %xmm0,%xmm1
jbe    115e <samplebinsearch+0x4e>
mov    %eax,%ebx
cmp    %eax,%r15d
jne    1130 <samplebinsearch+0x20>
jmp    1162 <samplebinsearch+0x52>
cmp    %eax,%r15d
mov    %eax,%r15d
jne    1130 <samplebinsearch+0x20>
jmp    1165 <samplebinsearch+0x55>
mov    %eax,%ebx
jmp    1165 <samplebinsearch+0x55>
mov    %r15d,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @samplebinsearch(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
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store double* %1, double** %6
store i32 %2, i32* %7
%13 = load double*, double** %6
%14 = load i32, i32* %7
%15 = sub nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds double, double* %13, i64 %16
%18 = load double, double* %17
store double %18, double* %8
%19 = load i32, i32* %5
%20 = load double, double* %8
%21 = call double @ighmm_rand_uniform_cont(i32 %19, double %20, i32 0)
store double %21, double* %9
store i32 0, i32* %10
%22 = load i32, i32* %7
%23 = sub nsw i32 %22, 1
store i32 %23, i32* %11
br label %24
24: ; preds = %3, %65
%25 = load i32, i32* %10
%26 = load i32, i32* %11
%27 = add nsw i32 %25, %26
%28 = ashr i32 %27, 1
store i32 %28, i32* %12
%29 = load double*, double** %6
%30 = load i32, i32* %12
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds double, double* %29, i64 %31
%33 = load double, double* %32
%34 = load double, double* %9
%35 = fcmp olt double %33, %34
br i1 %35, label %36, label %45
36: ; preds = %24
%37 = load i32, i32* %10
%38 = load i32, i32* %12
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = load i32, i32* %11
store i32 %41, i32* %4
br label %66
42: ; preds = %36
%43 = load i32, i32* %12
store i32 %43, i32* %10
br label %44
44: ; preds = %42
br label %65
45: ; preds = %24
%46 = load double*, double** %6
%47 = load i32, i32* %12
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds double, double* %46, i64 %48
%50 = load double, double* %49
%51 = load double, double* %9
%52 = fcmp ogt double %50, %51
br i1 %52, label %53, label %62
53: ; preds = %45
%54 = load i32, i32* %10
%55 = load i32, i32* %12
%56 = icmp eq i32 %54, %55
br i1 %56, label %57, label %59
57: ; preds = %53
%58 = load i32, i32* %10
store i32 %58, i32* %4
br label %66
59: ; preds = %53
%60 = load i32, i32* %12
store i32 %60, i32* %11
br label %61
61: ; preds = %59
br label %64
62: ; preds = %45
%63 = load i32, i32* %12
store i32 %63, i32* %4
br label %66
64: ; preds = %61
br label %65
65: ; preds = %64, %44
br label %24
66: ; preds = %62, %57, %40
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
define i32 @samplebinsearch(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, i32 0)
br label %9
9: ; preds = %20, %3
%10 = phi i32 [ %15, %20 ], [ 0, %3 ]
%11 = phi i32 [ %13, %20 ], [ %4, %3 ]
br label %12
12: ; preds = %9, %24
%13 = phi i32 [ %15, %24 ], [ %11, %9 ]
%14 = add nsw i32 %13, %10
%15 = ashr i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds double, double* %1, i64 %16
%18 = load double, double* %17
%19 = fcmp olt double %18, %8
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = icmp eq i32 %10, %15
br i1 %21, label %26, label %9
22: ; preds = %12
%23 = fcmp ogt double %18, %8
br i1 %23, label %24, label %26
24: ; preds = %22
%25 = icmp eq i32 %10, %15
br i1 %25, label %26, label %12
26: ; preds = %20, %22, %24
%27 = phi i32 [ %15, %22 ], [ %10, %24 ], [ %13, %20 ]
ret i32 %27
}