// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
movq %rsi, %rbx
cmpl $0x1, %edi
jg 0x1598
movq (%rbx), %rsi
leaq 0xba0(%rip), %rdi       # 0x2133
jmp 0x1798
movq 0x8(%rbx), %rdi
leaq 0xbc0(%rip), %rsi       # 0x2163
callq 0x10f0
movq 0x8(%rbx), %rbx
testq %rax, %rax
je 0x1782
movq %rax, %r12
movq %rbx, %rdi
callq 0x1080
leaq 0x5(%rax), %rdi
callq 0x10d0
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1040
movq %r15, %rdi
callq 0x1080
movl $0x7478742e, (%r15,%rax) # imm = 0x7478742E
movb $0x0, 0x4(%r15,%rax)
leaq 0xb8d(%rip), %rsi       # 0x2181
movq %r15, %rdi
callq 0x10f0
testq %rax, %rax
je 0x178e
movq %rax, %r14
movq %r15, (%rsp)
pushq $0x2
popq %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x10e0
movq %r12, %rdi
callq 0x10c0
movq %rax, %rbx
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x10e0
movl %ebx, %ebx
movq %rbx, %rdi
callq 0x10d0
movq %rax, %r15
pushq $0x1
popq %rdx
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x1060
movq %r12, %rdi
callq 0x1070
leaq 0xb26(%rip), %rbx       # 0x2183
xorl %eax, %eax
movl %eax, %r12d
movzwl 0x4(%r15,%r12,4), %eax
testw %ax, %ax
je 0x16a1
movzwl 0x6(%r15,%r12,4), %ecx
rolw $0x8, %cx
rolw $0x8, %ax
movw %cx, 0x6(%r15,%r12,4)
movw %ax, 0x4(%r15,%r12,4)
movzwl %ax, %edx
movzwl %cx, %ecx
movq %rbx, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x1090
leal 0x1(%r12), %eax
jmp 0x165f
leaq 0xb00(%rip), %rdi       # 0x21a8
xorl %ebx, %ebx
movl %r12d, %esi
xorl %eax, %eax
callq 0x1090
leaq (,%r12,8), %rdi
callq 0x10d0
movq %rax, %r13
leaq (%r15,%r12,4), %rax
addq $0x8, %rax
cmpq %rbx, %r12
je 0x16e4
movzwl 0x6(%r15,%rbx,4), %ecx
addq %rax, %rcx
movq %rcx, (%r13,%rbx,8)
incq %rbx
jmp 0x16cc
xorl %ebp, %ebp
cmpq %rbp, %r12
je 0x1751
movzwl 0x4(%r15,%rbp,4), %edx
leaq 0xac5(%rip), %rdi       # 0x21bd
movl %ebp, %esi
xorl %eax, %eax
callq 0x1090
movq (%r13,%rbp,8), %rbx
movq %rbx, %rsi
subq %r15, %rsi
leaq 0xac7(%rip), %rdi       # 0x21da
xorl %eax, %eax
callq 0x1090
movl 0x4(%r15,%rbp,4), %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1354
movzwl %ax, %esi
addq %rbx, %rsi
subq %r15, %rsi
leaq 0xa99(%rip), %rdi       # 0x21d3
xorl %eax, %eax
callq 0x1090
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x10a0
incq %rbp
jmp 0x16e6
movq %r14, %rdi
callq 0x1070
movq (%rsp), %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
leaq 0xa73(%rip), %rdi       # 0x21ec
callq 0x1050
xorl %eax, %eax
jmp 0x17a2
leaq 0x9dd(%rip), %rdi       # 0x2166
movq %rbx, %rsi
jmp 0x1798
leaq 0x9d1(%rip), %rdi       # 0x2166
movq %r15, %rsi
xorl %eax, %eax
callq 0x1090
pushq $0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq