// File: 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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x16f1(%rip), %rsi      # 0xe13c
callq 0x3410
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xca67
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xcc6b
movq -0x20(%rbp), %rcx
leaq -0x41(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x34f0
movl %eax, -0x60(%rbp)
leaq -0x40(%rbp), %rdi
movq -0x20(%rbp), %rcx
movl $0x1, %esi
movl $0xb, %edx
callq 0x34f0
movl %eax, -0x60(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x4c(%rbp), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x34f0
movl %eax, -0x60(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x50(%rbp), %rdi
movl $0x1, %esi
movl $0x2, %edx
callq 0x34f0
movl %eax, -0x60(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x21(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x34f0
movl %eax, -0x60(%rbp)
movzbl -0x21(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, -0x54(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x21(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x34f0
movl %eax, -0x60(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
movzbl -0x41(%rbp), %ecx
cmpl %ecx, %eax
jge 0xcb4f
movq -0x20(%rbp), %rcx
leaq -0x21(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x34f0
movl %eax, -0x60(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xcb21
movl -0x4c(%rbp), %eax
imull -0x50(%rbp), %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x3490
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movl -0x50(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jl 0xcc3d
movl $0x0, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jge 0xcc2d
movl $0x0, -0x64(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x21(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x34f0
movl %eax, -0x60(%rbp)
movzbl -0x21(%rbp), %eax
orl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x21(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x34f0
movl %eax, -0x60(%rbp)
movzbl -0x21(%rbp), %eax
shll $0x8, %eax
orl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x21(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x34f0
movl %eax, -0x60(%rbp)
movzbl -0x21(%rbp), %eax
shll $0x10, %eax
orl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %edx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x5c(%rbp), %ecx
imull -0x4c(%rbp), %ecx
addl -0x58(%rbp), %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0xcb86
jmp 0xcc2f
movl -0x5c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xcb75
movq -0x20(%rbp), %rdi
callq 0x3340
movl -0x50(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x50(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x4c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x10(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)