// File: /workspace/github_c_makefile_star2_O3/Joy-Division[P]tools-mgs/stage-extract.c
// Function: main
int main( int argc, char **argv ) {
	
	char name[9], dicname[16];
	char execpath[4096];
	unsigned int i;
	uint32_t offset, numstages;
	
	if ( argc < 2 ) {
		printf("Not enough args!\nUse: %s STAGE.DIR-file\n", argv[0]);
		return 1;
	}
	
	if(strlen(argv[0]) > 4096) BREAK("Path to executable exceeds 4096 chars!\n")
	strcpy(execpath, argv[0]);
	if(strrchr(execpath, '/') || strrchr(execpath, '\\')) cleanExecPath(execpath);
	else sprintf(execpath, "./");
	
	FILE *f;
	
	if( !(f = fopen( argv[1], "rb" ))) {
		printf("Couldnt open file %s\n", argv[1]);
		return 1;
	}
	fseek( f, 0, SEEK_SET );
	
	fread(&numstages, 4, 1, f);
	numstages /= 12;
	
	numcommondic = loaddic(&commondic, execpath, "common-mgs1", DIC_HASH_SINGLE_EXT, StrCode16);
	if(numcommondic > 0) numdicentries = numcommondic;
	for(i = 0;i < numstages;i++) {
		fseek(f, 4+(12*i), SEEK_SET);
		fread(name, 8, 1, f);
		name[8] = 0;
		sprintf(dicname, "%s%s", "mgs1-", name);
		fread(&offset, 4, 1, f);
		offset *= BLOCKSIZE;
		printf("Current stage: %s - offset: %08x\n", name, offset);
		createDirectory(name);
		numstagedic = loaddic(&stagedic, execpath, dicname, DIC_HASH_SINGLE_EXT, StrCode16);
		if(numstagedic > 0) numdicentries += numstagedic;
		unpackstage(f, offset, name);
		if(stagedic) free(stagedic);
		stagedic = NULL;
		if(numstagedic > 0) numdicentries -= numstagedic;
		numstagedic = 0;
	}
	
	free(commondic);
	
	printf("Done.\n");
	
	fclose(f);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1038, %rsp           # imm = 0x1038
movq (%rsi), %rbx
cmpl $0x1, %edi
jg 0x1c74
leaq 0x516(%rip), %rdi       # 0x217e
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10b0
jmp 0x1c93
movq %rsi, %r14
movq %rbx, %rdi
callq 0x10a0
cmpq $0x1001, %rax           # imm = 0x1001
jb 0x1caa
leaq 0x55e(%rip), %rdi       # 0x21ec
callq 0x1070
movl $0x1, %eax
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1050
movq %r15, %rdi
movl $0x2f, %esi
callq 0x10d0
movq %rax, %rbx
testq %rax, %rax
jne 0x1ce7
leaq 0x30(%rsp), %rdi
movl $0x5c, %esi
callq 0x10d0
testq %rax, %rax
je 0x1ed0
movb $0x0, (%rbx)
movq 0x8(%r14), %rdi
leaq 0x4b5(%rip), %rsi       # 0x21aa
callq 0x1130
testq %rax, %rax
je 0x1ec0
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1120
leaq 0x10(%rsp), %r14
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x1080
movl (%r14), %r14d
leaq 0x23a2(%rip), %rdi      # 0x40d8
leaq 0x470(%rip), %rdx       # 0x21ad
leaq -0xac8(%rip), %r8       # 0x127c
leaq 0x30(%rsp), %rsi
movl $0x2, %ecx
callq 0x1318
movl %eax, 0x238f(%rip)      # 0x40e8
testl %eax, %eax
jle 0x1d63
movl %eax, 0x238d(%rip)      # 0x40f0
cmpl $0xc, %r14d
jb 0x1e99
movl $0xaaaaaaab, %eax       # imm = 0xAAAAAAAB
imulq %rax, %r14
shrq $0x23, %r14
shll $0x2, %r14d
leaq (%r14,%r14,2), %rbp
leaq 0x17(%rsp), %r14
leaq 0x20(%rsp), %r13
xorl %r15d, %r15d
leaq 0x4(%r15), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1120
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x1080
movb $0x0, 0x1f(%rsp)
movq %r13, %rdi
leaq 0x3f8(%rip), %rsi       # 0x21b9
leaq 0x3f6(%rip), %rdx       # 0x21be
movq %r14, %rcx
xorl %eax, %eax
callq 0x1160
movl $0x4, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
movq %rbx, %rcx
callq 0x1080
movl 0xc(%rsp), %r12d
shll $0xb, %r12d
movl %r12d, 0xc(%rsp)
leaq 0x3c6(%rip), %rdi       # 0x21c4
movq %r14, %rsi
movl %r12d, %edx
xorl %eax, %eax
callq 0x10b0
movq %r14, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1060
leaq 0x22c1(%rip), %rdi      # 0x40e0
leaq 0x30(%rsp), %rsi
movq %r13, %rdx
movl $0x2, %ecx
leaq -0xbb7(%rip), %r8       # 0x127c
callq 0x1318
movl %eax, 0x22ae(%rip)      # 0x40ec
testl %eax, %eax
jle 0x1e48
addl %eax, 0x22a8(%rip)      # 0x40f0
movq %rbx, %rdi
movl %r12d, %esi
movq %r14, %rdx
callq 0x18a7
movq 0x2283(%rip), %rdi      # 0x40e0
testq %rdi, %rdi
je 0x1e67
callq 0x1030
movq $0x0, 0x226e(%rip) # 0x40e0
movl 0x2274(%rip), %eax      # 0x40ec
testl %eax, %eax
jle 0x1e82
subl %eax, 0x226e(%rip)      # 0x40f0
movl $0x0, 0x2260(%rip)      # 0x40ec
addq $0xc, %r15
cmpq %r15, %rbp
jne 0x1d8f
movq 0x2238(%rip), %rdi      # 0x40d8
callq 0x1030
leaq 0x33a(%rip), %rdi       # 0x21e6
callq 0x1070
movq %rbx, %rdi
callq 0x1090
xorl %eax, %eax
jmp 0x1c98
movq 0x8(%r14), %rsi
leaq 0x29d(%rip), %rdi       # 0x2168
jmp 0x1c6b
movb $0x0, 0x32(%rsp)
movw $0x2f2e, 0x30(%rsp)     # imm = 0x2F2E
jmp 0x1cea