// File: /workspace/github_c_makefile_star2_O2/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 0x4fd1
pushq $0x3
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
jmp 0x4fe7
movq 0x10(%rbx), %r12
testb $0x20, 0x8(%rbx)
jne 0x4ffa
movzbl 0x2(%r12), %esi
pushq $0x5
popq %rdx
movq %rbx, %rdi
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x4f56
leaq 0x98(%rsp), %r15
xorl %edi, %edi
movq %r15, %rsi
callq 0x23d0
pushq $-0x1
popq %rdi
leaq 0x8(%rsp), %rsi
callq 0x23d0
pushq $0x3f
popq %rdi
callq 0x417e
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 0xd236(%rip), %r12      # 0x12290
movq (%r12), %rcx
subl 0x198(%rcx), %eax
movl %eax, 0xb(%r14)
cvtsi2ssq 0x10(%r15), %xmm0
cvtsi2ssq 0x18(%r15), %xmm1
movss 0x7670(%rip), %xmm2     # 0xc6ec
divss %xmm2, %xmm1
addss %xmm0, %xmm1
movss %xmm1, 0xf(%r14)
cvtsi2ssq (%r15), %xmm0
cvtsi2ssq 0x8(%r15), %xmm1
divss %xmm2, %xmm1
addss %xmm0, %xmm1
movss %xmm1, 0x13(%r14)
callq 0x4387
movl %eax, 0x17(%r14)
callq 0x43ce
movl %eax, 0x1b(%r14)
callq 0x44b5
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)
andq $0x0, 0x37(%r14)
pushq $0x3f
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x512c
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r14
popq %r15
retq