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

/* Variables and functions */
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ clr_fl(int /*<<< orphan*/, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ set_fl(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int write(int /*<<< orphan*/, char *, int);
int main(int argc, char const *argv[]) {
  int ntowrite, nwrite;
  char *ptr;
  char buf[50000];
  ntowrite = read(STDIN_FILENO, buf, sizeof(buf));
  fprintf(stderr, "read %d bytes\n", ntowrite);
  set_fl(STDOUT_FILENO, O_NONBLOCK);
  ptr = buf;
  while (ntowrite > 0) {
    errno = 0;
    nwrite = write(STDOUT_FILENO, ptr, ntowrite);
    fprintf(stderr, "nwrite = %d, errno = %d\n", nwrite, errno);
    if (nwrite > 0) {
      ptr += nwrite;
      ntowrite -= nwrite;
    }
  }
  clr_fl(STDOUT_FILENO, O_NONBLOCK);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc370,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
lea    -0xc370(%rbp),%rsi
mov    $0xc350,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe67(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
call   1030 <set_fl@plt>
lea    -0xc370(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    123f <main+0xef>
mov    0x2df5(%rip),%rax
movl   $0x0,(%rax)
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x18(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    0x2dc4(%rip),%rax
mov    (%rax),%ecx
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
cmpl   $0x0,-0x18(%rbp)
jle    123a <main+0xea>
mov    -0x18(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    11c2 <main+0x72>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
call   1070 <clr_fl@plt>
xor    %eax,%eax
add    $0xc370,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc358,%rsp
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%r15
mov    %r15,%rsi
mov    $0xc350,%edx
call   1050 <read@plt>
mov    %eax,%ebp
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2e54(%rip),%r12
mov    (%r12),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1030 <set_fl@plt>
test   %ebp,%ebp
jle    1202 <main+0xb2>
mov    0x2e0f(%rip),%r13
lea    0xe4f(%rip),%r14
movl   $0x0,0x0(%r13)
mov    (%r12),%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
mov    %eax,%ebx
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    0x0(%r13),%ecx
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %ebx,%ebx
mov    $0x0,%eax
cmovs  %eax,%ebx
add    %rbx,%r15
sub    %ebx,%ebp
jg     11c0 <main+0x70>
mov    (%r12),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
call   1070 <clr_fl@plt>
xor    %eax,%eax
add    $0xc358,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"read %d bytes\0A\00"
@STDOUT_FILENO = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@.str.1 = [25 x i8] c"nwrite = %d, errno = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [50000 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @STDIN_FILENO
%11 = getelementptr inbounds [50000 x i8], [50000 x i8]* %9, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 50000)
store i32 %12, i32* %6
%13 = load i32, i32* @stderr
%14 = load i32, i32* %6
%15 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i32, i32* @STDOUT_FILENO
%17 = load i32, i32* @O_NONBLOCK
%18 = call i32 @set_fl(i32 %16, i32 %17)
%19 = getelementptr inbounds [50000 x i8], [50000 x i8]* %9, i64 0, i64 0
store i8* %19, i8** %8
br label %20
20: ; preds = %42, %2
%21 = load i32, i32* %6
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %43
23: ; preds = %20
store i32 0, i32* @errno
%24 = load i32, i32* @STDOUT_FILENO
%25 = load i8*, i8** %8
%26 = load i32, i32* %6
%27 = call i32 @write(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %7
%28 = load i32, i32* @stderr
%29 = load i32, i32* %7
%30 = load i32, i32* @errno
%31 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %30)
%32 = load i32, i32* %7
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %42
34: ; preds = %23
%35 = load i32, i32* %7
%36 = load i8*, i8** %8
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %8
%39 = load i32, i32* %7
%40 = load i32, i32* %6
%41 = sub nsw i32 %40, %39
store i32 %41, i32* %6
br label %42
42: ; preds = %34, %23
br label %20
43: ; preds = %20
%44 = load i32, i32* @STDOUT_FILENO
%45 = load i32, i32* @O_NONBLOCK
%46 = call i32 @clr_fl(i32 %44, i32 %45)
ret i32 0
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"read %d bytes\0A\00"
@STDOUT_FILENO = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@.str.1 = [25 x i8] c"nwrite = %d, errno = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [50000 x i8]
%4 = getelementptr inbounds [50000 x i8], [50000 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @STDIN_FILENO
%6 = call i32 @read(i32 %5, i8* %4, i32 50000)
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6)
%9 = load i32, i32* @STDOUT_FILENO
%10 = load i32, i32* @O_NONBLOCK
%11 = tail call i32 @set_fl(i32 %9, i32 %10)
%12 = icmp sgt i32 %6, 0
br i1 %12, label %13, label %27
13: ; preds = %2, %13
%14 = phi i8* [ %25, %13 ], [ %4, %2 ]
%15 = phi i32 [ %23, %13 ], [ %6, %2 ]
store i32 0, i32* @errno
%16 = load i32, i32* @STDOUT_FILENO
%17 = call i32 @write(i32 %16, i8* %14, i32 %15)
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %19)
%21 = icmp sgt i32 %17, 0
%22 = select i1 %21, i32 %17, i32 0
%23 = sub nsw i32 %15, %22
%24 = zext i32 %22 to i64
%25 = getelementptr i8, i8* %14, i64 %24
%26 = icmp sgt i32 %23, 0
br i1 %26, label %13, label %27
27: ; preds = %13, %2
%28 = load i32, i32* @STDOUT_FILENO
%29 = load i32, i32* @O_NONBLOCK
%30 = tail call i32 @clr_fl(i32 %28, i32 %29)
ret i32 0
}