// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rsi, %rax
movq %rdi, 0x8(%rsp)
leaq 0xc7b(%rip), %rsi       # 0x915c
movq %rax, %rdi
callq 0x3330
testq %rax, %rax
je 0x86b2
movq %rax, %rbx
leaq 0x7(%rsp), %r12
pushq $0x1
popq %r15
movq %r12, %rdi
movq %r15, %rsi
movq %r15, %rdx
movq %rax, %rcx
callq 0x3400
leaq 0x40(%rsp), %rdi
pushq $0xb
popq %rdx
movq %r15, %rsi
movq %rbx, %rcx
callq 0x3400
leaq 0x1c(%rsp), %rdi
andl $0x0, (%rdi)
leaq 0x18(%rsp), %r13
andl $0x0, (%r13)
pushq $0x2
popq %rbp
movq %r15, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x3400
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x3400
leaq 0x6(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x3400
movq %r13, %rdi
movq %r15, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x3400
movzbl (%r12), %ebp
subl $0x1, %ebp
jb 0x859a
movq %r13, %rdi
movq %r15, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x3400
jmp 0x8582
movl 0x1c(%rsp), %ebp
movl 0x18(%rsp), %r14d
movl %r14d, %eax
imull %ebp, %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x33a0
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
xorl %eax, %eax
testl %ebp, %ebp
cmovgl %ebp, %eax
movq %rax, 0x28(%rsp)
movl %r14d, 0x10(%rsp)
movslq %r14d, %rax
movl %ebp, 0x14(%rsp)
movslq %ebp, %rcx
leaq -0x1(%rax), %r13
movq %rcx, 0x20(%rsp)
imulq %rcx, %r13
testq %rax, %rax
jle 0x8690
decq %rax
movq %rax, 0x38(%rsp)
movq 0x28(%rsp), %rbp
movq %r13, 0x30(%rsp)
movq %r13, %rax
subq $0x1, %rbp
jb 0x867c
leaq 0x6(%rsp), %r14
movq %r14, %rdi
pushq $0x1
popq %r13
movq %r13, %rsi
movq %r13, %rdx
movq %rbx, %rcx
movq %rax, %r12
callq 0x3400
movzbl 0x6(%rsp), %r15d
movq %r14, %rdi
movq %r13, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x3400
movzbl 0x6(%rsp), %r14d
shll $0x8, %r14d
orl %r15d, %r14d
leaq 0x6(%rsp), %rdi
movq %r13, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x3400
movzbl 0x6(%rsp), %eax
shll $0x10, %eax
orl %r14d, %eax
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movl %eax, (%rcx,%r12,4)
movq %r12, %rax
incq %rax
jmp 0x8603
movq 0x30(%rsp), %r13
subq 0x20(%rsp), %r13
movq 0x38(%rsp), %rax
jmp 0x85e5
movq %rbx, %rdi
callq 0x32b0
movq 0x8(%rsp), %rax
movl 0x10(%rsp), %ecx
movl %ecx, 0xc(%rax)
movl %ecx, 0x8(%rax)
movl 0x14(%rsp), %ecx
movl %ecx, 0x10(%rax)
xorl %eax, %eax
jmp 0x86b5
pushq $-0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq