#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(void *, int, int, int /*<<< orphan*/ *);
void *malloc(int);
int /*<<< orphan*/ printf(char *);
void *random_bytes(int length) {
  void *out = malloc(length);
  FILE *file;
  file = fopen("/dev/urandom", "r");
  if (file == NULL) {
    printf("Error opening file. Program terminating\n");
    exit(1);
  }
  fread(out, length, 1, file);
  fclose(file);
  return out;
}


x86 assembly (clang, -O0):
<random_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
lea    0xe82(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11af <random_bytes+0x4f>
lea    0xe6f(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_bytes>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
call   1060 <malloc@plt>
mov    %rax,%r14
lea    0xe8b(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ab <random_bytes+0x4b>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [41 x i8] c"Error opening file. Program terminating\0A\00"
define i8* @random_bytes(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i8* @malloc(i32 %5)
store i8* %6, i8** %3
%7 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = load i32, i32* %2
%16 = load i32*, i32** %4
%17 = call i32 @fread(i8* %14, i32 %15, i32 1, i32* %16)
%18 = load i32*, i32** %4
%19 = call i32 @fclose(i32* %18)
%20 = load i8*, i8** %3
ret i8* %20
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
@str = [40 x i8] c"Error opening file. Program terminating\00"
define noalias i8* @random_bytes(i32 %0) {
%2 = tail call i8* @malloc(i32 %0)
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %1
%9 = tail call i32 @fread(i8* %2, i32 %0, i32 1, i32* %3)
%10 = tail call i32 @fclose(i32* %3)
ret i8* %2
}