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

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
int write(int /*<<< orphan*/, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  int ret;
  uint8_t buf[0x1000];
  while (1) {
    ret = read(STDIN_FILENO, buf, sizeof(buf));
    if (ret == -1) {
      perror("read error");
      exit(1);
    }
    if (ret == 0) {
      break;
    }
    ret = write(STDOUT_FILENO, buf, ret);
    if (ret == -1) {
      perror("write error");
      exit(2);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
lea    -0x4020(%rbp),%rsi
mov    $0x4000,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1196 <main+0x56>
lea    0xe79(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    11a5 <main+0x65>
jmp    11e5 <main+0xa5>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    -0x4020(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e0 <main+0xa0>
lea    0xe3a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
jmp    1159 <main+0x19>
xor    %eax,%eax
add    $0x4020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x4000,%rsp
mov    0x2e85(%rip),%r14
mov    %rsp,%rbx
mov    0x2e93(%rip),%r15
nopl   (%rax)
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x4000,%edx
call   1040 <read@plt>
test   %eax,%eax
je     11a1 <main+0x61>
cmp    $0xffffffff,%eax
je     11b0 <main+0x70>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1160 <main+0x20>
lea    0xe79(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
xor    %eax,%eax
add    $0x4000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe49(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@.str = [11 x i8] c"read error\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [12 x i8] c"write error\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4096 x i32]
%4 = bitcast [4096 x i32]* %3 to i8*
%5 = getelementptr inbounds [4096 x i32], [4096 x i32]* %3, i64 0, i64 0
br label %6
6: ; preds = %12, %2
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @read(i32 %7, i32* %5, i32 16384)
switch i32 %8, label %12 [
i32 -1, label %9
i32 0, label %19
]
9: ; preds = %6
%10 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %6
%13 = load i32, i32* @STDOUT_FILENO
%14 = call i32 @write(i32 %13, i32* %5, i32 %8)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %6
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 2)
unreachable
19: ; preds = %6
ret i32 0
}