// File: /workspace/github_c_makefile_star2_O0/Joy-Division[P]tools-mgs/txp-convert.c
// Function: main
int main( int argc, char **argv ) {
	
	unsigned char headerdata[32], *readyframe = NULL;
	uint32_t *gamecheck = NULL;
	
	unsigned int i, game;
	
	txpHeader *header = NULL;
	textureHeader *textures = NULL;
	frameHeader *frames = NULL;
	
	char *outputname = NULL;
	
	FILE *f;
	
	if( argc < 2 ) {
		printf("Not enough args!\nUse: %s TXP-file\n", argv[0]);
		return 1;
	}
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	
	outputname = malloc( strlen( argv[1] ) + 7 + 9 + 4 + 1 ); // will contain: infilename + "-tex###" + "-frame###" + ".png" + NULL
	
	fread( headerdata, 32, 1, f );
	gamecheck = (uint32_t *)(headerdata+4);
	
	game = (*gamecheck < 256) ? mgspo : mgspw;
	printf("TXP is %s compatible\n", (*gamecheck < 256) ? "portableops" : "peacewalker");
	
	header = getTxpHeader(headerdata, game);
	textures = getTextureInfos(f, *header, game);
	frames = getFrameInfos(f, *header, textures, game);
	
	getTexturePixels(f, *header, textures);
	getFramePixels(f, *header, textures, frames);
	
	for(i = 0; i < header->numframes; i++) {
	//~ for(i = 0; i < 1; i++) {
		printf("Processing frame %02d of %02d (texture %02d)...", i, header->numframes-1, frames[i].sourcetexture);
		fflush(stdout);
		readyframe = buildSingleFrameRgba(textures, frames[i]);
		printf("built ");
		fflush(stdout);
		sprintf(outputname, "%s-tex%03d-frame%03d.png", argv[1], frames[i].sourcetexture, i);
		lodepng_encode32_file(outputname, readyframe, frames[i].width, frames[i].height);
		printf("encoded ");
		fflush(stdout);
		free(readyframe);
		printf("managed\n");
		fflush(stdout);
	}
	
	fclose(f);
	
	for(i = 0; i < header->numtextures; i++) {
		free(textures[i].texpixels);
		if(textures[i].palpixels) free(textures[i].palpixels);
	}
	if(game == mgspw) for(i = 0; i < header->numframes; i++) free(frames[i].palpixels);
	
	free(header);
	free(textures);
	free(frames);
	free(outputname);
	
	printf("Done.\n");
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x131b0
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x28c3(%rip), %rdi      # 0x15a60
movb $0x0, %al
callq 0x1080
movl $0x1, -0x4(%rbp)
jmp 0x13601
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2261(%rip), %rsi      # 0x15420
callq 0x1110
movq %rax, -0x70(%rbp)
cmpq $0x0, %rax
jne 0x131f0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x28a6(%rip), %rdi      # 0x15a83
movb $0x0, %al
callq 0x1080
movl $0x1, -0x4(%rbp)
jmp 0x13601
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1070
movq %rax, %rdi
addq $0x7, %rdi
addq $0x9, %rdi
addq $0x4, %rdi
addq $0x1, %rdi
callq 0x10d0
movq %rax, -0x68(%rbp)
leaq -0x30(%rbp), %rdi
movq -0x70(%rbp), %rcx
movl $0x20, %esi
movl $0x1, %edx
callq 0x1040
leaq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl (%rax), %edx
movl $0x1, %eax
xorl %ecx, %ecx
cmpl $0x100, %edx            # imm = 0x100
cmovbl %ecx, %eax
movl %eax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movl (%rax), %ecx
leaq 0x2859(%rip), %rsi      # 0x15abb
leaq 0x2846(%rip), %rax      # 0x15aaf
cmpl $0x100, %ecx            # imm = 0x100
cmovbq %rax, %rsi
leaq 0x281f(%rip), %rdi      # 0x15a99
movb $0x0, %al
callq 0x1080
leaq -0x30(%rbp), %rdi
movl -0x48(%rbp), %esi
callq 0x117c0
movq %rax, -0x50(%rbp)
movq -0x70(%rbp), %rdi
movq -0x50(%rbp), %rax
movl -0x48(%rbp), %esi
movq (%rax), %rcx
movq %rcx, -0x88(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x80(%rbp)
movl 0x10(%rax), %eax
movl %eax, -0x78(%rbp)
leaq -0x88(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movl 0x10(%rax), %eax
movl %eax, 0x10(%rsp)
callq 0x119d0
movq %rax, -0x58(%rbp)
movq -0x70(%rbp), %rdi
movq -0x50(%rbp), %rax
movq -0x58(%rbp), %rsi
movl -0x48(%rbp), %edx
movq (%rax), %rcx
movq %rcx, -0xa0(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x98(%rbp)
movl 0x10(%rax), %eax
movl %eax, -0x90(%rbp)
leaq -0xa0(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movl 0x10(%rax), %eax
movl %eax, 0x10(%rsp)
callq 0x120e0
movq %rax, -0x60(%rbp)
movq -0x70(%rbp), %rdi
movq -0x50(%rbp), %rax
movq -0x58(%rbp), %rsi
movq (%rax), %rcx
movq %rcx, -0xb8(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0xb0(%rbp)
movl 0x10(%rax), %eax
movl %eax, -0xa8(%rbp)
leaq -0xb8(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movl 0x10(%rax), %eax
movl %eax, 0x10(%rsp)
callq 0x12a00
movq -0x70(%rbp), %rdi
movq -0x50(%rbp), %rax
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq (%rax), %rcx
movq %rcx, -0xd0(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0xc8(%rbp)
movl 0x10(%rax), %eax
movl %eax, -0xc0(%rbp)
leaq -0xd0(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movl 0x10(%rax), %eax
movl %eax, 0x10(%rsp)
callq 0x12dd0
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x1351f
movl -0x44(%rbp), %esi
movq -0x50(%rbp), %rax
movl 0xc(%rax), %edx
subl $0x1, %edx
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %ecx
leaq 0x26be(%rip), %rdi      # 0x15ac7
movb $0x0, %al
callq 0x1080
movq 0x5bc9(%rip), %rax      # 0x18fe0
movq (%rax), %rdi
callq 0x10e0
movq -0x58(%rbp), %rdi
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movq %rsp, %rax
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x12ff0
movq %rax, -0x38(%rbp)
leaq 0x269d(%rip), %rdi      # 0x15af7
movb $0x0, %al
callq 0x1080
movq 0x5b78(%rip), %rax      # 0x18fe0
movq (%rax), %rdi
callq 0x10e0
movq -0x68(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %ecx
movl -0x44(%rbp), %r8d
leaq 0x2666(%rip), %rsi      # 0x15afe
movb $0x0, %al
callq 0x1120
movq -0x68(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edx
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
callq 0x9ee0
leaq 0x2642(%rip), %rdi      # 0x15b17
movb $0x0, %al
callq 0x1080
movq 0x5afd(%rip), %rax      # 0x18fe0
movq (%rax), %rdi
callq 0x10e0
movq -0x38(%rbp), %rdi
callq 0x1030
leaq 0x2625(%rip), %rdi      # 0x15b20
movb $0x0, %al
callq 0x1080
movq 0x5ad7(%rip), %rax      # 0x18fe0
movq (%rax), %rdi
callq 0x10e0
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x133d4
movq -0x70(%rbp), %rdi
callq 0x1050
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x1358b
movq -0x58(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rdi
callq 0x1030
movq -0x58(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x20(%rax)
je 0x1357e
movq -0x58(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rdi
callq 0x1030
jmp 0x13580
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1352f
cmpl $0x1, -0x48(%rbp)
jne 0x135c8
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x135c6
movq -0x60(%rbp), %rax
movl -0x44(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq 0x28(%rax), %rdi
callq 0x1030
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x13598
jmp 0x135c8
movq -0x50(%rbp), %rdi
callq 0x1030
movq -0x58(%rbp), %rdi
callq 0x1030
movq -0x60(%rbp), %rdi
callq 0x1030
movq -0x68(%rbp), %rdi
callq 0x1030
leaq 0x2536(%rip), %rdi      # 0x15b29
movb $0x0, %al
callq 0x1080
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq