#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 *, ...);
scalar_t__ read(int, char *, int);
int main() {
  char buffer[20], buffer_help[200];
  int fd = open("a.txt", O_RDONLY);
  ssize_t read_return_value;
  if (fd < 0) {
    perror("open");
    return 1;
  }
  do {
    read_return_value = read(fd, buffer, 20 - 1);
    printf("%s", buffer);
    if (read_return_value == 0) {
      buffer[19] = '\0';
    }
  } while (read_return_value != 0);
  printf("\n");
  if (read_return_value < 0) {
    perror("read");
    return 2;
  }
  int close_return_value = close(fd);
  if (close_return_value < 0) {
    perror("close");
    return 3;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xf4(%rbp)
cmpl   $0x0,-0xf4(%rbp)
jge    11a2 <main+0x52>
lea    0xe75(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    126a <main+0x11a>
jmp    11a7 <main+0x57>
mov    -0xf4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x13,%edx
call   1050 <read@plt>
mov    %rax,-0x100(%rbp)
lea    -0x20(%rbp),%rsi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpq   $0x0,-0x100(%rbp)
jne    11e6 <main+0x96>
movb   $0x0,-0xd(%rbp)
jmp    11eb <main+0x9b>
cmpq   $0x0,-0x100(%rbp)
jne    11a7 <main+0x57>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpq   $0x0,-0x100(%rbp)
jge    122d <main+0xdd>
lea    0xdf4(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    126a <main+0x11a>
mov    -0xf4(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x104(%rbp)
cmpl   $0x0,-0x104(%rbp)
jge    1263 <main+0x113>
lea    0xdc3(%rip),%rdi
call   1070 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    126a <main+0x11a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
lea    0xe86(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11df <main+0x7f>
mov    %eax,%r15d
mov    %rsp,%rbx
lea    0xe7b(%rip),%r14
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x13,%edx
call   1060 <read@plt>
mov    %rax,%rbp
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
test   %rbp,%rbp
jne    1190 <main+0x30>
movb   $0x0,0x13(%rsp)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
js     11f2 <main+0x92>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe20(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    11d4 <main+0x74>
lea    0xe15(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%eax
jmp    11d4 <main+0x74>

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 = [3 x i8] c"%s\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [6 x i8] c"close\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [20 x i8]
%3 = alloca [200 x i8]
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = 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 %42
13: ; preds = %0
br label %14
14: ; preds = %25, %13
%15 = load i32, i32* %4
%16 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%17 = call i64 @read(i32 %15, i8* %16, i32 19)
store i64 %17, i64* %5
%18 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i64, i64* %5
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %14
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 19
store i8 0, i8* %23
br label %24
24: ; preds = %22, %14
br label %25
25: ; preds = %24
%26 = load i64, i64* %5
%27 = icmp ne i64 %26, 0
br i1 %27, label %14, label %28
28: ; preds = %25
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%30 = load i64, i64* %5
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 2, i32* %1
br label %42
34: ; preds = %28
%35 = load i32, i32* %4
%36 = call i32 @close(i32 %35)
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
store i32 3, i32* %1
br label %42
41: ; preds = %34
store i32 0, i32* %1
br label %42
42: ; preds = %41, %39, %32, %11
%43 = load i32, i32* %1
ret i32 %43
}

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 = [3 x i8] c"%s\00"
@.str.5 = [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 %19
8: ; preds = %0, %8
%9 = call i64 @read(i32 %4, i8* %2, i32 19)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%11 = icmp eq i64 %9, 0
br i1 %11, label %12, label %8
12: ; preds = %8
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 19
store i8 0, i8* %13
%14 = call i32 @putchar(i32 10)
%15 = call i32 @close(i32 %4)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %19
19: ; preds = %17, %12, %6
%20 = phi i32 [ 1, %6 ], [ 3, %17 ], [ 0, %12 ]
ret i32 %20
}