#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*/ assert(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(size_t);
char *realloc(char *, size_t);
char *bf_read(const char *filepath) {
  FILE *input = fopen(filepath, "r");
  if (!input)
    return 0;
  long begin = ftell(input);
  if (begin < 0)
    goto file_fail;
  if (fseek(input, 0, SEEK_END))
    goto file_fail;
  long end = ftell(input);
  if (end < 0)
    goto file_fail;
  if (fseek(input, 0, SEEK_SET))
    goto file_fail;
  assert(begin <= end);
  size_t navail = end - begin;
  size_t buflen = navail + 1;
  char *buf = malloc(buflen);
  if (!buf)
    goto file_fail;
  size_t nread = fread(buf, sizeof *buf, buflen, input);
  if (nread < navail) {
    if (ferror(input))
      goto read_fail;
    buf[nread] = '\0';
    char *tmp = realloc(buf, nread);
    if (!tmp)
      goto read_fail;
    buf = tmp;
  }
  fclose(input);
  return buf;
read_fail:
  free(buf);
file_fail:
  fclose(input);
  return 0;
}


x86 assembly (clang, -O0):
<bf_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d8 <bf_read+0x38>
movq   $0x0,-0x8(%rbp)
jmp    1347 <bf_read+0x1a7>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11f5 <bf_read+0x55>
jmp    1336 <bf_read+0x196>
mov    -0x18(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     1218 <bf_read+0x78>
jmp    1336 <bf_read+0x196>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1235 <bf_read+0x95>
jmp    1336 <bf_read+0x196>
mov    -0x18(%rbp),%rdi
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
je     1258 <bf_read+0xb8>
jmp    1336 <bf_read+0x196>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12a2 <bf_read+0x102>
jmp    1336 <bf_read+0x196>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1317 <bf_read+0x177>
mov    -0x18(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12e2 <bf_read+0x142>
jmp    132d <bf_read+0x18d>
mov    -0x40(%rbp),%rax
mov    -0x48(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   10b0 <realloc@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    130f <bf_read+0x16f>
jmp    132d <bf_read+0x18d>
mov    -0x50(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1347 <bf_read+0x1a7>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bf_read>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xe51(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1296 <bf_read+0xf6>
mov    %rax,%r14
mov    %rax,%rdi
call   1080 <ftell@plt>
test   %rax,%rax
js     128a <bf_read+0xea>
mov    %rax,%r12
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
jne    128c <bf_read+0xec>
mov    %r14,%rdi
call   1080 <ftell@plt>
test   %rax,%rax
js     128a <bf_read+0xea>
mov    %rax,%r15
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
jne    128c <bf_read+0xec>
xor    %edi,%edi
sub    %r12,%r15
setae  %dil
call   1070 <assert@plt>
lea    0x1(%r15),%rbx
mov    %rbx,%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     128a <bf_read+0xea>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <fread@plt>
cmp    %r15,%rax
jae    12a7 <bf_read+0x107>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    1282 <bf_read+0xe2>
movb   $0x0,(%r12,%rbx,1)
mov    %r12,%rdi
mov    %rbx,%rsi
call   10b0 <realloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    128c <bf_read+0xec>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1060 <fclose@plt>
jmp    1298 <bf_read+0xf8>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r12,%rbx
jmp    128c <bf_read+0xec>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i8* @bf_read(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
store i8* %0, i8** %3
%12 = load i8*, i8** %3
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %4
%14 = load i32*, i32** %4
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %1
store i8* null, i8** %2
br label %92
17: ; preds = %1
%18 = load i32*, i32** %4
%19 = call i64 @ftell(i32* %18)
store i64 %19, i64* %5
%20 = load i64, i64* %5
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %17
br label %89
23: ; preds = %17
%24 = load i32*, i32** %4
%25 = load i32, i32* @SEEK_END
%26 = call i64 @fseek(i32* %24, i32 0, i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23
br label %89
29: ; preds = %23
%30 = load i32*, i32** %4
%31 = call i64 @ftell(i32* %30)
store i64 %31, i64* %6
%32 = load i64, i64* %6
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
br label %89
35: ; preds = %29
%36 = load i32*, i32** %4
%37 = load i32, i32* @SEEK_SET
%38 = call i64 @fseek(i32* %36, i32 0, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %35
br label %89
41: ; preds = %35
%42 = load i64, i64* %5
%43 = load i64, i64* %6
%44 = icmp sle i64 %42, %43
%45 = zext i1 %44 to i32
%46 = call i32 @assert(i32 %45)
%47 = load i64, i64* %6
%48 = load i64, i64* %5
%49 = sub nsw i64 %47, %48
store i64 %49, i64* %7
%50 = load i64, i64* %7
%51 = add i64 %50, 1
store i64 %51, i64* %8
%52 = load i64, i64* %8
%53 = call i8* @malloc(i64 %52)
store i8* %53, i8** %9
%54 = load i8*, i8** %9
%55 = icmp ne i8* %54, null
br i1 %55, label %57, label %56
56: ; preds = %41
br label %89
57: ; preds = %41
%58 = load i8*, i8** %9
%59 = load i64, i64* %8
%60 = load i32*, i32** %4
%61 = call i64 @fread(i8* %58, i32 1, i64 %59, i32* %60)
store i64 %61, i64* %10
%62 = load i64, i64* %10
%63 = load i64, i64* %7
%64 = icmp ult i64 %62, %63
br i1 %64, label %65, label %82
65: ; preds = %57
%66 = load i32*, i32** %4
%67 = call i64 @ferror(i32* %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %70
69: ; preds = %65
br label %86
70: ; preds = %65
%71 = load i8*, i8** %9
%72 = load i64, i64* %10
%73 = getelementptr inbounds i8, i8* %71, i64 %72
store i8 0, i8* %73
%74 = load i8*, i8** %9
%75 = load i64, i64* %10
%76 = call i8* @realloc(i8* %74, i64 %75)
store i8* %76, i8** %11
%77 = load i8*, i8** %11
%78 = icmp ne i8* %77, null
br i1 %78, label %80, label %79
79: ; preds = %70
br label %86
80: ; preds = %70
%81 = load i8*, i8** %11
store i8* %81, i8** %9
br label %82
82: ; preds = %80, %57
%83 = load i32*, i32** %4
%84 = call i32 @fclose(i32* %83)
%85 = load i8*, i8** %9
store i8* %85, i8** %2
br label %92
86: ; preds = %79, %69
%87 = load i8*, i8** %9
%88 = call i32 @free(i8* %87)
br label %89
89: ; preds = %86, %56, %40, %34, %28, %22
%90 = load i32*, i32** %4
%91 = call i32 @fclose(i32* %90)
store i8* null, i8** %2
br label %92
92: ; preds = %89, %82, %16
%93 = load i8*, i8** %2
ret i8* %93
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define noalias i8* @bf_read(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %41, label %4
4: ; preds = %1
%5 = tail call i64 @ftell(i32* %2)
%6 = icmp slt i64 %5, 0
br i1 %6, label %38, label %7
7: ; preds = %4
%8 = load i32, i32* @SEEK_END
%9 = tail call i64 @fseek(i32* %2, i32 0, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %38
11: ; preds = %7
%12 = tail call i64 @ftell(i32* %2)
%13 = icmp slt i64 %12, 0
br i1 %13, label %38, label %14
14: ; preds = %11
%15 = load i32, i32* @SEEK_SET
%16 = tail call i64 @fseek(i32* %2, i32 0, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %38
18: ; preds = %14
%19 = icmp uge i64 %12, %5
%20 = zext i1 %19 to i32
%21 = tail call i32 @assert(i32 %20)
%22 = sub nsw i64 %12, %5
%23 = add i64 %22, 1
%24 = tail call i8* @malloc(i64 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %38, label %26
26: ; preds = %18
%27 = tail call i64 @fread(i8* %24, i32 1, i64 %23, i32* %2)
%28 = icmp ult i64 %27, %22
br i1 %28, label %29, label %38
29: ; preds = %26
%30 = tail call i64 @ferror(i32* %2)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = getelementptr inbounds i8, i8* %24, i64 %27
store i8 0, i8* %33
%34 = tail call i8* @realloc(i8* %24, i64 %27)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %38
36: ; preds = %32, %29
%37 = tail call i32 @free(i8* %24)
br label %38
38: ; preds = %36, %4, %7, %11, %14, %18, %26, %32
%39 = phi i8* [ %34, %32 ], [ %24, %26 ], [ null, %18 ], [ null, %14 ], [ null, %11 ], [ null, %7 ], [ null, %4 ], [ null, %36 ]
%40 = tail call i32 @fclose(i32* %2)
br label %41
41: ; preds = %38, %1
%42 = phi i8* [ null, %1 ], [ %39, %38 ]
ret i8* %42
}