// File: /workspace/github_cpp_makefile_star2_O1/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 $0x28, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movabsq $0x7fffffffffffffff, %r15 # imm = 0x7FFFFFFFFFFFFFFF
movq 0x30(%rdi), %rax
xorl %ecx, %ecx
testq %rax, %rax
je 0x16cb2
movq 0x28(%rbx), %rdx
shlq $0x5, %rax
xorl %esi, %esi
xorl %r12d, %r12d
cmpq $0x0, (%rdx,%rsi)
je 0x16ca7
movq 0x10(%rdx,%rsi), %rdi
cmpq %r15, %rdi
setl %r8b
negb %r8b
orb $0x1, %r8b
movzbl %r8b, %r8d
cmpq %r15, %rdi
cmovel %ecx, %r8d
jne 0x16c9a
movq 0x18(%rdx,%rsi), %r8
cmpq %r12, %r8
setl %r9b
negb %r9b
orb $0x1, %r9b
cmpq %r12, %r8
movzbl %r9b, %r8d
cmovel %ecx, %r8d
testb %r8b, %r8b
jg 0x16ca7
movq 0x18(%rdx,%rsi), %r12
movq %rdi, %r15
addq $0x20, %rsi
cmpq %rsi, %rax
jne 0x16c55
jmp 0x16cb5
xorl %r12d, %r12d
leaq 0x18(%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 0x16cff
movq 0x50(%rbx), %rax
cmpq 0x58(%rbx), %rax
sete %al
testb %bpl, %al
jne 0x16d06
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 0x18(%rsp), %r15
xorl %edi, %edi
movq %r15, %rsi
callq 0x3300
movq (%r15), %r12
movq 0x8(%r15), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x8(%rbx)
je 0x16d9e
xorl %r13d, %r13d
leaq 0x18f5d(%rip), %r15     # 0x2fca4
xorl %r14d, %r14d
xorl %ebp, %ebp
movq 0x10(%rbx), %rax
cmpw $0x0, 0x6(%rax,%rbp,8)
je 0x16d91
movl 0x20(%rbx), %ecx
notl %ecx
movl %ecx, (%rax,%rbp,8)
movq %rbp, 0x20(%rbx)
movq (%rbx), %rax
movq (%rax,%r13), %r14
movq $0x0, (%rax,%r13)
testq %r14, %r14
jne 0x16d80
movq %r15, %rdi
callq 0x80fb
movq $0x0, (%r14)
movq %r14, %rdi
callq *0x8(%r14)
movb $0x1, %r14b
incq %rbp
addq $0x10, %r13
cmpq 0x8(%rbx), %rbp
jb 0x16d4c
movq 0x30(%rbx), %r15
testq %r15, %r15
je 0x16e32
movq 0x28(%rbx), %rbp
shlq $0x5, %r15
xorl %esi, %esi
xorl %r13d, %r13d
movq (%rbp,%r13), %rdi
testq %rdi, %rdi
je 0x16e29
movq 0x10(%rbp,%r13), %rcx
cmpq %r12, %rcx
setl %al
negb %al
orb $0x1, %al
movzbl %al, %eax
cmpq %r12, %rcx
cmovel %esi, %eax
jne 0x16dfa
movq 0x18(%rbp,%r13), %rax
movq 0x10(%rsp), %rdx
cmpq %rdx, %rax
setl %cl
negb %cl
orb $0x1, %cl
cmpq %rdx, %rax
movzbl %cl, %eax
cmovel %esi, %eax
testb %al, %al
jg 0x16e29
movq 0x38(%rbx), %rax
movq %rbp, %rcx
addq %r13, %rcx
movq %rax, 0x10(%rbp,%r13)
subq 0x28(%rbx), %rcx
sarq $0x5, %rcx
movq %rcx, 0x38(%rbx)
movq %rsi, (%rbp,%r13)
movq %rsi, (%rdi)
callq *0x8(%rdi)
xorl %esi, %esi
movb $0x1, %r14b
addq $0x20, %r13
cmpq %r13, %r15
jne 0x16db8
movq 0x50(%rbx), %rax
cmpq 0x58(%rbx), %rax
je 0x16e5b
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 0x16e3c
movb $0x1, %r14b
andb $0x1, %r14b
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq