#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 */
typedef int /*<<< orphan*/ randval;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned int *, int, int, int /*<<< orphan*/ *);
int gen_rand(unsigned short int size) {
  unsigned int randval;
  FILE *f;
  f = fopen("/dev/urandom", "r");
  fread(&randval, sizeof(randval), 1, f);
  fclose(f);
  while (randval > size) {
    randval /= 11;
  };
  return randval;
}


x86 assembly (clang, -O0):
<gen_rand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
lea    0xeba(%rip),%rdi
lea    0xec0(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x8(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8(%rbp),%eax
movzwl -0x2(%rbp),%ecx
cmp    %ecx,%eax
jbe    1199 <gen_rand+0x69>
mov    -0x8(%rbp),%eax
mov    $0xb,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x8(%rbp)
jmp    1176 <gen_rand+0x46>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_rand>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0xec3(%rip),%rdi
lea    0xec9(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x4(%rsp),%eax
cmp    %ebx,%eax
jbe    118e <gen_rand+0x5e>
mov    $0xba2e8ba3,%ecx
nopl   0x0(%rax,%rax,1)
mov    %eax,%eax
imul   %rcx,%rax
shr    $0x23,%rax
cmp    %ebx,%eax
ja     1180 <gen_rand+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @gen_rand(i16 zeroext %0) {
%2 = alloca i16
%3 = alloca i32
%4 = alloca i32*
store i16 %0, i16* %2
%5 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %4
%6 = load i32*, i32** %4
%7 = call i32 @fread(i32* %3, i32 4, i32 1, i32* %6)
%8 = load i32*, i32** %4
%9 = call i32 @fclose(i32* %8)
br label %10
10: ; preds = %15, %1
%11 = load i32, i32* %3
%12 = load i16, i16* %2
%13 = zext i16 %12 to i32
%14 = icmp ugt i32 %11, %13
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* %3
%17 = udiv i32 %16, 11
store i32 %17, i32* %3
br label %10
18: ; preds = %10
%19 = load i32, i32* %3
ret i32 %19
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @gen_rand(i16 zeroext %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = call i32 @fread(i32* %2, i32 4, i32 1, i32* %4)
%6 = tail call i32 @fclose(i32* %4)
%7 = zext i16 %0 to i32
%8 = load i32, i32* %2
%9 = icmp ugt i32 %8, %7
br i1 %9, label %10, label %14
10: ; preds = %1, %10
%11 = phi i32 [ %12, %10 ], [ %8, %1 ]
%12 = udiv i32 %11, 11
%13 = icmp ugt i32 %12, %7
br i1 %13, label %10, label %14
14: ; preds = %10, %1
%15 = phi i32 [ %8, %1 ], [ %12, %10 ]
ret i32 %15
}