#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*/ outFileName;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, const char *argv[]) {
  FILE *ofp;
  char *mode = "r";
  char outFileName[256];
  ofp = fopen("/Users/Nocturne/Desktop/TTT-Pre-Internship-Exercises/Week 1 - "
              "Introduction/C/ReadAFile/ReadAFile/gundam.txt",
              mode);
  if (ofp == NULL) {
    perror("Error while opening the file.\n");
    exit(EXIT_FAILURE);
  } else {
    char *fileString = (char *)malloc(sizeof(outFileName));
    int x = 0;
    for (int i = 0; i < sizeof(outFileName); i++) {
      fileString[i] = x = fgetc(ofp);
      printf("%c", x);
    }
    printf("%s \n", fileString);
  }
  fclose(ofp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe70(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe63(%rip),%rdi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cd <main+0x5d>
lea    0xeb4(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x100,%edi
call   1060 <malloc@plt>
mov    %rax,-0x128(%rbp)
movl   $0x0,-0x12c(%rbp)
movl   $0x0,-0x130(%rbp)
movslq -0x130(%rbp),%rax
cmp    $0x100,%rax
jae    124f <main+0xdf>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x12c(%rbp)
mov    %al,%dl
mov    -0x128(%rbp),%rax
movslq -0x130(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x12c(%rbp),%esi
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x130(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x130(%rbp)
jmp    11f2 <main+0x82>
mov    -0x128(%rbp),%rsi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xe76(%rip),%rdi
lea    0xe6d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f1 <main+0x71>
mov    %rax,%r14
mov    $0x100,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
xor    %ebx,%ebx
nop
mov    %r14,%rdi
call   1060 <fgetc@plt>
mov    %al,(%r15,%rbx,1)
mov    %eax,%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    $0x100,%rbx
jne    11b0 <main+0x30>
lea    0xeb6(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe76(%rip),%rdi
call   1090 <perror@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [108 x i8] c"/Users/Nocturne/Desktop/TTT-Pre-Internship-Exercises/Week 1 - Introduction/C/ReadAFile/ReadAFile/gundam.txt\00"
@.str.2 = [31 x i8] c"Error while opening the file.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [5 x i8] c"%s \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca [256 x i8]
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %7
%12 = load i8*, i8** %7
%13 = call i32* @fopen(i8* getelementptr inbounds ([108 x i8], [108 x i8]* @.str.1, i64 0, i64 0), i8* %12)
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = call i64 @malloc(i32 256)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %9
store i32 0, i32* %10
store i32 0, i32* %11
br label %23
23: ; preds = %37, %20
%24 = load i32, i32* %11
%25 = sext i32 %24 to i64
%26 = icmp ult i64 %25, 256
br i1 %26, label %27, label %40
27: ; preds = %23
%28 = load i32*, i32** %6
%29 = call i32 @fgetc(i32* %28)
store i32 %29, i32* %10
%30 = trunc i32 %29 to i8
%31 = load i8*, i8** %9
%32 = load i32, i32* %11
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 %30, i8* %34
%35 = load i32, i32* %10
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %27
%38 = load i32, i32* %11
%39 = add nsw i32 %38, 1
store i32 %39, i32* %11
br label %23
40: ; preds = %23
%41 = load i8*, i8** %9
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %40
%44 = load i32*, i32** %6
%45 = call i32 @fclose(i32* %44)
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [108 x i8] c"/Users/Nocturne/Desktop/TTT-Pre-Internship-Exercises/Week 1 - Introduction/C/ReadAFile/ReadAFile/gundam.txt\00"
@.str.2 = [31 x i8] c"Error while opening the file.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [5 x i8] c"%s \0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([108 x i8], [108 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = tail call i64 @malloc(i32 256)
%11 = inttoptr i64 %10 to i8*
br label %15
12: ; preds = %15
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* %11)
%14 = tail call i32 @fclose(i32* %3)
ret i32 0
15: ; preds = %9, %15
%16 = phi i64 [ 0, %9 ], [ %21, %15 ]
%17 = tail call i32 @fgetc(i32* %3)
%18 = trunc i32 %17 to i8
%19 = getelementptr inbounds i8, i8* %11, i64 %16
store i8 %18, i8* %19
%20 = tail call i32 @putchar(i32 %17)
%21 = add nuw nsw i64 %16, 1
%22 = icmp eq i64 %21, 256
br i1 %22, label %12, label %15
}