// File: /workspace/github_cpp_makefile_star2_O1/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:
testq %rsi, %rsi
je 0x4eb1
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r14
movq %rsi, 0x8(%rsp)
movq (%rdx), %rax
movq 0x8(%rdx), %rsi
movabsq $-0x1111111111111111, %rbp # imm = 0xEEEEEEEEEEEEEEEF
subq %rax, %rsi
je 0x4eb7
imulq %rbp, %rsi
cmpq $0x1, %rsi
adcq $0x0, %rsi
addq $0x5, %rax
xorl %r13d, %r13d
movzwl (%rax), %edx
addq %rdx, %r13
addq $0x7, %r13
addq $0xf, %rax
decq %rsi
jne 0x4e9c
jmp 0x4eba
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
retq
xorl %r13d, %r13d
movl %ecx, 0x14(%rsp)
movzbl %cl, %ebx
leaq 0x12(,%rbx,4), %r15
addq %r13, %r15
movq %r15, %rdi
callq 0x2190
movq %rax, %r12
movq 0x8(%rsp), %rax
movq %r12, (%rax)
testq %r12, %r12
je 0x4fe9
movl %r15d, 0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movw $0xdce3, (%r12)         # imm = 0xDCE3
movzwl 0x2(%r12), %eax
andl $0xffffe0ff, %eax       # imm = 0xFFFFE0FF
shll $0xc, %ebx
orl %eax, %ebx
orl $0x100, %ebx            # imm = 0x100
movw %bx, 0x2(%r12)
movw %r13w, 0x10(%r12)
leaq 0x18(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x2200
movq (%r13), %rax
movq %rax, 0x4(%r12)
imull $0x3e8, 0x8(%r13), %eax # imm = 0x3E8
movl %eax, 0xc(%r12)
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
movl 0x14(%rsp), %ebx
je 0x4fc3
movq 0x8(%rsp), %rcx
movq (%rcx), %r12
addq $0x12, %r12
xorl %r13d, %r13d
xorl %r15d, %r15d
movl (%rax,%r13), %eax
movl %eax, (%r12)
movq (%r14), %rax
movb 0x4(%rax,%r13), %cl
movb %cl, 0x4(%r12)
movzwl 0x5(%rax,%r13), %ecx
movw %cx, 0x5(%r12)
testw %cx, %cx
je 0x4f9c
leaq 0x7(%r12), %rdi
movq 0x7(%rax,%r13), %rsi
movzwl 0x5(%rax,%r13), %edx
callq 0x20f0
movq (%r14), %rax
movq 0x8(%r14), %rcx
movzwl 0x5(%rax,%r13), %edx
addq %rdx, %r12
addq $0x7, %r12
incq %r15
subq %rax, %rcx
imulq %rbp, %rcx
addq $0xf, %r13
cmpq %rcx, %r15
jb 0x4f61
testb %bl, %bl
je 0x4fe5
movq 0x8(%rsp), %rax
movq (%rax), %r14
movl 0x8(%rax), %ebx
leal -0x4(%rbx), %eax
movzwl %ax, %edx
movq %r14, %rsi
callq 0x4c4a
movl %eax, -0x4(%r14,%rbx)
xorl %eax, %eax
jmp 0x4fee
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop