// File: /workspace/github_cpp_makefile_star2_O2/iamlouk[P]asbi/src/events/loop.cc
// Function: asbi::evts::Loop::getEvent()
evts::Event* evts::Loop::getEvent() {
	// check if done:
	// TODO: extract in other function to return early and avoid unnecessary locking
	{
		std::unique_lock<std::mutex> lock1(eventsMtx);
		std::unique_lock<std::mutex> lock2(tasksMtx);
		if (events.empty() && tasksRunning == 0 && tasks.empty() && timers.empty())
			return nullptr;
	}

	// check for timers
	evts::Timer timer;
	if (getNextTimer(timer)) {
		auto evt = new evts::Event();
		evt->taskDone = true;
		evt->callback = timer.callback;
		evt->callback->gc_manage = true;
		evt->doCall = true;
		return evt;
	}


	// wait for event from task or timer
	if (timers.empty()) {
		eventsSem.acquire();
	} else {
		std::chrono::duration<double, std::milli> tts = timers.first().dueAt - evts::timestamp();
		auto timedout = !eventsSem.acquireFor(tts);
		if (timedout)
			return getEvent(); // so that we can check on timers again
	}


	// take event from queue
	evts::Event* evt;
	{
		std::unique_lock<std::mutex> lock(eventsMtx);
		assert(!events.empty());
		evt = events.front();
		events.pop_front();

		if (evt->taskDone)
			evt->callback->gc_manage = true;
	}

	if (evt->doCall) {
		return evt;
	} else {
		delete evt;
		return getEvent();
	}
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
leaq 0xe8(%rdi), %r14
movq %rsp, %rdi
movq %r14, %rsi
callq 0x11e24
leaq 0x10(%rbx), %rsi
leaq 0x10(%rsp), %rdi
callq 0x11e24
movq 0x1a0(%rbx), %rax
cmpq 0x180(%rbx), %rax
jne 0x123ac
cmpl $0x0, 0x8(%rbx)
jne 0x123ac
movq 0xc8(%rbx), %rax
cmpq 0xa8(%rbx), %rax
jne 0x123ac
cmpq $0x0, 0x1c0(%rbx)
jne 0x123ac
leaq 0x10(%rsp), %rdi
callq 0x11e3a
movq %rsp, %rdi
callq 0x11e3a
xorl %r14d, %r14d
jmp 0x124bb
leaq 0x10(%rsp), %rdi
callq 0x11e3a
movq %rsp, %rdi
callq 0x11e3a
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x1275a
testb %al, %al
je 0x12404
pushq $0x28
popq %rdi
callq 0x5390
movq %rax, %r14
xorpd %xmm0, %xmm0
andq $0x0, 0x20(%rax)
movupd %xmm0, (%rax)
movupd %xmm0, 0x10(%rax)
movb $0x1, %al
movb %al, 0x20(%r14)
movq (%rsp), %rcx
movq %rcx, 0x18(%r14)
movb %al, 0x11(%rcx)
movb %al, 0x21(%r14)
jmp 0x124bb
cmpq $0x0, 0x1c0(%rbx)
je 0x12445
leaq 0x1c0(%rbx), %rdi
callq 0x12a98
movsd %xmm0, 0x20(%rsp)
callq 0x11dfc
movsd 0x20(%rsp), %xmm1
subsd %xmm0, %xmm1
leaq 0x110(%rbx), %rdi
movapd %xmm1, %xmm0
callq 0x11d42
testb %al, %al
jne 0x12451
jmp 0x124b0
leaq 0x110(%rbx), %rdi
callq 0x11cc2
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x11e24
movq 0x180(%rbx), %rax
cmpq %rax, 0x1a0(%rbx)
je 0x124c6
leaq 0x170(%rbx), %rdi
movq (%rax), %r14
callq 0x12ac8
cmpb $0x1, 0x20(%r14)
jne 0x1248c
movq 0x18(%r14), %rax
movb $0x1, 0x11(%rax)
leaq 0x10(%rsp), %rdi
callq 0x11e3a
cmpb $0x0, 0x21(%r14)
jne 0x124bb
movq %r14, %rdi
callq 0xe2ee
pushq $0x28
popq %rsi
movq %r14, %rdi
callq 0x53a0
movq %rbx, %rdi
callq 0x12338
movq %rax, %r14
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r14
retq
leaq 0x3093(%rip), %rdi      # 0x15560
leaq 0x3063(%rip), %rsi      # 0x15537
leaq 0x3095(%rip), %rcx      # 0x15570
movl $0x91, %edx
callq 0x5220
movq %rax, %rbx
movq %rsp, %rdi
callq 0x11e3a
movq %rbx, %rdi
callq 0x5600