// File: /workspace/github_cpp_makefile_star2_O3/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 %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
leaq 0xe8(%rdi), %r15
leaq 0x10(%rsp), %r14
movq %r15, (%r14)
xorl %ebp, %ebp
movb %bpl, 0x8(%r14)
movq %r14, %rdi
callq 0x1445a
movb $0x1, 0x8(%r14)
leaq 0x10(%rbx), %rax
movq %rsp, %rdi
movq %rax, (%rdi)
movb %bpl, 0x8(%rdi)
callq 0x1445a
movb $0x1, 0x8(%rsp)
movq 0x1a0(%rbx), %rax
cmpq 0x180(%rbx), %rax
jne 0x14b23
cmpl $0x0, 0x8(%rbx)
jne 0x14b23
movq 0xc8(%rbx), %rax
cmpq 0xa8(%rbx), %rax
jne 0x14b23
cmpq $0x0, 0x1c0(%rbx)
jne 0x14b23
movq %rsp, %rdi
callq 0x14444
leaq 0x10(%rsp), %rdi
callq 0x14444
xorl %r14d, %r14d
jmp 0x14c54
movq %rsp, %rdi
callq 0x14444
leaq 0x10(%rsp), %rdi
callq 0x14444
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x14fae
testb %al, %al
je 0x14b83
movl $0x28, %edi
callq 0x5380
movq %rax, %r14
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movupd %xmm0, 0x10(%rax)
movq $0x0, 0x20(%rax)
movb $0x1, %al
movb %al, 0x20(%r14)
movq 0x10(%rsp), %rcx
movq %rcx, 0x18(%r14)
movb %al, 0x11(%rcx)
movb %al, 0x21(%r14)
jmp 0x14c54
cmpq $0x0, 0x1c0(%rbx)
je 0x14bc7
leaq 0x1c0(%rbx), %rdi
callq 0x152d4
movsd %xmm0, 0x20(%rsp)
callq 0x1441d
movsd 0x20(%rsp), %xmm1
subsd %xmm0, %xmm1
leaq 0x110(%rbx), %rdi
movapd %xmm1, %xmm0
callq 0x14356
testb %al, %al
jne 0x14bd3
jmp 0x14c49
leaq 0x110(%rbx), %rdi
callq 0x142b0
movq %rsp, %r14
movq %r15, (%r14)
movb $0x0, 0x8(%r14)
movq %r14, %rdi
callq 0x1445a
movb $0x1, 0x8(%r14)
movq 0x180(%rbx), %rax
cmpq %rax, 0x1a0(%rbx)
je 0x14c62
leaq 0x170(%rbx), %rdi
movq (%rax), %r14
callq 0x15306
cmpb $0x1, 0x20(%r14)
jne 0x14c19
movq 0x18(%r14), %rax
movb $0x1, 0x11(%rax)
movq %rsp, %rdi
callq 0x14444
cmpb $0x0, 0x21(%r14)
jne 0x14c54
movq (%r14), %rdi
testq %rdi, %rdi
je 0x14c3c
movq 0x10(%r14), %rsi
subq %rdi, %rsi
callq 0x5390
movl $0x28, %esi
movq %r14, %rdi
callq 0x5390
movq %rbx, %rdi
callq 0x14a92
movq %rax, %r14
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x38e7(%rip), %rdi      # 0x18550
leaq 0x38b7(%rip), %rsi      # 0x18527
leaq 0x38e9(%rip), %rcx      # 0x18560
movl $0x91, %edx
callq 0x5210
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x14444
movq %rbx, %rdi
callq 0x55e0