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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, char *, int);
int main() {
  char buffer[20];
  int fd = open("a.txt", O_RDONLY);
  if (fd < 0) {
    perror("open");
    return 1;
  }
  ssize_t read_return_value = read(fd, buffer, 20 - 1);
  if (read_return_value < 0) {
    perror("read");
  }
  buffer[19] = '0';
  printf("%s\n", buffer);
  int close_return_value = close(fd);
  if (close_return_value < 0) {
    perror("close");
    return 2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1199 <main+0x49>
lea    0xe7e(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    120d <main+0xbd>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x13,%edx
call   1050 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11c5 <main+0x75>
lea    0xe4b(%rip),%rdi
call   1070 <perror@plt>
movb   $0x30,-0xd(%rbp)
lea    -0x20(%rbp),%rsi
lea    0xe3c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1206 <main+0xb6>
lea    0xe1f(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    120d <main+0xbd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11ad <main+0x5d>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x13,%edx
call   1050 <read@plt>
test   %rax,%rax
js     11c0 <main+0x70>
movb   $0x30,0x13(%rsp)
lea    0xe80(%rip),%rdi
mov    %rsp,%rsi
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
js     11ce <main+0x7e>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe52(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    11a7 <main+0x57>
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
jmp    1184 <main+0x34>
lea    0xe3f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%eax
jmp    11a7 <main+0x57>

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

LLVM IR (-O2):
@.str = [6 x i8] c"a.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [6 x i8] c"close\00"
define i32 @main() {
%1 = alloca [20 x i8]
%2 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %20
8: ; preds = %0
%9 = call i64 @read(i32 %4, i8* %2, i32 19)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 19
store i8 48, i8* %14
%15 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%16 = call i32 @close(i32 %4)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %20
20: ; preds = %18, %13, %6
%21 = phi i32 [ 1, %6 ], [ 2, %18 ], [ 0, %13 ]
ret i32 %21
}