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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, scalar_t__ *, int);
int /*<<< orphan*/ stderr;
uint64_t random_seed() {
  uint64_t lfsr;
  int fd_urandom;
  fd_urandom = open("/dev/urandom", O_RDONLY);
  if (fd_urandom == -1) {
    perror("open(\"/dev/urandom\", O_RDONLY)");
    return 0;
  }
  if (read(fd_urandom, &lfsr, sizeof(lfsr)) != sizeof(lfsr)) {
    perror("read(fd_urandom, &lfsr, sizeof(lfsr))");
    return 0;
  }
  close(fd_urandom);
  if (lfsr == 0) {
    fprintf(stderr,
            "WTF: read 64 zero bits from /dev/urandom, what are the odds?\n");
  }
  return lfsr;
}


x86 assembly (clang, -O0):
<random_seed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1193 <random_seed+0x43>
lea    0xe8c(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <random_seed+0xa9>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1040 <read@plt>
cltq
cmp    $0x8,%rax
je     11c9 <random_seed+0x79>
lea    0xe75(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <random_seed+0xa9>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11f1 <random_seed+0xa1>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_seed>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11b5 <random_seed+0x65>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1040 <read@plt>
cmp    $0x8,%eax
jne    11c3 <random_seed+0x73>
mov    %ebx,%edi
call   1030 <close@plt>
mov    0x8(%rsp),%rax
test   %rax,%rax
jne    11af <random_seed+0x5f>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xeaa(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe51(%rip),%rdi
call   1070 <perror@plt>
jmp    11ad <random_seed+0x5d>
lea    0xe62(%rip),%rdi
call   1070 <perror@plt>
jmp    11ad <random_seed+0x5d>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"open(\22/dev/urandom\22, O_RDONLY)\00"
@.str.2 = [38 x i8] c"read(fd_urandom, &lfsr, sizeof(lfsr))\00"
@stderr = global i32 0
@.str.3 = [62 x i8] c"WTF: read 64 zero bits from /dev/urandom, what are the odds?\0A\00"
define i64 @random_seed() {
%1 = alloca i64
%2 = alloca i64
%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* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* %1
br label %27
10: ; preds = %0
%11 = load i32, i32* %3
%12 = call i32 @read(i32 %11, i64* %2, i32 8)
%13 = sext i32 %12 to i64
%14 = icmp ne i64 %13, 8
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i64 0, i64* %1
br label %27
17: ; preds = %10
%18 = load i32, i32* %3
%19 = call i32 @close(i32 %18)
%20 = load i64, i64* %2
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %22, %17
%26 = load i64, i64* %2
store i64 %26, i64* %1
br label %27
27: ; preds = %25, %15, %8
%28 = load i64, i64* %1
ret i64 %28
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [31 x i8] c"open(\22/dev/urandom\22, O_RDONLY)\00"
@.str.2 = [38 x i8] c"read(fd_urandom, &lfsr, sizeof(lfsr))\00"
@stderr = global i32 0
@.str.3 = [62 x i8] c"WTF: read 64 zero bits from /dev/urandom, what are the odds?\0A\00"
define i64 @random_seed() {
%1 = alloca i64
%2 = bitcast i64* %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 eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %20
8: ; preds = %0
%9 = call i32 @read(i32 %4, i64* %1, i32 8)
%10 = icmp eq i32 %9, 8
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %20
13: ; preds = %8
%14 = tail call i32 @close(i32 %4)
%15 = load i64, i64* %1
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0))
br label %20
20: ; preds = %13, %17, %11, %6
%21 = phi i64 [ 0, %6 ], [ 0, %11 ], [ 0, %17 ], [ %15, %13 ]
ret i64 %21
}