// File: /workspace/github_c_makefile_star2_O1/sprt[P]lossless-transport/src/packet_implem.c
// Function: pkt_decode
pkt_status_code pkt_decode(const char *data, const size_t len, pkt_t *pkt) {
	if (data == NULL || len < HEADER_SIZE) {
		return E_NOHEADER;
	}

	size_t read = 0;

	memcpy(pkt, data + read, HEADER_SIZE);
	read += HEADER_SIZE;

	size_t payload_size = pkt_get_length(pkt) * sizeof (*pkt->payload);

	/* Field errors should be returned in the order those fields are
	 * declared in the struct. We could return E_UNCONSISTENT if the packet
	 * has a payload and isn't of type PTYPE_DATA, but we don't since it
	 * isn't specified and thus doing so might break other implementations
	 * (albeit buggy ones). */
	if (pkt_get_type(pkt) == 0) {
		return E_TYPE;
	} else if (pkt_get_type(pkt) != PTYPE_DATA && pkt_get_tr(pkt) != 0) {
		return E_TR;
	} else if (pkt_get_window(pkt) > MAX_WINDOW_SIZE) {
		return E_WINDOW;
	} else if (payload_size > MAX_PAYLOAD_SIZE) {
		/* FIXME: pkt_get_length returns 0 when the packet has been
		 * truncated, regardless of the value of length field. Thus, in
		 * that case we can't reliably check whether the length field
		 * overflows the maximum size given by the specification. */
		return E_LENGTH;
	} else if (len != pkt_get_total_size(pkt)) {
		// Return now in order to prevent a potential buffer overflow
		return E_UNCONSISTENT;
	} else if (pkt_get_crc1(pkt) != pkt_compute_crc1(pkt)) {
		return E_CRC;
	}

	// No-op if payload empty
	memcpy(pkt->payload, data + read, payload_size);
	read += payload_size;

	// Prevent memcpy from overflowing
	if (payload_size > 0) {
		memcpy(&pkt->crc2, data + read, sizeof (pkt->crc2));
		if (pkt_get_crc2(pkt) != pkt_compute_crc2(pkt, payload_size)) {
			return E_CRC;
		}
	}

	return PKT_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
testq %rdi, %rdi
sete %al
cmpq $0xc, %rsi
setb %cl
orb %al, %cl
movl $0x8, %ebp
jne 0x1653
movq %rdx, %rbx
movq %rdi, %r14
movl 0x8(%rdi), %eax
movl %eax, 0x8(%rdx)
movq (%rdi), %rax
movq %rax, (%rdx)
movzbl %al, %edx
testb $0x20, %al
jne 0x1578
movzwl 0x2(%rbx), %ecx
rolw $0x8, %cx
jmp 0x157a
xorl %ecx, %ecx
shrl $0x6, %edx
je 0x15b5
cmpl $0x1, %edx
je 0x1591
movl $0x2, %ebp
testb $0x20, %al
jne 0x1653
movzwl %cx, %r15d
movl $0x3, %ebp
cmpl $0x200, %r15d           # imm = 0x200
ja 0x1653
testb $0x20, %al
jne 0x15bf
movzwl 0x2(%rbx), %eax
rolw $0x8, %ax
jmp 0x15c1
movl $0x1, %ebp
jmp 0x1653
xorl %eax, %eax
movzwl %ax, %eax
leaq 0x10(%rax), %rcx
testw %ax, %ax
movl $0xc, %eax
cmovneq %rcx, %rax
movl $0x9, %ebp
cmpq %rsi, %rax
jne 0x1653
movl 0x8(%rbx), %r13d
bswapl %r13d
leaq 0x8(%rsp), %r12
movl $0x210, %edx            # imm = 0x210
movq %r12, %rdi
movq %rbx, %rsi
callq 0x10f0
andb $-0x21, (%r12)
xorl %edi, %edi
movq %r12, %rsi
movl $0x8, %edx
callq 0x1070
movl $0x4, %ebp
cmpl %eax, %r13d
jne 0x1653
leaq 0xc(%rbx), %r12
leaq 0xc(%r14), %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x10f0
testq %r15, %r15
je 0x1651
movl 0xc(%r14,%r15), %r14d
movl %r14d, 0x20c(%rbx)
bswapl %r14d
xorl %edi, %edi
movq %r12, %rsi
movl %r15d, %edx
callq 0x1070
cmpl %eax, %r14d
jne 0x1653
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq