#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 timeval {
  int dummy;
};
struct pollfd {
  int dummy;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ assert(int);
int map_poll_spec(struct pollfd *, unsigned long, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ map_select_results(struct pollfd *, unsigned long,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *);
struct timeval *map_timeout(int, struct timeval *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int poll_emu(struct pollfd *pArray, unsigned long n_fds, int timeout) {
  fd_set read_descs;
  fd_set write_descs;
  fd_set except_descs;
  struct timeval stime;
  int ready_descriptors;
  int max_fd;
  struct timeval *pTimeout;
  FD_ZERO(&read_descs);
  FD_ZERO(&write_descs);
  FD_ZERO(&except_descs);
  assert(pArray != (struct pollfd *)NULL);
  max_fd =
      map_poll_spec(pArray, n_fds, &read_descs, &write_descs, &except_descs);
  pTimeout = map_timeout(timeout, &stime);
  ready_descriptors =
      select(max_fd + 1, &read_descs, &write_descs, &except_descs, pTimeout);
  if (ready_descriptors >= 0) {
    map_select_results(pArray, n_fds, &read_descs, &write_descs, &except_descs);
  }
  return ready_descriptors;
}


x86 assembly (clang, -O0):
<poll_emu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <FD_ZERO@plt>
lea    -0x1c(%rbp),%rdi
call   1060 <FD_ZERO@plt>
lea    -0x20(%rbp),%rdi
call   1060 <FD_ZERO@plt>
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x1c(%rbp),%rcx
lea    -0x20(%rbp),%r8
call   1030 <map_poll_spec@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1040 <map_timeout@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%edi
add    $0x1,%edi
mov    -0x38(%rbp),%r8
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1070 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     120d <poll_emu+0xad>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x1c(%rbp),%rcx
lea    -0x20(%rbp),%r8
call   1080 <map_select_results@plt>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poll_emu>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r13d
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1060 <FD_ZERO@plt>
lea    0xc(%rsp),%r12
mov    %r12,%rdi
call   1060 <FD_ZERO@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
mov    %rbp,%r8
call   1030 <map_poll_spec@plt>
mov    %eax,%r15d
lea    0x10(%rsp),%rsi
mov    %r13d,%edi
call   1040 <map_timeout@plt>
add    $0x1,%r15d
mov    %r15d,%edi
lea    0x4(%rsp),%rsi
mov    %r12,%rdx
mov    %rbp,%rcx
mov    %rax,%r8
call   1070 <select@plt>
mov    %eax,%ebp
test   %eax,%eax
js     120a <poll_emu+0xaa>
lea    0x4(%rsp),%rdx
lea    0xc(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <map_select_results@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32 }
%struct.timeval = type { i32 }
define i32 @poll_emu(%struct.pollfd* %0, i64 %1, i32 %2) {
%4 = alloca %struct.pollfd*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.timeval
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.timeval*
store %struct.pollfd* %0, %struct.pollfd** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%14 = call i32 @FD_ZERO(i32* %7)
%15 = call i32 @FD_ZERO(i32* %8)
%16 = call i32 @FD_ZERO(i32* %9)
%17 = load %struct.pollfd*, %struct.pollfd** %4
%18 = icmp ne %struct.pollfd* %17, null
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load %struct.pollfd*, %struct.pollfd** %4
%22 = load i64, i64* %5
%23 = call i32 @map_poll_spec(%struct.pollfd* %21, i64 %22, i32* %7, i32* %8, i32* %9)
store i32 %23, i32* %12
%24 = load i32, i32* %6
%25 = call %struct.timeval* @map_timeout(i32 %24, %struct.timeval* %10)
store %struct.timeval* %25, %struct.timeval** %13
%26 = load i32, i32* %12
%27 = add nsw i32 %26, 1
%28 = load %struct.timeval*, %struct.timeval** %13
%29 = call i32 @select(i32 %27, i32* %7, i32* %8, i32* %9, %struct.timeval* %28)
store i32 %29, i32* %11
%30 = load i32, i32* %11
%31 = icmp sge i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %3
%33 = load %struct.pollfd*, %struct.pollfd** %4
%34 = load i64, i64* %5
%35 = call i32 @map_select_results(%struct.pollfd* %33, i64 %34, i32* %7, i32* %8, i32* %9)
br label %36
36: ; preds = %32, %3
%37 = load i32, i32* %11
ret i32 %37
}

LLVM IR (-O2):
%struct.pollfd = type { i32 }
%struct.timeval = type { i32 }
define i32 @poll_emu(%struct.pollfd* %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = bitcast %struct.timeval* %7 to i8*
%12 = call i32 @FD_ZERO(i32* %4)
%13 = call i32 @FD_ZERO(i32* %5)
%14 = call i32 @FD_ZERO(i32* %6)
%15 = icmp ne %struct.pollfd* %0, null
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = call i32 @map_poll_spec(%struct.pollfd* %0, i64 %1, i32* %4, i32* %5, i32* %6)
%19 = call %struct.timeval* @map_timeout(i32 %2, %struct.timeval* %7)
%20 = add nsw i32 %18, 1
%21 = call i32 @select(i32 %20, i32* %4, i32* %5, i32* %6, %struct.timeval* %19)
%22 = icmp sgt i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %3
%24 = call i32 @map_select_results(%struct.pollfd* %0, i64 %1, i32* %4, i32* %5, i32* %6)
br label %25
25: ; preds = %23, %3
ret i32 %21
}