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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, void *, size_t);
ssize_t rand_bytes(void *stream, size_t len) {
  static int urand = -1;
  if (urand == -1) {
    urand = open("/dev/urandom", O_RDONLY, 0);
  }
  if (urand < 0) {
    return -1;
  }
  return read(urand, stream, len);
}


x86 assembly (clang, -O0):
<rand_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpl   $0xffffffff,0x2ef9(%rip)
jne    115a <rand_bytes+0x3a>
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
lea    0xeb3(%rip),%rdi
xor    %edx,%edx
call   1040 <open@plt>
mov    %eax,0x2ed6(%rip)
cmpl   $0x0,0x2ecf(%rip)
jge    1173 <rand_bytes+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    1189 <rand_bytes+0x69>
mov    0x2eb7(%rip),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_bytes>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2f00(%rip),%edi
cmp    $0xffffffff,%edi
je     114b <rand_bytes+0x2b>
test   %edi,%edi
js     116e <rand_bytes+0x4e>
mov    %rbx,%rsi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <read@plt>
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
lea    0xea5(%rip),%rdi
xor    %edx,%edx
call   1040 <open@plt>
mov    %eax,%edi
mov    %eax,0x2ec6(%rip)
test   %edi,%edi
jns    1139 <rand_bytes+0x19>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@rand_bytes.urand = internal unnamed_addr global i32 -1
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @rand_bytes(i8* nocapture %0, i64 %1) {
%3 = load i32, i32* @rand_bytes.urand
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %2
%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, i32 0)
store i32 %7, i32* @rand_bytes.urand
br label %8
8: ; preds = %5, %2
%9 = phi i32 [ %7, %5 ], [ %3, %2 ]
%10 = icmp slt i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @read(i32 %9, i8* %0, i64 %1)
br label %13
13: ; preds = %8, %11
%14 = phi i32 [ %12, %11 ], [ -1, %8 ]
ret i32 %14
}