// File: /workspace/github_c_makefile_star2_O3/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 0x2a3b
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, 0x8(%rsp)
movl $0x80, %ebp
jmp 0x2647
leaq 0xd44(%rip), %rdi       # 0x3369
movl %eax, %esi
xorl %eax, %eax
callq 0x2070
jmp 0x2a36
movl $0x60, %edi
callq 0x20a0
movq %rax, 0x8(%rsp)
movl $0x60, %ebp
movq 0x8(%rbx), %rdi
leaq 0xd56(%rip), %rsi       # 0x33a8
callq 0x20c0
testq %rax, %rax
je 0x2a24
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x20b0
leaq 0x4c(%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
movq %rbx, 0x18(%rsp)
testq %rbx, %rbx
je 0x29fe
movl %ebp, %r15d
shrl $0x2, %ebp
movl %ebp, %eax
movq %rax, 0x20(%rsp)
leaq 0x4(%rsp), %rbx
leaq 0xd05(%rip), %rax       # 0x33c7
movq %rsp, %rbp
xorl %r12d, %r12d
movq %r14, 0x40(%rsp)
movq %r15, 0x28(%rsp)
leal 0x4(,%r12,4), %esi
movq %rax, %r13
movq %r14, %rdi
xorl %edx, %edx
callq 0x20b0
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rcx
callq 0x2050
movl 0x4(%rsp), %ebx
movq %r13, %rdi
movl %r12d, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2070
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x20b0
movl $0x1, %edx
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rcx
callq 0x2050
xorl %r15d, %r15d
movq %r12, 0x30(%rsp)
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
leaq 0xc97(%rip), %rsi       # 0x33df
movl %r12d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x20e0
movl $0x0, (%rsp)
movq %rbx, %rdi
leaq 0x8db(%rip), %rsi       # 0x3041
callq 0x20c0
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x2a16
movq 0x8(%rsp), %rbx
movl (%rbx,%r15,4), %ecx
leaq 0xc6d(%rip), %rdi       # 0x33f6
movl %r12d, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x2070
movq %r15, 0x38(%rsp)
movl (%rbx,%r15,4), %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x20b0
movl $0x9, %edi
callq 0x2030
movl $0x4, %esi
movl $0x1, %edx
movq %rbp, %r13
movq %rbp, %rdi
movq %r14, %rcx
callq 0x2050
movq %r14, %rdi
movl (%rsp), %r14d
movl %r14d, %r15d
shrl $0x18, %r15d
movl %r14d, %ebp
shrl $0x10, %ebp
movl $0xff, %eax
andl %eax, %ebp
movl %r14d, %ebx
shrl $0x8, %ebx
andl %eax, %ebx
movl %r14d, %r12d
andl %eax, %r12d
testl %r14d, %r14d
js 0x282f
leaq 0x2890(%rip), %rax      # 0x5090
movq (%rax,%r15,8), %rdx
leaq 0xc10(%rip), %rdi       # 0x341b
movl %r15d, %esi
xorl %eax, %eax
callq 0x2070
leaq 0xc18(%rip), %rdi       # 0x3434
movl %ebp, %esi
movl %ebx, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x2070
jmp 0x298e
callq 0x2090
addl $-0x4, %eax
addl $0xffffff30, %r15d      # imm = 0xFFFFFF30
cmpl $0x2f, %r15d
ja 0x2942
leaq 0x82d(%rip), %rdx       # 0x307c
movslq (%rdx,%r15,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
leaq 0xbf9(%rip), %rdi       # 0x3458
jmp 0x2969
leaq 0x10d1(%rip), %rdi      # 0x393c
jmp 0x2969
leaq 0x11a5(%rip), %rdi      # 0x3a1c
jmp 0x2982
leaq 0xf32(%rip), %rdi       # 0x37b5
jmp 0x2969
leaq 0x12a6(%rip), %rdi      # 0x3b35
jmp 0x2982
leaq 0x121a(%rip), %rdi      # 0x3ab5
jmp 0x2969
leaq 0xe43(%rip), %rdi       # 0x36ea
jmp 0x2969
leaq 0xc20(%rip), %rdi       # 0x34d3
jmp 0x2969
leaq 0xc9a(%rip), %rdi       # 0x3559
jmp 0x2969
leaq 0x1197(%rip), %rdi      # 0x3a62
jmp 0x2982
leaq 0xe9d(%rip), %rdi       # 0x3774
jmp 0x2969
leaq 0x10bd(%rip), %rdi      # 0x39a0
jmp 0x2982
leaq 0xcad(%rip), %rdi       # 0x359c
jmp 0x2969
leaq 0xe2e(%rip), %rdi       # 0x3726
jmp 0x2969
leaq 0xc15(%rip), %rdi       # 0x3516
jmp 0x2969
leaq 0xb8a(%rip), %rdi       # 0x3494
jmp 0x2969
leaq 0x11e2(%rip), %rdi      # 0x3af5
jmp 0x2969
leaq 0xccb(%rip), %rdi       # 0x35e7
jmp 0x2969
leaq 0xd01(%rip), %rdi       # 0x3626
jmp 0x2969
leaq 0xecb(%rip), %rdi       # 0x37f9
jmp 0x2969
leaq 0xf0d(%rip), %rdi       # 0x3844
jmp 0x2982
leaq 0xd76(%rip), %rdi       # 0x36b6
jmp 0x2982
leaq 0x1229(%rip), %rdi      # 0x3b72
movl %r14d, %esi
movl %eax, %edx
jmp 0x2987
leaq 0xd1b(%rip), %rdi       # 0x3672
jmp 0x2969
leaq 0xf92(%rip), %rdi       # 0x38f2
jmp 0x2982
leaq 0xf25(%rip), %rdi       # 0x388e
movl %eax, %esi
movl %ebp, %edx
movl %ebx, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x2070
jmp 0x298e
leaq 0x105d(%rip), %rdi      # 0x39df
movl %eax, %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2070
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rbp
movq %r13, %rdi
movq 0x10(%rsp), %rcx
callq 0x20f0
cmpl $0xfffe0000, (%rsp)     # imm = 0xFFFE0000
movq 0x40(%rsp), %r14
jne 0x27a9
movq 0x10(%rsp), %rdi
callq 0x2060
movq 0x38(%rsp), %r15
incq %r15
cmpq 0x20(%rsp), %r15
movq 0x28(%rsp), %r13
leaq 0x9ea(%rip), %rax       # 0x33c7
movq 0x30(%rsp), %r12
jne 0x2739
incq %r12
cmpq 0x18(%rsp), %r12
leaq 0x4(%rsp), %rbx
movq %r13, %r15
jb 0x26d2
leaq 0x1187(%rip), %rdi      # 0x3b8c
callq 0x2040
movq %r14, %rdi
callq 0x2060
xorl %ebx, %ebx
jmp 0x2a3b
leaq 0x627(%rip), %rdi       # 0x3044
leaq 0x50(%rsp), %rsi
jmp 0x2a2f
movq 0x8(%rbx), %rsi
leaq 0x615(%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