// File: /workspace/github_c_makefile_star2_O1/Joy-Division[P]tools-mgs/txp-convert.c
// Function: getFrameInfos
frameHeader *getFrameInfos(FILE *f, txpHeader header, textureHeader *textures, unsigned int game) {
	unsigned int i;
	frameHeaderPW *pwframe = NULL;
	frameHeaderPO *poframe = NULL;
	frameHeader *rethead = malloc(header.numframes * sizeof(frameHeader));
	fseek(f, header.framelist, SEEK_SET);
	
	printf("Raw frame values:\n");
	
	switch(game) {
		case mgspo: {
			printf(" index ~ unknown1 -   hash   - unknown2 - wdth - hght - unknown3 - texture  - unknown4 -    fx    -    fy    -  fwidth  - fheight  - unknown5\n");
			poframe = malloc(header.numframes * sizeof(frameHeaderPO));
			fread(poframe, header.numframes * sizeof(frameHeaderPO), 1, f);
			for(i = 0; i < header.numframes; i++) {
				printf("idx %02d ~ %08x - %08x - %08x - %04x - %04x - %08x - %08x - %08x - %f - %f - %f - %f - %08x\n", i,
					poframe[i].unknown1, poframe[i].hash, poframe[i].unknown2, poframe[i].width, poframe[i].height,
					poframe[i].unknown3, poframe[i].texture, poframe[i].unknown4,
					poframe[i].fx, poframe[i].fy, poframe[i].fwidth, poframe[i].fheight,
					poframe[i].unknown5);
				
				rethead[i].texpixels = NULL;
				rethead[i].palpixels = NULL;
				rethead[i].hash = poframe[i].hash;
				rethead[i].width = poframe[i].width;
				rethead[i].height = poframe[i].height;
				/* this is just the offset to the matching texture header */
				rethead[i].sourcetexture = (poframe[i].texture - header.texturelist) / 16;
				/* portableops doesnt store the source x and y directly, but as relative floats
				   so we need the sourcetexture first */
				rethead[i].sourcex = (unsigned int)(textures[rethead[i].sourcetexture].width * poframe[i].fx);
				rethead[i].sourcey = (unsigned int)(textures[rethead[i].sourcetexture].height * poframe[i].fy);
				
				/* portableops has one palette per texture */
				rethead[i].palette = textures[rethead[i].sourcetexture].palette;
			}
			free(poframe);
			break;
		}
		case mgspw: {
			pwframe = malloc(header.numframes * sizeof(frameHeaderPW));
			fread(pwframe, header.numframes * sizeof(frameHeaderPW), 1, f);
			for(i = 0; i < header.numframes; i++) {
				printf("idx %02d ~ %08x - %08x - %08x - %08x - %f - %f - %f - %f - %04x - %04x - %04x - %04x\n", i,
					pwframe[i].unknown1, pwframe[i].hash, pwframe[i].texture, pwframe[i].palette,
					pwframe[i].fx, pwframe[i].fy, pwframe[i].fwidth, pwframe[i].fheight,
					pwframe[i].width, pwframe[i].height, pwframe[i].x, pwframe[i].y);
			
				rethead[i].texpixels = NULL;
				rethead[i].palpixels = NULL;
				rethead[i].hash = pwframe[i].hash;
				rethead[i].width = pwframe[i].width;
				rethead[i].height = pwframe[i].height;
				/* this is just the offset to the matching texture header */
				rethead[i].sourcetexture = (pwframe[i].texture - header.texturelist) / 20;
				/* peacewalker has an absolute source x and y stored as well as relative ones */
				rethead[i].sourcex = pwframe[i].x;
				rethead[i].sourcey = pwframe[i].y;
				
				/* peacewalker has one palette per frame */
				rethead[i].palette = pwframe[i].palette;
			}
			free(pwframe);
			break;
		}
		default: {
			printf("TRIPLE FAULT YAY\n");
			break;
		}
	}
	
	printf("Processed frame values:\n");
	printf(" index ~   hash   - wdth - hgth - tx - srcx - srcy - palette\n");
	for(i = 0; i < header.numframes; i++) printf("idx %02d ~ %08x - %04d - %04d - %02d - %04d - %04d - %08x\n", i,
				rethead[i].hash, rethead[i].width, rethead[i].height, rethead[i].sourcetexture,
				rethead[i].sourcex, rethead[i].sourcey, rethead[i].palette);
	
	return rethead;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, %ebp
movq %rsi, %r15
movq %rdi, %r12
leaq 0x70(%rsp), %rbx
movl 0xc(%rbx), %eax
movq %rax, 0x8(%rsp)
shlq $0x4, %rax
leaq (%rax,%rax,2), %r13
movq %r13, %rdi
callq 0x10f0
movq %rax, %r14
movl 0x10(%rbx), %esi
movq %r12, %rdi
xorl %edx, %edx
callq 0x1110
leaq 0x303a(%rip), %rdi      # 0xfea0
callq 0x1050
cmpl $0x1, %ebp
movq %r14, 0x18(%rsp)
je 0xcfec
testl %ebp, %ebp
jne 0xd12b
leaq 0x302a(%rip), %rdi      # 0xfeb2
callq 0x1050
movq %r13, %rdi
callq 0x10f0
movq %r15, 0x20(%rsp)
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
movq %r12, %rcx
callq 0x1060
movq 0x8(%rsp), %r13
testq %r13, %r13
je 0xcfe4
movl 0x8(%rbx), %eax
movl %eax, 0x10(%rsp)
movl $0x20, %r14d
xorl %r12d, %r12d
movl -0x20(%r15,%r14), %edx
movl -0x1c(%r15,%r14), %ecx
movl %ecx, (%rsp)
movl -0x18(%r15,%r14), %r8d
movzwl -0x14(%r15,%r14), %r13d
movzwl -0x12(%r15,%r14), %ebp
movl -0x10(%r15,%r14), %eax
movq %rax, 0x28(%rsp)
movl -0xc(%r15,%r14), %ebx
movl -0x8(%r15,%r14), %r11d
movss -0x4(%r15,%r14), %xmm0
movss %xmm0, 0x30(%rsp)
cvtss2sd %xmm0, %xmm0
movss (%r15,%r14), %xmm1
movss %xmm1, 0x4(%rsp)
cvtss2sd 0x4(%r15,%r14), %xmm2
cvtss2sd %xmm1, %xmm1
cvtss2sd 0x8(%r15,%r14), %xmm3
movl 0xc(%r15,%r14), %r10d
subq $0x8, %rsp
leaq 0x2bca(%rip), %rdi      # 0xfb09
movl %r12d, %esi
movl %r13d, %r9d
movb $0x4, %al
pushq %r10
pushq %r11
pushq %rbx
pushq 0x48(%rsp)
pushq %rbp
callq 0x10a0
movq 0x48(%rsp), %rsi
addq $0x30, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%rsi,%r14)
movl (%rsp), %eax
movl %eax, -0x20(%rsi,%r14)
movl %r13d, -0x1c(%rsi,%r14)
movq 0x8(%rsp), %r13
movl %ebp, -0x18(%rsi,%r14)
subl 0x10(%rsp), %ebx
shrl $0x4, %ebx
movl %ebx, -0xc(%rsi,%r14)
leaq (%rbx,%rbx,4), %rax
movq 0x20(%rsp), %rdx
movl 0x8(%rdx,%rax,8), %ecx
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
mulss 0x30(%rsp), %xmm0
cvttss2si %xmm0, %rcx
movl %ecx, -0x14(%rsi,%r14)
movl 0xc(%rdx,%rax,8), %ecx
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
mulss 0x4(%rsp), %xmm0
cvttss2si %xmm0, %rcx
movl %ecx, -0x10(%rsi,%r14)
movl 0x14(%rdx,%rax,8), %eax
movl %eax, -0x8(%rsi,%r14)
incq %r12
addq $0x30, %r14
cmpq %r12, %r13
jne 0xcece
movq %r15, %rdi
jmp 0xd124
movq 0x8(%rsp), %r13
leaq (,%r13,8), %rax
leaq (%rax,%rax,4), %r14
movq %r14, %rdi
callq 0x10f0
movl $0x1, %edx
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq %r14, %rsi
movq %r12, %rcx
callq 0x1060
testq %r13, %r13
je 0xd11f
movl 0x8(%rbx), %eax
movl %eax, 0x20(%rsp)
movq 0x10(%rsp), %r12
addq $0x26, %r12
movq 0x18(%rsp), %rax
leaq 0x20(%rax), %rbx
xorl %r15d, %r15d
movl -0x26(%r12), %edx
movl -0x22(%r12), %ecx
movl %ecx, (%rsp)
movl -0x1e(%r12), %r13d
movl -0x1a(%r12), %r9d
movl %r9d, 0x4(%rsp)
xorps %xmm0, %xmm0
cvtss2sd -0x16(%r12), %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x12(%r12), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0xe(%r12), %xmm2
xorps %xmm3, %xmm3
cvtss2sd -0xa(%r12), %xmm3
movzwl -0x6(%r12), %r14d
movzwl -0x4(%r12), %ebp
movzwl -0x2(%r12), %r11d
movq %r11, 0x28(%rsp)
movzwl (%r12), %r10d
movq %r10, 0x30(%rsp)
leaq 0x2ab8(%rip), %rdi      # 0xfb66
movl %r15d, %esi
movl %r13d, %r8d
movb $0x4, %al
pushq %r10
pushq %r11
pushq %rbp
pushq %r14
callq 0x10a0
addq $0x20, %rsp
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movl (%rsp), %eax
movl %eax, -0x20(%rbx)
movl %r14d, -0x1c(%rbx)
movl %ebp, -0x18(%rbx)
subl 0x20(%rsp), %r13d
movl $0xcccccccd, %eax       # imm = 0xCCCCCCCD
imulq %rax, %r13
shrq $0x24, %r13
movl %r13d, -0xc(%rbx)
movq 0x8(%rsp), %r13
movq 0x28(%rsp), %rax
movl %eax, -0x14(%rbx)
movq 0x30(%rsp), %rax
movl %eax, -0x10(%rbx)
movl 0x4(%rsp), %eax
movl %eax, -0x8(%rbx)
incq %r15
addq $0x28, %r12
addq $0x30, %rbx
cmpq %r15, %r13
jne 0xd042
movq 0x10(%rsp), %rdi
callq 0x1030
jmp 0xd13c
leaq 0x2e0e(%rip), %rdi      # 0xff40
callq 0x1050
movq 0x8(%rsp), %r13
leaq 0x2e0e(%rip), %rdi      # 0xff51
callq 0x1050
leaq 0x2e1a(%rip), %rdi      # 0xff69
callq 0x1050
testq %r13, %r13
je 0xd1ac
movq 0x18(%rsp), %rbx
addq $0x18, %rbx
leaq 0x2a53(%rip), %r14      # 0xfbbc
xorl %r15d, %r15d
movl -0x18(%rbx), %edx
movl -0x14(%rbx), %ecx
movl -0x10(%rbx), %r8d
movl -0x4(%rbx), %r9d
movl -0xc(%rbx), %r10d
movl -0x8(%rbx), %r11d
movl (%rbx), %r12d
subq $0x8, %rsp
movq %r14, %rdi
movl %r15d, %esi
xorl %eax, %eax
pushq %r12
pushq %r11
pushq %r10
callq 0x10a0
addq $0x20, %rsp
incq %r15
addq $0x30, %rbx
cmpq %r15, %r13
jne 0xd16c
movq 0x18(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq