#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 int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, int);
int write(int, char *, int);
int main(void) {
  char buf[1024];
  int len;
  while ((len = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
    if (write(1, buf, len) != len) {
      perror("write");
      return 1;
    }
  }
  if (len < 0) {
    perror("read");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e97(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jle    11ab <main+0x7b>
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x414(%rbp),%eax
je     11a6 <main+0x76>
lea    0xe6b(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11d7 <main+0xa7>
jmp    1142 <main+0x12>
cmpl   $0x0,-0x414(%rbp)
jge    11d0 <main+0xa0>
lea    0xe47(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11d7 <main+0xa7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    0x2e9c(%rip),%r15
mov    %rsp,%rbx
lea    0xeb2(%rip),%r14
xchg   %ax,%ax
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    117b <main+0x4b>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1150 <main+0x20>
jmp    1196 <main+0x66>
xor    %eax,%eax
test   %ebp,%ebp
js     118f <main+0x5f>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe70(%rip),%r14
mov    %r14,%rdi
call   1050 <perror@plt>
mov    $0x1,%eax
jmp    1181 <main+0x51>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [6 x i8] c"write\00"
@.str.1 = [5 x i8] c"read\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i32
store i32 0, i32* %1
br label %4
4: ; preds = %17, %0
%5 = load i32, i32* @STDIN_FILENO
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%7 = call i32 @read(i32 %5, i8* %6, i32 1024)
store i32 %7, i32* %3
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %18
9: ; preds = %4
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%11 = load i32, i32* %3
%12 = call i32 @write(i32 1, i8* %10, i32 %11)
%13 = load i32, i32* %3
%14 = icmp ne i32 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %24
17: ; preds = %9
br label %4
18: ; preds = %4
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = 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 %24
23: ; preds = %18
store i32 0, i32* %1
br label %24
24: ; preds = %23, %21, %15
%25 = load i32, i32* %1
ret i32 %25
}

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