#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*/ buffer;

/* Variables and functions */
int EAGAIN;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(void) {
  char buffer[1024], *ptr;
  int n, m;
  while (1) {
    n = read(0, buffer, sizeof(buffer));
    if (n == 0)
      break;
    if (n < 0) {
      perror("read");
      return 1;
    }
    ptr = buffer;
    while (n > 0) {
      m = write(1, ptr, n);
      if (m == 0) {
        fprintf(stderr, "write: closed\n");
        return 1;
      }
      if (m < 0) {
        int err = errno;
        perror("write");
        if (err != EAGAIN)
          return 1;
        sleep(1);
      } else {
        n -= m;
        ptr += m;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x410(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x41c(%rbp)
cmpl   $0x0,-0x41c(%rbp)
jne    118d <main+0x3d>
jmp    12a8 <main+0x158>
cmpl   $0x0,-0x41c(%rbp)
jge    11b2 <main+0x62>
lea    0xe5f(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12af <main+0x15f>
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
cmpl   $0x0,-0x41c(%rbp)
jle    12a3 <main+0x153>
mov    -0x418(%rbp),%rsi
mov    -0x41c(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,-0x420(%rbp)
cmpl   $0x0,-0x420(%rbp)
jne    1218 <main+0xc8>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12af <main+0x15f>
cmpl   $0x0,-0x420(%rbp)
jge    1270 <main+0x120>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x424(%rbp)
lea    0xdd9(%rip),%rdi
call   1060 <perror@plt>
mov    -0x424(%rbp),%eax
mov    0x2d7b(%rip),%rcx
cmp    (%rcx),%eax
je     1261 <main+0x111>
movl   $0x1,-0x4(%rbp)
jmp    12af <main+0x15f>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    129e <main+0x14e>
mov    -0x420(%rbp),%ecx
mov    -0x41c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x41c(%rbp)
mov    -0x420(%rbp),%ecx
mov    -0x418(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x418(%rbp)
jmp    11c0 <main+0x70>
jmp    1162 <main+0x12>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
xor    %ebx,%ebx
mov    %rsp,%r15
xor    %edi,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
je     1223 <main+0xd3>
mov    %eax,%ebp
lea    0xe8e(%rip),%r14
mov    0x2e3b(%rip),%r13
test   %ebp,%ebp
js     1237 <main+0xe7>
mov    %r15,%rbx
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
je     1209 <main+0xb9>
js     11c2 <main+0x72>
sub    %eax,%ebp
mov    %eax,%eax
add    %rax,%rbx
test   %ebp,%ebp
jg     11a0 <main+0x50>
jmp    11f0 <main+0xa0>
mov    0x2e0f(%rip),%rax
mov    (%rax),%r12d
mov    %r14,%rdi
call   1060 <perror@plt>
cmp    0x0(%r13),%r12d
jne    121e <main+0xce>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    11bc <main+0x6c>
cs nopw 0x0(%rax,%rax,1)
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    118d <main+0x3d>
jmp    1223 <main+0xd3>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc2(%rip),%rdi
call   1060 <perror@plt>
jmp    121e <main+0xce>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"write: closed\0A\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"write\00"
@EAGAIN = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
br label %7
7: ; preds = %0, %53
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%9 = call i32 @read(i32 0, i8* %8, i32 1024)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %7
br label %54
13: ; preds = %7
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %55
18: ; preds = %13
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
store i8* %19, i8** %3
br label %20
20: ; preds = %52, %18
%21 = load i32, i32* %4
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %53
23: ; preds = %20
%24 = load i8*, i8** %3
%25 = load i32, i32* %4
%26 = call i32 @write(i32 1, i8* %24, i32 %25)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %55
32: ; preds = %23
%33 = load i32, i32* %5
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %32
%36 = load i32, i32* @errno
store i32 %36, i32* %6
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* %6
%39 = load i32, i32* @EAGAIN
%40 = icmp ne i32 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %35
store i32 1, i32* %1
br label %55
42: ; preds = %35
%43 = call i32 @sleep(i32 1)
br label %52
44: ; preds = %32
%45 = load i32, i32* %5
%46 = load i32, i32* %4
%47 = sub nsw i32 %46, %45
store i32 %47, i32* %4
%48 = load i32, i32* %5
%49 = load i8*, i8** %3
%50 = sext i32 %48 to i64
%51 = getelementptr inbounds i8, i8* %49, i64 %50
store i8* %51, i8** %3
br label %52
52: ; preds = %44, %42
br label %20
53: ; preds = %20
br label %7
54: ; preds = %12
store i32 0, i32* %1
br label %55
55: ; preds = %54, %41, %29, %16
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.1 = [15 x i8] c"write: closed\0A\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"write\00"
@EAGAIN = global i32 0
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = call i32 @read(i32 0, i8* %2, i32 1024)
%4 = icmp eq i32 %3, 0
br i1 %4, label %38, label %8
5: ; preds = %34
%6 = call i32 @read(i32 0, i8* %2, i32 1024)
%7 = icmp eq i32 %6, 0
br i1 %7, label %38, label %8
8: ; preds = %0, %5
%9 = phi i32 [ %6, %5 ], [ %3, %0 ]
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %38
13: ; preds = %8, %34
%14 = phi i8* [ %36, %34 ], [ %2, %8 ]
%15 = phi i32 [ %35, %34 ], [ %9, %8 ]
%16 = call i32 @write(i32 1, i8* %14, i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = tail call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %38
21: ; preds = %13
%22 = icmp slt i32 %16, 0
br i1 %22, label %23, label %30
23: ; preds = %21
%24 = load i32, i32* @errno
%25 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @EAGAIN
%27 = icmp eq i32 %24, %26
br i1 %27, label %28, label %38
28: ; preds = %23
%29 = tail call i32 @sleep(i32 1)
br label %34
30: ; preds = %21
%31 = sub nsw i32 %15, %16
%32 = zext i32 %16 to i64
%33 = getelementptr inbounds i8, i8* %14, i64 %32
br label %34
34: ; preds = %28, %30
%35 = phi i32 [ %31, %30 ], [ %15, %28 ]
%36 = phi i8* [ %33, %30 ], [ %14, %28 ]
%37 = icmp sgt i32 %35, 0
br i1 %37, label %13, label %5
38: ; preds = %5, %23, %0, %18, %11
%39 = phi i32 [ 1, %11 ], [ 1, %18 ], [ 0, %0 ], [ 1, %23 ], [ 0, %5 ]
ret i32 %39
}