#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 */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int O_NONBLOCK;
int O_RDONLY;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int main(void) {
  int fd, nr;
  char c;
  char *fname = "/dev/tty";
  if ((fd = open(fname, O_RDONLY | O_NONBLOCK)) < 0) {
    perror("open()");
    exit(-1);
  }
  for (;;) {
    if (nr = read(fd, &c, 1) > 0) {
      printf("%c\n", c);
      exit(0);
    } else if (nr == 0) {
      if (errno == EINTR)
        printf(".");
      if (errno == EAGAIN)
        printf("*");
    } else
      break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11a7 <main+0x57>
lea    0xe71(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    11ac <main+0x5c>
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
setg   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0xc(%rbp)
test   $0x1,%al
jne    11dc <main+0x8c>
jmp    11f5 <main+0xa5>
movsbl -0xd(%rbp),%esi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
jne    1254 <main+0x104>
mov    0x2dca(%rip),%rax
mov    (%rax),%rax
mov    0x2db8(%rip),%rcx
cmp    (%rcx),%rax
jne    1227 <main+0xd7>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%rax
mov    0x2d80(%rip),%rcx
cmp    (%rcx),%rax
jne    124f <main+0xff>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1259 <main+0x109>
jmp    1263 <main+0x113>
jmp    125e <main+0x10e>
jmp    11ac <main+0x5c>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e6c(%rip),%rax
mov    0x2e6d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
jns    11a5 <main+0x45>
lea    0xe73(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    %eax,%ebx
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    11d7 <main+0x77>
movsbl 0xf(%rsp),%esi
lea    0xe47(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x2df2(%rip),%r15
mov    0x2de3(%rip),%r12
mov    0x2dcc(%rip),%r13
lea    0xf(%rsp),%r14
jmp    1214 <main+0xb4>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jg     11bd <main+0x5d>
mov    (%r15),%rax
cmp    (%r12),%rax
jne    122a <main+0xca>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    (%r15),%rax
cmp    0x0(%r13),%rax
jne    1200 <main+0xa0>
mov    $0x2a,%edi
call   1030 <putchar@plt>
jmp    1200 <main+0xa0>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [4 x i8] c"%c\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [2 x i8] c".\00"
@EAGAIN = global i64 0
@.str.4 = [2 x i8] c"*\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %7, %8
%10 = call i32 @open(i8* %6, i32 %9)
store i32 %10, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %0
br label %16
16: ; preds = %44, %15
%17 = load i32, i32* %2
%18 = call i64 @read(i32 %17, i8* %4, i32 1)
%19 = icmp sgt i64 %18, 0
%20 = zext i1 %19 to i32
store i32 %20, i32* %3
br i1 %19, label %21, label %26
21: ; preds = %16
%22 = load i8, i8* %4
%23 = sext i8 %22 to i32
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %16
%27 = load i32, i32* %3
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %42
29: ; preds = %26
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %29
%36 = load i64, i64* @errno
%37 = load i64, i64* @EAGAIN
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
br label %43
42: ; preds = %26
br label %45
43: ; preds = %41
br label %44
44: ; preds = %43
br label %16
45: ; preds = %42
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [4 x i8] c"%c\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_RDONLY
%3 = load i32, i32* @O_NONBLOCK
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = call i64 @read(i32 %5, i8* %1, i32 1)
%9 = icmp sgt i64 %8, 0
br i1 %9, label %13, label %18
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %31, %7
%14 = load i8, i8* %1
%15 = sext i8 %14 to i32
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = tail call i32 @exit(i32 0)
unreachable
18: ; preds = %7, %31
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = tail call i32 @putchar(i32 46)
%24 = load i64, i64* @errno
br label %25
25: ; preds = %22, %18
%26 = phi i64 [ %24, %22 ], [ %19, %18 ]
%27 = load i64, i64* @EAGAIN
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = tail call i32 @putchar(i32 42)
br label %31
31: ; preds = %29, %25
%32 = call i64 @read(i32 %5, i8* %1, i32 1)
%33 = icmp sgt i64 %32, 0
br i1 %33, label %13, label %18
}