#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*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
char *readfile(const char *filename) {
  FILE *fp = fopen(filename, "r");
  assert(fp != NULL && "Error reading file");
  fseek(fp, 0, SEEK_END);
  long size = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  char *buffer = malloc((size + 1) * sizeof(char));
  assert(buffer && "[readfile] Couldn't malloc buffer");
  fread(buffer, sizeof(char), size, fp);
  buffer[size] = '\0';
  fclose(fp);
  return buffer;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeac(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x21(%rbp)
je     11aa <readfile+0x3a>
mov    $0x1,%al
mov    %al,-0x21(%rbp)
jmp    11aa <readfile+0x3a>
mov    -0x21(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x22(%rbp)
je     121b <readfile+0xab>
mov    $0x1,%al
mov    %al,-0x22(%rbp)
jmp    121b <readfile+0xab>
mov    -0x22(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %r15
push   %r14
push   %rbx
lea    0xe84(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r15
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1060 <ftell@plt>
mov    %rax,%r14
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
lea    0x1(%r14),%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %r15,%rcx
call   1030 <fread@plt>
movb   $0x0,(%rbx,%r14,1)
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"Error reading file\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [34 x i8] c"[readfile] Couldn't malloc buffer\00"
define i8* @readfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
br label %11
11: ; preds = %10, %1
%12 = phi i1 [ false, %1 ], [ true, %10 ]
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32*, i32** %3
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i32 0, i32 %16)
%18 = load i32*, i32** %3
%19 = call i64 @ftell(i32* %18)
store i64 %19, i64* %4
%20 = load i32*, i32** %3
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @fseek(i32* %20, i32 0, i32 %21)
%23 = load i64, i64* %4
%24 = add nsw i64 %23, 1
%25 = mul i64 %24, 1
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** %5
%27 = load i8*, i8** %5
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %30
29: ; preds = %11
br label %30
30: ; preds = %29, %11
%31 = phi i1 [ false, %11 ], [ true, %29 ]
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i8*, i8** %5
%35 = load i64, i64* %4
%36 = load i32*, i32** %3
%37 = call i32 @fread(i8* %34, i32 1, i64 %35, i32* %36)
%38 = load i8*, i8** %5
%39 = load i64, i64* %4
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8 0, i8* %40
%41 = load i32*, i32** %3
%42 = call i32 @fclose(i32* %41)
%43 = load i8*, i8** %5
ret i8* %43
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define noalias i8* @readfile(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 ne i32* %2, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %2, i32 0, i32 %6)
%8 = tail call i64 @ftell(i32* %2)
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %2, i32 0, i32 %9)
%11 = add nsw i64 %8, 1
%12 = tail call i8* @malloc(i64 %11)
%13 = icmp ne i8* %12, null
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i32 @fread(i8* %12, i32 1, i64 %8, i32* %2)
%17 = getelementptr inbounds i8, i8* %12, i64 %8
store i8 0, i8* %17
%18 = tail call i32 @fclose(i32* %2)
ret i8* %12
}