#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 off_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fsize(char *);
int getc(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ stderr;
char *readfile(char *filename) {
  int c;
  FILE *f;
  off_t filesize;
  char *buffer;
  char *bufferp;
  if ((filesize = fsize(filename)) == -1)
    return NULL;
  buffer = malloc(sizeof(char) * (filesize + 1));
  bufferp = buffer;
  if ((f = fopen(filename, "r")) == NULL) {
    fprintf(stderr, "Error opening file: %s\n", filename);
    return NULL;
  }
  while ((c = getc(f)) != EOF)
    *bufferp++ = c;
  *bufferp = '\0';
  fclose(f);
  return buffer;
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <fsize@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    118e <readfile+0x2e>
movq   $0x0,-0x8(%rbp)
jmp    1244 <readfile+0xe4>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11f1 <readfile+0x91>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe23(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1244 <readfile+0xe4>
jmp    11f6 <readfile+0x96>
mov    -0x20(%rbp),%rdi
call   1080 <getc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%eax
je     122c <readfile+0xcc>
mov    -0x14(%rbp),%eax
mov    %al,%cl
mov    -0x38(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x38(%rbp)
mov    %cl,(%rax)
jmp    11f6 <readfile+0x96>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1060 <fsize@plt>
cmp    $0xffffffff,%eax
je     11f8 <readfile+0x98>
add    $0x1,%eax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
lea    0xe73(%rip),%rsi
mov    %r15,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11e0 <readfile+0x80>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <getc@plt>
mov    0x2e3c(%rip),%r15
mov    %r14,%rbx
cmp    (%r15),%eax
je     11d3 <readfile+0x73>
mov    %r14,%rbx
nopw   0x0(%rax,%rax,1)
mov    %al,(%rbx)
add    $0x1,%rbx
mov    %r12,%rdi
call   1080 <getc@plt>
cmp    (%r15),%eax
jne    11c0 <readfile+0x60>
movb   $0x0,(%rbx)
mov    %r12,%rdi
call   1030 <fclose@plt>
jmp    11fb <readfile+0x9b>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
mov    %r15,%rdx
call   1040 <fprintf@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Error opening file: %s\0A\00"
@EOF = global i32 0
define i8* @readfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32 @fsize(i8* %9)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* null, i8** %2
br label %44
13: ; preds = %1
%14 = load i32, i32* %6
%15 = add nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = mul i64 1, %16
%18 = trunc i64 %17 to i32
%19 = call i8* @malloc(i32 %18)
store i8* %19, i8** %7
%20 = load i8*, i8** %7
store i8* %20, i8** %8
%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** %5
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %13
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %3
%27 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %26)
store i8* null, i8** %2
br label %44
28: ; preds = %13
br label %29
29: ; preds = %34, %28
%30 = load i32*, i32** %5
%31 = call i32 @getc(i32* %30)
store i32 %31, i32* %4
%32 = load i32, i32* @EOF
%33 = icmp ne i32 %31, %32
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = load i32, i32* %4
%36 = trunc i32 %35 to i8
%37 = load i8*, i8** %8
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %8
store i8 %36, i8* %37
br label %29
39: ; preds = %29
%40 = load i8*, i8** %8
store i8 0, i8* %40
%41 = load i32*, i32** %5
%42 = call i32 @fclose(i32* %41)
%43 = load i8*, i8** %7
store i8* %43, i8** %2
br label %44
44: ; preds = %39, %24, %12
%45 = load i8*, i8** %2
ret i8* %45
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"Error opening file: %s\0A\00"
@EOF = global i32 0
define noalias i8* @readfile(i8* %0) {
%2 = tail call i32 @fsize(i8* %0)
%3 = icmp eq i32 %2, -1
br i1 %3, label %27, label %4
4: ; preds = %1
%5 = add nuw nsw i32 %2, 1
%6 = tail call i8* @malloc(i32 %5)
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %13, label %9
9: ; preds = %4
%10 = tail call i32 @getc(i32* %7)
%11 = load i32, i32* @EOF
%12 = icmp eq i32 %10, %11
br i1 %12, label %24, label %16
13: ; preds = %4
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %27
16: ; preds = %9, %16
%17 = phi i32 [ %21, %16 ], [ %10, %9 ]
%18 = phi i8* [ %20, %16 ], [ %6, %9 ]
%19 = trunc i32 %17 to i8
%20 = getelementptr inbounds i8, i8* %18, i64 1
store i8 %19, i8* %18
%21 = tail call i32 @getc(i32* %7)
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %16
24: ; preds = %16, %9
%25 = phi i8* [ %6, %9 ], [ %20, %16 ]
store i8 0, i8* %25
%26 = tail call i32 @fclose(i32* %7)
br label %27
27: ; preds = %1, %24, %13
%28 = phi i8* [ null, %13 ], [ %6, %24 ], [ null, %1 ]
ret i8* %28
}