#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_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int main() {
  FILE *fp;
  char ch, filename[50];
  int c = 0, n = 0;
  printf("Please enter the name of the file you wish to open.\n");
  fgets(filename, 50, stdin);
  fp = fopen("Hello.txt", "r");
  if (fp == NULL) {
    perror("Error opening file.\n");
    exit(EXIT_FAILURE);
  }
  printf("The contents of the file are:\n");
  while ((ch = fgetc(fp)) != EOF) {
    if (ch == '\n') {
      n++;
    }
    printf("%c", ch);
    c++;
  }
  fclose(fp);
  printf("\n");
  printf("The number of characters in the file is %d and the number of lines "
         "is %d .\n",
         c, n);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x58(%rbp)
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x50(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    $0x32,%esi
call   1060 <fgets@plt>
lea    0xe7c(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ee <main+0x7e>
lea    0xe66(%rip),%rdi
call   1080 <perror@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%eax
mov    0x2dce(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1253 <main+0xe3>
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
jne    1233 <main+0xc3>
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
movsbl -0x11(%rbp),%esi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11fc <main+0x8c>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x54(%rbp),%esi
mov    -0x58(%rbp),%edx
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
lea    0xecc(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x32,%esi
call   1080 <fgets@plt>
lea    0xe3d(%rip),%rdi
lea    0xe40(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1264 <main+0xd4>
mov    %rax,%r14
lea    0xebf(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1070 <fgetc@plt>
mov    0x2dea(%rip),%r15
xor    %ebx,%ebx
mov    $0x0,%ebp
cmp    (%r15),%al
je     1233 <main+0xa3>
xor    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movsbl %al,%edi
xor    %eax,%eax
cmp    $0xa,%dil
sete   %al
add    %eax,%ebp
call   1030 <putchar@plt>
add    $0x1,%ebx
mov    %r14,%rdi
call   1070 <fgetc@plt>
cmp    (%r15),%al
jne    1210 <main+0x80>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xdd5(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda1(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [53 x i8] c"Please enter the name of the file you wish to open.\0A\00"
@stdin = global i32 0
@.str.1 = [10 x i8] c"Hello.txt\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [21 x i8] c"Error opening file.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [31 x i8] c"The contents of the file are:\0A\00"
@EOF = global i8 0
@.str.5 = [3 x i8] c"%c\00"
@.str.6 = [2 x i8] c"\0A\00"
@.str.7 = [76 x i8] c"The number of characters in the file is %d and the number of lines is %d .\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8
%4 = alloca [50 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
store i32 0, i32* %6
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %8, i32 50, i32 %9)
%11 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %11, i32** %2
%12 = load i32*, i32** %2
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %20
20: ; preds = %34, %18
%21 = load i32*, i32** %2
%22 = call signext i8 @fgetc(i32* %21)
store i8 %22, i8* %3
%23 = sext i8 %22 to i32
%24 = load i8, i8* @EOF
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %23, %25
br i1 %26, label %27, label %40
27: ; preds = %20
%28 = load i8, i8* %3
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 10
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
store i32 %33, i32* %6
br label %34
34: ; preds = %31, %27
%35 = load i8, i8* %3
%36 = sext i8 %35 to i32
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %36)
%38 = load i32, i32* %5
%39 = add nsw i32 %38, 1
store i32 %39, i32* %5
br label %20
40: ; preds = %20
%41 = load i32*, i32** %2
%42 = call i32 @fclose(i32* %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%44 = load i32, i32* %5
%45 = load i32, i32* %6
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.7, i64 0, i64 0), i32 %44, i32 %45)
ret i32 0
}

LLVM IR (-O2):
@stdin = global i32 0
@.str.1 = [10 x i8] c"Hello.txt\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [21 x i8] c"Error opening file.\0A\00"
@EXIT_FAILURE = global i32 0
@EOF = global i8 0
@.str.7 = [76 x i8] c"The number of characters in the file is %d and the number of lines is %d .\0A\00"
@str = [52 x i8] c"Please enter the name of the file you wish to open.\00"
@str.8 = [30 x i8] c"The contents of the file are:\00"
define i32 @main() {
%1 = alloca [50 x i8]
%2 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str, i64 0, i64 0))
%4 = load i32, i32* @stdin
%5 = call i32 @fgets(i8* %2, i32 50, i32 %4)
%6 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.8, i64 0, i64 0))
%14 = call signext i8 @fgetc(i32* %6)
%15 = load i8, i8* @EOF
%16 = icmp eq i8 %14, %15
br i1 %16, label %30, label %17
17: ; preds = %12, %17
%18 = phi i8 [ %27, %17 ], [ %14, %12 ]
%19 = phi i32 [ %24, %17 ], [ 0, %12 ]
%20 = phi i32 [ %26, %17 ], [ 0, %12 ]
%21 = sext i8 %18 to i32
%22 = icmp eq i8 %18, 10
%23 = zext i1 %22 to i32
%24 = add nuw nsw i32 %19, %23
%25 = call i32 @putchar(i32 %21)
%26 = add nuw nsw i32 %20, 1
%27 = call signext i8 @fgetc(i32* %6)
%28 = load i8, i8* @EOF
%29 = icmp eq i8 %27, %28
br i1 %29, label %30, label %17
30: ; preds = %17, %12
%31 = phi i32 [ 0, %12 ], [ %26, %17 ]
%32 = phi i32 [ 0, %12 ], [ %24, %17 ]
%33 = call i32 @fclose(i32* %6)
%34 = call i32 @putchar(i32 10)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([76 x i8], [76 x i8]* @.str.7, i64 0, i64 0), i32 %31, i32 %32)
ret i32 0
}