// File: /workspace/github_c_makefile_star2_O1/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 0x1629
movq (%rbx), %rsi
leaq 0xb0f(%rip), %rdi       # 0x2133
jmp 0x184c
movq 0x8(%rbx), %rdi
leaq 0xb2f(%rip), %rsi       # 0x2163
callq 0x10f0
movq 0x8(%rbx), %rbx
testq %rax, %rax
je 0x1836
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 0xafc(%rip), %rsi       # 0x2181
movq %r15, %rdi
callq 0x10f0
testq %rax, %rax
je 0x1842
movq %rax, %r14
movq %r15, (%rsp)
xorl %r13d, %r13d
movq %r12, %rdi
xorl %esi, %esi
movl $0x2, %edx
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
movl $0x1, %edx
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rcx
callq 0x1060
movq %r12, %rdi
callq 0x1070
movzwl 0x4(%r15), %eax
testw %ax, %ax
je 0x173e
leaq 0x4(%r15), %r12
leaq 0xa80(%rip), %rbx       # 0x2183
movq %r12, %rcx
xorl %r13d, %r13d
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 %r13d, %esi
xorl %eax, %eax
callq 0x1090
incl %r13d
leaq (%r12,%r13,4), %rcx
movzwl (%rcx), %eax
testw %ax, %ax
jne 0x1709
leaq 0xa63(%rip), %rdi       # 0x21a8
movl %r13d, %esi
xorl %eax, %eax
callq 0x1090
leaq (,%r13,8), %rdi
callq 0x10d0
movq %rax, %r12
testl %r13d, %r13d
je 0x1805
leaq (%r15,%r13,4), %rax
addq $0x8, %rax
movl %r13d, %ebp
xorl %ecx, %ecx
movzwl 0x6(%r15,%rcx,4), %edx
addq %rax, %rdx
movq %rdx, (%r12,%rcx,8)
incq %rcx
cmpq %rcx, %rbp
jne 0x1775
xorl %r13d, %r13d
movzwl 0x4(%r15,%r13,4), %edx
leaq 0xa23(%rip), %rdi       # 0x21bd
movl %r13d, %esi
xorl %eax, %eax
callq 0x1090
movq (%r12,%r13,8), %rbx
movq %rbx, %rsi
subq %r15, %rsi
leaq 0xa25(%rip), %rdi       # 0x21da
xorl %eax, %eax
callq 0x1090
movl 0x4(%r15,%r13,4), %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1358
testw %ax, %ax
je 0x17dc
movzwl %ax, %eax
decl %eax
addq %rax, %rbx
incq %rbx
subq %r15, %rbx
leaq 0x9ed(%rip), %rdi       # 0x21d3
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1090
movl $0xa, %edi
movq %r14, %rsi
callq 0x10a0
incq %r13
cmpq %r13, %rbp
jne 0x178d
movq %r14, %rdi
callq 0x1070
movq (%rsp), %rdi
callq 0x1030
movq %r15, %rdi
callq 0x1030
movq %r12, %rdi
callq 0x1030
leaq 0x9bf(%rip), %rdi       # 0x21ec
callq 0x1050
xorl %eax, %eax
jmp 0x1858
leaq 0x929(%rip), %rdi       # 0x2166
movq %rbx, %rsi
jmp 0x184c
leaq 0x91d(%rip), %rdi       # 0x2166
movq %r15, %rsi
xorl %eax, %eax
callq 0x1090
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq