#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;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ putchar(char);
scalar_t__ read(int, char *, int);
int main(void) {
  int fd;
  char c;
  if ((fd = open("test.txt", O_RDONLY)) < 0) {
    perror("open");
    exit(0);
  }
  while (read(fd, &c, 1) > 0) {
    putchar(c);
  }
  read(0, &c, 1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1193 <main+0x43>
lea    0xe82(%rip),%rdi
call   1060 <perror@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1198 <main+0x48>
mov    -0x8(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jle    11c1 <main+0x71>
movsbl -0x9(%rbp),%edi
call   1030 <putchar@plt>
jmp    1198 <main+0x48>
xor    %edi,%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11c9 <main+0x79>
mov    %eax,%ebx
lea    0x7(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11ae <main+0x5e>
lea    0x7(%rsp),%r14
nopw   0x0(%rax,%rax,1)
movsbl 0x7(%rsp),%edi
call   1030 <putchar@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
jg     1190 <main+0x40>
lea    0x7(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <read@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe39(%rip),%rdi
call   1060 <perror@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
store i32 0, i32* %1
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 0)
unreachable
10: ; preds = %0
br label %11
11: ; preds = %15, %10
%12 = load i32, i32* %2
%13 = call i64 @read(i32 %12, i8* %3, i32 1)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i8, i8* %3
%17 = call i32 @putchar(i8 signext %16)
br label %11
18: ; preds = %11
%19 = call i64 @read(i32 0, i8* %3, i32 1)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = call i64 @read(i32 %3, i8* %1, i32 1)
%7 = icmp sgt i64 %6, 0
br i1 %7, label %11, label %16
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %5, %11
%12 = load i8, i8* %1
%13 = tail call i32 @putchar(i8 signext %12)
%14 = call i64 @read(i32 %3, i8* %1, i32 1)
%15 = icmp sgt i64 %14, 0
br i1 %15, label %11, label %16
16: ; preds = %11, %5
%17 = call i64 @read(i32 0, i8* %1, i32 1)
ret i32 0
}