#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*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, int);
int write(int /*<<< orphan*/, char *, int);
int main(void) {
  int n;
  char buf[4096];
  while ((n = read(STDIN_FILENO, buf, 4096)) > 0)
    if (write(STDOUT_FILENO, buf, n) != n)
      perror("write");
  if (n < 0)
    perror("read");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jle    11aa <main+0x6a>
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1030 <write@plt>
cmp    -0x8(%rbp),%eax
je     11a5 <main+0x65>
lea    0xe60(%rip),%rdi
call   1050 <perror@plt>
jmp    1152 <main+0x12>
cmpl   $0x0,-0x8(%rbp)
jge    11c0 <main+0x80>
lea    0xe4b(%rip),%rdi
call   1050 <perror@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    0x2e82(%rip),%r15
mov    (%r15),%edi
mov    %rsp,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11b2 <main+0x72>
mov    0x2e7d(%rip),%r12
mov    %rsp,%rbx
lea    0xe83(%rip),%r14
jmp    1196 <main+0x56>
nop
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11b2 <main+0x72>
mov    (%r12),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1180 <main+0x40>
mov    %r14,%rdi
call   1050 <perror@plt>
jmp    1180 <main+0x40>
test   %ebp,%ebp
jns    11c2 <main+0x82>
lea    0xe49(%rip),%rdi
call   1050 <perror@plt>
xor    %edi,%edi
call   1060 <exit@plt>

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

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