// File: /workspace/github_c_makefile_star2_O1/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 0x1c84
leaq 0x506(%rip), %rdi       # 0x217e
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10b0
jmp 0x1ca3
movq %rsi, %r14
movq %rbx, %rdi
callq 0x10a0
cmpq $0x1001, %rax           # imm = 0x1001
jb 0x1cba
leaq 0x54e(%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 0x1cf7
leaq 0x30(%rsp), %rdi
movl $0x5c, %esi
callq 0x10d0
testq %rax, %rax
je 0x1ee4
movb $0x0, (%rbx)
movq 0x8(%r14), %rdi
leaq 0x4a5(%rip), %rsi       # 0x21aa
callq 0x1130
testq %rax, %rax
je 0x1ed4
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), %r12d
movl $0xaaaaaaab, %r15d      # imm = 0xAAAAAAAB
imulq %r12, %r15
shrq $0x23, %r15
movl %r15d, (%r14)
leaq 0x2381(%rip), %rdi      # 0x40d8
leaq 0x44f(%rip), %rdx       # 0x21ad
leaq -0xae9(%rip), %r8       # 0x127c
leaq 0x30(%rsp), %rsi
movl $0x2, %ecx
callq 0x1318
movl %eax, 0x236e(%rip)      # 0x40e8
testl %eax, %eax
jle 0x1d84
movl %eax, 0x236c(%rip)      # 0x40f0
cmpl $0xc, %r12d
jb 0x1ead
shll $0x2, %r15d
leaq (%r15,%r15,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 0x3e4(%rip), %rsi       # 0x21b9
leaq 0x3e2(%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 0x3b2(%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 0x22ad(%rip), %rdi      # 0x40e0
leaq 0x30(%rsp), %rsi
movq %r13, %rdx
movl $0x2, %ecx
leaq -0xbcb(%rip), %r8       # 0x127c
callq 0x1318
movl %eax, 0x229a(%rip)      # 0x40ec
testl %eax, %eax
jle 0x1e5c
addl %eax, 0x2294(%rip)      # 0x40f0
movq %rbx, %rdi
movl %r12d, %esi
movq %r14, %rdx
callq 0x18e2
movq 0x226f(%rip), %rdi      # 0x40e0
testq %rdi, %rdi
je 0x1e7b
callq 0x1030
movq $0x0, 0x225a(%rip) # 0x40e0
movl 0x2260(%rip), %eax      # 0x40ec
testl %eax, %eax
jle 0x1e96
subl %eax, 0x225a(%rip)      # 0x40f0
movl $0x0, 0x224c(%rip)      # 0x40ec
addq $0xc, %r15
cmpq %r15, %rbp
jne 0x1da3
movq 0x2224(%rip), %rdi      # 0x40d8
callq 0x1030
leaq 0x326(%rip), %rdi       # 0x21e6
callq 0x1070
movq %rbx, %rdi
callq 0x1090
xorl %eax, %eax
jmp 0x1ca8
movq 0x8(%r14), %rsi
leaq 0x289(%rip), %rdi       # 0x2168
jmp 0x1c7b
movb $0x0, 0x32(%rsp)
movw $0x2f2e, 0x30(%rsp)     # imm = 0x2F2E
jmp 0x1cfa