#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 {
  int tv_sec;
  int tv_usec;
};
typedef int /*<<< orphan*/ seed;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int getpid();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, unsigned int *, int);
int /*<<< orphan*/ srandom(unsigned int);
void random_seed(void) {
  int fd;
  struct timeval tv;
  unsigned int seed;
  if ((fd = open("/dev/hwrng", O_RDONLY)) >= 0 ||
      (fd = open("/dev/random", O_RDONLY)) >= 0 ||
      (fd = open("/dev/urandom", O_RDONLY)) >= 0) {
    read(fd, &seed, sizeof(seed));
    close(fd);
  } else {
    gettimeofday(&tv, NULL);
    seed = (getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec;
  }
  srandom(seed);
}


x86 assembly (clang, -O0):
<random_seed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jge    11cb <random_seed+0x6b>
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jge    11cb <random_seed+0x6b>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe5d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jl     11e9 <random_seed+0x89>
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
jmp    1209 <random_seed+0xa9>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
shl    $0x10,%eax
xor    -0x10(%rbp),%eax
xor    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <srandom@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_seed>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e7a(%rip),%r14
mov    (%r14),%esi
lea    0xe88(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ad <random_seed+0x4d>
mov    (%r14),%esi
lea    0xe7e(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ad <random_seed+0x4d>
mov    (%r14),%esi
lea    0xe75(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11d5 <random_seed+0x75>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1070 <read@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
add    $0x18,%rsp
pop    %rbx
pop    %r14
jmp    1030 <srandom@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
shl    $0x10,%edi
xor    0x10(%rsp),%edi
xor    0x14(%rsp),%edi
jmp    11c9 <random_seed+0x69>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [11 x i8] c"/dev/hwrng\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"/dev/random\00"
@.str.2 = [13 x i8] c"/dev/urandom\00"
define void @random_seed() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %1
%6 = icmp sge i32 %5, 0
br i1 %6, label %15, label %7
7: ; preds = %0
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %8)
store i32 %9, i32* %1
%10 = icmp sge i32 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %12)
store i32 %13, i32* %1
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %11, %7, %0
%16 = load i32, i32* %1
%17 = call i32 @read(i32 %16, i32* %3, i32 4)
%18 = load i32, i32* %1
%19 = call i32 @close(i32 %18)
br label %30
20: ; preds = %11
%21 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%22 = call i32 (...) @getpid()
%23 = shl i32 %22, 16
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%25 = load i32, i32* %24
%26 = xor i32 %23, %25
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%28 = load i32, i32* %27
%29 = xor i32 %26, %28
store i32 %29, i32* %3
br label %30
30: ; preds = %20, %15
%31 = load i32, i32* %3
%32 = call i32 @srandom(i32 %31)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [11 x i8] c"/dev/hwrng\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"/dev/random\00"
@.str.2 = [13 x i8] c"/dev/urandom\00"
define void @random_seed() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp sgt i32 %6, -1
br i1 %7, label %16, label %8
8: ; preds = %0
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = icmp sgt i32 %10, -1
br i1 %11, label %16, label %12
12: ; preds = %8
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = icmp sgt i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %12, %8, %0
%17 = phi i32 [ %6, %0 ], [ %10, %8 ], [ %14, %12 ]
%18 = call i32 @read(i32 %17, i32* %2, i32 4)
%19 = tail call i32 @close(i32 %17)
%20 = load i32, i32* %2
br label %31
21: ; preds = %12
%22 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%23 = tail call i32 (...) @getpid()
%24 = shl i32 %23, 16
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%26 = load i32, i32* %25
%27 = xor i32 %24, %26
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%29 = load i32, i32* %28
%30 = xor i32 %27, %29
br label %31
31: ; preds = %21, %16
%32 = phi i32 [ %30, %21 ], [ %20, %16 ]
%33 = tail call i32 @srandom(i32 %32)
ret void
}