// File: 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
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x1bf53(%rip), %rsi     # 0x22271
xorl %eax, %eax
movl %eax, %edx
callq 0x1c710
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x6343
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdi
addq $0x28, %rdi
callq 0x4360
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rdx
addq $0x48, %rdx
leaq 0x1bdb4(%rip), %rsi     # 0x2210a
callq 0x7e20
cmpl $0x1, %eax
jne 0x63a3
movq 0x2cc71(%rip), %rax     # 0x32fd8
movq (%rax), %rdi
leaq 0x1bd67(%rip), %rsi     # 0x220d8
leaq 0x1bd92(%rip), %rdx     # 0x2210a
movb $0x0, %al
callq 0x43a0
movq 0x2cc52(%rip), %rax     # 0x32fd8
movq (%rax), %rdi
leaq 0x1c5a0(%rip), %rsi     # 0x22930
movb $0x0, %al
callq 0x43a0
movl $0x2, -0x4(%rbp)
jmp 0x67b4
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rdx
addq $0x4c, %rdx
leaq 0x1bd5e(%rip), %rsi     # 0x22114
callq 0x7e20
cmpl $0x1, %eax
jne 0x6403
movq 0x2cc11(%rip), %rax     # 0x32fd8
movq (%rax), %rdi
leaq 0x1bd07(%rip), %rsi     # 0x220d8
leaq 0x1bd3c(%rip), %rdx     # 0x22114
movb $0x0, %al
callq 0x43a0
movq 0x2cbf2(%rip), %rax     # 0x32fd8
movq (%rax), %rdi
leaq 0x1c540(%rip), %rsi     # 0x22930
movb $0x0, %al
callq 0x43a0
movl $0x2, -0x4(%rbp)
jmp 0x67b4
movq -0x10(%rbp), %rdi
leaq 0x1be82(%rip), %rsi     # 0x22290
callq 0x82f0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x50(%rax)
movq -0x10(%rbp), %rdi
leaq 0x1be71(%rip), %rsi     # 0x22298
callq 0x82f0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x54(%rax)
movq -0x10(%rbp), %rdi
leaq 0x1be5f(%rip), %rsi     # 0x2229f
callq 0x7b60
cmpq $0x0, %rax
jne 0x6487
movq 0x2cb86(%rip), %rax     # 0x32fd8
movq (%rax), %rdi
leaq 0x1be49(%rip), %rsi     # 0x222a5
movb $0x0, %al
callq 0x43a0
movq 0x2cb6e(%rip), %rax     # 0x32fd8
movq (%rax), %rdi
leaq 0x1c4bc(%rip), %rsi     # 0x22930
movb $0x0, %al
callq 0x43a0
movl $0x1, -0x4(%rbp)
jmp 0x67b4
leaq -0x88(%rbp), %rdi
callq 0x8330
movq -0x10(%rbp), %rdi
leaq 0x1be01(%rip), %rsi     # 0x2229f
callq 0x7b60
movq %rax, -0x120(%rbp)
jmp 0x64ac
movq -0x120(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x68, %rsi
callq 0x6190
movl %eax, -0x124(%rbp)
jmp 0x64c8
movl -0x124(%rbp), %eax
movl %eax, -0x8c(%rbp)
cmpl $0x0, -0x8c(%rbp)
je 0x653c
movq 0x2caf4(%rip), %rax     # 0x32fd8
movq (%rax), %rdi
leaq 0x1be19(%rip), %rsi     # 0x22307
movb $0x0, %al
callq 0x43a0
movq 0x2cadc(%rip), %rax     # 0x32fd8
movq (%rax), %rdi
leaq 0x1c42a(%rip), %rsi     # 0x22930
movb $0x0, %al
callq 0x43a0
movl -0x8c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xa0(%rbp)
jmp 0x6798
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x67a6
movq -0x18(%rbp), %rax
movl $0x0, 0x58(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x5c(%rax)
movq -0x10(%rbp), %rdi
leaq 0x1bdd8(%rip), %rsi     # 0x22335
callq 0x7b60
movq %rax, -0x130(%rbp)
jmp 0x656b
movq -0x130(%rbp), %rax
cmpq $0x0, %rax
je 0x65b9
movq -0x10(%rbp), %rdi
leaq 0x1bdb2(%rip), %rsi     # 0x22335
callq 0x7b60
movq %rax, -0x138(%rbp)
jmp 0x6591
movq -0x138(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x58, %rsi
callq 0x67d0
movl %eax, -0x13c(%rbp)
jmp 0x65ad
movl -0x13c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x1bd7c(%rip), %rsi     # 0x22340
callq 0x7b60
movq %rax, -0x148(%rbp)
jmp 0x65d2
movq -0x148(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
je 0x672f
leaq -0x118(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x8370
movq -0x158(%rbp), %rsi
movl $0x0, -0x118(%rbp)
movq -0xa8(%rbp), %rdi
callq 0x6870
movl %eax, -0x14c(%rbp)
jmp 0x6626
movl -0x14c(%rbp), %eax
movl %eax, -0x8c(%rbp)
cmpl $0x0, -0x8c(%rbp)
je 0x66a3
movq 0x2c996(%rip), %rax     # 0x32fd8
movq (%rax), %rdi
leaq 0x1bcf9(%rip), %rsi     # 0x22345
movb $0x0, %al
callq 0x43a0
movq 0x2c97e(%rip), %rax     # 0x32fd8
movq (%rax), %rdi
leaq 0x1c2cc(%rip), %rsi     # 0x22930
movb $0x0, %al
callq 0x43a0
movl -0x8c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0xa0(%rbp)
jmp 0x66e2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x8450
jmp 0x67a6
movq -0x18(%rbp), %rdi
addq $0xd0, %rdi
leaq -0x118(%rbp), %rsi
callq 0x83b0
movq %rax, -0x160(%rbp)
jmp 0x66c3
movq -0x160(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x83e0
jmp 0x66d8
movl $0x0, -0xa0(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x8450
movl -0xa0(%rbp), %eax
testl %eax, %eax
jne 0x6798
jmp 0x66fe
jmp 0x6700
movq -0xa8(%rbp), %rdi
leaq 0x1bc32(%rip), %rsi     # 0x22340
callq 0x7f80
movq %rax, -0x168(%rbp)
jmp 0x671c
movq -0x168(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x65e0
movq -0x18(%rbp), %rax
movl 0xc8(%rax), %eax
movq -0x18(%rbp), %rcx
subl 0x54(%rcx), %eax
movq -0x18(%rbp), %rcx
movl 0x48(%rcx), %ecx
movq -0x18(%rbp), %rdx
addl 0x50(%rdx), %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x64(%rax)
movq -0x18(%rbp), %rax
movl 0xcc(%rax), %eax
movq -0x18(%rbp), %rcx
subl 0x54(%rcx), %eax
movq -0x18(%rbp), %rcx
movl 0x4c(%rcx), %ecx
movq -0x18(%rbp), %rdx
addl 0x50(%rdx), %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x60(%rax)
movl $0x0, -0x4(%rbp)
movl $0x1, -0xa0(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x8490
jmp 0x67b4
leaq -0x88(%rbp), %rdi
callq 0x8490
jmp 0x67c0
movl -0x4(%rbp), %eax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x98(%rbp), %rdi
callq 0x4470
nopl (%rax)