#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);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int main(int argc, char *argv[]) {
  FILE *fp;
  int count = 0;
  char filename[200];
  char str[200];
  printf("Please enter the name of the file you wish to open: ");
  gets(filename);
  printf("Opening %s", filename);
  fp = fopen(filename, "a");
  if (fp == NULL) {
    printf("ERROR");
    exit(1);
  }
  printf("\nEnter new text, type exit() to save and quit\n");
  while (strcmp(gets(str), "exit()") != 0) {
    fprintf(fp, "\n%s", str);
  }
  fclose(fp);
  fp = fopen(filename, "r");
  printf("\n== File Content ==\n");
  while (fgets(filename, 200, fp) != NULL) {
    printf("\n%s", filename);
    count++;
  }
  printf("\n================\n");
  printf("\nLine Count: %d", count);
  printf("\n================\n");
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xf0(%rbp),%rdi
call   1080 <gets@plt>
lea    -0xf0(%rbp),%rsi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xf0(%rbp),%rdi
lea    0xe63(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1209 <main+0x89>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x1c0(%rbp),%rdi
call   1080 <gets@plt>
mov    %eax,%edi
lea    0xe4b(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1257 <main+0xd7>
mov    -0x18(%rbp),%rdi
lea    -0x1c0(%rbp),%rdx
lea    0xe31(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1217 <main+0x97>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0xf0(%rbp),%rdi
lea    0xe14(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xf0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xc8,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     12c7 <main+0x147>
lea    -0xf0(%rbp),%rsi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1285 <main+0x105>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%esi
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x198,%rsp
lea    0xe5c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1090 <gets@plt>
lea    0xe78(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe72(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1308 <main+0x178>
mov    %rax,%rbx
lea    0xe7c(%rip),%rdi
call   1030 <puts@plt>
lea    0xd0(%rsp),%rdi
call   1090 <gets@plt>
lea    0xe46(%rip),%rsi
mov    %eax,%edi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1255 <main+0xc5>
lea    0xe3a(%rip),%r14
lea    0xd0(%rsp),%rbp
lea    0xe24(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1080 <fprintf@plt>
mov    %rbp,%rdi
call   1090 <gets@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1230 <main+0xa0>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xdef(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r15
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
mov    $0xc8,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12c7 <main+0x137>
lea    0xdb5(%rip),%r14
mov    %rsp,%rbx
xor    %ebp,%ebp
nop
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
mov    $0xc8,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    12a0 <main+0x110>
jmp    12c9 <main+0x139>
xor    %ebp,%ebp
lea    0xdd7(%rip),%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
lea    0xd76(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x198,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd33(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [53 x i8] c"Please enter the name of the file you wish to open: \00"
@.str.1 = [11 x i8] c"Opening %s\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [6 x i8] c"ERROR\00"
@.str.4 = [47 x i8] c"\0AEnter new text, type exit() to save and quit\0A\00"
@.str.5 = [7 x i8] c"exit()\00"
@.str.6 = [4 x i8] c"\0A%s\00"
@.str.7 = [2 x i8] c"r\00"
@.str.8 = [21 x i8] c"\0A== File Content ==\0A\00"
@.str.9 = [19 x i8] c"\0A================\0A\00"
@.str.10 = [16 x i8] c"\0ALine Count: %d\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 [200 x i8]
%9 = alloca [200 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%12 = call i32 @gets(i8* %11)
%13 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %16, i32** %6
%17 = load i32*, i32** %6
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0))
br label %24
24: ; preds = %29, %22
%25 = getelementptr inbounds [200 x i8], [200 x i8]* %9, i64 0, i64 0
%26 = call i32 @gets(i8* %25)
%27 = call i64 @strcmp(i32 %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i32*, i32** %6
%31 = getelementptr inbounds [200 x i8], [200 x i8]* %9, i64 0, i64 0
%32 = call i32 @fprintf(i32* %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %31)
br label %24
33: ; preds = %24
%34 = load i32*, i32** %6
%35 = call i32 @fclose(i32* %34)
%36 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
store i32* %37, i32** %6
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0))
br label %39
39: ; preds = %44, %33
%40 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%41 = load i32*, i32** %6
%42 = call i32* @fgets(i8* %40, i32 200, i32* %41)
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %49
44: ; preds = %39
%45 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %45)
%47 = load i32, i32* %7
%48 = add nsw i32 %47, 1
store i32 %48, i32* %7
br label %39
49: ; preds = %39
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0))
%51 = load i32, i32* %7
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0), i32 %51)
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0))
%54 = load i32*, i32** %6
%55 = call i32 @fclose(i32* %54)
ret i32 0
}

LLVM IR (-O2):
@.str = [53 x i8] c"Please enter the name of the file you wish to open: \00"
@.str.1 = [11 x i8] c"Opening %s\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [6 x i8] c"ERROR\00"
@.str.5 = [7 x i8] c"exit()\00"
@.str.6 = [4 x i8] c"\0A%s\00"
@.str.7 = [2 x i8] c"r\00"
@.str.10 = [16 x i8] c"\0ALine Count: %d\00"
@str = [46 x i8] c"\0AEnter new text, type exit() to save and quit\00"
@str.11 = [20 x i8] c"\0A== File Content ==\00"
@str.13 = [18 x i8] c"\0A================\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [200 x i8]
%4 = alloca [200 x i8]
%5 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @gets(i8* %5)
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%10 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%17 = call i32 @gets(i8* %6)
%18 = call i64 @strcmp(i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %15, %20
%21 = call i32 @fprintf(i32* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %6)
%22 = call i32 @gets(i8* %6)
%23 = call i64 @strcmp(i32 %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %20
25: ; preds = %20, %15
%26 = call i32 @fclose(i32* %10)
%27 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.11, i64 0, i64 0))
%29 = call i32* @fgets(i8* %5, i32 200, i32* %27)
%30 = icmp eq i32* %29, null
br i1 %30, label %37, label %31
31: ; preds = %25, %31
%32 = phi i32 [ %34, %31 ], [ 0, %25 ]
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %5)
%34 = add nuw nsw i32 %32, 1
%35 = call i32* @fgets(i8* %5, i32 200, i32* %27)
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %31
37: ; preds = %31, %25
%38 = phi i32 [ 0, %25 ], [ %34, %31 ]
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.13, i64 0, i64 0))
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0), i32 %38)
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.13, i64 0, i64 0))
%42 = call i32 @fclose(i32* %27)
ret i32 0
}