// File: /workspace/github_cpp_makefile_star2_O3/Alcaro[P]bridges/arlib/runloop-poll.cpp
// Function: (anonymous namespace)::runloop2_poll::step(bool)
bool step(bool wait)
	{
	again:
		bool ret = false;
		
		timestamp timeout = timestamp::at_never();
		for (auto& node : timeouts.waiting)
		{
			if (node.prod.has_waiter() && node.timeout <= timeout)
				timeout = node.timeout;
		}
		
		timestamp now = timestamp::now();
		duration dur = timeout - now;
#ifdef ARLIB_GUI_GTK3
		GMainContext* gmain_ctx;
		size_t gmain_n_pollfd;
		gint gmain_priority;
		if (has_gui_events)
		{
			gmain_ctx = g_main_context_default(); // g_main_context_query needs this (_prepare doesn't, unclear if glib bug)
			g_main_context_acquire(gmain_ctx);
			g_main_context_prepare(gmain_ctx, &gmain_priority);
			
		query_again:
			int timeout_ms;
			// GPollFD docs say must be same as pollfd, but the structs are
			//  struct pollfd { int fd; short int events; short int revents; };
			//  struct _GPollFD { gint fd; gushort events; gushort revents; };
			// which are not compatible, the latter two fields change signedness
			// it works in practice, but this assert is unusable
			//static_assert(std::is_layout_compatible_v<struct pollfd, GPollFD>);
			gmain_n_pollfd = g_main_context_query(gmain_ctx, gmain_priority, &timeout_ms, (GPollFD*)fds.extra_pollfd(), fds.n_extra_pollfd());
			if (gmain_n_pollfd > fds.n_extra_pollfd())
			{
				fds.reserve_space(gmain_n_pollfd);
				goto query_again;
			}
			
			if (timeout_ms >= 0)
				dur = min(dur, duration::ms(timeout_ms));
		}
#endif
		if (dur.sec < 0 || !scheduled.empty() || !wait)
			dur = { 0, 0 };
		
		test_rethrow();
		ppoll(fds.wait_fd.ptr(), fds.wait_fd.size(), (struct timespec*)&dur, nullptr);
		
#ifdef ARLIB_GUI_GTK3
		if (has_gui_events)
		{
			ret |= g_main_context_check(gmain_ctx, gmain_priority, (GPollFD*)fds.extra_pollfd(), gmain_n_pollfd);
			g_main_context_dispatch(gmain_ctx);
			g_main_context_release(gmain_ctx);
		}
#endif
		
		now = timestamp::now();
		
		// the loop must be index-based, activating a coroutine can add more waiters which can reallocate
		// (and the fd object contains two different arrays to loop over)
		for (size_t n=0;n<fds.n_wait_fds();n++)
		{
			if (fds.wait_fd[n].revents != 0)
			{
				fds.activate(n);
				ret = true;
			}
		}
		for (auto& node : timeouts.waiting)
		{
			if (node.prod.has_waiter() && node.timeout <= now)
			{
				timeouts.activate(&node);
				ret = true;
			}
		}
		while (!scheduled.empty())
		{
			scheduled.pop().resume();
			ret = true;
		}
		test_rethrow();
		
		if (ret && has_gui_events)
		{
			// if there are many events of different priorities ready to run, g_main_context_prepare only considers those of highest priority
			// loop a few more times until glib is done with everything
			wait = false;
			goto again;
		}
		
		return ret;
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movabsq $0x7fffffffffffffff, %r15 # imm = 0x7FFFFFFFFFFFFFFF
movq 0x30(%rdi), %rax
testq %rax, %rax
je 0x16e85
movq 0x28(%rbx), %rcx
shlq $0x5, %rax
xorl %edx, %edx
xorl %r12d, %r12d
cmpq $0x0, (%rcx,%rdx)
je 0x16e7a
movq 0x10(%rcx,%rdx), %rsi
cmpq %r15, %rsi
setl %dil
jne 0x16e6d
cmpq %r12, 0x18(%rcx,%rdx)
setl %dil
je 0x16e77
testb %dil, %dil
je 0x16e7a
movq 0x18(%rcx,%rdx), %r12
movq %rsi, %r15
addq $0x20, %rdx
cmpq %rdx, %rax
jne 0x16e4d
jmp 0x16e88
xorl %r12d, %r12d
leaq 0x28(%rsp), %r14
xorl %edi, %edi
movq %r14, %rsi
callq 0x3300
subq (%r14), %r15
subq 0x8(%r14), %r12
leaq 0x3b9aca00(%r12), %rax
movq %r12, %rcx
sarq $0x3f, %rcx
testq %r12, %r12
cmovnsq %r12, %rax
addq %r15, %rcx
movq %rcx, (%rsp)
movq %rax, 0x8(%rsp)
js 0x16ed2
movq 0x50(%rbx), %rax
cmpq 0x58(%rbx), %rax
sete %al
testb %bpl, %al
jne 0x16ed9
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %rsi
xorl %r14d, %r14d
movq %rsp, %rdx
xorl %ecx, %ecx
callq 0x3030
leaq 0x28(%rsp), %r15
xorl %edi, %edi
movq %r15, %rsi
callq 0x3300
movq (%r15), %rax
movq %rax, 0x20(%rsp)
movq 0x8(%r15), %rax
movq %rax, 0x18(%rsp)
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x16f7d
xorl %ebp, %ebp
leaq 0x18dbc(%rip), %r15     # 0x2fcdc
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x10(%rbx), %rcx
cmpw $0x0, 0x6(%rcx,%r12,8)
je 0x16f71
movl 0x20(%rbx), %eax
notl %eax
movl %eax, (%rcx,%r12,8)
movq %r12, 0x20(%rbx)
movq (%rbx), %rax
movq (%rax,%rbp), %r14
movq $0x0, (%rax,%rbp)
testq %r14, %r14
jne 0x16f5c
movq %r15, %rdi
callq 0x8683
movq $0x0, (%r14)
movq %r14, %rdi
callq *0x8(%r14)
movq 0x8(%rbx), %rax
movb $0x1, %r14b
incq %r12
addq $0x10, %rbp
cmpq %rax, %r12
jb 0x16f26
movq 0x30(%rbx), %r15
testq %r15, %r15
je 0x16ff0
movq 0x28(%rbx), %rbp
shlq $0x5, %r15
xorl %r13d, %r13d
xorl %r12d, %r12d
movq (%rbp,%r12), %rdi
testq %rdi, %rdi
je 0x16fe7
movq 0x20(%rsp), %rax
cmpq %rax, 0x10(%rbp,%r12)
setl %al
jne 0x16fbc
movq 0x18(%rsp), %rax
cmpq %rax, 0x18(%rbp,%r12)
setl %al
je 0x16fc0
testb %al, %al
je 0x16fe7
movq 0x38(%rbx), %rax
leaq (%r12,%rbp), %rcx
movq %rax, 0x10(%rbp,%r12)
subq 0x28(%rbx), %rcx
sarq $0x5, %rcx
movq %rcx, 0x38(%rbx)
movq %r13, (%rbp,%r12)
movq %r13, (%rdi)
callq *0x8(%rdi)
movb $0x1, %r14b
addq $0x20, %r12
cmpq %r12, %r15
jne 0x16f94
movq 0x50(%rbx), %rax
cmpq 0x58(%rbx), %rax
je 0x17019
movq 0x40(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x50(%rbx)
movq (%rcx,%rax,8), %rdi
callq *(%rdi)
movq 0x50(%rbx), %rax
cmpq 0x58(%rbx), %rax
jne 0x16ffa
movb $0x1, %r14b
andb $0x1, %r14b
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq