#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 */
typedef int /*<<< orphan*/ uint8_t;
struct pollfd {
  int fd;
  int events;
  int /*<<< orphan*/ revents;
};

/* Variables and functions */
int POLLERR;
int POLLPRI;
int /*<<< orphan*/ SEEK_SET;
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int poll(struct pollfd *, int, int);
int read(int, int /*<<< orphan*/ *, int);
int pollSysfs(int fd, int timeout) {
  uint8_t dummy;
  if (read(fd, &dummy, 1) == -1) {
    return -1;
  }
  struct pollfd fds = {.fd = fd, .events = POLLPRI | POLLERR, .revents = 0};
  int timeout_in_ms = (timeout > 0) ? (timeout / 1000) : timeout;
  int poll_result = poll(&fds, 1, timeout_in_ms);
  if (poll_result == -1) {
    return -1;
  }
  if (lseek(fds.fd, 0, SEEK_SET) == -1) {
    return -1;
  }
  return poll_result;
}


x86 assembly (clang, -O0):
<pollSysfs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    1164 <pollSysfs+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <pollSysfs+0xd7>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
mov    0x2e66(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11a3 <pollSysfs+0x73>
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
jmp    11a9 <pollSysfs+0x79>
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edx
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1050 <poll@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11d9 <pollSysfs+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <pollSysfs+0xd7>
mov    -0x20(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1201 <pollSysfs+0xd1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1207 <pollSysfs+0xd7>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pollSysfs>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
lea    0x14(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     11bf <pollSysfs+0x8f>
mov    0x2e9b(%rip),%rax
mov    0x2e84(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ebp,0x8(%rsp)
mov    %ecx,0xc(%rsp)
movl   $0x0,0x10(%rsp)
test   %ebx,%ebx
jle    118b <pollSysfs+0x5b>
movslq %ebx,%rax
imul   $0x10624dd3,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x26,%rbx
add    %eax,%ebx
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    %ebx,%edx
call   1050 <poll@plt>
cmp    $0xffffffff,%eax
je     11bf <pollSysfs+0x8f>
mov    %eax,%ebx
mov    0x8(%rsp),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
jmp    11c4 <pollSysfs+0x94>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLPRI = global i32 0
@POLLERR = global i32 0
@SEEK_SET = global i32 0
define i32 @pollSysfs(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.pollfd
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* %4
%11 = call i32 @read(i32 %10, i32* %6, i32 1)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %45
14: ; preds = %2
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 0
%16 = load i32, i32* %4
store i32 %16, i32* %15
%17 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 1
%18 = load i32, i32* @POLLPRI
%19 = load i32, i32* @POLLERR
%20 = or i32 %18, %19
store i32 %20, i32* %17
%21 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 2
store i32 0, i32* %21
%22 = load i32, i32* %5
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = load i32, i32* %5
%26 = sdiv i32 %25, 1000
br label %29
27: ; preds = %14
%28 = load i32, i32* %5
br label %29
29: ; preds = %27, %24
%30 = phi i32 [ %26, %24 ], [ %28, %27 ]
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = call i32 @poll(%struct.pollfd* %7, i32 1, i32 %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %29
store i32 -1, i32* %3
br label %45
36: ; preds = %29
%37 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i32 0, i32 0
%38 = load i32, i32* %37
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @lseek(i32 %38, i32 0, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 -1, i32* %3
br label %45
43: ; preds = %36
%44 = load i32, i32* %9
store i32 %44, i32* %3
br label %45
45: ; preds = %43, %42, %35, %13
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLPRI = global i32 0
@POLLERR = global i32 0
@SEEK_SET = global i32 0
define i32 @pollSysfs(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.pollfd
%5 = bitcast i32* %3 to i8*
%6 = call i32 @read(i32 %0, i32* %3, i32 1)
%7 = icmp eq i32 %6, -1
br i1 %7, label %29, label %8
8: ; preds = %2
%9 = bitcast %struct.pollfd* %4 to i8*
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 0
store i32 %0, i32* %10
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 1
%12 = load i32, i32* @POLLPRI
%13 = load i32, i32* @POLLERR
%14 = or i32 %13, %12
store i32 %14, i32* %11
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i64 0, i32 2
store i32 0, i32* %15
%16 = icmp sgt i32 %1, 0
%17 = sdiv i32 %1, 1000
%18 = select i1 %16, i32 %17, i32 %1
%19 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %27, label %21
21: ; preds = %8
%22 = load i32, i32* %10
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @lseek(i32 %22, i32 0, i32 %23)
%25 = icmp eq i32 %24, -1
%26 = select i1 %25, i32 -1, i32 %19
br label %27
27: ; preds = %21, %8
%28 = phi i32 [ -1, %8 ], [ %26, %21 ]
br label %29
29: ; preds = %2, %27
%30 = phi i32 [ %28, %27 ], [ -1, %2 ]
ret i32 %30
}