#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 uint64_t;
typedef int /*<<< orphan*/ seed;
typedef scalar_t__ int64_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ full_read(int, int *, int);
scalar_t__ getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ srand(int);
scalar_t__ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ twister_init_by_array64(int *, int);
int /*<<< orphan*/ twister_init_genrand64(int);
void random_init(void) {
  int fd = open("/dev/urandom", O_RDONLY);
  if (fd == -1) {
    fd = open("/dev/random", O_RDONLY);
  }
  if (fd >= 0) {
    uint64_t seed[8];
    if (full_read(fd, seed, sizeof(seed)) < (int64_t)sizeof(seed))
      goto nasty_fallback;
    srand(seed[0]);
    twister_init_by_array64(seed, sizeof(seed) / sizeof(seed[0]));
  } else {
    uint64_t seed;
  nasty_fallback:
    seed = (uint64_t)getpid() ^ (uint64_t)time(NULL);
    seed |= (((uint64_t)(uintptr_t)&seed) << 32);
    srand(seed);
    twister_init_genrand64(seed);
  }
  close(fd);
  return;
}


x86 assembly (clang, -O0):
<random_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    11c2 <random_init+0x42>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     1207 <random_init+0x87>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x20,%edx
call   1040 <full_read@plt>
cmp    $0x20,%rax
jge    11ec <random_init+0x6c>
jmp    120c <random_init+0x8c>
mov    -0x30(%rbp),%edi
call   1070 <srand@plt>
lea    -0x30(%rbp),%rdi
mov    $0x8,%esi
call   1030 <twister_init_by_array64@plt>
jmp    124b <random_init+0xcb>
jmp    120c <random_init+0x8c>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x38(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%eax
xor    %ecx,%eax
mov    %eax,-0x34(%rbp)
lea    -0x34(%rbp),%rax
mov    $0x20,%ecx
shl    %cl,%eax
or     -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
call   1070 <srand@plt>
mov    -0x34(%rbp),%edi
call   1090 <twister_init_genrand64@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_init>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e5a(%rip),%r14
mov    (%r14),%esi
lea    0xe68(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11aa <random_init+0x2a>
test   %ebx,%ebx
jns    11bf <random_init+0x3f>
jmp    11d4 <random_init+0x54>
mov    (%r14),%esi
lea    0xe59(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
test   %ebx,%ebx
js     11d4 <random_init+0x54>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x20,%edx
call   1040 <full_read@plt>
cmp    $0x20,%rax
jge    11ee <random_init+0x6e>
xor    %eax,%eax
call   1050 <getpid@plt>
xor    %edi,%edi
call   1080 <time@plt>
call   1070 <srand@plt>
call   1090 <twister_init_genrand64@plt>
jmp    1203 <random_init+0x83>
mov    (%rsp),%edi
call   1070 <srand@plt>
mov    %rsp,%rdi
mov    $0x8,%esi
call   1030 <twister_init_by_array64@plt>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"/dev/random\00"
define void @random_init() {
%1 = alloca i32
%2 = alloca [8 x i32]
%3 = alloca i32
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %9)
store i32 %10, i32* %1
br label %11
11: ; preds = %8, %0
%12 = load i32, i32* %1
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %26
14: ; preds = %11
%15 = load i32, i32* %1
%16 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 0
%17 = call i64 @full_read(i32 %15, i32* %16, i32 32)
%18 = icmp slt i64 %17, 32
br i1 %18, label %19, label %20
19: ; preds = %14
br label %27
20: ; preds = %14
%21 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 0
%22 = load i32, i32* %21
%23 = call i32 @srand(i32 %22)
%24 = getelementptr inbounds [8 x i32], [8 x i32]* %2, i64 0, i64 0
%25 = call i32 @twister_init_by_array64(i32* %24, i32 8)
br label %42
26: ; preds = %11
br label %27
27: ; preds = %26, %19
%28 = call i64 (...) @getpid()
%29 = trunc i64 %28 to i32
%30 = call i64 @time(i32* null)
%31 = trunc i64 %30 to i32
%32 = xor i32 %29, %31
store i32 %32, i32* %3
%33 = ptrtoint i32* %3 to i64
%34 = trunc i64 %33 to i32
%35 = shl i32 %34, 32
%36 = load i32, i32* %3
%37 = or i32 %36, %35
store i32 %37, i32* %3
%38 = load i32, i32* %3
%39 = call i32 @srand(i32 %38)
%40 = load i32, i32* %3
%41 = call i32 @twister_init_genrand64(i32 %40)
br label %42
42: ; preds = %27, %20
%43 = load i32, i32* %1
%44 = call i32 @close(i32 %43)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"/dev/random\00"
define void @random_init() {
%1 = alloca [8 x i32]
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %8
8: ; preds = %5, %0
%9 = phi i32 [ %7, %5 ], [ %3, %0 ]
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %21
11: ; preds = %8
%12 = bitcast [8 x i32]* %1 to i8*
%13 = getelementptr inbounds [8 x i32], [8 x i32]* %1, i64 0, i64 0
%14 = call i64 @full_read(i32 %9, i32* %13, i32 32)
%15 = icmp slt i64 %14, 32
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = load i32, i32* %13
%18 = call i32 @srand(i32 %17)
%19 = call i32 @twister_init_by_array64(i32* %13, i32 8)
br label %26
20: ; preds = %11
br label %21
21: ; preds = %20, %8
%22 = call i64 (...) @getpid()
%23 = call i64 @time(i32* null)
%24 = call i32 @srand(i32 poison)
%25 = call i32 @twister_init_genrand64(i32 undef)
br label %26
26: ; preds = %16, %21
%27 = call i32 @close(i32 %9)
ret void
}