#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__ errno;
int read(int /*<<< orphan*/, char *, int);
int tty_do(char);
int tty_event(void) {
  char c;
  int n;
  do {
    n = read(0, &c, 1);
    if (n == -1 && errno != EAGAIN)
      return -1;
    if (n)
      n = tty_do(c);
  } while (n == 1);
  return n;
}


x86 assembly (clang, -O0):
<tty_event>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %edi,%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    116b <tty_event+0x4b>
mov    0x2e94(%rip),%rax
mov    (%rax),%rax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%rax
je     116b <tty_event+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1196 <tty_event+0x76>
cmpl   $0x0,-0xc(%rbp)
je     1181 <tty_event+0x61>
movsbl -0x5(%rbp),%edi
call   1040 <tty_do@plt>
mov    %eax,-0xc(%rbp)
jmp    1186 <tty_event+0x66>
cmpl   $0x1,-0xc(%rbp)
je     1128 <tty_event+0x8>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_event>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xf(%rsp),%rbx
mov    0x2eab(%rip),%r14
mov    0x2e94(%rip),%r15
jmp    1157 <tty_event+0x37>
xchg   %ax,%ax
mov    (%r14),%rax
cmp    (%r15),%rax
jne    1171 <tty_event+0x51>
movsbl 0xf(%rsp),%edi
call   1040 <tty_do@plt>
cmp    $0x1,%eax
jne    1176 <tty_event+0x56>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1140 <tty_event+0x20>
test   %eax,%eax
jne    1148 <tty_event+0x28>
jmp    1176 <tty_event+0x56>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @tty_event() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32
br label %4
4: ; preds = %20, %0
%5 = call i32 @read(i32 0, i8* %2, i32 1)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %13
8: ; preds = %4
%9 = load i64, i64* @errno
%10 = load i64, i64* @EAGAIN
%11 = icmp ne i64 %9, %10
br i1 %11, label %12, label %13
12: ; preds = %8
store i32 -1, i32* %1
br label %25
13: ; preds = %8, %4
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i8, i8* %2
%18 = call i32 @tty_do(i8 signext %17)
store i32 %18, i32* %3
br label %19
19: ; preds = %16, %13
br label %20
20: ; preds = %19
%21 = load i32, i32* %3
%22 = icmp eq i32 %21, 1
br i1 %22, label %4, label %23
23: ; preds = %20
%24 = load i32, i32* %3
store i32 %24, i32* %1
br label %25
25: ; preds = %23, %12
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
define i32 @tty_event() {
%1 = alloca i8
br label %2
2: ; preds = %8, %0
%3 = call i32 @read(i32 0, i8* %1, i32 1)
switch i32 %3, label %8 [
i32 -1, label %4
i32 0, label %12
]
4: ; preds = %2
%5 = load i64, i64* @errno
%6 = load i64, i64* @EAGAIN
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %12
8: ; preds = %2, %4
%9 = load i8, i8* %1
%10 = tail call i32 @tty_do(i8 signext %9)
%11 = icmp eq i32 %10, 1
br i1 %11, label %2, label %12
12: ; preds = %2, %8, %4
%13 = phi i32 [ -1, %4 ], [ %10, %8 ], [ %3, %2 ]
ret i32 %13
}