#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*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_END;
char *calloc(int, long);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int fread(char *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *nfile_read(const char *fname) {
  FILE *fp = fopen(fname, "rb");
  if (!fp) {
    perror(fname);
    exit(EXIT_FAILURE);
  }
  fseek(fp, 0L, SEEK_END);
  long size = ftell(fp);
  rewind(fp);
  char *buffer = calloc(1, size + 1);
  if (!buffer) {
    fclose(fp);
    fputs("ERR: nfile_read: failed to allocate memory.", stderr);
    exit(EXIT_FAILURE);
  }
  if (fread(buffer, size, 1, fp) != 1) {
    fclose(fp);
    free(buffer);
    fputs("ERR: nfile_read: failed to read file.", stderr);
    exit(EXIT_FAILURE);
  }
  fclose(fp);
  return buffer;
}


x86 assembly (clang, -O0):
<nfile_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f2 <nfile_read+0x42>
mov    -0x8(%rbp),%rdi
call   10c0 <perror@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1090 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edi
call   1080 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    126b <nfile_read+0xbb>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
lea    0xdab(%rip),%rdi
call   1070 <fputs@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     12bf <nfile_read+0x10f>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%esi
lea    0xd83(%rip),%rdi
call   1070 <fputs@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nfile_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe3f(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     123a <nfile_read+0x8a>
mov    %rax,%rbx
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1090 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1060 <rewind@plt>
lea    0x1(%r15),%rsi
mov    $0x1,%edi
call   1080 <calloc@plt>
test   %rax,%rax
je     1244 <nfile_read+0x94>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
cmp    $0x1,%ebp
jne    125e <nfile_read+0xae>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   10c0 <perror@plt>
jmp    127b <nfile_read+0xcb>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
lea    0xda7(%rip),%rdi
jmp    1276 <nfile_read+0xc6>
mov    %r14,%rdi
call   1030 <free@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
lea    0xdb9(%rip),%rdi
call   1070 <fputs@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@.str.1 = [44 x i8] c"ERR: nfile_read: failed to allocate memory.\00"
@stderr = global i32 0
@.str.2 = [38 x i8] c"ERR: nfile_read: failed to read file.\00"
define i8* @nfile_read(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 ([3 x i8], [3 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 %15, label %10
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call i32 @perror(i8* %11)
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = load i32*, i32** %3
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i64 0, i32 %17)
%19 = load i32*, i32** %3
%20 = call i64 @ftell(i32* %19)
store i64 %20, i64* %4
%21 = load i32*, i32** %3
%22 = call i32 @rewind(i32* %21)
%23 = load i64, i64* %4
%24 = add nsw i64 %23, 1
%25 = call i8* @calloc(i32 1, i64 %24)
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %35, label %28
28: ; preds = %15
%29 = load i32*, i32** %3
%30 = call i32 @fclose(i32* %29)
%31 = load i32, i32* @stderr
%32 = call i32 @fputs(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %15
%36 = load i8*, i8** %5
%37 = load i64, i64* %4
%38 = load i32*, i32** %3
%39 = call i32 @fread(i8* %36, i64 %37, i32 1, i32* %38)
%40 = icmp ne i32 %39, 1
br i1 %40, label %41, label %50
41: ; preds = %35
%42 = load i32*, i32** %3
%43 = call i32 @fclose(i32* %42)
%44 = load i8*, i8** %5
%45 = call i32 @free(i8* %44)
%46 = load i32, i32* @stderr
%47 = call i32 @fputs(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %46)
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %35
%51 = load i32*, i32** %3
%52 = call i32 @fclose(i32* %51)
%53 = load i8*, i8** %5
ret i8* %53
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@.str.1 = [44 x i8] c"ERR: nfile_read: failed to allocate memory.\00"
@stderr = global i32 0
@.str.2 = [38 x i8] c"ERR: nfile_read: failed to read file.\00"
define i8* @nfile_read(i8* nocapture %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 = tail call i32 @perror(i8* %0)
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %1
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %2, i64 0, i32 %9)
%11 = tail call i64 @ftell(i32* %2)
%12 = tail call i32 @rewind(i32* %2)
%13 = add nsw i64 %11, 1
%14 = tail call i8* @calloc(i32 1, i64 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %22
16: ; preds = %8
%17 = tail call i32 @fclose(i32* %2)
%18 = load i32, i32* @stderr
%19 = tail call i32 @fputs(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %8
%23 = tail call i32 @fread(i8* %14, i64 %11, i32 1, i32* %2)
%24 = icmp eq i32 %23, 1
%25 = tail call i32 @fclose(i32* %2)
br i1 %24, label %32, label %26
26: ; preds = %22
%27 = tail call i32 @free(i8* %14)
%28 = load i32, i32* @stderr
%29 = tail call i32 @fputs(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %22
ret i8* %14
}