// File: /workspace/github_cpp_makefile_star2_O2/rellla[P]E3DC-Rscp/RscpProtocol.cpp
// Function: RscpProtocol::createFrameAsBuffer(SRscpFrameBuffer*, std::vector<SRscpValue, std::allocator<SRscpValue>> const&, bool)
int32_t RscpProtocol::createFrameAsBuffer(SRscpFrameBuffer *frameBuffer, const std::vector<SRscpValue> & data, bool calcCRC) {
	if(frameBuffer == NULL) {
		return RSCP::ERR_INVALID_INPUT;
	}

	// calculate the required frame size
	size_t sFrameSize =  sizeof(SRscpFrameHeader);

	// calculate the complete data size
	size_t sDataSize = 0;
	for(size_t i = 0; i < data.size(); ++i) {
		sDataSize += sizeof(SRscpValue) - sizeof(data[i].data) + data[i].length;
	}

	// allocate the required memory
	frameBuffer->data = (uint8_t *) malloc(sFrameSize + sDataSize + (calcCRC ? 4 : 0));
	if(frameBuffer->data == NULL) {
		return RSCP::ERR_NO_MEMORY;
	}
	// set the memory size
	frameBuffer->dataLength = sFrameSize + sDataSize + (calcCRC ? 4 : 0);

	// set initial header values
	memset(frameBuffer->data, 0, sizeof(SRscpFrameHeader));
	SRscpFrame* tmpFrame = reinterpret_cast<SRscpFrame*> (frameBuffer->data);
	tmpFrame->header.magic = RSCP::MAGIC;
	tmpFrame->header.ctrl.bits.crc = calcCRC;
	tmpFrame->header.ctrl.bits.version = RSCP::VERSION;
	tmpFrame->header.dataLength = sDataSize;
	setHeaderTimestamp(tmpFrame);

	// insert data from the SRscpValues
	uint8_t *dataPtr = frameBuffer->data + sFrameSize;
	for(size_t i = 0; i < data.size(); ++i) {
		SRscpValue *value = reinterpret_cast<SRscpValue *>(dataPtr);
		value->tag = data[i].tag;
		value->dataType = data[i].dataType;
		value->length = data[i].length;
		if(value->length > 0) {
			// copy into the position of the data pointer and not into the data pointer itself as the data is appended
			memcpy(&value->data, data[i].data, data[i].length);
		}
		dataPtr += sizeof(SRscpValue) - sizeof(data[i].data) + data[i].length;
	}

	// calculate CRC if necessary and add to the frame
	if(calcCRC) {
		uint32_t uCRC32 = calculateCRC32(frameBuffer->data, frameBuffer->dataLength - sizeof(uint32_t));
		memcpy(frameBuffer->data + frameBuffer->dataLength - sizeof(uCRC32), &uCRC32, sizeof(uCRC32));
	}

	return RSCP::OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rsi, %rsi
je 0x4691
movq %rdx, %r14
movq %rsi, %rbx
movq (%rdx), %rsi
movq 0x8(%rdx), %rax
subq %rsi, %rax
pushq $0xf
popq %r12
cqto
idivq %r12
addq $0x5, %rsi
xorl %r13d, %r13d
subq $0x1, %rax
jb 0x45aa
movzwl (%rsi), %edx
addq %rdx, %r13
addq $0x7, %r13
addq $0xf, %rsi
jmp 0x4594
movl %ecx, 0x4(%rsp)
movzbl %cl, %ebp
leaq 0x12(,%rbp,4), %r15
addq %r13, %r15
movq %r15, %rdi
callq 0x21d0
movq %rax, (%rbx)
testq %rax, %rax
je 0x4695
movl %r15d, 0x8(%rbx)
andl $0x0, 0xc(%rax)
andq $0x0, 0x4(%rax)
movw $0xdce3, (%rax)         # imm = 0xDCE3
shll $0xc, %ebp
orl $0x100, %ebp            # imm = 0x100
movw %bp, 0x2(%rax)
movw %r13w, 0x10(%rax)
movq %rax, %rsi
callq 0x4396
movq (%rbx), %r15
addq $0x12, %r15
movq (%r14), %rcx
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x8(%r14), %rax
subq %rcx, %rax
cqto
idivq %r12
cmpq %rax, %rbp
jae 0x466c
movl (%rcx,%r13), %eax
movl %eax, (%r15)
movq (%r14), %rcx
movb 0x4(%rcx,%r13), %al
movb %al, 0x4(%r15)
movzwl 0x5(%rcx,%r13), %eax
movw %ax, 0x5(%r15)
testw %ax, %ax
je 0x4656
leaq 0x7(%r15), %rdi
movq 0x7(%rcx,%r13), %rsi
movzwl 0x5(%rcx,%r13), %edx
callq 0x2120
movq (%r14), %rcx
movzwl 0x5(%rcx,%r13), %eax
addq %rax, %r15
addq $0x7, %r15
incq %rbp
addq $0xf, %r13
jmp 0x460b
xorl %r14d, %r14d
cmpb $0x0, 0x4(%rsp)
je 0x4699
movq (%rbx), %r15
movl 0x8(%rbx), %ebx
leal -0x4(%rbx), %eax
movzwl %ax, %edx
movq %r15, %rsi
callq 0x43d0
movl %eax, -0x4(%r15,%rbx)
jmp 0x4699
pushq $-0x1
jmp 0x4697
pushq $-0x2
popq %r14
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop