// File: /workspace/github_c_makefile_star2_O1/yakushstanislav[P]EagleMQ/src/handlers.c
// Function: process_request
void process_request(EagleClient *client)
{
	ProtocolRequestHeader *req;

process:
	if (!client->bodylen)
	{
		if (client->nread < sizeof(*req)) {
			add_status_response(client, 0, EG_PROTOCOL_STATUS_ERROR_PACKET);
			return;
		}

		req = (ProtocolRequestHeader*)(client->buffer + client->offset);

		if (req->magic != EG_PROTOCOL_REQ)
		{
			client->offset = 0;
			client->bodylen = 0;
			add_status_response(client, 0, EG_PROTOCOL_STATUS_ERROR_PACKET);
			return;
		}

		if (req->bodylen > EG_MAX_BUF_SIZE)
		{
			client->offset = 0;
			client->bodylen = 0;
			add_status_response(client, 0, EG_PROTOCOL_STATUS_ERROR_PACKET);
			return;
		}

		if (client->length < req->bodylen) {
			client->request = xrealloc(client->request, EG_MAX_BUF_SIZE);
			client->length = EG_MAX_BUF_SIZE;
		}

		int delta = client->nread - sizeof(*req);

		if (delta == (int)req->bodylen)
		{
			memcpy(client->request, client->buffer + client->offset, client->nread);
			client->pos = client->nread;
			client->offset = 0;
			client->nread = 0;
		}
		else if (delta < (int)req->bodylen)
		{
			memcpy(client->request, client->buffer + client->offset, client->nread);
			client->bodylen = req->bodylen - delta;
			client->pos = client->nread;
			return;
		}
		else if (delta > (int)req->bodylen)
		{
			client->pos = req->bodylen + sizeof(*req);
			memcpy(client->request, client->buffer + client->offset, client->pos);
			client->offset += client->pos;
			client->nread -= client->pos;
		}
	} else {
		if (client->nread == client->bodylen)
		{
			memcpy(client->request + client->pos, client->buffer, client->nread);
			client->pos += client->nread;
			client->offset = 0;
			client->nread = 0;
			client->bodylen = 0;
		}
		else if (client->nread < client->bodylen)
		{
			memcpy(client->request + client->pos, client->buffer, client->nread);
			client->pos += client->nread;
			client->bodylen -= client->nread;
			client->offset = 0;
			return;
		}
		else if (client->nread > client->bodylen)
		{
			memcpy(client->request + client->pos, client->buffer, client->bodylen);
			client->pos += client->bodylen;
			client->nread -= client->bodylen;
			client->offset = client->bodylen;
			client->bodylen = 0;
		}
	}

	req = (ProtocolRequestHeader*)client->request;
	parse_command(client, req);

	if ((int)client->nread >= (int)sizeof(*req)) {
		goto process;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
leaq 0x38(%rdi), %r14
leaq 0xa276(%rip), %r15      # 0x122a0
movq (%r14), %rax
testq %rax, %rax
je 0x806b
movq 0x40(%rbx), %rdx
cmpq %rax, %rdx
jne 0x80f2
movq 0x10(%rbx), %rdi
movq 0x30(%rbx), %rsi
addq 0x20(%rbx), %rdi
callq 0x2290
movq 0x40(%rbx), %rax
addq %rax, 0x20(%rbx)
movl $0x0, 0x2c(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
jmp 0x815a
cmpq $0x7, 0x40(%rbx)
jbe 0x81a1
movq 0x30(%rbx), %r12
movslq 0x2c(%rbx), %r13
cmpw $0x70, (%r12,%r13)
jne 0x8192
movslq 0x4(%r12,%r13), %rax
testq %rax, %rax
js 0x8192
cmpq %rax, 0x18(%rbx)
jae 0x80b8
movq 0x10(%rbx), %rdi
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
callq 0x4401
movq %rax, 0x10(%rbx)
movq $0x7fffffff, 0x18(%rbx) # imm = 0x7FFFFFFF
movq 0x40(%rbx), %rdx
leal -0x8(%rdx), %ebp
movl 0x4(%r12,%r13), %eax
cmpl %eax, %ebp
jne 0x8127
movq 0x10(%rbx), %rdi
movslq 0x2c(%rbx), %rsi
addq 0x30(%rbx), %rsi
callq 0x2290
movq 0x40(%rbx), %rax
movq %rax, 0x20(%rbx)
movl $0x0, 0x2c(%rbx)
movq $0x0, 0x40(%rbx)
jmp 0x815a
jb 0x81be
jbe 0x815a
movq 0x10(%rbx), %rdi
movq 0x30(%rbx), %rsi
addq 0x20(%rbx), %rdi
movq %rax, %rdx
callq 0x2290
movq 0x38(%rbx), %rax
addq %rax, 0x20(%rbx)
subq %rax, 0x40(%rbx)
movl %eax, 0x2c(%rbx)
movq $0x0, 0x38(%rbx)
jmp 0x815a
cmpl %eax, %ebp
jl 0x81e4
cmpl %eax, %ebp
jle 0x815a
addq $0x8, %rax
movq %rax, 0x20(%rbx)
movq 0x10(%rbx), %rdi
movslq 0x2c(%rbx), %rsi
addq 0x30(%rbx), %rsi
movq %rax, %rdx
callq 0x2290
movq 0x20(%rbx), %rax
addl %eax, 0x2c(%rbx)
subq %rax, 0x40(%rbx)
movq 0x10(%rbx), %rcx
movzbl 0x2(%rcx), %esi
movq (%r15,%rsi,8), %rax
testq %rax, %rax
je 0x8179
movzbl 0x3(%rcx), %ecx
movl %ecx, 0x28(%rbx)
movq %rbx, %rdi
callq *%rax
jmp 0x8186
movq %rbx, %rdi
movl $0x4, %edx
callq 0x51a2
cmpl $0x7, 0x40(%rbx)
jg 0x802a
jmp 0x8208
movl $0x0, 0x2c(%rbx)
movq $0x0, 0x38(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
movl $0x3, %edx
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x51a2
movq 0x10(%rbx), %rdi
movq 0x30(%rbx), %rsi
addq 0x20(%rbx), %rdi
callq 0x2290
movq 0x40(%rbx), %rax
addq %rax, 0x20(%rbx)
subq %rax, 0x38(%rbx)
movl $0x0, 0x2c(%rbx)
jmp 0x8208
movq 0x10(%rbx), %rdi
movslq 0x2c(%rbx), %rsi
addq 0x30(%rbx), %rsi
callq 0x2290
movl 0x4(%r12,%r13), %eax
subl %ebp, %eax
movq %rax, 0x38(%rbx)
movq 0x40(%rbx), %rax
movq %rax, 0x20(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq