#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;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit_errno(char *);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned int *, int);
int /*<<< orphan*/ srandom(unsigned int);
void init_random(void) {
  unsigned int seed;
  int fd = open("/dev/urandom", O_RDONLY);
  if (fd < 0)
    exit_errno("can't open /dev/urandom");
  if (read(fd, &seed, sizeof(seed)) != sizeof(seed))
    exit_errno("can't read from /dev/urandom");
  close(fd);
  srandom(seed);
}


x86 assembly (clang, -O0):
<init_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xec0(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1186 <init_random+0x36>
lea    0xe7f(%rip),%rdi
call   1060 <exit_errno@plt>
mov    -0x8(%rbp),%edi
lea    -0x4(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cltq
cmp    $0x4,%rax
je     11af <init_random+0x5f>
lea    0xe6e(%rip),%rdi
call   1060 <exit_errno@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%edi
call   1030 <srandom@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_random>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
lea    0xec3(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    117c <init_random+0x2c>
lea    0xe89(%rip),%rdi
call   1060 <exit_errno@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
je     119e <init_random+0x4e>
lea    0xe7f(%rip),%rdi
call   1060 <exit_errno@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
add    $0x10,%rsp
pop    %rbx
jmp    1030 <srandom@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"can't open /dev/urandom\00"
@.str.2 = [29 x i8] c"can't read from /dev/urandom\00"
define void @init_random() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_RDONLY
%4 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @exit_errno(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = load i32, i32* %2
%11 = call i32 @read(i32 %10, i32* %1, i32 4)
%12 = sext i32 %11 to i64
%13 = icmp ne i64 %12, 4
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @exit_errno(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
%17 = load i32, i32* %2
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* %1
%20 = call i32 @srandom(i32 %19)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [24 x i8] c"can't open /dev/urandom\00"
@.str.2 = [29 x i8] c"can't read from /dev/urandom\00"
define void @init_random() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @exit_errno(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = call i32 @read(i32 %4, i32* %1, i32 4)
%10 = icmp eq i32 %9, 4
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @exit_errno(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = tail call i32 @close(i32 %4)
%15 = load i32, i32* %1
%16 = tail call i32 @srandom(i32 %15)
ret void
}