#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 {
  scalar_t__ fd;
  int events;
  int revents;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int POLLPRI;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ lseek(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, char);
int read(scalar_t__, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usage();
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  struct pollfd fds[1];
  char c;
  if (argc != 2)
    usage();
  fds[0].fd = open(argv[1], O_RDONLY);
  if (fds[0].fd < 0) {
    perror(argv[1]);
    exit(1);
  }
  if (read(fds[0].fd, &c, 1) != 1) {
    perror("read");
    exit(1);
  }
  fds[0].events = POLLPRI;
  fds[0].revents = 0;
  if (poll(fds, 1, -1) < 0) {
    perror("poll");
    exit(1);
  }
  if (!(fds[0].revents & POLLPRI)) {
    fprintf(stderr, "poll returned with no events\n");
    exit(1);
  }
  usleep(5000);
  if (lseek(fds[0].fd, 0, SEEK_SET) < 0) {
    perror("lseek");
    exit(1);
  }
  if (read(fds[0].fd, &c, 1) != 1) {
    perror("read");
    exit(1);
  }
  printf("%c\n", c);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c7 <main+0x27>
mov    $0x0,%al
call   1070 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1203 <main+0x63>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     1234 <main+0x94>
lea    0xddb(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
cmp    $0x0,%rax
jge    127a <main+0xda>
lea    0xd9a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%eax
mov    0x2d6c(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    12ae <main+0x10e>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
lea    0xd6b(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x1388,%edi
call   10c0 <usleep@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d05(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x0,%rax
jge    12ec <main+0x14c>
lea    0xd4b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     131d <main+0x17d>
lea    0xcf2(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd0a(%rip),%rdi
movsbl -0x21(%rbp),%esi
call   1030 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     11b4 <main+0x14>
xor    %eax,%eax
call   1070 <usage@plt>
mov    0x8(%rbx),%rdi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %rax,0x10(%rsp)
test   %rax,%rax
jns    11d9 <main+0x39>
mov    0x8(%rbx),%rdi
jmp    12ba <main+0x11a>
lea    0xf(%rsp),%rsi
mov    %rax,%rdi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    12b3 <main+0x113>
mov    0x2df5(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
movl   $0x0,0x1c(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1080 <poll@plt>
test   %rax,%rax
jns    122e <main+0x8e>
lea    0xddc(%rip),%rdi
jmp    12ba <main+0x11a>
mov    (%rbx),%eax
test   %eax,0x1c(%rsp)
jne    1255 <main+0xb5>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x1388,%edi
call   10c0 <usleep@plt>
mov    0x10(%rsp),%rdi
mov    0x2d5d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
jns    1282 <main+0xe2>
lea    0xda8(%rip),%rdi
jmp    12ba <main+0x11a>
mov    0x10(%rsp),%rdi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    12b3 <main+0x113>
movsbl 0xf(%rsp),%esi
lea    0xd87(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xd46(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i64, i32, i32 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"read\00"
@POLLPRI = global i32 0
@.str.1 = [5 x i8] c"poll\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"poll returned with no events\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [6 x i8] c"lseek\00"
@.str.4 = [4 x i8] c"%c\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1 x %struct.pollfd]
%7 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (...) @usage()
br label %12
12: ; preds = %10, %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_RDONLY
%17 = call i64 @open(i8* %15, i32 %16)
%18 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 0
store i64 %17, i64* %19
%20 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %20, i32 0, i32 0
%22 = load i64, i64* %21
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %12
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @perror(i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %12
%31 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%32 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = call i32 @read(i64 %33, i8* %7, i32 1)
%35 = icmp ne i32 %34, 1
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %30
%40 = load i32, i32* @POLLPRI
%41 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%42 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %41, i32 0, i32 1
store i32 %40, i32* %42
%43 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%44 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %43, i32 0, i32 2
store i32 0, i32* %44
%45 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%46 = call i64 @poll(%struct.pollfd* %45, i32 1, i32 -1)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %39
%49 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %39
%52 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%53 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %52, i32 0, i32 2
%54 = load i32, i32* %53
%55 = load i32, i32* @POLLPRI
%56 = and i32 %54, %55
%57 = icmp ne i32 %56, 0
br i1 %57, label %62, label %58
58: ; preds = %51
%59 = load i32, i32* @stderr
%60 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %51
%63 = call i32 @usleep(i32 5000)
%64 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%65 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %64, i32 0, i32 0
%66 = load i64, i64* %65
%67 = load i32, i32* @SEEK_SET
%68 = call i64 @lseek(i64 %66, i32 0, i32 %67)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %62
%71 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %62
%74 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%75 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %74, i32 0, i32 0
%76 = load i64, i64* %75
%77 = call i32 @read(i64 %76, i8* %7, i32 1)
%78 = icmp ne i32 %77, 1
br i1 %78, label %79, label %82
79: ; preds = %73
%80 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %73
%83 = load i8, i8* %7
%84 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8 signext %83)
%85 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.pollfd = type { i64, i32, i32 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"read\00"
@POLLPRI = global i32 0
@.str.1 = [5 x i8] c"poll\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"poll returned with no events\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [6 x i8] c"lseek\00"
@.str.4 = [4 x i8] c"%c\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1 x %struct.pollfd]
%4 = alloca i8
%5 = bitcast [1 x %struct.pollfd]* %3 to i8*
%6 = icmp eq i32 %0, 2
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 (...) @usage()
br label %9
9: ; preds = %7, %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i64 @open(i8* %11, i32 %12)
%14 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0
%15 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 0
store i64 %13, i64* %15
%16 = icmp slt i64 %13, 0
br i1 %16, label %17, label %21
17: ; preds = %9
%18 = load i8*, i8** %10
%19 = tail call i32 @perror(i8* %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %9
%22 = call i32 @read(i64 %13, i8* %4, i32 1)
%23 = icmp eq i32 %22, 1
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = load i32, i32* @POLLPRI
%29 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 1
store i32 %28, i32* %29
%30 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 2
store i32 0, i32* %30
%31 = call i64 @poll(%struct.pollfd* %14, i32 1, i32 -1)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = load i32, i32* %30
%38 = load i32, i32* @POLLPRI
%39 = and i32 %38, %37
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %36
%46 = call i32 @usleep(i32 5000)
%47 = load i64, i64* %15
%48 = load i32, i32* @SEEK_SET
%49 = call i64 @lseek(i64 %47, i32 0, i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %45
%52 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %45
%55 = load i64, i64* %15
%56 = call i32 @read(i64 %55, i8* %4, i32 1)
%57 = icmp eq i32 %56, 1
br i1 %57, label %61, label %58
58: ; preds = %54
%59 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %54
%62 = load i8, i8* %4
%63 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8 signext %62)
%64 = call i32 @exit(i32 0)
unreachable
}