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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(unsigned int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ srand(unsigned int);
void seed_random() {
  FILE *fp = fopen("/dev/urandom", "r");
  unsigned int foo;
  struct timeval t;
  if (!fp) {
    gettimeofday(&t, NULL);
    foo = t.tv_usec;
  } else {
    size_t res = fread(&foo, sizeof(foo), 1, fp);
    if (res == 0)
      printf("fread failed\n");
    fclose(fp);
  }
  srand(foo);
}


x86 assembly (clang, -O0):
<seed_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe91(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11a2 <seed_random+0x42>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    11dd <seed_random+0x7d>
mov    -0x8(%rbp),%rcx
lea    -0xc(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d4 <seed_random+0x74>
lea    0xe40(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0xc(%rbp),%edi
call   1070 <srand@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<seed_random>:
push   %rbx
sub    $0x10,%rsp
lea    0xe94(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11b6 <seed_random+0x56>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %rax,%rax
jne    11a8 <seed_random+0x48>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0xc(%rsp),%edi
jmp    11c6 <seed_random+0x66>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    0x8(%rsp),%edi
add    $0x10,%rsp
pop    %rbx
jmp    1070 <srand@plt>

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

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