#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
void *getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int c;
  int ct = 0;
  FILE *fp;
  const char filename[] =
      "/Users/seanreed/iosProjects/week1-cont/week1-cont/problem_12_alt.txt";
  if ((fp = fopen(filename, "r"))) {
    while ((c = getc(fp)) != EOF) {
      ct++;
      printf("%c", c);
    }
    rewind(fp);
    char string[ct + 1];
    for (int i = 0; i < ct; i++) {
      string[i] = getc(fp);
    }
    fclose(fp);
    printf("\n String is\n%s", string);
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
lea    -0x70(%rbp),%rdi
lea    0xe75(%rip),%rsi
mov    $0x45,%edx
call   1060 <memcpy@plt>
lea    -0x70(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     129f <main+0x13f>
jmp    11b8 <main+0x58>
mov    -0x20(%rbp),%rdi
call   1080 <getc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%eax
je     11f2 <main+0x92>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11b8 <main+0x58>
mov    -0x20(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x78(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x90(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x80(%rbp)
movl   $0x0,-0x84(%rbp)
mov    -0x84(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1273 <main+0x113>
mov    -0x20(%rbp),%rdi
call   1080 <getc@plt>
mov    %rax,%rcx
mov    -0x90(%rbp),%rax
mov    %cl,%dl
movslq -0x84(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
jmp    1231 <main+0xd1>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x90(%rbp),%rsi
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x78(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
movaps 0xeb8(%rip),%xmm0
movaps %xmm0,-0x40(%rbp)
movaps 0xe9d(%rip),%xmm0
movaps %xmm0,-0x50(%rbp)
movaps 0xe82(%rip),%xmm0
movaps %xmm0,-0x60(%rbp)
movaps 0xe67(%rip),%xmm0
movaps %xmm0,-0x70(%rbp)
movabs $0x7478742e746c61,%rax
mov    %rax,-0x33(%rbp)
lea    0xe93(%rip),%rsi
lea    -0x70(%rbp),%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1251 <main+0xf1>
mov    %rax,%r14
mov    %rax,%rdi
call   1080 <getc@plt>
mov    0x2e12(%rip),%r15
xor    %ebx,%ebx
cmp    %eax,(%r15)
je     11f7 <main+0x97>
xor    %ebx,%ebx
nop
add    $0x1,%ebx
mov    %eax,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
call   1080 <getc@plt>
cmp    %eax,(%r15)
jne    11e0 <main+0x80>
mov    %r14,%rdi
call   1060 <rewind@plt>
mov    %rsp,%r15
mov    %ebx,%r13d
mov    %rsp,%r12
lea    0x10(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
test   %ebx,%ebx
je     1235 <main+0xd5>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r14,%rdi
call   1080 <getc@plt>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1220 <main+0xc0>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xe03(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.filename = [69 x i8] c"/Users/seanreed/iosProjects/week1-cont/week1-cont/problem_12_alt.txt\00"
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.1 = [3 x i8] c"%c\00"
@.str.2 = [15 x i8] c"\0A String is\0A%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca [69 x i8]
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%13 = bitcast [69 x i8]* %9 to i8*
%14 = getelementptr inbounds [69 x i8], [69 x i8]* %9, i64 0, i64 0
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %8
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %56
17: ; preds = %2
br label %18
18: ; preds = %24, %17
%19 = load i32*, i32** %8
%20 = call i8* @getc(i32* %19)
%21 = ptrtoint i8* %20 to i32
store i32 %21, i32* %6
%22 = load i32, i32* @EOF
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
%27 = load i32, i32* %6
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %18
29: ; preds = %18
%30 = load i32*, i32** %8
%31 = call i32 @rewind(i32* %30)
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
%34 = zext i32 %33 to i64
%35 = call i8* @llvm.stacksave()
store i8* %35, i8** %10
%36 = alloca i8, i64 %34
store i64 %34, i64* %11
store i32 0, i32* %12
br label %37
37: ; preds = %48, %29
%38 = load i32, i32* %12
%39 = load i32, i32* %7
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %51
41: ; preds = %37
%42 = load i32*, i32** %8
%43 = call i8* @getc(i32* %42)
%44 = ptrtoint i8* %43 to i8
%45 = load i32, i32* %12
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %36, i64 %46
store i8 %44, i8* %47
br label %48
48: ; preds = %41
%49 = load i32, i32* %12
%50 = add nsw i32 %49, 1
store i32 %50, i32* %12
br label %37
51: ; preds = %37
%52 = load i32*, i32** %8
%53 = call i32 @fclose(i32* %52)
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %36)
store i32 0, i32* %3
%55 = load i8*, i8** %10
br label %56
56: ; preds = %51, %2
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@__const.main.filename = [69 x i8] c"/Users/seanreed/iosProjects/week1-cont/week1-cont/problem_12_alt.txt\00"
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.2 = [15 x i8] c"\0A String is\0A%s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [69 x i8]
%4 = getelementptr inbounds [69 x i8], [69 x i8]* %3, i64 0, i64 0
%5 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %44, label %7
7: ; preds = %2
%8 = tail call i8* @getc(i32* %5)
%9 = ptrtoint i8* %8 to i64
%10 = trunc i64 %9 to i32
%11 = load i32, i32* @EOF
%12 = icmp eq i32 %11, %10
br i1 %12, label %23, label %13
13: ; preds = %7, %13
%14 = phi i32 [ %20, %13 ], [ %10, %7 ]
%15 = phi i32 [ %16, %13 ], [ 0, %7 ]
%16 = add nuw nsw i32 %15, 1
%17 = tail call i32 @putchar(i32 %14)
%18 = tail call i8* @getc(i32* %5)
%19 = ptrtoint i8* %18 to i64
%20 = trunc i64 %19 to i32
%21 = load i32, i32* @EOF
%22 = icmp eq i32 %21, %20
br i1 %22, label %23, label %13
23: ; preds = %13, %7
%24 = phi i32 [ 0, %7 ], [ %16, %13 ]
%25 = tail call i32 @rewind(i32* %5)
%26 = add nuw nsw i32 %24, 1
%27 = zext i32 %26 to i64
%28 = tail call i8* @llvm.stacksave()
%29 = alloca i8, i64 %27
%30 = icmp eq i32 %24, 0
br i1 %30, label %33, label %31
31: ; preds = %23
%32 = zext i32 %24 to i64
br label %36
33: ; preds = %36, %23
%34 = tail call i32 @fclose(i32* %5)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %29)
br label %44
36: ; preds = %31, %36
%37 = phi i64 [ 0, %31 ], [ %42, %36 ]
%38 = tail call i8* @getc(i32* %5)
%39 = ptrtoint i8* %38 to i64
%40 = trunc i64 %39 to i8
%41 = getelementptr inbounds i8, i8* %29, i64 %37
store i8 %40, i8* %41
%42 = add nuw nsw i64 %37, 1
%43 = icmp eq i64 %42, %32
br i1 %43, label %33, label %36
44: ; preds = %2, %33
ret i32 0
}