#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 */
struct pollfd {
  int fd;
  int events;
  int revents;
};
typedef int /*<<< orphan*/ fds;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int POLLPRI;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct pollfd *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int fd;
  struct pollfd fds[1];
  char buffer[16];
  fd = open("/sys/class/tty/tty0/active", O_RDONLY);
  if (fd < 0) {
    perror("Cannot open "
           "/sys/class/tty/tty0/active");
    return 1;
  }
  memset(fds, 0, sizeof(fds));
  fds[0].fd = fd;
  fds[0].events = POLLPRI;
  while (1) {
    int n = poll(fds, 1, -1);
    if (n <= 0) {
      perror("poll error.");
      return 1;
    }
    if (fds[0].revents & POLLPRI) {
      lseek(fd, 0, SEEK_SET);
      n = read(fd, buffer, 16);
      if (n <= 0) {
        perror("Cannot read from "
               "/sys/class/tty/tty0/active"
               " file.");
        return 1;
      }
      fwrite(buffer, n, 1, stdout);
      fflush(stdout);
    } else {
      fprintf(stderr, "Unknown poll event.\n");
      return 1;
    }
  }
  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)
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0xe56(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e0 <main+0x50>
lea    0xe31(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12eb <main+0x15b>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     123a <main+0xaa>
lea    0xdfe(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12eb <main+0x15b>
mov    -0x18(%rbp),%eax
mov    0x2dac(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12c5 <main+0x135>
mov    -0x14(%rbp),%edi
mov    0x2d6f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1050 <read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     1298 <main+0x108>
lea    0xdac(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12eb <main+0x15b>
lea    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    0x2d1a(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
jmp    12e6 <main+0x156>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
lea    0xd90(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12eb <main+0x15b>
jmp    1202 <main+0x72>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0xe5e(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     128a <main+0xfa>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    %ebx,(%rsp)
mov    0x2e17(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
test   %eax,%eax
jle    126a <main+0xda>
mov    0x2dca(%rip),%r13
lea    0x10(%rsp),%r14
mov    0x2db6(%rip),%rbp
mov    %rsp,%r15
nopl   (%rax)
mov    (%r12),%eax
test   %eax,0x8(%rsp)
je     1273 <main+0xe3>
mov    0x0(%r13),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1293 <main+0x103>
mov    0x0(%rbp),%ecx
mov    %r14,%rdi
mov    %eax,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    0x0(%rbp),%edi
call   1070 <fflush@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
test   %eax,%eax
jg     1210 <main+0x80>
lea    0xdb6(%rip),%rdi
jmp    129a <main+0x10a>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
call   1060 <fprintf@plt>
jmp    129f <main+0x10f>
lea    0xd6f(%rip),%rdi
jmp    129a <main+0x10a>
lea    0xd99(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [27 x i8] c"/sys/class/tty/tty0/active\00"
@O_RDONLY = global i32 0
@.str.1 = [39 x i8] c"Cannot open /sys/class/tty/tty0/active\00"
@POLLPRI = global i32 0
@.str.2 = [12 x i8] c"poll error.\00"
@SEEK_SET = global i32 0
@.str.3 = [50 x i8] c"Cannot read from /sys/class/tty/tty0/active file.\00"
@stdout = global i32 0
@stderr = global i32 0
@.str.4 = [21 x i8] c"Unknown poll event.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1 x %struct.pollfd]
%8 = alloca [16 x i8]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %61
16: ; preds = %2
%17 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%18 = call i32 @memset(%struct.pollfd* %17, i32 0, i32 12)
%19 = load i32, i32* %6
%20 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = load i32, i32* @POLLPRI
%23 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%24 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %23, i32 0, i32 1
store i32 %22, i32* %24
br label %25
25: ; preds = %16, %60
%26 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%27 = call i32 @poll(%struct.pollfd* %26, i32 1, i32 -1)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp sle i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %61
32: ; preds = %25
%33 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %7, i64 0, i64 0
%34 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %33, i32 0, i32 2
%35 = load i32, i32* %34
%36 = load i32, i32* @POLLPRI
%37 = and i32 %35, %36
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %57
39: ; preds = %32
%40 = load i32, i32* %6
%41 = load i32, i32* @SEEK_SET
%42 = call i32 @lseek(i32 %40, i32 0, i32 %41)
%43 = load i32, i32* %6
%44 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 16)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = icmp sle i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %39
%49 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %61
50: ; preds = %39
%51 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%52 = load i32, i32* %9
%53 = load i32, i32* @stdout
%54 = call i32 @fwrite(i8* %51, i32 %52, i32 1, i32 %53)
%55 = load i32, i32* @stdout
%56 = call i32 @fflush(i32 %55)
br label %60
57: ; preds = %32
%58 = load i32, i32* @stderr
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %61
60: ; preds = %50
br label %25
61: ; preds = %57, %48, %30, %14
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@.str = [27 x i8] c"/sys/class/tty/tty0/active\00"
@O_RDONLY = global i32 0
@.str.1 = [39 x i8] c"Cannot open /sys/class/tty/tty0/active\00"
@POLLPRI = global i32 0
@.str.2 = [12 x i8] c"poll error.\00"
@SEEK_SET = global i32 0
@.str.3 = [50 x i8] c"Cannot read from /sys/class/tty/tty0/active file.\00"
@stdout = global i32 0
@stderr = global i32 0
@.str.4 = [21 x i8] c"Unknown poll event.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1 x %struct.pollfd]
%4 = alloca [16 x i8]
%5 = bitcast [1 x %struct.pollfd]* %3 to i8*
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([27 x i8], [27 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 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %46
12: ; preds = %2
%13 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0
%14 = call i32 @memset(%struct.pollfd* %13, i32 0, i32 12)
%15 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 0
store i32 %8, i32* %15
%16 = load i32, i32* @POLLPRI
%17 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 1
store i32 %16, i32* %17
%18 = call i32 @poll(%struct.pollfd* %13, i32 1, i32 -1)
%19 = icmp slt i32 %18, 1
br i1 %19, label %22, label %20
20: ; preds = %12
%21 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 2
br label %24
22: ; preds = %39, %12
%23 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %46
24: ; preds = %20, %39
%25 = load i32, i32* %21
%26 = load i32, i32* @POLLPRI
%27 = and i32 %26, %25
%28 = icmp eq i32 %27, 0
br i1 %28, label %36, label %29
29: ; preds = %24
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @lseek(i32 %8, i32 0, i32 %30)
%32 = call i32 @read(i32 %8, i8* %6, i32 16)
%33 = icmp slt i32 %32, 1
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
br label %46
36: ; preds = %24
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %46
39: ; preds = %29
%40 = load i32, i32* @stdout
%41 = call i32 @fwrite(i8* %6, i32 %32, i32 1, i32 %40)
%42 = load i32, i32* @stdout
%43 = call i32 @fflush(i32 %42)
%44 = call i32 @poll(%struct.pollfd* %13, i32 1, i32 -1)
%45 = icmp slt i32 %44, 1
br i1 %45, label %22, label %24
46: ; preds = %36, %34, %22, %10
ret i32 1
}