// File: /workspace/github_c_makefile_star2_O3/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 $0xe8, %rsp
movq %rdx, 0x40(%rsp)
movl %esi, %r12d
movq %rdi, %r14
movl %esi, %esi
xorl %edx, %edx
callq 0x1120
leaq 0x3c(%rsp), %rbx
movl $0x1, (%rbx)
leaq 0x5c(%rsp), %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x1080
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0x4, %esi
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rcx
callq 0x1080
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x1080
incl %ebp
cmpl $0x0, 0x3c(%rsp)
jne 0x18f6
movl %ebp, %ebx
leaq (,%rbx,8), %rdi
callq 0x1100
movq %rax, %r15
movq %rbx, 0x48(%rsp)
leaq (,%rbx,4), %rdi
callq 0x1100
movq %rax, 0x28(%rsp)
leal 0x4(%r12), %esi
xorl %ebx, %ebx
movq %r14, %rdi
movq %rsi, 0x10(%rsp)
xorl %edx, %edx
callq 0x1120
testl %ebp, %ebp
je 0x1c08
movl %ebp, 0x1c(%rsp)
movq %r14, 0x30(%rsp)
leal 0x800(%r12), %r14d
addl $0x10, %r12d
movq %r12, 0x50(%rsp)
movl $0x1, %ebp
xorl %r13d, %r13d
movl $0x0, 0xc(%rsp)
movq 0x10(%rsp), %rax
leal (%rax,%r13,8), %esi
movq 0x30(%rsp), %r12
movq %r12, %rdi
xorl %edx, %edx
callq 0x1120
leaq (%r15,%r13,8), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x1080
leaq (%r15,%r13,8), %r12
addq $0x4, %r12
movl 0x4(%r15,%r13,8), %eax
testl %eax, %eax
cmovel %ebp, %ebx
cmpl $0xff630000, (%r15,%r13,8) # imm = 0xFF630000
jne 0x1a14
movl $0x0, (%r12)
movl $0x0, 0xc(%rsp)
testl $0x7ff, %r14d           # imm = 0x7FF
je 0x1ab2
andl $0xfffff800, %r14d      # imm = 0xFFFFF800
addl $0x800, %r14d           # imm = 0x800
jmp 0x1ab2
testl %ebx, %ebx
je 0x1a8c
movq %rbx, 0x20(%rsp)
leal (,%r13,8), %esi
addl 0x50(%rsp), %esi
movl 0xc(%rsp), %ebx
incl %ebx
movq 0x30(%rsp), %rbp
movq %rbp, %rdi
xorl %edx, %edx
callq 0x1120
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %rbp, %rcx
callq 0x1080
movl (%r12), %eax
cmpl $0x1, %ebx
movl %ebx, 0xc(%rsp)
jbe 0x1ab6
movl %ebx, %ecx
negq %rcx
movq $-0x1, %rdx
leal (%rdx,%r13), %esi
subl 0x4(%r15,%rsi,8), %eax
movl %eax, 0x4(%r15,%r13,8)
decq %rdx
cmpq %rdx, %rcx
jne 0x1a6c
movl $0x1, %ebp
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %rcx
movl %r14d, (%rcx,%r13,4)
testl %ebx, %ebx
jne 0x1ac9
movl %eax, %ecx
andl $0x7ff, %ecx            # imm = 0x7FF
je 0x1ac9
andl $0xfffff800, %eax       # imm = 0xFFFFF800
addl %eax, %r14d
addl $0x800, %r14d           # imm = 0x800
xorl %ebx, %ebx
jmp 0x1acc
movq 0x28(%rsp), %rcx
movl %r14d, (%rcx,%r13,4)
movl $0x1, %ebp
movq 0x20(%rsp), %rbx
addl %eax, %r14d
incq %r13
cmpq 0x48(%rsp), %r13
jne 0x199a
movl 0x1c(%rsp), %ebp
testl %ebp, %ebp
je 0x1c08
movw $0x1, 0xc(%rsp)
xorl %ebx, %ebx
movl (%r15,%rbx,8), %r12d
cmpl $0xff630000, %r12d      # imm = 0xFF630000
je 0x1bfa
movl %r12d, %r13d
shrl $0x18, %r13d
movl %r12d, %eax
shrl $0x10, %eax
movzbl %al, %edi
movl %edi, 0x10(%rsp)
callq 0x1708
movq %rax, %rbp
leaq 0x60(%rsp), %r14
movq %r14, %rdi
leaq 0x634(%rip), %rsi       # 0x215f
movq 0x40(%rsp), %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1160
movq %r14, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1060
movzwl 0xc(%rsp), %r14d
movzwl %r14w, %ecx
movzwl %r12w, %edi
movl %r13d, %esi
movl 0x10(%rsp), %edx
callq 0x178e
leaq 0x60(%rsp), %r13
movq %r13, %rdi
leaq 0x5ec(%rip), %rsi       # 0x215c
movq 0x40(%rsp), %rdx
movq %rbp, 0x10(%rsp)
movq %rbp, %rcx
movq %rax, 0x20(%rsp)
movq %rax, %r8
xorl %eax, %eax
callq 0x1160
movq 0x28(%rsp), %rax
movl (%rax,%rbx,4), %esi
movq 0x30(%rsp), %rbp
movq %rbp, %rdi
xorl %edx, %edx
callq 0x1120
movq %r13, %rdi
leaq 0x5b8(%rip), %rsi       # 0x2165
callq 0x1130
testq %rax, %rax
je 0x1c2e
movq %rax, %r13
movl 0x4(%r15,%rbx,8), %esi
movq %rbp, %rdi
movq %rax, %rdx
callq 0x161c
movq %r13, %rdi
callq 0x1090
cmpw $0x1, %r12w
adcw $0x0, %r14w
movw %r14w, 0xc(%rsp)
movq 0x10(%rsp), %rdi
callq 0x1030
movq 0x20(%rsp), %rdi
callq 0x1030
movl 0x1c(%rsp), %ebp
incq %rbx
cmpq %rbx, 0x48(%rsp)
jne 0x1aef
movq %r15, %rdi
callq 0x1030
movq 0x28(%rsp), %rdi
callq 0x1030
movl %ebp, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x533(%rip), %rdi       # 0x2168
leaq 0x60(%rsp), %rsi
xorl %eax, %eax
callq 0x10b0
movl $0x1, %ebp
jmp 0x1c1a