#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;
scalar_t__ calloc(size_t, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ print_errno(char *);
int /*<<< orphan*/ stderr;
char *readin_file(char const *path) {
  size_t size = 0;
  FILE *f;
  char *raw_file;
  if ((f = fopen(path, "r")) == NULL) {
    print_errno("Error in fopen!");
    return NULL;
  }
  if (fseek(f, 0L, SEEK_END) < 0) {
    print_errno("Error in SEEK_END fseek!");
    goto io_error;
  }
  if ((size = ftell(f)) < 0) {
    print_errno("Error in ftell!");
    goto io_error;
  }
  if (fseek(f, 0L, SEEK_SET) < 0) {
    print_errno("Error in SEEK_SET fseek!");
    goto io_error;
  }
  raw_file = (char *)calloc(size + 1, sizeof(char));
  if (raw_file == NULL) {
    fprintf(stderr, "OUT OF MEMORY\n");
    goto io_error;
  }
  if (fread(raw_file, size, 1, f) <= 0) {
    fprintf(stderr, "Failed to read from file, or it was empty\n");
    free(raw_file);
    goto io_error;
  }
  fclose(f);
  raw_file[size] = '\0';
  return raw_file;
io_error:
  fclose(f);
  return NULL;
}


x86 assembly (clang, -O0):
<readin_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11db <readin_file+0x4b>
lea    0xe39(%rip),%rdi
call   1030 <print_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    1316 <readin_file+0x186>
mov    -0x20(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
jge    120c <readin_file+0x7c>
lea    0xe10(%rip),%rdi
call   1030 <print_errno@plt>
jmp    1305 <readin_file+0x175>
mov    -0x20(%rbp),%rdi
call   1090 <ftell@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jae    1234 <readin_file+0xa4>
lea    0xe01(%rip),%rdi
call   1030 <print_errno@plt>
jmp    1305 <readin_file+0x175>
mov    -0x20(%rbp),%rdi
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
jge    1265 <readin_file+0xd5>
lea    0xde0(%rip),%rdi
call   1030 <print_errno@plt>
jmp    1305 <readin_file+0x175>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12a0 <readin_file+0x110>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xdbe(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1305 <readin_file+0x175>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fread@plt>
cmp    $0x0,%rax
jg     12e3 <readin_file+0x153>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
jmp    1305 <readin_file+0x175>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1316 <readin_file+0x186>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readin_file>:
push   %r15
push   %r14
push   %rbx
lea    0xe64(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1221 <readin_file+0x91>
mov    %rax,%r15
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
js     122f <readin_file+0x9f>
mov    %r15,%rdi
call   1090 <ftell@plt>
mov    %rax,%r14
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
js     123d <readin_file+0xad>
lea    0x1(%r14),%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
test   %rax,%rax
je     124b <readin_file+0xbb>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %r15,%rcx
call   1050 <fread@plt>
test   %rax,%rax
jle    1262 <readin_file+0xd2>
mov    %r15,%rdi
call   1060 <fclose@plt>
movb   $0x0,(%rbx,%r14,1)
jmp    1289 <readin_file+0xf9>
lea    0xdda(%rip),%rdi
call   1030 <print_errno@plt>
jmp    1287 <readin_file+0xf7>
lea    0xddc(%rip),%rdi
call   1030 <print_errno@plt>
jmp    127f <readin_file+0xef>
lea    0xde7(%rip),%rdi
call   1030 <print_errno@plt>
jmp    127f <readin_file+0xef>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
call   1080 <fprintf@plt>
jmp    127f <readin_file+0xef>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [16 x i8] c"Error in fopen!\00"
@SEEK_END = global i32 0
@.str.2 = [25 x i8] c"Error in SEEK_END fseek!\00"
@.str.3 = [16 x i8] c"Error in ftell!\00"
@SEEK_SET = global i32 0
@.str.4 = [25 x i8] c"Error in SEEK_SET fseek!\00"
@stderr = global i32 0
@.str.5 = [15 x i8] c"OUT OF MEMORY\0A\00"
@.str.6 = [43 x i8] c"Failed to read from file, or it was empty\0A\00"
define i8* @readin_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i8*
store i8* %0, i8** %3
store i64 0, i64* %4
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @print_errno(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %63
12: ; preds = %1
%13 = load i32*, i32** %5
%14 = load i32, i32* @SEEK_END
%15 = call i64 @fseek(i32* %13, i64 0, i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @print_errno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %60
19: ; preds = %12
%20 = load i32*, i32** %5
%21 = call i64 @ftell(i32* %20)
store i64 %21, i64* %4
%22 = icmp ult i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @print_errno(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %60
25: ; preds = %19
%26 = load i32*, i32** %5
%27 = load i32, i32* @SEEK_SET
%28 = call i64 @fseek(i32* %26, i64 0, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @print_errno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %60
32: ; preds = %25
%33 = load i64, i64* %4
%34 = add i64 %33, 1
%35 = call i64 @calloc(i64 %34, i32 1)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %6
%37 = load i8*, i8** %6
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %60
42: ; preds = %32
%43 = load i8*, i8** %6
%44 = load i64, i64* %4
%45 = load i32*, i32** %5
%46 = call i64 @fread(i8* %43, i64 %44, i32 1, i32* %45)
%47 = icmp sle i64 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %42
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0))
%51 = load i8*, i8** %6
%52 = call i32 @free(i8* %51)
br label %60
53: ; preds = %42
%54 = load i32*, i32** %5
%55 = call i32 @fclose(i32* %54)
%56 = load i8*, i8** %6
%57 = load i64, i64* %4
%58 = getelementptr inbounds i8, i8* %56, i64 %57
store i8 0, i8* %58
%59 = load i8*, i8** %6
store i8* %59, i8** %2
br label %63
60: ; preds = %48, %39, %30, %23, %17
%61 = load i32*, i32** %5
%62 = call i32 @fclose(i32* %61)
store i8* null, i8** %2
br label %63
63: ; preds = %60, %53, %10
%64 = load i8*, i8** %2
ret i8* %64
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [16 x i8] c"Error in fopen!\00"
@SEEK_END = global i32 0
@.str.2 = [25 x i8] c"Error in SEEK_END fseek!\00"
@SEEK_SET = global i32 0
@.str.4 = [25 x i8] c"Error in SEEK_SET fseek!\00"
@stderr = global i32 0
@.str.5 = [15 x i8] c"OUT OF MEMORY\0A\00"
@.str.6 = [43 x i8] c"Failed to read from file, or it was empty\0A\00"
define i8* @readin_file(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 %4, label %6
4: ; preds = %1
%5 = tail call i32 @print_errno(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %39
6: ; preds = %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i64 @fseek(i32* %2, i64 0, i32 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @print_errno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %37
12: ; preds = %6
%13 = tail call i64 @ftell(i32* %2)
%14 = load i32, i32* @SEEK_SET
%15 = tail call i64 @fseek(i32* %2, i64 0, i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @print_errno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %37
19: ; preds = %12
%20 = add i64 %13, 1
%21 = tail call i64 @calloc(i64 %20, i32 1)
%22 = inttoptr i64 %21 to i8*
%23 = icmp eq i64 %21, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %37
27: ; preds = %19
%28 = tail call i64 @fread(i8* %22, i64 %13, i32 1, i32* %2)
%29 = icmp slt i64 %28, 1
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0))
%33 = tail call i32 @free(i8* %22)
br label %37
34: ; preds = %27
%35 = tail call i32 @fclose(i32* %2)
%36 = getelementptr inbounds i8, i8* %22, i64 %13
store i8 0, i8* %36
br label %39
37: ; preds = %30, %24, %17, %10
%38 = tail call i32 @fclose(i32* %2)
br label %39
39: ; preds = %37, %34, %4
%40 = phi i8* [ null, %4 ], [ null, %37 ], [ %22, %34 ]
ret i8* %40
}