#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 O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int main(int argc, char *argv[]) {
  int ret;
  char buf[16] = {0};
  int fd;
  fd = open("/dev/tty", O_RDONLY | O_NONBLOCK);
  if (fd < 0) {
    printf("open tty error!\n");
    return -1;
  }
  ret = read(fd, buf, 15);
  if (ret > 0)
    printf("|%s|\n", buf);
  else
    printf("no avaiable data!\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    0x2e62(%rip),%rax
or     (%rax),%esi
lea    0xe71(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11bb <main+0x6b>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <main+0xbd>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xf,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11f0 <main+0xa0>
lea    -0x30(%rbp),%rsi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11fe <main+0xae>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x2e7d(%rip),%rax
mov    0x2e7e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11a6 <main+0x56>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xf,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11b9 <main+0x69>
lea    0xe6f(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c5 <main+0x75>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    11ce <main+0x7e>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [17 x i8] c"open tty error!\0A\00"
@.str.2 = [6 x i8] c"|%s|\0A\00"
@.str.3 = [19 x i8] c"no avaiable data!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [16 x i8]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [16 x i8]* %7 to i8*
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %32
18: ; preds = %2
%19 = load i32, i32* %8
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 15)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %25)
br label %29
27: ; preds = %18
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = load i32, i32* %8
%31 = call i32 @close(i32 %30)
store i32 0, i32* %3
br label %32
32: ; preds = %29, %16
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [6 x i8] c"|%s|\0A\00"
@str = [18 x i8] c"no avaiable data!\00"
@str.4 = [16 x i8] c"open tty error!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.4, i64 0, i64 0))
br label %21
12: ; preds = %2
%13 = call i32 @read(i32 %8, i8* %4, i32 15)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %19
17: ; preds = %12
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %19
19: ; preds = %17, %15
%20 = call i32 @close(i32 %8)
br label %21
21: ; preds = %19, %10
%22 = phi i32 [ -1, %10 ], [ 0, %19 ]
ret i32 %22
}