#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 */
char EOF;
int /*<<< orphan*/ braf_displayError(char *);
int /*<<< orphan*/ braf_displayErrorInArguments(char *, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
char getc(int /*<<< orphan*/ *);
char *realloc(char *, int);
char *braf_extractCodeFromFile(const char *file) {
  FILE *fileHandler = fopen(file, "r");
  if (fileHandler) {
    char *code = NULL;
    char currChar = 0;
    int charIndex = 0;
    while ((currChar = getc(fileHandler)) != EOF) {
      code = realloc(code, sizeof(char) * (charIndex + 1));
      if (!code) {
        free(code);
        code = NULL;
        braf_displayError(
            "Code parsing error. Error while allocating memory for the code to "
            "be interpreted. Memory might not be enough.");
        return NULL;
      }
      code[charIndex++] = currChar;
    }
    fclose(fileHandler);
    return code;
  } else {
    braf_displayErrorInArguments("file does not exist: ", file);
    return NULL;
  }
}


x86 assembly (clang, -O0):
<braf_extractCodeFromFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1252 <braf_extractCodeFromFile+0xe2>
movq   $0x0,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <getc@plt>
mov    %al,-0x21(%rbp)
movsbl %al,%eax
mov    0x2e24(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     123c <braf_extractCodeFromFile+0xcc>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121f <braf_extractCodeFromFile+0xaf>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
lea    0xdf5(%rip),%rdi
call   1080 <braf_displayError@plt>
movq   $0x0,-0x8(%rbp)
jmp    126a <braf_extractCodeFromFile+0xfa>
mov    -0x21(%rbp),%dl
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x28(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11ae <braf_extractCodeFromFile+0x3e>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    126a <braf_extractCodeFromFile+0xfa>
mov    -0x10(%rbp),%rsi
lea    0xe13(%rip),%rdi
call   1070 <braf_displayErrorInArguments@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<braf_extractCodeFromFile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
lea    0xe7e(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a9 <braf_extractCodeFromFile+0x39>
mov    %rax,%r14
mov    %rax,%rdi
call   1090 <getc@plt>
mov    0x2e4a(%rip),%r12
cmp    (%r12),%al
jne    11bd <braf_extractCodeFromFile+0x4d>
xor    %r15d,%r15d
jmp    11fb <braf_extractCodeFromFile+0x8b>
lea    0xec0(%rip),%rdi
mov    %rbx,%rsi
call   1070 <braf_displayErrorInArguments@plt>
xor    %r15d,%r15d
jmp    121b <braf_extractCodeFromFile+0xab>
mov    %eax,%ebp
xor    %ebx,%ebx
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0x1(%rbx),%esi
mov    %r15,%rdi
call   1050 <realloc@plt>
test   %rax,%rax
je     1205 <braf_extractCodeFromFile+0x95>
mov    %rax,%r15
mov    %bpl,(%rax,%rbx,1)
mov    %r14,%rdi
call   1090 <getc@plt>
mov    %eax,%ebp
add    $0x1,%rbx
cmp    (%r12),%al
jne    11d0 <braf_extractCodeFromFile+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    121b <braf_extractCodeFromFile+0xab>
xor    %r15d,%r15d
xor    %edi,%edi
call   1030 <free@plt>
lea    0xdec(%rip),%rdi
call   1080 <braf_displayError@plt>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i8 0
@.str.1 = [110 x i8] c"Code parsing error. Error while allocating memory for the code to be interpreted. Memory might not be enough.\00"
@.str.2 = [22 x i8] c"file does not exist: \00"
define i8* @braf_extractCodeFromFile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %45
12: ; preds = %1
store i8* null, i8** %5
store i8 0, i8* %6
store i32 0, i32* %7
br label %13
13: ; preds = %34, %12
%14 = load i32*, i32** %4
%15 = call signext i8 @getc(i32* %14)
store i8 %15, i8* %6
%16 = sext i8 %15 to i32
%17 = load i8, i8* @EOF
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %16, %18
br i1 %19, label %20, label %41
20: ; preds = %13
%21 = load i8*, i8** %5
%22 = load i32, i32* %7
%23 = add nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = mul i64 1, %24
%26 = trunc i64 %25 to i32
%27 = call i8* @realloc(i8* %21, i32 %26)
store i8* %27, i8** %5
%28 = load i8*, i8** %5
%29 = icmp ne i8* %28, null
br i1 %29, label %34, label %30
30: ; preds = %20
%31 = load i8*, i8** %5
%32 = call i32 @free(i8* %31)
store i8* null, i8** %5
%33 = call i32 @braf_displayError(i8* getelementptr inbounds ([110 x i8], [110 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %48
34: ; preds = %20
%35 = load i8, i8* %6
%36 = load i8*, i8** %5
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 %35, i8* %40
br label %13
41: ; preds = %13
%42 = load i32*, i32** %4
%43 = call i32 @fclose(i32* %42)
%44 = load i8*, i8** %5
store i8* %44, i8** %2
br label %48
45: ; preds = %1
%46 = load i8*, i8** %3
%47 = call i32 @braf_displayErrorInArguments(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %46)
store i8* null, i8** %2
br label %48
48: ; preds = %45, %41, %30
%49 = load i8*, i8** %2
ret i8* %49
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i8 0
@.str.1 = [110 x i8] c"Code parsing error. Error while allocating memory for the code to be interpreted. Memory might not be enough.\00"
@.str.2 = [22 x i8] c"file does not exist: \00"
define i8* @braf_extractCodeFromFile(i8* %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 %27, label %4
4: ; preds = %1
%5 = tail call signext i8 @getc(i32* %2)
%6 = load i8, i8* @EOF
%7 = icmp eq i8 %5, %6
br i1 %7, label %24, label %8
8: ; preds = %4, %19
%9 = phi i64 [ %12, %19 ], [ 0, %4 ]
%10 = phi i8 [ %21, %19 ], [ %5, %4 ]
%11 = phi i8* [ %14, %19 ], [ null, %4 ]
%12 = add nuw i64 %9, 1
%13 = trunc i64 %12 to i32
%14 = tail call i8* @realloc(i8* %11, i32 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %8
%17 = tail call i32 @free(i8* null)
%18 = tail call i32 @braf_displayError(i8* getelementptr inbounds ([110 x i8], [110 x i8]* @.str.1, i64 0, i64 0))
br label %29
19: ; preds = %8
%20 = getelementptr inbounds i8, i8* %14, i64 %9
store i8 %10, i8* %20
%21 = tail call signext i8 @getc(i32* %2)
%22 = load i8, i8* @EOF
%23 = icmp eq i8 %21, %22
br i1 %23, label %24, label %8
24: ; preds = %19, %4
%25 = phi i8* [ null, %4 ], [ %14, %19 ]
%26 = tail call i32 @fclose(i32* %2)
br label %29
27: ; preds = %1
%28 = tail call i32 @braf_displayErrorInArguments(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %29
29: ; preds = %16, %24, %27
%30 = phi i8* [ null, %27 ], [ null, %16 ], [ %25, %24 ]
ret i8* %30
}