// File: /workspace/github_cpp_makefile_star2_O3/tjohnman[P]sfxr-sdl2/build/../src/tools.cpp
// Function: LoadTGA(Spriteset&, char const*)
int LoadTGA(Spriteset& tiles, const char *filename)
{
	FILE *file;
	unsigned char byte, crap[16], id_length;
	int i, width, height, channels, x, y;
	file=fopen(filename, "rb");
	if (!file)
		return -1;
	
	int n;
	(void)n;
	n = fread(&id_length, 1, 1, file);
	n = fread(crap, 1, 11, file);
	width=0;
	height=0;
	n = fread(&width, 1, 2, file);	// width
	n = fread(&height, 1, 2, file);	// height
	n = fread(&byte, 1, 1, file);	// bits
	channels=byte/8;
	(void)channels;
	n = fread(&byte, 1, 1, file);	// image descriptor byte (per-bit info)
	for(i=0;i<id_length;i++)
		n = fread(&byte, 1, 1, file);	// image description
	tiles.data=(DWORD*)malloc(width*height*sizeof(DWORD));
	for(y=height-1;y>=0;y--)
		for(x=0;x<width;x++)
		{
			DWORD pixel=0;
			n = fread(&byte, 1, 1, file);
			pixel|=byte;
			n = fread(&byte, 1, 1, file);
			pixel|=byte<<8;
			n = fread(&byte, 1, 1, file);
			pixel|=byte<<16;
			tiles.data[y*width+x]=pixel;
		}
	fclose(file);
	tiles.height=height;
	tiles.width=height;
	tiles.pitch=width;
	
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rax
movq %rdi, %rbx
leaq 0x13f9(%rip), %rsi      # 0xa16c
movq %rax, %rdi
callq 0x2310
testq %rax, %rax
je 0x8f6e
movq %rax, %r14
leaq 0xf(%rsp), %r15
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rax, %rcx
callq 0x23c0
leaq 0x50(%rsp), %rdi
movl $0x1, %esi
movl $0xb, %edx
movq %r14, %rcx
callq 0x23c0
xorl %eax, %eax
leaq 0x24(%rsp), %rdi
movl %eax, (%rdi)
leaq 0x20(%rsp), %r12
movl %eax, (%r12)
movl $0x1, %esi
movl $0x2, %edx
movq %r14, %rcx
callq 0x23c0
movl $0x1, %esi
movl $0x2, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x23c0
leaq 0xe(%rsp), %r12
movl $0x1, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x23c0
movl $0x1, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x23c0
movzbl (%r15), %ebp
testl %ebp, %ebp
je 0x8e46
leaq 0xe(%rsp), %r15
movl $0x1, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x23c0
decl %ebp
jne 0x8e2d
movq %r14, 0x30(%rsp)
movl 0x24(%rsp), %ecx
movslq 0x20(%rsp), %r14
movl %r14d, %eax
movq %rcx, 0x10(%rsp)
imull %ecx, %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x2370
movq %rax, (%rbx)
movq %r14, 0x28(%rsp)
testq %r14, %r14
jle 0x8f4d
movslq 0x10(%rsp), %rcx
movq 0x28(%rsp), %rax
leaq -0x1(%rax), %rdx
movq %rcx, 0x38(%rsp)
imulq %rcx, %rdx
movq %rdx, 0x18(%rsp)
leaq 0xe(%rsp), %rbp
movq %rbx, 0x48(%rsp)
movq 0x30(%rsp), %r15
movq %rax, 0x40(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0x8f2b
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %r13
movl $0x1, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %r15, %rcx
callq 0x23c0
movzbl 0xe(%rsp), %r14d
movl $0x1, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %r15, %rcx
callq 0x23c0
movzbl 0xe(%rsp), %ebx
shll $0x8, %ebx
orl %r14d, %ebx
movl $0x1, %esi
movl $0x1, %edx
movq %rbp, %rdi
movq %r15, %rcx
callq 0x23c0
movzbl 0xe(%rsp), %eax
shll $0x10, %eax
orl %ebx, %eax
movq 0x48(%rsp), %rbx
movq (%rbx), %rcx
movl %eax, (%rcx,%r13,4)
incq %r13
decq %r12
jne 0x8ebd
movq 0x40(%rsp), %rdx
leaq -0x1(%rdx), %rax
movq 0x18(%rsp), %rcx
subq 0x38(%rsp), %rcx
movq %rcx, 0x18(%rsp)
cmpq $0x2, %rdx
jge 0x8ea7
movq 0x30(%rsp), %rdi
callq 0x2280
movq 0x28(%rsp), %rax
movl %eax, 0xc(%rbx)
movl %eax, 0x8(%rbx)
movq 0x10(%rsp), %rax
movl %eax, 0x10(%rbx)
xorl %eax, %eax
jmp 0x8f73
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq