// File: /workspace/github_c_makefile_star2_O2/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 $0x258, %rsp            # imm = 0x258
movq %rsi, %rbx
cmpl $0x2, %edi
jg 0x150c
movq (%rbx), %rsi
leaq 0xb9d(%rip), %rdi       # 0x207c
xorl %eax, %eax
callq 0x1070
leaq 0xbcf(%rip), %rdi       # 0x20bc
pushq $0x1
popq %rbx
xorl %esi, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1070
leaq 0xcc9(%rip), %rdi       # 0x21cb
callq 0x1040
jmp 0x17b5
movl %edi, %ebp
leaq 0x50(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x1080
movq 0x10(%rbx), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x10d0
movq %rax, %r14
cmpl $0x2, %r14d
jae 0x156c
cmpl $0x4, %ebp
jne 0x1550
movq 0x18(%rbx), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x10d0
testl %eax, %eax
setne 0x3(%rsp)
testb $0x1, %r14b
je 0x1582
movl $0x80, %edi
callq 0x10a0
movq %rax, 0x8(%rsp)
movl $0x80, %ebp
jmp 0x1592
leaq 0xb7e(%rip), %rdi       # 0x20f1
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
jmp 0x17b2
pushq $0x60
popq %rdi
callq 0x10a0
movq %rax, 0x8(%rsp)
pushq $0x60
popq %rbp
movq 0x8(%rbx), %rdi
leaq 0xb93(%rip), %rsi       # 0x2130
callq 0x10c0
testq %rax, %rax
je 0x17a0
movq %rax, %r14
movl %ebp, %eax
shrl $0x2, %eax
movq %rax, 0x38(%rsp)
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10b0
leaq 0x4c(%rsp), %rbx
pushq $0x4
popq %r15
pushq $0x1
popq %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x1050
movl (%rbx), %ebx
leaq 0xb48(%rip), %rdi       # 0x2133
movl %ebx, %esi
xorl %eax, %eax
callq 0x1070
movl %ebp, %eax
movq %rax, 0x28(%rsp)
leaq 0x4(%rsp), %rbp
xorl %r13d, %r13d
movq %rbx, 0x20(%rsp)
movq %r14, 0x18(%rsp)
cmpq %rbx, %r13
je 0x17c9
leal 0x4(,%r13,4), %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x10b0
leaq 0x14(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x1050
movl 0x14(%rsp), %ebx
leaq 0xb0a(%rip), %rdi       # 0x2150
movl %r13d, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1070
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x10b0
movq 0x8(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x1050
xorl %r14d, %r14d
movq %r13, 0x30(%rsp)
cmpq 0x38(%rsp), %r14
je 0x1780
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
leaq 0xad3(%rip), %rsi       # 0x2169
movl %r13d, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x10e0
andl $0x0, 0x4(%rsp)
movq %rbx, %rdi
leaq 0x98f(%rip), %rsi       # 0x2041
callq 0x10c0
testq %rax, %rax
je 0x1792
movq %r13, %rsi
movq %rax, %r13
movq 0x8(%rsp), %rax
movl (%rax,%r14,4), %ecx
xorl %ebx, %ebx
leaq 0xaa5(%rip), %rdi       # 0x217d
movl %r14d, %edx
xorl %eax, %eax
callq 0x1070
movq %r14, 0x40(%rsp)
movq 0x8(%rsp), %rax
movl (%rax,%r14,4), %esi
movq 0x18(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x10b0
cmpl $0xfffe0000, %ebx       # imm = 0xFFFE0000
je 0x172f
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x1050
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x10f0
movl 0x4(%rsp), %ebx
jmp 0x16ff
movq %r13, %rdi
callq 0x1090
cmpq $0x4, %rax
sete %al
andb 0x3(%rsp), %al
cmpb $0x1, %al
jne 0x1766
leaq 0xa62(%rip), %rdi       # 0x21af
callq 0x1040
movq %r13, %rdi
callq 0x1060
leaq 0x50(%rsp), %rdi
callq 0x1030
jmp 0x176e
movq %r13, %rdi
callq 0x1060
movq 0x40(%rsp), %r14
incq %r14
movq 0x30(%rsp), %r13
jmp 0x167c
incq %r13
movq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r14
jmp 0x160d
leaq 0x8ab(%rip), %rdi       # 0x2044
leaq 0x50(%rsp), %rsi
jmp 0x17ab
movq 0x8(%rbx), %rsi
leaq 0x899(%rip), %rdi       # 0x2044
xorl %eax, %eax
callq 0x1070
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9d9(%rip), %rdi       # 0x21a9
callq 0x1040
movq %r14, %rdi
callq 0x1060
xorl %ebx, %ebx
jmp 0x17b5