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

/* Variables and functions */
int EIO;
int ETIMEDOUT;
int /*<<< orphan*/ POLLIN;
int poll(struct pollfd *, int, int);
int dev_poll(int fd, int timeout_ms) {
  struct pollfd ufds;
  int n;
  ufds.fd = fd;
  ufds.events = POLLIN;
  if ((n = poll(&ufds, 1, timeout_ms)) == 1)
    return 0;
  if (n == 0)
    return -ETIMEDOUT;
  return -EIO;
}


x86 assembly (clang, -O0):
<dev_poll>:
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)
mov    -0xc(%rbp),%edx
lea    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1030 <poll@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x1,%eax
jne    1159 <dev_poll+0x49>
movl   $0x0,-0x4(%rbp)
jmp    1184 <dev_poll+0x74>
cmpl   $0x0,-0x1c(%rbp)
jne    1176 <dev_poll+0x66>
xor    %eax,%eax
mov    0x2e64(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1184 <dev_poll+0x74>
xor    %eax,%eax
mov    0x2e59(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dev_poll>:
push   %rax
mov    %esi,%edx
mov    %edi,(%rsp)
mov    0x2ecb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
call   1030 <poll@plt>
cmp    $0x1,%eax
je     1142 <dev_poll+0x32>
test   %eax,%eax
jne    1146 <dev_poll+0x36>
mov    0x2e90(%rip),%rcx
jmp    114d <dev_poll+0x3d>
xor    %eax,%eax
pop    %rcx
ret
mov    0x2e8b(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@ETIMEDOUT = global i32 0
@EIO = global i32 0
define i32 @dev_poll(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.pollfd
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* @POLLIN
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %6, i32 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* %5
%13 = call i32 @poll(%struct.pollfd* %6, i32 1, i32 %12)
store i32 %13, i32* %7
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %25
16: ; preds = %2
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @ETIMEDOUT
%21 = sub nsw i32 0, %20
store i32 %21, i32* %3
br label %25
22: ; preds = %16
%23 = load i32, i32* @EIO
%24 = sub nsw i32 0, %23
store i32 %24, i32* %3
br label %25
25: ; preds = %22, %19, %15
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@ETIMEDOUT = global i32 0
@EIO = global i32 0
define i32 @dev_poll(i32 %0, i32 %1) {
%3 = alloca %struct.pollfd
%4 = bitcast %struct.pollfd* %3 to i8*
%5 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 0
store i32 %0, i32* %5
%6 = load i32, i32* @POLLIN
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 1
store i32 %6, i32* %7
%8 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 %1)
switch i32 %8, label %12 [
i32 1, label %15
i32 0, label %9
]
9: ; preds = %2
%10 = load i32, i32* @ETIMEDOUT
%11 = sub nsw i32 0, %10
br label %15
12: ; preds = %2
%13 = load i32, i32* @EIO
%14 = sub nsw i32 0, %13
br label %15
15: ; preds = %2, %12, %9
%16 = phi i32 [ %11, %9 ], [ %14, %12 ], [ 0, %2 ]
ret i32 %16
}