#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(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
char *getSourceFileContents(char *filename) {
  int singleLineLength = 0, contentsSize = 5000;
  char *contents = (char *)malloc(contentsSize);
  char buffer[1024];
  contents[0] = '\0';
  FILE *sourceCode = fopen(filename, "r");
  if (sourceCode != NULL) {
    while (fgets(buffer, 1024, sourceCode) != NULL) {
      singleLineLength += strlen(buffer);
      if (singleLineLength >= contentsSize) {
        contentsSize += 5000;
        contents = realloc(contents, contentsSize);
      }
      sprintf(contents, "%s%s", contents, buffer);
    }
    sprintf(contents, "%s\n", contents);
    fclose(sourceCode);
    return contents;
  } else {
    fprintf(
        stderr,
        "Opening of BASIC file '%s' failed, are you sure this file exists?\n",
        filename);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<getSourceFileContents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x1388,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
je     129d <getSourceFileContents+0x10d>
jmp    11ea <getSourceFileContents+0x5a>
lea    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     126e <getSourceFileContents+0xde>
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
movslq -0xc(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jl     124c <getSourceFileContents+0xbc>
mov    -0x10(%rbp),%eax
add    $0x1388,%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1080 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    -0x420(%rbp),%rcx
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
jmp    11ea <getSourceFileContents+0x5a>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd8a(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
mov    -0x428(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd5a(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<getSourceFileContents>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbp
mov    $0x1388,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
movb   $0x0,(%rax)
lea    0xe45(%rip),%rsi
mov    %rbp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1280 <getSourceFileContents+0xf0>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     124f <getSourceFileContents+0xbf>
mov    $0x1388,%ebp
mov    %rsp,%r15
lea    0xe0f(%rip),%r12
xor    %ebx,%ebx
jmp    1228 <getSourceFileContents+0x98>
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   10a0 <sprintf@plt>
mov    %r15,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     124f <getSourceFileContents+0xbf>
mov    %r15,%rdi
call   1040 <strlen@plt>
movslq %ebx,%rbx
add    %rax,%rbx
cmp    %ebx,%ebp
jg     1200 <getSourceFileContents+0x70>
add    $0x1388,%ebp
mov    %r13,%rdi
mov    %ebp,%esi
call   1080 <realloc@plt>
mov    %rax,%r13
jmp    1200 <getSourceFileContents+0x70>
lea    0xdb1(%rip),%rsi
mov    %r13,%rdi
mov    %r13,%rdx
xor    %eax,%eax
call   10a0 <sprintf@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %r13,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
lea    0xd7b(%rip),%rsi
mov    %rbp,%rdx
call   1060 <fprintf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@stderr = global i32 0
@.str.3 = [67 x i8] c"Opening of BASIC file '%s' failed, are you sure this file exists?\0A\00"
define i8* @getSourceFileContents(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [1024 x i8]
%7 = alloca i32*
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 5000, i32* %4
%8 = load i32, i32* %4
%9 = call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %5
%11 = load i8*, i8** %5
%12 = getelementptr inbounds i8, i8* %11, i64 0
store i8 0, i8* %12
%13 = load i8*, i8** %2
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %51
17: ; preds = %1
br label %18
18: ; preds = %39, %17
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%20 = load i32*, i32** %7
%21 = call i32* @fgets(i8* %19, i32 1024, i32* %20)
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %44
23: ; preds = %18
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%25 = call i64 @strlen(i8* %24)
%26 = load i32, i32* %3
%27 = sext i32 %26 to i64
%28 = add nsw i64 %27, %25
%29 = trunc i64 %28 to i32
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = load i32, i32* %4
%32 = icmp sge i32 %30, %31
br i1 %32, label %33, label %39
33: ; preds = %23
%34 = load i32, i32* %4
%35 = add nsw i32 %34, 5000
store i32 %35, i32* %4
%36 = load i8*, i8** %5
%37 = load i32, i32* %4
%38 = call i8* @realloc(i8* %36, i32 %37)
store i8* %38, i8** %5
br label %39
39: ; preds = %33, %23
%40 = load i8*, i8** %5
%41 = load i8*, i8** %5
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%43 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %40, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %41, i8* %42)
br label %18
44: ; preds = %18
%45 = load i8*, i8** %5
%46 = load i8*, i8** %5
%47 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %45, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %46)
%48 = load i32*, i32** %7
%49 = call i32 @fclose(i32* %48)
%50 = load i8*, i8** %5
ret i8* %50
51: ; preds = %1
%52 = load i32, i32* @stderr
%53 = load i8*, i8** %2
%54 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.3, i64 0, i64 0), i8* %53)
%55 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [5 x i8] c"%s%s\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@stderr = global i32 0
@.str.3 = [67 x i8] c"Opening of BASIC file '%s' failed, are you sure this file exists?\0A\00"
define i8* @getSourceFileContents(i8* %0) {
%2 = alloca [1024 x i8]
%3 = tail call i64 @malloc(i32 5000)
%4 = inttoptr i64 %3 to i8*
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
store i8 0, i8* %4
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %34, label %8
8: ; preds = %1
%9 = call i32* @fgets(i8* %5, i32 1024, i32* %6)
%10 = icmp eq i32* %9, null
br i1 %10, label %30, label %11
11: ; preds = %8, %24
%12 = phi i64 [ %18, %24 ], [ 0, %8 ]
%13 = phi i8* [ %26, %24 ], [ %4, %8 ]
%14 = phi i32 [ %25, %24 ], [ 5000, %8 ]
%15 = call i64 @strlen(i8* %5)
%16 = shl i64 %12, 32
%17 = ashr exact i64 %16, 32
%18 = add nsw i64 %15, %17
%19 = trunc i64 %18 to i32
%20 = icmp sgt i32 %14, %19
br i1 %20, label %24, label %21
21: ; preds = %11
%22 = add nsw i32 %14, 5000
%23 = call i8* @realloc(i8* %13, i32 %22)
br label %24
24: ; preds = %21, %11
%25 = phi i32 [ %22, %21 ], [ %14, %11 ]
%26 = phi i8* [ %23, %21 ], [ %13, %11 ]
%27 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %26, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %26, i8* %5)
%28 = call i32* @fgets(i8* %5, i32 1024, i32* %6)
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %11
30: ; preds = %24, %8
%31 = phi i8* [ %4, %8 ], [ %26, %24 ]
%32 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %31, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = call i32 @fclose(i32* %6)
ret i8* %31
34: ; preds = %1
%35 = load i32, i32* @stderr
%36 = tail call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%37 = tail call i32 @exit(i32 0)
unreachable
}