#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  FILE *fp;
  int lines = 0;
  char ch;
  char filename[10];
  printf("Please enter the filename you wish to open: ");
  gets(filename);
  fp = fopen(filename, "r");
  if (fp == NULL) {
    printf("\n\n Error: File cannot be opened");
    exit(1);
  }
  printf("\n\n File opened successfully\n\n");
  while ((ch = fgetc(fp)) != EOF) {
    if (ch == '\n') {
      lines++;
    }
  }
  fclose(fp);
  printf("\n\n File has been now closed \n\n");
  printf("\nThere are %d lines in %s\n", lines, filename);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x1f(%rbp),%rdi
call   1060 <gets@plt>
lea    -0x1f(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c4 <main+0x64>
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x15(%rbp)
movsbl %al,%eax
mov    0x2e00(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     120e <main+0xae>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    1209 <main+0xa9>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11d2 <main+0x72>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%esi
lea    -0x1f(%rbp),%rdx
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe80(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x6(%rsp),%r14
mov    %r14,%rdi
call   1070 <gets@plt>
lea    0xe90(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     121b <main+0xab>
mov    %rax,%r14
lea    0xeb6(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1060 <fgetc@plt>
mov    0x2e20(%rip),%r15
cmp    (%r15),%al
je     11e6 <main+0x76>
xor    %ebx,%ebx
nop
xor    %ecx,%ecx
cmp    $0xa,%al
sete   %cl
add    %ecx,%ebx
mov    %r14,%rdi
call   1060 <fgetc@plt>
cmp    (%r15),%al
jne    11d0 <main+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
lea    0xe4e(%rip),%rdi
lea    0x6(%rsp),%rdx
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe0d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [45 x i8] c"Please enter the filename you wish to open: \00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [32 x i8] c"\0A\0A Error: File cannot be opened\00"
@.str.3 = [30 x i8] c"\0A\0A File opened successfully\0A\0A\00"
@EOF = global i8 0
@.str.4 = [31 x i8] c"\0A\0A File has been now closed \0A\0A\00"
@.str.5 = [27 x i8] c"\0AThere are %d lines in %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8
%5 = alloca [10 x i8]
store i32 0, i32* %1
store i32 0, i32* %3
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%8 = call i32 @gets(i8* %7)
%9 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %2
%11 = load i32*, i32** %2
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %18
18: ; preds = %32, %16
%19 = load i32*, i32** %2
%20 = call signext i8 @fgetc(i32* %19)
store i8 %20, i8* %4
%21 = sext i8 %20 to i32
%22 = load i8, i8* @EOF
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %21, %23
br i1 %24, label %25, label %33
25: ; preds = %18
%26 = load i8, i8* %4
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 10
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* %3
%31 = add nsw i32 %30, 1
store i32 %31, i32* %3
br label %32
32: ; preds = %29, %25
br label %18
33: ; preds = %18
%34 = load i32*, i32** %2
%35 = call i32 @fclose(i32* %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32, i32* %3
%38 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %37, i8* %38)
ret i32 0
}

LLVM IR (-O2):
@.str = [45 x i8] c"Please enter the filename you wish to open: \00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [32 x i8] c"\0A\0A Error: File cannot be opened\00"
@EOF = global i8 0
@.str.5 = [27 x i8] c"\0AThere are %d lines in %s\0A\00"
@str = [29 x i8] c"\0A\0A File opened successfully\0A\00"
@str.6 = [30 x i8] c"\0A\0A File has been now closed \0A\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @gets(i8* %2)
%5 = call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%12 = call signext i8 @fgetc(i32* %5)
%13 = load i8, i8* @EOF
%14 = icmp eq i8 %12, %13
br i1 %14, label %24, label %15
15: ; preds = %10, %15
%16 = phi i8 [ %21, %15 ], [ %12, %10 ]
%17 = phi i32 [ %20, %15 ], [ 0, %10 ]
%18 = icmp eq i8 %16, 10
%19 = zext i1 %18 to i32
%20 = add nuw nsw i32 %17, %19
%21 = call signext i8 @fgetc(i32* %5)
%22 = load i8, i8* @EOF
%23 = icmp eq i8 %21, %22
br i1 %23, label %24, label %15
24: ; preds = %15, %10
%25 = phi i32 [ 0, %10 ], [ %20, %15 ]
%26 = call i32 @fclose(i32* %5)
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.6, i64 0, i64 0))
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %25, i8* %2)
ret i32 0
}