#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 /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned char *, unsigned int);
void randombytes(unsigned char *bytes, unsigned int len) {
  int fd = -1;
  int temp;
  while (fd == -1) {
    fd = open("/dev/random", O_RDONLY);
  }
  while (len) {
    temp = read(fd, bytes, len);
    bytes += temp;
    len -= temp;
  }
  close(fd);
}


x86 assembly (clang, -O0):
<randombytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    116d <randombytes+0x3d>
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    0xea0(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
jmp    1146 <randombytes+0x16>
jmp    1172 <randombytes+0x42>
cmpl   $0x0,-0xc(%rbp)
je     11af <randombytes+0x7f>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    1172 <randombytes+0x42>
mov    -0x10(%rbp),%edi
call   1030 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randombytes>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2ea3(%rip),%r12
lea    0xeb4(%rip),%r15
nopl   0x0(%rax)
mov    (%r12),%esi
mov    %r15,%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1150 <randombytes+0x20>
mov    %eax,%ebp
test   %r14d,%r14d
je     1187 <randombytes+0x57>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <read@plt>
cltq
add    %rax,%rbx
sub    %eax,%r14d
jne    1170 <randombytes+0x40>
mov    %ebp,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <close@plt>

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

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