// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movl %esi, %r13d
testb $0x3, %r13b
je 0x3f6a
movq %rdi, %rbx
movl %r13d, %eax
andl $0x6, %eax
xorl %ebp, %ebp
cmpl $0x2, %eax
je 0x3f28
cmpl $-0x1, (%rbx)
je 0x40f0
cmpl $0x2, %eax
jne 0x3f71
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x4005
xorl %r12d, %r12d
testq %r12, %r12
je 0x3f4d
movq 0x8(%r12), %rcx
cmpq %rcx, 0x8(%rax)
jge 0x3f5b
movq %rax, %r12
movq 0x30(%rax), %rax
testq %rax, %rax
jne 0x3f3d
jmp 0x3f80
jne 0x3f50
movq 0x10(%rax), %rcx
cmpq 0x10(%r12), %rcx
jl 0x3f4d
jmp 0x3f50
xorl %ebp, %ebp
jmp 0x4241
xorl %ecx, %ecx
btl $0x2, %r13d
adcl $-0x1, %ecx
jmp 0x400a
xorl %r14d, %r14d
leaq 0x18(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x21a0
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
movq %rcx, %rax
imulq 0x8(%r15)
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rdi
movq %rdi, %rax
subq %rdx, %rax
imulq $0x3e8, %rax, %r8       # imm = 0x3E8
leaq 0xf4240(%r8), %rax
xorl %r9d, %r9d
cmpq %rdx, %rdi
setl %r9b
cmovgeq %r8, %rax
subq (%r15), %rsi
subq %r9, %rsi
testq %rsi, %rsi
cmovleq %r14, %rsi
testq %rax, %rax
cmovleq %r14, %rax
mulq %rcx
movq %rdx, %rcx
imull $0x3e8, %esi, %eax      # imm = 0x3E8
shrq $0x7, %rcx
addl %eax, %ecx
jmp 0x400a
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x30(%rbx), %r14
movl (%r14), %edi
movq 0x8(%r14), %rsi
movl 0x4(%rbx), %edx
callq 0x2340
testl %eax, %eax
jle 0x40f0
movl %r13d, 0x10(%rsp)
movq 0x8(%r14), %r11
movq 0x18(%rbx), %rcx
movl %eax, 0x8(%rsp)
movl %eax, %r13d
addq $0x4, %r11
xorl %edx, %edx
movl $0x2, %esi
movl -0x4(%r11), %edi
movl (%r11), %r8d
movl %edi, %r9d
andl $0x1, %r9d
movl %edi, %r10d
shrl %r10d
andl $0x2, %r10d
testb $0x18, %dil
cmovnel %esi, %r10d
orl %r9d, %r10d
movl %r8d, (%rcx,%rdx,8)
movl %r10d, 0x4(%rcx,%rdx,8)
incq %rdx
addq $0xc, %r11
cmpq %rdx, %r13
jne 0x4044
xorl %r15d, %r15d
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
movslq (%rcx,%r15,8), %r12
movq %r12, %rdx
shlq $0x5, %rdx
leaq (%rax,%rdx), %r14
movl 0x4(%rcx,%r15,8), %ebp
movl (%rax,%rdx), %eax
andl %ebp, %eax
testb $0x1, %al
je 0x40c9
movq 0x18(%r14), %rdx
movq %rbx, %rdi
movl %r12d, %esi
movl %ebp, %ecx
callq *0x8(%r14)
movl (%r14), %eax
andl %ebp, %eax
testb $0x2, %al
je 0x40df
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rax
jne 0x40d1
jmp 0x40df
testb $0x2, %al
je 0x40df
movq 0x10(%r14), %rax
movq 0x18(%r14), %rdx
movq %rbx, %rdi
movl %r12d, %esi
movl %ebp, %ecx
callq *%rax
incq %r15
cmpq %r15, %r13
jne 0x407f
movl 0x8(%rsp), %ebp
movl 0x10(%rsp), %r13d
testb $0x2, %r13b
je 0x4241
movq 0x20(%rbx), %r14
testq %r14, %r14
je 0x423b
leaq 0x20(%rbx), %r13
movq 0x8(%rbx), %r15
xorl %r12d, %r12d
cmpq %r15, (%r14)
jl 0x4125
movq 0x30(%r14), %r14
testq %r14, %r14
jne 0x4112
jmp 0x423e
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x21a0
movabsq $0x20c49ba5e353f7cf, %rax # imm = 0x20C49BA5E353F7CF
imulq 0x20(%rsp)
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
movq 0x8(%r14), %rax
cmpq %rax, 0x18(%rsp)
jle 0x4209
movq %r13, 0x8(%rsp)
movq (%r14), %r13
movq 0x28(%r14), %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq *0x18(%r14)
movq %r12, %rcx
incl %ecx
movq %rcx, 0x10(%rsp)
cmpl $-0x1, %eax
je 0x421b
movl %eax, %r12d
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x21a0
movabsq $0x20c49ba5e353f7cf, %rax # imm = 0x20C49BA5E353F7CF
imulq 0x20(%rsp)
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
movslq %r12d, %rax
imulq $0x10624dd3, %rax, %rax # imm = 0x10624DD3
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x26, %rax
addl %ecx, %eax
cltq
movq 0x18(%rsp), %rcx
addq %rax, %rcx
imull $0x3e8, %eax, %eax      # imm = 0x3E8
subl %eax, %r12d
movslq %r12d, %rax
leaq (%rdx,%rax), %rsi
xorl %edi, %edi
cmpq $0x3e8, %rsi            # imm = 0x3E8
setge %dil
leaq -0x3e8(%rax,%rdx), %rax
cmovlq %rsi, %rax
addq %rcx, %rdi
movq %rdi, 0x8(%r14)
movq %rax, 0x10(%r14)
jmp 0x4226
jne 0x4215
cmpq 0x10(%r14), %rdx
jge 0x415d
addq $0x30, %r14
jmp 0x4233
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3e98
movq 0x8(%rsp), %r13
movq %r13, %r14
movq 0x10(%rsp), %r12
movq (%r14), %r14
jmp 0x411b
xorl %r12d, %r12d
addl %r12d, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq