// File: /workspace/github_c_makefile_star2_O1/Joy-Division[P]tools-zoe/mdx-splitter.c
// Function: main
int main( int argc, char **argv ) {
	
	if ( argc < 3 ) {
		printf("Not enough args!\nUse: %s MDX-file game [skip empty, see below]\n", argv[0]);
		printf("supported games are:\n%d (MGS1)\n%d (ZoE1 and MGS2HD)\n", mgs1, zoe1);
		printf("To skip empty sequences (which only contain the ending signature), pass 1 after the game\n");
		return 1;
	}
	
	FILE *f, *o;
	char outputname[512] = {0};
	unsigned int game, skip, subsize, numsubs;
	unsigned int curtbl, cursub;
	uint32_t numtables, tableoffset, *subtable;
	uint32_t curtoken;
	game = strtoul(argv[2], NULL, 10);
	if(!(game<NUMWVXGAMES)) {
		printf("Unsupported game %d!\nRun without args to see supported types.\n", game);
		return 1;
	}
	
	if( argc == 4 ) {
		skip = strtoul(argv[3], NULL, 10);
	}
	
	switch(game) {
		case mgs1: {
			subsize = 0x60;
			subtable = malloc(0x60);
			break;
		}
		case zoe1: {
			subsize = 0x80;
			subtable = malloc(0x80);
			break;
		}
	}
	numsubs = subsize / 4;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fseek(f, 0, SEEK_SET);
	fread(&numtables, 4, 1, f);
	printf("Number of tables in MDX: %d\n", numtables);
	
	for(curtbl = 0;curtbl < numtables;curtbl++) {
		fseek(f, 4+(4*curtbl), SEEK_SET);
		fread(&tableoffset, 4, 1, f);
		printf("Table %d - Offset: %08x\n", curtbl, tableoffset);
		fseek(f, tableoffset, SEEK_SET);
		fread(subtable, subsize, 1, f);
		for(cursub = 0;cursub < numsubs;cursub++) {
			sprintf(outputname, "tbl%05d-sub%05d.bin", curtbl, cursub);
			curtoken = 0;
			if( !(o = fopen( outputname, "wb" ))) {
				printf("Couldnt open file %s\n", outputname);
				return 1;
			}
			printf("Table %d - Subsequence %02d - Offset: %08x\n", curtbl, cursub, subtable[cursub]);
			fseek(f, subtable[cursub], SEEK_SET);
			while(curtoken != 0xFFFE0000) {
				fread(&curtoken, 4, 1, f);
				fwrite(&curtoken, 4, 1, o);
			}
			if((ftell(o) == 4) && skip) {
				printf("Empty sequence! Skipping...\n");
				fclose(o);
				remove(outputname);
			}
			else fclose(o);
		}
	}
	
	printf("Done.\n");
	
	fclose(f);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %rsi, %rbx
cmpl $0x2, %edi
jg 0x1531
movq (%rbx), %rsi
leaq 0xb7d(%rip), %rdi       # 0x207c
xorl %eax, %eax
callq 0x1070
leaq 0xbaf(%rip), %rdi       # 0x20bc
movl $0x1, %ebx
xorl %esi, %esi
movl $0x1, %edx
xorl %eax, %eax
callq 0x1070
leaq 0xca4(%rip), %rdi       # 0x21cb
callq 0x1040
jmp 0x17dd
movl %edi, %ebp
leaq 0x30(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x1080
movq 0x10(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x10d0
movq %rax, %r14
cmpl $0x2, %r14d
jae 0x1593
cmpl $0x4, %ebp
jne 0x1579
movq 0x18(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x10d0
testl %eax, %eax
setne 0x7(%rsp)
testb $0x1, %r14b
je 0x15a9
movl $0x80, %edi
callq 0x10a0
movq %rax, %r14
movl $0x80, %ebp
jmp 0x15bb
leaq 0xb57(%rip), %rdi       # 0x20f1
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
jmp 0x17d8
movl $0x60, %edi
callq 0x10a0
movq %rax, %r14
movl $0x60, %ebp
movq 0x8(%rbx), %rdi
leaq 0xb6a(%rip), %rsi       # 0x2130
callq 0x10c0
testq %rax, %rax
je 0x17c6
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10b0
leaq 0x2c(%rsp), %rbx
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x1050
movl (%rbx), %ebx
leaq 0xb2d(%rip), %rdi       # 0x2133
movl %ebx, %esi
xorl %eax, %eax
callq 0x1070
movq %rbx, 0x10(%rsp)
testq %rbx, %rbx
je 0x17a0
movl %ebp, %ecx
shrl $0x2, %ebp
movl %ebp, %eax
movq %rax, 0x20(%rsp)
leaq 0xc(%rsp), %r12
leaq 0xb1b(%rip), %r13       # 0x2150
leaq 0x8(%rsp), %rbx
xorl %ebp, %ebp
movq %rcx, 0x18(%rsp)
leal 0x4(,%rbp,4), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x10b0
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0x1050
movl 0xc(%rsp), %r12d
movq %r13, %rdi
movl %ebp, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x1070
movq %r15, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x10b0
movl $0x1, %edx
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq %r15, %rcx
callq 0x1050
xorl %r12d, %r12d
leaq 0x30(%rsp), %r13
movq %r13, %rdi
leaq 0xaba(%rip), %rsi       # 0x2169
movl %ebp, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x10e0
movl $0x0, 0x8(%rsp)
movq %r13, %rdi
leaq 0x974(%rip), %rsi       # 0x2041
callq 0x10c0
testq %rax, %rax
je 0x17b8
movq %rax, %r13
movl (%r14,%r12,4), %ecx
leaq 0xa94(%rip), %rdi       # 0x217d
movl %ebp, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x1070
movl (%r14,%r12,4), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x10b0
cmpl $0xfffe0000, 0x8(%rsp)  # imm = 0xFFFE0000
je 0x1739
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x1050
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r13, %rcx
callq 0x10f0
jmp 0x1703
movq %r13, %rdi
callq 0x1090
cmpq $0x4, %rax
sete %al
andb 0x7(%rsp), %al
cmpb $0x1, %al
jne 0x1770
leaq 0xa58(%rip), %rdi       # 0x21af
callq 0x1040
movq %r13, %rdi
callq 0x1060
leaq 0x30(%rsp), %rdi
callq 0x1030
jmp 0x1778
movq %r13, %rdi
callq 0x1060
incq %r12
cmpq 0x20(%rsp), %r12
jne 0x16a0
incq %rbp
cmpq 0x10(%rsp), %rbp
leaq 0xc(%rsp), %r12
leaq 0x9b6(%rip), %r13       # 0x2150
jne 0x1641
leaq 0xa02(%rip), %rdi       # 0x21a9
callq 0x1040
movq %r15, %rdi
callq 0x1060
xorl %ebx, %ebx
jmp 0x17dd
leaq 0x885(%rip), %rdi       # 0x2044
leaq 0x30(%rsp), %rsi
jmp 0x17d1
movq 0x8(%rbx), %rsi
leaq 0x873(%rip), %rdi       # 0x2044
xorl %eax, %eax
callq 0x1070
movl $0x1, %ebx
movl %ebx, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq