#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*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char *file_get_contents(const char *filename, char *contents) {
  FILE *fp;
  int c;
  int n;
  fp = fopen(filename, "rb");
  if (fp == NULL) {
    contents[0] = '\0';
    return contents;
  }
  while ((c = fgetc(fp)) != EOF) {
    *contents++ = (char)c;
  }
  fclose(fp);
  return contents;
}


x86 assembly (clang, -O0):
<file_get_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1173 <file_get_contents+0x43>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11bf <file_get_contents+0x8f>
jmp    1178 <file_get_contents+0x48>
mov    -0x20(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e5d(%rip),%rcx
cmp    (%rcx),%eax
je     11ae <file_get_contents+0x7e>
mov    -0x24(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    1178 <file_get_contents+0x48>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_get_contents>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
lea    0xec1(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     117d <file_get_contents+0x4d>
mov    %rax,%r14
mov    %rax,%rdi
call   1040 <fgetc@plt>
mov    0x2e8d(%rip),%r15
cmp    (%r15),%eax
je     1173 <file_get_contents+0x43>
mov    %al,(%rbx)
add    $0x1,%rbx
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    (%r15),%eax
jne    1160 <file_get_contents+0x30>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    1180 <file_get_contents+0x50>
movb   $0x0,(%rbx)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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