#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 strlen(char *);
int /*<<< orphan*/ system(char *);
int main(void) {
  int len;
  char ch, file_name[25];
  FILE *fp;
  system("ls");
  printf("Enter the name of file you wish to open...\n");
  fgets(file_name, 255, stdin);
  len = strlen(file_name);
  if (file_name[len - 1] == '\n')
    file_name[len - 1] = 0;
  fp = fopen(file_name, "r");
  if (fp == NULL) {
    perror("Error while opening the file.\n");
    printf("%s", file_name);
    exit(EXIT_FAILURE);
  }
  printf("The contents of %s file are :\n", file_name);
  while ((ch = fgetc(fp)) != EOF)
    printf("%c", ch);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rdi
call   1050 <system@plt>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
mov    $0xff,%esi
call   1080 <fgets@plt>
lea    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x30(%rbp,%rax,1),%eax
cmp    $0xa,%eax
jne    11ff <main+0x6f>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x30(%rbp,%rax,1)
lea    -0x30(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    124a <main+0xba>
lea    0xe0c(%rip),%rdi
call   10a0 <perror@plt>
lea    -0x30(%rbp),%rsi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x30(%rbp),%rsi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %al,-0x9(%rbp)
movsbl %al,%eax
mov    0x2d6e(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1294 <main+0x104>
movsbl -0x9(%rbp),%esi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    125c <main+0xcc>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xe42(%rip),%rdi
call   1070 <system@plt>
lea    0xe7c(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
call   10a0 <fgets@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    11fe <main+0x4e>
movb   $0x0,-0x1(%rsp,%rax,1)
lea    0xdfe(%rip),%rsi
mov    %rsp,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1267 <main+0xb7>
mov    %rax,%rbx
lea    0xe0b(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1090 <fgetc@plt>
mov    0x2dab(%rip),%r14
cmp    (%r14),%al
je     1255 <main+0xa5>
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1090 <fgetc@plt>
cmp    (%r14),%al
jne    1240 <main+0x90>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd97(%rip),%rdi
call   10c0 <perror@plt>
lea    0xdaa(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

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

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