// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, 0x40(%rsp)
movq %rdi, %rbx
movq %rsi, 0x20(%rsp)
movl %esi, %esi
xorl %edx, %edx
callq 0x1120
leaq 0x3c(%rsp), %r15
movl $0x1, (%r15)
leaq 0x4c(%rsp), %r12
pushq $0x4
popq %r13
pushq $0x1
popq %rdx
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rcx
callq 0x1080
pushq $0x1
popq %rax
pushq $-0x1
popq %rbp
pushq $0x1
popq %r14
testl %eax, %eax
je 0x18be
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x1080
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rbx, %rcx
callq 0x1080
movl 0x3c(%rsp), %eax
incl %ebp
jmp 0x1890
movl %ebp, 0x1c(%rsp)
movl %ebp, %r13d
leaq (,%r13,8), %rdi
callq 0x1100
movq %rax, %r15
leaq (,%r13,4), %rdi
callq 0x1100
movq %rax, 0x28(%rsp)
movq 0x20(%rsp), %r14
leal 0x4(%r14), %esi
xorl %ebp, %ebp
movq %rbx, 0x30(%rsp)
movq %rbx, %rdi
movq %rsi, 0x8(%rsp)
xorl %edx, %edx
callq 0x1120
leal 0x800(%r14), %esi
addl $0x10, %r14d
movq %r14, 0x20(%rsp)
pushq $-0x1
popq %r14
xorl %ebx, %ebx
xorl %r8d, %r8d
movq %r13, 0x10(%rsp)
cmpq %r13, %rbp
je 0x1a57
movq %rsi, (%rsp)
movq 0x8(%rsp), %rax
leal (%rax,%rbp,8), %esi
movq 0x30(%rsp), %r12
movq %r12, %rdi
xorl %edx, %edx
movl %r8d, %r13d
callq 0x1120
leaq (%r15,%rbp,8), %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r12, %rcx
callq 0x1080
movl %r13d, %r8d
leaq (%r15,%rbp,8), %r12
addq $0x4, %r12
movl 0x4(%r15,%rbp,8), %eax
testl %eax, %eax
pushq $0x1
popq %rcx
cmovel %ecx, %r8d
cmpl $0xff630000, (%r15,%rbp,8) # imm = 0xFF630000
jne 0x19ab
andl $0x0, (%r12)
xorl %ebx, %ebx
movq (%rsp), %rsi
testw $0x7ff, %si             # imm = 0x7FF
je 0x199e
andl $0xfffff800, %esi       # imm = 0xFFFFF800
addl $0x800, %esi            # imm = 0x800
xorl %r8d, %r8d
movq 0x10(%rsp), %r13
jmp 0x1a4c
testl %r8d, %r8d
je 0x1a14
movl %r8d, 0x38(%rsp)
leal (,%rbp,8), %esi
addl 0x20(%rsp), %esi
incl %ebx
movq 0x30(%rsp), %r13
movq %r13, %rdi
xorl %edx, %edx
callq 0x1120
movq %r12, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x1080
movq %r14, %rax
pushq $0x1
popq %rcx
movq (%rsp), %rsi
cmpq %rbx, %rcx
jae 0x1a04
movl %eax, %edx
movl 0x4(%r15,%rdx,8), %edx
subl %edx, (%r12)
incq %rcx
decq %rax
jmp 0x19ec
movl (%r12), %eax
movq 0x10(%rsp), %r13
movl 0x38(%rsp), %r8d
jmp 0x1a1d
movq (%rsp), %rsi
movq 0x10(%rsp), %r13
testl %r8d, %r8d
setne %cl
movq 0x28(%rsp), %rdx
movl %esi, (%rdx,%rbp,4)
testw $0x7ff, %ax             # imm = 0x7FF
sete %dl
orb %cl, %dl
je 0x1a3c
addl %esi, %eax
movl %eax, %esi
jmp 0x1a4c
andl $0xfffff800, %eax       # imm = 0xFFFFF800
addl %eax, %esi
addl $0x800, %esi            # imm = 0x800
xorl %r8d, %r8d
incq %rbp
incq %r14
jmp 0x1924
movw $0x1, %ax
xorl %r12d, %r12d
movl 0x1c(%rsp), %ebp
cmpq %r12, %r13
je 0x1b78
movl (%r15,%r12,8), %ebx
cmpl $0xff630000, %ebx       # imm = 0xFF630000
je 0x1b70
movl %ebx, %r14d
shrl $0x18, %r14d
movw %ax, (%rsp)
movl %ebx, %eax
shrl $0x10, %eax
movzbl %al, %edi
movl %edi, 0x8(%rsp)
callq 0x16a5
movq %rax, %r13
leaq 0x50(%rsp), %rdi
leaq 0x6b9(%rip), %rsi       # 0x215f
movq 0x40(%rsp), %rbp
movq %rbp, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1160
leaq 0x50(%rsp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1060
movzwl (%rsp), %ecx
movzwl %bx, %edi
movl %r14d, %esi
movl 0x8(%rsp), %edx
callq 0x1724
movq %rax, %r14
leaq 0x50(%rsp), %rdi
leaq 0x673(%rip), %rsi       # 0x215c
movq %rbp, %rdx
movq %r13, 0x8(%rsp)
movq %r13, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x1160
movq 0x28(%rsp), %rax
movl (%rax,%r12,4), %esi
movq 0x30(%rsp), %r13
movq %r13, %rdi
xorl %edx, %edx
callq 0x1120
leaq 0x50(%rsp), %rdi
leaq 0x643(%rip), %rsi       # 0x2165
callq 0x1130
testq %rax, %rax
je 0x1b8e
movq %rax, %rbp
movl 0x4(%r15,%r12,8), %esi
movq %r13, %rdi
movq %rax, %rdx
callq 0x15c4
movq %rbp, %rdi
callq 0x1090
cmpw $0x1, %bx
movzwl (%rsp), %ebx
adcw $0x0, %bx
movq 0x8(%rsp), %rdi
callq 0x1030
movq %r14, %rdi
callq 0x1030
movl 0x1c(%rsp), %ebp
movq 0x10(%rsp), %r13
movl %ebx, %eax
incq %r12
jmp 0x1a62
movq %r15, %rdi
callq 0x1030
movq 0x28(%rsp), %rdi
callq 0x1030
movl %ebp, %eax
jmp 0x1ba4
leaq 0x5d3(%rip), %rdi       # 0x2168
leaq 0x50(%rsp), %rsi
xorl %eax, %eax
callq 0x10b0
pushq $0x1
popq %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq