// File: /workspace/github_c_makefile_star2_O0/bhk[P]tooltree/monoglot/./xpio_c.c
// Function: xpqueue_wait
static int xpqueue_wait(lua_State *L)
{
   XPQueue *me = XLUA_CAST(L, 1, XPQueue);
   int ndxUser, ndxSlots;
   int numOut;
   int nfdsUsed = 0;
   int timeout;
   int mode;

   if (lua_toboolean(L, 2)) {
      double num = luaL_checknumber(L, 2) * 1000.0;
      if (num < 0) {
         timeout = 0;
      } else if (num > INT_MAX) {
         timeout = INT_MAX;
      } else {
         timeout = (int) num;
      }
   } else {
      timeout = -1;
   }

   // get user value
   lua_getuservalue(L, 1);
   ndxUser = lua_gettop(L);

   // create "slots" table:  fd -> index into pfd[]
   // not a long-lived table, so over-allocation is not a problem
   lua_createtable(L, INITIAL_SIZE, 0);
   ndxSlots = lua_gettop(L);

   // construct pfds[] from readers and writers.  First add readers,
   // allocating slots in pfds[] and recording the index used for each fd.
   // Then add writers, allocating slots for each fd only when one has not
   // allready been allocated for a reader.

   for (mode = XPQUEUE_READ; mode <= XPQUEUE_WRITE; ++mode) {

      // get readers or writers
      lua_rawgeti(L, ndxUser, mode);

      for (lua_pushnil(L); lua_next(L, -2) != 0; lua_pop(L, 1)) {
         // key is at -2;  value is at -1
         int fd = lua_tointeger(L, -2);
         int ndxFD = 0;
         int bAllocated = 0;

         // see if there is already a slot for this fd
         if (mode == XPQUEUE_WRITE) {
            lua_rawgeti(L, ndxSlots, fd);
            ndxFD = lua_tointegerx(L, -1, &bAllocated);
            lua_pop(L, 1);
         }

         //printf(">> mode=%d fd=%d ndxFD=%d bAllocated=%d\n", mode, fd, ndxFD, bAllocated);
         // allocate a slot
         if (!bAllocated) {
            XPQueue_ensureFDs(me, L, nfdsUsed);
            ndxFD = nfdsUsed++;
            me->pfds[ndxFD].fd = fd;
            me->pfds[ndxFD].events = (mode == XPQUEUE_READ ? POLLIN : POLLOUT);
         } else {
            me->pfds[ndxFD].events |= POLLOUT;
         }

         // make note of the slot allocated for fd
         if (mode == XPQUEUE_READ) {
            lua_pushinteger(L, ndxFD);
            lua_rawseti(L, ndxSlots, fd);
         }
      }
   }

   lua_rawgeti(L, ndxUser, 3);  // childWaiters

   lua_newtable(L);  // result = array of ready tasks

   // stack: readers(-4) writers(-3) childWaiters(-2) ready(-1)

   // Move ready childWaiters to ready queue, and count pending ones
   int numChildWaiters = XPQueue_wakeChildWaiters(me, L, -1, -2);
   if (lengthOf(L, -1)) {
      timeout = 0;
   }

   if (numChildWaiters) {
      // wait on sigchldPipe
      // printf("... adding sigchldPipe to read set\n");
      XPQueue_ensureFDs(me, L, nfdsUsed);
      me->pfds[nfdsUsed].fd = xpproc_getSigPipe();
      me->pfds[nfdsUsed].events = POLLIN;
      ++nfdsUsed;
   }

   if (timeout == -1 && nfdsUsed == 0) {
      // nothing to wait on
      lua_pushnil(L);
      return 1;
   }

   do {
      //printf("poll(_, %d, %d) ...\n", nfdsUsed, timeout);
      numOut = poll(me->pfds, nfdsUsed, timeout);
      //printf("... poll -> %d\n", numOut);
   } while (numOut < 0 && errno == EINTR);
   if (numOut < 0) {
      return luaL_error(L, "xpio: poll error (%s)", strerror(errno));
   }

   XPQueue_wakeSockets(me, L, -1, -4, -3,
                       me->pfds, nfdsUsed - (numChildWaiters ? 1 : 0));

   if (numChildWaiters &&
       (me->pfds[nfdsUsed-1].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL)) &&
       xpproc_reap()) {
      (void) XPQueue_wakeChildWaiters(me, L, -1, -2);
   }

   return 1;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq 0x58(%rsp), %rdi
