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

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ pipe_setup_for_binary(int /*<<< orphan*/);
scalar_t__ read(int /*<<< orphan*/, char *, int);
scalar_t__ write(int /*<<< orphan*/, char *, scalar_t__);
int main(int argc, char *argv[]) {
  char buf[1000];
  ssize_t n_read, n_written, n_written_tot;
  pipe_setup_for_binary(STDIN_FILENO);
  while ((n_read = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
    n_written_tot = 0;
    while (n_written_tot < n_read) {
      n_written =
          write(STDOUT_FILENO, buf + n_written_tot, n_read - n_written_tot);
      if (n_written <= 0)
        return 1;
      n_written_tot += n_written;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
call   1050 <pipe_setup_for_binary@plt>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
lea    -0x400(%rbp),%rsi
mov    $0x3e8,%edx
call   1040 <read@plt>
mov    %rax,-0x408(%rbp)
cmp    $0x0,%rax
jle    120b <main+0xdb>
movq   $0x0,-0x418(%rbp)
mov    -0x418(%rbp),%rax
cmp    -0x408(%rbp),%rax
jge    1206 <main+0xd6>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    -0x400(%rbp),%rsi
add    -0x418(%rbp),%rsi
mov    -0x408(%rbp),%rdx
sub    -0x418(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x410(%rbp)
cmpq   $0x0,-0x410(%rbp)
jg     11ec <main+0xbc>
movl   $0x1,-0x4(%rbp)
jmp    1212 <main+0xe2>
mov    -0x410(%rbp),%rax
add    -0x418(%rbp),%rax
mov    %rax,-0x418(%rbp)
jmp    118d <main+0x5d>
jmp    1157 <main+0x27>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f0,%rsp
mov    0x2e91(%rip),%r15
mov    (%r15),%edi
call   1050 <pipe_setup_for_binary@plt>
mov    (%r15),%edi
mov    %rsp,%rsi
mov    $0x3e8,%edx
call   1040 <read@plt>
test   %rax,%rax
jle    11b8 <main+0x88>
mov    %rax,%r13
mov    0x2e82(%rip),%r12
mov    %rsp,%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r12),%edi
lea    (%rsp,%rbx,1),%rsi
mov    %r13,%rdx
sub    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
jle    11bc <main+0x8c>
add    %rax,%rbx
cmp    %rbx,%r13
jg     1180 <main+0x50>
mov    (%r15),%edi
mov    %r14,%rsi
mov    $0x3e8,%edx
call   1040 <read@plt>
mov    %rax,%r13
test   %rax,%rax
jg     1171 <main+0x41>
xor    %eax,%eax
jmp    11c1 <main+0x91>
mov    $0x1,%eax
add    $0x3f0,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1000 x i8]
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @STDIN_FILENO
%11 = call i32 @pipe_setup_for_binary(i32 %10)
br label %12
12: ; preds = %38, %2
%13 = load i32, i32* @STDIN_FILENO
%14 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%15 = call i64 @read(i32 %13, i8* %14, i32 1000)
store i64 %15, i64* %7
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %39
17: ; preds = %12
store i64 0, i64* %9
br label %18
18: ; preds = %34, %17
%19 = load i64, i64* %9
%20 = load i64, i64* %7
%21 = icmp slt i64 %19, %20
br i1 %21, label %22, label %38
22: ; preds = %18
%23 = load i32, i32* @STDOUT_FILENO
%24 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%25 = load i64, i64* %9
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = load i64, i64* %7
%28 = load i64, i64* %9
%29 = sub nsw i64 %27, %28
%30 = call i64 @write(i32 %23, i8* %26, i64 %29)
store i64 %30, i64* %8
%31 = load i64, i64* %8
%32 = icmp sle i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %22
store i32 1, i32* %3
br label %40
34: ; preds = %22
%35 = load i64, i64* %8
%36 = load i64, i64* %9
%37 = add nsw i64 %36, %35
store i64 %37, i64* %9
br label %18
38: ; preds = %18
br label %12
39: ; preds = %12
store i32 0, i32* %3
br label %40
40: ; preds = %39, %33
%41 = load i32, i32* %3
ret i32 %41
}

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