#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, int);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  struct pollfd fds[2];
  int ret;
  fds[0].fd = STDIN_FILENO;
  fds[0].events = POLLIN;
  fds[1].fd = STDOUT_FILENO;
  fds[1].events = POLLOUT;
  ret = poll(fds, 2, 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)
mov    0x2e82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x2,%esi
mov    $0x1388,%edx
call   1040 <poll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11a7 <main+0x77>
lea    0xe6a(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    121d <main+0xed>
cmpl   $0x0,-0x24(%rbp)
jne    11d0 <main+0xa0>
lea    0xe4d(%rip),%rdi
mov    $0x5,%esi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    121d <main+0xed>
mov    -0x1c(%rbp),%eax
mov    0x2dfe(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11f3 <main+0xc3>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
mov    0x2dfb(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1216 <main+0xe6>
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
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 [2 x %struct.pollfd]
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @STDIN_FILENO
%5 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i32 0, i32 2
store i32 %4, i32* %6
%7 = load i32, i32* @POLLIN
%8 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %8, i32 0, i32 0
store i32 %7, i32* %9
%10 = load i32, i32* @STDOUT_FILENO
%11 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 1
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %11, i32 0, i32 2
store i32 %10, i32* %12
%13 = load i32, i32* @POLLOUT
%14 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 1
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0
%17 = call i32 @poll(%struct.pollfd* %16, i32 2, i32 5000)
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, i64 0, i64 0))
store i32 1, i32* %1
br label %46
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 ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 5)
store i32 0, i32* %1
br label %46
27: ; preds = %22
%28 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 0
%29 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %28, i32 0, i32 1
%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 %36
34: ; preds = %27
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %27
%37 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %2, i64 0, i64 1
%38 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %37, i32 0, i32 1
%39 = load i32, i32* %38
%40 = load i32, i32* @POLLOUT
%41 = and i32 %39, %40
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %36
store i32 0, i32* %1
br label %46
46: ; preds = %45, %25, %20
%47 = load i32, i32* %1
ret i32 %47
}

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 = bitcast [2 x %struct.pollfd]* %1 to i8*
%3 = load i32, i32* @STDIN_FILENO
%4 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %1, i64 0, i64 0
%5 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %1, i64 0, i64 0, i32 2
store i32 %3, i32* %5
%6 = load i32, i32* @POLLIN
%7 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %1, i64 0, i64 0, i32 0
store i32 %6, i32* %7
%8 = load i32, i32* @STDOUT_FILENO
%9 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %1, i64 0, i64 1, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* @POLLOUT
%11 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %1, i64 0, i64 1, i32 0
store i32 %10, i32* %11
%12 = call i32 @poll(%struct.pollfd* %4, i32 2, i32 5000)
switch i32 %12, label %17 [
i32 -1, label %13
i32 0, label %15
]
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %33
15: ; preds = %0
%16 = 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 %33
17: ; preds = %0
%18 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %1, i64 0, i64 0, i32 1
%19 = load i32, i32* %18
%20 = load i32, i32* @POLLIN
%21 = and i32 %20, %19
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %17
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.4, i64 0, i64 0))
br label %25
25: ; preds = %23, %17
%26 = getelementptr inbounds [2 x %struct.pollfd], [2 x %struct.pollfd]* %1, i64 0, i64 1, i32 1
%27 = load i32, i32* %26
%28 = load i32, i32* @POLLOUT
%29 = and i32 %28, %27
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %25
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %33
33: ; preds = %25, %31, %15, %13
%34 = phi i32 [ 1, %13 ], [ 0, %15 ], [ 0, %31 ], [ 0, %25 ]
ret i32 %34
}