#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 /*<<< orphan*/ uint8_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
scalar_t__ read(int, int /*<<< orphan*/ *, size_t);
void get_random(uint8_t *data, size_t num_bytes) {
  int gathered_bytes = open("/dev/urandom", O_RDONLY);
  size_t len = 0;
  while (len < num_bytes) {
    ssize_t num_gathered_bytes =
        read(gathered_bytes, data + len, num_bytes - len);
    if (num_gathered_bytes < 0) {
      puts("Error, unable to read stream");
      close(gathered_bytes);
      exit(EXIT_FAILURE);
    } else {
      len += num_gathered_bytes;
    }
  }
  close(gathered_bytes);
}


x86 assembly (clang, -O0):
<get_random>:
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    11ef <get_random+0x9f>
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x10(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1050 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11de <get_random+0x8e>
lea    0xe4a(%rip),%rdi
call   1030 <puts@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1180 <get_random+0x30>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_random>:
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     119f <get_random+0x4f>
xor    %ebp,%ebp
nopl   0x0(%rax)
lea    (%r14,%rbp,4),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15d,%edi
call   1050 <read@plt>
test   %rax,%rax
js     11b1 <get_random+0x61>
add    %rax,%rbp
cmp    %rbx,%rbp
jb     1180 <get_random+0x30>
mov    %r15d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>
lea    0xe55(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
call   1040 <close@plt>
mov    0x2e0c(%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
@.str.1 = [29 x i8] c"Error, unable to read stream\00"
@EXIT_FAILURE = global i32 0
define void @get_random(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store i32* %0, i32** %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 = %35, %2
%11 = load i64, i64* %6
%12 = load i64, i64* %4
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %36
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i32*, i32** %3
%17 = load i64, i64* %6
%18 = getelementptr inbounds i32, i32* %16, i64 %17
%19 = load i64, i64* %4
%20 = load i64, i64* %6
%21 = sub i64 %19, %20
%22 = call i64 @read(i32 %15, i32* %18, i64 %21)
store i64 %22, i64* %7
%23 = load i64, i64* %7
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %14
%26 = call i32 @puts(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %14
%32 = load i64, i64* %7
%33 = load i64, i64* %6
%34 = add i64 %33, %32
store i64 %34, i64* %6
br label %35
35: ; preds = %31
br label %10
36: ; preds = %10
%37 = load i32, i32* %5
%38 = call i32 @close(i32 %37)
ret void
}

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