// File: /workspace/github_c_makefile_star2_O0/Joy-Division[P]tools-mgs/stage-extract.c
// Function: unpackstage
int unpackstage(FILE *f, uint32_t stageoffset, char *stagename) {
	fseek(f, stageoffset, SEEK_SET);
	
	unsigned int i, j, numfiles, temp;
	unsigned int fakedar = 0, numfakedar = 0;
	uint16_t flags, type, hash, texnum = 1;
	char *flagstring, *namestring;
	char outputname[128];
	uint32_t header, namehash = 1;
	fileentry *files = NULL;
	unsigned int *fileoffsets;
	
	FILE *o;
	
	fread(&header, 4, 1, f);
	
	for(i = 0; namehash; i++) {
		fread(&namehash, 4, 1, f);
		fread(&header, 4, 1, f);
	}
	i--;
	numfiles = i;
	
	//~ printf("Number of file entries in archive: %d\n", numfiles);
	files = malloc(sizeof(fileentry)*numfiles);
	fileoffsets = malloc(sizeof(unsigned int)*numfiles);
	fseek(f, stageoffset+4, SEEK_SET);
	temp = stageoffset+0x800;
	for(i = 0;i < numfiles; i++) {
		fseek(f, stageoffset+(4+(8*i)), SEEK_SET);
		fread(&files[i], sizeof(fileentry), 1, f);
		if(!files[i].size) {
			//~ printf("Begin of fake stg_mdl1.dar!\n");
			fakedar = 1;
		}
		if(files[i].hash == 0xFF630000) {
			//~ printf("End of fake stg_mdl1.dar!\n");
			files[i].size = 0;
			fakedar = 0;
			numfakedar = 0;
			if(temp % 0x800) temp += 0x800-(temp % 0x800);
			continue;
		}
		if(fakedar) {
			numfakedar++;
			//~ printf("Getting size of file %d in fake dar\n", numfakedar);
			fseek(f, stageoffset+(4+(8*i)+12), SEEK_SET);
			fread(&files[i].size, 4, 1, f);
			for( j = 1; j < numfakedar; j++ ) {
				//~ printf("Subtracting size of file %d from fake dar\n", numfakedar-j);
				files[i].size -= files[i-j].size;
			}
		}
		//~ printf("Filehash %08x - Filesize %08x - Fileoffset = %08x\n", files[i].hash, files[i].size, temp);
		fileoffsets[i] = temp;
		if((files[i].size % 0x800) && !fakedar) temp += (files[i].size + (0x800-(files[i].size % 0x800)));
		else temp += files[i].size;
	}
	
	//~ printf("Begin extracting\n");
	
	for(i = 0; i < numfiles; i++) {
		if(files[i].hash == 0xFF630000) {
			//~ printf("Skipping fake dar entry %d!\n", i);
			continue;
		}
		type = files[i].hash >> 24;
		flags = (files[i].hash >> 16) & 0xFF;
		flagstring = getInfoFromFlags(flags);
		sprintf(outputname, "%s/%s", stagename, flagstring);
		createDirectory(outputname);
		hash = files[i].hash & 0xFFFF;
		namestring = getNameFromDetails(hash, type, flags, texnum);
		//~ printf("Info about file %d:\nType %02x(%s) - Flags %02x(%s) - Name %04x(%s)\n", i, type, typestring, flags, flagstring, hash, namestring);
		sprintf(outputname, "%s/%s/%s", stagename, flagstring, namestring);
		//~ printf("Extracting file as %s\n", outputname);
		fseek(f, fileoffsets[i], SEEK_SET);
		if( !(o = fopen( outputname, "wb" ))) {
			printf("Couldnt open file %s\n", outputname);
			return 1;
		}
		writeFile(f, files[i].size, o);
		fclose(o);
		if(!hash) texnum++;
		free(flagstring);
		free(namestring);
	}
	
	free(files);
	free(fileoffsets);
	//~ printf("Done with stage.\n");
	return numfiles;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1120
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movw $0x1, -0x40(%rbp)
movl $0x1, -0xd8(%rbp)
movq $0x0, -0xe0(%rbp)
movq -0x10(%rbp), %rcx
leaq -0xd4(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1080
movl $0x0, -0x24(%rbp)
cmpl $0x0, -0xd8(%rbp)
je 0x1c88
movq -0x10(%rbp), %rcx
leaq -0xd8(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1080
movq -0x10(%rbp), %rcx
leaq -0xd4(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x1080
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1c40
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, %edi
shlq $0x3, %rdi
callq 0x1100
movq %rax, -0xe0(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, %edi
shlq $0x2, %rdi
callq 0x1100
movq %rax, -0xe8(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %eax
addl $0x4, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1120
movl -0x14(%rbp), %eax
addl $0x800, %eax            # imm = 0x800
movl %eax, -0x30(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x1ecb
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %eax
movl -0x24(%rbp), %ecx
shll $0x3, %ecx
addl $0x4, %ecx
addl %ecx, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1120
movq -0xe0(%rbp), %rdi
movl -0x24(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rcx
movl $0x8, %esi
movl $0x1, %edx
callq 0x1080
movq -0xe0(%rbp), %rax
movl -0x24(%rbp), %ecx
cmpl $0x0, 0x4(%rax,%rcx,8)
jne 0x1d4d
movl $0x1, -0x34(%rbp)
movq -0xe0(%rbp), %rax
movl -0x24(%rbp), %ecx
cmpl $0xff630000, (%rax,%rcx,8) # imm = 0xFF630000
jne 0x1da8
movq -0xe0(%rbp), %rax
movl -0x24(%rbp), %ecx
movl $0x0, 0x4(%rax,%rcx,8)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x30(%rbp), %eax
andl $0x7ff, %eax            # imm = 0x7FF
cmpl $0x0, %eax
je 0x1da3
movl -0x30(%rbp), %ecx
andl $0x7ff, %ecx            # imm = 0x7FF
movl $0x800, %eax            # imm = 0x800
subl %ecx, %eax
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x1ebd
cmpl $0x0, -0x34(%rbp)
je 0x1e46
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %eax
movl -0x24(%rbp), %ecx
shll $0x3, %ecx
addl $0x4, %ecx
addl $0xc, %ecx
addl %ecx, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1120
movq -0xe0(%rbp), %rdi
movl -0x24(%rbp), %eax
shlq $0x3, %rax
addq %rax, %rdi
addq $0x4, %rdi
movq -0x10(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x1080
movl $0x1, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0x1e44
movq -0xe0(%rbp), %rax
movl -0x24(%rbp), %ecx
subl -0x28(%rbp), %ecx
movl %ecx, %ecx
movl 0x4(%rax,%rcx,8), %esi
movq -0xe0(%rbp), %rax
movl -0x24(%rbp), %ecx
movl 0x4(%rax,%rcx,8), %edx
subl %esi, %edx
movl %edx, 0x4(%rax,%rcx,8)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1e0a
jmp 0x1e46
movl -0x30(%rbp), %edx
movq -0xe8(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movq -0xe0(%rbp), %rax
movl -0x24(%rbp), %ecx
movl 0x4(%rax,%rcx,8), %eax
andl $0x7ff, %eax            # imm = 0x7FF
cmpl $0x0, %eax
je 0x1ea7
cmpl $0x0, -0x34(%rbp)
jne 0x1ea7
movq -0xe0(%rbp), %rax
movl -0x24(%rbp), %ecx
movl 0x4(%rax,%rcx,8), %eax
movq -0xe0(%rbp), %rcx
movl -0x24(%rbp), %edx
movl 0x4(%rcx,%rdx,8), %edx
andl $0x7ff, %edx            # imm = 0x7FF
movl $0x800, %ecx            # imm = 0x800
subl %edx, %ecx
addl %ecx, %eax
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x1ebb
movq -0xe0(%rbp), %rax
movl -0x24(%rbp), %ecx
movl 0x4(%rax,%rcx,8), %eax
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0x1ebd
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1ce8
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x2069
movq -0xe0(%rbp), %rax
movl -0x24(%rbp), %ecx
cmpl $0xff630000, (%rax,%rcx,8) # imm = 0xFF630000
jne 0x1ef6
jmp 0x205b
movq -0xe0(%rbp), %rax
movl -0x24(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x18, %eax
movw %ax, -0x3c(%rbp)
movq -0xe0(%rbp), %rax
movl -0x24(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x10, %eax
andl $0xff, %eax
movw %ax, -0x3a(%rbp)
movzwl -0x3a(%rbp), %edi
callq 0x1990
movq %rax, -0x48(%rbp)
leaq -0xd0(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0x48(%rbp), %rcx
leaq 0x1266(%rip), %rsi      # 0x31ac
movb $0x0, %al
callq 0x1160
leaq -0xd0(%rbp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1070
movq -0xe0(%rbp), %rax
movl -0x24(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0xffff, %eax           # imm = 0xFFFF
movw %ax, -0x3e(%rbp)
movw -0x3e(%rbp), %si
movw -0x3c(%rbp), %ax
movzwl -0x3a(%rbp), %edx
movzwl -0x40(%rbp), %ecx
movzwl %si, %edi
movzwl %ax, %esi
callq 0x1a60
movq %rax, -0x50(%rbp)
leaq -0xd0(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %r8
leaq 0x11fc(%rip), %rsi      # 0x31a9
movb $0x0, %al
callq 0x1160
movq -0x10(%rbp), %rdi
movq -0xe8(%rbp), %rax
movl -0x24(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1120
leaq -0xd0(%rbp), %rdi
leaq 0x11d6(%rip), %rsi      # 0x31b2
callq 0x1130
movq %rax, -0xf0(%rbp)
cmpq $0x0, %rax
jne 0x200c
leaq -0xd0(%rbp), %rsi
leaq 0x11b9(%rip), %rdi      # 0x31b5
movb $0x0, %al
callq 0x10b0
movl $0x1, -0x4(%rbp)
jmp 0x2087
movq -0x10(%rbp), %rdi
movq -0xe0(%rbp), %rax
movl -0x24(%rbp), %ecx
movl 0x4(%rax,%rcx,8), %esi
movq -0xf0(%rbp), %rdx
callq 0x17d0
movq -0xf0(%rbp), %rdi
callq 0x1090
cmpw $0x0, -0x3e(%rbp)
jne 0x2049
movw -0x40(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x40(%rbp)
movq -0x48(%rbp), %rdi
callq 0x1030
movq -0x50(%rbp), %rdi
callq 0x1030
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x1ed2
movq -0xe0(%rbp), %rdi
callq 0x1030
movq -0xe8(%rbp), %rdi
callq 0x1030
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)