#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*/ epanic(char *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *read_all(const char *path) {
  FILE *fp = fopen(path, "rb");
  if (!fp)
    epanic("failed to open %s", path);
  if (fseek(fp, 0, SEEK_END) < 0)
    epanic("failed to seek in %s", path);
  long len = ftell(fp);
  char *buf = malloc(len + 1);
  if (!buf)
    epanic("read_all");
  rewind(fp);
  size_t rlen = fread(buf, 1, len, fp);
  if ((errno = ferror(fp)))
    epanic("failed to read %s", path);
  buf[rlen] = 0;
  fclose(fp);
  return buf;
}


x86 assembly (clang, -O0):
<read_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cd <read_all+0x3d>
mov    -0x8(%rbp),%rsi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1080 <epanic@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
jge    11fd <read_all+0x6d>
mov    -0x8(%rbp),%rsi
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1080 <epanic@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1234 <read_all+0xa4>
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1080 <epanic@plt>
mov    -0x10(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
mov    0x2d71(%rip),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
je     1284 <read_all+0xf4>
mov    -0x8(%rbp),%rsi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1080 <epanic@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_all>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe5e(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c0 <read_all+0x30>
lea    0xe4d(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <epanic@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
jns    11e9 <read_all+0x59>
lea    0xe36(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <epanic@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r12
lea    0x1(%rax),%rdi
call   1090 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1213 <read_all+0x83>
lea    0xe1e(%rip),%rdi
xor    %eax,%eax
call   1080 <epanic@plt>
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1030 <ferror@plt>
mov    0x2d98(%rip),%rcx
mov    %eax,(%rcx)
test   %eax,%eax
je     1257 <read_all+0xc7>
lea    0xde6(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <epanic@plt>
movb   $0x0,(%r15,%r12,1)
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%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 = [18 x i8] c"failed to open %s\00"
@SEEK_END = global i32 0
@.str.2 = [21 x i8] c"failed to seek in %s\00"
@.str.3 = [9 x i8] c"read_all\00"
@errno = global i32 0
@.str.4 = [18 x i8] c"failed to read %s\00"
define i8* @read_all(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
%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 %14, label %11
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i32 (i8*, ...) @epanic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %11, %1
%15 = load i32*, i32** %3
%16 = load i32, i32* @SEEK_END
%17 = call i64 @fseek(i32* %15, i32 0, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i8*, i8** %2
%21 = call i32 (i8*, ...) @epanic(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %19, %14
%23 = load i32*, i32** %3
%24 = call i64 @ftell(i32* %23)
store i64 %24, i64* %4
%25 = load i64, i64* %4
%26 = add nsw i64 %25, 1
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %5
%28 = load i8*, i8** %5
%29 = icmp ne i8* %28, null
br i1 %29, label %32, label %30
30: ; preds = %22
%31 = call i32 (i8*, ...) @epanic(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %22
%33 = load i32*, i32** %3
%34 = call i32 @rewind(i32* %33)
%35 = load i8*, i8** %5
%36 = load i64, i64* %4
%37 = load i32*, i32** %3
%38 = call i64 @fread(i8* %35, i32 1, i64 %36, i32* %37)
store i64 %38, i64* %6
%39 = load i32*, i32** %3
%40 = call i32 @ferror(i32* %39)
store i32 %40, i32* @errno
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %32
%43 = load i8*, i8** %2
%44 = call i32 (i8*, ...) @epanic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %42, %32
%46 = load i8*, i8** %5
%47 = load i64, i64* %6
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8 0, i8* %48
%49 = load i32*, i32** %3
%50 = call i32 @fclose(i32* %49)
%51 = load i8*, i8** %5
ret i8* %51
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [18 x i8] c"failed to open %s\00"
@SEEK_END = global i32 0
@.str.2 = [21 x i8] c"failed to seek in %s\00"
@.str.3 = [9 x i8] c"read_all\00"
@errno = global i32 0
@.str.4 = [18 x i8] c"failed to read %s\00"
define noalias i8* @read_all(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 %6
4: ; preds = %1
%5 = tail call i32 (i8*, ...) @epanic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %6
6: ; preds = %4, %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i64 @fseek(i32* %2, i32 0, i32 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 (i8*, ...) @epanic(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %10, %6
%13 = tail call i64 @ftell(i32* %2)
%14 = add nsw i64 %13, 1
%15 = tail call i8* @malloc(i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 (i8*, ...) @epanic(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = tail call i32 @rewind(i32* %2)
%21 = tail call i64 @fread(i8* %15, i32 1, i64 %13, i32* %2)
%22 = tail call i32 @ferror(i32* %2)
store i32 %22, i32* @errno
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %19
%25 = tail call i32 (i8*, ...) @epanic(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %26
26: ; preds = %24, %19
%27 = getelementptr inbounds i8, i8* %15, i64 %21
store i8 0, i8* %27
%28 = tail call i32 @fclose(i32* %2)
ret i8* %15
}