// File: /workspace/github_c_makefile_star2_O3/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 0x166d
movq %rdx, %rbx
movq %rdi, %r14
movl 0x8(%rdi), %eax
movl %eax, 0x8(%rdx)
movq (%rdi), %rax
movq %rax, (%rdx)
movzbl %al, %ecx
testb $0x20, %al
jne 0x157b
movzwl 0x2(%rbx), %edx
rolw $0x8, %dx
jmp 0x157d
xorl %edx, %edx
shrl $0x6, %ecx
je 0x15a8
movzwl %dx, %r15d
cmpl $0x1, %ecx
jne 0x15b2
movl $0x3, %ebp
cmpl $0x200, %r15d           # imm = 0x200
ja 0x166d
movl $0xc, %ecx
testb $0x20, %al
je 0x15d1
jmp 0x15ee
movl $0x1, %ebp
jmp 0x166d
movl $0x2, %ebp
testb $0x20, %al
jne 0x166d
movl $0x3, %ebp
cmpl $0x200, %r15d           # imm = 0x200
ja 0x166d
movzwl 0x2(%rbx), %eax
movl %eax, %ecx
rolw $0x8, %cx
movzwl %cx, %edx
addq $0x10, %rdx
testw %ax, %ax
movl $0xc, %ecx
cmovneq %rdx, %rcx
movl $0x9, %ebp
cmpq %rsi, %rcx
jne 0x166d
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 0x166d
leaq 0xc(%rbx), %r12
leaq 0xc(%r14), %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x10f0
testq %r15, %r15
je 0x166b
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 0x166d
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq