// File: /workspace/github_c_makefile_star2_O2/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 0x1be2
leaq 0x5a8(%rip), %rdi       # 0x217e
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10b0
jmp 0x1c01
movq %rsi, %r14
movq %rbx, %rdi
callq 0x10a0
cmpq $0x1001, %rax           # imm = 0x1001
jb 0x1c16
leaq 0x5f0(%rip), %rdi       # 0x21ec
callq 0x1070
pushq $0x1
popq %rax
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
pushq $0x2f
popq %rsi
movq %r15, %rdi
callq 0x10d0
movq %rax, %rbx
testq %rax, %rax
jne 0x1c4f
leaq 0x30(%rsp), %rdi
pushq $0x5c
popq %rsi
callq 0x10d0
testq %rax, %rax
je 0x1e14
movb $0x0, (%rbx)
movq 0x8(%r14), %rdi
leaq 0x54d(%rip), %rsi       # 0x21aa
callq 0x1130
testq %rax, %rax
je 0x1e04
movq %rax, %rbx
movq %rax, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1120
leaq 0x10(%rsp), %r14
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x1080
movl (%r14), %eax
pushq $0xc
popq %rcx
xorl %edx, %edx
divl %ecx
movl %eax, %ebp
leaq 0x2435(%rip), %rdi      # 0x40d8
leaq 0x503(%rip), %rdx       # 0x21ad
leaq -0xa35(%rip), %r8       # 0x127c
leaq 0x30(%rsp), %rsi
pushq $0x2
popq %rcx
callq 0x12f8
movl %eax, 0x2424(%rip)      # 0x40e8
testl %eax, %eax
jle 0x1cce
movl %eax, 0x2422(%rip)      # 0x40f0
movl %ebp, %eax
imulq $0xc, %rax, %r13
leaq 0x17(%rsp), %r14
leaq 0x20(%rsp), %r15
xorl %r12d, %r12d
cmpq %r12, %r13
je 0x1ddd
leal 0x4(%r12), %esi
andl $-0x4, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1120
movq %r14, %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rbp
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x1080
movb $0x0, 0x1f(%rsp)
movq %r15, %rdi
leaq 0x49a(%rip), %rsi       # 0x21b9
leaq 0x498(%rip), %rdx       # 0x21be
movq %r14, %rcx
xorl %eax, %eax
callq 0x1160
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
movq %rbp, %rdx
movq %rbx, %rcx
callq 0x1080
movl 0xc(%rsp), %ebp
shll $0xb, %ebp
movl %ebp, 0xc(%rsp)
leaq 0x46f(%rip), %rdi       # 0x21c4
movq %r14, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x10b0
movq %r14, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1060
leaq 0x236b(%rip), %rdi      # 0x40e0
leaq 0x30(%rsp), %rsi
movq %r15, %rdx
pushq $0x2
popq %rcx
leaq -0xb0b(%rip), %r8       # 0x127c
callq 0x12f8
movl %eax, 0x235a(%rip)      # 0x40ec
testl %eax, %eax
jle 0x1d9c
addl %eax, 0x2354(%rip)      # 0x40f0
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x1839
movq 0x2330(%rip), %rdi      # 0x40e0
callq 0x1030
andq $0x0, 0x2323(%rip)      # 0x40e0
movl 0x2329(%rip), %eax      # 0x40ec
testl %eax, %eax
jle 0x1dcd
subl %eax, 0x2323(%rip)      # 0x40f0
andl $0x0, 0x2318(%rip)      # 0x40ec
addq $0xc, %r12
jmp 0x1ce1
movq 0x22f4(%rip), %rdi      # 0x40d8
callq 0x1030
leaq 0x3f6(%rip), %rdi       # 0x21e6
callq 0x1070
movq %rbx, %rdi
callq 0x1090
xorl %eax, %eax
jmp 0x1c04
movq 0x8(%r14), %rsi
leaq 0x359(%rip), %rdi       # 0x2168
jmp 0x1bd9
movb $0x0, 0x32(%rsp)
movw $0x2f2e, 0x30(%rsp)     # imm = 0x2F2E
jmp 0x1c52