#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 /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int const, int);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  const int numfds = 1;
  struct pollfd fds[numfds];
  int ret;
  fds[0].fd = STDIN_FILENO;
  fds[0].events = POLLIN;
  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");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x8(%rbp)
mov    %rsp,%rax
mov    %rax,-0x10(%rbp)
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x1,%esi
mov    $0x1388,%edx
call   1040 <poll@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a4 <main+0x74>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x18(%rbp)
jmp    1205 <main+0xd5>
cmpl   $0x0,-0x14(%rbp)
jne    11d4 <main+0xa4>
lea    0xe50(%rip),%rdi
mov    $0x5,%esi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x18(%rbp)
jmp    1205 <main+0xd5>
mov    -0x2c(%rbp),%eax
mov    0x2e0a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11f7 <main+0xc7>
lea    0xe2a(%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   %rbx
sub    $0x10,%rsp
mov    0x2e8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e8f(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x1388,%edx
call   1050 <poll@plt>
test   %eax,%eax
je     1191 <main+0x51>
cmp    $0xffffffff,%eax
je     11ae <main+0x6e>
mov    (%rbx),%eax
xor    %ebx,%ebx
test   %eax,0x4(%rsp)
je     11a6 <main+0x66>
lea    0xe90(%rip),%rdi
call   1030 <puts@plt>
jmp    11a6 <main+0x66>
lea    0xe6d(%rip),%rdi
xor    %ebx,%ebx
mov    $0x5,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe4b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%ebx
jmp    11a6 <main+0x66>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@STDIN_FILENO = global i32 0
@POLLIN = 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"
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 1, i32* %2
%6 = call i8* @llvm.stacksave()
store i8* %6, i8** %3
%7 = alloca %struct.pollfd, i64 1
%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 = call i32 @poll(%struct.pollfd* %7, i32 1, i32 5000)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = 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 %34
19: ; preds = %0
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = 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 %34
24: ; preds = %19
%25 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 0
%26 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = load i32, i32* @POLLIN
%29 = and i32 %27, %28
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %24
store i32 0, i32* %1
store i32 1, i32* %5
br label %34
34: ; preds = %33, %22, %17
%35 = load i8*, i8** %3
%36 = load i32, i32* %1
ret i32 %36
}

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