#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 /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *);
size_t read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ set_flag(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
size_t write(int /*<<< orphan*/, char *, size_t);
int main() {
  set_flag(STDIN_FILENO, O_NONBLOCK);
  size_t len;
  char buffer[100] = {'\0'};
  sleep(3);
  while ((len = read(STDIN_FILENO, buffer, sizeof(buffer))) != -1) {
    if (len == 0) {
      printf("finished\n");
    } else if (len > 0) {
      if (write(STDOUT_FILENO, buffer, len) != len) {
        fprintf(stderr, "write error:%s\n", strerror(errno));
        exit(1);
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   10b0 <set_flag@plt>
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %rax,-0x10(%rbp)
cmp    $0xffffffffffffffff,%rax
je     128a <main+0xfa>
cmpq   $0x0,-0x10(%rbp)
jne    1214 <main+0x84>
lea    0xdf6(%rip),%rdi
call   1040 <printf@plt>
jmp    1285 <main+0xf5>
cmpq   $0x0,-0x10(%rbp)
jbe    1280 <main+0xf0>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    -0x80(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x10(%rbp),%rax
je     127b <main+0xeb>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x84(%rbp),%edi
mov    %rax,%rdx
lea    0xd9e(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1280 <main+0xf0>
jmp    1285 <main+0xf5>
jmp    11d3 <main+0x43>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x70,%rsp
mov    0x2e2c(%rip),%r12
mov    (%r12),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   10a0 <set_flag@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movl   $0x0,0x60(%rsp)
mov    $0x3,%edi
call   1090 <sleep@plt>
mov    %rsp,%r15
lea    0xe2e(%rip),%r14
mov    0x2e07(%rip),%r13
jmp    11f8 <main+0x78>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    (%r12),%edi
mov    %r15,%rsi
mov    $0x64,%edx
call   1050 <read@plt>
test   %rax,%rax
je     11f0 <main+0x70>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
je     125d <main+0xdd>
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <write@plt>
cmp    %rbx,%rax
je     11f8 <main+0x78>
mov    0x2d9e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdb7(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@O_NONBLOCK = global i32 0
@.str = [10 x i8] c"finished\0A\00"
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"write error:%s\0A\00"
@errno = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [100 x i8]
store i32 0, i32* %1
%4 = load i32, i32* @STDIN_FILENO
%5 = load i32, i32* @O_NONBLOCK
%6 = call i32 @set_flag(i32 %4, i32 %5)
%7 = bitcast [100 x i8]* %3 to i8*
%8 = call i32 @sleep(i32 3)
br label %9
9: ; preds = %37, %0
%10 = load i32, i32* @STDIN_FILENO
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%12 = call i64 @read(i32 %10, i8* %11, i32 100)
store i64 %12, i64* %2
%13 = icmp ne i64 %12, -1
br i1 %13, label %14, label %38
14: ; preds = %9
%15 = load i64, i64* %2
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %37
19: ; preds = %14
%20 = load i64, i64* %2
%21 = icmp ugt i64 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %19
%23 = load i32, i32* @STDOUT_FILENO
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%25 = load i64, i64* %2
%26 = call i64 @write(i32 %23, i8* %24, i64 %25)
%27 = load i64, i64* %2
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %35
29: ; preds = %22
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %22
br label %36
36: ; preds = %35, %19
br label %37
37: ; preds = %36, %17
br label %9
38: ; preds = %9
ret i32 0
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@O_NONBLOCK = global i32 0
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str.1 = [16 x i8] c"write error:%s\0A\00"
@errno = global i32 0
@str = [9 x i8] c"finished\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = load i32, i32* @STDIN_FILENO
%3 = load i32, i32* @O_NONBLOCK
%4 = tail call i32 @set_flag(i32 %2, i32 %3)
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%6 = tail call i32 @sleep(i32 3)
br label %7
7: ; preds = %16, %0
%8 = load i32, i32* @STDIN_FILENO
%9 = call i64 @read(i32 %8, i8* %5, i32 100)
switch i64 %9, label %12 [
i64 -1, label %23
i64 0, label %10
]
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %16
12: ; preds = %7
%13 = load i32, i32* @STDOUT_FILENO
%14 = call i64 @write(i32 %13, i8* %5, i64 %9)
%15 = icmp eq i64 %14, %9
br i1 %15, label %16, label %17
16: ; preds = %12, %10
br label %7
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %7
ret i32 0
}