// File: /workspace/github_c_makefile_star2_O2/yakushstanislav[P]EagleMQ/src/event.c
// Function: process_events
int process_events(EventLoop *loop, int flags)
{
	FileEvent *file_event;
	TimeEvent *shortest = NULL;
	struct timeval tv, *tvp;
	int processed = 0, i, mask, fd, rfired, events;
	long sec, ms;

	if (!(flags & EG_EVENT_TIME) && !(flags & EG_EVENT_FILE)) {
		return 0;
	}

	if (loop->maxfd != -1 || ((flags & EG_EVENT_TIME) && !(flags & EG_DONT_WAIT)))
	{
		if (flags & EG_EVENT_TIME && !(flags & EG_DONT_WAIT)) {
			shortest = search_nearest_timer(loop);
		}

		if (shortest)
		{
			get_time(&sec, &ms);

			tvp = &tv;
			tvp->tv_sec = shortest->sec - sec;

			if (shortest->ms < ms) {
				tvp->tv_usec = ((shortest->ms + 1000) - ms) * 1000;
				tvp->tv_sec--;
			} else {
				tvp->tv_usec = (shortest->ms - ms) * 1000;
			}

			if (tvp->tv_sec < 0) {
				tvp->tv_sec = 0;
			}

			if (tvp->tv_usec < 0) {
				tvp->tv_usec = 0;
			}
		} else {
			if (flags & EG_DONT_WAIT) {
				tv.tv_sec = tv.tv_usec = 0;
				tvp = &tv;
			} else {
				tvp = NULL;
			}
		}

		events = event_api_poll(loop, tvp);
		for (i = 0; i < events; i++)
		{
			file_event = &loop->events[loop->fired[i].fd];
			mask = loop->fired[i].mask;
			fd = loop->fired[i].fd;
			rfired = 0;

			if (file_event->mask & mask & EG_EVENT_READABLE) {
				rfired = 1;
				file_event->read_handler(loop, fd, file_event->data, mask);
			}

			if (file_event->mask & mask & EG_EVENT_WRITABLE) {
				if (!rfired || file_event->write_handler != file_event->read_handler) {
					file_event->write_handler(loop, fd, file_event->data, mask);
				}
			}
			processed++;
		}
	}

	if (flags & EG_EVENT_TIME) {
		processed += process_time_events(loop);
	}

	return processed;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, %ebp
testb $0x3, %bpl
je 0x395f
movq %rdi, %rbx
movl %ebp, %ecx
andl $0x6, %ecx
cmpl $0x2, %ecx
je 0x391f
xorl %eax, %eax
cmpl $-0x1, (%rbx)
je 0x3afa
cmpl $0x2, %ecx
jne 0x39d4
leaq 0x20(%rbx), %rax
xorl %r12d, %r12d
movq (%rax), %rax
testq %rax, %rax
je 0x3966
testq %r12, %r12
je 0x3947
movq 0x8(%r12), %rcx
cmpq %rcx, 0x8(%rax)
jge 0x3950
movq %rax, %r12
addq $0x30, %rax
jmp 0x392f
jne 0x394a
movq 0x10(%rax), %rcx
cmpq 0x10(%r12), %rcx
jl 0x3947
jmp 0x394a
xorl %eax, %eax
jmp 0x3bb4
testq %r12, %r12
je 0x39d4
leaq 0x40(%rsp), %r14
leaq 0x38(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x3bc3
movq 0x8(%r12), %rcx
subq (%r14), %rcx
movq %rcx, 0x20(%rsp)
movq 0x10(%r12), %rax
movq (%r15), %rdx
cmpq %rdx, %rax
jge 0x39a8
addq $0x3e8, %rax            # imm = 0x3E8
decq %rcx
movq %rcx, 0x20(%rsp)
subq %rdx, %rax
imulq $0x3e8, %rax, %rdx      # imm = 0x3E8
movq %rdx, 0x28(%rsp)
testq %rcx, %rcx
jns 0x39c2
andq $0x0, 0x20(%rsp)
leaq 0x20(%rsp), %rdx
testq %rax, %rax
jns 0x39e9
andq $0x0, 0x28(%rsp)
jmp 0x39e9
testb $0x4, %bpl
jne 0x39de
xorl %edx, %edx
jmp 0x39e9
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdx
movaps %xmm0, (%rdx)
movq 0x30(%rbx), %r14
movl (%r14), %edi
movq 0x8(%r14), %rsi
movl 0x4(%rbx), %ecx
testq %rdx, %rdx
je 0x3a17
imull $0x3e8, (%rdx), %r8d    # imm = 0x3E8
movq 0x8(%rdx), %rax
movl $0x3e8, %r9d            # imm = 0x3E8
cqto
idivq %r9
addl %r8d, %eax
jmp 0x3a1a
pushq $-0x1
popq %rax
movl %ecx, %edx
movl %eax, %ecx
callq 0x2350
testl %eax, %eax
movl %ebp, 0x1c(%rsp)
jle 0x3a79
movl %eax, %r15d
movq 0x8(%r14), %rcx
movq 0x18(%rbx), %rdx
addq $0x4, %rcx
xorl %esi, %esi
pushq $0x2
popq %rdi
cmpq %rsi, %r15
je 0x3a7b
movl -0x4(%rcx), %r8d
movl (%rcx), %r9d
movl %r8d, %r10d
andl $0x1, %r10d
movl %r8d, %r11d
shrl %r11d
andl $0x2, %r11d
testb $0x18, %r8b
cmovnel %edi, %r11d
orl %r10d, %r11d
movl %r9d, (%rdx,%rsi,8)
movl %r11d, 0x4(%rdx,%rsi,8)
incq %rsi
addq $0xc, %rcx
jmp 0x3a3f
xorl %eax, %eax
movl %eax, 0xc(%rsp)
movl %eax, %r13d
xorl %ebp, %ebp
cmpq %rbp, %r13
je 0x3af2
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
movslq (%rcx,%rbp,8), %r15
movq %r15, %rdx
shlq $0x5, %rdx
leaq (%rax,%rdx), %r14
movl 0x4(%rcx,%rbp,8), %r12d
movl (%rax,%rdx), %eax
andl %r12d, %eax
testb $0x1, %al
je 0x3ad6
movq 0x18(%r14), %rdx
movq %rbx, %rdi
movl %r15d, %esi
movl %r12d, %ecx
callq *0x8(%r14)
movl (%r14), %eax
andl %r12d, %eax
testb $0x2, %al
je 0x3aed
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rax
jne 0x3ade
jmp 0x3aed
testb $0x2, %al
je 0x3aed
movq 0x10(%r14), %rax
movq 0x18(%r14), %rdx
movq %rbx, %rdi
movl %r15d, %esi
movl %r12d, %ecx
callq *%rax
incq %rbp
jmp 0x3a84
movl 0x1c(%rsp), %ebp
movl 0xc(%rsp), %eax
testb $0x2, %bpl
je 0x3bb4
movl %eax, 0xc(%rsp)
leaq 0x20(%rbx), %rbp
movq 0x8(%rbx), %r15
movq 0x20(%rbx), %r13
xorl %r12d, %r12d
leaq 0x10(%rsp), %r14
leaq 0x30(%rsp), %rsi
testq %r13, %r13
je 0x3bad
cmpq %r15, (%r13)
jl 0x3b36
movq 0x30(%r13), %r13
jmp 0x3b21
movq %r14, %rdi
callq 0x3bc3
movq 0x8(%r13), %rax
cmpq %rax, 0x10(%rsp)
jle 0x3b7b
movq %rbp, %r14
movq (%r13), %rbp
movq 0x28(%r13), %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq *0x18(%r13)
incl %r12d
cmpl $-0x1, %eax
je 0x3b8e
leaq 0x8(%r13), %rsi
movslq %eax, %rdi
addq $0x10, %r13
movq %r13, %rdx
callq 0x3832
jmp 0x3b99
jne 0x3b88
movq 0x30(%rsp), %rax
cmpq 0x10(%r13), %rax
jge 0x3b49
addq $0x30, %r13
jmp 0x3ba4
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x38a1
movq %r14, %rbp
movq %r14, %r13
leaq 0x10(%rsp), %r14
movq (%r13), %r13
jmp 0x3b1c
movl 0xc(%rsp), %eax
addl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq