#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
size_t file_get_size(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
unsigned char *malloc(size_t);
unsigned char *file_get_contents(size_t *len, const char *path) {
  FILE *file;
  size_t i;
  int c;
  unsigned char *data;
  i = 0, *len = 0;
  data = NULL;
  file = fopen(path, "r");
  if (file) {
    *len = file_get_size(file);
    if (*len > 0)
      data = malloc(*len + 1);
    if (data) {
      for (; i < *len && (c = fgetc(file)) != EOF; data[i++] = c)
        ;
      data[*len] = '\0';
    }
    fclose(file);
  }
  return data;
}


x86 assembly (clang, -O0):
<file_get_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     125d <file_get_contents+0x10d>
mov    -0x18(%rbp),%rdi
call   1030 <file_get_size@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jbe    11cf <file_get_contents+0x7f>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1254 <file_get_contents+0x104>
jmp    11df <file_get_contents+0x8f>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
mov    %al,-0x31(%rbp)
jae    1210 <file_get_contents+0xc0>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    0x2de0(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1220 <file_get_contents+0xd0>
jmp    1245 <file_get_contents+0xf5>
jmp    1225 <file_get_contents+0xd5>
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    11df <file_get_contents+0x8f>
mov    -0x30(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_get_contents>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%rax
mov    %rdi,%r15
movq   $0x0,(%rdi)
lea    0xe93(%rip),%rsi
mov    %rax,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d0 <file_get_contents+0x80>
mov    %rax,%r14
mov    %rax,%rdi
call   1030 <file_get_size@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     11d5 <file_get_contents+0x85>
add    $0x1,%rax
mov    %rax,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11d5 <file_get_contents+0x85>
mov    %rax,%r12
xor    %ebx,%ebx
mov    0x2e3e(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1050 <fgetc@plt>
cmp    0x0(%r13),%eax
je     11da <file_get_contents+0x8a>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
mov    (%r15),%rax
cmp    %rax,%rbx
jb     11b0 <file_get_contents+0x60>
jmp    11dd <file_get_contents+0x8d>
xor    %r12d,%r12d
jmp    11ea <file_get_contents+0x9a>
xor    %r12d,%r12d
jmp    11e2 <file_get_contents+0x92>
mov    (%r15),%rax
movb   $0x0,(%r12,%rax,1)
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
define i8* @file_get_contents(i64* %0, i8* %1) {
%3 = alloca i64*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
store i64* %0, i64** %3
store i8* %1, i8** %4
store i64 0, i64* %6
%9 = load i64*, i64** %3
store i64 0, i64* %9
store i8* null, i8** %8
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %58
14: ; preds = %2
%15 = load i32*, i32** %5
%16 = call i64 @file_get_size(i32* %15)
%17 = load i64*, i64** %3
store i64 %16, i64* %17
%18 = load i64*, i64** %3
%19 = load i64, i64* %18
%20 = icmp ugt i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = load i64*, i64** %3
%23 = load i64, i64* %22
%24 = add i64 %23, 1
%25 = call i8* @malloc(i64 %24)
store i8* %25, i8** %8
br label %26
26: ; preds = %21, %14
%27 = load i8*, i8** %8
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %55
29: ; preds = %26
br label %30
30: ; preds = %43, %29
%31 = load i64, i64* %6
%32 = load i64*, i64** %3
%33 = load i64, i64* %32
%34 = icmp ult i64 %31, %33
br i1 %34, label %35, label %40
35: ; preds = %30
%36 = load i32*, i32** %5
%37 = call i32 @fgetc(i32* %36)
store i32 %37, i32* %7
%38 = load i32, i32* @EOF
%39 = icmp ne i32 %37, %38
br label %40
40: ; preds = %35, %30
%41 = phi i1 [ false, %30 ], [ %39, %35 ]
br i1 %41, label %42, label %50
42: ; preds = %40
br label %43
43: ; preds = %42
%44 = load i32, i32* %7
%45 = trunc i32 %44 to i8
%46 = load i8*, i8** %8
%47 = load i64, i64* %6
%48 = add i64 %47, 1
store i64 %48, i64* %6
%49 = getelementptr inbounds i8, i8* %46, i64 %47
store i8 %45, i8* %49
br label %30
50: ; preds = %40
%51 = load i8*, i8** %8
%52 = load i64*, i64** %3
%53 = load i64, i64* %52
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
br label %55
55: ; preds = %50, %26
%56 = load i32*, i32** %5
%57 = call i32 @fclose(i32* %56)
br label %58
58: ; preds = %55, %2
%59 = load i8*, i8** %8
ret i8* %59
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
define i8* @file_get_contents(i64* nocapture %0, i8* nocapture %1) {
store i64 0, i64* %0
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %31, label %5
5: ; preds = %2
%6 = tail call i64 @file_get_size(i32* %3)
store i64 %6, i64* %0
%7 = icmp eq i64 %6, 0
br i1 %7, label %28, label %8
8: ; preds = %5
%9 = add i64 %6, 1
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %28, label %12
12: ; preds = %8, %19
%13 = phi i64 [ %21, %19 ], [ 0, %8 ]
%14 = tail call i32 @fgetc(i32* %3)
%15 = load i32, i32* @EOF
%16 = icmp eq i32 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = load i64, i64* %0
br label %25
19: ; preds = %12
%20 = trunc i32 %14 to i8
%21 = add nuw i64 %13, 1
%22 = getelementptr inbounds i8, i8* %10, i64 %13
store i8 %20, i8* %22
%23 = load i64, i64* %0
%24 = icmp ult i64 %21, %23
br i1 %24, label %12, label %25
25: ; preds = %19, %17
%26 = phi i64 [ %18, %17 ], [ %23, %19 ]
%27 = getelementptr inbounds i8, i8* %10, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %5, %25, %8
%29 = phi i8* [ %10, %25 ], [ null, %8 ], [ null, %5 ]
%30 = tail call i32 @fclose(i32* %3)
br label %31
31: ; preds = %28, %2
%32 = phi i8* [ %29, %28 ], [ null, %2 ]
ret i8* %32
}