#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 /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int read(int, unsigned char *, int);
extern void randombytes(unsigned char *random, unsigned long long len) {
  int fd = open("/dev/random", O_RDONLY);
  unsigned int i;
  int res = 0;
  if (fd < 0) {
    exit(-1);
  }
  for (i = 0; i < len; ++i) {
    res = read(fd, random + i, 1);
    if (res < 0) {
      close(fd);
      exit(-2);
    }
  }
  close(fd);
  return;
}


x86 assembly (clang, -O0):
<randombytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    0xea0(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1183 <randombytes+0x43>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x10(%rbp),%rax
jae    11e0 <randombytes+0xa0>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%eax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11cd <randombytes+0x8d>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    $0xfffffffe,%edi
call   1060 <exit@plt>
jmp    11d2 <randombytes+0x92>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118a <randombytes+0x4a>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randombytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
lea    0xea3(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11ae <randombytes+0x6e>
mov    %eax,%ebp
test   %r15,%r15
je     118c <randombytes+0x4c>
xor    %ebx,%ebx
nop
lea    (%r14,%rbx,1),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
js     119d <randombytes+0x5d>
add    $0x1,%ebx
cmp    %r15,%rbx
jb     1170 <randombytes+0x30>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <close@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    $0xfffffffe,%edi
call   1060 <exit@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/random\00"
@O_RDONLY = 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 ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = icmp eq i64 %1, 0
br i1 %7, label %22, label %14
8: ; preds = %2
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %14
%11 = add nuw nsw i64 %15, 1
%12 = and i64 %11, 4294967295
%13 = icmp ult i64 %12, %1
br i1 %13, label %14, label %22
14: ; preds = %6, %10
%15 = phi i64 [ %12, %10 ], [ 0, %6 ]
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = tail call i32 @read(i32 %4, i8* %16, i32 1)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %10
19: ; preds = %14
%20 = tail call i32 @close(i32 %4)
%21 = tail call i32 @exit(i32 -2)
unreachable
22: ; preds = %10, %6
%23 = tail call i32 @close(i32 %4)
ret void
}