// File: /workspace/github_c_makefile_star2_O1/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 0x1ae6a(%rip), %rdx     # 0x1f910
leaq 0x12584(%rip), %rcx     # 0x17031
movl $0x1, %r12d
movl $0x1, %esi
callq 0x47de
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
movl $0x2, %esi
callq 0x6162
testl %eax, %eax
je 0x4b09
movq %rbx, %rdi
movl $0x2, %esi
callq 0x7bfc
mulsd 0x12530(%rip), %xmm0    # 0x17018
xorl %eax, %eax
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x4b0e
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
ucomisd 0x1251f(%rip), %xmm0    # 0x17020
ja 0x4b0e
cvttsd2si %xmm0, %eax
jmp 0x4b0e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x14(%rsp)
movq %rbx, %rdi
movl $0x1, %esi
callq 0x6822
movq %rbx, %rdi
callq 0x5c2f
movl %eax, 0xc(%rsp)
xorl %r13d, %r13d
movq %rbx, %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x676c
movq %rbx, %rdi
callq 0x5c2f
movl %eax, 0x20(%rsp)
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movl %r12d, %edx
callq 0x66f1
movq %rbx, %rdi
callq 0x630a
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x6f12
testl %eax, %eax
je 0x4c6d
xorl %eax, %eax
cmpl $0x1, %r12d
setne %al
leal (%rax,%rax,2), %eax
incl %eax
movl %eax, 0x10(%rsp)
movl %r13d, %r15d
xorl %ebp, %ebp
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
xorl %edx, %edx
callq 0x60c9
movq %rax, %r14
movl $0x0, 0x30(%rsp)
cmpl $0x2, %r12d
jne 0x4bdd
movq %rbx, %rdi
movl 0x20(%rsp), %esi
movl %r14d, %edx
callq 0x66f1
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x30(%rsp), %rdx
callq 0x60c9
movq %rax, %rbp
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x5c43
cmpl $0x0, 0x30(%rsp)
je 0x4bfd
movq 0x18(%rsp), %rax
movq (%rax), %rax
movslq %ebp, %rcx
orw $0x4, 0x4(%rax,%rcx,8)
movl %r15d, %r13d
movl %ebp, %r15d
jmp 0x4c28
movq 0x18(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x4fd8
leal 0x1(%r15), %r13d
movq (%rbp), %rax
movslq %r15d, %rcx
movl %r14d, (%rax,%rcx,8)
movl 0x10(%rsp), %edx
movw %dx, 0x4(%rax,%rcx,8)
cmpl $0x1, %r12d
jne 0x4c48
movslq %r15d, %rsi
movq %rbx, %rdi
callq 0x6333
movq %rbx, %rdi
movl 0x20(%rsp), %esi
movl %r14d, %edx
callq 0x69b9
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x5c43
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
callq 0x6f12
movl %r13d, %r15d
testl %eax, %eax
jne 0x4b8a
incl %r12d
cmpl $0x3, %r12d
jne 0x4b49
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movl $0x3, %edx
callq 0x66f1
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x676c
movq %rbx, %rdi
callq 0x509b
movl %eax, %r12d
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x461a
testl %eax, %eax
movl 0x14(%rsp), %eax
cmovnel %ebp, %eax
movl %eax, %ebp
testl %r12d, %r12d
je 0x4cef
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl %r13d, %edx
callq 0x4fd8
movl 0x1b566(%rip), %eax     # 0x20240
movq (%r14), %rcx
movslq %r13d, %r13
movl %eax, (%rcx,%r13,8)
movw $0x1, 0x4(%rcx,%r13,8)
incl %r13d
cmpl $-0x1, %ebp
jne 0x4d0b
testl %r13d, %r13d
jne 0x4d0b
movq %rbx, %rdi
callq 0x630a
movl $0x1, %ebp
jmp 0x4f59
movslq %r13d, %r15
movq 0x18(%rsp), %rax
movq (%rax), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x3340
testl %eax, %eax
jns 0x4d50
callq 0x3060
movl (%rax), %edi
cmpl $0x4, %edi
je 0x4d0e
callq 0x3410
leaq 0x12509(%rip), %rsi     # 0x17245
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x75d0
movl %eax, %ebp
jmp 0x4f59
movq 0x18(%rsp), %rax
movq (%rax), %r14
movl %r12d, 0x28(%rsp)
cmpl $0x1, %r12d
adcl $-0x1, %r13d
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x461a
movl %eax, %ebp
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x5c0e
movl %eax, 0x10(%rsp)
movq %rbx, %rdi
movl $0xfffffffc, %esi       # imm = 0xFFFFFFFC
callq 0x5c0e
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
movl $0xfffffffd, %esi       # imm = 0xFFFFFFFD
callq 0x5c0e
movl %eax, 0x14(%rsp)
testl %r13d, %r13d
jle 0x4e97
movl %r13d, %eax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
testb $0x1, 0x4(%r14,%r12,8)
je 0x4e22
testb $0x39, 0x6(%r14,%r12,8)
je 0x4e22
movl (%r14,%r12,8), %edx
movq %rbx, %rdi
movl 0xc(%rsp), %r13d
movl %r13d, %esi
callq 0x66f1
movq %rbx, %rdi
callq 0x630a
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
leaq 0x1240f(%rip), %rdx     # 0x17205
callq 0x68eb
incl %ebp
movq %rbx, %rdi
movl 0x10(%rsp), %esi
movl %ebp, %edx
callq 0x69b9
movq %rbx, %rdi
callq 0x630a
movl (%r14,%r12,8), %edx
movq %rbx, %rdi
movl %r13d, %esi
callq 0x69b9
testb $0x4, 0x4(%r14,%r12,8)
je 0x4e89
testb $0x3c, 0x6(%r14,%r12,8)
je 0x4e89
movl (%r14,%r12,8), %edx
movq %rbx, %rdi
movl 0x14(%rsp), %r13d
movl %r13d, %esi
callq 0x66f1
movq %rbx, %rdi
callq 0x630a
movq %rbx, %rdi
movl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
leaq 0x123a8(%rip), %rdx     # 0x17205
callq 0x68eb
incl %ebp
movq %rbx, %rdi
movl 0x10(%rsp), %esi
movl %ebp, %edx
callq 0x69b9
movq %rbx, %rdi
callq 0x630a
movl (%r14,%r12,8), %edx
movq %rbx, %rdi
movl %r13d, %esi
callq 0x69b9
incq %r12
cmpq %r12, 0x20(%rsp)
jne 0x4dbb
movl $0x1, %ebp
cmpl $0x0, 0x28(%rsp)
je 0x4f59
movq 0x18(%rsp), %rax
movq (%rax), %rax
testb $0x39, -0x2(%rax,%r15,8)
je 0x4f59
xorl %ebp, %ebp
leaq 0x30(%rsp), %r14
movl $0x1, %r15d
movl 0x1b372(%rip), %edi     # 0x20240
movl $0x20, %edx
movq %r14, %rsi
callq 0x31e0
testl %eax, %eax
cmovgl %r15d, %ebp
cmpl $0x20, %eax
je 0x4ec8
testl %ebp, %ebp
je 0x4d01
xorl %r15d, %r15d
leaq 0x2c(%rsp), %r14
leaq 0x1b353(%rip), %r12     # 0x20250
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %r14, %rsi
movl $0x1, %edx
callq 0x3360
testl %eax, %eax
jle 0x4f34
movq %r12, %rcx
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x4f34
cmpl %eax, 0x8(%rcx)
jne 0x4f16
movl $0x0, 0x8(%rcx)
movl 0x2c(%rsp), %edx
movl %edx, 0xc(%rcx)
incl %r15d
testl %eax, %eax
jg 0x4efd
cmpl $-0x1, %eax
jne 0x4f47
callq 0x3060
cmpl $0x4, (%rax)
je 0x4efd
movl $0x1, %ebp
testl %r15d, %r15d
je 0x4f59
movq %rbx, %rdi
callq 0x509b
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq