#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int fd;
};
struct epoll_event {
  TYPE_1__ data;
  int /*<<< orphan*/ events;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ events;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ EPOLLIN;
int /*<<< orphan*/ EPOLL_CLOEXEC;
int /*<<< orphan*/ EPOLL_CTL_ADD;
int /*<<< orphan*/ EPOLL_CTL_DEL;
int O_CLOEXEC;
int O_RDONLY;
int STDIN_FILENO;
int /*<<< orphan*/ _IONBF;
int /*<<< orphan*/ close(int);
int epoll_create1(int /*<<< orphan*/);
int epoll_ctl(int, int /*<<< orphan*/, int, struct epoll_event *);
int epoll_wait(int, struct epoll_event *, int, int);
scalar_t__ errno;
int /*<<< orphan*/ isatty(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ read_active_tty(int, int);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int main(void) {
  int active_tty_fd, epollfd, i, nfds, retval;
  bool running;
  struct epoll_event ev, events[2];
  char buffer[4096];
  ssize_t bytes;
  setvbuf(stdin, NULL, _IONBF, 0);
  active_tty_fd = open("/sys/class/tty/tty0/active", O_RDONLY | O_CLOEXEC);
  if (active_tty_fd == -1) {
    perror("open(/sys/class/tty/tty0/active)");
    return 1;
  }
  if (!read_active_tty(active_tty_fd, false)) {
    return 1;
  }
  if (!isatty(STDIN_FILENO)) {
    return 0;
  }
  epollfd = epoll_create1(EPOLL_CLOEXEC);
  if (epollfd == -1) {
    perror("epoll_create1");
    close(active_tty_fd);
    return 1;
  }
  ev.events = EPOLLIN;
  ev.data.fd = STDIN_FILENO;
  if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) {
    perror("epoll_ctl(EPOLL_CTL_ADD, stdin)");
    close(active_tty_fd);
    close(epollfd);
    return 1;
  }
  ev.events = 0;
  ev.data.fd = active_tty_fd;
  if (epoll_ctl(epollfd, EPOLL_CTL_ADD, active_tty_fd, &ev) == -1) {
    perror("epoll_ctl(EPOLL_CTL_ADD)");
    close(active_tty_fd);
    close(epollfd);
    return 1;
  }
  running = true;
  retval = 0;
  while (running) {
    nfds = epoll_wait(epollfd, events, sizeof(events) / sizeof(events[0]), -1);
    if (nfds == -1) {
      perror("epoll_pwait");
      retval = 1;
      break;
    }
    for (i = 0; i < nfds; i++) {
      if (events[i].data.fd == 0) {
        bytes = read(STDIN_FILENO, buffer, sizeof(buffer));
        if (bytes == -1) {
          if (errno == EINTR) {
            continue;
          }
          perror("fread(stdin)");
          retval = 1;
          running = false;
        } else if (!bytes) {
          running = false;
        } else {
          printf("Eating some bytes from stdin...\n");
        }
      }
      if (events[i].data.fd == active_tty_fd) {
        if (!read_active_tty(active_tty_fd, true)) {
          retval = 1;
          running = false;
        }
      }
    }
  }
  if (epoll_ctl(epollfd, EPOLL_CTL_DEL, active_tty_fd, &ev) == -1) {
    perror("epoll_ctl(EPOLL_CTL_DEL)");
    retval = 1;
  }
  close(active_tty_fd);
  if (epoll_ctl(epollfd, EPOLL_CTL_DEL, STDIN_FILENO, &ev) == -1) {
    perror("epoll_ctl(EPOLL_CTL_DEL, stdin)");
    retval = 1;
  }
  close(epollfd);
  return retval;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   10a0 <setvbuf@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    0x2d99(%rip),%rax
or     (%rax),%esi
lea    0xe08(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1222 <main+0x72>
lea    0xe0a(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1541 <main+0x391>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1030 <read_active_tty@plt>
cmp    $0x0,%eax
jne    1241 <main+0x91>
movl   $0x1,-0x4(%rbp)
jmp    1541 <main+0x391>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   1040 <isatty@plt>
cmp    $0x0,%eax
jne    1264 <main+0xb4>
movl   $0x0,-0x4(%rbp)
jmp    1541 <main+0x391>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   10d0 <epoll_create1@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    129f <main+0xef>
lea    0xdb6(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1541 <main+0x391>
mov    0x2d3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2ce6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2d07(%rip),%rax
mov    (%rax),%esi
mov    0x2cce(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
call   1070 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
jne    1306 <main+0x156>
lea    0xd65(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1541 <main+0x391>
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2cab(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
lea    -0x28(%rbp),%rcx
call   1070 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
jne    135c <main+0x1ac>
lea    0xd2f(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1541 <main+0x391>
movl   $0x1,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     14bd <main+0x30d>
mov    -0xc(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x2,%edx
mov    $0xffffffff,%ecx
call   1090 <epoll_wait@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    13af <main+0x1ff>
lea    0xce5(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x18(%rbp)
jmp    14bd <main+0x30d>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    14b8 <main+0x308>
movslq -0x10(%rbp),%rax
cmpl   $0x0,-0x40(%rbp,%rax,8)
jne    146b <main+0x2bb>
mov    0x2bc0(%rip),%rax
mov    (%rax),%edi
lea    -0x1040(%rbp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,-0x1044(%rbp)
cmpl   $0xffffffff,-0x1044(%rbp)
jne    143c <main+0x28c>
mov    0x2ba3(%rip),%rax
mov    (%rax),%rax
mov    0x2b91(%rip),%rcx
cmp    (%rcx),%rax
jne    141d <main+0x26d>
jmp    14aa <main+0x2fa>
lea    0xc6b(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
jmp    1466 <main+0x2b6>
cmpl   $0x0,-0x1044(%rbp)
jne    1455 <main+0x2a5>
movl   $0x0,-0x1c(%rbp)
jmp    1461 <main+0x2b1>
lea    0xc40(%rip),%rdi
call   1050 <printf@plt>
jmp    1466 <main+0x2b6>
jmp    146b <main+0x2bb>
movslq -0x10(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%eax
cmp    -0x8(%rbp),%eax
jne    14a5 <main+0x2f5>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1030 <read_active_tty@plt>
cmp    $0x0,%eax
jne    14a0 <main+0x2f0>
movl   $0x1,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
jmp    14a5 <main+0x2f5>
jmp    14aa <main+0x2fa>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    13b6 <main+0x206>
jmp    136a <main+0x1ba>
mov    -0xc(%rbp),%edi
mov    0x2b31(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
lea    -0x28(%rbp),%rcx
call   1070 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
jne    14f1 <main+0x341>
lea    0xbd8(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
mov    0x2af5(%rip),%rax
mov    (%rax),%esi
mov    0x2a8c(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
call   1070 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
jne    1533 <main+0x383>
lea    0xbaf(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
xor    %ecx,%ecx
call   10a0 <setvbuf@plt>
mov    0x2df5(%rip),%rax
mov    0x2d9e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe0b(%rip),%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     13a4 <main+0x1f4>
mov    %eax,%ebx
xor    %ebp,%ebp
mov    %eax,%edi
xor    %esi,%esi
call   1030 <read_active_tty@plt>
test   %eax,%eax
je     139a <main+0x1ea>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1050 <isatty@plt>
test   %eax,%eax
je     144d <main+0x29d>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   10d0 <epoll_create1@plt>
cmp    $0xffffffff,%eax
je     13ba <main+0x20a>
mov    %eax,%r15d
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x2d3c(%rip),%rax
mov    (%rax),%edx
mov    %edx,(%rsp)
mov    0x2d60(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rsp,%rcx
mov    %r15d,%edi
call   1070 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
je     13ca <main+0x21a>
movl   $0x0,0x4(%rsp)
mov    %ebx,(%rsp)
mov    0x0(%rbp),%esi
mov    %rsp,%rcx
mov    %r15d,%edi
mov    %ebx,%edx
call   1070 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
je     13d3 <main+0x223>
xor    %ebp,%ebp
mov    $0x1,%r14d
mov    %r15d,0xc(%rsp)
jmp    12d4 <main+0x124>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x1,%r15d
test   %r15d,%r15d
mov    0xc(%rsp),%r15d
je     1406 <main+0x256>
mov    %r15d,%edi
lea    0x10(%rsp),%rsi
mov    $0x2,%edx
mov    $0xffffffff,%ecx
call   1090 <epoll_wait@plt>
cmp    $0xffffffff,%eax
je     13f5 <main+0x245>
test   %eax,%eax
jle    12c0 <main+0x110>
mov    %eax,%r13d
mov    $0x1,%r15d
xor    %r12d,%r12d
jmp    1329 <main+0x179>
mov    0x2c9b(%rip),%rax
mov    (%rax),%rax
mov    0x2c89(%rip),%rcx
cmp    (%rcx),%rax
jne    1384 <main+0x1d4>
nopl   0x0(%rax)
add    $0x1,%r12
cmp    %r12,%r13
je     12c6 <main+0x116>
cmpl   $0x0,0x10(%rsp,%r12,8)
je     1350 <main+0x1a0>
cmp    %ebx,0x10(%rsp,%r12,8)
jne    1320 <main+0x170>
mov    %ebx,%edi
mov    $0x1,%esi
call   1030 <read_active_tty@plt>
test   %eax,%eax
cmove  %r14d,%ebp
cmove  %eax,%r15d
jmp    1320 <main+0x170>
mov    0x2c41(%rip),%rax
mov    (%rax),%edi
lea    0x20(%rsp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
test   %eax,%eax
je     137f <main+0x1cf>
cmp    $0xffffffff,%eax
je     1306 <main+0x156>
lea    0xd5d(%rip),%rdi
call   1040 <puts@plt>
jmp    1331 <main+0x181>
mov    %eax,%r15d
jmp    1331 <main+0x181>
lea    0xd04(%rip),%rdi
call   10c0 <perror@plt>
xor    %r15d,%r15d
mov    $0x1,%ebp
jmp    1331 <main+0x181>
mov    $0x1,%ebp
jmp    144d <main+0x29d>
lea    0xc70(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%ebp
jmp    144d <main+0x29d>
lea    0xc7b(%rip),%rdi
call   10c0 <perror@plt>
mov    %ebx,%edi
jmp    13e9 <main+0x239>
lea    0xc79(%rip),%rdi
jmp    13da <main+0x22a>
lea    0xc90(%rip),%rdi
call   10c0 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    $0x1,%ebp
jmp    144d <main+0x29d>
lea    0xc87(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%ebp
mov    0x2beb(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%rcx
mov    %r15d,%edi
mov    %ebx,%edx
call   1070 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
je     1461 <main+0x2b1>
mov    %ebx,%edi
call   1060 <close@plt>
mov    (%r14),%esi
mov    0x2b65(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %r15d,%edi
call   1070 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
je     1474 <main+0x2c4>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc34(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%ebp
jmp    1422 <main+0x272>
lea    0xc3a(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%ebp
jmp    1445 <main+0x295>

LLVM IR (-O0):
%struct.epoll_event = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@stdin = global i32 0
@_IONBF = global i32 0
@.str = [27 x i8] c"/sys/class/tty/tty0/active\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@.str.1 = [33 x i8] c"open(/sys/class/tty/tty0/active)\00"
@STDIN_FILENO = global i32 0
@EPOLL_CLOEXEC = global i32 0
@.str.2 = [14 x i8] c"epoll_create1\00"
@EPOLLIN = global i32 0
@EPOLL_CTL_ADD = global i32 0
@.str.3 = [32 x i8] c"epoll_ctl(EPOLL_CTL_ADD, stdin)\00"
@.str.4 = [25 x i8] c"epoll_ctl(EPOLL_CTL_ADD)\00"
@.str.5 = [12 x i8] c"epoll_pwait\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.6 = [13 x i8] c"fread(stdin)\00"
@.str.7 = [33 x i8] c"Eating some bytes from stdin...\0A\00"
@EPOLL_CTL_DEL = global i32 0
@.str.8 = [25 x i8] c"epoll_ctl(EPOLL_CTL_DEL)\00"
@.str.9 = [32 x i8] c"epoll_ctl(EPOLL_CTL_DEL, stdin)\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.epoll_event
%9 = alloca [2 x %struct.epoll_event]
%10 = alloca [4096 x i8]
%11 = alloca i32
store i32 0, i32* %1
%12 = load i32, i32* @stdin
%13 = load i32, i32* @_IONBF
%14 = call i32 @setvbuf(i32 %12, i32* null, i32 %13, i32 0)
%15 = load i32, i32* @O_RDONLY
%16 = load i32, i32* @O_CLOEXEC
%17 = or i32 %15, %16
%18 = call i32 @open(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 %18, i32* %2
%19 = load i32, i32* %2
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %0
%22 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %163
23: ; preds = %0
%24 = load i32, i32* %2
%25 = call i32 @read_active_tty(i32 %24, i32 0)
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %23
store i32 1, i32* %1
br label %163
28: ; preds = %23
%29 = load i32, i32* @STDIN_FILENO
%30 = call i32 @isatty(i32 %29)
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %28
store i32 0, i32* %1
br label %163
33: ; preds = %28
%34 = load i32, i32* @EPOLL_CLOEXEC
%35 = call i32 @epoll_create1(i32 %34)
store i32 %35, i32* %3
%36 = load i32, i32* %3
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
store i32 1, i32* %1
br label %163
42: ; preds = %33
%43 = load i32, i32* @EPOLLIN
%44 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %8, i32 0, i32 1
store i32 %43, i32* %44
%45 = load i32, i32* @STDIN_FILENO
%46 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %8, i32 0, i32 0
%47 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %46, i32 0, i32 0
store i32 %45, i32* %47
%48 = load i32, i32* %3
%49 = load i32, i32* @EPOLL_CTL_ADD
%50 = load i32, i32* @STDIN_FILENO
%51 = call i32 @epoll_ctl(i32 %48, i32 %49, i32 %50, %struct.epoll_event* %8)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %59
53: ; preds = %42
%54 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%55 = load i32, i32* %2
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* %3
%58 = call i32 @close(i32 %57)
store i32 1, i32* %1
br label %163
59: ; preds = %42
%60 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %8, i32 0, i32 1
store i32 0, i32* %60
%61 = load i32, i32* %2
%62 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %8, i32 0, i32 0
%63 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %62, i32 0, i32 0
store i32 %61, i32* %63
%64 = load i32, i32* %3
%65 = load i32, i32* @EPOLL_CTL_ADD
%66 = load i32, i32* %2
%67 = call i32 @epoll_ctl(i32 %64, i32 %65, i32 %66, %struct.epoll_event* %8)
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %75
69: ; preds = %59
%70 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%71 = load i32, i32* %2
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %3
%74 = call i32 @close(i32 %73)
store i32 1, i32* %1
br label %163
75: ; preds = %59
store i32 1, i32* %7
store i32 0, i32* %6
br label %76
76: ; preds = %140, %75
%77 = load i32, i32* %7
%78 = icmp ne i32 %77, 0
br i1 %78, label %79, label %141
79: ; preds = %76
%80 = load i32, i32* %3
%81 = getelementptr inbounds [2 x %struct.epoll_event], [2 x %struct.epoll_event]* %9, i64 0, i64 0
%82 = call i32 @epoll_wait(i32 %80, %struct.epoll_event* %81, i32 2, i32 -1)
store i32 %82, i32* %5
%83 = load i32, i32* %5
%84 = icmp eq i32 %83, -1
br i1 %84, label %85, label %87
85: ; preds = %79
%86 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %6
br label %141
87: ; preds = %79
store i32 0, i32* %4
br label %88
88: ; preds = %137, %87
%89 = load i32, i32* %4
%90 = load i32, i32* %5
%91 = icmp slt i32 %89, %90
br i1 %91, label %92, label %140
92: ; preds = %88
%93 = load i32, i32* %4
%94 = sext i32 %93 to i64
%95 = getelementptr inbounds [2 x %struct.epoll_event], [2 x %struct.epoll_event]* %9, i64 0, i64 %94
%96 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %95, i32 0, i32 0
%97 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %96, i32 0, i32 0
%98 = load i32, i32* %97
%99 = icmp eq i32 %98, 0
br i1 %99, label %100, label %121
100: ; preds = %92
%101 = load i32, i32* @STDIN_FILENO
%102 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%103 = call i32 @read(i32 %101, i8* %102, i32 4096)
store i32 %103, i32* %11
%104 = load i32, i32* %11
%105 = icmp eq i32 %104, -1
br i1 %105, label %106, label %113
106: ; preds = %100
%107 = load i64, i64* @errno
%108 = load i64, i64* @EINTR
%109 = icmp eq i64 %107, %108
br i1 %109, label %110, label %111
110: ; preds = %106
br label %137
111: ; preds = %106
%112 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %6
store i32 0, i32* %7
br label %120
113: ; preds = %100
%114 = load i32, i32* %11
%115 = icmp ne i32 %114, 0
br i1 %115, label %117, label %116
116: ; preds = %113
store i32 0, i32* %7
br label %119
117: ; preds = %113
%118 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0))
br label %119
119: ; preds = %117, %116
br label %120
120: ; preds = %119, %111
br label %121
121: ; preds = %120, %92
%122 = load i32, i32* %4
%123 = sext i32 %122 to i64
%124 = getelementptr inbounds [2 x %struct.epoll_event], [2 x %struct.epoll_event]* %9, i64 0, i64 %123
%125 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %124, i32 0, i32 0
%126 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %125, i32 0, i32 0
%127 = load i32, i32* %126
%128 = load i32, i32* %2
%129 = icmp eq i32 %127, %128
br i1 %129, label %130, label %136
130: ; preds = %121
%131 = load i32, i32* %2
%132 = call i32 @read_active_tty(i32 %131, i32 1)
%133 = icmp ne i32 %132, 0
br i1 %133, label %135, label %134
134: ; preds = %130
store i32 1, i32* %6
store i32 0, i32* %7
br label %135
135: ; preds = %134, %130
br label %136
136: ; preds = %135, %121
br label %137
137: ; preds = %136, %110
%138 = load i32, i32* %4
%139 = add nsw i32 %138, 1
store i32 %139, i32* %4
br label %88
140: ; preds = %88
br label %76
141: ; preds = %85, %76
%142 = load i32, i32* %3
%143 = load i32, i32* @EPOLL_CTL_DEL
%144 = load i32, i32* %2
%145 = call i32 @epoll_ctl(i32 %142, i32 %143, i32 %144, %struct.epoll_event* %8)
%146 = icmp eq i32 %145, -1
br i1 %146, label %147, label %149
147: ; preds = %141
%148 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %6
br label %149
149: ; preds = %147, %141
%150 = load i32, i32* %2
%151 = call i32 @close(i32 %150)
%152 = load i32, i32* %3
%153 = load i32, i32* @EPOLL_CTL_DEL
%154 = load i32, i32* @STDIN_FILENO
%155 = call i32 @epoll_ctl(i32 %152, i32 %153, i32 %154, %struct.epoll_event* %8)
%156 = icmp eq i32 %155, -1
br i1 %156, label %157, label %159
157: ; preds = %149
%158 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %6
br label %159
159: ; preds = %157, %149
%160 = load i32, i32* %3
%161 = call i32 @close(i32 %160)
%162 = load i32, i32* %6
store i32 %162, i32* %1
br label %163
163: ; preds = %159, %69, %53, %38, %32, %27, %21
%164 = load i32, i32* %1
ret i32 %164
}

LLVM IR (-O2):
%struct.epoll_event = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
@stdin = global i32 0
@_IONBF = global i32 0
@.str = [27 x i8] c"/sys/class/tty/tty0/active\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@.str.1 = [33 x i8] c"open(/sys/class/tty/tty0/active)\00"
@STDIN_FILENO = global i32 0
@EPOLL_CLOEXEC = global i32 0
@.str.2 = [14 x i8] c"epoll_create1\00"
@EPOLLIN = global i32 0
@EPOLL_CTL_ADD = global i32 0
@.str.3 = [32 x i8] c"epoll_ctl(EPOLL_CTL_ADD, stdin)\00"
@.str.4 = [25 x i8] c"epoll_ctl(EPOLL_CTL_ADD)\00"
@.str.5 = [12 x i8] c"epoll_pwait\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.6 = [13 x i8] c"fread(stdin)\00"
@EPOLL_CTL_DEL = global i32 0
@.str.8 = [25 x i8] c"epoll_ctl(EPOLL_CTL_DEL)\00"
@.str.9 = [32 x i8] c"epoll_ctl(EPOLL_CTL_DEL, stdin)\00"
@str = [32 x i8] c"Eating some bytes from stdin...\00"
define i32 @main() {
%1 = alloca %struct.epoll_event
%2 = alloca [2 x %struct.epoll_event]
%3 = alloca [4096 x i8]
%4 = bitcast %struct.epoll_event* %1 to i8*
%5 = bitcast [2 x %struct.epoll_event]* %2 to i8*
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @stdin
%8 = load i32, i32* @_IONBF
%9 = tail call i32 @setvbuf(i32 %7, i32* null, i32 %8, i32 0)
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_CLOEXEC
%12 = or i32 %11, %10
%13 = tail call i32 @open(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %119
17: ; preds = %0
%18 = tail call i32 @read_active_tty(i32 %13, i32 0)
%19 = icmp eq i32 %18, 0
br i1 %19, label %119, label %20
20: ; preds = %17
%21 = load i32, i32* @STDIN_FILENO
%22 = tail call i32 @isatty(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %119, label %24
24: ; preds = %20
%25 = load i32, i32* @EPOLL_CLOEXEC
%26 = tail call i32 @epoll_create1(i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @close(i32 %13)
br label %119
31: ; preds = %24
%32 = load i32, i32* @EPOLLIN
%33 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %1, i64 0, i32 1
store i32 %32, i32* %33
%34 = load i32, i32* @STDIN_FILENO
%35 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %1, i64 0, i32 0, i32 0
store i32 %34, i32* %35
%36 = load i32, i32* @EPOLL_CTL_ADD
%37 = call i32 @epoll_ctl(i32 %26, i32 %36, i32 %34, %struct.epoll_event* %1)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @close(i32 %13)
%42 = call i32 @close(i32 %26)
br label %119
43: ; preds = %31
store i32 0, i32* %33
store i32 %13, i32* %35
%44 = load i32, i32* @EPOLL_CTL_ADD
%45 = call i32 @epoll_ctl(i32 %26, i32 %44, i32 %13, %struct.epoll_event* %1)
%46 = icmp eq i32 %45, -1
br i1 %46, label %49, label %47
47: ; preds = %43
%48 = getelementptr inbounds [2 x %struct.epoll_event], [2 x %struct.epoll_event]* %2, i64 0, i64 0
br label %57
49: ; preds = %43
%50 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 @close(i32 %13)
%52 = call i32 @close(i32 %26)
br label %119
53: ; preds = %95, %61
%54 = phi i32 [ %58, %61 ], [ %96, %95 ]
%55 = phi i32 [ 1, %61 ], [ %97, %95 ]
%56 = icmp eq i32 %55, 0
br i1 %56, label %100, label %57
57: ; preds = %47, %53
%58 = phi i32 [ 0, %47 ], [ %54, %53 ]
%59 = call i32 @epoll_wait(i32 %26, %struct.epoll_event* %48, i32 2, i32 -1)
%60 = icmp eq i32 %59, -1
br i1 %60, label %65, label %61
61: ; preds = %57
%62 = icmp sgt i32 %59, 0
br i1 %62, label %63, label %53
63: ; preds = %61
%64 = zext i32 %59 to i64
br label %67
65: ; preds = %57
%66 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %100
67: ; preds = %63, %95
%68 = phi i64 [ 0, %63 ], [ %98, %95 ]
%69 = phi i32 [ 1, %63 ], [ %97, %95 ]
%70 = phi i32 [ %58, %63 ], [ %96, %95 ]
%71 = getelementptr inbounds [2 x %struct.epoll_event], [2 x %struct.epoll_event]* %2, i64 0, i64 %68, i32 0, i32 0
%72 = load i32, i32* %71
%73 = icmp eq i32 %72, 0
br i1 %73, label %74, label %85
74: ; preds = %67
%75 = load i32, i32* @STDIN_FILENO
%76 = call i32 @read(i32 %75, i8* %6, i32 4096)
switch i32 %76, label %83 [
i32 -1, label %77
i32 0, label %85
]
77: ; preds = %74
%78 = load i64, i64* @errno
%79 = load i64, i64* @EINTR
%80 = icmp eq i64 %78, %79
br i1 %80, label %95, label %81
81: ; preds = %77
%82 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
br label %85
83: ; preds = %74
%84 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %85
85: ; preds = %74, %81, %83, %67
%86 = phi i32 [ 1, %81 ], [ %70, %83 ], [ %70, %67 ], [ %70, %74 ]
%87 = phi i32 [ 0, %81 ], [ %69, %83 ], [ %69, %67 ], [ %76, %74 ]
%88 = load i32, i32* %71
%89 = icmp eq i32 %88, %13
br i1 %89, label %90, label %95
90: ; preds = %85
%91 = call i32 @read_active_tty(i32 %13, i32 1)
%92 = icmp eq i32 %91, 0
%93 = select i1 %92, i32 1, i32 %86
%94 = select i1 %92, i32 0, i32 %87
br label %95
95: ; preds = %90, %85, %77
%96 = phi i32 [ %70, %77 ], [ %86, %85 ], [ %93, %90 ]
%97 = phi i32 [ %69, %77 ], [ %87, %85 ], [ %94, %90 ]
%98 = add nuw nsw i64 %68, 1
%99 = icmp eq i64 %98, %64
br i1 %99, label %53, label %67
100: ; preds = %53, %65
%101 = phi i32 [ 1, %65 ], [ %54, %53 ]
%102 = load i32, i32* @EPOLL_CTL_DEL
%103 = call i32 @epoll_ctl(i32 %26, i32 %102, i32 %13, %struct.epoll_event* %1)
%104 = icmp eq i32 %103, -1
br i1 %104, label %105, label %107
105: ; preds = %100
%106 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0))
br label %107
107: ; preds = %105, %100
%108 = phi i32 [ 1, %105 ], [ %101, %100 ]
%109 = call i32 @close(i32 %13)
%110 = load i32, i32* @EPOLL_CTL_DEL
%111 = load i32, i32* @STDIN_FILENO
%112 = call i32 @epoll_ctl(i32 %26, i32 %110, i32 %111, %struct.epoll_event* %1)
%113 = icmp eq i32 %112, -1
br i1 %113, label %114, label %116
114: ; preds = %107
%115 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.9, i64 0, i64 0))
br label %116
116: ; preds = %114, %107
%117 = phi i32 [ 1, %114 ], [ %108, %107 ]
%118 = call i32 @close(i32 %26)
br label %119
119: ; preds = %20, %17, %116, %49, %39, %28, %15
%120 = phi i32 [ 1, %15 ], [ 1, %28 ], [ 1, %39 ], [ 1, %49 ], [ %117, %116 ], [ 1, %17 ], [ 0, %20 ]
ret i32 %120
}