#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert_failed(char *);
int close_fd(int);
int open(char *, int /*<<< orphan*/);
int read_fd(int, unsigned long *, int);
int /*<<< orphan*/ srandom(unsigned long);
void __attribute__((constructor)) init_random() {
  int fd = open("/dev/urandom", O_RDONLY);
  if (fd == -1)
    assert_failed("open(/dev/urandom) failed");
  unsigned long seed;
  if (read_fd(fd, &seed, sizeof seed) != sizeof seed)
    assert_failed("read(/dev/urandom) failed");
  if (close_fd(fd) == -1)
    assert_failed("close(/dev/urandom) failed");
  srandom(seed);
}


x86 assembly (clang, -O0):
<init_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1186 <init_random+0x36>
lea    0xe8c(%rip),%rdi
call   1070 <assert_failed@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1040 <read_fd@plt>
cltq
cmp    $0x8,%rax
je     11af <init_random+0x5f>
lea    0xe7d(%rip),%rdi
call   1070 <assert_failed@plt>
mov    -0x4(%rbp),%edi
call   1050 <close_fd@plt>
cmp    $0xffffffff,%eax
jne    11cc <init_random+0x7c>
lea    0xe7a(%rip),%rdi
call   1070 <assert_failed@plt>
mov    -0x10(%rbp),%rdi
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    0xe9b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    117d <init_random+0x2d>
lea    0xe95(%rip),%rdi
call   1070 <assert_failed@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1040 <read_fd@plt>
cmp    $0x8,%eax
je     119f <init_random+0x4f>
lea    0xe8d(%rip),%rdi
call   1070 <assert_failed@plt>
mov    %ebx,%edi
call   1050 <close_fd@plt>
cmp    $0xffffffff,%eax
jne    11b7 <init_random+0x67>
lea    0xe8f(%rip),%rdi
call   1070 <assert_failed@plt>
mov    0x8(%rsp),%rdi
call   1030 <srandom@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"open(/dev/urandom) failed\00"
@.str.2 = [26 x i8] c"read(/dev/urandom) failed\00"
@.str.3 = [27 x i8] c"close(/dev/urandom) failed\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @init_random, i8* null }]
define void @init_random() {
%1 = alloca i32
%2 = alloca i64
%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* %1
%5 = load i32, i32* %1
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @assert_failed(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = load i32, i32* %1
%11 = call i32 @read_fd(i32 %10, i64* %2, i32 8)
%12 = sext i32 %11 to i64
%13 = icmp ne i64 %12, 8
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @assert_failed(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %9
%17 = load i32, i32* %1
%18 = call i32 @close_fd(i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @assert_failed(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = load i64, i64* %2
%24 = call i32 @srandom(i64 %23)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"open(/dev/urandom) failed\00"
@.str.2 = [26 x i8] c"read(/dev/urandom) failed\00"
@.str.3 = [27 x i8] c"close(/dev/urandom) failed\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @init_random, i8* null }]
define void @init_random() {
%1 = alloca i64
%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 %7
5: ; preds = %0
%6 = tail call i32 @assert_failed(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = bitcast i64* %1 to i8*
%9 = call i32 @read_fd(i32 %3, i64* %1, i32 8)
%10 = icmp eq i32 %9, 8
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = call i32 @assert_failed(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %11, %7
%14 = call i32 @close_fd(i32 %3)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @assert_failed(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = load i64, i64* %1
%20 = call i32 @srandom(i64 %19)
ret void
}