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

/* Variables and functions */
int /*<<< orphan*/ ETIMEDOUT;
int POLLIN;
int /*<<< orphan*/ errno;
int poll(struct pollfd *, int, int);
int qio_wait_readable(int fd, int timeoutms) {
  struct pollfd fds[1];
  fds[0].fd = fd;
  fds[0].events = POLLIN;
  int pollret = poll(fds, 1, timeoutms);
  if (pollret == 0) {
    errno = ETIMEDOUT;
    return 0;
  } else if (pollret < 0) {
    return -1;
  }
  if (fds[0].revents & POLLIN)
    return 1;
  return -1;
}


x86 assembly (clang, -O0):
<qio_wait_readable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2ebd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    $0x1,%esi
call   1030 <poll@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    116c <qio_wait_readable+0x5c>
mov    0x2e7b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e7a(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11af <qio_wait_readable+0x9f>
cmpl   $0x0,-0x1c(%rbp)
jge    1182 <qio_wait_readable+0x72>
movl   $0xffffffff,-0x4(%rbp)
jmp    11af <qio_wait_readable+0x9f>
jmp    1187 <qio_wait_readable+0x77>
mov    -0x10(%rbp),%eax
mov    0x2e57(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11a8 <qio_wait_readable+0x98>
movl   $0x1,-0x4(%rbp)
jmp    11af <qio_wait_readable+0x9f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qio_wait_readable>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,%edx
mov    %edi,(%rsp)
mov    0x2ec7(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
call   1030 <poll@plt>
test   %eax,%eax
je     114a <qio_wait_readable+0x3a>
js     1160 <qio_wait_readable+0x50>
mov    (%rbx),%eax
and    0x8(%rsp),%eax
cmp    $0x1,%eax
sbb    %eax,%eax
or     $0x1,%eax
jmp    1165 <qio_wait_readable+0x55>
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
mov    0x2e7e(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
jmp    1165 <qio_wait_readable+0x55>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@ETIMEDOUT = global i32 0
@errno = global i32 0
define i32 @qio_wait_readable(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1 x %struct.pollfd]
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 0
store i32 %8, i32* %10
%11 = load i32, i32* @POLLIN
%12 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 1
store i32 %11, i32* %13
%14 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%15 = load i32, i32* %5
%16 = call i32 @poll(%struct.pollfd* %14, i32 1, i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = load i32, i32* @ETIMEDOUT
store i32 %20, i32* @errno
store i32 0, i32* %3
br label %35
21: ; preds = %2
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 -1, i32* %3
br label %35
25: ; preds = %21
br label %26
26: ; preds = %25
%27 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %6, i64 0, i64 0
%28 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %27, i32 0, i32 2
%29 = load i32, i32* %28
%30 = load i32, i32* @POLLIN
%31 = and i32 %29, %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %26
store i32 1, i32* %3
br label %35
34: ; preds = %26
store i32 -1, i32* %3
br label %35
35: ; preds = %34, %33, %24, %19
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@ETIMEDOUT = global i32 0
@errno = global i32 0
define i32 @qio_wait_readable(i32 %0, i32 %1) {
%3 = alloca [1 x %struct.pollfd]
%4 = bitcast [1 x %struct.pollfd]* %3 to i8*
%5 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 0
store i32 %0, i32* %6
%7 = load i32, i32* @POLLIN
%8 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 1
store i32 %7, i32* %8
%9 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 %1)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i32, i32* @ETIMEDOUT
store i32 %12, i32* @errno
br label %22
13: ; preds = %2
%14 = icmp slt i32 %9, 0
br i1 %14, label %22, label %15
15: ; preds = %13
%16 = getelementptr inbounds [1 x %struct.pollfd], [1 x %struct.pollfd]* %3, i64 0, i64 0, i32 2
%17 = load i32, i32* %16
%18 = load i32, i32* @POLLIN
%19 = and i32 %18, %17
%20 = icmp eq i32 %19, 0
%21 = select i1 %20, i32 -1, i32 1
br label %22
22: ; preds = %15, %13, %11
%23 = phi i32 [ 0, %11 ], [ -1, %13 ], [ %21, %15 ]
ret i32 %23
}