// File: /workspace/github_c_makefile_star2_O3/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, %ebx
movl %edx, %ebp
movq %rsi, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
movl %edx, %edi
shlq $0x4, %rdi
movq %rdi, 0x30(%rsp)
callq 0x5170
movq %rax, 0x18(%rsp)
testl %ebp, %ebp
je 0x5e5c
movl %ebx, %eax
andl $0x4, %eax
movl %eax, 0x24(%rsp)
xorl %r13d, %r13d
movl %ebx, 0x20(%rsp)
movq 0x8(%rsp), %rax
movl 0x8(%rax,%r13), %esi
shll $0xb, %esi
movq 0x10(%rsp), %r12
movq %r12, %rdi
xorl %edx, %edx
callq 0x51a0
movq 0x18(%rsp), %rax
leaq (%rax,%r13), %r14
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r12, %rcx
callq 0x5080
testb %bl, %bl
jns 0x5d6e
movq 0x8(%rsp), %rax
movq (%rax,%r13), %rdx
movq %r14, %rdi
movl $0x1, %esi
movl %ebx, %ecx
callq 0x5b4e
testb $0x14, %bl
movq 0x18(%rsp), %r15
movl (%r15,%r13), %eax
movl %eax, %ecx
bswapl %ecx
cmovel %eax, %ecx
leal (,%rcx,8), %eax
testb $0x3, %bl
cmovel %ecx, %eax
leal (,%rax,4), %ecx
testb $0x8, %bl
leal (%rcx,%rcx,2), %ebp
cmovel %eax, %ebp
movl 0x24(%rsp), %ecx
shll %cl, %ebp
leal 0x4(%rbp), %r14d
movl %r14d, (%r15,%r13)
movq %rbp, %rdi
callq 0x5170
movq %rax, %r12
movq %rax, 0x8(%r15,%r13)
testb %bl, %bl
js 0x5dd7
movl $0x1, %edx
movq %r12, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rcx
callq 0x5080
jmp 0x5e4d
movl %r14d, %edi
movq %rdi, 0x28(%rsp)
callq 0x5170
movq %rax, %r15
movq 0x8(%rsp), %rax
movl 0x8(%rax,%r13), %esi
shll $0xb, %esi
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x51a0
movl $0x1, %edx
movq %r15, %rdi
movq 0x28(%rsp), %rsi
movq %rbx, %rcx
movl 0x20(%rsp), %ebx
callq 0x5080
shrl $0x2, %r14d
movq 0x8(%rsp), %rax
movq (%rax,%r13), %rdx
movq %r15, %rdi
movl %r14d, %esi
movl %ebx, %ecx
callq 0x5b4e
leaq 0x4(%r15), %rsi
movq %r12, %rdi
movq %rbp, %rdx
callq 0x5160
movq %r15, %rdi
callq 0x5030
addq $0x10, %r13
cmpq %r13, 0x30(%rsp)
jne 0x5d18
movq 0x18(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq