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

/* Variables and functions */
int POLLIN;
int POLLOUT;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int const, int);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  const int numfds = 2;
  struct pollfd fds[numfds];
  int ret;
  fds[0].fd = STDIN_FILENO;
  fds[0].events = POLLIN;
  fds[1].fd = STDOUT_FILENO;
  fds[1].events = POLLOUT;
  ret = poll(fds, numfds, 5 * 1000);
  if (ret == -1) {
    perror("poll");
    return 1;
  }
  if (!ret) {
    printf("%d seconds elapsed.\n", 5);
    return 0;
  }
  if (fds[0].revents & POLLIN)
    printf("stdin is readable\n");
  if (fds[1].revents & POLLOUT)
    printf("stdout is writable\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x2,-0x8(%rbp)
mov    %rsp,%rax
mov    %rax,-0x10(%rbp)
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1388,%edx
call   1040 <poll@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11bc <main+0x8c>
lea    0xe5c(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x18(%rbp)
jmp    1240 <main+0x110>
cmpl   $0x0,-0x14(%rbp)
jne    11ec <main+0xbc>
lea    0xe38(%rip),%rdi
mov    $0x5,%esi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x18(%rbp)
jmp    1240 <main+0x110>
mov    -0x2c(%rbp),%eax
mov    0x2de2(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     120f <main+0xdf>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
mov    0x2ddf(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1232 <main+0x102>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e7d(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,(%rsp)
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e84(%rip),%r14
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
mov    %rsp,%rdi
mov    $0x2,%esi
mov    $0x1388,%edx
call   1050 <poll@plt>
test   %eax,%eax
je     11c3 <main+0x83>
cmp    $0xffffffff,%eax
je     11e2 <main+0xa2>
mov    (%rbx),%eax
test   %eax,0x4(%rsp)
je     11aa <main+0x6a>
lea    0xe88(%rip),%rdi
call   1030 <puts@plt>
mov    (%r14),%eax
xor    %ebx,%ebx
test   %eax,0x10(%rsp)
je     11d8 <main+0x98>
lea    0xe5e(%rip),%rdi
call   1030 <puts@plt>
jmp    11d8 <main+0x98>
lea    0xe3b(%rip),%rdi
xor    %ebx,%ebx
mov    $0x5,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe17(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%ebx
jmp    11d8 <main+0x98>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@STDOUT_FILENO = global i32 0
@POLLOUT = global i32 0
@.str = [5 x i8] c"poll\00"
@.str.1 = [21 x i8] c"%d seconds elapsed.\0A\00"
@.str.2 = [19 x i8] c"stdin is readable\0A\00"
@.str.3 = [20 x i8] c"stdout is writable\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 2, i32* %2
%6 = call i8* @llvm.stacksave()
store i8* %6, i8** %3
%7 = alloca %struct.pollfd, i64 2
%8 = load i32, i32* @STDIN_FILENO
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 0
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 2
store i32 %8, i32* %10
%11 = load i32, i32* @POLLIN
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 0
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = load i32, i32* @STDOUT_FILENO
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 1
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %15, i32 0, i32 2
store i32 %14, i32* %16
%17 = load i32, i32* @POLLOUT
%18 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 1
%19 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = call i32 @poll(%struct.pollfd* %7, i32 2, i32 5000)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %0
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
store i32 1, i32* %5
br label %49
25: ; preds = %0
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 5)
store i32 0, i32* %1
store i32 1, i32* %5
br label %49
30: ; preds = %25
%31 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 0
%32 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = load i32, i32* @POLLIN
%35 = and i32 %33, %34
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %30
%40 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 1
%41 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %40, i32 0, i32 1
%42 = load i32, i32* %41
%43 = load i32, i32* @POLLOUT
%44 = and i32 %42, %43
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %46, %39
store i32 0, i32* %1
store i32 1, i32* %5
br label %49
49: ; preds = %48, %28, %23
%50 = load i8*, i8** %3
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@STDOUT_FILENO = global i32 0
@POLLOUT = global i32 0
@.str = [5 x i8] c"poll\00"
@.str.1 = [21 x i8] c"%d seconds elapsed.\0A\00"
@str = [19 x i8] c"stdout is writable\00"
@str.4 = [18 x i8] c"stdin is readable\00"
define i32 @main() {
%1 = alloca [2 x %struct.pollfd]
%2 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %1, i64 0, i64 0
%3 = load i32, i32* @STDIN_FILENO
%4 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %1, i64 0, i64 0, i32 2
store i32 %3, i32* %4
%5 = load i32, i32* @POLLIN
%6 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %1, i64 0, i64 0, i32 0
store i32 %5, i32* %6
%7 = load i32, i32* @STDOUT_FILENO
%8 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %1, i64 0, i64 1, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @POLLOUT
%10 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %1, i64 0, i64 1, i32 0
store i32 %9, i32* %10
%11 = call i32 @poll(%struct.pollfd* %2, i32 2, i32 5000)
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, i64 0, i64 0))
br label %32
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 5)
br label %32
16: ; preds = %0
%17 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %1, i64 0, i64 0, i32 1
%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 %24, label %22
22: ; preds = %16
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.4, i64 0, i64 0))
br label %24
24: ; preds = %22, %16
%25 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %1, i64 0, i64 1, i32 1
%26 = load i32, i32* %25
%27 = load i32, i32* @POLLOUT
%28 = and i32 %27, %26
%29 = icmp eq i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %24
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %32
32: ; preds = %24, %30, %14, %12
%33 = phi i32 [ 1, %12 ], [ 0, %14 ], [ 0, %30 ], [ 0, %24 ]
ret i32 %33
}