// File: /workspace/github_c_makefile_star2_O1/Joy-Division[P]tools-zoe/mdx-parser.c
// Function: main
int main( int argc, char **argv ) {
	
	if ( argc < 3 ) {
		printf("Not enough args!\nUse: %s MDX-file game\n", argv[0]);
		printf("supported games are:\n%d (MGS1)\n%d (ZoE1 and MGS2HD)\n", mgs1, zoe1);
		return 1;
	}
	
	
	FILE *f, *o;
	char outputname[512] = {0};
	unsigned int game, tracklistsize, numtracks;
	unsigned int cursong, curtrack;
	unsigned int event, param0, param1, param2, note, velocity, length, snap;
	uint32_t numsongs, songoffset, *tracklist;
	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;
	}
	
	
	switch(game) {
		case mgs1: {
			tracklistsize = 0x60;
			tracklist = malloc(0x60);
			break;
		}
		case zoe1: {
			tracklistsize = 0x80;
			tracklist = malloc(0x80);
			break;
		}
	}
	numtracks = tracklistsize / 4;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	fseek(f, 0, SEEK_SET);
	fread(&numsongs, 4, 1, f);
	printf("Number of songs in MDX: %d\n", numsongs);
	
	for(cursong = 0;cursong < numsongs;cursong++) {
		fseek(f, 4+(4*cursong), SEEK_SET);
		fread(&songoffset, 4, 1, f);
		printf("Song %d - Offset: %08x\n", cursong, songoffset);
		fseek(f, songoffset, SEEK_SET);
		fread(tracklist, tracklistsize, 1, f);
		for(curtrack = 0;curtrack < numtracks;curtrack++) {
			sprintf(outputname, "song%05d-track%05d.bin", cursong, curtrack);
			curtoken = 0;
			if( !(o = fopen( outputname, "wb" ))) {
				printf("Couldnt open file %s\n", outputname);
				return 1;
			}
			printf("Song %d - Track %02d - Offset: %08x\n", cursong, curtrack, tracklist[curtrack]);
			fseek(f, tracklist[curtrack], SEEK_SET);
			
			/* smf stuffs */
			//~ track = smf_track_new();
			
			while(curtoken != 0xFFFE0000) {
				printf("\t");
				fread(&curtoken, 4, 1, f);
				
				event = curtoken >> 24;
				param0 = (curtoken >> 16) & 0xFF;
				param1 = (curtoken >> 8) & 0xFF;
				param2 = curtoken & 0xFF;
				
				if(event < 0x80 ) {
					/* possible midi note */
					note = event;
					snap = param0;
					length = param1;
					velocity = param2;
					printf("Encountered note %x(%s)\n", note, midinotes[note]);
					printf("\tsnap %x - length %x - velocity %x\n", snap, length, velocity);
				}
				else {
					switch(event) {
						case 0xD0: {
							printf("Encountered Tempo event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD1: {
							printf("Encountered Decrease event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD2: {
							printf("Encountered Instrument A event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD3: {
							printf("Encountered Instrument B event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD4: {
							printf("Encountered Instrument C event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD5: {
							printf("Encountered Change Master Volume event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD6: {
							printf("Encountered Fade out event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD7: {
							printf("Encountered Change Channel Volume event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xD8: {
							printf("Encountered Fade out note event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xDD: {
							printf("Encountered Pan event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						/*
						case 0xDE: {
							printf("Encountered Pan1 event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						*/
						
						case 0xDF: {
							printf("Encountered Pitch event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xE0: {
							printf("Encountered Pitch note when changed event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xE3: {
							printf("Encountered Modulation event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xE5: {
							printf("Encountered Swirl up note event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xE6: {
							printf("Encountered Pitch bend on change event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xE7: {
							printf("Encountered Set Loop Start (MEMORY A) event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						case 0xE8: {
							printf("Encountered Set Loop End (MEMORY A) (repeat PARAM0 times) event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xE9: {
							printf("Encountered Set Loop Start (MEMORY B) event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						case 0xEA: {
							printf("Encountered Set Loop End (MEMORY B) (repeat PARAM0 times) event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xEB: {
							printf("Encountered Set Loop Start event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						case 0xEC: {
							printf("Encountered Set Loop End event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						case 0xED: {
							printf("Encountered Set Loop Start (0xED) event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						case 0xEE: {
							printf("Encountered Set Loop End (0xEE) (two per 0xED) event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						case 0xF2: {
							printf("Encountered Silence A event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xF3: {
							printf("Encountered Silence B event at %08x, parameters %02x %02x %02x\n", (unsigned int)ftell(f)-4, param0, param1, param2);
							break;
						}
						
						case 0xFF: {
							printf("Encountered End Of Track event at %08x, complete event %08x\n", (unsigned int)ftell(f)-4, curtoken);
							break;
						}
						
						default: {
							printf("Unknown event %x at %08x\n", curtoken, (unsigned int)ftell(f)-4);
							break;
						}
					}
				}
				fwrite(&curtoken, 4, 1, o);
			}
			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 0x25de
movq (%rbx), %rsi
leaq 0xd54(%rip), %rdi       # 0x330c
xorl %eax, %eax
callq 0x2070
leaq 0xd6e(%rip), %rdi       # 0x3334
movl $0x1, %ebx
xorl %esi, %esi
movl $0x1, %edx
xorl %eax, %eax
callq 0x2070
jmp 0x2a43
leaq 0x50(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x2080
movq 0x10(%rbx), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x20d0
cmpl $0x2, %eax
jae 0x261e
testb $0x1, %al
je 0x2633
movl $0x80, %edi
callq 0x20a0
movq %rax, 0x10(%rsp)
movl $0x80, %ebp
jmp 0x2647
leaq 0xd44(%rip), %rdi       # 0x3369
movl %eax, %esi
xorl %eax, %eax
callq 0x2070
jmp 0x2a3e
movl $0x60, %edi
callq 0x20a0
movq %rax, 0x10(%rsp)
movl $0x60, %ebp
movq 0x8(%rbx), %rdi
leaq 0xd56(%rip), %rsi       # 0x33a8
callq 0x20c0
testq %rax, %rax
je 0x2a2c
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x20b0
leaq 0x1c(%rsp), %rbx
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rcx
callq 0x2050
movl (%rbx), %ebx
leaq 0xd19(%rip), %rdi       # 0x33ab
movl %ebx, %esi
xorl %eax, %eax
callq 0x2070
testl %ebx, %ebx
je 0x2a06
movl %ebp, %r15d
shrl $0x2, %ebp
movl %ebp, %eax
movq %rax, 0x28(%rsp)
leaq 0xc(%rsp), %rbx
leaq 0xd0b(%rip), %r12       # 0x33c7
leaq 0x8(%rsp), %r13
xorl %ebp, %ebp
movq %r14, 0x48(%rsp)
movq %r15, 0x30(%rsp)
leal 0x4(,%rbp,4), %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x20b0
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rcx
callq 0x2050
movl 0xc(%rsp), %ebx
movq %r12, %rdi
movl %ebp, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2070
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x20b0
movl $0x1, %edx
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rcx
callq 0x2050
xorl %r12d, %r12d
movq %rbp, 0x38(%rsp)
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
leaq 0xca1(%rip), %rsi       # 0x33df
movl %ebp, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x20e0
movl $0x0, 0x8(%rsp)
movq %rbx, %rdi
leaq 0x8e5(%rip), %rsi       # 0x3041
callq 0x20c0
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x2a1e
movq 0x10(%rsp), %rbx
movl (%rbx,%r12,4), %ecx
leaq 0xc77(%rip), %rdi       # 0x33f6
movl %ebp, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x2070
movq %r12, 0x40(%rsp)
movl (%rbx,%r12,4), %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x20b0
cmpl $0xfffe0000, 0x8(%rsp)  # imm = 0xFFFE0000
je 0x29c3
movl $0x9, %edi
callq 0x2030
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rbp
movq %r13, %rdi
movq %r14, %rcx
callq 0x2050
movq %r14, %rdi
movl 0x8(%rsp), %r14d
movl %r14d, %r15d
shrl $0x18, %r15d
movl %r14d, %r13d
shrl $0x10, %r13d
movl $0xff, %eax
andl %eax, %r13d
movl %r14d, %ebx
shrl $0x8, %ebx
andl %eax, %ebx
movl %r14d, %r12d
andl %eax, %r12d
testl %r14d, %r14d
js 0x2836
leaq 0x288a(%rip), %rax      # 0x5090
movq (%rax,%r15,8), %rdx
leaq 0xc0a(%rip), %rdi       # 0x341b
movl %r15d, %esi
xorl %eax, %eax
callq 0x2070
leaq 0xc12(%rip), %rdi       # 0x3434
movl %r13d, %esi
movl %ebx, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x2070
jmp 0x2996
callq 0x2090
addl $-0x4, %eax
addl $0xffffff30, %r15d      # imm = 0xFFFFFF30
cmpl $0x2f, %r15d
ja 0x2949
leaq 0x826(%rip), %rdx       # 0x307c
movslq (%rdx,%r15,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
leaq 0xbf2(%rip), %rdi       # 0x3458
jmp 0x2970
leaq 0x10ca(%rip), %rdi      # 0x393c
jmp 0x2970
leaq 0x119e(%rip), %rdi      # 0x3a1c
jmp 0x298a
leaq 0xf2b(%rip), %rdi       # 0x37b5
jmp 0x2970
leaq 0x129f(%rip), %rdi      # 0x3b35
jmp 0x298a
leaq 0x1213(%rip), %rdi      # 0x3ab5
jmp 0x2970
leaq 0xe3c(%rip), %rdi       # 0x36ea
jmp 0x2970
leaq 0xc19(%rip), %rdi       # 0x34d3
jmp 0x2970
leaq 0xc93(%rip), %rdi       # 0x3559
jmp 0x2970
leaq 0x1190(%rip), %rdi      # 0x3a62
jmp 0x298a
leaq 0xe96(%rip), %rdi       # 0x3774
jmp 0x2970
leaq 0x10b6(%rip), %rdi      # 0x39a0
jmp 0x298a
leaq 0xca6(%rip), %rdi       # 0x359c
jmp 0x2970
leaq 0xe27(%rip), %rdi       # 0x3726
jmp 0x2970
leaq 0xc0e(%rip), %rdi       # 0x3516
jmp 0x2970
leaq 0xb83(%rip), %rdi       # 0x3494
jmp 0x2970
leaq 0x11db(%rip), %rdi      # 0x3af5
jmp 0x2970
leaq 0xcc4(%rip), %rdi       # 0x35e7
jmp 0x2970
leaq 0xcfa(%rip), %rdi       # 0x3626
jmp 0x2970
leaq 0xec4(%rip), %rdi       # 0x37f9
jmp 0x2970
leaq 0xf06(%rip), %rdi       # 0x3844
jmp 0x298a
leaq 0xd6f(%rip), %rdi       # 0x36b6
jmp 0x298a
leaq 0x1222(%rip), %rdi      # 0x3b72
movl %r14d, %esi
movl %eax, %edx
jmp 0x298f
leaq 0xd14(%rip), %rdi       # 0x3672
jmp 0x2970
leaq 0xf8b(%rip), %rdi       # 0x38f2
jmp 0x298a
leaq 0xf1e(%rip), %rdi       # 0x388e
movl %eax, %esi
movl %r13d, %edx
movl %ebx, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x2070
jmp 0x2996
leaq 0x1055(%rip), %rdi      # 0x39df
movl %eax, %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2070
movl $0x4, %esi
movl $0x1, %edx
movq %rbp, %r13
movq %rbp, %rdi
movq 0x20(%rsp), %rcx
callq 0x20f0
cmpl $0xfffe0000, 0x8(%rsp)  # imm = 0xFFFE0000
movq 0x48(%rsp), %r14
jne 0x27ac
movq 0x20(%rsp), %rdi
callq 0x2060
movq 0x40(%rsp), %r12
incq %r12
cmpq 0x28(%rsp), %r12
movq 0x30(%rsp), %r15
movq 0x38(%rsp), %rbp
jne 0x272f
incq %rbp
movl 0x1c(%rsp), %eax
cmpq %rax, %rbp
leaq 0xc(%rsp), %rbx
leaq 0x9c7(%rip), %r12       # 0x33c7
jb 0x26cd
leaq 0x117f(%rip), %rdi      # 0x3b8c
callq 0x2040
movq %r14, %rdi
callq 0x2060
xorl %ebx, %ebx
jmp 0x2a43
leaq 0x61f(%rip), %rdi       # 0x3044
leaq 0x50(%rsp), %rsi
jmp 0x2a37
movq 0x8(%rbx), %rsi
leaq 0x60d(%rip), %rdi       # 0x3044
xorl %eax, %eax
callq 0x2070
movl $0x1, %ebx
movl %ebx, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq