#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(unsigned int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, unsigned int);
char *realloc(char *, unsigned int);
int /*<<< orphan*/ strncpy(char *, char *, unsigned int);
char *get_file_contents(const char *file) {
  FILE *fh;
  char *buff = NULL, *tmp = NULL;
  char *loc = NULL;
  int rlen = 0;
  unsigned int pages = 1;
  unsigned int fs = 0;
  fh = fopen(file, "r");
  if (!fh)
    return NULL;
  buff = malloc(pages * 4096 + 1);
  if (buff == NULL) {
    fclose(fh);
    return NULL;
  }
  loc = buff;
  while ((rlen = fread(loc, 1, 4096, fh))) {
    fs += rlen;
    if (rlen == 4096) {
      pages++;
      tmp = realloc(buff, pages * 4096 + 1);
      if (tmp == NULL) {
        free(buff);
        fclose(fh);
        return NULL;
      }
      buff = tmp;
      loc = buff + ((pages - 1) * 4096);
    } else
      break;
  }
  fclose(fh);
  tmp = malloc(fs + 4);
  if (tmp) {
    memset(tmp, 0, fs + 4);
    strncpy(tmp, buff, fs);
    free(buff);
    buff = tmp;
  } else
    buff[fs + 1] = 0;
  return buff;
}


x86 assembly (clang, -O0):
<get_file_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x1,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e5 <get_file_contents+0x65>
movq   $0x0,-0x8(%rbp)
jmp    1341 <get_file_contents+0x1c1>
mov    -0x38(%rbp),%edi
shl    $0xc,%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1218 <get_file_contents+0x98>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1341 <get_file_contents+0x1c1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1050 <fread@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
je     12cf <get_file_contents+0x14f>
mov    -0x34(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x1000,-0x34(%rbp)
jne    12c5 <get_file_contents+0x145>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%esi
shl    $0xc,%esi
add    $0x1,%esi
call   1090 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12a2 <get_file_contents+0x122>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    1341 <get_file_contents+0x1c1>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x38(%rbp),%ecx
sub    $0x1,%ecx
shl    $0xc,%ecx
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    12ca <get_file_contents+0x14a>
jmp    12cf <get_file_contents+0x14f>
jmp    1220 <get_file_contents+0xa0>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x3c(%rbp),%edi
add    $0x4,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1329 <get_file_contents+0x1a9>
mov    -0x28(%rbp),%rdi
mov    -0x3c(%rbp),%edx
add    $0x4,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1339 <get_file_contents+0x1b9>
mov    -0x20(%rbp),%rax
mov    -0x3c(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_contents>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe6e(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1287 <get_file_contents+0x107>
mov    %rax,%r14
mov    $0x1001,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     127f <get_file_contents+0xff>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
xor    %r12d,%r12d
test   %eax,%eax
je     1229 <get_file_contents+0xa9>
mov    $0x2001,%ebx
mov    %r15,%r13
nopl   (%rax)
add    %eax,%r12d
cmp    $0x1000,%eax
jne    1226 <get_file_contents+0xa6>
mov    %r13,%rdi
mov    %ebx,%esi
call   1090 <realloc@plt>
test   %rax,%rax
je     1277 <get_file_contents+0xf7>
mov    %rax,%r15
lea    -0x1001(%rbx),%edi
add    %rax,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
add    $0x1000,%ebx
mov    %r15,%r13
test   %eax,%eax
jne    11e0 <get_file_contents+0x60>
jmp    1229 <get_file_contents+0xa9>
mov    %r13,%r15
mov    %r14,%rdi
call   1060 <fclose@plt>
lea    0x4(%r12),%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     126c <get_file_contents+0xec>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1070 <memset@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1040 <strncpy@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%r15
jmp    128a <get_file_contents+0x10a>
add    $0x1,%r12d
movb   $0x0,(%r15,%r12,1)
jmp    128a <get_file_contents+0x10a>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i8* @get_file_contents(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store i8* null, i8** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i32 0, i32* %8
store i32 1, i32* %9
store i32 0, i32* %10
%11 = load i8*, i8** %3
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %4
%13 = load i32*, i32** %4
%14 = icmp ne i32* %13, null
br i1 %14, label %16, label %15
15: ; preds = %1
store i8* null, i8** %2
br label %92
16: ; preds = %1
%17 = load i32, i32* %9
%18 = mul i32 %17, 4096
%19 = add i32 %18, 1
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %5
%21 = load i8*, i8** %5
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32*, i32** %4
%25 = call i32 @fclose(i32* %24)
store i8* null, i8** %2
br label %92
26: ; preds = %16
%27 = load i8*, i8** %5
store i8* %27, i8** %7
br label %28
28: ; preds = %63, %26
%29 = load i8*, i8** %7
%30 = load i32*, i32** %4
%31 = call i32 @fread(i8* %29, i32 1, i32 4096, i32* %30)
store i32 %31, i32* %8
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %64
33: ; preds = %28
%34 = load i32, i32* %8
%35 = load i32, i32* %10
%36 = add i32 %35, %34
store i32 %36, i32* %10
%37 = load i32, i32* %8
%38 = icmp eq i32 %37, 4096
br i1 %38, label %39, label %62
39: ; preds = %33
%40 = load i32, i32* %9
%41 = add i32 %40, 1
store i32 %41, i32* %9
%42 = load i8*, i8** %5
%43 = load i32, i32* %9
%44 = mul i32 %43, 4096
%45 = add i32 %44, 1
%46 = call i8* @realloc(i8* %42, i32 %45)
store i8* %46, i8** %6
%47 = load i8*, i8** %6
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %54
49: ; preds = %39
%50 = load i8*, i8** %5
%51 = call i32 @free(i8* %50)
%52 = load i32*, i32** %4
%53 = call i32 @fclose(i32* %52)
store i8* null, i8** %2
br label %92
54: ; preds = %39
%55 = load i8*, i8** %6
store i8* %55, i8** %5
%56 = load i8*, i8** %5
%57 = load i32, i32* %9
%58 = sub i32 %57, 1
%59 = mul i32 %58, 4096
%60 = zext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %56, i64 %60
store i8* %61, i8** %7
br label %63
62: ; preds = %33
br label %64
63: ; preds = %54
br label %28
64: ; preds = %62, %28
%65 = load i32*, i32** %4
%66 = call i32 @fclose(i32* %65)
%67 = load i32, i32* %10
%68 = add i32 %67, 4
%69 = call i8* @malloc(i32 %68)
store i8* %69, i8** %6
%70 = load i8*, i8** %6
%71 = icmp ne i8* %70, null
br i1 %71, label %72, label %84
72: ; preds = %64
%73 = load i8*, i8** %6
%74 = load i32, i32* %10
%75 = add i32 %74, 4
%76 = call i32 @memset(i8* %73, i32 0, i32 %75)
%77 = load i8*, i8** %6
%78 = load i8*, i8** %5
%79 = load i32, i32* %10
%80 = call i32 @strncpy(i8* %77, i8* %78, i32 %79)
%81 = load i8*, i8** %5
%82 = call i32 @free(i8* %81)
%83 = load i8*, i8** %6
store i8* %83, i8** %5
br label %90
84: ; preds = %64
%85 = load i8*, i8** %5
%86 = load i32, i32* %10
%87 = add i32 %86, 1
%88 = zext i32 %87 to i64
%89 = getelementptr inbounds i8, i8* %85, i64 %88
store i8 0, i8* %89
br label %90
90: ; preds = %84, %72
%91 = load i8*, i8** %5
store i8* %91, i8** %2
br label %92
92: ; preds = %90, %49, %23, %15
%93 = load i8*, i8** %2
ret i8* %93
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i8* @get_file_contents(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 %49, label %4
4: ; preds = %1
%5 = tail call dereferenceable_or_null(4097) i8* @malloc(i32 4097)
%6 = icmp eq i8* %5, null
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = tail call i32 @fread(i8* %5, i32 1, i32 4096, i32* %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %34, label %12
10: ; preds = %4
%11 = tail call i32 @fclose(i32* %2)
br label %49
12: ; preds = %7, %28
%13 = phi i32 [ %32, %28 ], [ %8, %7 ]
%14 = phi i32 [ %17, %28 ], [ 0, %7 ]
%15 = phi i32 [ %20, %28 ], [ 1, %7 ]
%16 = phi i8* [ %23, %28 ], [ %5, %7 ]
%17 = add i32 %13, %14
%18 = icmp eq i32 %13, 4096
br i1 %18, label %19, label %34
19: ; preds = %12
%20 = add i32 %15, 1
%21 = shl i32 %20, 12
%22 = or i32 %21, 1
%23 = tail call i8* @realloc(i8* %16, i32 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = tail call i32 @free(i8* %16)
%27 = tail call i32 @fclose(i32* %2)
br label %49
28: ; preds = %19
%29 = shl i32 %15, 12
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %23, i64 %30
%32 = tail call i32 @fread(i8* %31, i32 1, i32 4096, i32* %2)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %12
34: ; preds = %28, %12, %7
%35 = phi i8* [ %5, %7 ], [ %16, %12 ], [ %23, %28 ]
%36 = phi i32 [ 0, %7 ], [ %17, %12 ], [ %17, %28 ]
%37 = tail call i32 @fclose(i32* %2)
%38 = add i32 %36, 4
%39 = tail call i8* @malloc(i32 %38)
%40 = icmp eq i8* %39, null
br i1 %40, label %45, label %41
41: ; preds = %34
%42 = tail call i32 @memset(i8* %39, i32 0, i32 %38)
%43 = tail call i32 @strncpy(i8* %39, i8* %35, i32 %36)
%44 = tail call i32 @free(i8* %35)
br label %49
45: ; preds = %34
%46 = add i32 %36, 1
%47 = zext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %35, i64 %47
store i8 0, i8* %48
br label %49
49: ; preds = %41, %45, %1, %25, %10
%50 = phi i8* [ null, %10 ], [ null, %25 ], [ null, %1 ], [ %39, %41 ], [ %35, %45 ]
ret i8* %50
}