#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*/ F_GETFD;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int ret = 0;
  int readfd;
  char buf[10240] = {
      0,
  };
  char *pstr = NULL;
  if (argc == 2) {
    readfd = open(argv[1], O_RDONLY);
  } else {
    readfd = open("/dev/stdout", O_RDONLY);
  }
  if (readfd < 0) {
    fprintf(stderr, "[%d]open error %d\n", errno, readfd, argv[1]);
    return -1;
  }
  fprintf(stderr, "open %d \n", readfd);
  ret = fcntl(readfd, F_SETFL, fcntl(readfd, F_GETFD, 0) | O_NONBLOCK);
  if (ret == -1) {
    fprintf(stderr, "change %d error\n", readfd);
  }
  pstr = buf;
  ret = 0;
  while (readfd > 0) {
    errno = 0;
    ret = read(readfd, pstr, sizeof(buf));
    if (ret == -1 && errno == 11) {
      fprintf(stderr, "[%d]blocking[%d]\n", readfd);
      continue;
    }
    if (ret > 0 && ret < 1024) {
      pstr += ret;
      continue;
    }
    if (ret >= 1024) {
      pstr += ret;
      break;
    }
    if (ret == 0) {
      fprintf(stderr, "[%d]file is end\n", readfd);
      break;
    }
  }
  fprintf(stderr, "read:\n%s\n", buf);
  close(readfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2830,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x2820(%rbp),%rdi
xor    %esi,%esi
mov    $0x2800,%edx
call   1040 <memset@plt>
movq   $0x0,-0x2828(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11c6 <main+0x66>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
jmp    11de <main+0x7e>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
lea    0xe2a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    121f <main+0xbf>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%edx
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%r8
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13da <main+0x27a>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x2830(%rbp)
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x282c(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0x2830(%rbp),%edi
mov    -0x282c(%rbp),%esi
mov    %eax,%edx
mov    0x2d6f(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12a7 <main+0x147>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xd89(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
lea    -0x2820(%rbp),%rax
mov    %rax,-0x2828(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    13ad <main+0x24d>
mov    0x2cf3(%rip),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%edi
mov    -0x2828(%rbp),%rsi
mov    $0x2800,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1323 <main+0x1c3>
mov    0x2cc5(%rip),%rax
cmpl   $0xb,(%rax)
jne    1323 <main+0x1c3>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xd23(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    12bc <main+0x15c>
cmpl   $0x0,-0x14(%rbp)
jle    1356 <main+0x1f6>
cmpl   $0x400,-0x14(%rbp)
jge    1356 <main+0x1f6>
mov    -0x14(%rbp),%ecx
mov    -0x2828(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x2828(%rbp)
jmp    12bc <main+0x15c>
cmpl   $0x400,-0x14(%rbp)
jl     137f <main+0x21f>
mov    -0x14(%rbp),%ecx
mov    -0x2828(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x2828(%rbp)
jmp    13ad <main+0x24d>
cmpl   $0x0,-0x14(%rbp)
jne    13a8 <main+0x248>
mov    0x2c40(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xcb0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    13ad <main+0x24d>
jmp    12bc <main+0x15c>
mov    0x2c1c(%rip),%rax
mov    (%rax),%edi
lea    -0x2820(%rbp),%rdx
lea    0xc99(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2800,%rsp
mov    %rsi,%r15
mov    %edi,%ebx
mov    %rsp,%r14
mov    $0x2800,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
cmp    $0x2,%ebx
jne    1191 <main+0x31>
mov    0x8(%r15),%rdi
jmp    1198 <main+0x38>
lea    0xe68(%rip),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebx
mov    0x2e21(%rip),%r12
mov    (%r12),%edi
test   %eax,%eax
js     1206 <main+0xa6>
lea    0xe61(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%r15d
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2dfe(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %r15d,%esi
mov    %eax,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     122d <main+0xcd>
test   %ebx,%ebx
jne    1245 <main+0xe5>
jmp    12c4 <main+0x164>
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
mov    0x8(%r15),%r8
lea    0xdf2(%rip),%rsi
mov    %ebx,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12e2 <main+0x182>
mov    (%r12),%edi
lea    0xdf1(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
test   %ebx,%ebx
je     12c4 <main+0x164>
mov    0x2d74(%rip),%rbp
lea    0xde7(%rip),%r15
jmp    1270 <main+0x110>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r12),%edi
mov    %r15,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
movl   $0x0,0x0(%rbp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x2800,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
jne    1291 <main+0x131>
cmpl   $0xb,0x0(%rbp)
je     1260 <main+0x100>
lea    -0x1(%rax),%ecx
cmp    $0x3fe,%ecx
jbe    12a9 <main+0x149>
cmp    $0x3ff,%eax
jg     12c4 <main+0x164>
test   %eax,%eax
jne    1270 <main+0x110>
jmp    12b0 <main+0x150>
mov    %eax,%eax
add    %rax,%r14
jmp    1270 <main+0x110>
mov    (%r12),%edi
lea    0xd91(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%r12),%edi
lea    0xd8e(%rip),%rsi
xor    %ebp,%ebp
mov    %rsp,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x2800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [12 x i8] c"/dev/stdout\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"[%d]open error %d\0A\00"
@errno = global i32 0
@.str.2 = [10 x i8] c"open %d \0A\00"
@F_SETFL = global i32 0
@F_GETFD = global i32 0
@O_NONBLOCK = global i32 0
@.str.3 = [17 x i8] c"change %d error\0A\00"
@.str.4 = [18 x i8] c"[%d]blocking[%d]\0A\00"
@.str.5 = [17 x i8] c"[%d]file is end\0A\00"
@.str.6 = [10 x i8] c"read:\0A%s\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 [10240 x i8]
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%10 = bitcast [10240 x i8]* %8 to i8*
store i8* null, i8** %9
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %7
br label %22
19: ; preds = %2
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 %21, i32* %7
br label %22
22: ; preds = %19, %13
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = load i32, i32* %7
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %27, i32 %28, i8* %31)
store i32 -1, i32* %3
br label %102
33: ; preds = %22
%34 = load i32, i32* @stderr
%35 = load i32, i32* %7
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %7
%38 = load i32, i32* @F_SETFL
%39 = load i32, i32* %7
%40 = load i32, i32* @F_GETFD
%41 = call i32 @fcntl(i32 %39, i32 %40, i32 0)
%42 = load i32, i32* @O_NONBLOCK
%43 = or i32 %41, %42
%44 = call i32 @fcntl(i32 %37, i32 %38, i32 %43)
store i32 %44, i32* %6
%45 = load i32, i32* %6
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %51
47: ; preds = %33
%48 = load i32, i32* @stderr
%49 = load i32, i32* %7
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %47, %33
%52 = getelementptr inbounds [10240 x i8], [10240 x i8]* %8, i64 0, i64 0
store i8* %52, i8** %9
store i32 0, i32* %6
br label %53
53: ; preds = %95, %75, %65, %51
%54 = load i32, i32* %7
%55 = icmp sgt i32 %54, 0
br i1 %55, label %56, label %96
56: ; preds = %53
store i32 0, i32* @errno
%57 = load i32, i32* %7
%58 = load i8*, i8** %9
%59 = call i32 @read(i32 %57, i8* %58, i32 10240)
store i32 %59, i32* %6
%60 = load i32, i32* %6
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %69
62: ; preds = %56
%63 = load i32, i32* @errno
%64 = icmp eq i32 %63, 11
br i1 %64, label %65, label %69
65: ; preds = %62
%66 = load i32, i32* @stderr
%67 = load i32, i32* %7
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %67)
br label %53
69: ; preds = %62, %56
%70 = load i32, i32* %6
%71 = icmp sgt i32 %70, 0
br i1 %71, label %72, label %80
72: ; preds = %69
%73 = load i32, i32* %6
%74 = icmp slt i32 %73, 1024
br i1 %74, label %75, label %80
75: ; preds = %72
%76 = load i32, i32* %6
%77 = load i8*, i8** %9
%78 = sext i32 %76 to i64
%79 = getelementptr inbounds i8, i8* %77, i64 %78
store i8* %79, i8** %9
br label %53
80: ; preds = %72, %69
%81 = load i32, i32* %6
%82 = icmp sge i32 %81, 1024
br i1 %82, label %83, label %88
83: ; preds = %80
%84 = load i32, i32* %6
%85 = load i8*, i8** %9
%86 = sext i32 %84 to i64
%87 = getelementptr inbounds i8, i8* %85, i64 %86
store i8* %87, i8** %9
br label %96
88: ; preds = %80
%89 = load i32, i32* %6
%90 = icmp eq i32 %89, 0
br i1 %90, label %91, label %95
91: ; preds = %88
%92 = load i32, i32* @stderr
%93 = load i32, i32* %7
%94 = call i32 (i32, i8*, ...) @fprintf(i32 %92, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %93)
br label %96
95: ; preds = %88
br label %53
96: ; preds = %91, %83, %53
%97 = load i32, i32* @stderr
%98 = getelementptr inbounds [10240 x i8], [10240 x i8]* %8, i64 0, i64 0
%99 = call i32 (i32, i8*, ...) @fprintf(i32 %97, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %98)
%100 = load i32, i32* %7
%101 = call i32 @close(i32 %100)
store i32 0, i32* %3
br label %102
102: ; preds = %96, %25
%103 = load i32, i32* %3
ret i32 %103
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [12 x i8] c"/dev/stdout\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"[%d]open error %d\0A\00"
@errno = global i32 0
@.str.2 = [10 x i8] c"open %d \0A\00"
@F_SETFL = global i32 0
@F_GETFD = global i32 0
@O_NONBLOCK = global i32 0
@.str.3 = [17 x i8] c"change %d error\0A\00"
@.str.4 = [18 x i8] c"[%d]blocking[%d]\0A\00"
@.str.5 = [17 x i8] c"[%d]file is end\0A\00"
@.str.6 = [10 x i8] c"read:\0A%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [10240 x i8]
%4 = getelementptr inbounds [10240 x i8], [10240 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %2, %6
%10 = phi i8* [ %8, %6 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %2 ]
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp slt i32 %12, 0
%14 = load i32, i32* @stderr
br i1 %13, label %15, label %20
15: ; preds = %9
%16 = load i32, i32* @errno
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %12, i8* %18)
br label %63
20: ; preds = %9
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%22 = load i32, i32* @F_SETFL
%23 = load i32, i32* @F_GETFD
%24 = tail call i32 @fcntl(i32 %12, i32 %23, i32 0)
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %25, %24
%27 = tail call i32 @fcntl(i32 %12, i32 %22, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %20
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %12)
br label %32
32: ; preds = %29, %20
%33 = icmp eq i32 %12, 0
br i1 %33, label %59, label %34
34: ; preds = %32, %49
%35 = phi i8* [ %51, %49 ], [ %4, %32 ]
br label %36
36: ; preds = %45, %34
store i32 0, i32* @errno
%37 = call i32 @read(i32 %12, i8* %35, i32 10240)
%38 = icmp eq i32 %37, -1
%39 = load i32, i32* @errno
%40 = icmp eq i32 %39, 11
%41 = select i1 %38, i1 %40, i1 false
br i1 %41, label %42, label %46
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = tail call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %12)
br label %45
45: ; preds = %42, %54
br label %36
46: ; preds = %36
%47 = add i32 %37, -1
%48 = icmp ult i32 %47, 1023
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = zext i32 %37 to i64
%51 = getelementptr inbounds i8, i8* %35, i64 %50
br label %34
52: ; preds = %46
%53 = icmp sgt i32 %37, 1023
br i1 %53, label %59, label %54
54: ; preds = %52
%55 = icmp eq i32 %37, 0
br i1 %55, label %56, label %45
56: ; preds = %54
%57 = load i32, i32* @stderr
%58 = tail call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %12)
br label %59
59: ; preds = %52, %32, %56
%60 = load i32, i32* @stderr
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %4)
%62 = call i32 @close(i32 %12)
br label %63
63: ; preds = %59, %15
%64 = phi i32 [ -1, %15 ], [ 0, %59 ]
ret i32 %64
}