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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(uintptr_t *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
uintptr_t random_uintptr_from_dev_urandom() {
  static FILE *f = NULL;
  if (f == NULL) {
    f = fopen("/dev/urandom", "r");
    if (!f) {
      perror("cannot open /dev/urandom");
      exit(-1);
    }
  }
  {
    uintptr_t a;
    fread(&a, sizeof(uintptr_t), 1, f);
    return a;
  }
}


x86 assembly (clang, -O0):
<random_uintptr_from_dev_urandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpq   $0x0,0x2ef8(%rip)
jne    1199 <random_uintptr_from_dev_urandom+0x59>
lea    0xeb1(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,0x2ed8(%rip)
cmpq   $0x0,0x2ed0(%rip)
jne    1194 <random_uintptr_from_dev_urandom+0x54>
lea    0xe7d(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
jmp    1199 <random_uintptr_from_dev_urandom+0x59>
mov    0x2ea8(%rip),%rcx
lea    -0x8(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_uintptr_from_dev_urandom>:
push   %rax
mov    0x2f00(%rip),%rcx
test   %rcx,%rcx
jne    116f <random_uintptr_from_dev_urandom+0x2f>
lea    0xeba(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,%rcx
mov    %rax,0x2ede(%rip)
test   %rax,%rax
je     1187 <random_uintptr_from_dev_urandom+0x47>
mov    %rsp,%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    (%rsp),%rax
pop    %rcx
ret
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@random_uintptr_from_dev_urandom.f = internal global i32* null
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"cannot open /dev/urandom\00"
define i64 @random_uintptr_from_dev_urandom() {
%1 = alloca i64
%2 = load i32*, i32** @random_uintptr_from_dev_urandom.f
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %12
4: ; preds = %0
%5 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** @random_uintptr_from_dev_urandom.f
%6 = load i32*, i32** @random_uintptr_from_dev_urandom.f
%7 = icmp ne i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %4
br label %12
12: ; preds = %11, %0
%13 = load i32*, i32** @random_uintptr_from_dev_urandom.f
%14 = call i32 @fread(i64* %1, i32 8, i32 1, i32* %13)
%15 = load i64, i64* %1
ret i64 %15
}

LLVM IR (-O2):
@random_uintptr_from_dev_urandom.f = internal unnamed_addr global i32* null
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"cannot open /dev/urandom\00"
define i64 @random_uintptr_from_dev_urandom() {
%1 = alloca i64
%2 = load i32*, i32** @random_uintptr_from_dev_urandom.f
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %10
4: ; preds = %0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** @random_uintptr_from_dev_urandom.f
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %4, %0
%11 = phi i32* [ %5, %4 ], [ %2, %0 ]
%12 = bitcast i64* %1 to i8*
%13 = call i32 @fread(i64* %1, i32 8, i32 1, i32* %11)
%14 = load i64, i64* %1
ret i64 %14
}