#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*/ fclose(int /*<<< orphan*/ *);
int file_size(char *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
char *read_file(char filename[]) {
  int size = file_size(filename);
  if (size < 1)
    return NULL;
  char *file_content = malloc(size + 1);
  if (file_content == NULL)
    return NULL;
  FILE *fp = fopen(filename, "r");
  size_t n = fread(file_content, 1, size, fp);
  file_content[n] = 0;
  fclose(fp);
  return file_content;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <file_size@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    117f <read_file+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    11f0 <read_file+0xa0>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a6 <read_file+0x56>
movq   $0x0,-0x8(%rbp)
jmp    11f0 <read_file+0xa0>
mov    -0x10(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1050 <file_size@plt>
test   %eax,%eax
jle    11a6 <read_file+0x56>
mov    %eax,%ebx
lea    0x1(%rbx),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11a6 <read_file+0x56>
mov    %rax,%r14
lea    0xe86(%rip),%rsi
mov    %r15,%rdi
call   1070 <fopen@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
movb   $0x0,(%r14,%rax,1)
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    11a9 <read_file+0x59>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define noalias i8* @read_file(i8* %0) {
%2 = tail call i32 @file_size(i8* %0)
%3 = icmp slt i32 %2, 1
br i1 %3, label %13, label %4
4: ; preds = %1
%5 = add nuw nsw i32 %2, 1
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %13, label %8
8: ; preds = %4
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = tail call i64 @fread(i8* %6, i32 1, i32 %2, i32* %9)
%11 = getelementptr inbounds i8, i8* %6, i64 %10
store i8 0, i8* %11
%12 = tail call i32 @fclose(i32* %9)
br label %13
13: ; preds = %8, %4, %1
%14 = phi i8* [ null, %1 ], [ %6, %8 ], [ null, %4 ]
ret i8* %14
}