#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 drand();
int rand_int(int size) {
  int i;
  double r = drand();
  double sum = 0;
  for (i = 0; i < size; i++) {
    sum += 1.0;
    if (r <= sum) {
      return i;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<rand_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1030 <drand@plt>
movsd  %xmm0,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1186 <rand_int+0x76>
movsd  0xeb6(%rip),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x18(%rbp),%xmm1
movsd  -0x20(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     1173 <rand_int+0x63>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118d <rand_int+0x7d>
jmp    1178 <rand_int+0x68>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1136 <rand_int+0x26>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_int>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <drand@plt>
mov    $0xffffffff,%eax
test   %ebx,%ebx
jle    1155 <rand_int+0x45>
xorpd  %xmm1,%xmm1
movsd  0xecd(%rip),%xmm2
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
addsd  %xmm2,%xmm1
ucomisd %xmm0,%xmm1
jae    1153 <rand_int+0x43>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    1140 <rand_int+0x30>
jmp    1155 <rand_int+0x45>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @rand_int(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
%6 = alloca double
store i32 %0, i32* %3
%7 = call double (...) @drand()
store double %7, double* %5
store double 0.000000e+00, double* %6
store i32 0, i32* %4
br label %8
8: ; preds = %21, %1
%9 = load i32, i32* %4
%10 = load i32, i32* %3
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %24
12: ; preds = %8
%13 = load double, double* %6
%14 = fadd double %13, 1.000000e+00
store double %14, double* %6
%15 = load double, double* %5
%16 = load double, double* %6
%17 = fcmp ole double %15, %16
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = load i32, i32* %4
store i32 %19, i32* %2
br label %25
20: ; preds = %12
br label %21
21: ; preds = %20
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
store i32 %23, i32* %4
br label %8
24: ; preds = %8
store i32 -1, i32* %2
br label %25
25: ; preds = %24, %18
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
define i32 @rand_int(i32 %0) {
%2 = tail call double (...) @drand()
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %12
4: ; preds = %1, %9
%5 = phi double [ %7, %9 ], [ 0.000000e+00, %1 ]
%6 = phi i32 [ %10, %9 ], [ 0, %1 ]
%7 = fadd double %5, 1.000000e+00
%8 = fcmp ugt double %2, %7
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = add nuw nsw i32 %6, 1
%11 = icmp eq i32 %10, %0
br i1 %11, label %12, label %4
12: ; preds = %4, %9, %1
%13 = phi i32 [ -1, %1 ], [ -1, %9 ], [ %6, %4 ]
ret i32 %13
}