// File: /workspace/github_c_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
leaq 0x1b00e(%rip), %rdx     # 0x1f910
leaq 0x10728(%rip), %rcx     # 0x15031
pushq $0x1
popq %r12
movl %r12d, %esi
callq 0x465c
movq %rax, 0x18(%rsp)
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x5ec3
testl %eax, %eax
je 0x4966
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x78e4
mulsd 0x106dc(%rip), %xmm0    # 0x15018
movl $0x0, 0x8(%rsp)
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x496d
movl $0x7fffffff, 0x8(%rsp)  # imm = 0x7FFFFFFF
ucomisd 0x106c2(%rip), %xmm0    # 0x15020
ja 0x496d
cvttsd2si %xmm0, %eax
jmp 0x4969
pushq $-0x1
popq %rax
movl %eax, 0x8(%rsp)
movq %rbx, %rdi
movl %r12d, %esi
callq 0x654e
movq %rbx, %rdi
callq 0x59a3
movl %eax, 0x10(%rsp)
pushq $0x10
popq %rsi
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %edx, %edx
callq 0x649a
movq %rbx, %rdi
callq 0x59a3
movl %eax, 0xc(%rsp)
pushq $-0x2
popq %r13
cmpl $0x3, %r12d
je 0x4ab9
movq %rbx, %rdi
movl 0x10(%rsp), %esi
movl %r12d, %edx
callq 0x641f
movq %rbx, %rdi
callq 0x604b
xorl %eax, %eax
cmpl $0x1, %r12d
setne %al
leal (%rax,%rax,2), %eax
incl %eax
movl %eax, 0x14(%rsp)
movq %rbx, %rdi
movl %r13d, %esi
callq 0x6c49
testl %eax, %eax
je 0x4ab1
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0x5e24
movq %rax, %r14
andl $0x0, 0x30(%rsp)
cmpl $0x2, %r12d
jne 0x4a4c
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movl %r14d, %edx
callq 0x641f
movq %rbx, %rdi
pushq $-0x1
popq %rsi
leaq 0x30(%rsp), %rdx
callq 0x5e24
movq %rax, %rbp
movq %rbx, %rdi
movl %r13d, %esi
callq 0x59b7
cmpl $0x0, 0x30(%rsp)
je 0x4a4c
movq 0x18(%rsp), %rax
movq (%rax), %rax
movslq %ebp, %rcx
orw $0x4, 0x4(%rax,%rcx,8)
jmp 0x4aa1
movl %r12d, %r13d
movq 0x18(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x4e0a
leal 0x1(%r15), %ebp
movq (%r12), %rax
movl %r13d, %r12d
movslq %r15d, %rsi
movl %r14d, (%rax,%rsi,8)
movl 0x14(%rsp), %ecx
movw %cx, 0x4(%rax,%rsi,8)
cmpl $0x1, %r13d
jne 0x4a9a
movq %rbx, %rdi
callq 0x6071
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movl %r14d, %edx
callq 0x66f2
movl %ebp, %r15d
pushq $-0x2
popq %r13
movq %rbx, %rdi
movl %r13d, %esi
callq 0x59b7
jmp 0x49d7
incl %r12d
jmp 0x49a4
pushq $0x3
popq %rdx
movq %rbx, %rdi
movl 0x10(%rsp), %esi
callq 0x641f
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x649a
movq %rbx, %rdi
callq 0x4ed9
movl %eax, %r12d
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0x44b2
testl %eax, %eax
movl 0x8(%rsp), %eax
cmovnel %ebp, %eax
movl %eax, 0x8(%rsp)
testl %r12d, %r12d
je 0x4b2c
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x4e0a
movl 0x1b729(%rip), %eax     # 0x20240
movq (%r14), %rcx
movslq %r15d, %r15
movl %eax, (%rcx,%r15,8)
movw $0x1, 0x4(%rcx,%r15,8)
incl %r15d
cmpl $-0x1, 0x8(%rsp)
jne 0x4b48
testl %r15d, %r15d
jne 0x4b48
movq %rbx, %rdi
callq 0x604b
pushq $0x1
popq %rbp
jmp 0x4d86
movslq %r15d, %r14
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq %r14, %rsi
movl 0x8(%rsp), %edx
callq 0x3340
testl %eax, %eax
jns 0x4b8f
callq 0x3060
movl (%rax), %edi
cmpl $0x4, %edi
je 0x4b4b
callq 0x3410
leaq 0x106df(%rip), %rsi     # 0x1525a
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x72e8
movl %eax, %ebp
jmp 0x4d86
movq %r14, 0x28(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %r14
movl %r12d, 0x8(%rsp)
cmpl $0x1, %r12d
adcl $-0x1, %r15d
pushq $-0x1
popq %r12
movq %rbx, %rdi
movl %r12d, %esi
callq 0x44b2
movl %eax, %ebp
movq %rbx, %rdi
movl %r12d, %esi
callq 0x5982
movl %eax, 0xc(%rsp)
pushq $-0x4
popq %rsi
movq %rbx, %rdi
callq 0x5982
movl %eax, 0x14(%rsp)
pushq $-0x3
popq %rsi
movq %rbx, %rdi
callq 0x5982
movl %eax, 0x10(%rsp)
xorl %r12d, %r12d
testl %r15d, %r15d
cmovlel %r12d, %r15d
cmpq %r12, %r15
je 0x4cd0
movzwl 0x4(%r14,%r12,8), %eax
testb $0x1, %al
je 0x4c67
testb $0x39, 0x6(%r14,%r12,8)
je 0x4c67
movl (%r14,%r12,8), %edx
movq %rbx, %rdi
movl 0x14(%rsp), %r13d
movl %r13d, %esi
callq 0x641f
movq %rbx, %rdi
callq 0x604b
movq %rbx, %rdi
pushq $-0x2
popq %rsi
leaq 0x105e5(%rip), %rdx     # 0x1521a
callq 0x661d
incl %ebp
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movl %ebp, %edx
callq 0x66f2
movq %rbx, %rdi
callq 0x604b
movl (%r14,%r12,8), %edx
movq %rbx, %rdi
movl %r13d, %esi
callq 0x66f2
movzwl 0x4(%r14,%r12,8), %eax
testb $0x4, %al
je 0x4cc8
testb $0x3c, 0x6(%r14,%r12,8)
je 0x4cc8
movl (%r14,%r12,8), %edx
movq %rbx, %rdi
movl 0x10(%rsp), %r13d
movl %r13d, %esi
callq 0x641f
movq %rbx, %rdi
callq 0x604b
movq %rbx, %rdi
pushq $-0x2
popq %rsi
leaq 0x1057e(%rip), %rdx     # 0x1521a
callq 0x661d
incl %ebp
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movl %ebp, %edx
callq 0x66f2
movq %rbx, %rdi
callq 0x604b
movl (%r14,%r12,8), %edx
movq %rbx, %rdi
movl %r13d, %esi
callq 0x66f2
incq %r12
jmp 0x4bf1
pushq $0x1
popq %rbp
cmpl $0x0, 0x8(%rsp)
je 0x4d86
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq 0x28(%rsp), %rcx
testb $0x39, -0x2(%rax,%rcx,8)
je 0x4d86
xorl %r12d, %r12d
leaq 0x30(%rsp), %r14
pushq $0x20
popq %r15
movl 0x1b538(%rip), %edi     # 0x20240
movq %r14, %rsi
movq %r15, %rdx
callq 0x31e0
testl %eax, %eax
cmovgl %ebp, %r12d
cmpl $0x20, %eax
je 0x4d02
testl %r12d, %r12d
je 0x4d86
xorl %r12d, %r12d
pushq $-0x1
popq %r14
leaq 0x24(%rsp), %r15
pushq $0x1
popq %rbp
leaq 0x1b517(%rip), %r13     # 0x20250
movl %r14d, %edi
movq %r15, %rsi
movl %ebp, %edx
callq 0x3360
testl %eax, %eax
jle 0x4d6a
movq %r13, %rcx
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x4d39
cmpl %eax, 0x8(%rcx)
jne 0x4d4d
andl $0x0, 0x8(%rcx)
movl 0x24(%rsp), %eax
movl %eax, 0xc(%rcx)
incl %r12d
jmp 0x4d39
cmpl $-0x1, %eax
jne 0x4d79
callq 0x3060
cmpl $0x4, (%rax)
je 0x4d39
testl %r12d, %r12d
je 0x4d86
movq %rbx, %rdi
callq 0x4ed9
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq