#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*/ *fopen(char *, char *);
scalar_t__ fread(unsigned int *, int, int, int /*<<< orphan*/ *);
unsigned int rand();
int /*<<< orphan*/ srand(unsigned int);
scalar_t__ time(int /*<<< orphan*/ *);
unsigned int get_rand_int(unsigned int range) {
  static FILE *fp;
  unsigned int randval;
  if (!fp) {
    fp = fopen("/dev/urandom", "r");
  }
  if (fread(&randval, sizeof(randval), 1, fp)) {
    return (randval % range);
  } else {
    srand((unsigned int)time(NULL));
    return (rand() % range);
  }
}


x86 assembly (clang, -O0):
<get_rand_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
cmpq   $0x0,0x2eed(%rip)
jne    1183 <get_rand_int+0x33>
lea    0xe90(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,0x2ecd(%rip)
mov    0x2ec6(%rip),%rcx
lea    -0xc(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
je     11b7 <get_rand_int+0x67>
mov    -0xc(%rbp),%eax
xor    %edx,%edx
divl   -0x8(%rbp)
mov    %edx,-0x4(%rbp)
jmp    11d6 <get_rand_int+0x86>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
mov    $0x0,%al
call   1070 <rand@plt>
xor    %edx,%edx
divl   -0x8(%rbp)
mov    %edx,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_rand_int>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2ef2(%rip),%rcx
test   %rcx,%rcx
jne    1180 <get_rand_int+0x30>
lea    0xe96(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    %rax,0x2ed0(%rip)
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
test   %rax,%rax
je     119f <get_rand_int+0x4f>
mov    0xc(%rsp),%eax
jmp    11b4 <get_rand_int+0x64>
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,%edi
call   1040 <srand@plt>
xor    %eax,%eax
call   1070 <rand@plt>
xor    %edx,%edx
div    %ebx
mov    %edx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@get_rand_int.fp = internal global i32* null
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_rand_int(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32*, i32** @get_rand_int.fp
%6 = icmp ne i32* %5, null
br i1 %6, label %9, label %7
7: ; preds = %1
%8 = 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* %8, i32** @get_rand_int.fp
br label %9
9: ; preds = %7, %1
%10 = load i32*, i32** @get_rand_int.fp
%11 = call i64 @fread(i32* %4, i32 4, i32 1, i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* %4
%15 = load i32, i32* %3
%16 = urem i32 %14, %15
store i32 %16, i32* %2
br label %24
17: ; preds = %9
%18 = call i64 @time(i32* null)
%19 = trunc i64 %18 to i32
%20 = call i32 @srand(i32 %19)
%21 = call i32 (...) @rand()
%22 = load i32, i32* %3
%23 = urem i32 %21, %22
store i32 %23, i32* %2
br label %24
24: ; preds = %17, %13
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
@get_rand_int.fp = internal unnamed_addr global i32* null
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @get_rand_int(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32*, i32** @get_rand_int.fp
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = 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))
store i32* %7, i32** @get_rand_int.fp
br label %8
8: ; preds = %6, %1
%9 = phi i32* [ %7, %6 ], [ %4, %1 ]
%10 = call i64 @fread(i32* %2, i32 4, i32 1, i32* %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = load i32, i32* %2
br label %19
14: ; preds = %8
%15 = tail call i64 @time(i32* null)
%16 = trunc i64 %15 to i32
%17 = tail call i32 @srand(i32 %16)
%18 = tail call i32 (...) @rand()
br label %19
19: ; preds = %14, %12
%20 = phi i32 [ %13, %12 ], [ %18, %14 ]
%21 = urem i32 %20, %0
ret i32 %21
}