#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 uniform();
int pick_base_from_prior(double basefreq[4]) {
  int i = 0;
  double U, p;
  U = uniform();
  p = basefreq[0];
  while (U > p) {
    i++;
    p = p + basefreq[i];
  }
  return i;
}


x86 assembly (clang, -O0):
<pick_base_from_prior>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1030 <uniform@plt>
movsd  %xmm0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm0
ucomisd -0x20(%rbp),%xmm0
jbe    1171 <pick_base_from_prior+0x61>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
movsd  -0x20(%rbp),%xmm0
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
addsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    113c <pick_base_from_prior+0x2c>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pick_base_from_prior>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <uniform@plt>
movsd  (%r14),%xmm1
ucomisd %xmm1,%xmm0
jbe    1141 <pick_base_from_prior+0x31>
xor    %ebx,%ebx
nopl   (%rax)
addsd  0x8(%r14,%rbx,8),%xmm1
add    $0x1,%rbx
ucomisd %xmm1,%xmm0
ja     1130 <pick_base_from_prior+0x20>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @pick_base_from_prior(double* %0) {
%2 = alloca double*
%3 = alloca i32
%4 = alloca double
%5 = alloca double
store double* %0, double** %2
store i32 0, i32* %3
%6 = call double (...) @uniform()
store double %6, double* %4
%7 = load double*, double** %2
%8 = getelementptr inbounds double, double* %7, i64 0
%9 = load double, double* %8
store double %9, double* %5
br label %10
10: ; preds = %14, %1
%11 = load double, double* %4
%12 = load double, double* %5
%13 = fcmp ogt double %11, %12
br i1 %13, label %14, label %24
14: ; preds = %10
%15 = load i32, i32* %3
%16 = add nsw i32 %15, 1
store i32 %16, i32* %3
%17 = load double, double* %5
%18 = load double*, double** %2
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds double, double* %18, i64 %20
%22 = load double, double* %21
%23 = fadd double %17, %22
store double %23, double* %5
br label %10
24: ; preds = %10
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
define i32 @pick_base_from_prior(double* nocapture %0) {
%2 = tail call double (...) @uniform()
%3 = load double, double* %0
%4 = fcmp ogt double %2, %3
br i1 %4, label %5, label %15
5: ; preds = %1, %5
%6 = phi i64 [ %8, %5 ], [ 0, %1 ]
%7 = phi double [ %11, %5 ], [ %3, %1 ]
%8 = add nuw nsw i64 %6, 1
%9 = getelementptr inbounds double, double* %0, i64 %8
%10 = load double, double* %9
%11 = fadd double %7, %10
%12 = fcmp ogt double %2, %11
br i1 %12, label %5, label %13
13: ; preds = %5
%14 = trunc i64 %8 to i32
br label %15
15: ; preds = %13, %1
%16 = phi i32 [ 0, %1 ], [ %14, %13 ]
ret i32 %16
}