// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
jge 0x1623
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0xa68(%rip), %rdi       # 0x2055
movb $0x0, %al
callq 0x1070
leaq 0xa9a(%rip), %rdi       # 0x2095
xorl %esi, %esi
movl $0x1, %edx
movb $0x0, %al
callq 0x1070
leaq 0xaba(%rip), %rdi       # 0x20ca
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
jmp 0x19b9
leaq -0x220(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x1080
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x10d0
movl %eax, -0x224(%rbp)
cmpl $0x2, -0x224(%rbp)
jb 0x167b
movl -0x224(%rbp), %esi
leaq 0xabc(%rip), %rdi       # 0x2124
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
jmp 0x19b9
cmpl $0x4, -0x8(%rbp)
jne 0x169d
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x10d0
movl %eax, -0x228(%rbp)
movl -0x224(%rbp), %eax
movl %eax, -0x250(%rbp)
testl %eax, %eax
je 0x16bc
jmp 0x16af
movl -0x250(%rbp), %eax
subl $0x1, %eax
je 0x16d9
jmp 0x16f4
movl $0x60, -0x22c(%rbp)
movl $0x60, %edi
callq 0x10a0
movq %rax, -0x248(%rbp)
jmp 0x16f4
movl $0x80, -0x22c(%rbp)
movl $0x80, %edi
callq 0x10a0
movq %rax, -0x248(%rbp)
movl -0x22c(%rbp), %eax
shrl $0x2, %eax
movl %eax, -0x230(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xa51(%rip), %rsi       # 0x2163
callq 0x10c0
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x1743
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x8ed(%rip), %rdi       # 0x201d
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
jmp 0x19b9
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10b0
movq -0x18(%rbp), %rcx
leaq -0x23c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1050
movl -0x23c(%rbp), %esi
leaq 0x9ed(%rip), %rdi       # 0x2166
movb $0x0, %al
callq 0x1070
movl $0x0, -0x234(%rbp)
movl -0x234(%rbp), %eax
cmpl -0x23c(%rbp), %eax
jae 0x199b
movq -0x18(%rbp), %rdi
movl -0x234(%rbp), %eax
shll $0x2, %eax
addl $0x4, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10b0
movq -0x18(%rbp), %rcx
leaq -0x240(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1050
movl -0x234(%rbp), %esi
movl -0x240(%rbp), %edx
leaq 0x99f(%rip), %rdi       # 0x2183
movb $0x0, %al
callq 0x1070
movq -0x18(%rbp), %rdi
movl -0x240(%rbp), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10b0
movq -0x248(%rbp), %rdi
movl -0x22c(%rbp), %eax
movl %eax, %esi
movq -0x18(%rbp), %rcx
movl $0x1, %edx
callq 0x1050
movl $0x0, -0x238(%rbp)
movl -0x238(%rbp), %eax
cmpl -0x230(%rbp), %eax
jae 0x1985
leaq -0x220(%rbp), %rdi
movl -0x234(%rbp), %edx
movl -0x238(%rbp), %ecx
leaq 0x94b(%rip), %rsi       # 0x219c
movb $0x0, %al
callq 0x10e0
movl $0x0, -0x24c(%rbp)
leaq -0x220(%rbp), %rdi
leaq 0x7aa(%rip), %rsi       # 0x201a
callq 0x10c0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x18a0
leaq -0x220(%rbp), %rsi
leaq 0x790(%rip), %rdi       # 0x201d
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
jmp 0x19b9
movl -0x234(%rbp), %esi
movl -0x238(%rbp), %edx
movq -0x248(%rbp), %rax
movl -0x238(%rbp), %ecx
movl (%rax,%rcx,4), %ecx
leaq 0x8ed(%rip), %rdi       # 0x21b0
movb $0x0, %al
callq 0x1070
movq -0x18(%rbp), %rdi
movq -0x248(%rbp), %rax
movl -0x238(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10b0
cmpl $0xfffe0000, -0x24c(%rbp) # imm = 0xFFFE0000
je 0x1929
movq -0x18(%rbp), %rcx
leaq -0x24c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1050
movq -0x20(%rbp), %rcx
leaq -0x24c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x10f0
jmp 0x18e7
movq -0x20(%rbp), %rdi
callq 0x1090
cmpq $0x4, %rax
jne 0x1966
cmpl $0x0, -0x228(%rbp)
je 0x1966
leaq 0x894(%rip), %rdi       # 0x21dc
movb $0x0, %al
callq 0x1070
movq -0x20(%rbp), %rdi
callq 0x1060
leaq -0x220(%rbp), %rdi
callq 0x1040
jmp 0x196f
movq -0x20(%rbp), %rdi
callq 0x1060
jmp 0x1971
movl -0x238(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x238(%rbp)
jmp 0x1825
jmp 0x1987
movl -0x234(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x234(%rbp)
jmp 0x178a
leaq 0x857(%rip), %rdi       # 0x21f9
movb $0x0, %al
callq 0x1070
movq -0x18(%rbp), %rdi
callq 0x1060
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq