// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1050, %rsp           # imm = 0x1050
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x20e0
movq -0x10(%rbp), %rax
movq (%rax), %rsi
leaq 0x10fe(%rip), %rdi      # 0x31cb
movb $0x0, %al
callq 0x10b0
movl $0x1, -0x4(%rbp)
jmp 0x23ea
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x10a0
cmpq $0x1000, %rax           # imm = 0x1000
jbe 0x210e
leaq 0x10f9(%rip), %rdi      # 0x31f4
movb $0x0, %al
callq 0x10b0
movl $0x1, -0x4(%rbp)
jmp 0x23ea
leaq -0x1030(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x1060
leaq -0x1030(%rbp), %rdi
movl $0x2f, %esi
callq 0x10d0
cmpq $0x0, %rax
jne 0x214f
leaq -0x1030(%rbp), %rdi
movl $0x5c, %esi
callq 0x10d0
cmpq $0x0, %rax
je 0x2165
leaq -0x1030(%rbp), %rdi
movl $0x2f, %esi
callq 0x10d0
movb $0x0, (%rax)
jmp 0x217a
leaq -0x1030(%rbp), %rdi
leaq 0x10a9(%rip), %rsi      # 0x321c
movb $0x0, %al
callq 0x1160
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x1096(%rip), %rsi      # 0x321f
callq 0x1130
movq %rax, -0x1048(%rbp)
cmpq $0x0, %rax
jne 0x21bd
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq 0x100b(%rip), %rdi      # 0x31b5
movb $0x0, %al
callq 0x10b0
movl $0x1, -0x4(%rbp)
jmp 0x23ea
movq -0x1048(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1120
movq -0x1048(%rbp), %rcx
leaq -0x103c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1080
movl -0x103c(%rbp), %eax
movl $0xc, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, -0x103c(%rbp)
leaq -0x1030(%rbp), %rsi
leaq 0x2ec9(%rip), %rdi      # 0x50d8
leaq 0x100c(%rip), %rdx      # 0x3222
movl $0x2, %ecx
leaq -0xfa2(%rip), %r8       # 0x1280
callq 0x13d0
movl %eax, 0x2ebb(%rip)      # 0x50e8
cmpl $0x0, 0x2eb4(%rip)      # 0x50e8
jle 0x2242
movl 0x2eac(%rip), %eax      # 0x50e8
movl %eax, 0x2eae(%rip)      # 0x50f0
movl $0x0, -0x1034(%rbp)
movl -0x1034(%rbp), %eax
cmpl -0x103c(%rbp), %eax
jae 0x23bd
movq -0x1048(%rbp), %rdi
imull $0xc, -0x1034(%rbp), %eax
addl $0x4, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1120
leaq -0x19(%rbp), %rdi
movq -0x1048(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x1080
movb $0x0, -0x11(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x19(%rbp), %rcx
leaq 0xf87(%rip), %rsi       # 0x322e
leaq 0xf85(%rip), %rdx       # 0x3233
movb $0x0, %al
callq 0x1160
movq -0x1048(%rbp), %rcx
leaq -0x1038(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1080
movl -0x1038(%rbp), %eax
shll $0xb, %eax
movl %eax, -0x1038(%rbp)
leaq -0x19(%rbp), %rsi
movl -0x1038(%rbp), %edx
leaq 0xf47(%rip), %rdi       # 0x3239
movb $0x0, %al
callq 0x10b0
leaq -0x19(%rbp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1070
leaq -0x1030(%rbp), %rsi
leaq -0x30(%rbp), %rdx
leaq 0x2dc7(%rip), %rdi      # 0x50e0
movl $0x2, %ecx
leaq -0x10a5(%rip), %r8      # 0x1280
callq 0x13d0
movl %eax, 0x2dbc(%rip)      # 0x50ec
cmpl $0x0, 0x2db5(%rip)      # 0x50ec
jle 0x234b
movl 0x2dad(%rip), %eax      # 0x50ec
addl 0x2dab(%rip), %eax      # 0x50f0
movl %eax, 0x2da5(%rip)      # 0x50f0
movq -0x1048(%rbp), %rdi
movl -0x1038(%rbp), %esi
leaq -0x19(%rbp), %rdx
callq 0x1bd0
cmpq $0x0, 0x2d77(%rip)      # 0x50e0
je 0x2377
movq 0x2d6e(%rip), %rdi      # 0x50e0
callq 0x1030
movq $0x0, 0x2d5e(%rip) # 0x50e0
cmpl $0x0, 0x2d63(%rip)      # 0x50ec
jle 0x239f
movl 0x2d5b(%rip), %ecx      # 0x50ec
movl 0x2d59(%rip), %eax      # 0x50f0
subl %ecx, %eax
movl %eax, 0x2d51(%rip)      # 0x50f0
movl $0x0, 0x2d43(%rip)      # 0x50ec
movl -0x1034(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1034(%rbp)
jmp 0x224c
movq 0x2d14(%rip), %rdi      # 0x50d8
callq 0x1030
leaq 0xe8b(%rip), %rdi       # 0x325b
movb $0x0, %al
callq 0x10b0
movq -0x1048(%rbp), %rdi
callq 0x1090
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1050, %rsp           # imm = 0x1050
popq %rbp
retq