// File: /workspace/github_c_makefile_star2_O3/yakushstanislav[P]EagleMQ/src/handlers.c
// Function: channel_list_command_handler
void channel_list_command_handler(EagleClient *client)
{
	ProtocolRequestChannelList *req = (ProtocolRequestChannelList*)client->request;
	ProtocolResponseHeader res;
	ListIterator iterator;
	ListNode *node;
	Channel_t *channel;
	char *list;
	uint32_t topics;
	uint32_t patterns;
	int i;

	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) && !BIT_CHECK(client->perm, EG_USER_CHANNEL_PERM)
		&& !BIT_CHECK(client->perm, EG_USER_CHANNEL_LIST_PERM)) {
		add_status_response(client, req->cmd, EG_PROTOCOL_STATUS_ERROR_ACCESS);
		return;
	}

	set_response_header(&res, req->cmd, EG_PROTOCOL_STATUS_SUCCESS,
		EG_LIST_LENGTH(server->channels) * (64 + (sizeof(uint32_t) * 3)));

	list = (char*)xcalloc(sizeof(res) + res.bodylen);

	memcpy(list, &res, sizeof(res));

	i = sizeof(res);
	list_rewind(server->channels, &iterator);
	while ((node = list_next_node(&iterator)) != NULL)
	{
		channel = EG_LIST_NODE_VALUE(node);

		topics = EG_KEYLIST_LENGTH(channel->topics);
		patterns = EG_KEYLIST_LENGTH(channel->patterns);

		memcpy(list + i, channel->name, strlenz(channel->name));
		i += 64;
		memcpy(list + i, &channel->flags, sizeof(uint32_t));
		i += sizeof(uint32_t);
		memcpy(list + i, &topics, sizeof(uint32_t));
		i += sizeof(uint32_t);
		memcpy(list + i, &patterns, sizeof(uint32_t));
		i += sizeof(uint32_t);
	}

	add_response(client, list, sizeof(res) + res.bodylen);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpq $0x7, 0x20(%rdi)
ja 0x7ae7
xorl %esi, %esi
movl $0x3, %edx
jmp 0x7c07
movq 0x10(%rdi), %rax
movabsq $0x200000000024, %rcx   # imm = 0x200000000024
movzbl 0x2(%rax), %r12d
testq %rcx, 0x8(%rdi)
je 0x7bff
movq %rdi, 0x10(%rsp)
leaq 0xa780(%rip), %r14      # 0x12290
movq (%r14), %rax
movq 0x180(%rax), %rax
imull $0x4c, 0x20(%rax), %ebx
leaq 0x8(%rbx), %rdi
callq 0x47f6
movq %rax, %rbp
movq %rbx, 0x8(%rsp)
movq %rbx, %rax
shlq $0x20, %rax
shll $0x10, %r12d
addq %r12, %rax
addq $0x1000080, %rax        # imm = 0x1000080
movq %rax, (%rbp)
movq (%r14), %rax
movq 0x180(%rax), %rdi
leaq 0x18(%rsp), %r12
movq %r12, %rsi
callq 0x8ed9
movq %r12, %rdi
callq 0x8e98
testq %rax, %rax
je 0x7bdb
movl $0x50, %r14d
leal -0x48(%r14), %ecx
movq 0x10(%rax), %r13
movq 0x50(%r13), %rax
movq 0x58(%r13), %rdx
movl 0x20(%rax), %ebx
movl 0x20(%rdx), %eax
movl %eax, 0x4(%rsp)
movslq %ecx, %r15
leaq (%r15,%rbp), %r12
movq %r13, %rdi
callq 0x2150
leaq 0x1(%rax), %rdx
movq %r12, %rdi
movq %r13, %rsi
callq 0x2280
movl 0x40(%r13), %eax
movl %eax, 0x40(%rbp,%r15)
leal -0x4(%r14), %eax
cltq
movl %ebx, (%rbp,%rax)
movslq %r14d, %r14
movl 0x4(%rsp), %eax
movl %eax, (%rbp,%r14)
leaq 0x18(%rsp), %rdi
callq 0x8e98
addl $0x4c, %r14d
testq %rax, %rax
jne 0x7b71
movq 0x8(%rsp), %rdx
addl $0x8, %edx
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x5836
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r12d, %esi
movl $0x5, %edx
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5642