#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(unsigned int *, int, int, int /*<<< orphan*/ *);
unsigned int getpid();
int /*<<< orphan*/ srand(unsigned int);
scalar_t__ time(int /*<<< orphan*/ *);
unsigned rand_init() {
  unsigned int seed;
  FILE *fp = fopen("/dev/urandom", "rb");
  if (fp == NULL)
    fp = fopen("/dev/random", "rb");
  if (fp != NULL) {
    fread(&seed, sizeof(seed), 1, fp);
    fclose(fp);
  } else {
    seed = (unsigned int)((unsigned)time(NULL) + getpid());
  }
  srand(seed);
  return seed;
}


x86 assembly (clang, -O0):
<rand_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe91(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a1 <rand_init+0x41>
lea    0xe7f(%rip),%rdi
lea    0xe75(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d1 <rand_init+0x71>
mov    -0x10(%rbp),%rcx
lea    -0x4(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
jmp    11ee <rand_init+0x8e>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <srand@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_init>:
push   %r14
push   %rbx
push   %rax
lea    0xe95(%rip),%rdi
lea    0xe9b(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    119a <rand_init+0x3a>
lea    0xe8a(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ce <rand_init+0x6e>
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x4(%rsp),%ebx
mov    %ebx,%edi
call   1060 <srand@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %edi,%edi
call   1070 <time@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
add    %r14d,%ebx
jmp    11bd <rand_init+0x5d>

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

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