#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;
  scalar_t__ fd;
};
typedef int nfds_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int POLLIN;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int main() {
  struct pollfd pfd;
  nfds_t nfds;
  int timeout;
  int retval;
  char buf[1024] = {0};
  pfd.fd = 0;
  pfd.events = POLLIN;
  nfds = 1;
  timeout = 5000;
  retval = poll(&pfd, nfds, timeout);
  if (retval == -1)
    perror("poll()");
  else if (retval) {
    printf("Data is available now.\n");
    printf("pollfd.revents & POLLIN = %d\n", pfd.revents & POLLIN);
    read(0, buf, 1024);
    printf("buf = %s\n", buf);
  } else {
    printf("No data within five seconds.\n");
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
movq   $0x0,-0x10(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x1388,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    -0x18(%rbp),%rdi
call   1060 <poll@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11d4 <main+0x74>
lea    0xe36(%rip),%rdi
call   1070 <perror@plt>
jmp    1246 <main+0xe6>
cmpl   $0x0,-0x24(%rbp)
je     1233 <main+0xd3>
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%esi
mov    0x2df2(%rip),%rax
and    (%rax),%esi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x430(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1050 <read@plt>
lea    -0x430(%rbp),%rsi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1241 <main+0xe1>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1246 <main+0xe6>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x410,%rsp
lea    0x10(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1050 <memset@plt>
movq   $0x0,0x8(%rsp)
mov    0x2e4f(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x1388,%edx
call   1070 <poll@plt>
cmp    $0xffffffff,%eax
jne    11cf <main+0x5f>
lea    0xe44(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
test   %eax,%eax
jne    11e1 <main+0x71>
lea    0xe55(%rip),%rdi
call   1030 <puts@plt>
jmp    11c1 <main+0x51>
lea    0xe64(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%esi
and    0x4(%rsp),%esi
lea    0xe0d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x10(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
lea    0xe09(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c1 <main+0x51>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i64 }
@POLLIN = global i32 0
@.str = [7 x i8] c"poll()\00"
@.str.1 = [24 x i8] c"Data is available now.\0A\00"
@.str.2 = [30 x i8] c"pollfd.revents & POLLIN = %d\0A\00"
@.str.3 = [10 x i8] c"buf = %s\0A\00"
@.str.4 = [30 x i8] c"No data within five seconds.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.pollfd
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
store i32 0, i32* %1
%7 = bitcast [1024 x i8]* %6 to i8*
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i32 0, i32 2
store i64 0, i64* %8
%9 = load i32, i32* @POLLIN
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i32 0, i32 0
store i32 %9, i32* %10
store i32 1, i32* %3
store i32 5000, i32* %4
%11 = load i32, i32* %3
%12 = load i32, i32* %4
%13 = call i32 @poll(%struct.pollfd* %2, i32 %11, i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %35
18: ; preds = %0
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%23 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load i32, i32* @POLLIN
%26 = and i32 %24, %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%29 = call i32 @read(i32 0, i8* %28, i32 1024)
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %34
32: ; preds = %18
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %21
br label %35
35: ; preds = %34, %16
%36 = load i32, i32* @EXIT_SUCCESS
%37 = call i32 @exit(i32 %36)
unreachable
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i64 }
@POLLIN = global i32 0
@.str = [7 x i8] c"poll()\00"
@.str.2 = [30 x i8] c"pollfd.revents & POLLIN = %d\0A\00"
@.str.3 = [10 x i8] c"buf = %s\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [29 x i8] c"No data within five seconds.\00"
@str.5 = [23 x i8] c"Data is available now.\00"
define i32 @main() {
%1 = alloca %struct.pollfd
%2 = alloca [1024 x i8]
%3 = bitcast %struct.pollfd* %1 to i8*
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 0, i32 2
store i64 0, i64* %5
%6 = load i32, i32* @POLLIN
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 0, i32 0
store i32 %6, i32* %7
%8 = call i32 @poll(%struct.pollfd* %1, i32 1, i32 5000)
switch i32 %8, label %11 [
i32 -1, label %9
i32 0, label %20
]
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %22
11: ; preds = %0
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.5, i64 0, i64 0))
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 0, i32 1
%14 = load i32, i32* %13
%15 = load i32, i32* @POLLIN
%16 = and i32 %15, %14
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = call i32 @read(i32 0, i8* %4, i32 1024)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %4)
br label %22
20: ; preds = %0
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %11, %20, %9
%23 = load i32, i32* @EXIT_SUCCESS
%24 = call i32 @exit(i32 %23)
unreachable
}