#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 /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, void *, size_t);
ssize_t fill_buffer_random(void *buf, size_t len) {
  int rndsrc = open("/dev/urandom", O_RDONLY);
  if (rndsrc < 0)
    rndsrc = open("/dev/random", O_RDONLY);
  if (rndsrc < 0)
    return -1;
  ssize_t res = read(rndsrc, buf, len);
  close(rndsrc);
  return res;
}


x86 assembly (clang, -O0):
<fill_buffer_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%esi
lea    0xeb0(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    117a <fill_buffer_random+0x4a>
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1190 <fill_buffer_random+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <fill_buffer_random+0x81>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_buffer_random>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ea4(%rip),%r15
mov    (%r15),%esi
lea    0xeb2(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    116e <fill_buffer_random+0x3e>
mov    (%r15),%esi
lea    0xeaa(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1191 <fill_buffer_random+0x61>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    1186 <fill_buffer_random+0x56>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"/dev/random\00"
define i32 @fill_buffer_random(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %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 %15
12: ; preds = %2
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 %14, i32* %6
br label %15
15: ; preds = %12, %2
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 -1, i32* %3
br label %27
19: ; preds = %15
%20 = load i32, i32* %6
%21 = load i8*, i8** %4
%22 = load i64, i64* %5
%23 = call i32 @read(i32 %20, i8* %21, i64 %22)
store i32 %23, i32* %7
%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 = %19, %18
%28 = load i32, i32* %3
ret i32 %28
}

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