#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*/ exit(int);
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);
void 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");
      exit(1);
    }
    ptr = buffer;
    while (n > 0) {
      m = write(1, ptr, n);
      if (m == 0) {
        fprintf(stderr, "write: closed\n");
        exit(1);
      }
      if (m < 0) {
        int err = errno;
        perror("write");
        if (err != EAGAIN)
          exit(1);
        sleep(1);
      } else {
        n -= m;
        ptr += m;
      }
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    -0x400(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x40c(%rbp)
cmpl   $0x0,-0x40c(%rbp)
jne    1196 <main+0x36>
jmp    12ab <main+0x14b>
cmpl   $0x0,-0x40c(%rbp)
jge    11b9 <main+0x59>
lea    0xe56(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x400(%rbp),%rax
mov    %rax,-0x408(%rbp)
cmpl   $0x0,-0x40c(%rbp)
jle    12a6 <main+0x146>
mov    -0x408(%rbp),%rsi
mov    -0x40c(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,-0x410(%rbp)
cmpl   $0x0,-0x410(%rbp)
jne    121d <main+0xbd>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x410(%rbp)
jge    1273 <main+0x113>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x414(%rbp)
lea    0xdd4(%rip),%rdi
call   1060 <perror@plt>
mov    -0x414(%rbp),%eax
mov    0x2d76(%rip),%rcx
cmp    (%rcx),%eax
je     1264 <main+0x104>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    12a1 <main+0x141>
mov    -0x410(%rbp),%ecx
mov    -0x40c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x40c(%rbp)
mov    -0x410(%rbp),%ecx
mov    -0x408(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x408(%rbp)
jmp    11c7 <main+0x67>
jmp    116b <main+0xb>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsp,%rbx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
jne    118e <main+0x2e>
xor    %edi,%edi
call   1070 <exit@plt>
mov    %eax,%ebp
lea    0xe7d(%rip),%r15
mov    0x2e2a(%rip),%r13
jmp    11b8 <main+0x58>
xor    %edi,%edi
mov    %r12,%rbx
mov    %r12,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
je     1187 <main+0x27>
test   %ebp,%ebp
js     1225 <main+0xc5>
mov    %rbx,%r12
nop
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
je     1206 <main+0xa6>
js     11e2 <main+0x82>
sub    %eax,%ebp
mov    %eax,%eax
add    %rax,%rbx
test   %ebp,%ebp
jg     11c0 <main+0x60>
jmp    11a0 <main+0x40>
mov    0x2def(%rip),%rax
mov    (%rax),%r14d
mov    %r15,%rdi
call   1060 <perror@plt>
cmp    0x0(%r13),%r14d
jne    121b <main+0xbb>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11dc <main+0x7c>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xdd4(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

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 void @main() {
%1 = alloca [1024 x i8]
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
br label %6
6: ; preds = %0, %55
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%8 = call i32 @read(i32 0, i8* %7, i32 1024)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %6
br label %56
12: ; preds = %6
%13 = load i32, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
store i8* %19, i8** %2
br label %20
20: ; preds = %54, %18
%21 = load i32, i32* %3
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %55
23: ; preds = %20
%24 = load i8*, i8** %2
%25 = load i32, i32* %3
%26 = call i32 @write(i32 1, i8* %24, i32 %25)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %33
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))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %23
%34 = load i32, i32* %4
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %46
36: ; preds = %33
%37 = load i32, i32* @errno
store i32 %37, i32* %5
%38 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* %5
%40 = load i32, i32* @EAGAIN
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %36
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %36
%45 = call i32 @sleep(i32 1)
br label %54
46: ; preds = %33
%47 = load i32, i32* %4
%48 = load i32, i32* %3
%49 = sub nsw i32 %48, %47
store i32 %49, i32* %3
%50 = load i32, i32* %4
%51 = load i8*, i8** %2
%52 = sext i32 %50 to i64
%53 = getelementptr inbounds i8, i8* %51, i64 %52
store i8* %53, i8** %2
br label %54
54: ; preds = %46, %44
br label %20
55: ; preds = %20
br label %6
56: ; preds = %11
%57 = call i32 @exit(i32 0)
unreachable
}

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 void @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 %42, label %8
5: ; preds = %38
%6 = call i32 @read(i32 0, i8* %2, i32 1024)
%7 = icmp eq i32 %6, 0
br i1 %7, label %42, 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 %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8, %38
%15 = phi i8* [ %40, %38 ], [ %2, %8 ]
%16 = phi i32 [ %39, %38 ], [ %9, %8 ]
%17 = call i32 @write(i32 1, i8* %15, i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %14
%24 = icmp slt i32 %17, 0
br i1 %24, label %25, label %34
25: ; preds = %23
%26 = load i32, i32* @errno
%27 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @EAGAIN
%29 = icmp eq i32 %26, %28
br i1 %29, label %32, label %30
30: ; preds = %25
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %25
%33 = tail call i32 @sleep(i32 1)
br label %38
34: ; preds = %23
%35 = sub nsw i32 %16, %17
%36 = zext i32 %17 to i64
%37 = getelementptr inbounds i8, i8* %15, i64 %36
br label %38
38: ; preds = %34, %32
%39 = phi i32 [ %16, %32 ], [ %35, %34 ]
%40 = phi i8* [ %15, %32 ], [ %37, %34 ]
%41 = icmp sgt i32 %39, 0
br i1 %41, label %14, label %5
42: ; preds = %5, %0
%43 = tail call i32 @exit(i32 0)
unreachable
}