#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 /*<<< orphan*/ printf(char *, ...);
unsigned short int gen_rand(unsigned short int size) {
  unsigned int randval;
  FILE *f;
  f = fopen("/dev/urandom", "r");
  printf("file opened;\n");
  fread(&randval, sizeof(randval), 1, f);
  printf("random digit is read;\n");
  fclose(f);
  printf("file closed;\n");
  while (randval > size) {
    randval /= 5;
    printf("randval = %u;\n", randval);
  };
  printf("%u\n", randval);
  return randval;
}


x86 assembly (clang, -O0):
<gen_rand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
lea    0xeaa(%rip),%rdi
lea    0xeb0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rcx
lea    -0x8(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
movzwl -0x2(%rbp),%ecx
cmp    %ecx,%eax
jbe    11e4 <gen_rand+0xa4>
mov    -0x8(%rbp),%eax
mov    $0x5,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11b0 <gen_rand+0x70>
mov    -0x8(%rbp),%esi
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
movzwl %ax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_rand>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
lea    0xea0(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0xeac(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe98(%rip),%rdi
call   1030 <puts@plt>
mov    0x4(%rsp),%ebx
cmp    %ebp,%ebx
jbe    11ea <gen_rand+0x9a>
mov    $0xcccccccd,%r15d
lea    0xe48(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    %ebx,%ebx
imul   %r15,%rbx
shr    $0x22,%rbx
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    %ebp,%ebx
ja     11d0 <gen_rand+0x80>
lea    0xe2d(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [14 x i8] c"file opened;\0A\00"
@.str.3 = [23 x i8] c"random digit is read;\0A\00"
@.str.4 = [14 x i8] c"file closed;\0A\00"
@.str.5 = [15 x i8] c"randval = %u;\0A\00"
@.str.6 = [4 x i8] c"%u\0A\00"
define zeroext i16 @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 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%7 = load i32*, i32** %4
%8 = call i32 @fread(i32* %3, i32 4, i32 1, i32* %7)
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%10 = load i32*, i32** %4
%11 = call i32 @fclose(i32* %10)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %13
13: ; preds = %18, %1
%14 = load i32, i32* %3
%15 = load i16, i16* %2
%16 = zext i16 %15 to i32
%17 = icmp ugt i32 %14, %16
br i1 %17, label %18, label %23
18: ; preds = %13
%19 = load i32, i32* %3
%20 = udiv i32 %19, 5
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %21)
br label %13
23: ; preds = %13
%24 = load i32, i32* %3
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %3
%27 = trunc i32 %26 to i16
ret i16 %27
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@.str.5 = [15 x i8] c"randval = %u;\0A\00"
@.str.6 = [4 x i8] c"%u\0A\00"
@str = [13 x i8] c"file opened;\00"
@str.7 = [22 x i8] c"random digit is read;\00"
@str.8 = [13 x i8] c"file closed;\00"
define zeroext i16 @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 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%6 = call i32 @fread(i32* %2, i32 4, i32 1, i32* %4)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.7, i64 0, i64 0))
%8 = tail call i32 @fclose(i32* %4)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.8, i64 0, i64 0))
%10 = zext i16 %0 to i32
%11 = load i32, i32* %2
%12 = icmp ugt i32 %11, %10
br i1 %12, label %13, label %18
13: ; preds = %1, %13
%14 = phi i32 [ %15, %13 ], [ %11, %1 ]
%15 = udiv i32 %14, 5
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %15)
%17 = icmp ugt i32 %15, %10
br i1 %17, label %13, label %18
18: ; preds = %13, %1
%19 = phi i32 [ %11, %1 ], [ %15, %13 ]
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %19)
%21 = trunc i32 %19 to i16
ret i16 %21
}