// File: /workspace/github_cpp_makefile_star2_O1/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 0xb834(%rip), %rsi      # 0x12436
xorl %edx, %edx
callq 0xe9e8
movq %rax, %r12
leaq 0xb82c(%rip), %rsi      # 0x1243f
movq %r15, %rdi
xorl %edx, %edx
callq 0xe9e8
testq %rax, %rax
jne 0x6f31
testq %r12, %r12
je 0x6db0
leaq 0xb84b(%rip), %rsi      # 0x12481
movq %r12, %rdi
callq 0x42c0
testl %eax, %eax
je 0x6e29
leaq 0xb838(%rip), %rsi      # 0x12485
movq %r12, %rdi
callq 0x42c0
testl %eax, %eax
jne 0x6f82
movq 0x30(%r15), %rdi
callq 0xd476
leaq -0x1e8(%rbp), %rdi
leaq -0x60(%rbp), %rdx
movq %rax, %rsi
callq 0x40e0
movq -0x1e8(%rbp), %rdi
movq -0x1e0(%rbp), %rsi
addq %rdi, %rsi
leaq -0x60(%rbp), %rdx
movb $0xa, (%rdx)
callq 0xb2a6
movq %rax, %rcx
subq -0x1e8(%rbp), %rcx
movq %rcx, -0x1e0(%rbp)
movb $0x0, (%rax)
movq -0x1e8(%rbp), %rdi
movq -0x1e0(%rbp), %rsi
addq %rdi, %rsi
leaq -0x60(%rbp), %rdx
movb $0xd, (%rdx)
callq 0xb2a6
movq %rax, %rcx
subq -0x1e8(%rbp), %rcx
movq %rcx, -0x1e0(%rbp)
movb $0x0, (%rax)
movq -0x1e8(%rbp), %rdi
movq -0x1e0(%rbp), %rsi
addq %rdi, %rsi
leaq -0x60(%rbp), %rdx
movb $0x20, (%rdx)
callq 0xb2a6
leaq -0x1e8(%rbp), %rsi
movq %rax, %rcx
subq (%rsi), %rcx
movq %rcx, 0x8(%rsi)
movb $0x0, (%rax)
leaq -0x60(%rbp), %rdi
callq 0x11509
movq -0x58(%rbp), %r13
shrq $0x2, %r13
testl %r13d, %r13d
setne %r12b
je 0x6f2c
movq -0x60(%rbp), %r14
movl (%r14), %eax
leaq -0x40(%rbp), %rsi
movl %eax, (%rsi)
movq -0x30(%rbp), %rdi
callq 0x7051
movl %eax, %r15d
cmpl $0x1, %eax
je 0x6f3c
movl %r13d, %r13d
movl $0x1, %r12d
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x6d6d
movups -0x40(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x8(%rbx)
jmp 0x6d79
movq %rbx, %rdi
leaq -0x40(%rbp), %rdx
callq 0xb176
cmpq %r12, %r13
je 0x6f38
movl (%r14,%r12,4), %eax
movl %eax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x7051
movl %eax, %r15d
incq %r12
cmpl $0x1, %eax
jne 0x6d55
decq %r12
cmpq %r13, %r12
setb %r12b
jmp 0x6f3c
leaq 0xb579(%rip), %rsi      # 0x12330
movq %r15, %rdi
callq 0xd6a6
testq %rax, %rax
je 0x6f27
movq %rax, %r12
leaq -0x1e8(%rbp), %r13
leaq 0xb557(%rip), %r14      # 0x12330
movq %r12, %rdi
movq -0x30(%rbp), %rsi
movq %r13, %rdx
callq 0x7004
movl %eax, %r15d
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x6e06
movups -0x1e8(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x8(%rbx)
jmp 0x6e11
movq %rbx, %rdi
movq %r13, %rdx
callq 0xb176
movq %r12, %rdi
movq %r14, %rsi
callq 0xd84e
movq %rax, %r12
testq %rax, %rax
jne 0x6dd9
jmp 0x6f6d
movq 0x30(%r15), %rdi
callq 0xd476
leaq -0x60(%rbp), %rdi
leaq -0x40(%rbp), %rdx
movq %rax, %rsi
callq 0x40e0
leaq -0x1e8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movl $0x18, %edx
callq 0x4350
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6e69
callq 0x41c0
leaq -0x40(%rbp), %rax
movl $0x0, (%rax)
xorl %r15d, %r15d
leaq -0x1e8(%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 0x6f00
movq %r13, %rdi
callq 0x40d0
cmpl $0x2c, %eax
je 0x6eb9
movq %r13, %rdi
callq 0x40d0
cmpl $0xa, %eax
jne 0x6ec1
movq %r13, %rdi
callq 0x43f0
movl -0x40(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x30(%rbp), %rdi
movq %r12, %rsi
callq 0x7051
movl %eax, %r15d
cmpl $0x1, %eax
je 0x6f00
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x6ef3
movups -0x60(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x8(%rbx)
jmp 0x6e81
movq %rbx, %rdi
movq %r12, %rdx
callq 0xb176
jmp 0x6e81
movq 0x130a1(%rip), %rsi     # 0x19fa8
leaq -0x1e8(%rbp), %rdi
callq 0x4170
leaq -0x168(%rbp), %rdi
callq 0x40a0
testb $0x5, %r14b
jne 0x6f6d
jmp 0x6f8a
xorl %r15d, %r15d
jmp 0x6f6d
xorl %r15d, %r15d
jmp 0x6f3c
callq 0x4988
jmp 0x6f8a
seta %r12b
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6f4e
callq 0x41c0
leaq -0x1d8(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6f63
callq 0x41c0
movl $0x1, %eax
testb %r12b, %r12b
jne 0x6f70
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 0x6f70
movq %rax, %rbx
leaq -0x50(%rbp), %rax
jmp 0x6fac
jmp 0x6fbc
jmp 0x6fa2
jmp 0x6fa2
jmp 0x6fa2
movq %rax, %rbx
leaq -0x1d8(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6ffc
callq 0x41c0
jmp 0x6ffc
movq %rax, %rbx
jmp 0x6ffc
movq %rax, %rbx
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6fa5
callq 0x41c0
jmp 0x6fa5
jmp 0x6fda
movq %rax, %rbx
movq 0x12fc4(%rip), %rsi     # 0x19fa8
leaq -0x1e8(%rbp), %rdi
callq 0x4170
leaq -0x168(%rbp), %rdi
callq 0x40a0
movq %rbx, %rdi
callq 0x43d0