#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 /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int strlen(char *);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(void) {
  char buf[10];
  int fd, n;
  fd = open("/dev/tty", O_RDONLY | O_NONBLOCK);
  if (fd < 0) {
    perror("open /dev/tty");
    exit(1);
  }
  int flags;
  flags = fcntl(fd, F_GETFL, 0);
  flags |= O_NONBLOCK;
  fcntl(fd, F_SETFL, flags);
  printf("here\n");
  while (1) {
    n = read(fd, buf, 10);
    printf("read buf %s\n", buf);
    write(STDOUT_FILENO, "try again\n", strlen("try again\n"));
    sleep(1);
  }
  write(STDOUT_FILENO, buf, n);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
or     (%rax),%esi
lea    0xe4d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e0 <main+0x50>
lea    0xe2f(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1030 <fcntl@plt>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0xa,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
lea    -0xe(%rbp),%rsi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    0xdc1(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
lea    0xdb0(%rip),%rsi
call   1040 <write@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1227 <main+0x97>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e23(%rip),%rax
mov    0x2e24(%rip),%rbx
mov    (%rbx),%esi
or     (%rax),%esi
lea    0xe3e(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1271 <main+0xd1>
mov    %eax,%r15d
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
or     (%rbx),%eax
mov    0x2dd5(%rip),%rcx
mov    (%rcx),%esi
mov    %r15d,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
lea    0xe20(%rip),%rdi
call   1030 <puts@plt>
lea    0xe(%rsp),%r12
lea    0xdf7(%rip),%r14
mov    0x2dd2(%rip),%r13
lea    0xdf6(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0xa,%edx
call   1080 <read@plt>
mov    %r14,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x0(%r13),%ebx
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    1230 <main+0x90>
lea    0xd88(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [14 x i8] c"open /dev/tty\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@.str.2 = [6 x i8] c"here\0A\00"
@.str.3 = [13 x i8] c"read buf %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [11 x i8] c"try again\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @O_RDONLY
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = load i32, i32* %3
%17 = load i32, i32* @F_GETFL
%18 = call i32 @fcntl(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %5
%19 = load i32, i32* @O_NONBLOCK
%20 = load i32, i32* %5
%21 = or i32 %20, %19
store i32 %21, i32* %5
%22 = load i32, i32* %3
%23 = load i32, i32* @F_SETFL
%24 = load i32, i32* %5
%25 = call i32 @fcntl(i32 %22, i32 %23, i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %15, %27
%28 = load i32, i32* %3
%29 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 10)
store i32 %30, i32* %4
%31 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @STDOUT_FILENO
%34 = call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%35 = call i32 @write(i32 %33, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = call i32 @sleep(i32 1)
br label %27
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [14 x i8] c"open /dev/tty\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@.str.3 = [13 x i8] c"read buf %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [11 x i8] c"try again\0A\00"
@str = [5 x i8] c"here\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = load i32, i32* @O_NONBLOCK
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i32, i32* @F_GETFL
%13 = tail call i32 @fcntl(i32 %6, i32 %12, i32 0)
%14 = load i32, i32* @O_NONBLOCK
%15 = or i32 %14, %13
%16 = load i32, i32* @F_SETFL
%17 = tail call i32 @fcntl(i32 %6, i32 %16, i32 %15)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %19
19: ; preds = %19, %11
%20 = call i32 @read(i32 %6, i8* %2, i32 10)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%22 = load i32, i32* @STDOUT_FILENO
%23 = call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%24 = call i32 @write(i32 %22, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%25 = call i32 @sleep(i32 1)
br label %19
}