#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ stdout;
int main() {
  char file_name[50];
  char buf[1025];
  int fd;
  printf("Enter filename:\n");
  fflush(stdout);
  scanf("%s", file_name);
  fd = open(file_name, O_RDONLY);
  if (fd == -1) {
    printf("Error: %d\n", errno);
    return -1;
  }
  if (read(fd, buf, sizeof(buf)) < 0) {
    printf("Error: %d\n", errno);
    close(fd);
    return -1;
  }
  printf("%s\n", buf);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
lea    -0x40(%rbp),%rsi
lea    0xe78(%rip),%rdi
call   1070 <scanf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x454(%rbp)
cmpl   $0xffffffff,-0x454(%rbp)
jne    11e6 <main+0x86>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <main+0xfc>
mov    -0x454(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    $0x401,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jge    1235 <main+0xd5>
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x454(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <main+0xfc>
lea    -0x450(%rbp),%rsi
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x454(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x450,%rsp
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
lea    0xe67(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1080 <scanf@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     11e7 <main+0x77>
mov    %eax,%ebx
lea    0x40(%rsp),%rsi
mov    %eax,%edi
mov    $0x401,%edx
call   1060 <read@plt>
test   %rax,%rax
js     1200 <main+0x90>
lea    0x40(%rsp),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    1223 <main+0xb3>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    0xe0c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    121e <main+0xae>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    0xdf3(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x450,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Enter filename:\0A\00"
@stdout = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@O_RDONLY = global i32 0
@.str.2 = [11 x i8] c"Error: %d\0A\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [50 x i8]
%3 = alloca [1025 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @stdout
%7 = call i32 @fflush(i32 %6)
%8 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = load i32, i32* @errno
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %16)
store i32 -1, i32* %1
br label %33
18: ; preds = %0
%19 = load i32, i32* %4
%20 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%21 = call i64 @read(i32 %19, i8* %20, i32 1025)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i32, i32* @errno
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %4
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %1
br label %33
28: ; preds = %18
%29 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %4
%32 = call i32 @close(i32 %31)
store i32 0, i32* %1
br label %33
33: ; preds = %28, %23, %15
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@stdout = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@O_RDONLY = global i32 0
@.str.2 = [11 x i8] c"Error: %d\0A\00"
@errno = global i32 0
@str = [16 x i8] c"Enter filename:\00"
define i32 @main() {
%1 = alloca [50 x i8]
%2 = alloca [1025 x i8]
%3 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%6 = load i32, i32* @stdout
%7 = tail call i32 @fflush(i32 %6)
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %3, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = load i32, i32* @errno
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %13)
br label %25
15: ; preds = %0
%16 = call i64 @read(i32 %10, i8* %4, i32 1025)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @errno
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = call i32 @close(i32 %10)
br label %25
22: ; preds = %15
%23 = call i32 @puts(i8* %4)
%24 = call i32 @close(i32 %10)
br label %25
25: ; preds = %22, %18, %12
%26 = phi i32 [ -1, %12 ], [ -1, %18 ], [ 0, %22 ]
ret i32 %26
}