// File: /workspace/github_c_makefile_star2_O3/yakushstanislav[P]EagleMQ/src/handlers.c
// Function: stat_command_handler
void stat_command_handler(EagleClient *client)
{
	ProtocolRequestStat *req = (ProtocolRequestStat*)client->request;
	ProtocolResponseStat *stat;
	struct rusage self_ru, c_ru;

	if (client->pos < sizeof(*req)) {
		add_status_response(client, 0, EG_PROTOCOL_STATUS_ERROR_PACKET);
		return;
	}

	if (!BIT_CHECK(client->perm, EG_USER_ADMIN_PERM)) {
		add_status_response(client, req->cmd, EG_PROTOCOL_STATUS_ERROR_ACCESS);
		return;
	}

	getrusage(RUSAGE_SELF, &self_ru);
	getrusage(RUSAGE_CHILDREN, &c_ru);

	stat = (ProtocolResponseStat*)xcalloc(sizeof(*stat));

	set_response_header(&stat->header, req->cmd, EG_PROTOCOL_STATUS_SUCCESS, sizeof(stat->body));

	stat->body.version.major = EAGLE_VERSION_MAJOR;
	stat->body.version.minor = EAGLE_VERSION_MINOR;
	stat->body.version.patch = EAGLE_VERSION_PATCH;

	stat->body.uptime = time(NULL) - server->start_time;
	stat->body.used_cpu_sys = (float)self_ru.ru_stime.tv_sec + (float)self_ru.ru_stime.tv_usec/1000000;
	stat->body.used_cpu_user = (float)self_ru.ru_utime.tv_sec + (float)self_ru.ru_utime.tv_usec/1000000;
	stat->body.used_memory = xmalloc_used_memory();
	stat->body.used_memory_rss = xmalloc_memory_rss();
	stat->body.fragmentation_ratio = xmalloc_fragmentation_ratio();
	stat->body.clients = EG_LIST_LENGTH(server->clients);
	stat->body.users = EG_LIST_LENGTH(server->users);
	stat->body.queues = EG_LIST_LENGTH(server->queues);
	stat->body.routes = EG_LIST_LENGTH(server->routes);
	stat->body.channels = EG_LIST_LENGTH(server->channels);
	stat->body.resv3 = 0;
	stat->body.resv4 = 0;

	add_response(client, stat, sizeof(*stat));
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdi, %rbx
cmpq $0x7, 0x20(%rdi)
ja 0x56ca
movq %rbx, %rdi
xorl %esi, %esi
movl $0x3, %edx
jmp 0x56e2
movq 0x10(%rbx), %r12
testb $0x20, 0x8(%rbx)
jne 0x56f5
movzbl 0x2(%r12), %esi
movq %rbx, %rdi
movl $0x5, %edx
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x5642
leaq 0x98(%rsp), %r15
xorl %edi, %edi
movq %r15, %rsi
callq 0x23c0
leaq 0x8(%rsp), %rsi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x23c0
movl $0x3f, %edi
callq 0x47f6
movq %rax, %r14
movb 0x2(%r12), %al
movw $0x80, (%r14)
movb %al, 0x2(%r14)
movb $0x1, 0x3(%r14)
movl $0x37, 0x4(%r14)
movw $0x301, 0x8(%r14)       # imm = 0x301
movb $0x0, 0xa(%r14)
xorl %edi, %edi
callq 0x22a0
leaq 0xcb37(%rip), %r12      # 0x12290
movq (%r12), %rcx
subl 0x198(%rcx), %eax
movl %eax, 0xb(%r14)
cvtsi2ssq 0x10(%r15), %xmm0
cvtsi2ssq 0x18(%r15), %xmm1
movss 0x6fb1(%rip), %xmm2     # 0xc72c
divss %xmm2, %xmm1
addss %xmm0, %xmm1
movss %xmm1, 0xf(%r14)
xorps %xmm0, %xmm0
cvtsi2ssq (%r15), %xmm0
xorps %xmm1, %xmm1
cvtsi2ssq 0x8(%r15), %xmm1
divss %xmm2, %xmm1
addss %xmm0, %xmm1
movss %xmm1, 0x13(%r14)
callq 0x4a10
movl %eax, 0x17(%r14)
callq 0x4a57
movl %eax, 0x1b(%r14)
callq 0x4b32
movss %xmm0, 0x1f(%r14)
movq (%r12), %rax
movq 0x160(%rax), %rcx
movl 0x20(%rcx), %ecx
movl %ecx, 0x23(%r14)
movq 0x168(%rax), %rcx
movl 0x20(%rcx), %ecx
movl %ecx, 0x27(%r14)
movq 0x170(%rax), %rcx
movl 0x20(%rcx), %ecx
movl %ecx, 0x2b(%r14)
movq 0x178(%rax), %rcx
movl 0x20(%rcx), %ecx
movl %ecx, 0x2f(%r14)
movq 0x180(%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x33(%r14)
movq $0x0, 0x37(%r14)
movq %rbx, %rdi
movq %r14, %rsi
movl $0x3f, %edx
callq 0x5836
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r14
popq %r15
retq