#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 */
char EOF;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ hndl_fatal_error(char *);
char *malloc(long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *read_file_into_mem(const char *infile) {
  FILE *fp;
  if ((fp = fopen(infile, "r"))) {
    fseek(fp, 0L, SEEK_END);
    long bytes = ftell(fp);
    rewind(fp);
    char *mem = malloc(bytes * sizeof(char) + 1);
    if (!mem)
      hndl_fatal_error("malloc");
    fread(mem, 1, bytes, fp);
    fclose(fp);
    *(mem + bytes) = EOF;
    return mem;
  } else
    hndl_fatal_error("fopen");
}


x86 assembly (clang, -O0):
<read_file_into_mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1242 <read_file_into_mem+0xc2>
mov    -0x18(%rbp),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x20(%rbp),%rdi
shl    $0x0,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1202 <read_file_into_mem+0x82>
lea    0xe05(%rip),%rdi
call   1080 <hndl_fatal_error@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%dl
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    124e <read_file_into_mem+0xce>
lea    0xdc0(%rip),%rdi
call   1080 <hndl_fatal_error@plt>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_into_mem>:
push   %r15
push   %r14
push   %rbx
lea    0xe74(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1209 <read_file_into_mem+0x89>
mov    %rax,%rbx
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %r14,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11df <read_file_into_mem+0x5f>
lea    0xe28(%rip),%rdi
call   1080 <hndl_fatal_error@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%al
mov    %al,(%r15,%r14,1)
jmp    1215 <read_file_into_mem+0x95>
lea    0xdf9(%rip),%rdi
call   1080 <hndl_fatal_error@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.1 = [7 x i8] c"malloc\00"
@EOF = global i8 0
@.str.2 = [6 x i8] c"fopen\00"
define i8* @read_file_into_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 ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %38
10: ; preds = %1
%11 = load i32*, i32** %4
%12 = load i32, i32* @SEEK_END
%13 = call i32 @fseek(i32* %11, i64 0, i32 %12)
%14 = load i32*, i32** %4
%15 = call i64 @ftell(i32* %14)
store i64 %15, i64* %5
%16 = load i32*, i32** %4
%17 = call i32 @rewind(i32* %16)
%18 = load i64, i64* %5
%19 = mul i64 %18, 1
%20 = add i64 %19, 1
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = icmp ne i8* %22, null
br i1 %23, label %26, label %24
24: ; preds = %10
%25 = call i32 @hndl_fatal_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %10
%27 = load i8*, i8** %6
%28 = load i64, i64* %5
%29 = load i32*, i32** %4
%30 = call i32 @fread(i8* %27, i32 1, i64 %28, i32* %29)
%31 = load i32*, i32** %4
%32 = call i32 @fclose(i32* %31)
%33 = load i8, i8* @EOF
%34 = load i8*, i8** %6
%35 = load i64, i64* %5
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8 %33, i8* %36
%37 = load i8*, i8** %6
store i8* %37, i8** %2
br label %40
38: ; preds = %1
%39 = call i32 @hndl_fatal_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %26, %38
%41 = load i8*, i8** %2
ret i8* %41
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.1 = [7 x i8] c"malloc\00"
@EOF = global i8 0
@.str.2 = [6 x i8] c"fopen\00"
define noalias i8* @read_file_into_mem(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %19, label %4
4: ; preds = %1
%5 = load i32, i32* @SEEK_END
%6 = tail call i32 @fseek(i32* %2, i64 0, i32 %5)
%7 = tail call i64 @ftell(i32* %2)
%8 = tail call i32 @rewind(i32* %2)
%9 = add i64 %7, 1
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = tail call i32 @hndl_fatal_error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %4
%15 = tail call i32 @fread(i8* %10, i32 1, i64 %7, i32* %2)
%16 = tail call i32 @fclose(i32* %2)
%17 = load i8, i8* @EOF
%18 = getelementptr inbounds i8, i8* %10, i64 %7
store i8 %17, i8* %18
br label %21
19: ; preds = %1
%20 = tail call i32 @hndl_fatal_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %14
%22 = phi i8* [ %10, %14 ], [ undef, %19 ]
ret i8* %22
}