// File: /workspace/github_c_makefile_star2_O1/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, 0x30(%rsp)
movq %rdi, %r14
movq %rsi, %r12
movl %esi, %esi
xorl %edx, %edx
callq 0x1120
leaq 0x2c(%rsp), %rbx
movl $0x1, (%rbx)
leaq 0x4c(%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, 0x2c(%rsp)
jne 0x1931
movl %ebp, %ebx
leaq (,%rbx,8), %rdi
callq 0x1100
movq %rax, %r15
movq %rbx, 0x38(%rsp)
leaq (,%rbx,4), %rdi
callq 0x1100
movq %rax, 0x18(%rsp)
leal 0x4(%r12), %esi
xorl %ebx, %ebx
movq %r14, 0x20(%rsp)
movq %r14, %rdi
movq %rsi, (%rsp)
xorl %edx, %edx
callq 0x1120
testl %ebp, %ebp
je 0x1c18
leal 0x800(%r12), %esi
addl $0x10, %r12d
movq %r12, 0x40(%rsp)
xorl %r14d, %r14d
xorl %r12d, %r12d
movq %rsi, 0x8(%rsp)
movq (%rsp), %rax
leal (%rax,%r14,8), %esi
movq 0x20(%rsp), %r13
movq %r13, %rdi
xorl %edx, %edx
callq 0x1120
leaq (%r15,%r14,8), %rdi
movl $0x8, %esi
movl $0x1, %edx
movq %r13, %rcx
callq 0x1080
leaq (%r15,%r14,8), %r13
addq $0x4, %r13
cmpl $0x0, 0x4(%r15,%r14,8)
movl $0x1, %eax
cmovel %eax, %ebx
cmpl $0xff630000, (%r15,%r14,8) # imm = 0xFF630000
jne 0x1a45
movl $0x0, (%r13)
xorl %r12d, %r12d
movq 0x8(%rsp), %rsi
testl $0x7ff, %esi            # imm = 0x7FF
je 0x1af3
andl $0xfffff800, %esi       # imm = 0xFFFFF800
addl $0x800, %esi            # imm = 0x800
jmp 0x1af3
testl %ebx, %ebx
je 0x1abd
movq %rbx, 0x10(%rsp)
leal (,%r14,8), %esi
addl 0x40(%rsp), %esi
incl %r12d
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1120
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %rbx, %rcx
callq 0x1080
movl %r12d, %ecx
cmpl $0x2, %r12d
jb 0x1ab5
movl (%r13), %eax
movl %ecx, %r12d
movl %ecx, %ecx
negq %rcx
movq $-0x1, %rdx
leal (%r14,%rdx), %esi
subl 0x4(%r15,%rsi,8), %eax
movl %eax, 0x4(%r15,%r14,8)
decq %rdx
cmpq %rdx, %rcx
jne 0x1a9d
jmp 0x1ab8
movl %ecx, %r12d
movq 0x10(%rsp), %rbx
testl %ebx, %ebx
setne %cl
movq 0x18(%rsp), %rax
movq 0x8(%rsp), %rsi
movl %esi, (%rax,%r14,4)
movl (%r13), %eax
testl $0x7ff, %eax            # imm = 0x7FF
sete %dl
orb %cl, %dl
je 0x1ae6
addl %esi, %eax
movl %eax, %esi
jmp 0x1af5
andl $0xfffff800, %eax       # imm = 0xFFFFF800
addl %eax, %esi
addl $0x800, %esi            # imm = 0x800
xorl %ebx, %ebx
incq %r14
cmpq 0x38(%rsp), %r14
jne 0x19c6
movw $0x1, %ax
xorl %r14d, %r14d
movl (%r15,%r14,8), %r12d
cmpl $0xff630000, %r12d      # imm = 0xFF630000
je 0x1c0a
movw %ax, 0x8(%rsp)
movl %r12d, %ebx
shrl $0x18, %ebx
movl %r12d, %eax
shrl $0x10, %eax
movzbl %al, %edi
movl %edi, (%rsp)
callq 0x16fa
movq %rax, %r13
leaq 0x50(%rsp), %rdi
leaq 0x619(%rip), %rsi       # 0x215f
movq 0x30(%rsp), %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1160
leaq 0x50(%rsp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1060
movzwl 0x8(%rsp), %ecx
movzwl %r12w, %edi
movl %ebx, %esi
movl (%rsp), %edx
callq 0x1780
leaq 0x50(%rsp), %rdi
leaq 0x5d9(%rip), %rsi       # 0x215c
movq 0x30(%rsp), %rdx
movq %r13, (%rsp)
movq %r13, %rcx
movq %rax, 0x10(%rsp)
movq %rax, %r8
xorl %eax, %eax
callq 0x1160
movq 0x18(%rsp), %rax
movl (%rax,%r14,4), %esi
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1120
leaq 0x50(%rsp), %rdi
leaq 0x5a3(%rip), %rsi       # 0x2165
callq 0x1130
testq %rax, %rax
je 0x1c2c
movq %rax, %r13
movl 0x4(%r15,%r14,8), %esi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x15fc
movq %r13, %rdi
callq 0x1090
cmpw $0x1, %r12w
movzwl 0x8(%rsp), %ebx
adcw $0x0, %bx
movq (%rsp), %rdi
callq 0x1030
movq 0x10(%rsp), %rdi
callq 0x1030
movl %ebx, %eax
incq %r14
cmpq %r14, 0x38(%rsp)
jne 0x1b0a
movq %r15, %rdi
callq 0x1030
movq 0x18(%rsp), %rdi
callq 0x1030
jmp 0x1c44
leaq 0x535(%rip), %rdi       # 0x2168
leaq 0x50(%rsp), %rsi
xorl %eax, %eax
callq 0x10b0
movl $0x1, %ebp
movl %ebp, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq