#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 size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
char *calloc(size_t, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int open(char const *, int /*<<< orphan*/);
size_t read(int, char *, size_t);
char *realloc(char *, size_t);
char *get_file_contents(const char *path) {
  const size_t g = 512;
  ssize_t s = g, p = 0, r;
  int fd;
  char *c;
  if ((fd = open(path, O_RDONLY)) < 0)
    return NULL;
  c = calloc(s, sizeof(char));
  while ((r = read(fd, &c[p], s - p)) > -1) {
    p += r;
    if (p >= s) {
      s += g;
      c = realloc(c, s * sizeof(char));
    }
    if (r == 0) {
      c[p] = '\0';
      close(fd);
      return c;
    }
  }
  free(c);
  close(fd);
  return NULL;
}


x86 assembly (clang, -O0):
<get_file_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x200,-0x18(%rbp)
movq   $0x200,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    11af <get_file_contents+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    126e <get_file_contents+0x10e>
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x40(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1050 <read@plt>
mov    %rax,-0x30(%rbp)
cmp    $0xffffffffffffffff,%rax
jbe    1255 <get_file_contents+0xf5>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     1224 <get_file_contents+0xc4>
mov    -0x20(%rbp),%rax
add    $0x200,%rax
mov    %rax,-0x20(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
shl    $0x0,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1250 <get_file_contents+0xf0>
mov    -0x40(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    126e <get_file_contents+0x10e>
jmp    11c1 <get_file_contents+0x61>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_contents>:
push   %r14
push   %rbx
push   %rax
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     1198 <get_file_contents+0x48>
mov    %eax,%ebx
mov    $0x200,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r14
mov    $0x200,%edx
mov    %ebx,%edi
mov    %rax,%rsi
call   1050 <read@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i8* @get_file_contents(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %3
store i64 512, i64* %4
store i64 512, i64* %5
store i64 0, i64* %6
%10 = load i8*, i8** %3
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i8* null, i8** %2
br label %58
15: ; preds = %1
%16 = load i64, i64* %5
%17 = call i8* @calloc(i64 %16, i32 1)
store i8* %17, i8** %9
br label %18
18: ; preds = %52, %15
%19 = load i32, i32* %8
%20 = load i8*, i8** %9
%21 = load i64, i64* %6
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i64, i64* %5
%24 = load i64, i64* %6
%25 = sub i64 %23, %24
%26 = call i64 @read(i32 %19, i8* %22, i64 %25)
store i64 %26, i64* %7
%27 = icmp ugt i64 %26, -1
br i1 %27, label %28, label %53
28: ; preds = %18
%29 = load i64, i64* %7
%30 = load i64, i64* %6
%31 = add i64 %30, %29
store i64 %31, i64* %6
%32 = load i64, i64* %6
%33 = load i64, i64* %5
%34 = icmp uge i64 %32, %33
br i1 %34, label %35, label %42
35: ; preds = %28
%36 = load i64, i64* %5
%37 = add i64 %36, 512
store i64 %37, i64* %5
%38 = load i8*, i8** %9
%39 = load i64, i64* %5
%40 = mul i64 %39, 1
%41 = call i8* @realloc(i8* %38, i64 %40)
store i8* %41, i8** %9
br label %42
42: ; preds = %35, %28
%43 = load i64, i64* %7
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %52
45: ; preds = %42
%46 = load i8*, i8** %9
%47 = load i64, i64* %6
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8 0, i8* %48
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
%51 = load i8*, i8** %9
store i8* %51, i8** %2
br label %58
52: ; preds = %42
br label %18
53: ; preds = %18
%54 = load i8*, i8** %9
%55 = call i32 @free(i8* %54)
%56 = load i32, i32* %8
%57 = call i32 @close(i32 %56)
store i8* null, i8** %2
br label %58
58: ; preds = %53, %45, %14
%59 = load i8*, i8** %2
ret i8* %59
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define noalias i8* @get_file_contents(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %1
%6 = tail call i8* @calloc(i64 512, i32 1)
%7 = tail call i64 @read(i32 %3, i8* %6, i64 512)
%8 = tail call i32 @free(i8* %6)
%9 = tail call i32 @close(i32 %3)
br label %10
10: ; preds = %1, %5
ret i8* null
}