#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 */
scalar_t__ EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ clearerr(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
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 *strerror(int /*<<< orphan*/);
char *file_read(const char *fn) {
  assert(fn != NULL);
  FILE *fp;
  char *contents;
  size_t fsize;
  size_t nread;
  fp = fopen(fn, "r");
  if (!fp) {
    fprintf(stderr, "Could not open file `%s' for reading: %s\n", fn,
            strerror(errno));
    exit(EXIT_FAILURE);
  }
  fseek(fp, 0, SEEK_END);
  fsize = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  fsize *= sizeof *contents;
  contents = malloc(fsize + 1);
  if (!contents) {
    fprintf(stderr, "Could not malloc %lu bytes for file contents: %s\n",
            fsize + 1, strerror(errno));
    exit(EXIT_FAILURE);
  }
  nread = fread(contents, sizeof *contents, fsize, fp);
  if (nread != fsize) {
    fprintf(stderr, "WARNING: There was a problem reading `%s': %s\n", fn,
            strerror(errno));
  }
  contents[nread] = '\0';
  if (fclose(fp) == EOF) {
    fprintf(stderr, "WARNING: There was a problem closing `%s': %s\n", fn,
            strerror(errno));
    clearerr(fp);
  }
  return contents;
}


x86 assembly (clang, -O0):
<file_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
lea    0xe27(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1235 <file_read+0x85>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2d5f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x20(%rbp),%rax
shl    $0x0,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12de <file_read+0x12e>
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xd63(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@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     1340 <file_read+0x190>
mov    0x2cc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xd25(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2c84(%rip),%rcx
cmp    (%rcx),%rax
jne    13a8 <file_read+0x1f8>
mov    0x2c64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2c48(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xcf5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <clearerr@plt>
mov    -0x18(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_read>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1050 <assert@plt>
lea    0xe2f(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12d1 <file_read+0x121>
mov    %rax,%r12
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r12,%rdi
call   1070 <ftell@plt>
mov    %rax,%rbx
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    0x1(%rbx),%r13
mov    %r13,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     12f6 <file_read+0x146>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1030 <fread@plt>
mov    %rax,%r13
cmp    %rbx,%rax
je     1276 <file_read+0xc6>
mov    0x2d80(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xdf7(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
movb   $0x0,(%r15,%r13,1)
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    0x2d56(%rip),%rcx
cmp    (%rcx),%rax
jne    12c4 <file_read+0x114>
mov    0x2d3a(%rip),%rax
mov    (%rax),%ebx
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xde0(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r12,%rdi
call   1080 <clearerr@plt>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2cf8(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd13(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
jmp    1319 <file_read+0x169>
mov    0x2cd3(%rip),%rax
mov    (%rax),%ebx
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd18(%rip),%rsi
mov    %ebx,%edi
mov    %r13,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2c8e(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"Could not open file `%s' for reading: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [50 x i8] c"Could not malloc %lu bytes for file contents: %s\0A\00"
@.str.3 = [47 x i8] c"WARNING: There was a problem reading `%s': %s\0A\00"
@EOF = global i64 0
@.str.4 = [47 x i8] c"WARNING: There was a problem closing `%s': %s\0A\00"
define i8* @file_read(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 = icmp ne i8* %7, null
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i8*, i8** %2
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %3
%13 = load i32*, i32** %3
%14 = icmp ne i32* %13, null
br i1 %14, label %23, label %15
15: ; preds = %1
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %2
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %17, i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %1
%24 = load i32*, i32** %3
%25 = load i32, i32* @SEEK_END
%26 = call i32 @fseek(i32* %24, i32 0, i32 %25)
%27 = load i32*, i32** %3
%28 = call i64 @ftell(i32* %27)
store i64 %28, i64* %5
%29 = load i32*, i32** %3
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @fseek(i32* %29, i32 0, i32 %30)
%32 = load i64, i64* %5
%33 = mul i64 %32, 1
store i64 %33, i64* %5
%34 = load i64, i64* %5
%35 = add i64 %34, 1
%36 = call i8* @malloc(i64 %35)
store i8* %36, i8** %4
%37 = load i8*, i8** %4
%38 = icmp ne i8* %37, null
br i1 %38, label %48, label %39
39: ; preds = %23
%40 = load i32, i32* @stderr
%41 = load i64, i64* %5
%42 = add i64 %41, 1
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i64 %42, i8* %44)
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %23
%49 = load i8*, i8** %4
%50 = load i64, i64* %5
%51 = load i32*, i32** %3
%52 = call i64 @fread(i8* %49, i32 1, i64 %50, i32* %51)
store i64 %52, i64* %6
%53 = load i64, i64* %6
%54 = load i64, i64* %5
%55 = icmp ne i64 %53, %54
br i1 %55, label %56, label %62
56: ; preds = %48
%57 = load i32, i32* @stderr
%58 = load i8*, i8** %2
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %58, i8* %60)
br label %62
62: ; preds = %56, %48
%63 = load i8*, i8** %4
%64 = load i64, i64* %6
%65 = getelementptr inbounds i8, i8* %63, i64 %64
store i8 0, i8* %65
%66 = load i32*, i32** %3
%67 = call i64 @fclose(i32* %66)
%68 = load i64, i64* @EOF
%69 = icmp eq i64 %67, %68
br i1 %69, label %70, label %78
70: ; preds = %62
%71 = load i32, i32* @stderr
%72 = load i8*, i8** %2
%73 = load i32, i32* @errno
%74 = call i8* @strerror(i32 %73)
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i8* %72, i8* %74)
%76 = load i32*, i32** %3
%77 = call i32 @clearerr(i32* %76)
br label %78
78: ; preds = %70, %62
%79 = load i8*, i8** %4
ret i8* %79
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"Could not open file `%s' for reading: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [50 x i8] c"Could not malloc %lu bytes for file contents: %s\0A\00"
@.str.3 = [47 x i8] c"WARNING: There was a problem reading `%s': %s\0A\00"
@EOF = global i64 0
@.str.4 = [47 x i8] c"WARNING: There was a problem closing `%s': %s\0A\00"
define noalias i8* @file_read(i8* %0) {
%2 = icmp ne i8* %0, null
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %14
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %1
%15 = load i32, i32* @SEEK_END
%16 = tail call i32 @fseek(i32* %5, i32 0, i32 %15)
%17 = tail call i64 @ftell(i32* %5)
%18 = load i32, i32* @SEEK_SET
%19 = tail call i32 @fseek(i32* %5, i32 0, i32 %18)
%20 = add i64 %17, 1
%21 = tail call i8* @malloc(i64 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %30
23: ; preds = %14
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i64 %20, i8* %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %14
%31 = tail call i64 @fread(i8* %21, i32 1, i64 %17, i32* %5)
%32 = icmp eq i64 %31, %17
br i1 %32, label %38, label %33
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = tail call i8* @strerror(i32 %35)
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %36)
br label %38
38: ; preds = %33, %30
%39 = getelementptr inbounds i8, i8* %21, i64 %31
store i8 0, i8* %39
%40 = tail call i64 @fclose(i32* %5)
%41 = load i64, i64* @EOF
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %49
43: ; preds = %38
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = tail call i8* @strerror(i32 %45)
%47 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %46)
%48 = tail call i32 @clearerr(i32* %5)
br label %49
49: ; preds = %43, %38
ret i8* %21
}