#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 */
float frand();
int rand_int(int size) {
  int i;
  float r = frand();
  float 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 <frand@plt>
movss  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x14(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1191 <rand_int+0x81>
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xead(%rip),%xmm1
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x14(%rbp)
movss  -0x10(%rbp),%xmm1
movss  -0x14(%rbp),%xmm0
ucomiss %xmm1,%xmm0
jb     117e <rand_int+0x6e>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1198 <rand_int+0x88>
jmp    1183 <rand_int+0x73>
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 <frand@plt>
mov    $0xffffffff,%eax
test   %ebx,%ebx
jle    1154 <rand_int+0x44>
xorps  %xmm1,%xmm1
movss  0xece(%rip),%xmm2
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
addss  %xmm2,%xmm1
ucomiss %xmm0,%xmm1
jae    1152 <rand_int+0x42>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    1140 <rand_int+0x30>
jmp    1154 <rand_int+0x44>
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 float
%6 = alloca float
store i32 %0, i32* %3
%7 = call float (...) @frand()
store float %7, float* %5
store float 0.000000e+00, float* %6
store i32 0, i32* %4
br label %8
8: ; preds = %23, %1
%9 = load i32, i32* %4
%10 = load i32, i32* %3
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %26
12: ; preds = %8
%13 = load float, float* %6
%14 = fpext float %13 to double
%15 = fadd double %14, 1.000000e+00
%16 = fptrunc double %15 to float
store float %16, float* %6
%17 = load float, float* %5
%18 = load float, float* %6
%19 = fcmp ole float %17, %18
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = load i32, i32* %4
store i32 %21, i32* %2
br label %27
22: ; preds = %12
br label %23
23: ; preds = %22
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %8
26: ; preds = %8
store i32 -1, i32* %2
br label %27
27: ; preds = %26, %20
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
define i32 @rand_int(i32 %0) {
%2 = tail call float (...) @frand()
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %12
4: ; preds = %1, %9
%5 = phi float [ %7, %9 ], [ 0.000000e+00, %1 ]
%6 = phi i32 [ %10, %9 ], [ 0, %1 ]
%7 = fadd float %5, 1.000000e+00
%8 = fcmp ugt float %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
}