#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
char *file_content(char *f_name, size_t *size) {
  FILE *fp = fopen(f_name, "rb");
  if (!fp) {
    printf("Failed to open file: %s\n", f_name);
    return NULL;
  }
  fseek(fp, 0, SEEK_END);
  *size = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  char *content = malloc(sizeof(char) * (*size) + sizeof(char));
  if (!content) {
    printf("file_content: Failed to allocate room for file content\n");
    return NULL;
  }
  if (fread(content, sizeof(char), *size, fp) != *size) {
    printf("file_content: Failed to read all bytes, expected %u\n", *size);
    free(content);
    return NULL;
  }
  content[*size] = '\0';
  return content;
}


x86 assembly (clang, -O0):
<file_content>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11be <file_content+0x4e>
mov    -0x10(%rbp),%rsi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a1 <file_content+0x131>
mov    -0x20(%rbp),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1239 <file_content+0xc9>
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a1 <file_content+0x131>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
je     128a <file_content+0x11a>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12a1 <file_content+0x131>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_content>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xe6b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     120a <file_content+0x8a>
mov    %rax,%rbx
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,(%r14)
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    (%r14),%r12
lea    0x1(%r12),%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1220 <file_content+0xa0>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    (%r14),%rsi
cmp    %rsi,%rax
jne    122e <file_content+0xae>
movb   $0x0,(%r15,%rax,1)
jmp    1247 <file_content+0xc7>
lea    0xdf2(%rip),%rdi
xor    %r15d,%r15d
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1247 <file_content+0xc7>
lea    0xe2a(%rip),%rdi
call   1040 <puts@plt>
jmp    1244 <file_content+0xc4>
lea    0xde7(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [25 x i8] c"Failed to open file: %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [56 x i8] c"file_content: Failed to allocate room for file content\0A\00"
@.str.3 = [53 x i8] c"file_content: Failed to read all bytes, expected %u\0A\00"
define i8* @file_content(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32*
%7 = alloca i8*
store i8* %0, i8** %4
store i64* %1, i64** %5
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %13)
store i8* null, i8** %3
br label %56
15: ; preds = %2
%16 = load i32*, i32** %6
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i32 0, i32 %17)
%19 = load i32*, i32** %6
%20 = call i64 @ftell(i32* %19)
%21 = load i64*, i64** %5
store i64 %20, i64* %21
%22 = load i32*, i32** %6
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @fseek(i32* %22, i32 0, i32 %23)
%25 = load i64*, i64** %5
%26 = load i64, i64* %25
%27 = mul i64 1, %26
%28 = add i64 %27, 1
%29 = trunc i64 %28 to i32
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %7
%31 = load i8*, i8** %7
%32 = icmp ne i8* %31, null
br i1 %32, label %35, label %33
33: ; preds = %15
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %3
br label %56
35: ; preds = %15
%36 = load i8*, i8** %7
%37 = load i64*, i64** %5
%38 = load i64, i64* %37
%39 = load i32*, i32** %6
%40 = call i64 @fread(i8* %36, i32 1, i64 %38, i32* %39)
%41 = load i64*, i64** %5
%42 = load i64, i64* %41
%43 = icmp ne i64 %40, %42
br i1 %43, label %44, label %50
44: ; preds = %35
%45 = load i64*, i64** %5
%46 = load i64, i64* %45
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i64 %46)
%48 = load i8*, i8** %7
%49 = call i32 @free(i8* %48)
store i8* null, i8** %3
br label %56
50: ; preds = %35
%51 = load i8*, i8** %7
%52 = load i64*, i64** %5
%53 = load i64, i64* %52
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
%55 = load i8*, i8** %7
store i8* %55, i8** %3
br label %56
56: ; preds = %50, %44, %33, %12
%57 = load i8*, i8** %3
ret i8* %57
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [25 x i8] c"Failed to open file: %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [53 x i8] c"file_content: Failed to read all bytes, expected %u\0A\00"
@str = [55 x i8] c"file_content: Failed to allocate room for file content\00"
define noalias i8* @file_content(i8* %0, i64* nocapture %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 %5, label %7
5: ; preds = %2
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %29
7: ; preds = %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %3)
store i64 %10, i64* %1
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @fseek(i32* %3, i32 0, i32 %11)
%13 = load i64, i64* %1
%14 = trunc i64 %13 to i32
%15 = add i32 %14, 1
%16 = tail call i8* @malloc(i32 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %7
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
br label %29
20: ; preds = %7
%21 = tail call i64 @fread(i8* %16, i32 1, i64 %13, i32* %3)
%22 = load i64, i64* %1
%23 = icmp eq i64 %21, %22
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0), i64 %22)
%26 = tail call i32 @free(i8* %16)
br label %29
27: ; preds = %20
%28 = getelementptr inbounds i8, i8* %16, i64 %21
store i8 0, i8* %28
br label %29
29: ; preds = %18, %24, %27, %5
%30 = phi i8* [ null, %5 ], [ null, %24 ], [ %16, %27 ], [ null, %18 ]
ret i8* %30
}