movl $0x1, %esi
leaq 0x2b006(%rip), %rdx     # 0x30880
leaq 0x227b0(%rip), %rcx     # 0x28031
callq 0x5450
movq %rax, 0x50(%rsp)
movl $0x0, 0x40(%rsp)
movq 0x58(%rsp), %rdi
movl $0x2, %esi
callq 0x8060
cmpl $0x0, %eax
je 0x5909
movq 0x58(%rsp), %rdi
movl $0x2, %esi
callq 0xb4e0
movsd 0x2275a(%rip), %xmm1    # 0x28018
mulsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x30(%rsp), %xmm0
jbe 0x58dd
movl $0x0, 0x3c(%rsp)
jmp 0x5907
movsd 0x30(%rsp), %xmm0
movsd 0x22735(%rip), %xmm1    # 0x28020
ucomisd %xmm1, %xmm0
jbe 0x58fb
movl $0x7fffffff, 0x3c(%rsp) # imm = 0x7FFFFFFF
jmp 0x5905
cvttsd2si 0x30(%rsp), %eax
movl %eax, 0x3c(%rsp)
jmp 0x5907
jmp 0x5911
movl $0xffffffff, 0x3c(%rsp) # imm = 0xFFFFFFFF
movq 0x58(%rsp), %rdi
movl $0x1, %esi
callq 0x8f60
movq 0x58(%rsp), %rdi
callq 0x7470
movl %eax, 0x4c(%rsp)
movq 0x58(%rsp), %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x8dd0
movq 0x58(%rsp), %rdi
callq 0x7470
movl %eax, 0x48(%rsp)
movl $0x1, 0x38(%rsp)
cmpl $0x2, 0x38(%rsp)
jg 0x5acf
movq 0x58(%rsp), %rdi
movl 0x4c(%rsp), %esi
movl 0x38(%rsp), %edx
callq 0x8cb0
movq 0x58(%rsp), %rdi
callq 0x8440
movq 0x58(%rsp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x9e50
cmpl $0x0, %eax
je 0x5abd
movq 0x58(%rsp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
xorl %eax, %eax
movl %eax, %edx
callq 0x7ef0
movl %eax, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movl $0x0, 0x24(%rsp)
cmpl $0x2, 0x38(%rsp)
jne 0x59fb
movq 0x58(%rsp), %rdi
movl 0x48(%rsp), %esi
movl 0x2c(%rsp), %edx
callq 0x8cb0
movq 0x58(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x24(%rsp), %rdx
callq 0x7ef0
movl %eax, 0x28(%rsp)
movq 0x58(%rsp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x74a0
cmpl $0x0, 0x24(%rsp)
jne 0x5a65
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl 0x40(%rsp), %edx
callq 0x5d60
movl 0x40(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x40(%rsp)
movl %eax, 0x28(%rsp)
movl 0x2c(%rsp), %edx
movq 0x50(%rsp), %rax
movq (%rax), %rax
movslq 0x28(%rsp), %rcx
movl %edx, (%rax,%rcx,8)
movl 0x38(%rsp), %edx
movl $0x4, %eax
movl $0x1, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
movw %ax, %dx
movq 0x50(%rsp), %rax
movq (%rax), %rax
movslq 0x28(%rsp), %rcx
movw %dx, 0x4(%rax,%rcx,8)
jmp 0x5a7f
movq 0x50(%rsp), %rax
movq (%rax), %rax
movslq 0x28(%rsp), %rcx
movswl 0x4(%rax,%rcx,8), %edx
orl $0x4, %edx
movw %dx, 0x4(%rax,%rcx,8)
cmpl $0x1, 0x38(%rsp)
jne 0x5aa7
movq 0x58(%rsp), %rdi
movslq 0x28(%rsp), %rsi
callq 0x84c0
movq 0x58(%rsp), %rdi
movl 0x48(%rsp), %esi
movl 0x2c(%rsp), %edx
callq 0x92d0
jmp 0x5aa9
movq 0x58(%rsp), %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x74a0
jmp 0x597c
jmp 0x5abf
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x5955
movq 0x58(%rsp), %rdi
movl 0x4c(%rsp), %esi
movl $0x3, %edx
callq 0x8cb0
movq 0x58(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x8dd0
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
callq 0x5dd0
movl %eax, 0x20(%rsp)
movq 0x58(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x5170
cmpl $0x0, %eax
je 0x5b29
movl $0x0, 0x3c(%rsp)
cmpl $0x0, 0x20(%rsp)
je 0x5b79
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl 0x40(%rsp), %edx
callq 0x5d60
callq 0x5f60
movl %eax, %edx
movq 0x50(%rsp), %rax
movq (%rax), %rax
movslq 0x40(%rsp), %rcx
movl %edx, (%rax,%rcx,8)
movq 0x50(%rsp), %rax
movq (%rax), %rax
movslq 0x40(%rsp), %rcx
movw $0x1, 0x4(%rax,%rcx,8)
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
cmpl $-0x1, 0x3c(%rsp)
jne 0x5b9e
cmpl $0x0, 0x40(%rsp)
jne 0x5b9e
movq 0x58(%rsp), %rdi
callq 0x8440
movl $0x1, 0x64(%rsp)
jmp 0x5cae
jmp 0x5ba0
movq 0x50(%rsp), %rax
movq (%rax), %rdi
movslq 0x40(%rsp), %rsi
movl 0x3c(%rsp), %edx
callq 0x3360
movl %eax, 0x44(%rsp)
xorl %eax, %eax
cmpl $0x0, 0x44(%rsp)
movb %al, 0x1f(%rsp)
jge 0x5bd6
callq 0x3060
cmpl $0x4, (%rax)
sete %al
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x5ba0
cmpl $0x0, 0x44(%rsp)
jge 0x5c1a
movq 0x58(%rsp), %rax
movq %rax, 0x10(%rsp)
callq 0x3060
movl (%rax), %edi
callq 0x3420
movq 0x10(%rsp), %rdi
movq %rax, %rdx
leaq 0x22650(%rip), %rsi     # 0x2825a
movb $0x0, %al
callq 0xab70
movl %eax, 0x64(%rsp)
jmp 0x5cae
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rax
movq (%rax), %r9
movl 0x40(%rsp), %eax
movl 0x20(%rsp), %r8d
xorl %ecx, %ecx
movl $0x1, %edx
cmpl $0x0, %r8d
cmovnel %edx, %ecx
subl %ecx, %eax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xfffffffc, %ecx       # imm = 0xFFFFFFFC
movl $0xfffffffd, %r8d       # imm = 0xFFFFFFFD
movl %eax, (%rsp)
callq 0x5f70
cmpl $0x0, 0x20(%rsp)
je 0x5ca6
movq 0x50(%rsp), %rax
movq (%rax), %rax
movl 0x40(%rsp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movswl 0x6(%rax,%rcx,8), %eax
andl $0x39, %eax
cmpl $0x0, %eax
je 0x5ca6
callq 0x6140
cmpl $0x0, %eax
je 0x5ca6
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
callq 0x5dd0
movl $0x1, 0x64(%rsp)
movl 0x64(%rsp), %eax
addq $0x68, %rsp
retq
nopw (%rax,%rax)