// File: /workspace/github_cpp_makefile_star2_O3/halsafar[P]libtmx-parser/./src/tmxparser.cpp
// Function: tmxparser::_parseLayerDataNode(tinyxml2::XMLElement*, std::vector<tmxparser::TmxTileset, std::allocator<tmxparser::TmxTileset>> const&, std::vector<tmxparser::TmxLayerTile, std::allocator<tmxparser::TmxLayerTile>>*)
TmxReturn _parseLayerDataNode(tinyxml2::XMLElement* element, const TmxTilesetCollection_t& tilesets, TmxLayerTileCollection_t* outTileCollection)
{
	TmxReturn error = TmxReturn::kSuccess;

	const char* encoding = element->Attribute("encoding");
	const char* compression = element->Attribute("compression");

	if (compression != NULL)
	{
		LOGE("Does not support compression yet...");
		return TmxReturn::kErrorParsing;
	}

	if (encoding == NULL)
	{
		for (tinyxml2::XMLElement* child = element->FirstChildElement("tile"); child != NULL; child = child->NextSiblingElement("tile"))
		{
			TmxLayerTile tile;
			error = _parseLayerXmlTileNode(child, tilesets, &tile);
			outTileCollection->push_back(tile);
		}
	}
	else if (strcmp(encoding, "csv") == 0)
	{
		std::stringstream csvss(element->FirstChild()->Value());
		//LOGE("TEXT = %s", csv.c_str());

		unsigned int gid = 0;
		while (csvss >> gid)
		{
			if (csvss.peek() == ',' || csvss.peek() == '\n')
			{
				csvss.ignore();
			}

			TmxLayerTile tile;

			tile.gid = gid;
			error = _calculateTileIndices(tilesets, &tile);
			if (error == TmxReturn::kErrorParsing)
			{
				return error;
			}

			outTileCollection->push_back(tile);
		}
	}
	else if (strcmp(encoding, "base64") == 0)
	{
		std::string csvbase64 = element->FirstChild()->Value();

		csvbase64.erase(std::remove(csvbase64.begin(), csvbase64.end(), '\n'), csvbase64.end());
		csvbase64.erase(std::remove(csvbase64.begin(), csvbase64.end(), '\r'), csvbase64.end());
		csvbase64.erase(std::remove(csvbase64.begin(), csvbase64.end(), ' '), csvbase64.end());

		std::string csv = base64_decode(csvbase64);

		unsigned int length = csv.size() / sizeof(unsigned int);

		// tiled base64 layer data is an unsigned 32bit array little endian
		// TODO - verify this on other platforms, write some tests
		unsigned int* p = (unsigned int*)csv.c_str();
		for (unsigned int i = 0; i < length; i++)
		{
			TmxLayerTile tile;
			tile.gid = p[i];
			error = _calculateTileIndices(tilesets, &tile);
			if (error == TmxReturn::kErrorParsing)
			{
				return error;
			}

			outTileCollection->push_back(tile);
		}
	}
	else
	{
		LOGE("Unsupported layer compression [%s]... coming soon...", encoding);
		return TmxReturn::kErrorParsing;
	}

	return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rdx, %rbx
movq %rsi, -0x30(%rbp)
movq %rdi, %r15
leaq 0xb85a(%rip), %rsi      # 0x12436
xorl %edx, %edx
callq 0xe806
movq %rax, %r12
leaq 0xb852(%rip), %rsi      # 0x1243f
movq %r15, %rdi
xorl %edx, %edx
callq 0xe806
testq %rax, %rax
jne 0x6f19
testq %r12, %r12
je 0x6d8f
leaq 0xb871(%rip), %rsi      # 0x12481
movq %r12, %rdi
callq 0x42c0
testl %eax, %eax
je 0x6e08
leaq 0xb85e(%rip), %rsi      # 0x12485
movq %r12, %rdi
callq 0x42c0
testl %eax, %eax
jne 0x6f69
movq 0x30(%r15), %rdi
callq 0xd3dc
leaq -0x1f0(%rbp), %rdi
leaq -0x60(%rbp), %rdx
movq %rax, %rsi
callq 0x40e0
movq -0x1f0(%rbp), %rdi
movq -0x1e8(%rbp), %rsi
addq %rdi, %rsi
leaq -0x60(%rbp), %rdx
movb $0xa, (%rdx)
callq 0xb29a
movq %rax, %rcx
subq -0x1f0(%rbp), %rcx
movq %rcx, -0x1e8(%rbp)
movb $0x0, (%rax)
movq -0x1f0(%rbp), %rdi
movq -0x1e8(%rbp), %rsi
addq %rdi, %rsi
leaq -0x60(%rbp), %rdx
movb $0xd, (%rdx)
callq 0xb29a
movq %rax, %rcx
subq -0x1f0(%rbp), %rcx
movq %rcx, -0x1e8(%rbp)
movb $0x0, (%rax)
movq -0x1f0(%rbp), %rdi
movq -0x1e8(%rbp), %rsi
addq %rdi, %rsi
leaq -0x60(%rbp), %rdx
movb $0x20, (%rdx)
callq 0xb29a
leaq -0x1f0(%rbp), %rsi
movq %rax, %rcx
subq (%rsi), %rcx
movq %rcx, 0x8(%rsi)
movb $0x0, (%rax)
leaq -0x60(%rbp), %rdi
callq 0x112d4
movq -0x58(%rbp), %r13
shrq $0x2, %r13
testl %r13d, %r13d
je 0x6ee4
movq -0x60(%rbp), %r14
movl (%r14), %eax
leaq -0x40(%rbp), %rsi
movl %eax, (%rsi)
movq -0x30(%rbp), %rdi
callq 0x7038
movb $0x1, %r12b
cmpl $0x1, %eax
je 0x6d84
movl %eax, %r15d
movl %r13d, %eax
movq %rax, -0x68(%rbp)
leaq -0x1(%rax), %r12
xorl %r13d, %r13d
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x6d47
movups -0x40(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x8(%rbx)
jmp 0x6d53
movq %rbx, %rdi
leaq -0x40(%rbp), %rdx
callq 0xb16a
cmpq %r13, %r12
je 0x6f20
movl 0x4(%r14,%r13,4), %eax
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x7038
movl %eax, %r15d
incq %r13
cmpl $0x1, %eax
jne 0x6d2f
cmpq -0x68(%rbp), %r13
setb %r12b
movl $0x1, %r15d
jmp 0x6f23
leaq 0xb59a(%rip), %rsi      # 0x12330
movq %r15, %rdi
callq 0xd5f8
testq %rax, %rax
je 0x6edf
movq %rax, %r12
leaq -0x1f0(%rbp), %r13
leaq 0xb578(%rip), %r14      # 0x12330
movq %r12, %rdi
movq -0x30(%rbp), %rsi
movq %r13, %rdx
callq 0x6feb
movl %eax, %r15d
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x6de5
movups -0x1f0(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x8(%rbx)
jmp 0x6df0
movq %rbx, %rdi
movq %r13, %rdx
callq 0xb16a
movq %r12, %rdi
movq %r14, %rsi
callq 0xd720
movq %rax, %r12
testq %rax, %rax
jne 0x6db8
jmp 0x6f54
movq 0x30(%r15), %rdi
callq 0xd3dc
leaq -0x60(%rbp), %rdi
leaq -0x40(%rbp), %rdx
movq %rax, %rsi
callq 0x40e0
leaq -0x1f0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movl $0x18, %edx
callq 0x4350
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6e48
callq 0x41c0
leaq -0x40(%rbp), %rax
movl $0x0, (%rax)
xorl %r15d, %r15d
leaq -0x1f0(%rbp), %r13
leaq -0x60(%rbp), %r12
movq %r13, %rdi
leaq -0x40(%rbp), %rsi
callq 0x43c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x20(%rax,%rcx), %r14d
testb $0x5, %r14b
jne 0x6ef2
movq %r13, %rdi
callq 0x40d0
cmpl $0x2c, %eax
je 0x6e98
movq %r13, %rdi
callq 0x40d0
cmpl $0xa, %eax
jne 0x6ea0
movq %r13, %rdi
callq 0x43f0
movl -0x40(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x30(%rbp), %rdi
movq %r12, %rsi
callq 0x7038
cmpl $0x1, %eax
je 0x6eec
movl %eax, %r15d
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x6ed2
movups -0x60(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x8(%rbx)
jmp 0x6e60
movq %rbx, %rdi
movq %r12, %rdx
callq 0xb16a
jmp 0x6e60
xorl %r15d, %r15d
jmp 0x6f54
xorl %r12d, %r12d
xorl %r15d, %r15d
jmp 0x6f23
movl $0x1, %r15d
movq 0x130af(%rip), %rsi     # 0x19fa8
leaq -0x1f0(%rbp), %rdi
callq 0x4170
leaq -0x170(%rbp), %rdi
callq 0x40a0
testb $0x5, %r14b
jne 0x6f54
jmp 0x6f71
callq 0x4988
jmp 0x6f71
xorl %r12d, %r12d
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6f35
callq 0x41c0
leaq -0x1e0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6f4a
callq 0x41c0
movl $0x1, %eax
testb %r12b, %r12b
jne 0x6f57
movl %r15d, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x49bb
movl $0x1, %eax
jmp 0x6f57
movq %rax, %rbx
leaq -0x50(%rbp), %rax
jmp 0x6f93
jmp 0x6fa3
jmp 0x6f89
jmp 0x6f89
jmp 0x6f89
movq %rax, %rbx
leaq -0x1e0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6fe3
callq 0x41c0
jmp 0x6fe3
movq %rax, %rbx
jmp 0x6fe3
movq %rax, %rbx
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6f8c
callq 0x41c0
jmp 0x6f8c
jmp 0x6fc1
movq %rax, %rbx
movq 0x12fdd(%rip), %rsi     # 0x19fa8
leaq -0x1f0(%rbp), %rdi
callq 0x4170
leaq -0x170(%rbp), %rdi
callq 0x40a0
movq %rbx, %rdi
callq 0x43d0