// File: /workspace/github_c_makefile_star2_O2/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 0xa9a6(%rip), %r15      # 0x122a0
movq 0x38(%rbx), %rax
movq 0x40(%rbx), %rdx
testq %rax, %rax
je 0x793c
cmpq %rax, %rdx
jne 0x79bc
movq 0x10(%rbx), %rdi
movq 0x30(%rbx), %rsi
addq 0x20(%rbx), %rdi
movq %rax, %rdx
callq 0x2280
movq 0x40(%rbx), %rax
addq %rax, 0x20(%rbx)
andl $0x0, 0x2c(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
jmp 0x7a1c
cmpq $0x7, %rdx
jbe 0x7a5b
movq 0x30(%rbx), %r12
movslq 0x2c(%rbx), %r13
cmpw $0x70, (%r12,%r13)
jne 0x7a52
movslq 0x4(%r12,%r13), %rax
testq %rax, %rax
js 0x7a52
cmpq %rax, 0x18(%rbx)
jae 0x7991
movq 0x10(%rbx), %rdi
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
callq 0x4207
movq %rax, 0x10(%rbx)
movq $0x7fffffff, 0x18(%rbx) # imm = 0x7FFFFFFF
movq 0x40(%rbx), %rdx
movl 0x4(%r12,%r13), %eax
leal -0x8(%rdx), %ebp
cmpl %eax, %ebp
jne 0x79ee
movq 0x10(%rbx), %rdi
movslq 0x2c(%rbx), %rsi
addq 0x30(%rbx), %rsi
callq 0x2280
movq 0x40(%rbx), %rax
movq %rax, 0x20(%rbx)
andl $0x0, 0x2c(%rbx)
andq $0x0, 0x40(%rbx)
jmp 0x7a1c
jb 0x7a76
jbe 0x7a1c
movq 0x10(%rbx), %rdi
movq 0x30(%rbx), %rsi
addq 0x20(%rbx), %rdi
movq %rax, %rdx
callq 0x2280
movq 0x38(%rbx), %rax
addq %rax, 0x20(%rbx)
subq %rax, 0x40(%rbx)
movl %eax, 0x2c(%rbx)
andq $0x0, 0x38(%rbx)
jmp 0x7a1c
jl 0x7a99
jle 0x7a1c
movl %eax, %edx
addq $0x8, %rdx
movq %rdx, 0x20(%rbx)
movq 0x10(%rbx), %rdi
movslq 0x2c(%rbx), %rsi
addq 0x30(%rbx), %rsi
callq 0x2280
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 0x7a3b
movzbl 0x3(%rcx), %ecx
movl %ecx, 0x28(%rbx)
movq %rbx, %rdi
callq *%rax
jmp 0x7a46
movq %rbx, %rdi
pushq $0x4
popq %rdx
callq 0x4f56
cmpl $0x7, 0x40(%rbx)
jg 0x78fa
jmp 0x7abd
andl $0x0, 0x2c(%rbx)
andq $0x0, 0x38(%rbx)
pushq $0x3
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4f56
movq 0x10(%rbx), %rdi
movq 0x30(%rbx), %rsi
addq 0x20(%rbx), %rdi
callq 0x2280
movq 0x40(%rbx), %rax
addq %rax, 0x20(%rbx)
subq %rax, 0x38(%rbx)
andl $0x0, 0x2c(%rbx)
jmp 0x7abd
movq 0x10(%rbx), %rdi
movslq 0x2c(%rbx), %rsi
addq 0x30(%rbx), %rsi
callq 0x2280
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