#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 *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(unsigned long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *copy_file_contents(char *path) {
  FILE *fp = fopen(path, "r");
  if (!fp) {
    fclose(fp);
    fprintf(stderr, "Failed to open file at '%s'\n", path);
    exit(1);
  }
  fseek(fp, 0, SEEK_END);
  long size = ftell(fp);
  rewind(fp);
  char *buffer = malloc((unsigned long)size + 1);
  if (!buffer) {
    fclose(fp);
    fprintf(stderr, "Failed to allocate memory for reading '%s'\n", path);
    exit(1);
  }
  if (!fread(buffer, (size_t)size, 1, fp)) {
    fclose(fp);
    free(buffer);
    fprintf(stderr, "Failed to read file '%s'\n", path);
    exit(1);
  }
  fclose(fp);
  buffer[size] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<copy_file_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f7 <copy_file_contents+0x57>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe1a(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1269 <copy_file_contents+0xc9>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdc5(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x0,%eax
jne    12bd <copy_file_contents+0x11d>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd9d(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_file_contents>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
lea    0xe4e(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1228 <copy_file_contents+0x88>
mov    %rax,%r12
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r12,%rdi
call   1080 <ftell@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1060 <rewind@plt>
lea    0x1(%r15),%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     1241 <copy_file_contents+0xa1>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1050 <fclose@plt>
test   %ebp,%ebp
je     125b <copy_file_contents+0xbb>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1050 <fclose@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
jmp    1273 <copy_file_contents+0xd3>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    0xdc6(%rip),%rsi
jmp    1273 <copy_file_contents+0xd3>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
mov    %r14,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Failed to open file at '%s'\0A\00"
@SEEK_END = global i32 0
@.str.2 = [44 x i8] c"Failed to allocate memory for reading '%s'\0A\00"
@.str.3 = [26 x i8] c"Failed to read file '%s'\0A\00"
define i8* @copy_file_contents(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp ne i32* %8, null
br i1 %9, label %17, label %10
10: ; preds = %1
%11 = load i32*, i32** %3
%12 = call i32 @fclose(i32* %11)
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %2
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %1
%18 = load i32*, i32** %3
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %3
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %4
%23 = load i32*, i32** %3
%24 = call i32 @rewind(i32* %23)
%25 = load i64, i64* %4
%26 = add i64 %25, 1
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %5
%28 = load i8*, i8** %5
%29 = icmp ne i8* %28, null
br i1 %29, label %37, label %30
30: ; preds = %17
%31 = load i32*, i32** %3
%32 = call i32 @fclose(i32* %31)
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %2
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %17
%38 = load i8*, i8** %5
%39 = load i64, i64* %4
%40 = load i32*, i32** %3
%41 = call i32 @fread(i8* %38, i64 %39, i32 1, i32* %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %52, label %43
43: ; preds = %37
%44 = load i32*, i32** %3
%45 = call i32 @fclose(i32* %44)
%46 = load i8*, i8** %5
%47 = call i32 @free(i8* %46)
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %2
%50 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %37
%53 = load i32*, i32** %3
%54 = call i32 @fclose(i32* %53)
%55 = load i8*, i8** %5
%56 = load i64, i64* %4
%57 = getelementptr inbounds i8, i8* %55, i64 %56
store i8 0, i8* %57
%58 = load i8*, i8** %5
ret i8* %58
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"Failed to open file at '%s'\0A\00"
@SEEK_END = global i32 0
@.str.2 = [44 x i8] c"Failed to allocate memory for reading '%s'\0A\00"
@.str.3 = [26 x i8] c"Failed to read file '%s'\0A\00"
define noalias i8* @copy_file_contents(i8* %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 %4, label %9
4: ; preds = %1
%5 = tail call i32 @fclose(i32* null)
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %2, i32 0, i32 %10)
%12 = tail call i64 @ftell(i32* %2)
%13 = tail call i32 @rewind(i32* %2)
%14 = add i64 %12, 1
%15 = tail call i8* @malloc(i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %22
17: ; preds = %9
%18 = tail call i32 @fclose(i32* %2)
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %9
%23 = tail call i32 @fread(i8* %15, i64 %12, i32 1, i32* %2)
%24 = icmp eq i32 %23, 0
%25 = tail call i32 @fclose(i32* %2)
br i1 %24, label %26, label %31
26: ; preds = %22
%27 = tail call i32 @free(i8* %15)
%28 = load i32, i32* @stderr
%29 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %22
%32 = getelementptr inbounds i8, i8* %15, i64 %12
store i8 0, i8* %32
ret i8* %15
}