// File: /workspace/github_c_makefile_star2_O3/Joy-Division[P]tools-mgs/gcx-decompile.c
// Function: main
int main( int argc, char** argv ) {
	
	if ( argc < 2 )
	{
		printf( "Not enough arguments given!\nUsage: %s file.gcx\n", argv[0] );
		return 1;
	}
	
	FILE *f, *o;
	unsigned int i, j, numdefs;
	unsigned long int used;
	unsigned char *gcxdata = NULL, **procs = NULL, *endofproc = NULL;
	char *outputname = NULL;
	
	gcxdef *deflist = NULL;
	
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	
	outputname = malloc(strlen(argv[1])+4+1);
	strcpy(outputname, argv[1]);
	strcat(outputname, ".txt");
	
	if( !(o = fopen(outputname, "w" ))) {
		printf("Couldnt open file %s\n", outputname);
		return 1;
	}
	
	fseek(f, 0, SEEK_END);
	i = ftell(f);
	fseek(f, 0, SEEK_SET);
	gcxdata = malloc(i);
	fread(gcxdata, i, 1, f);
	fclose(f);
	
	deflist = (gcxdef *)(gcxdata+4);
	
	for(numdefs = 0; deflist[numdefs].name; numdefs++) {
		deflist[numdefs].off = bswap_16(deflist[numdefs].off);
		deflist[numdefs].name = bswap_16(deflist[numdefs].name);
		printf("proc %02u - hash %04x - offset %04x\n", numdefs, deflist[numdefs].name, deflist[numdefs].off);
	}
	printf("have %u definitions\n", numdefs);
	
	procs = malloc(sizeof(unsigned char *) * numdefs);
	// skip 4 bytes at beginning, skip numdefs procdefs, skip 4 zerobytes at end of proctable, add proc off
	for(i = 0; i < numdefs; i++) procs[i] = (gcxdata + 4 + sizeof(gcxdef)*numdefs + 4 + deflist[i].off);
	
	for(i = 0; i < numdefs; i++) {
	//~ for(i = 0; i < 1; i++) {
		printf("parsing proc %u %04x\n", i, deflist[i].name);
		printf("proc is at %08lx\n", procs[i]-gcxdata);
		used = gcx_proc(deflist[i], procs[i], o);
		endofproc = procs[i];
		for(j = 0; j < used; j++) endofproc++;
		printf("end of proc is at %08lx\n", endofproc-gcxdata);
		fprintf(o, "\n");
	}
	
	fclose(o);
	free(outputname);
	free(gcxdata);
	free(procs);
	printf("Done.\n");
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
cmpl $0x1, %edi
jg 0x1626
movq (%rbx), %rsi
leaq 0xb12(%rip), %rdi       # 0x2133
jmp 0x1867
movq 0x8(%rbx), %rdi
leaq 0xb32(%rip), %rsi       # 0x2163
callq 0x10f0
movq 0x8(%rbx), %rbx
testq %rax, %rax
je 0x1851
movq %rax, %r13
movq %rbx, %rdi
callq 0x1080
leaq 0x5(%rax), %rdi
callq 0x10d0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1040
movq %r14, %rdi
callq 0x1080
movl $0x7478742e, (%r14,%rax) # imm = 0x7478742E
movb $0x0, 0x4(%r14,%rax)
leaq 0xaff(%rip), %rsi       # 0x2181
movq %r14, %rdi
callq 0x10f0
testq %rax, %rax
je 0x185d
movq %rax, %rbp
movq %r14, 0x8(%rsp)
xorl %r12d, %r12d
movq %r13, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x10e0
movq %r13, %rdi
callq 0x10c0
movq %rax, %rbx
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10e0
movl %ebx, %ebx
movq %rbx, %rdi
callq 0x10d0
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %rbx, %rsi
movq %r13, %rcx
callq 0x1060
movq %r13, %rdi
callq 0x1070
movzwl 0x4(%r15), %eax
testw %ax, %ax
je 0x173c
leaq 0x4(%r15), %r14
leaq 0xa82(%rip), %rbx       # 0x2183
movq %r14, %rcx
xorl %r12d, %r12d
movzwl 0x2(%rcx), %esi
rolw $0x8, %si
rolw $0x8, %ax
movw %si, 0x2(%rcx)
movw %ax, (%rcx)
movzwl %ax, %edx
movzwl %si, %ecx
movq %rbx, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x1090
incl %r12d
leaq (%r14,%r12,4), %rcx
movzwl (%rcx), %eax
testw %ax, %ax
jne 0x1707
leaq 0xa65(%rip), %rdi       # 0x21a8
movl %r12d, %esi
xorl %eax, %eax
callq 0x1090
leaq (,%r12,8), %rdi
callq 0x10d0
movq %rax, %r14
testl %r12d, %r12d
movq %rbp, %r13
je 0x181f
leaq (%r15,%r12,4), %rax
addq $0x8, %rax
movl %r12d, %esi
xorl %ecx, %ecx
movzwl 0x6(%r15,%rcx,4), %edx
addq %rax, %rdx
movq %rdx, (%r14,%rcx,8)
incq %rcx
cmpq %rcx, %rsi
jne 0x1776
testl %r12d, %r12d
movq %rbp, %r13
je 0x181f
xorl %r12d, %r12d
movq %rsi, 0x10(%rsp)
movzwl 0x4(%r15,%r12,4), %edx
leaq 0xa11(%rip), %rdi       # 0x21bd
movl %r12d, %esi
xorl %eax, %eax
callq 0x1090
movq (%r14,%r12,8), %rbp
movq %rbp, %rbx
subq %r15, %rbx
leaq 0xa13(%rip), %rdi       # 0x21da
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
movl 0x4(%r15,%r12,4), %edi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x1358
movzwl %ax, %eax
leal -0x1(%rax), %esi
addq %rbp, %rsi
subq %r15, %rsi
incq %rsi
testw %ax, %ax
cmoveq %rbx, %rsi
leaq 0x9d5(%rip), %rdi       # 0x21d3
xorl %eax, %eax
callq 0x1090
movl $0xa, %edi
movq %r13, %rsi
callq 0x10a0
movq 0x10(%rsp), %rsi
incq %r12
cmpq %r12, %rsi
jne 0x179f
movq %r13, %rdi
callq 0x1070
movq 0x8(%rsp), %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
leaq 0x9a4(%rip), %rdi       # 0x21ec
callq 0x1050
xorl %eax, %eax
jmp 0x1873
leaq 0x90e(%rip), %rdi       # 0x2166
movq %rbx, %rsi
jmp 0x1867
leaq 0x902(%rip), %rdi       # 0x2166
movq %r14, %rsi
xorl %eax, %eax
callq 0x1090
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq