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

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ POLLIN;
int POLLNVAL;
int /*<<< orphan*/ STDIN_FILENO;
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(void) {
  printf("Wait 5 seconds for poll() to time out, or press a key to cause it to "
         "return before it times out.\n");
  struct pollfd fds;
  fds.fd = STDIN_FILENO;
  fds.events = POLLIN;
  int retval = poll(&fds, 1, 5000);
  if (retval == -1)
    perror("poll()");
  else if (retval == 0)
    printf("Timed out. No data within five seconds.\n");
  else {
    printf("Data is available now.\n");
    if (fds.revents & POLLNVAL) {
      printf("file descriptor was bad\n");
    }
  }
  if (1) {
    char buf[1024];
    read(STDIN_FILENO, buf, 1024);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe97(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0x1,%esi
mov    $0x1388,%edx
call   1050 <poll@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11b7 <main+0x67>
lea    0xeb5(%rip),%rdi
call   1060 <perror@plt>
jmp    1209 <main+0xb9>
cmpl   $0x0,-0x14(%rbp)
jne    11d2 <main+0x82>
lea    0xea1(%rip),%rdi
call   1030 <printf@plt>
jmp    1204 <main+0xb4>
lea    0xeb9(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
mov    0x2e00(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11ff <main+0xaf>
lea    0xeb0(%rip),%rdi
call   1030 <printf@plt>
jmp    1204 <main+0xb4>
jmp    1209 <main+0xb9>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x410,%rsp
lea    0xea8(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e5d(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x1388,%edx
call   1050 <poll@plt>
cmp    $0xffffffff,%eax
jne    11c0 <main+0x70>
lea    0xe64(%rip),%rdi
call   1060 <perror@plt>
mov    (%rbx),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
test   %eax,%eax
jne    11cd <main+0x7d>
lea    0xecc(%rip),%rdi
jmp    11ee <main+0x9e>
lea    0xe94(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
test   %eax,(%rsp)
je     11a1 <main+0x51>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
jmp    11a1 <main+0x51>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@.str = [98 x i8] c"Wait 5 seconds for poll() to time out, or press a key to cause it to return before it times out.\0A\00"
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@.str.1 = [7 x i8] c"poll()\00"
@.str.2 = [41 x i8] c"Timed out. No data within five seconds.\0A\00"
@.str.3 = [24 x i8] c"Data is available now.\0A\00"
@POLLNVAL = global i32 0
@.str.4 = [25 x i8] c"file descriptor was bad\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.pollfd
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32 0, i32* %1
%5 = call i32 @printf(i8* getelementptr inbounds ([98 x i8], [98 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @STDIN_FILENO
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i32 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @POLLIN
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i32 0, i32 1
store i32 %8, i32* %9
%10 = call i32 @poll(%struct.pollfd* %2, i32 1, i32 5000)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %31
15: ; preds = %0
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %30
20: ; preds = %15
%21 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%22 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i32 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @POLLNVAL
%25 = and i32 %23, %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %20
br label %30
30: ; preds = %29, %18
br label %31
31: ; preds = %30, %13
%32 = load i32, i32* @STDIN_FILENO
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%34 = call i32 @read(i32 %32, i8* %33, i32 1024)
%35 = load i32, i32* @EXIT_SUCCESS
%36 = call i32 @exit(i32 %35)
unreachable
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@.str.1 = [7 x i8] c"poll()\00"
@POLLNVAL = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [97 x i8] c"Wait 5 seconds for poll() to time out, or press a key to cause it to return before it times out.\00"
@str.5 = [23 x i8] c"Data is available now.\00"
@str.6 = [24 x i8] c"file descriptor was bad\00"
@str.7 = [40 x i8] c"Timed out. No data within five seconds.\00"
define i32 @main() {
%1 = alloca %struct.pollfd
%2 = alloca [1024 x i8]
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([97 x i8], [97 x i8]* @str, i64 0, i64 0))
%4 = bitcast %struct.pollfd* %1 to i8*
%5 = load i32, i32* @STDIN_FILENO
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 0, i32 2
store i32 %5, i32* %6
%7 = load i32, i32* @POLLIN
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 0, i32 1
store i32 %7, i32* %8
%9 = call i32 @poll(%struct.pollfd* %1, i32 1, i32 5000)
switch i32 %9, label %14 [
i32 -1, label %10
i32 0, label %12
]
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %23
12: ; preds = %0
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.7, i64 0, i64 0))
br label %23
14: ; preds = %0
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.5, i64 0, i64 0))
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %1, i64 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @POLLNVAL
%19 = and i32 %18, %17
%20 = icmp eq i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %14
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.6, i64 0, i64 0))
br label %23
23: ; preds = %12, %21, %14, %10
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%25 = load i32, i32* @STDIN_FILENO
%26 = call i32 @read(i32 %25, i8* %24, i32 1024)
%27 = load i32, i32* @EXIT_SUCCESS
%28 = call i32 @exit(i32 %27)
unreachable
}