// File: halsafar[P]libtmx-parser/./src/tmxparser.cpp
// Function: tmxparser::calculateTileCoordinatesUV(tmxparser::TmxTileset const&, unsigned int, float, bool, tmxparser::TmxRect&)
TmxReturn calculateTileCoordinatesUV(const TmxTileset& tileset,  unsigned int tileFlatIndex, float pixelCorrection, bool flipY, TmxRect& outRect)
{
	if (tileFlatIndex >= tileset.colCount * tileset.rowCount)
	{
		return TmxReturn::kInvalidTileIndex;
	}

	TileId_t xIndex = tileFlatIndex % tileset.colCount;
	TileId_t yIndex = tileFlatIndex / tileset.colCount;

	unsigned int widthDelta = tileset.tileSpacingInImage + tileset.tileMarginInImage*xIndex;
	unsigned int heightDelta = tileset.tileSpacingInImage + tileset.tileMarginInImage*yIndex;

	float u = (float)((xIndex * tileset.tileWidth) + widthDelta + pixelCorrection) / (float)tileset.image.width;
	float v = (float)((yIndex * tileset.tileHeight) + heightDelta + pixelCorrection) / (float)tileset.image.height;

	float u2 = (float)((( (xIndex+1) * tileset.tileWidth) + widthDelta) - pixelCorrection) / (float)tileset.image.width;
	float v2 = (float)((( (yIndex+1) * tileset.tileHeight) + heightDelta) - pixelCorrection) / (float)tileset.image.height;

	if (flipY)
	{
		float tmpV = v;
		v = 1.f - v2;
		v2 = 1.f - tmpV;
	}

	outRect.u = u;
	outRect.v = v;
	outRect.u2 = u2;
	outRect.v2 = v2;

	return kSuccess;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movb %dl, %al
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movss %xmm0, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq %rcx, -0x28(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x64(%rcx), %ecx
movq -0x10(%rbp), %rdx
imull 0x60(%rdx), %ecx
cmpl %ecx, %eax
jb 0x79bd
movl $0x7, -0x4(%rbp)
jmp 0x7b55
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x64(%rcx), %ecx
xorl %edx, %edx
movl %edx, -0x50(%rbp)
divl %ecx
movl %edx, %eax
movl -0x50(%rbp), %edx
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x64(%rcx), %ecx
divl %ecx
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rcx
movl 0x50(%rcx), %eax
movl 0x54(%rcx), %ecx
movl -0x2c(%rbp), %edx
imull %edx, %ecx
addl %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rcx
movl 0x50(%rcx), %eax
movl 0x54(%rcx), %ecx
movl -0x30(%rbp), %edx
imull %edx, %ecx
addl %ecx, %eax
movl %eax, -0x38(%rbp)
movl -0x2c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl 0x48(%rax), %edx
movl 0xc8(%rax), %eax
imull %edx, %ecx
movl -0x34(%rbp), %edx
addl %edx, %ecx
cvtsi2ss %rcx, %xmm0
movss -0x18(%rbp), %xmm1
addss %xmm1, %xmm0
cvtsi2ss %rax, %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x3c(%rbp)
movl -0x30(%rbp), %ecx
movq -0x10(%rbp), %rax
movl 0x4c(%rax), %edx
movl 0xcc(%rax), %eax
imull %edx, %ecx
movl -0x38(%rbp), %edx
addl %edx, %ecx
cvtsi2ss %rcx, %xmm0
movss -0x18(%rbp), %xmm1
addss %xmm1, %xmm0
cvtsi2ss %rax, %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x40(%rbp)
movl -0x2c(%rbp), %ecx
incl %ecx
movq -0x10(%rbp), %rax
movl 0x48(%rax), %edx
movl 0xc8(%rax), %eax
imull %edx, %ecx
movl -0x34(%rbp), %edx
addl %edx, %ecx
cvtsi2ss %rcx, %xmm0
movss -0x18(%rbp), %xmm1
subss %xmm1, %xmm0
cvtsi2ss %rax, %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x44(%rbp)
movl -0x30(%rbp), %ecx
incl %ecx
movq -0x10(%rbp), %rax
movl 0x4c(%rax), %edx
movl 0xcc(%rax), %eax
imull %edx, %ecx
movl -0x38(%rbp), %edx
addl %edx, %ecx
cvtsi2ss %rcx, %xmm0
movss -0x18(%rbp), %xmm1
subss %xmm1, %xmm0
cvtsi2ss %rax, %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x48(%rbp)
testb $0x1, -0x19(%rbp)
je 0x7b17
movss -0x40(%rbp), %xmm0
movss %xmm0, -0x4c(%rbp)
movss 0x1a509(%rip), %xmm0    # 0x22004
subss -0x48(%rbp), %xmm0
movss %xmm0, -0x40(%rbp)
movss 0x1a4f7(%rip), %xmm0    # 0x22004
subss -0x4c(%rbp), %xmm0
movss %xmm0, -0x48(%rbp)
movss -0x3c(%rbp), %xmm0
movq -0x28(%rbp), %rax
movss %xmm0, (%rax)
movss -0x40(%rbp), %xmm0
movq -0x28(%rbp), %rax
movss %xmm0, 0x4(%rax)
movss -0x44(%rbp), %xmm0
movq -0x28(%rbp), %rax
movss %xmm0, 0x8(%rax)
movss -0x48(%rbp), %xmm0
movq -0x28(%rbp), %rax
movss %xmm0, 0xc(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq
nopw (%rax,%rax)