#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 open(char *, int /*<<< orphan*/);
unsigned long random();
scalar_t__ read(int, unsigned long *, int);
unsigned long myrandom(void) {
  static int fd = -1;
  unsigned long data;
  if (fd == -1)
    fd = open("/dev/urandom", O_RDONLY);
  if (fd == -1 || read(fd, &data, sizeof data) <= 0)
    return random();
  return data;
}


x86 assembly (clang, -O0):
<myrandom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpl   $0xffffffff,0x2ef9(%rip)
jne    1160 <myrandom+0x30>
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
lea    0xeab(%rip),%rdi
call   1050 <open@plt>
mov    %eax,0x2ed8(%rip)
cmpl   $0xffffffff,0x2ed1(%rip)
je     118b <myrandom+0x5b>
mov    0x2ec5(%rip),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
jg     119b <myrandom+0x6b>
mov    $0x0,%al
call   1040 <random@plt>
mov    %rax,-0x8(%rbp)
jmp    11a3 <myrandom+0x73>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myrandom>:
push   %rax
mov    0x2f01(%rip),%edi
cmp    $0xffffffff,%edi
jne    115e <myrandom+0x2e>
mov    0x2ea5(%rip),%rax
mov    (%rax),%esi
lea    0xeb4(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%edi
mov    %eax,0x2edf(%rip)
cmp    $0xffffffff,%eax
je     1176 <myrandom+0x46>
mov    %rsp,%rsi
mov    $0x8,%edx
call   1030 <read@plt>
test   %rax,%rax
jle    1176 <myrandom+0x46>
mov    (%rsp),%rax
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
jmp    1040 <random@plt>

LLVM IR (-O0):
@myrandom.fd = internal global i32 -1
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i64 @myrandom() {
%1 = alloca i64
%2 = alloca i64
%3 = load i32, i32* @myrandom.fd
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* @myrandom.fd
br label %8
8: ; preds = %5, %0
%9 = load i32, i32* @myrandom.fd
%10 = icmp eq i32 %9, -1
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = load i32, i32* @myrandom.fd
%13 = call i64 @read(i32 %12, i64* %2, i32 8)
%14 = icmp sle i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11, %8
%16 = call i64 (...) @random()
store i64 %16, i64* %1
br label %19
17: ; preds = %11
%18 = load i64, i64* %2
store i64 %18, i64* %1
br label %19
19: ; preds = %17, %15
%20 = load i64, i64* %1
ret i64 %20
}

LLVM IR (-O2):
@myrandom.fd = internal unnamed_addr global i32 -1
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i64 @myrandom() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = load i32, i32* @myrandom.fd
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* @myrandom.fd
%8 = icmp eq i32 %7, -1
br i1 %8, label %13, label %9
9: ; preds = %0, %5
%10 = phi i32 [ %7, %5 ], [ %3, %0 ]
%11 = call i64 @read(i32 %10, i64* %1, i32 8)
%12 = icmp slt i64 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %9, %5
%14 = tail call i64 (...) @random()
br label %17
15: ; preds = %9
%16 = load i64, i64* %1
br label %17
17: ; preds = %15, %13
%18 = phi i64 [ %14, %13 ], [ %16, %15 ]
ret i64 %18
}