#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *read_file(const char *filename) {
  FILE *fp = fopen(filename, "r");
  size_t file_size;
  char *file_contents;
  if (!fp)
    return NULL;
  fseek(fp, 0L, SEEK_END);
  file_size = ftell(fp);
  rewind(fp);
  file_contents = (char *)malloc(sizeof(char) * (file_size + 1));
  if (!file_contents) {
    fclose(fp);
    return NULL;
  }
  if (fread(file_contents, file_size, 1, fp) < 1) {
    if (ferror(fp)) {
      fclose(fp);
      free((void *)file_contents);
      return NULL;
    }
  }
  fclose(fp);
  file_contents[file_size] = '\0';
  return file_contents;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c8 <read_file+0x38>
movq   $0x0,-0x8(%rbp)
jmp    129f <read_file+0x10f>
mov    -0x18(%rbp),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122c <read_file+0x9c>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    129f <read_file+0x10f>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fread@plt>
cmp    $0x1,%eax
jge    1282 <read_file+0xf2>
mov    -0x18(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     127d <read_file+0xed>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    129f <read_file+0x10f>
jmp    1282 <read_file+0xf2>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%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):
<read_file>:
push   %r15
push   %r14
push   %rbx
lea    0xe64(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1231 <read_file+0xa1>
mov    %rax,%rbx
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1070 <rewind@plt>
lea    0x1(%r15),%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     120a <read_file+0x7a>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
test   %eax,%eax
jle    1214 <read_file+0x84>
mov    %rbx,%rdi
call   1060 <fclose@plt>
movb   $0x0,(%r14,%r15,1)
jmp    1234 <read_file+0xa4>
mov    %rbx,%rdi
call   1060 <fclose@plt>
jmp    1231 <read_file+0xa1>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     11fb <read_file+0x6b>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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