#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*/ ENOSYS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
int read(int, void *, int);
int read_random_bytes(void *buf, int len) {
  int fd, rc;
  fd = open("/dev/urandom", O_RDONLY);
  if (fd < 0) {
    errno = ENOSYS;
    return -1;
  }
  rc = read(fd, buf, len);
  if (rc < len)
    rc = -1;
  close(fd);
  return rc;
}


x86 assembly (clang, -O0):
<read_random_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
lea    0xeb1(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    117f <read_random_bytes+0x4f>
mov    0x2e78(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11b2 <read_random_bytes+0x82>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a4 <read_random_bytes+0x74>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_random_bytes>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2ea8(%rip),%rax
mov    (%rax),%esi
lea    0xeb7(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1173 <read_random_bytes+0x43>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
cmp    %ebx,%eax
mov    $0xffffffff,%ebx
cmovge %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
jmp    118a <read_random_bytes+0x5a>
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    0x2e4d(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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