#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, 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(void) {
  struct pollfd fds[1];
  int ret, rfd;
  int len;
  char rbuf[23];
  int count;
  rfd = open("/dev/poll_dev", O_RDONLY);
  fds[0].fd = rfd;
  fds[0].events = POLLIN;
  ret = poll(fds, 1, 100 * 1000);
  if (ret == -1) {
    perror("poll");
    return -1;
  }
  if (!ret) {
    printf("%d seconds elapsed, \n", 100);
    return 0;
  }
  if (fds[0].revents & POLLIN) {
    printf("fd is readable\n");
    memset(rbuf, 0, 23);
    ret = read(rfd, rbuf, 23);
    printf("\nREAD %d byte: %s\n", ret, rbuf);
    if (ret == -1) {
      perror("read");
      exit(EXIT_FAILURE);
    }
    printf("\nREAD %d byte: %s\n", ret, rbuf);
  }
  close(rfd);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0x1,%esi
mov    $0x186a0,%edx
call   1070 <poll@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f1 <main+0x71>
lea    0xe2e(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <main+0x145>
cmpl   $0x0,-0x14(%rbp)
jne    121a <main+0x9a>
lea    0xe11(%rip),%rdi
mov    $0x64,%esi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c5 <main+0x145>
mov    -0x8(%rbp),%eax
mov    0x2dbc(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12af <main+0x12f>
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x17,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x17,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    -0x40(%rbp),%rdx
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0xffffffff,-0x14(%rbp)
jne    129a <main+0x11a>
lea    0xdc5(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%esi
lea    -0x40(%rbp),%rdx
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
lea    0xe58(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
mov    %eax,(%rsp)
mov    0x2e27(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x186a0,%edx
call   1080 <poll@plt>
test   %eax,%eax
jne    11f6 <main+0x66>
lea    0xe36(%rip),%rdi
xor    %ebx,%ebx
mov    $0x64,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
cmp    $0xffffffff,%eax
jne    120e <main+0x7e>
lea    0xe0c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11eb <main+0x5b>
mov    0x0(%rbp),%eax
test   %eax,0x8(%rsp)
je     128f <main+0xff>
lea    0xe23(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x17,%edx
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x17,%edx
call   1070 <read@plt>
mov    %eax,%ebp
lea    0xdda(%rip),%rdi
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0xffffffff,%ebp
jne    127a <main+0xea>
lea    0xdd5(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xda8(%rip),%rdi
lea    0x10(%rsp),%rdx
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [14 x i8] c"/dev/poll_dev\00"
@O_RDONLY = global i32 0
@POLLIN = global i32 0
@.str.1 = [5 x i8] c"poll\00"
@.str.2 = [22 x i8] c"%d seconds elapsed, \0A\00"
@.str.3 = [16 x i8] c"fd is readable\0A\00"
@.str.4 = [19 x i8] c"\0AREAD %d byte: %s\0A\00"
@.str.5 = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [1 x %struct.pollfd]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [23 x i8]
%7 = alloca i32
store i32 0, i32* %1
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 0
store i32 %10, i32* %12
%13 = load i32, i32* @POLLIN
%14 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 1
store i32 %13, i32* %15
%16 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0
%17 = call i32 @poll(%struct.pollfd* %16, i32 1, i32 100000)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %59
22: ; preds = %0
%23 = load i32, i32* %3
%24 = icmp ne i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 100)
store i32 0, i32* %1
br label %59
27: ; preds = %22
%28 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %2, i64 0, i64 0
%29 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %28, i32 0, i32 2
%30 = load i32, i32* %29
%31 = load i32, i32* @POLLIN
%32 = and i32 %30, %31
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %54
34: ; preds = %27
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%36 = getelementptr inbounds [23 x i8], [23 x i8]* %6, i64 0, i64 0
%37 = call i32 @memset(i8* %36, i32 0, i32 23)
%38 = load i32, i32* %4
%39 = getelementptr inbounds [23 x i8], [23 x i8]* %6, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 23)
store i32 %40, i32* %3
%41 = load i32, i32* %3
%42 = getelementptr inbounds [23 x i8], [23 x i8]* %6, i64 0, i64 0
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %41, i8* %42)
%44 = load i32, i32* %3
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %50
46: ; preds = %34
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %34
%51 = load i32, i32* %3
%52 = getelementptr inbounds [23 x i8], [23 x i8]* %6, i64 0, i64 0
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %51, i8* %52)
br label %54
54: ; preds = %50, %27
%55 = load i32, i32* %4
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* @EXIT_SUCCESS
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %25, %20
%60 = load i32, i32* %1
ret i32 %60
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@.str = [14 x i8] c"/dev/poll_dev\00"
@O_RDONLY = global i32 0
@POLLIN = global i32 0
@.str.1 = [5 x i8] c"poll\00"
@.str.2 = [22 x i8] c"%d seconds elapsed, \0A\00"
@.str.4 = [19 x i8] c"\0AREAD %d byte: %s\0A\00"
@.str.5 = [5 x i8] c"read\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [15 x i8] c"fd is readable\00"
define i32 @main() {
%1 = alloca [1 x %struct.pollfd]
%2 = alloca [23 x i8]
%3 = bitcast [1 x %struct.pollfd]* %1 to i8*
%4 = getelementptr inbounds [23 x i8], [23 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %1, i64 0, i64 0
%8 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %1, i64 0, i64 0, i32 0
store i32 %6, i32* %8
%9 = load i32, i32* @POLLIN
%10 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %1, i64 0, i64 0, i32 1
store i32 %9, i32* %10
%11 = call i32 @poll(%struct.pollfd* %7, i32 1, i32 100000)
switch i32 %11, label %16 [
i32 -1, label %12
i32 0, label %14
]
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %38
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 100)
br label %38
16: ; preds = %0
%17 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %1, i64 0, i64 0, i32 2
%18 = load i32, i32* %17
%19 = load i32, i32* @POLLIN
%20 = and i32 %19, %18
%21 = icmp eq i32 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %16
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%24 = call i32 @memset(i8* %4, i32 0, i32 23)
%25 = call i32 @read(i32 %6, i8* %4, i32 23)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %25, i8* %4)
%27 = icmp eq i32 %25, -1
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %22
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %25, i8* %4)
br label %34
34: ; preds = %32, %16
%35 = call i32 @close(i32 %6)
%36 = load i32, i32* @EXIT_SUCCESS
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %14, %12
%39 = phi i32 [ -1, %12 ], [ 0, %14 ]
ret i32 %39
}