#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*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned char *, size_t);
int /*<<< orphan*/ stderr;
void randombytes(unsigned char *buf, size_t bytes) {
  int r = open("/dev/urandom", O_RDONLY);
  size_t len = 0;
  while (len < bytes) {
    int a = read(r, buf + len, bytes - len);
    if (a < 0) {
      fprintf(stderr, "Could not read random bytes\n");
      exit(EXIT_FAILURE);
    }
    len += a;
  }
  close(r);
}


x86 assembly (clang, -O0):
<randombytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
lea    0xe90(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11e7 <randombytes+0x97>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11d6 <randombytes+0x86>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1180 <randombytes+0x30>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randombytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
lea    0xe93(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%r15d
test   %rbx,%rbx
je     11a0 <randombytes+0x50>
xor    %ebp,%ebp
nopl   0x0(%rax)
lea    (%r14,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1040 <read@plt>
test   %eax,%eax
js     11b2 <randombytes+0x62>
mov    %eax,%eax
add    %rax,%rbp
cmp    %rbx,%rbp
jb     1180 <randombytes+0x30>
mov    %r15d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <close@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [29 x i8] c"Could not read random bytes\0A\00"
@EXIT_FAILURE = global i32 0
define void @randombytes(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%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* %5
store i64 0, i64* %6
br label %10
10: ; preds = %30, %2
%11 = load i64, i64* %6
%12 = load i64, i64* %4
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %35
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %3
%17 = load i64, i64* %6
%18 = getelementptr inbounds i8, i8* %16, i64 %17
%19 = load i64, i64* %4
%20 = load i64, i64* %6
%21 = sub i64 %19, %20
%22 = call i32 @read(i32 %15, i8* %18, i64 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %14
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %14
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = load i64, i64* %6
%34 = add i64 %33, %32
store i64 %34, i64* %6
br label %10
35: ; preds = %10
%36 = load i32, i32* %5
%37 = call i32 @close(i32 %36)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [29 x i8] c"Could not read random bytes\0A\00"
@EXIT_FAILURE = global i32 0
define void @randombytes(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 eq i64 %1, 0
br i1 %5, label %21, label %8
6: ; preds = %8
%7 = icmp ult i64 %15, %1
br i1 %7, label %8, label %21
8: ; preds = %2, %6
%9 = phi i64 [ %15, %6 ], [ 0, %2 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = sub i64 %1, %9
%12 = tail call i32 @read(i32 %4, i8* %10, i64 %11)
%13 = icmp slt i32 %12, 0
%14 = zext i32 %12 to i64
%15 = add i64 %9, %14
br i1 %13, label %16, label %6
16: ; preds = %8
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %6, %2
%22 = tail call i32 @close(i32 %4)
ret void
}