// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rsi, -0x30(%rbp)
movq %rdi, %r15
leaq 0xa3ee(%rip), %rsi      # 0x10436
xorl %edx, %edx
callq 0xc8a0
movq %rax, %r12
leaq 0xa3e6(%rip), %rsi      # 0x1043f
movq %r15, %rdi
xorl %edx, %edx
callq 0xc8a0
testq %rax, %rax
jne 0x6301
testq %r12, %r12
je 0x61be
leaq 0xa405(%rip), %rsi      # 0x10481
movq %r12, %rdi
callq 0x42e0
testl %eax, %eax
je 0x6213
leaq 0xa3f2(%rip), %rsi      # 0x10485
movq %r12, %rdi
callq 0x42e0
testl %eax, %eax
jne 0x631f
movq 0x30(%r15), %rdi
callq 0xb914
leaq -0x1f0(%rbp), %rdi
leaq -0x50(%rbp), %rdx
movq %rax, %rsi
callq 0x40d0
movq -0x1f0(%rbp), %rdi
movq -0x1e8(%rbp), %rsi
addq %rdi, %rsi
leaq -0x50(%rbp), %rdx
movb $0xa, (%rdx)
callq 0x9e4e
leaq -0x1f0(%rbp), %rdi
movq (%rdi), %rdx
addq 0x8(%rdi), %rdx
movq %rax, %rsi
callq 0x4080
movq -0x1f0(%rbp), %rdi
movq -0x1e8(%rbp), %rsi
addq %rdi, %rsi
leaq -0x50(%rbp), %rdx
movb $0xd, (%rdx)
callq 0x9e4e
leaq -0x1f0(%rbp), %rdi
movq (%rdi), %rdx
addq 0x8(%rdi), %rdx
movq %rax, %rsi
callq 0x4080
movq -0x1f0(%rbp), %rdi
movq -0x1e8(%rbp), %rsi
addq %rdi, %rsi
leaq -0x50(%rbp), %rdx
movb $0x20, (%rdx)
callq 0x9e4e
leaq -0x1f0(%rbp), %rdi
movq (%rdi), %rdx
addq 0x8(%rdi), %rdx
movq %rax, %rsi
callq 0x4080
movq %r14, -0x68(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
callq 0xf0cf
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %rax
shrq $0x2, %rax
movl %eax, %r13d
xorl %r14d, %r14d
leaq -0x60(%rbp), %r12
xorl %r15d, %r15d
cmpq %r14, %r13
je 0x62c1
movl (%rbx,%r14,4), %eax
movl %eax, -0x60(%rbp)
movq -0x30(%rbp), %rdi
movq %r12, %rsi
callq 0x63ff
cmpl $0x1, %eax
je 0x62c5
movl %eax, %r15d
movq -0x68(%rbp), %rdi
movq %r12, %rsi
callq 0x6d18
incq %r14
jmp 0x6185
leaq 0xa16b(%rip), %r12      # 0x10330
movq %r15, %rdi
movq %r12, %rsi
callq 0xbae8
movq %rax, %rbx
xorl %r15d, %r15d
leaq -0x1f0(%rbp), %r13
testq %rbx, %rbx
je 0x6349
movq %rbx, %rdi
movq -0x30(%rbp), %rsi
movq %r13, %rdx
callq 0x63b2
movl %eax, %r15d
movq %r14, %rdi
movq %r13, %rsi
callq 0x6d18
movq %rbx, %rdi
movq %r12, %rsi
callq 0xbbb8
movq %rax, %rbx
jmp 0x61dd
movq 0x30(%r15), %rdi
callq 0xb914
leaq -0x50(%rbp), %rdi
leaq -0x60(%rbp), %rdx
movq %rax, %rsi
callq 0x40d0
leaq -0x1f0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
pushq $0x18
popq %rdx
callq 0x4350
leaq -0x50(%rbp), %r12
movq %r12, %rdi
callq 0x4478
leaq -0x60(%rbp), %rax
andl $0x0, (%rax)
xorl %r15d, %r15d
leaq -0x1f0(%rbp), %r13
movq %r13, %rdi
leaq -0x60(%rbp), %rsi
callq 0x43c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x20(%rax,%rcx), %ebx
testb $0x5, %bl
jne 0x62ee
movq %r13, %rdi
callq 0x40c0
cmpl $0x2c, %eax
je 0x6292
movq %r13, %rdi
callq 0x40c0
cmpl $0xa, %eax
jne 0x629a
movq %r13, %rdi
callq 0x43e0
movl -0x60(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x30(%rbp), %rdi
movq %r12, %rsi
callq 0x63ff
cmpl $0x1, %eax
je 0x62ea
movl %eax, %r15d
movq %r14, %rdi
movq %r12, %rsi
callq 0x6d18
jmp 0x625c
xorl %ebx, %ebx
jmp 0x62cf
cmpq %r13, %r14
setb %bl
pushq $0x1
popq %r15
leaq -0x50(%rbp), %rdi
callq 0x4478
leaq -0x1f0(%rbp), %rdi
callq 0x4478
testb %bl, %bl
jne 0x6345
jmp 0x6349
pushq $0x1
popq %r15
leaq -0x1f0(%rbp), %rdi
callq 0x4170
testb $0x5, %bl
jne 0x6349
jmp 0x6345
movq 0x12cc8(%rip), %rbx     # 0x18fd0
movq (%rbx), %rcx
leaq 0xa139(%rip), %rdi      # 0x1044b
pushq $0x35
popq %rsi
pushq $0x1
popq %rdx
callq 0x4430
jmp 0x633a
movq 0x12caa(%rip), %rbx     # 0x18fd0
movq (%rbx), %rdi
leaq 0xa15c(%rip), %rsi      # 0x1048c
movq %r12, %rdx
xorl %eax, %eax
callq 0x42f0
movq (%rbx), %rsi
pushq $0xa
popq %rdi
callq 0x4270
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
jmp 0x6394
jmp 0x637a
jmp 0x6375
jmp 0x6375
jmp 0x6375
jmp 0x6375
jmp 0x6375
jmp 0x6375
movq %rax, %rbx
jmp 0x638d
movq %rax, %rbx
jmp 0x63aa
jmp 0x639b
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x4478
leaq -0x1f0(%rbp), %rdi
callq 0x4478
jmp 0x63aa
movq %rax, %rbx
leaq -0x1f0(%rbp), %rdi
callq 0x4170
movq %rbx, %rdi
callq 0x43d0