#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 */
scalar_t__ EOF;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fread(unsigned int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ srand(unsigned int);
int /*<<< orphan*/ stderr;
scalar_t__ time(int /*<<< orphan*/ *);
void init_seed(void) {
  unsigned int seed;
  FILE *f;
  f = fopen("/dev/urandom", "r");
  if (f == NULL) {
    perror("Can not open /dev/urandom");
    fprintf(stderr, "Using system time to seed RNG.\n");
    seed = (unsigned int)time(NULL);
  } else {
    if (1 != fread(&seed, sizeof(seed), 1, f)) {
      fprintf(stderr, "Can not read from /dev/urandom. "
                      "Using system time to seed RNG.\n");
      seed = (unsigned int)time(NULL);
    }
    if (fclose(f) == EOF) {
      perror("Can not close /dev/urandom");
    }
  }
  srand(seed);
}


x86 assembly (clang, -O0):
<init_seed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xeeb(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cc <init_seed+0x5c>
lea    0xe61(%rip),%rdi
call   1090 <perror@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,-0x4(%rbp)
jmp    123d <init_seed+0xcd>
mov    -0x10(%rbp),%rcx
lea    -0x4(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
je     1213 <init_seed+0xa3>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2dc5(%rip),%rcx
cmp    (%rcx),%rax
jne    1238 <init_seed+0xc8>
lea    0xe29(%rip),%rdi
call   1090 <perror@plt>
jmp    123d <init_seed+0xcd>
mov    -0x4(%rbp),%edi
call   1050 <srand@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_seed>:
push   %rbx
sub    $0x10,%rsp
lea    0xeee(%rip),%rdi
lea    0xe7d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11ee <init_seed+0x7e>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11cc <init_seed+0x5c>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
je     121c <init_seed+0xac>
mov    0xc(%rsp),%edi
add    $0x10,%rsp
pop    %rbx
jmp    1050 <srand@plt>
lea    0xe0d(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,0xc(%rsp)
jmp    11e0 <init_seed+0x70>
lea    0xe39(%rip),%rdi
call   1090 <perror@plt>
jmp    11e0 <init_seed+0x70>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [26 x i8] c"Can not open /dev/urandom\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"Using system time to seed RNG.\0A\00"
@.str.4 = [64 x i8] c"Can not read from /dev/urandom. Using system time to seed RNG.\0A\00"
@EOF = global i64 0
@.str.5 = [27 x i8] c"Can not close /dev/urandom\00"
define void @init_seed() {
%1 = alloca i32
%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 = icmp eq i32* %4, null
br i1 %5, label %6, label %12
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%10 = call i64 @time(i32* null)
%11 = trunc i64 %10 to i32
store i32 %11, i32* %1
br label %29
12: ; preds = %0
%13 = load i32*, i32** %2
%14 = call i32 @fread(i32* %1, i32 4, i32 1, i32* %13)
%15 = icmp ne i32 1, %14
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.4, i64 0, i64 0))
%19 = call i64 @time(i32* null)
%20 = trunc i64 %19 to i32
store i32 %20, i32* %1
br label %21
21: ; preds = %16, %12
%22 = load i32*, i32** %2
%23 = call i64 @fclose(i32* %22)
%24 = load i64, i64* @EOF
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
br label %29
29: ; preds = %28, %6
%30 = load i32, i32* %1
%31 = call i32 @srand(i32 %30)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [26 x i8] c"Can not open /dev/urandom\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"Using system time to seed RNG.\0A\00"
@.str.4 = [64 x i8] c"Can not read from /dev/urandom. Using system time to seed RNG.\0A\00"
@EOF = global i64 0
@.str.5 = [27 x i8] c"Can not close /dev/urandom\00"
define void @init_seed() {
%1 = alloca i32
%2 = bitcast i32* %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 = icmp eq i32* %3, null
br i1 %4, label %5, label %11
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%9 = tail call i64 @time(i32* null)
%10 = trunc i64 %9 to i32
store i32 %10, i32* %1
br label %25
11: ; preds = %0
%12 = call i32 @fread(i32* %1, i32 4, i32 1, i32* %3)
%13 = icmp eq i32 %12, 1
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.4, i64 0, i64 0))
%17 = tail call i64 @time(i32* null)
%18 = trunc i64 %17 to i32
store i32 %18, i32* %1
br label %19
19: ; preds = %14, %11
%20 = tail call i64 @fclose(i32* %3)
%21 = load i64, i64* @EOF
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %25
25: ; preds = %19, %23, %5
%26 = load i32, i32* %1
%27 = tail call i32 @srand(i32 %26)
ret void
}