#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*/ SEEK_END;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *readfile(const char *path) {
  FILE *fp;
  char *buffer;
  size_t size;
  if (!(fp = fopen(path, "rb"))) {
    fprintf(stderr, "error reading %s\n", path);
    exit(1);
  }
  fseek(fp, 0L, SEEK_END);
  size = ftell(fp);
  rewind(fp);
  buffer = malloc(size + 1);
  fread(buffer, size, 1, fp);
  fclose(fp);
  buffer[size] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11dd <readfile+0x4d>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe35(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe61(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1207 <readfile+0x77>
mov    %rax,%rbx
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <rewind@plt>
lea    0x1(%r14),%rdi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
movb   $0x0,(%r15,%r14,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"error reading %s\0A\00"
@SEEK_END = global i32 0
define i8* @readfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %14, label %9
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %2
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = load i32*, i32** %3
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i64 0, i32 %16)
%18 = load i32*, i32** %3
%19 = call i64 @ftell(i32* %18)
store i64 %19, i64* %5
%20 = load i32*, i32** %3
%21 = call i32 @rewind(i32* %20)
%22 = load i64, i64* %5
%23 = add i64 %22, 1
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %4
%25 = load i8*, i8** %4
%26 = load i64, i64* %5
%27 = load i32*, i32** %3
%28 = call i32 @fread(i8* %25, i64 %26, i32 1, i32* %27)
%29 = load i32*, i32** %3
%30 = call i32 @fclose(i32* %29)
%31 = load i8*, i8** %4
%32 = load i64, i64* %5
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 0, i8* %33
%34 = load i8*, i8** %4
ret i8* %34
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [18 x i8] c"error reading %s\0A\00"
@SEEK_END = global i32 0
define noalias i8* @readfile(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %1
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %2, i64 0, i32 %9)
%11 = tail call i64 @ftell(i32* %2)
%12 = tail call i32 @rewind(i32* %2)
%13 = add i64 %11, 1
%14 = tail call i8* @malloc(i64 %13)
%15 = tail call i32 @fread(i8* %14, i64 %11, i32 1, i32* %2)
%16 = tail call i32 @fclose(i32* %2)
%17 = getelementptr inbounds i8, i8* %14, i64 %11
store i8 0, i8* %17
ret i8* %14
}