// File: /workspace/github_cpp_makefile_star2_O2/halsafar[P]libtmx-parser/./src/tmxparser.cpp
// Function: tmxparser::_parseTileset(tinyxml2::XMLElement*, tmxparser::TmxTileset*)
TmxReturn _parseTileset(tinyxml2::XMLElement* element, TmxTileset* outTileset)
{
  char const* name = element->Attribute("name");
  if (name != nullptr)
  {
    outTileset->name = name;
  }

  CHECK_AND_RETRIEVE_REQ_ATTRIBUTE(element->QueryUnsignedAttribute, "tilewidth", &outTileset->tileWidth);
  CHECK_AND_RETRIEVE_REQ_ATTRIBUTE(element->QueryUnsignedAttribute, "tileheight", &outTileset->tileHeight);
  outTileset->tileSpacingInImage = element->UnsignedAttribute("spacing");
  outTileset->tileMarginInImage = element->UnsignedAttribute("margin");

  // TODO - read TODO at end of this function
  if (element->FirstChildElement("image") == NULL)
  {
    LOGE("We do not support maps with tilesets that have no image associated currently...");
    return kErrorParsing;
  }

  TmxImage image;
  TmxReturn error = _parseImageNode(element->FirstChildElement("image"), &outTileset->image);
  if (error)
  {
    LOGE("Error parsing image node...");
    return error;
  }

  outTileset->offset.x = 0;
  outTileset->offset.y = 0;

  if (element->FirstChildElement("tileoffset") != NULL)
  {
    error = _parseOffsetNode(element->FirstChildElement("tileoffset"), &outTileset->offset);
  }

  for (tinyxml2::XMLElement* child = element->FirstChildElement("tile"); child != NULL; child = child->NextSiblingElement("tile"))
  {
    TmxTileDefinition tileDef;

    tileDef.id = 0;

    error = _parseTileDefinitionNode(child, &tileDef);
    if (error)
    {
      LOGE("Error parsing tile definition");
      return error;
    }

    outTileset->tileDefinitions[tileDef.id] = tileDef;
  }

  // derive row/col count, calculate tile indices
  // TODO - this is why we do not accept tilesets without image tag atm
  outTileset->colCount = (outTileset->image.width - outTileset->tileMarginInImage) / (outTileset->tileWidth + outTileset->tileSpacingInImage);
  outTileset->rowCount = (outTileset->image.height - outTileset->tileMarginInImage) / (outTileset->tileHeight + outTileset->tileSpacingInImage);

	return TmxReturn::kSuccess;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0xa821(%rip), %rsi      # 0x10261
xorl %edx, %edx
callq 0xc8a0
testq %rax, %rax
je 0x5a58
leaq 0x28(%rbx), %rdi
movq %rax, %rsi
callq 0x4420
leaq 0x48(%rbx), %rdx
leaq 0xa697(%rip), %rsi      # 0x100fa
movq %r14, %rdi
callq 0x6932
cmpl $0x1, %eax
je 0x5cae
leaq 0x4c(%rbx), %rdx
leaq 0xa685(%rip), %rsi      # 0x10104
movq %r14, %rdi
callq 0x6932
cmpl $0x1, %eax
je 0x5cc8
leaq 0xa7e9(%rip), %rsi      # 0x10280
movq %r14, %rdi
callq 0x6bc4
movl %eax, 0x50(%rbx)
leaq 0xa7df(%rip), %rsi      # 0x10288
movq %r14, %rdi
callq 0x6bc4
movl %eax, 0x54(%rbx)
leaq 0xa7d4(%rip), %rsi      # 0x1028f
movq %r14, %rdi
callq 0xbae8
testq %rax, %rax
je 0x5cf6
leaq -0x100(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq -0xe0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq -0xc0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0xa784(%rip), %rsi      # 0x1028f
movq %r14, %rdi
callq 0xbae8
leaq 0x68(%rbx), %rsi
movq %rax, %rdi
callq 0x5915
movl %eax, %r15d
testl %eax, %eax
jne 0x5d26
andq $0x0, 0x58(%rbx)
leaq 0xa7ef(%rip), %rsi      # 0x10325
movq %r14, %rdi
callq 0xbae8
testq %rax, %rax
je 0x5b5e
leaq 0xa7db(%rip), %rsi      # 0x10325
movq %r14, %rdi
callq 0xbae8
leaq 0x58(%rbx), %rsi
movq %rax, %rdi
callq 0x5d7e
leaq 0xa7cb(%rip), %rsi      # 0x10330
movq %r14, %rdi
callq 0xbae8
movq %rax, %r14
leaq -0x70(%rbp), %r15
movq %rbx, -0x38(%rbp)
leaq 0xd0(%rbx), %r12
leaq -0xa8(%rbp), %r13
testq %r14, %r14
je 0x5c4c
movq %r15, -0xa0(%rbp)
movq $0x1, -0x98(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%r15)
movl $0x3f800000, -0x80(%rbp) # imm = 0x3F800000
movups %xmm0, 0x28(%r15)
movups %xmm0, 0x18(%r15)
movups %xmm0, 0x8(%r15)
movups %xmm0, -0x8(%r15)
andl $0x0, -0xa8(%rbp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x5de2
movl %eax, %ebx
testl %eax, %eax
jne 0x5c19
movq %r12, %rdi
movq %r13, %rsi
callq 0x990a
movq %rax, %rdi
movq %r13, %rsi
callq 0x6c1a
movq %r13, %rdi
callq 0x6c5e
testl %ebx, %ebx
jne 0x5c89
movq %r14, %rdi
leaq 0xa724(%rip), %rsi      # 0x10330
callq 0xbbb8
movq %rax, %r14
jmp 0x5b86
movq 0x133b0(%rip), %rax     # 0x18fd0
movq (%rax), %rcx
leaq 0xa70b(%rip), %rdi      # 0x10335
pushq $0x2f
popq %rsi
pushq $0x1
popq %rdx
callq 0x4430
movq 0x13394(%rip), %rax     # 0x18fd0
movq (%rax), %rsi
pushq $0xa
popq %rdi
callq 0x4270
movl %ebx, -0x2c(%rbp)
jmp 0x5bf2
movq -0x38(%rbp), %r8
movl 0xc8(%r8), %eax
movl 0x50(%r8), %ecx
movl 0x54(%r8), %esi
subl %esi, %eax
movl 0x48(%r8), %edi
addl %ecx, %edi
xorl %r15d, %r15d
xorl %edx, %edx
divl %edi
movl %eax, 0x64(%r8)
movl 0xcc(%r8), %eax
subl %esi, %eax
addl 0x4c(%r8), %ecx
xorl %edx, %edx
divl %ecx
movl %eax, 0x60(%r8)
jmp 0x5c8d
movl -0x2c(%rbp), %r15d
leaq -0x110(%rbp), %rdi
callq 0x6c8c
movl %r15d, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1331b(%rip), %rbx     # 0x18fd0
movq (%rbx), %rdi
leaq 0xa409(%rip), %rsi      # 0x100c8
leaq 0xa434(%rip), %rdx      # 0x100fa
jmp 0x5ce0
movq 0x13301(%rip), %rbx     # 0x18fd0
movq (%rbx), %rdi
leaq 0xa3ef(%rip), %rsi      # 0x100c8
leaq 0xa424(%rip), %rdx      # 0x10104
xorl %eax, %eax
callq 0x42f0
movq (%rbx), %rsi
pushq $0xa
popq %rdi
callq 0x4270
pushq $0x2
jmp 0x5d1f
movq 0x132d3(%rip), %rbx     # 0x18fd0
movq (%rbx), %rcx
leaq 0xa58e(%rip), %rdi      # 0x10295
pushq $0x61
popq %rsi
pushq $0x1
popq %rdx
callq 0x4430
movq (%rbx), %rsi
pushq $0xa
popq %rdi
callq 0x4270
pushq $0x1
popq %r15
jmp 0x5c99
movq 0x132a3(%rip), %rbx     # 0x18fd0
movq (%rbx), %rcx
leaq 0xa5c0(%rip), %rdi      # 0x102f7
pushq $0x2d
popq %rsi
pushq $0x1
popq %rdx
callq 0x4430
movq (%rbx), %rsi
pushq $0xa
popq %rdi
callq 0x4270
jmp 0x5c8d
jmp 0x5d56
jmp 0x5d56
movq %rax, %rbx
jmp 0x5d6a
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x6c5e
leaq -0x110(%rbp), %rdi
callq 0x6c8c
movq %rbx, %rdi
callq 0x43d0