// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x17dd
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x969(%rip), %rdi       # 0x2133
movb $0x0, %al
callq 0x1080
movl $0x1, -0x4(%rbp)
jmp 0x1b37
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x94f(%rip), %rsi       # 0x2163
callq 0x10d0
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x1845
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x934(%rip), %rdi       # 0x2166
movb $0x0, %al
callq 0x1080
movl $0x1, -0x4(%rbp)
jmp 0x1b37
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x1070
movq %rax, %rdi
addq $0x4, %rdi
addq $0x1, %rdi
callq 0x10b0
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x1040
movq -0x58(%rbp), %rdi
leaq 0x8fa(%rip), %rsi       # 0x217c
callq 0x10e0
movq -0x58(%rbp), %rdi
leaq 0x8ef(%rip), %rsi       # 0x2181
callq 0x10d0
movq %rax, -0x20(%rbp)
cmpq $0x0, %rax
jne 0x18bf
movq -0x58(%rbp), %rsi
leaq 0x8ba(%rip), %rdi       # 0x2166
movb $0x0, %al
callq 0x1080
movl $0x1, -0x4(%rbp)
jmp 0x1b37
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x10c0
movq -0x18(%rbp), %rdi
callq 0x10a0
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10c0
movl -0x24(%rbp), %eax
movl %eax, %edi
callq 0x10b0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
movl -0x24(%rbp), %eax
movl %eax, %esi
movq -0x18(%rbp), %rcx
movl $0x1, %edx
callq 0x1050
movq -0x18(%rbp), %rdi
callq 0x1060
movq -0x40(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x60(%rbp), %rax
movl -0x2c(%rbp), %ecx
cmpw $0x0, (%rax,%rcx,4)
je 0x19ac
movq -0x60(%rbp), %rax
movl -0x2c(%rbp), %ecx
movzwl 0x2(%rax,%rcx,4), %edi
callq 0x1260
movw %ax, %dx
movq -0x60(%rbp), %rax
movl -0x2c(%rbp), %ecx
movw %dx, 0x2(%rax,%rcx,4)
movq -0x60(%rbp), %rax
movl -0x2c(%rbp), %ecx
movzwl (%rax,%rcx,4), %edi
callq 0x1260
movw %ax, %dx
movq -0x60(%rbp), %rax
movl -0x2c(%rbp), %ecx
movw %dx, (%rax,%rcx,4)
movl -0x2c(%rbp), %esi
movq -0x60(%rbp), %rax
movl -0x2c(%rbp), %ecx
movzwl (%rax,%rcx,4), %edx
movq -0x60(%rbp), %rax
movl -0x2c(%rbp), %ecx
movzwl 0x2(%rax,%rcx,4), %ecx
leaq 0x7e9(%rip), %rdi       # 0x2183
movb $0x0, %al
callq 0x1080
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x192d
movl -0x2c(%rbp), %esi
leaq 0x7f2(%rip), %rdi       # 0x21a8
movb $0x0, %al
callq 0x1080
movl -0x2c(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x10b0
movq %rax, -0x48(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x1a1b
movq -0x40(%rbp), %rdx
addq $0x4, %rdx
movl -0x2c(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rdx
addq $0x4, %rdx
movq -0x60(%rbp), %rax
movl -0x24(%rbp), %ecx
movzwl 0x2(%rax,%rcx,4), %eax
cltq
addq %rax, %rdx
movq -0x48(%rbp), %rax
movl -0x24(%rbp), %ecx
movq %rdx, (%rax,%rcx,8)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x19d6
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x1afe
movl -0x24(%rbp), %esi
movq -0x60(%rbp), %rax
movl -0x24(%rbp), %ecx
movzwl (%rax,%rcx,4), %edx
leaq 0x77a(%rip), %rdi       # 0x21bd
movb $0x0, %al
callq 0x1080
movq -0x48(%rbp), %rax
movl -0x24(%rbp), %ecx
movq (%rax,%rcx,8), %rsi
movq -0x40(%rbp), %rax
subq %rax, %rsi
leaq 0x777(%rip), %rdi       # 0x21da
movb $0x0, %al
callq 0x1080
movq -0x60(%rbp), %rax
movl -0x24(%rbp), %ecx
movq -0x48(%rbp), %rdx
movl -0x24(%rbp), %esi
movq (%rdx,%rsi,8), %rsi
movq -0x20(%rbp), %rdx
movl (%rax,%rcx,4), %edi
callq 0x1450
movzwl %ax, %eax
movq %rax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movl -0x24(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpq -0x38(%rbp), %rax
jae 0x1ac5
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1aa5
movq -0x50(%rbp), %rsi
movq -0x40(%rbp), %rax
subq %rax, %rsi
leaq 0x6fc(%rip), %rdi       # 0x21d3
movb $0x0, %al
callq 0x1080
movq -0x20(%rbp), %rdi
leaq 0x548(%rip), %rsi       # 0x2031
movb $0x0, %al
callq 0x1090
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1a22
movq -0x20(%rbp), %rdi
callq 0x1060
movq -0x58(%rbp), %rdi
callq 0x1030
movq -0x40(%rbp), %rdi
callq 0x1030
movq -0x48(%rbp), %rdi
callq 0x1030
leaq 0x6c3(%rip), %rdi       # 0x21ec
movb $0x0, %al
callq 0x1080
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq