// File: /workspace/github_cpp_makefile_star2_O2/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, %rax # imm = 0x7FFFFFFFFFFFFFFF
leaq 0x10(%rsp), %r14
movq %rax, (%r14)
andq $0x0, 0x8(%r14)
movq 0x28(%rdi), %r15
movq 0x30(%rdi), %r12
addq $0x10, %r15
shlq $0x5, %r12
testq %r12, %r12
je 0x12712
cmpq $0x0, -0x10(%r15)
je 0x12708
movq %r15, %rdi
movq %r14, %rsi
callq 0x12ece
testb %al, %al
jg 0x12708
movups (%r15), %xmm0
movaps %xmm0, 0x10(%rsp)
addq $0x20, %r15
addq $-0x20, %r12
jmp 0x126e4
callq 0x12f00
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rsi
subq %rax, %rcx
subq %rdx, %rsi
leaq 0x3b9aca00(%rsi), %rax
movq %rsi, %rdx
sarq $0x3f, %rdx
testq %rsi, %rsi
cmovnsq %rsi, %rax
addq %rcx, %rdx
movq %rdx, (%rsp)
movq %rax, 0x8(%rsp)
js 0x1275a
movq 0x50(%rbx), %rax
cmpq 0x58(%rbx), %rax
sete %al
testb %bpl, %al
jne 0x12761
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq 0x10(%rbx), %rdi
movq 0x18(%rbx), %rsi
xorl %r12d, %r12d
movq %rsp, %rdx
xorl %ecx, %ecx
callq 0x3030
callq 0x12f00
movq %rax, 0x28(%rsp)
movq %rdx, 0x30(%rsp)
leaq 0x16498(%rip), %r15     # 0x28c24
xorl %r13d, %r13d
xorl %r14d, %r14d
cmpq 0x8(%rbx), %r13
jae 0x127e2
movq 0x10(%rbx), %rax
cmpw $0x0, 0x6(%rax,%r13,8)
je 0x127d9
movl 0x20(%rbx), %ecx
notl %ecx
movl %ecx, (%rax,%r13,8)
movq %r13, 0x20(%rbx)
movq (%rbx), %rax
movq (%rax,%r12), %r14
andq $0x0, (%rax,%r12)
testq %r14, %r14
jne 0x127cb
movq %r15, %rdi
callq 0x6c65
andq $0x0, (%r14)
movq %r14, %rdi
callq *0x8(%r14)
movb $0x1, %r14b
incq %r13
addq $0x10, %r12
jmp 0x12792
movq 0x28(%rbx), %rbp
movq 0x30(%rbx), %r13
leaq 0x10(%rbp), %r15
shlq $0x5, %r13
leaq 0x28(%rsp), %r12
testq %r13, %r13
je 0x12873
cmpq $0x0, -0x10(%r15)
je 0x12850
movq %r15, %rdi
movq %r12, %rsi
callq 0x12ece
testb %al, %al
jg 0x12850
movq 0x38(%rbx), %rax
movq %rax, (%r15)
movq %rbp, %rax
subq 0x28(%rbx), %rax
sarq $0x5, %rax
movq %rax, 0x38(%rbx)
movq -0x10(%r15), %r14
andq $0x0, -0x10(%r15)
testq %r14, %r14
jne 0x12842
leaq 0x163e7(%rip), %rdi     # 0x28c24
callq 0x6c65
andq $0x0, (%r14)
movq %r14, %rdi
callq *0x8(%r14)
movb $0x1, %r14b
addq $0x20, %r15
addq $0x20, %rbp
addq $-0x20, %r13
jmp 0x127f7
movq 0x40(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x50(%rbx)
movq (%rcx,%rax,8), %rdi
callq *(%rdi)
movb $0x1, %r14b
movq 0x50(%rbx), %rax
cmpq 0x58(%rbx), %rax
jne 0x1285e
andb $0x1, %r14b
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq