#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ stderr;
char *read_file_z(const char *filename) {
  FILE *file;
  char *data;
  size_t len;
  size_t read_len;
  file = fopen(filename, "rb");
  if (!file) {
    fprintf(stderr, "ERROR: Can't read: %s\n", filename);
    exit(1);
  }
  fseek(file, 0, SEEK_END);
  len = ftell(file);
  data = malloc(len + 1);
  if (!data) {
    fprintf(stderr, "ERROR: Allocation failed reading: %s\n", filename);
    fclose(file);
    exit(1);
  }
  data[len] = '\0';
  fseek(file, 0, SEEK_SET);
  read_len = fread(data, 1, len, file);
  if (read_len != len) {
    fprintf(stderr, "ERROR: Failed reading: %s\n", filename);
    fclose(file);
    exit(1);
  }
  return data;
}


x86 assembly (clang, -O0):
<read_file_z>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ce <read_file_z+0x4e>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe44(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1237 <read_file_z+0xb7>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdfb(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    0x2d82(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     12ab <read_file_z+0x12b>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdad(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file_z>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe6e(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     120d <read_file_z+0x8d>
mov    %rax,%r15
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1060 <ftell@plt>
mov    %rax,%r12
lea    0x1(%rax),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     122f <read_file_z+0xaf>
mov    %rax,%rbx
movb   $0x0,(%rax,%r12,1)
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    %r12,%rax
jne    1241 <read_file_z+0xc1>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
jmp    1251 <read_file_z+0xd1>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"ERROR: Can't read: %s\0A\00"
@SEEK_END = global i32 0
@.str.2 = [38 x i8] c"ERROR: Allocation failed reading: %s\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [27 x i8] c"ERROR: Failed reading: %s\0A\00"
define i8* @read_file_z(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp ne i32* %9, null
br i1 %10, label %16, label %11
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %2
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = load i32*, i32** %3
%18 = load i32, i32* @SEEK_END
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = load i32*, i32** %3
%21 = call i64 @ftell(i32* %20)
store i64 %21, i64* %5
%22 = load i64, i64* %5
%23 = add i64 %22, 1
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** %4
%25 = load i8*, i8** %4
%26 = icmp ne i8* %25, null
br i1 %26, label %34, label %27
27: ; preds = %16
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %2
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = load i32*, i32** %3
%32 = call i32 @fclose(i32* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %16
%35 = load i8*, i8** %4
%36 = load i64, i64* %5
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 0, i8* %37
%38 = load i32*, i32** %3
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @fseek(i32* %38, i32 0, i32 %39)
%41 = load i8*, i8** %4
%42 = load i64, i64* %5
%43 = load i32*, i32** %3
%44 = call i64 @fread(i8* %41, i32 1, i64 %42, i32* %43)
store i64 %44, i64* %6
%45 = load i64, i64* %6
%46 = load i64, i64* %5
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %55
48: ; preds = %34
%49 = load i32, i32* @stderr
%50 = load i8*, i8** %2
%51 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %50)
%52 = load i32*, i32** %3
%53 = call i32 @fclose(i32* %52)
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %34
%56 = load i8*, i8** %4
ret i8* %56
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"ERROR: Can't read: %s\0A\00"
@SEEK_END = global i32 0
@.str.2 = [38 x i8] c"ERROR: Allocation failed reading: %s\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [27 x i8] c"ERROR: Failed reading: %s\0A\00"
define noalias i8* @read_file_z(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %1
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %2, i32 0, i32 %9)
%11 = tail call i64 @ftell(i32* %2)
%12 = add i64 %11, 1
%13 = tail call i8* @malloc(i64 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %8
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%18 = tail call i32 @fclose(i32* %2)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %8
%21 = getelementptr inbounds i8, i8* %13, i64 %11
store i8 0, i8* %21
%22 = load i32, i32* @SEEK_SET
%23 = tail call i32 @fseek(i32* %2, i32 0, i32 %22)
%24 = tail call i64 @fread(i8* %13, i32 1, i64 %11, i32* %2)
%25 = icmp eq i64 %24, %11
br i1 %25, label %31, label %26
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%29 = tail call i32 @fclose(i32* %2)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %20
ret i8* %13
}