#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 */
struct timeval {
  unsigned int tv_sec;
  unsigned int tv_usec;
};
typedef int /*<<< orphan*/ seed;
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*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
unsigned long int random_seed() {
  unsigned int seed;
  struct timeval tv;
  FILE *devrandom;
  if ((devrandom = fopen("/dev/urandom", "r")) == NULL) {
    gettimeofday(&tv, 0);
    seed = tv.tv_sec + tv.tv_usec;
  } else {
    fread(&seed, sizeof(seed), 1, devrandom);
    fclose(devrandom);
  }
  return seed;
}


x86 assembly (clang, -O0):
<random_seed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
lea    0xeb7(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1182 <random_seed+0x42>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a2 <random_seed+0x62>
mov    -0x18(%rbp),%rcx
lea    -0x4(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_seed>:
push   %rbx
sub    $0x10,%rsp
lea    0xeb4(%rip),%rdi
lea    0xeba(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1185 <random_seed+0x45>
mov    %rax,%rbx
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x4(%rsp),%eax
jmp    1199 <random_seed+0x59>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    0xc(%rsp),%eax
add    0x8(%rsp),%eax
mov    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define i64 @random_seed() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i32*
%4 = 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* %4, i32** %3
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %13
6: ; preds = %0
%7 = call i32 @gettimeofday(%struct.timeval* %2, i32 0)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%11 = load i32, i32* %10
%12 = add i32 %9, %11
store i32 %12, i32* %1
br label %18
13: ; preds = %0
%14 = load i32*, i32** %3
%15 = call i32 @fread(i32* %1, i32 4, i32 1, i32* %14)
%16 = load i32*, i32** %3
%17 = call i32 @fclose(i32* %16)
br label %18
18: ; preds = %13, %6
%19 = load i32, i32* %1
%20 = zext i32 %19 to i64
ret i64 %20
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define i64 @random_seed() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.timeval* %2 to i8*
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %14
7: ; preds = %0
%8 = call i32 @gettimeofday(%struct.timeval* %2, i32 0)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%12 = load i32, i32* %11
%13 = add i32 %12, %10
br label %18
14: ; preds = %0
%15 = call i32 @fread(i32* %1, i32 4, i32 1, i32* %5)
%16 = tail call i32 @fclose(i32* %5)
%17 = load i32, i32* %1
br label %18
18: ; preds = %14, %7
%19 = phi i32 [ %17, %14 ], [ %13, %7 ]
%20 = zext i32 %19 to i64
ret i64 %20
}