#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 / size;
    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    119a <rand_int+0x8a>
cvtsi2sdl -0x8(%rbp),%xmm0
movsd  0xeb1(%rip),%xmm1
divsd  %xmm0,%xmm1
movss  -0x14(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
addsd  %xmm1,%xmm0
cvtsd2ss %xmm0,%xmm0
movss  %xmm0,-0x14(%rbp)
movss  -0x10(%rbp),%xmm1
movss  -0x14(%rbp),%xmm0
ucomiss %xmm1,%xmm0
jb     1187 <rand_int+0x77>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a1 <rand_int+0x91>
jmp    118c <rand_int+0x7c>
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    115c <rand_int+0x4c>
cvtsi2sd %ebx,%xmm2
movsd  0xecd(%rip),%xmm1
divsd  %xmm2,%xmm1
xorpd  %xmm2,%xmm2
nopl   0x0(%rax,%rax,1)
cvtss2sd %xmm2,%xmm2
addsd  %xmm1,%xmm2
cvtsd2ss %xmm2,%xmm2
ucomiss %xmm0,%xmm2
jae    115a <rand_int+0x4a>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    1140 <rand_int+0x30>
jmp    115c <rand_int+0x4c>
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 = %26, %1
%9 = load i32, i32* %4
%10 = load i32, i32* %3
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %29
12: ; preds = %8
%13 = load i32, i32* %3
%14 = sitofp i32 %13 to double
%15 = fdiv double 1.000000e+00, %14
%16 = load float, float* %6
%17 = fpext float %16 to double
%18 = fadd double %17, %15
%19 = fptrunc double %18 to float
store float %19, float* %6
%20 = load float, float* %5
%21 = load float, float* %6
%22 = fcmp ole float %20, %21
br i1 %22, label %23, label %25
23: ; preds = %12
%24 = load i32, i32* %4
store i32 %24, i32* %2
br label %30
25: ; preds = %12
br label %26
26: ; preds = %25
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %8
29: ; preds = %8
store i32 -1, i32* %2
br label %30
30: ; preds = %29, %23
%31 = load i32, i32* %2
ret i32 %31
}

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