#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 /*<<< orphan*/ events;
};
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ POLLPRI;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  int fd;
  struct pollfd pfd;
  const int BUFFER_SIZE = 1024;
  int count;
  char buffer[BUFFER_SIZE];
  if (argc < 2) {
    printf("Usage: %s file\n", argv[0]);
    exit(2);
  }
  fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    perror(argv[1]);
    exit(1);
  }
  pfd.fd = fd;
  pfd.events = POLLPRI;
  while (1) {
    while (count = read(fd, buffer, sizeof(buffer) - 1)) {
      if (count < 0) {
        perror("read");
        exit(1);
      }
      write(STDOUT_FILENO, buffer, count);
    }
    lseek(fd, 0, SEEK_SET);
    if (poll(&pfd, 1, -1) < 0) {
      perror("poll");
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x400,-0x24(%rbp)
mov    %rsp,%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ce <main+0x4e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe41(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1208 <main+0x88>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    121f <main+0x9f>
jmp    1224 <main+0xa4>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
je     1281 <main+0x101>
cmpl   $0x0,-0x28(%rbp)
jge    1264 <main+0xe4>
lea    0xdbb(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    -0x430(%rbp),%rsi
call   1030 <write@plt>
jmp    1224 <main+0xa4>
mov    -0x14(%rbp),%edi
mov    0x2d3d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1070 <poll@plt>
cmp    $0x0,%rax
jge    12c7 <main+0x147>
lea    0xd5d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    121a <main+0x9a>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11b0 <main+0x30>
mov    (%rbx),%rsi
lea    0xe5f(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    0x8(%rbx),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
jns    11cf <main+0x4f>
mov    0x8(%rbx),%rdi
jmp    124e <main+0xce>
mov    %eax,%ebp
mov    %eax,0x8(%rsp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x10(%rsp),%rbx
mov    0x2dda(%rip),%r15
lea    0x8(%rsp),%r14
mov    0x2df6(%rip),%r12
jmp    1220 <main+0xa0>
mov    (%r15),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0xffffffff,%edx
call   1070 <poll@plt>
test   %rax,%rax
js     125d <main+0xdd>
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
test   %eax,%eax
je     11fc <main+0x7c>
test   %eax,%eax
js     1247 <main+0xc7>
mov    (%r12),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1220 <main+0xa0>
lea    0xdc2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdb1(%rip),%rdi
jmp    124e <main+0xce>

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

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@.str = [16 x i8] c"Usage: %s file\0A\00"
@O_RDONLY = global i32 0
@POLLPRI = global i32 0
@.str.1 = [5 x i8] c"read\00"
@STDOUT_FILENO = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [5 x i8] c"poll\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.pollfd
%4 = bitcast %struct.pollfd* %3 to i8*
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 2)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* %14, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = load i8*, i8** %13
%20 = tail call i32 @perror(i8* %19)
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %12
%23 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 0
store i32 %16, i32* %23
%24 = load i32, i32* @POLLPRI
%25 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 1
store i32 %24, i32* %25
br label %26
26: ; preds = %40, %22
%27 = call i32 @read(i32 %16, i8* %6, i32 1023)
%28 = icmp eq i32 %27, 0
br i1 %28, label %40, label %29
29: ; preds = %26, %35
%30 = phi i32 [ %38, %35 ], [ %27, %26 ]
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %29
%36 = load i32, i32* @STDOUT_FILENO
%37 = call i32 @write(i32 %36, i8* %6, i32 %30)
%38 = call i32 @read(i32 %16, i8* %6, i32 1023)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %29
40: ; preds = %35, %26
%41 = load i32, i32* @SEEK_SET
%42 = call i32 @lseek(i32 %16, i32 0, i32 %41)
%43 = call i64 @poll(%struct.pollfd* %3, i32 1, i32 -1)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %26
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
}