#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;
scalar_t__ chk_malloc(unsigned long long, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
unsigned long long fread(char *, int, unsigned long long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned long long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ print_error(char *, char *);
char *readFile(char *infile, unsigned long long *infilelen) {
  FILE *file = NULL;
  char *buffer = NULL;
  file = fopen(infile, "rb");
  if (!file) {
    print_error("Unable to open file %s", infile);
    return NULL;
  }
  fseek(file, 0, SEEK_END);
  *infilelen = ftell(file);
  fseek(file, 0, SEEK_SET);
  buffer = (char *)chk_malloc(*infilelen + 1, sizeof(char));
  unsigned long long readBytes = fread(buffer, 1, *infilelen, file);
  if (readBytes != *infilelen)
    print_error("ERROR reading from %s\n", infile);
  fclose(file);
  return buffer;
}


x86 assembly (clang, -O0):
<readFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cc <readFile+0x5c>
mov    -0x10(%rbp),%rsi
lea    0xe49(%rip),%rdi
call   1040 <print_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    126f <readFile+0xff>
mov    -0x20(%rbp),%rdi
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <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    0x2dda(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1080 <chk_malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
je     125e <readFile+0xee>
mov    -0x10(%rbp),%rsi
lea    0xdc1(%rip),%rdi
call   1040 <print_error@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe7b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1208 <readFile+0x98>
mov    %rax,%rbx
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,(%r15)
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    (%r15),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1080 <chk_malloc@plt>
mov    %rax,%r12
mov    (%r15),%rdx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    (%r15),%rax
je     11fe <readFile+0x8e>
lea    0xe24(%rip),%rdi
mov    %r14,%rsi
call   1040 <print_error@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    121a <readFile+0xaa>
lea    0xdf4(%rip),%rdi
mov    %r14,%rsi
call   1040 <print_error@plt>
xor    %r12d,%r12d
mov    %r12,%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 = [23 x i8] c"Unable to open file %s\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [23 x i8] c"ERROR reading from %s\0A\00"
define i8* @readFile(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
store i8* %0, i8** %4
store i64* %1, i64** %5
store i32* null, i32** %6
store i8* null, i8** %7
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @print_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i8* null, i8** %3
br label %47
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i32*, i32** %6
%21 = call i64 @ftell(i32* %20)
%22 = load i64*, i64** %5
store i64 %21, i64* %22
%23 = load i32*, i32** %6
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @fseek(i32* %23, i32 0, i32 %24)
%26 = load i64*, i64** %5
%27 = load i64, i64* %26
%28 = add i64 %27, 1
%29 = call i64 @chk_malloc(i64 %28, i32 1)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %7
%31 = load i8*, i8** %7
%32 = load i64*, i64** %5
%33 = load i64, i64* %32
%34 = load i32*, i32** %6
%35 = call i64 @fread(i8* %31, i32 1, i64 %33, i32* %34)
store i64 %35, i64* %8
%36 = load i64, i64* %8
%37 = load i64*, i64** %5
%38 = load i64, i64* %37
%39 = icmp ne i64 %36, %38
br i1 %39, label %40, label %43
40: ; preds = %16
%41 = load i8*, i8** %4
%42 = call i32 @print_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %40, %16
%44 = load i32*, i32** %6
%45 = call i32 @fclose(i32* %44)
%46 = load i8*, i8** %7
store i8* %46, i8** %3
br label %47
47: ; preds = %43, %13
%48 = load i8*, i8** %3
ret i8* %48
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [23 x i8] c"Unable to open file %s\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [23 x i8] c"ERROR reading from %s\0A\00"
define i8* @readFile(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 @print_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %25
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 = add i64 %13, 1
%15 = tail call i64 @chk_malloc(i64 %14, i32 1)
%16 = inttoptr i64 %15 to i8*
%17 = load i64, i64* %1
%18 = tail call i64 @fread(i8* %16, i32 1, i64 %17, i32* %3)
%19 = load i64, i64* %1
%20 = icmp eq i64 %18, %19
br i1 %20, label %23, label %21
21: ; preds = %7
%22 = tail call i32 @print_error(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %23
23: ; preds = %21, %7
%24 = tail call i32 @fclose(i32* %3)
br label %25
25: ; preds = %23, %5
%26 = phi i8* [ %16, %23 ], [ null, %5 ]
ret i8* %26
}