// File: /workspace/github_cpp_makefile_star2_O1/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 0x14664
movb $0x1, 0x8(%r14)
leaq 0x10(%rbx), %rax
movq %rsp, %rdi
movq %rax, (%rdi)
movb %bpl, 0x8(%rdi)
callq 0x14664
movb $0x1, 0x8(%rsp)
movq 0x1a0(%rbx), %rax
cmpq 0x180(%rbx), %rax
jne 0x14d37
cmpl $0x0, 0x8(%rbx)
jne 0x14d37
movq 0xc8(%rbx), %rax
cmpq 0xa8(%rbx), %rax
jne 0x14d37
cmpq $0x0, 0x1c0(%rbx)
jne 0x14d37
movq %rsp, %rdi
callq 0x1464e
leaq 0x10(%rsp), %rdi
callq 0x1464e
xorl %r14d, %r14d
jmp 0x14e85
movq %rsp, %rdi
callq 0x1464e
leaq 0x10(%rsp), %rdi
callq 0x1464e
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x151e0
testb %al, %al
je 0x14d97
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 0x14e85
cmpq $0x0, 0x1c0(%rbx)
je 0x14ddd
leaq 0x1c0(%rbx), %rdi
callq 0x15518
movsd %xmm0, 0x20(%rsp)
callq 0x14627
movsd 0x20(%rsp), %xmm1
subsd %xmm0, %xmm1
leaq 0x110(%rbx), %rdi
movapd %xmm1, %xmm0
callq 0x1455e
movl %eax, %ebp
testb %al, %al
je 0x14deb
xorl %r14d, %r14d
jmp 0x14df6
leaq 0x110(%rbx), %rdi
callq 0x144bc
jmp 0x14dff
movq %rbx, %rdi
callq 0x14ca6
movq %rax, %r14
testb %bpl, %bpl
je 0x14e85
movq %rsp, %r14
movq %r15, (%r14)
movb $0x0, 0x8(%r14)
movq %r14, %rdi
callq 0x14664
movb $0x1, 0x8(%r14)
movq 0x180(%rbx), %rax
cmpq %rax, 0x1a0(%rbx)
je 0x14e93
leaq 0x170(%rbx), %rdi
movq (%rax), %r14
callq 0x1554a
cmpb $0x1, 0x20(%r14)
jne 0x14e45
movq 0x18(%r14), %rax
movb $0x1, 0x11(%rax)
movq %rsp, %rdi
callq 0x1464e
cmpb $0x0, 0x21(%r14)
jne 0x14e85
testq %r14, %r14
je 0x14e7a
movq (%r14), %rdi
testq %rdi, %rdi
je 0x14e6d
movq 0x10(%r14), %rsi
subq %rdi, %rsi
callq 0x5390
movl $0x28, %esi
movq %r14, %rdi
callq 0x5390
movq %rbx, %rdi
callq 0x14ca6
movq %rax, %r14
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x36b6(%rip), %rdi      # 0x18550
leaq 0x3686(%rip), %rsi      # 0x18527
leaq 0x36b8(%rip), %rcx      # 0x18560
movl $0x91, %edx
callq 0x5210
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x1464e
movq %rbx, %rdi
callq 0x55e0
nop