#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, long, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
int /*<<< orphan*/ perror(int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
long read_file(const char *fname, char **content) {
  FILE *f = fopen(fname, "rb");
  if (!f)
    goto err;
  if (0 != fseek(f, 0, SEEK_END))
    goto err;
  long fsize = ftell(f);
  if (fsize == -1L)
    goto err;
  if (0 != fseek(f, 0, SEEK_SET))
    goto err;
  *content = malloc(fsize + 1);
  fread(*content, fsize, 1, f);
  if (ferror(f))
    goto err;
  fclose(f);
  (*content)[fsize] = 0;
  return fsize;
err:
  perror(strerror(errno));
  return -1;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c4 <read_file+0x34>
jmp    129c <read_file+0x10c>
mov    -0x20(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11eb <read_file+0x5b>
jmp    129c <read_file+0x10c>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1208 <read_file+0x78>
jmp    129c <read_file+0x10c>
mov    -0x20(%rbp),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     122f <read_file+0x9f>
jmp    129c <read_file+0x10c>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1277 <read_file+0xe7>
jmp    129c <read_file+0x10c>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12b9 <read_file+0x129>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%edi
call   10a0 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xe61(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     11c4 <read_file+0x34>
mov    %rax,%rbx
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
je     11e9 <read_file+0x59>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%edi
call   10a0 <perror@plt>
mov    $0xffffffffffffffff,%r15
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
call   1060 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     11c4 <read_file+0x34>
mov    %rax,%r15
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
jne    11c4 <read_file+0x34>
mov    %r15,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    11c4 <read_file+0x34>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    (%r14),%rax
movb   $0x0,(%rax,%r15,1)
jmp    11e0 <read_file+0x50>

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@errno = global i32 0
define i64 @read_file(i8* nocapture %0, i8** 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 %26, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i64 @fseek(i32* %3, i32 0, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %26
9: ; preds = %5
%10 = tail call i64 @ftell(i32* %3)
%11 = icmp eq i64 %10, -1
br i1 %11, label %26, label %12
12: ; preds = %9
%13 = load i32, i32* @SEEK_SET
%14 = tail call i64 @fseek(i32* %3, i32 0, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %12
%17 = add nuw nsw i64 %10, 1
%18 = tail call i8* @malloc(i64 %17)
store i8* %18, i8** %1
%19 = tail call i32 @fread(i8* %18, i64 %10, i32 1, i32* %3)
%20 = tail call i64 @ferror(i32* %3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = tail call i32 @fclose(i32* %3)
%24 = load i8*, i8** %1
%25 = getelementptr inbounds i8, i8* %24, i64 %10
store i8 0, i8* %25
br label %30
26: ; preds = %16, %12, %9, %5, %2
%27 = load i32, i32* @errno
%28 = tail call i32 @strerror(i32 %27)
%29 = tail call i32 @perror(i32 %28)
br label %30
30: ; preds = %26, %22
%31 = phi i64 [ -1, %26 ], [ %10, %22 ]
ret i64 %31
}