#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *file_to_mem(char *name) {
  FILE *infile;
  size_t numbytes;
  char *buffer;
  infile = fopen(name, "rb");
  if (infile == NULL) {
    fprintf(stderr, "Error opening file '%s'\n", name);
    return NULL;
  }
  fseek(infile, 0L, SEEK_END);
  numbytes = ftell(infile);
  fseek(infile, 0L, SEEK_SET);
  buffer = (char *)malloc((numbytes + 1) * sizeof(char));
  if (buffer == NULL) {
    perror("Error reading file");
    return NULL;
  }
  if (fread(buffer, sizeof(char), numbytes, infile) != numbytes) {
    perror("Error reading file");
    return NULL;
  }
  fclose(infile);
  *(buffer + numbytes) = '\0';
  return (buffer);
}


x86 assembly (clang, -O0):
<file_to_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d1 <file_to_mem+0x51>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe44(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1299 <file_to_mem+0x119>
mov    -0x18(%rbp),%rdi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1243 <file_to_mem+0xc3>
lea    0xdeb(%rip),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1299 <file_to_mem+0x119>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x20(%rbp),%rax
je     127c <file_to_mem+0xfc>
lea    0xdb2(%rip),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1299 <file_to_mem+0x119>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_to_mem>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1205 <file_to_mem+0x85>
mov    %rax,%rbx
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r15
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
lea    0x1(%r15),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1229 <file_to_mem+0xa9>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %r15,%rax
jne    1229 <file_to_mem+0xa9>
mov    %rbx,%rdi
call   1040 <fclose@plt>
movb   $0x0,(%r14,%r15,1)
jmp    1220 <file_to_mem+0xa0>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdec(%rip),%rdi
call   10a0 <perror@plt>
jmp    121d <file_to_mem+0x9d>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Error opening file '%s'\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [19 x i8] c"Error reading file\00"
define i8* @file_to_mem(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %3
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %13)
store i8* null, i8** %2
br label %49
15: ; preds = %1
%16 = load i32*, i32** %4
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i64 0, i32 %17)
%19 = load i32*, i32** %4
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %5
%21 = load i32*, i32** %4
%22 = load i32, i32* @SEEK_SET
%23 = call i32 @fseek(i32* %21, i64 0, i32 %22)
%24 = load i64, i64* %5
%25 = add i64 %24, 1
%26 = mul i64 %25, 1
%27 = call i64 @malloc(i64 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %15
%32 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %49
33: ; preds = %15
%34 = load i8*, i8** %6
%35 = load i64, i64* %5
%36 = load i32*, i32** %4
%37 = call i64 @fread(i8* %34, i32 1, i64 %35, i32* %36)
%38 = load i64, i64* %5
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %33
%41 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %49
42: ; preds = %33
%43 = load i32*, i32** %4
%44 = call i32 @fclose(i32* %43)
%45 = load i8*, i8** %6
%46 = load i64, i64* %5
%47 = getelementptr inbounds i8, i8* %45, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** %6
store i8* %48, i8** %2
br label %49
49: ; preds = %42, %40, %31, %11
%50 = load i8*, i8** %2
ret i8* %50
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Error opening file '%s'\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [19 x i8] c"Error reading file\00"
define i8* @file_to_mem(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 %7
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %27
7: ; preds = %1
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %2, i64 0, i32 %8)
%10 = tail call i64 @ftell(i32* %2)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %2, i64 0, i32 %11)
%13 = add i64 %10, 1
%14 = tail call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %19
17: ; preds = %7
%18 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %27
19: ; preds = %7
%20 = tail call i64 @fread(i8* %15, i32 1, i64 %10, i32* %2)
%21 = icmp eq i64 %20, %10
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %27
24: ; preds = %19
%25 = tail call i32 @fclose(i32* %2)
%26 = getelementptr inbounds i8, i8* %15, i64 %10
store i8 0, i8* %26
br label %27
27: ; preds = %24, %22, %17, %4
%28 = phi i8* [ null, %4 ], [ null, %17 ], [ null, %22 ], [ %15, %24 ]
ret i8* %28
}