// File: /workspace/github_c_makefile_star2_O0/Joy-Division[P]tools-zoe/efx-splitter.c
// Function: main
int main( int argc, char **argv ) {
	
	if ( argc < 2 ) {
		printf("Not enough args!\nUse: %s EFX-file\n", argv[0]);
		printf("supported games are:\nMGS1\nZoE1 and MGS2HD\n");
		printf("The game will be automatically detected based on filesize!\n");
		return 1;
	}
	
	FILE *f, *o;
	char outputname[512] = {0};
	unsigned char *dumpblock = NULL;
	unsigned int numentries, sizelimit = 0x2000, size, game, blocksize, sequenceblockoffset;
	unsigned int i, j;
	efxFlags minihead;
	
	uint32_t sequenceoffset;
	uint32_t curtoken;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fseek(f, 0, SEEK_END);
	size = ftell(f);
	fseek(f, 0, SEEK_SET);
	if(size < sizelimit) {
		game = mgs1;
		numentries = 0x80;
		sequenceblockoffset = 0;
		printf("Game based on filesize is MGS1\n");
	}
	else {
		game = zoe1;
		numentries = 0x100;
		sequenceblockoffset = 0x800;
		printf("Game based on filesize is ZoE1 or MGS2HD\n");
	}
	
	if(game == zoe1) {
		printf("Dumping block 0 of efx (found in games after MGS1)\n");
		sprintf(outputname, "block0-unknown.bin");
		if( !(o = fopen( outputname, "wb" ))) {
			printf("Couldnt open file %s\n", outputname);
			return 1;
		}
		dumpblock = malloc(0x800);
		fread(dumpblock, 0x800, 1, f);
		fwrite(dumpblock, 0x800, 1, o);
		fclose(o);
		free(dumpblock);
	}
	
	printf("Dumping block 1 of efx (offset block)\n");
	sprintf(outputname, "block1-offsets.bin");
	if( !(o = fopen( outputname, "wb" ))) {
		printf("Couldnt open file %s\n", outputname);
		return 1;
	}
	dumpblock = malloc(0x800);
	fread(dumpblock, 0x800, 1, f);
	fwrite(dumpblock, 0x800, 1, o);
	fclose(o);
	free(dumpblock);
	
	printf("Dumping block 2 of efx (sequence block)\n");
	sprintf(outputname, "block2-sequences.bin");
	if( !(o = fopen( outputname, "wb" ))) {
		printf("Couldnt open file %s\n", outputname);
		return 1;
	}
	if(game == mgs1) blocksize = size-0x800;
	else if(game == zoe1) blocksize = size-0x1000;
	dumpblock = malloc(blocksize);
	fread(dumpblock, blocksize, 1, f);
	fwrite(dumpblock, blocksize, 1, o);
	fclose(o);
	free(dumpblock);
	
	
	for(i = 0; i < numentries; i++) {
		fseek(f, sequenceblockoffset + (0x10*i), SEEK_SET);
		fread(&minihead, 4, 1, f);
		printf("Minihead: %02x %02x %02x %02x\n", minihead.unknown1, minihead.numSequences, minihead.unknown2, minihead.unknown3);
		printf("Sequences in entry %02x: %d\n", i, minihead.numSequences);
		if(minihead.numSequences) {
			for(j = 0; j < minihead.numSequences; j++) {
				curtoken = 0;
				fseek(f, sequenceblockoffset + (0x10*i) + 4 + (4*j), SEEK_SET);
				fread(&sequenceoffset, 4, 1, f);
				if(sequenceoffset == 0xFFFFFFFF) {
					printf("Invalid sequence offset!\nEntry %02x - Sequence %d\n", i, j);
					return 1;
				}
				printf("Entry %02x - Sequence %d - Offset %08x\n", i, j, sequenceoffset);
				fseek(f, sequenceblockoffset+(numentries*0x10)+sequenceoffset, SEEK_SET);
				sprintf(outputname, "ent%03d-seq%d.bin", i, j);
				if( !(o = fopen( outputname, "wb" ))) {
					printf("Couldnt open file %s\n", outputname);
					return 1;
				}
				while(curtoken != 0xFFFE0000) {
					fread(&curtoken, 4, 1, f);
					fwrite(&curtoken, 4, 1, o);
				}
				fclose(o);
			}
		}
	}
	
	printf("Done.\n");
	
	fclose(f);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x260, %rsp            # imm = 0x260
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x160c
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0xa78(%rip), %rdi       # 0x2055
movb $0x0, %al
callq 0x1070
leaq 0xa8d(%rip), %rdi       # 0x2078
movb $0x0, %al
callq 0x1070
leaq 0xaaa(%rip), %rdi       # 0x20a3
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
jmp 0x1c10
leaq -0x220(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x1080
movq $0x0, -0x228(%rbp)
movl $0x2000, -0x230(%rbp)   # imm = 0x2000
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xa9c(%rip), %rsi       # 0x20df
callq 0x10c0
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x1674
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x9bc(%rip), %rdi       # 0x201d
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
jmp 0x1c10
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x10b0
movq -0x18(%rbp), %rdi
callq 0x1090
movl %eax, -0x234(%rbp)
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10b0
movl -0x234(%rbp), %eax
cmpl -0x230(%rbp), %eax
jae 0x16e0
movl $0x0, -0x238(%rbp)
movl $0x80, -0x22c(%rbp)
movl $0x0, -0x240(%rbp)
leaq 0xa0b(%rip), %rdi       # 0x20e2
movb $0x0, %al
callq 0x1070
jmp 0x170c
movl $0x1, -0x238(%rbp)
movl $0x100, -0x22c(%rbp)    # imm = 0x100
movl $0x800, -0x240(%rbp)    # imm = 0x800
leaq 0x9fd(%rip), %rdi       # 0x2102
movb $0x0, %al
callq 0x1070
cmpl $0x1, -0x238(%rbp)
jne 0x17d4
leaq 0xa0c(%rip), %rdi       # 0x212c
movb $0x0, %al
callq 0x1070
leaq -0x220(%rbp), %rdi
leaq 0xa2b(%rip), %rsi       # 0x2160
movb $0x0, %al
callq 0x10d0
leaq -0x220(%rbp), %rdi
leaq 0x8d0(%rip), %rsi       # 0x201a
callq 0x10c0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x177a
leaq -0x220(%rbp), %rsi
leaq 0x8b6(%rip), %rdi       # 0x201d
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
jmp 0x1c10
movl $0x800, %edi            # imm = 0x800
callq 0x10a0
movq %rax, -0x228(%rbp)
movq -0x228(%rbp), %rdi
movq -0x18(%rbp), %rcx
movl $0x800, %esi            # imm = 0x800
movl $0x1, %edx
callq 0x1050
movq -0x228(%rbp), %rdi
movq -0x20(%rbp), %rcx
movl $0x800, %esi            # imm = 0x800
movl $0x1, %edx
callq 0x10e0
movq -0x20(%rbp), %rdi
callq 0x1060
movq -0x228(%rbp), %rdi
callq 0x1030
leaq 0x998(%rip), %rdi       # 0x2173
movb $0x0, %al
callq 0x1070
leaq -0x220(%rbp), %rdi
leaq 0x9aa(%rip), %rsi       # 0x219a
movb $0x0, %al
callq 0x10d0
leaq -0x220(%rbp), %rdi
leaq 0x815(%rip), %rsi       # 0x201a
callq 0x10c0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x1835
leaq -0x220(%rbp), %rsi
leaq 0x7fb(%rip), %rdi       # 0x201d
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
jmp 0x1c10
movl $0x800, %edi            # imm = 0x800
callq 0x10a0
movq %rax, -0x228(%rbp)
movq -0x228(%rbp), %rdi
movq -0x18(%rbp), %rcx
movl $0x800, %esi            # imm = 0x800
movl $0x1, %edx
callq 0x1050
movq -0x228(%rbp), %rdi
movq -0x20(%rbp), %rcx
movl $0x800, %esi            # imm = 0x800
movl $0x1, %edx
callq 0x10e0
movq -0x20(%rbp), %rdi
callq 0x1060
movq -0x228(%rbp), %rdi
callq 0x1030
leaq 0x917(%rip), %rdi       # 0x21ad
movb $0x0, %al
callq 0x1070
leaq -0x220(%rbp), %rdi
leaq 0x92b(%rip), %rsi       # 0x21d6
movb $0x0, %al
callq 0x10d0
leaq -0x220(%rbp), %rdi
leaq 0x75a(%rip), %rsi       # 0x201a
callq 0x10c0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x18f0
leaq -0x220(%rbp), %rsi
leaq 0x740(%rip), %rdi       # 0x201d
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
jmp 0x1c10
cmpl $0x0, -0x238(%rbp)
jne 0x190c
movl -0x234(%rbp), %eax
subl $0x800, %eax            # imm = 0x800
movl %eax, -0x23c(%rbp)
jmp 0x1928
cmpl $0x1, -0x238(%rbp)
jne 0x1926
movl -0x234(%rbp), %eax
subl $0x1000, %eax           # imm = 0x1000
movl %eax, -0x23c(%rbp)
jmp 0x1928
movl -0x23c(%rbp), %eax
movl %eax, %edi
callq 0x10a0
movq %rax, -0x228(%rbp)
movq -0x228(%rbp), %rdi
movl -0x23c(%rbp), %eax
movl %eax, %esi
movq -0x18(%rbp), %rcx
movl $0x1, %edx
callq 0x1050
movq -0x228(%rbp), %rdi
movl -0x23c(%rbp), %eax
movl %eax, %esi
movq -0x20(%rbp), %rcx
movl $0x1, %edx
callq 0x10e0
movq -0x20(%rbp), %rdi
callq 0x1060
movq -0x228(%rbp), %rdi
callq 0x1030
movl $0x0, -0x244(%rbp)
movl -0x244(%rbp), %eax
cmpl -0x22c(%rbp), %eax
jae 0x1bf2
movq -0x18(%rbp), %rdi
movl -0x240(%rbp), %eax
movl -0x244(%rbp), %ecx
shll $0x4, %ecx
addl %ecx, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10b0
movq -0x18(%rbp), %rcx
leaq -0x24c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1050
movzbl -0x24c(%rbp), %esi
movzbl -0x24b(%rbp), %edx
movzbl -0x24a(%rbp), %ecx
movzbl -0x249(%rbp), %r8d
leaq 0x7e6(%rip), %rdi       # 0x21eb
movb $0x0, %al
callq 0x1070
movl -0x244(%rbp), %esi
movzbl -0x24b(%rbp), %edx
leaq 0x7ea(%rip), %rdi       # 0x220a
movb $0x0, %al
callq 0x1070
cmpb $0x0, -0x24b(%rbp)
je 0x1bdc
movl $0x0, -0x248(%rbp)
movl -0x248(%rbp), %eax
movzbl -0x24b(%rbp), %ecx
cmpl %ecx, %eax
jae 0x1bda
movl $0x0, -0x254(%rbp)
movq -0x18(%rbp), %rdi
movl -0x240(%rbp), %eax
movl -0x244(%rbp), %ecx
shll $0x4, %ecx
addl %ecx, %eax
addl $0x4, %eax
movl -0x248(%rbp), %ecx
shll $0x2, %ecx
addl %ecx, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10b0
movq -0x18(%rbp), %rcx
leaq -0x250(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1050
cmpl $-0x1, -0x250(%rbp)
jne 0x1ad4
movl -0x244(%rbp), %esi
movl -0x248(%rbp), %edx
leaq 0x766(%rip), %rdi       # 0x2227
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
jmp 0x1c10
movl -0x244(%rbp), %esi
movl -0x248(%rbp), %edx
movl -0x250(%rbp), %ecx
leaq 0x76d(%rip), %rdi       # 0x225a
movb $0x0, %al
callq 0x1070
movq -0x18(%rbp), %rdi
movl -0x240(%rbp), %eax
movl -0x22c(%rbp), %ecx
shll $0x4, %ecx
addl %ecx, %eax
addl -0x250(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10b0
leaq -0x220(%rbp), %rdi
movl -0x244(%rbp), %edx
movl -0x248(%rbp), %ecx
leaq 0x74e(%rip), %rsi       # 0x2282
movb $0x0, %al
callq 0x10d0
leaq -0x220(%rbp), %rdi
leaq 0x4d1(%rip), %rsi       # 0x201a
callq 0x10c0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x1b79
leaq -0x220(%rbp), %rsi
leaq 0x4b7(%rip), %rdi       # 0x201d
movb $0x0, %al
callq 0x1070
movl $0x1, -0x4(%rbp)
jmp 0x1c10
jmp 0x1b7b
cmpl $0xfffe0000, -0x254(%rbp) # imm = 0xFFFE0000
je 0x1bbd
movq -0x18(%rbp), %rcx
leaq -0x254(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1050
movq -0x20(%rbp), %rcx
leaq -0x254(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x10e0
jmp 0x1b7b
movq -0x20(%rbp), %rdi
callq 0x1060
movl -0x248(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x248(%rbp)
jmp 0x1a3e
jmp 0x1bdc
jmp 0x1bde
movl -0x244(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x244(%rbp)
jmp 0x1995
leaq 0x69b(%rip), %rdi       # 0x2294
movb $0x0, %al
callq 0x1070
movq -0x18(%rbp), %rdi
callq 0x1060
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq