#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 tv_usec;
  scalar_t__ tv_sec;
};
struct forth_instance {
  int dummy;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          struct timeval *);
int st_pop(struct forth_instance *);
int /*<<< orphan*/ st_push(struct forth_instance *, int);
void poll_func(struct forth_instance *fi) {
  fd_set rfds;
  struct timeval tv;
  int sd, timeout;
  timeout = st_pop(fi);
  sd = st_pop(fi);
  tv.tv_sec = 0;
  tv.tv_usec = timeout;
  FD_ZERO(&rfds);
  FD_SET(sd, &rfds);
  select(FD_SETSIZE, &rfds, NULL, NULL, (timeout == -1) ? NULL : &tv);
  st_push(fi, (int)FD_ISSET(sd, &rfds));
}


x86 assembly (clang, -O0):
<poll_func>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <st_pop@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <st_pop@plt>
mov    %eax,-0x24(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x20(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x24(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11c8 <poll_func+0x68>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    11d5 <poll_func+0x75>
lea    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11d5 <poll_func+0x75>
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%r8
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1060 <select@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1030 <FD_ISSET@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
call   1070 <st_push@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<poll_func>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
call   1040 <st_pop@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <st_pop@plt>
mov    %eax,%r14d
movq   $0x0,0x10(%rsp)
mov    %ebp,0x8(%rsp)
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1050 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %r15,%rsi
call   1080 <FD_SET@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
cmp    $0xffffffff,%ebp
lea    0x8(%rsp),%r8
cmove  %rax,%r8
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
mov    %r14d,%edi
mov    %r15,%rsi
call   1030 <FD_ISSET@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1070 <st_push@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.forth_instance = type { i32 }
%struct.timeval = type { i32, i64 }
@FD_SETSIZE = global i32 0
define void @poll_func(%struct.forth_instance* %0) {
%2 = alloca %struct.forth_instance*
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = alloca i32
store %struct.forth_instance* %0, %struct.forth_instance** %2
%7 = load %struct.forth_instance*, %struct.forth_instance** %2
%8 = call i32 @st_pop(%struct.forth_instance* %7)
store i32 %8, i32* %6
%9 = load %struct.forth_instance*, %struct.forth_instance** %2
%10 = call i32 @st_pop(%struct.forth_instance* %9)
store i32 %10, i32* %5
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
store i64 0, i64* %11
%12 = load i32, i32* %6
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
store i32 %12, i32* %13
%14 = call i32 @FD_ZERO(i32* %3)
%15 = load i32, i32* %5
%16 = call i32 @FD_SET(i32 %15, i32* %3)
%17 = load i32, i32* @FD_SETSIZE
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %1
br label %22
21: ; preds = %1
br label %22
22: ; preds = %21, %20
%23 = phi %struct.timeval* [ null, %20 ], [ %4, %21 ]
%24 = call i32 @select(i32 %17, i32* %3, i32* null, i32* null, %struct.timeval* %23)
%25 = load %struct.forth_instance*, %struct.forth_instance** %2
%26 = load i32, i32* %5
%27 = call i64 @FD_ISSET(i32 %26, i32* %3)
%28 = trunc i64 %27 to i32
%29 = call i32 @st_push(%struct.forth_instance* %25, i32 %28)
ret void
}

LLVM IR (-O2):
%struct.forth_instance = type { i32 }
%struct.timeval = type { i32, i64 }
@FD_SETSIZE = global i32 0
define void @poll_func(%struct.forth_instance* %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = tail call i32 @st_pop(%struct.forth_instance* %0)
%7 = tail call i32 @st_pop(%struct.forth_instance* %0)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i64 0, i64* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 %6, i32* %9
%10 = call i32 @FD_ZERO(i32* %2)
%11 = call i32 @FD_SET(i32 %7, i32* %2)
%12 = load i32, i32* @FD_SETSIZE
%13 = icmp eq i32 %6, -1
%14 = select i1 %13, %struct.timeval* null, %struct.timeval* %3
%15 = call i32 @select(i32 %12, i32* %2, i32* null, i32* null, %struct.timeval* %14)
%16 = call i64 @FD_ISSET(i32 %7, i32* %2)
%17 = trunc i64 %16 to i32
%18 = call i32 @st_push(%struct.forth_instance* %0, i32 %17)
ret void
}