#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*/ assert(char *);
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
char *realloc(char *, size_t);
char *get_file_contents(const char *path, size_t *len) {
  FILE *f = NULL;
  int did_save_errno = 0;
  char *ret = NULL;
  int saved_errno = 0;
  char *buf = NULL;
  size_t bytes_read;
  size_t buf_size;
  size_t buf_used;
  f = fopen(path, "r");
  if (!f) {
    did_save_errno = 1;
    saved_errno = errno;
    goto out;
  }
  buf_size = 1024;
  buf_used = 0;
  buf = malloc(buf_size);
  assert(buf);
  while ((bytes_read = fread(buf + buf_used, 1, buf_size - buf_used, f)) > 0) {
    buf_used += bytes_read;
    if (buf_size == buf_used) {
      buf_size *= 2;
      buf = realloc(buf, buf_size);
      assert(buf);
    }
  }
  if (bytes_read < 0) {
    did_save_errno = 1;
    saved_errno = errno;
    goto out;
  }
  ret = buf;
  ret[buf_used] = '\0';
  buf = NULL;
  *len = buf_used;
out:
  if (f)
    fclose(f);
  free(buf);
  if (did_save_errno)
    errno = saved_errno;
  return ret;
}


x86 assembly (clang, -O0):
<get_file_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11dd <get_file_contents+0x6d>
movl   $0x1,-0x1c(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    12be <get_file_contents+0x14e>
movq   $0x400,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x48(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <assert@plt>
mov    -0x38(%rbp),%rdi
add    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rdx
sub    -0x50(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jbe    1274 <get_file_contents+0x104>
mov    -0x40(%rbp),%rax
add    -0x50(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x50(%rbp),%rax
jne    126f <get_file_contents+0xff>
mov    -0x48(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <assert@plt>
jmp    1203 <get_file_contents+0x93>
cmpq   $0x0,-0x40(%rbp)
jae    1297 <get_file_contents+0x127>
movl   $0x1,-0x1c(%rbp)
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    12be <get_file_contents+0x14e>
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x50(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movq   $0x0,-0x38(%rbp)
mov    -0x50(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x18(%rbp)
je     12d2 <get_file_contents+0x162>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x1c(%rbp)
je     12f1 <get_file_contents+0x181>
mov    -0x2c(%rbp),%ecx
mov    0x2cf1(%rip),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x50,%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
mov    %rsi,%r14
lea    0xe7b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1228 <get_file_contents+0xb8>
mov    %rax,%r15
mov    $0x400,%r13d
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1060 <assert@plt>
mov    $0x400,%edx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   1040 <fread@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    11ff <get_file_contents+0x8f>
mov    %r12,%rbx
movb   $0x0,(%rbx)
mov    %rbp,(%r14)
mov    %r15,%rdi
call   1050 <fclose@plt>
jmp    122b <get_file_contents+0xbb>
lea    (%r12,%rbp,1),%rbx
mov    %r13,%rdx
sub    %rbp,%rdx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     11d0 <get_file_contents+0x60>
mov    %rbp,%rcx
mov    %rax,%rbp
add    %rcx,%rbp
cmp    %rbp,%r13
jne    11e0 <get_file_contents+0x70>
add    %r13,%r13
mov    %r12,%rdi
mov    %r13,%rsi
call   1080 <realloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1060 <assert@plt>
jmp    11e0 <get_file_contents+0x70>
xor    %r12d,%r12d
xor    %edi,%edi
call   1030 <free@plt>
mov    %r12,%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"
@errno = global i32 0
define i8* @get_file_contents(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i8* %0, i8** %3
store i64* %1, i64** %4
store i32* null, i32** %5
store i32 0, i32* %6
store i8* null, i8** %7
store i32 0, i32* %8
store i8* null, i8** %9
%13 = load i8*, i8** %3
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %5
%15 = load i32*, i32** %5
%16 = icmp ne i32* %15, null
br i1 %16, label %19, label %17
17: ; preds = %2
store i32 1, i32* %6
%18 = load i32, i32* @errno
store i32 %18, i32* %8
br label %62
19: ; preds = %2
store i64 1024, i64* %11
store i64 0, i64* %12
%20 = load i64, i64* %11
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %9
%22 = load i8*, i8** %9
%23 = call i32 @assert(i8* %22)
br label %24
24: ; preds = %49, %19
%25 = load i8*, i8** %9
%26 = load i64, i64* %12
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i64, i64* %11
%29 = load i64, i64* %12
%30 = sub i64 %28, %29
%31 = load i32*, i32** %5
%32 = call i64 @fread(i8* %27, i32 1, i64 %30, i32* %31)
store i64 %32, i64* %10
%33 = icmp ugt i64 %32, 0
br i1 %33, label %34, label %50
34: ; preds = %24
%35 = load i64, i64* %10
%36 = load i64, i64* %12
%37 = add i64 %36, %35
store i64 %37, i64* %12
%38 = load i64, i64* %11
%39 = load i64, i64* %12
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %49
41: ; preds = %34
%42 = load i64, i64* %11
%43 = mul i64 %42, 2
store i64 %43, i64* %11
%44 = load i8*, i8** %9
%45 = load i64, i64* %11
%46 = call i8* @realloc(i8* %44, i64 %45)
store i8* %46, i8** %9
%47 = load i8*, i8** %9
%48 = call i32 @assert(i8* %47)
br label %49
49: ; preds = %41, %34
br label %24
50: ; preds = %24
%51 = load i64, i64* %10
%52 = icmp ult i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %50
store i32 1, i32* %6
%54 = load i32, i32* @errno
store i32 %54, i32* %8
br label %62
55: ; preds = %50
%56 = load i8*, i8** %9
store i8* %56, i8** %7
%57 = load i8*, i8** %7
%58 = load i64, i64* %12
%59 = getelementptr inbounds i8, i8* %57, i64 %58
store i8 0, i8* %59
store i8* null, i8** %9
%60 = load i64, i64* %12
%61 = load i64*, i64** %4
store i64 %60, i64* %61
br label %62
62: ; preds = %55, %53, %17
%63 = load i32*, i32** %5
%64 = icmp ne i32* %63, null
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = load i32*, i32** %5
%67 = call i32 @fclose(i32* %66)
br label %68
68: ; preds = %65, %62
%69 = load i8*, i8** %9
%70 = call i32 @free(i8* %69)
%71 = load i32, i32* %6
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %68
%74 = load i32, i32* %8
store i32 %74, i32* @errno
br label %75
75: ; preds = %73, %68
%76 = load i8*, i8** %7
ret i8* %76
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
define i8* @get_file_contents(i8* nocapture %0, i64* nocapture writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %33, label %5
5: ; preds = %2
%6 = tail call dereferenceable_or_null(1024) i8* @malloc(i64 1024)
%7 = tail call i32 @assert(i8* %6)
%8 = tail call i64 @fread(i8* %6, i32 1, i64 1024, i32* %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %28, label %10
10: ; preds = %5, %21
%11 = phi i64 [ %26, %21 ], [ %8, %5 ]
%12 = phi i64 [ %15, %21 ], [ 0, %5 ]
%13 = phi i64 [ %23, %21 ], [ 1024, %5 ]
%14 = phi i8* [ %22, %21 ], [ %6, %5 ]
%15 = add i64 %11, %12
%16 = icmp eq i64 %13, %15
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = shl i64 %13, 1
%19 = tail call i8* @realloc(i8* %14, i64 %18)
%20 = tail call i32 @assert(i8* %19)
br label %21
21: ; preds = %17, %10
%22 = phi i8* [ %19, %17 ], [ %14, %10 ]
%23 = phi i64 [ %18, %17 ], [ %13, %10 ]
%24 = getelementptr inbounds i8, i8* %22, i64 %15
%25 = sub i64 %23, %15
%26 = tail call i64 @fread(i8* %24, i32 1, i64 %25, i32* %3)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %10
28: ; preds = %21, %5
%29 = phi i8* [ %6, %5 ], [ %22, %21 ]
%30 = phi i64 [ 0, %5 ], [ %15, %21 ]
%31 = phi i8* [ %6, %5 ], [ %24, %21 ]
store i8 0, i8* %31
store i64 %30, i64* %1
%32 = tail call i32 @fclose(i32* %3)
br label %33
33: ; preds = %2, %28
%34 = phi i8* [ %29, %28 ], [ null, %2 ]
%35 = tail call i32 @free(i8* null)
ret i8* %34
}