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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int main() {
  char *filename = "hello.txt";
  char buffer[1024];
  int fd = open(filename, O_RDONLY);
  int size = 0;
  if (fd == -1) {
    printf("Error in open: errno=%d.\n", errno);
  } else {
    printf("open() successful.\n");
    do {
      size = read(fd, buffer, 1024);
      printf("Read: %d bytes.\n", size);
      if (size == -1) {
        printf("Error in read: errno=%d.\n", errno);
        break;
      }
      buffer[size] = '\0';
      printf("%s", buffer);
    } while (size > 0);
    if (close(fd))
      printf("Error in close: errno=%d.\n", errno);
    else
      printf("close() successful.\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x414(%rbp)
movl   $0x0,-0x418(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
jne    11a8 <main+0x68>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1285 <main+0x145>
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x418(%rbp)
mov    -0x418(%rbp),%esi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0xffffffff,-0x418(%rbp)
jne    1210 <main+0xd0>
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1241 <main+0x101>
movslq -0x418(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x418(%rbp)
jg     11b6 <main+0x76>
mov    -0x414(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     1272 <main+0x132>
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1280 <main+0x140>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1285 <main+0x145>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11dd <main+0x8d>
mov    %eax,%r12d
lea    0xeea(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
lea    0xe92(%rip),%r14
lea    0xeb6(%rip),%r15
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0xffffffff,%ebp
je     11ef <main+0x9f>
movslq %ebp,%rax
movb   $0x0,(%rsp,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebp,%ebp
jg     11a0 <main+0x50>
jmp    1206 <main+0xb6>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    0xe1d(%rip),%rdi
jmp    1223 <main+0xd3>
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    0xe36(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12d,%edi
call   1050 <close@plt>
test   %rax,%rax
je     122c <main+0xdc>
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
lea    0xe2f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1238 <main+0xe8>
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"hello.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"Error in open: errno=%d.\0A\00"
@errno = global i32 0
@.str.2 = [20 x i8] c"open() successful.\0A\00"
@.str.3 = [17 x i8] c"Read: %d bytes.\0A\00"
@.str.4 = [26 x i8] c"Error in read: errno=%d.\0A\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [27 x i8] c"Error in close: errno=%d.\0A\00"
@.str.7 = [21 x i8] c"close() successful.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %2
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %4
store i32 0, i32* %5
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load i32, i32* @errno
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %46
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %33, %14
%17 = load i32, i32* %4
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 1024)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %16
%25 = load i32, i32* @errno
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %25)
br label %36
27: ; preds = %16
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %29
store i8 0, i8* %30
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %27
%34 = load i32, i32* %5
%35 = icmp sgt i32 %34, 0
br i1 %35, label %16, label %36
36: ; preds = %33, %24
%37 = load i32, i32* %4
%38 = call i64 @close(i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i32, i32* @errno
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %41)
br label %45
43: ; preds = %36
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
br label %45
45: ; preds = %43, %40
br label %46
46: ; preds = %45, %11
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"hello.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [26 x i8] c"Error in open: errno=%d.\0A\00"
@errno = global i32 0
@.str.3 = [17 x i8] c"Read: %d bytes.\0A\00"
@.str.4 = [26 x i8] c"Error in read: errno=%d.\0A\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [27 x i8] c"Error in close: errno=%d.\0A\00"
@str = [19 x i8] c"open() successful.\00"
@str.8 = [20 x i8] c"close() successful.\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @errno
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %31
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %11
11: ; preds = %18, %9
%12 = call i32 @read(i32 %4, i8* %2, i32 1024)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %12, -1
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @errno
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %16)
br label %23
18: ; preds = %11
%19 = sext i32 %12 to i64
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %2)
%22 = icmp sgt i32 %12, 0
br i1 %22, label %11, label %23
23: ; preds = %18, %15
%24 = call i64 @close(i32 %4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = load i32, i32* @errno
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %27)
br label %31
29: ; preds = %23
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.8, i64 0, i64 0))
br label %31
31: ; preds = %26, %29, %6
ret i32 0
}