#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;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int POLLIN;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int main(int argc, char *argv[]) {
  struct pollfd ufds[1];
  int timeout = 10000;
  int fd, rc, nbytes = 32;
  char *buffer, *filename = "/dev/mycdrv";
  if (argc > 1)
    filename = argv[1];
  if (argc > 2)
    nbytes = atoi(argv[2]);
  buffer = malloc(nbytes);
  fd = open(filename, O_RDONLY);
  printf("opened file: %s,  with file descriptor = %d\n", filename, fd);
  ufds[0].fd = fd;
  ufds[0].events = POLLIN;
  for (;;) {
    if ((rc = poll(ufds, 1, timeout)) < 0) {
      perror("Failure in poll\n");
      exit(EXIT_FAILURE);
    }
    if (rc > 0) {
      printf(" poll returns %d, revents = 0x%03x", rc, ufds[0].revents);
      if (ufds[0].revents & POLLIN) {
        rc = read(fd, buffer, nbytes);
        printf("reading %d bytes:%s\n", rc, buffer);
      } else {
        printf("POLLIN not set!\n");
      }
    } else {
      printf("poll timed out in %d milliseconds on %s.\n", timeout, filename);
    }
  }
  close(fd);
  printf("Shutting down %s\n", argv[0]);
  exit(EXIT_SUCCESS);
}


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)
movl   $0x2710,-0x24(%rbp)
movl   $0x20,-0x30(%rbp)
lea    0xe55(%rip),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11c5 <main+0x45>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11df <main+0x5f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x28(%rbp)
mov    -0x40(%rbp),%rsi
mov    -0x28(%rbp),%edx
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    $0x1,%esi
call   1060 <poll@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    125e <main+0xde>
lea    0xdee(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x2c(%rbp)
jle    12d0 <main+0x150>
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    0x2d5a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12bd <main+0x13d>
mov    -0x28(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
mov    -0x38(%rbp),%rdx
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12cb <main+0x14b>
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12e5 <main+0x165>
mov    -0x24(%rbp),%esi
mov    -0x40(%rbp),%rdx
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1227 <main+0xa7>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movl   $0x20,0x4(%rsp)
cmp    $0x2,%edi
jl     11c0 <main+0x30>
mov    0x8(%rsi),%rbp
je     11c7 <main+0x37>
mov    0x10(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,0x4(%rsp)
jmp    11c7 <main+0x37>
lea    0xe39(%rip),%rbp
mov    0x4(%rsp),%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
mov    %eax,%r13d
lea    0xe1e(%rip),%rdi
mov    %rbp,%r15
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13d,0x8(%rsp)
mov    0x2dd7(%rip),%r14
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2710,%edx
call   1070 <poll@plt>
test   %eax,%eax
jns    1242 <main+0xb2>
lea    0xe0a(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0x8(%rsp),%rbp
lea    0xdfc(%rip),%r12
jmp    127c <main+0xec>
lea    0xe2b(%rip),%rdi
mov    $0x2710,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x2710,%edx
call   1070 <poll@plt>
test   %eax,%eax
js     1228 <main+0x98>
test   %eax,%eax
je     1250 <main+0xc0>
mov    0x10(%rsp),%edx
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r14),%eax
test   %eax,0x10(%rsp)
je     12c0 <main+0x130>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    0x4(%rsp),%edx
call   1050 <read@plt>
lea    0xdbe(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
jmp    125f <main+0xcf>
cs nopw 0x0(%rax,%rax,1)
lea    0xde5(%rip),%rdi
call   1030 <puts@plt>
jmp    1266 <main+0xd6>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [12 x i8] c"/dev/mycdrv\00"
@O_RDONLY = global i32 0
@.str.1 = [45 x i8] c"opened file: %s, with file descriptor = %d\0A\00"
@POLLIN = global i32 0
@.str.2 = [17 x i8] c"Failure in poll\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [35 x i8] c" poll returns %d, revents = 0x%03x\00"
@.str.4 = [21 x i8] c"reading %d bytes:%s\0A\00"
@.str.5 = [17 x i8] c"POLLIN not set!\0A\00"
@.str.6 = [42 x i8] c"poll timed out in %d milliseconds on %s.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1 x %struct.pollfd]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 10000, i32* %7
store i32 32, i32* %10
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %12
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %12
br label %19
19: ; preds = %15, %2
%20 = load i32, i32* %4
%21 = icmp sgt i32 %20, 2
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %10
br label %27
27: ; preds = %22, %19
%28 = load i32, i32* %10
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %11
%30 = load i8*, i8** %12
%31 = load i32, i32* @O_RDONLY
%32 = call i32 @open(i8* %30, i32 %31)
store i32 %32, i32* %8
%33 = load i8*, i8** %12
%34 = load i32, i32* %8
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %33, i32 %34)
%36 = load i32, i32* %8
%37 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%38 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %37, i32 0, i32 0
store i32 %36, i32* %38
%39 = load i32, i32* @POLLIN
%40 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%41 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %40, i32 0, i32 1
store i32 %39, i32* %41
br label %42
42: ; preds = %81, %27
%43 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%44 = load i32, i32* %7
%45 = call i32 @poll(%struct.pollfd* %43, i32 1, i32 %44)
store i32 %45, i32* %9
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %42
%52 = load i32, i32* %9
%53 = icmp sgt i32 %52, 0
br i1 %53, label %54, label %77
54: ; preds = %51
%55 = load i32, i32* %9
%56 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%57 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %56, i32 0, i32 2
%58 = load i32, i32* %57
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %55, i32 %58)
%60 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%61 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %60, i32 0, i32 2
%62 = load i32, i32* %61
%63 = load i32, i32* @POLLIN
%64 = and i32 %62, %63
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %74
66: ; preds = %54
%67 = load i32, i32* %8
%68 = load i8*, i8** %11
%69 = load i32, i32* %10
%70 = call i32 @read(i32 %67, i8* %68, i32 %69)
store i32 %70, i32* %9
%71 = load i32, i32* %9
%72 = load i8*, i8** %11
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %71, i8* %72)
br label %76
74: ; preds = %54
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %76
76: ; preds = %74, %66
br label %81
77: ; preds = %51
%78 = load i32, i32* %7
%79 = load i8*, i8** %12
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i32 %78, i8* %79)
br label %81
81: ; preds = %77, %76
br label %42
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@.str = [12 x i8] c"/dev/mycdrv\00"
@O_RDONLY = global i32 0
@.str.1 = [45 x i8] c"opened file: %s, with file descriptor = %d\0A\00"
@POLLIN = global i32 0
@.str.2 = [17 x i8] c"Failure in poll\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [35 x i8] c" poll returns %d, revents = 0x%03x\00"
@.str.4 = [21 x i8] c"reading %d bytes:%s\0A\00"
@.str.6 = [42 x i8] c"poll timed out in %d milliseconds on %s.\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [16 x i8] c"POLLIN not set!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1 x %struct.pollfd]
%4 = bitcast [1 x %struct.pollfd]* %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %14
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = icmp eq i32 %0, 2
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
br label %14
14: ; preds = %2, %10, %6
%15 = phi i8* [ %8, %10 ], [ %8, %6 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %2 ]
%16 = phi i32 [ %13, %10 ], [ 32, %6 ], [ 32, %2 ]
%17 = tail call i8* @malloc(i32 %16)
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* %15, i32 %18)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %19)
%21 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0
%22 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 0
store i32 %19, i32* %22
%23 = load i32, i32* @POLLIN
%24 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 1
store i32 %23, i32* %24
%25 = call i32 @poll(%struct.pollfd* %21, i32 1, i32 10000)
%26 = icmp slt i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %14
%28 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 2
br label %33
29: ; preds = %50, %14
%30 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %27, %50
%34 = phi i32 [ %25, %27 ], [ %51, %50 ]
%35 = icmp eq i32 %34, 0
br i1 %35, label %48, label %36
36: ; preds = %33
%37 = load i32, i32* %28
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %34, i32 %37)
%39 = load i32, i32* %28
%40 = load i32, i32* @POLLIN
%41 = and i32 %40, %39
%42 = icmp eq i32 %41, 0
br i1 %42, label %46, label %43
43: ; preds = %36
%44 = call i32 @read(i32 %19, i8* %17, i32 %16)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %44, i8* %17)
br label %50
46: ; preds = %36
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %50
48: ; preds = %33
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i32 10000, i8* %15)
br label %50
50: ; preds = %43, %46, %48
%51 = call i32 @poll(%struct.pollfd* %21, i32 1, i32 10000)
%52 = icmp slt i32 %51, 0
br i1 %52, label %29, label %33
}