#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_SYNC;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ err_sys(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ set_fl(int /*<<< orphan*/, int /*<<< orphan*/);
int write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  int n;
  char buf[4096];
  set_fl(STDOUT_FILENO, O_SYNC);
  while ((n = read(STDIN_FILENO, buf, 4096)) > 0) {
    if (write(STDOUT_FILENO, buf, n) != n) {
      err_sys("write error");
    }
  }
  if (n < 0) {
    err_sys("read error");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e90(%rip),%rax
mov    (%rax),%edi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
call   1030 <set_fl@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    11c8 <main+0x88>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <write@plt>
cmp    -0x14(%rbp),%eax
je     11c3 <main+0x83>
lea    0xe42(%rip),%rdi
call   1040 <err_sys@plt>
jmp    1170 <main+0x30>
cmpl   $0x0,-0x14(%rbp)
jge    11de <main+0x9e>
lea    0xe33(%rip),%rdi
call   1040 <err_sys@plt>
xor    %eax,%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    0x2e9a(%rip),%r15
mov    (%r15),%edi
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
call   1030 <set_fl@plt>
mov    0x2e62(%rip),%r12
mov    (%r12),%edi
mov    %rsp,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11d2 <main+0x92>
mov    %rsp,%rbx
lea    0xe71(%rip),%r14
jmp    11b7 <main+0x77>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r12),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11d2 <main+0x92>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <write@plt>
cmp    %ebp,%eax
je     11a0 <main+0x60>
mov    %r14,%rdi
call   1040 <err_sys@plt>
jmp    11a0 <main+0x60>
test   %ebp,%ebp
jns    11e2 <main+0xa2>
lea    0xe2f(%rip),%rdi
call   1040 <err_sys@plt>
xor    %eax,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@O_SYNC = global i32 0
@STDIN_FILENO = global i32 0
@.str = [12 x i8] c"write error\00"
@.str.1 = [11 x i8] c"read error\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @STDOUT_FILENO
%6 = load i32, i32* @O_SYNC
%7 = tail call i32 @set_fl(i32 %5, i32 %6)
%8 = load i32, i32* @STDIN_FILENO
%9 = call i32 @read(i32 %8, i8* %4, i32 4096)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %2, %18
%12 = phi i32 [ %20, %18 ], [ %9, %2 ]
%13 = load i32, i32* @STDOUT_FILENO
%14 = call i32 @write(i32 %13, i8* %4, i32 %12)
%15 = icmp eq i32 %14, %12
br i1 %15, label %18, label %16
16: ; preds = %11
%17 = tail call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %11
%19 = load i32, i32* @STDIN_FILENO
%20 = call i32 @read(i32 %19, i8* %4, i32 4096)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %11, label %22
22: ; preds = %18, %2
%23 = phi i32 [ %9, %2 ], [ %20, %18 ]
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
ret i32 0
}