// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-mgs/dat-extract_enc.c
// Function: processStages
stageinfo *processStages( FILE *f, tocStageEntry *stages, unsigned int numstages, unsigned int flags) {
	stageinfo *retinfo = malloc(sizeof(stageinfo)*numstages);
	unsigned int i;
	unsigned char *tempdata = NULL;
	for(i = 0; i < numstages; i++) {
		fseek(f, stages[i].offset*0x800, SEEK_SET);
		fread(&retinfo[i].size, 4, 1, f);
		if(flags & ENCRYPTED) decryptStage((unsigned char *)&retinfo[i].size, 1, stages[i].name, flags);
		
		if(flags & IS_BIGENDIAN) retinfo[i].size = bswap_32(retinfo[i].size);
		
		if(flags & IS_SMALLENTRY) retinfo[i].size *= 8;
		if(flags & IS_BIGENTRY) retinfo[i].size *= 12;
		if(flags & IS_VERYBIGENTRY) retinfo[i].size *= 16;
		
		retinfo[i].size += 4;
		retinfo[i].data = malloc(retinfo[i].size-4);
		if(flags & ENCRYPTED) {
			tempdata = malloc(retinfo[i].size);
			/* if a STAGE, seek back, read _all_ data into memory and decrypt it */
			fseek(f, stages[i].offset*0x800, SEEK_SET);
			fread(tempdata, retinfo[i].size, 1, f);
			decryptStage(tempdata, (retinfo[i].size/4), stages[i].name, flags);
			memcpy(retinfo[i].data, tempdata+4, retinfo[i].size-4);
			free(tempdata);
		}
		/* if not a STAGE, just read the unencrypted data */
		else fread(retinfo[i].data, retinfo[i].size-4, 1, f);
	}
	return retinfo;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %ecx, %r13d
movq %rsi, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
movl %edx, %edi
shlq $0x4, %rdi
movq %rdi, 0x30(%rsp)
callq 0x5170
movq %rax, 0x18(%rsp)
movl %r13d, %eax
andl $0x4, %eax
movl %eax, 0x24(%rsp)
xorl %ebx, %ebx
movl %r13d, 0x20(%rsp)
cmpq %rbx, 0x30(%rsp)
je 0x5dba
movq 0x8(%rsp), %rax
movl 0x8(%rax,%rbx), %esi
shll $0xb, %esi
movq 0x10(%rsp), %r12
movq %r12, %rdi
xorl %edx, %edx
callq 0x51a0
movq 0x18(%rsp), %rax
leaq (%rax,%rbx), %r14
movq %r14, %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r12, %rcx
callq 0x5080
testb %r13b, %r13b
jns 0x5cd4
movq 0x8(%rsp), %rax
movq (%rax,%rbx), %rdx
movq %r14, %rdi
pushq $0x1
popq %rsi
movl %r13d, %ecx
callq 0x5ac9
testb $0x14, %r13b
movq 0x18(%rsp), %r15
movl (%r15,%rbx), %eax
movl %eax, %ecx
bswapl %ecx
cmovel %eax, %ecx
leal (,%rcx,8), %eax
testb $0x3, %r13b
cmovel %ecx, %eax
imull $0xc, %eax, %r12d
testb $0x8, %r13b
cmovel %eax, %r12d
movl 0x24(%rsp), %ecx
shll %cl, %r12d
leal 0x4(%r12), %r14d
movl %r14d, (%r15,%rbx)
movq %r12, %rdi
callq 0x5170
movq %rax, 0x8(%r15,%rbx)
testb %r13b, %r13b
js 0x5d39
movq %rax, %rdi
movq %r12, %rsi
pushq $0x1
popq %rdx
movq 0x10(%rsp), %rcx
callq 0x5080
jmp 0x5db1
movl %r14d, %r13d
movq %r13, %rdi
movq %rax, 0x28(%rsp)
callq 0x5170
movq %rax, %rbp
movq 0x8(%rsp), %rax
movl 0x8(%rax,%rbx), %esi
shll $0xb, %esi
movq 0x10(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x51a0
movq %rbp, %rdi
movq %r13, %rsi
movl 0x20(%rsp), %r13d
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x5080
shrl $0x2, %r14d
movq 0x8(%rsp), %rax
movq (%rax,%rbx), %rdx
movq %rbp, %rdi
movl %r14d, %esi
movl %r13d, %ecx
callq 0x5ac9
leaq 0x4(%rbp), %rsi
movq 0x28(%rsp), %rdi
movq %r12, %rdx
callq 0x5160
movq %rbp, %rdi
callq 0x5030
addq $0x10, %rbx
jmp 0x5c78
movq 0x18(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq