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

/* Variables and functions */
int BUFSIZ;
scalar_t__ EINTR;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
scalar_t__ errno;
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, ...);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, int);
int write(int, char *, int);
int main(void) {
  char buf[BUFSIZ];
  int i, fl, got;
  if ((fl = fcntl(0, F_GETFL)) < 0)
    perror("stdin");
  fcntl(0, F_SETFL, fl | O_NONBLOCK);
  got = read(0, buf, sizeof buf);
  fcntl(0, F_SETFL, fl);
  for (i = 0; i < got;) {
    int put;
    put = write(1, buf + i, got - i);
    if (put < 0) {
      if (errno == EINTR)
        continue;
      return 2;
    }
    i += put;
  }
  return got ? 0 : 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11a9 <main+0x69>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    0x2e2c(%rip),%rax
or     (%rax),%edx
xor    %edi,%edi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    %eax,%edx
xor    %edi,%edi
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
xor    %edi,%edi
mov    $0x0,%al
call   1030 <fcntl@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    126b <main+0x12b>
mov    -0x38(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x24(%rbp),%edx
sub    -0x1c(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    125d <main+0x11d>
mov    0x2d96(%rip),%rax
mov    (%rax),%rax
mov    0x2d7c(%rip),%rcx
cmp    (%rcx),%rax
jne    124a <main+0x10a>
jmp    11f7 <main+0xb7>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1285 <main+0x145>
mov    -0x28(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11f7 <main+0xb7>
mov    -0x24(%rbp),%edx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e7b(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r15
mov    %r14,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %eax,%r12d
test   %eax,%eax
js     1229 <main+0xe9>
mov    0x2e48(%rip),%r13
mov    0x0(%r13),%esi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
or     %r12d,%edx
xor    %ebx,%ebx
xor    %edi,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
xor    %edi,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1050 <read@plt>
mov    %eax,%r14d
mov    0x0(%r13),%esi
xor    %edi,%edi
mov    %r12d,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %r14d,%r14d
jle    1212 <main+0xd2>
mov    0x2df1(%rip),%r12
mov    0x2dda(%rip),%r13
jmp    11e7 <main+0xa7>
add    %eax,%ebx
cmp    %ebx,%r14d
jle    1212 <main+0xd2>
movslq %ebx,%rax
lea    (%r15,%rax,1),%rsi
mov    %r14d,%edx
sub    %eax,%edx
mov    $0x1,%edi
call   1040 <write@plt>
test   %eax,%eax
jns    11e0 <main+0xa0>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     11e2 <main+0xa2>
mov    $0x2,%eax
jmp    121a <main+0xda>
xor    %eax,%eax
test   %r14d,%r14d
sete   %al
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdd0(%rip),%rdi
call   1060 <perror@plt>
jmp    1189 <main+0x49>

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

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