// File: /workspace/github_c_makefile_star2_O3/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 0x1ae14(%rip), %rdx     # 0x1f910
leaq 0x1252e(%rip), %rcx     # 0x17031
movl $0x1, %r12d
movl $0x1, %esi
callq 0x4834
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
movl $0x2, %esi
callq 0x62d0
testl %eax, %eax
je 0x4b5f
movq %rbx, %rdi
movl $0x2, %esi
callq 0x7d93
mulsd 0x124da(%rip), %xmm0    # 0x17018
xorl %eax, %eax
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x4b64
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
ucomisd 0x124c9(%rip), %xmm0    # 0x17020
ja 0x4b64
cvttsd2si %xmm0, %eax
jmp 0x4b64
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x14(%rsp)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x6986
movq %rbx, %rdi
callq 0x5c80
movl %eax, 0xc(%rsp)
xorl %r15d, %r15d
movq %rbx, %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x68d0
movq %rbx, %rdi
callq 0x5c80
movl %eax, 0x20(%rsp)
movq 0x18(%rsp), %rbp
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movl %r12d, %edx
callq 0x6855
movq %rbx, %rdi
callq 0x6478
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x7095
testl %eax, %eax
je 0x4cb5
xorl %eax, %eax
cmpl $0x1, %r12d
setne %al
leal (%rax,%rax,2), %eax
incl %eax
movl %eax, 0x10(%rsp)
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
xorl %edx, %edx
callq 0x6237
movq %rax, %r13
movl $0x0, 0x30(%rsp)
cmpl $0x2, %r12d
jne 0x4c4d
movq %rbx, %rdi
movl 0x20(%rsp), %esi
movl %r13d, %edx
callq 0x6855
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x30(%rsp), %rdx
callq 0x6237
movq %rax, %r14
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x5c94
cmpl $0x0, 0x30(%rsp)
je 0x4c4d
movq 0x18(%rsp), %rax
movq (%rax), %rax
movslq %r14d, %rcx
orw $0x4, 0x4(%rax,%rcx,8)
jmp 0x4c93
movq %rbp, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x5022
leal 0x1(%r15), %r14d
movq (%rbp), %rax
movslq %r15d, %rsi
movl %r13d, (%rax,%rsi,8)
movl 0x10(%rsp), %ecx
movw %cx, 0x4(%rax,%rsi,8)
cmpl $0x1, %r12d
jne 0x4c90
movq %rbx, %rdi
callq 0x64a1
movq %rbx, %rdi
movl 0x20(%rsp), %esi
movl %r13d, %edx
callq 0x6b2f
movl %r14d, %r15d
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x5c94
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x7095
testl %eax, %eax
jne 0x4be2
incl %r12d
cmpl $0x3, %r12d
jne 0x4ba4
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movl $0x3, %edx
callq 0x6855
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x68d0
movq %rbx, %rdi
callq 0x50f9
movl %eax, %r12d
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x4670
testl %eax, %eax
movl 0x14(%rsp), %eax
cmovnel %ebp, %eax
movl %eax, %ebp
testl %r12d, %r12d
je 0x4d37
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x5022
movl 0x1b51e(%rip), %eax     # 0x20240
movq (%r14), %rcx
movslq %r15d, %r15
movl %eax, (%rcx,%r15,8)
movw $0x1, 0x4(%rcx,%r15,8)
incl %r15d
cmpl $-0x1, %ebp
jne 0x4d53
testl %r15d, %r15d
jne 0x4d53
movq %rbx, %rdi
callq 0x6478
movl $0x1, %ebp
jmp 0x4fa3
movslq %r15d, %r13
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x3340
testl %eax, %eax
jns 0x4d98
callq 0x3060
movl (%rax), %edi
cmpl $0x4, %edi
je 0x4d56
callq 0x3410
leaq 0x124c1(%rip), %rsi     # 0x17245
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x7750
movl %eax, %ebp
jmp 0x4fa3
movq 0x18(%rsp), %rax
movq (%rax), %r14
movl %r12d, 0x28(%rsp)
cmpl $0x1, %r12d
adcl $-0x1, %r15d
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x4670
movl %eax, %ebp
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x5c5f
movl %eax, 0x10(%rsp)
movq %rbx, %rdi
movl $0xfffffffc, %esi       # imm = 0xFFFFFFFC
callq 0x5c5f
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x5c5f
movl %eax, 0x14(%rsp)
testl %r15d, %r15d
jle 0x4ee3
movl %r15d, %eax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
movzwl 0x4(%r14,%r12,8), %eax
testb $0x1, %al
je 0x4e72
testb $0x39, 0x6(%r14,%r12,8)
je 0x4e72
movl (%r14,%r12,8), %edx
movq %rbx, %rdi
movl 0xc(%rsp), %r15d
movl %r15d, %esi
callq 0x6855
movq %rbx, %rdi
callq 0x6478
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
leaq 0x123c5(%rip), %rdx     # 0x17205
callq 0x6a5a
incl %ebp
movq %rbx, %rdi
movl 0x10(%rsp), %esi
movl %ebp, %edx
callq 0x6b2f
movq %rbx, %rdi
callq 0x6478
movl (%r14,%r12,8), %edx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x6b2f
movzwl 0x4(%r14,%r12,8), %eax
testb $0x4, %al
je 0x4ed5
testb $0x3c, 0x6(%r14,%r12,8)
je 0x4ed5
movl (%r14,%r12,8), %edx
movq %rbx, %rdi
movl 0x14(%rsp), %r15d
movl %r15d, %esi
callq 0x6855
movq %rbx, %rdi
callq 0x6478
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
leaq 0x1235c(%rip), %rdx     # 0x17205
callq 0x6a5a
incl %ebp
movq %rbx, %rdi
movl 0x10(%rsp), %esi
movl %ebp, %edx
callq 0x6b2f
movq %rbx, %rdi
callq 0x6478
movl (%r14,%r12,8), %edx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x6b2f
incq %r12
cmpq %r12, 0x20(%rsp)
jne 0x4e03
movl $0x1, %ebp
cmpl $0x0, 0x28(%rsp)
je 0x4fa3
movq 0x18(%rsp), %rax
movq (%rax), %rax
testb $0x39, -0x2(%rax,%r13,8)
je 0x4fa3
xorl %ebp, %ebp
leaq 0x30(%rsp), %r14
movl $0x1, %r15d
movl 0x1b326(%rip), %edi     # 0x20240
movl $0x20, %edx
movq %r14, %rsi
callq 0x31e0
testl %eax, %eax
cmovgl %r15d, %ebp
cmpl $0x20, %eax
je 0x4f14
testl %ebp, %ebp
je 0x4d49
xorl %r15d, %r15d
leaq 0x2c(%rsp), %r14
leaq 0x1b307(%rip), %r12     # 0x20250
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
movl $0x1, %edx
callq 0x3360
testl %eax, %eax
jle 0x4f82
movq %r12, %rcx
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x4f49
cmpl %eax, 0x8(%rcx)
jne 0x4f62
movl $0x0, 0x8(%rcx)
movl 0x2c(%rsp), %eax
movl %eax, 0xc(%rcx)
incl %r15d
jmp 0x4f49
cmpl $-0x1, %eax
jne 0x4f91
callq 0x3060
cmpl $0x4, (%rax)
je 0x4f49
movl $0x1, %ebp
testl %r15d, %r15d
je 0x4fa3
movq %rbx, %rdi
callq 0x50f9
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq