#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 size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
size_t read(int, char *, int);
int /*<<< orphan*/ stderr;
int main(void) {
  char finput[] = "/etc/fstab";
  int fd;
  ssize_t nread;
  char buffer[1024 + 1];
  if ((fd = open(finput, O_RDONLY)) < 0) {
    fprintf(stderr, "Err. open file\n");
    exit(EXIT_FAILURE);
  }
  if ((nread = read(fd, buffer, 1024)) < 0) {
    fprintf(stderr, "Err. read file\n");
    exit(EXIT_FAILURE);
  }
  buffer[nread] = '\0';
  printf("%s", buffer);
  close(fd);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe87(%rip),%rax
mov    %rax,-0xf(%rbp)
mov    0xe84(%rip),%ax
mov    %ax,-0x7(%rbp)
mov    0xe7c(%rip),%al
mov    %al,-0x5(%rbp)
lea    -0xf(%rbp),%rdi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d2 <main+0x72>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jae    1217 <main+0xb7>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rsi
lea    0xdfa(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x418,%rsp
movabs $0x7473662f6374652f,%rax
66 73 74
mov    %rax,(%rsp)
movl   $0x626174,0x7(%rsp)
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11da <main+0x7a>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xe64(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe21(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@__const.main.finput = [11 x i8] c"/etc/fstab\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"Err. open file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"Err. read file\0A\00"
@.str.2 = [3 x i8] c"%s\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [11 x i8]
%3 = alloca i32
%4 = alloca i64
%5 = alloca [1025 x i8]
store i32 0, i32* %1
%6 = bitcast [11 x i8]* %2 to i8*
%7 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = load i32, i32* %3
%18 = getelementptr inbounds [1025 x i8], [1025 x i8]* %5, i64 0, i64 0
%19 = call i64 @read(i32 %17, i8* %18, i32 1024)
store i64 %19, i64* %4
%20 = icmp ult i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %16
%27 = load i64, i64* %4
%28 = getelementptr inbounds [1025 x i8], [1025 x i8]* %5, i64 0, i64 %27
store i8 0, i8* %28
%29 = getelementptr inbounds [1025 x i8], [1025 x i8]* %5, i64 0, i64 0
%30 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %3
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* @EXIT_SUCCESS
ret i32 %33
}

LLVM IR (-O2):
@__const.main.finput = [11 x i8] c"/etc/fstab\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"Err. open file\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [11 x i8]
%2 = alloca [1025 x i8]
%3 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = call i64 @read(i32 %6, i8* %4, i32 1024)
%15 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%17 = call i32 @close(i32 %6)
%18 = load i32, i32* @EXIT_SUCCESS
ret i32 %18
}