#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*/);
scalar_t__ random();
int read(int, void *, size_t);
int random_bytes(void *buffer, size_t size) {
  static int devrandom = -1;
  int rc, i;
  if (devrandom < 0) {
    devrandom = open("/dev/urandom", O_RDONLY);
    if (devrandom < 0)
      goto fail;
  }
  rc = read(devrandom, buffer, size);
  return rc;
fail:
  for (i = 0; i < size; i++)
    ((unsigned char *)buffer)[i] = (unsigned char)(random());
  return 0;
}


x86 assembly (clang, -O0):
<random_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpl   $0x0,0x2ef1(%rip)
jge    117f <random_bytes+0x4f>
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
lea    0xea3(%rip),%rdi
call   1050 <open@plt>
mov    %eax,0x2ed0(%rip)
cmpl   $0x0,0x2ec9(%rip)
jge    117a <random_bytes+0x4a>
jmp    11a0 <random_bytes+0x70>
jmp    117f <random_bytes+0x4f>
mov    0x2eb3(%rip),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11de <random_bytes+0xae>
movl   $0x0,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11d7 <random_bytes+0xa7>
mov    $0x0,%al
call   1040 <random@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11a7 <random_bytes+0x77>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_bytes>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2ef7(%rip),%edi
test   %edi,%edi
jns    1166 <random_bytes+0x36>
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
lea    0xeab(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%edi
mov    %eax,0x2ed6(%rip)
test   %eax,%eax
js     1176 <random_bytes+0x46>
mov    %r14,%rsi
mov    %r15,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <read@plt>
test   %r15,%r15
je     1194 <random_bytes+0x64>
xor    %ebx,%ebx
nopl   (%rax)
xor    %eax,%eax
call   1040 <random@plt>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1180 <random_bytes+0x50>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@random_bytes.devrandom = internal global i32 -1
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @random_bytes(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* @random_bytes.devrandom
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* @random_bytes.devrandom
%13 = load i32, i32* @random_bytes.devrandom
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %10
br label %23
16: ; preds = %10
br label %17
17: ; preds = %16, %2
%18 = load i32, i32* @random_bytes.devrandom
%19 = load i8*, i8** %4
%20 = load i64, i64* %5
%21 = call i32 @read(i32 %18, i8* %19, i64 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
store i32 %22, i32* %3
br label %40
23: ; preds = %15
store i32 0, i32* %7
br label %24
24: ; preds = %36, %23
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = load i64, i64* %5
%28 = icmp ult i64 %26, %27
br i1 %28, label %29, label %39
29: ; preds = %24
%30 = call i64 (...) @random()
%31 = trunc i64 %30 to i8
%32 = load i8*, i8** %4
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 %31, i8* %35
br label %36
36: ; preds = %29
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %24
39: ; preds = %24
store i32 0, i32* %3
br label %40
40: ; preds = %39, %17
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@random_bytes.devrandom = internal unnamed_addr global i32 -1
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @random_bytes(i8* nocapture %0, i64 %1) {
%3 = load i32, i32* @random_bytes.devrandom
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %11
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)
store i32 %7, i32* @random_bytes.devrandom
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = icmp eq i64 %1, 0
br i1 %10, label %21, label %14
11: ; preds = %5, %2
%12 = phi i32 [ %7, %5 ], [ %3, %2 ]
%13 = tail call i32 @read(i32 %12, i8* %0, i64 %1)
br label %21
14: ; preds = %9, %14
%15 = phi i64 [ %19, %14 ], [ 0, %9 ]
%16 = tail call i64 (...) @random()
%17 = trunc i64 %16 to i8
%18 = getelementptr inbounds i8, i8* %0, i64 %15
store i8 %17, i8* %18
%19 = add nuw i64 %15, 1
%20 = icmp eq i64 %19, %1
br i1 %20, label %21, label %14
21: ; preds = %14, %9, %11
%22 = phi i32 [ %13, %11 ], [ 0, %9 ], [ 0, %14 ]
ret i32 %22
}