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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(long *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ srand(long);
long get_rand() {
  long seed;
  FILE *f = fopen("/dev/urandom", "r");
  fread(&seed, sizeof(seed), 1, f);
  fclose(f);
  seed = seed % 5000;
  if (seed < 0)
    seed = seed * -1;
  srand(seed);
  return seed;
}


x86 assembly (clang, -O0):
<get_rand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeb1(%rip),%rdi
lea    0xeb7(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rcx
lea    -0x8(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8(%rbp),%rax
mov    $0x1388,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jge    11a5 <get_rand+0x65>
imul   $0xffffffffffffffff,-0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <srand@plt>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_rand>:
push   %rbx
sub    $0x10,%rsp
lea    0xeb4(%rip),%rdi
lea    0xeba(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x8(%rsp),%rcx
movabs $0x346dc5d63886594b,%rdx
c5 6d 34
mov    %rcx,%rax
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0xa,%rdx
add    %rax,%rdx
imul   $0x1388,%rdx,%rax
sub    %rax,%rcx
mov    %rcx,%rbx
neg    %rbx
cmovs  %rcx,%rbx
mov    %rbx,%rdi
call   1050 <srand@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define i64 @get_rand() {
%1 = alloca i64
%2 = alloca i32*
%3 = 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* %3, i32** %2
%4 = load i32*, i32** %2
%5 = call i32 @fread(i64* %1, i32 8, i32 1, i32* %4)
%6 = load i32*, i32** %2
%7 = call i32 @fclose(i32* %6)
%8 = load i64, i64* %1
%9 = srem i64 %8, 5000
store i64 %9, i64* %1
%10 = load i64, i64* %1
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = load i64, i64* %1
%14 = mul nsw i64 %13, -1
store i64 %14, i64* %1
br label %15
15: ; preds = %12, %0
%16 = load i64, i64* %1
%17 = call i32 @srand(i64 %16)
%18 = load i64, i64* %1
ret i64 %18
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define i64 @get_rand() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = 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))
%4 = call i32 @fread(i64* %1, i32 8, i32 1, i32* %3)
%5 = tail call i32 @fclose(i32* %3)
%6 = load i64, i64* %1
%7 = srem i64 %6, 5000
%8 = tail call i64 @llvm.abs.i64(i64 %7, i1 true)
%9 = tail call i32 @srand(i64 %8)
ret i64 %8
}