// File: /workspace/github_c_makefile_star2_O1/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
movq %rdi, 0x28(%rsp)
shlq $0x4, %rdi
callq 0x5160
movq %rax, 0x18(%rsp)
testl %ebp, %ebp
je 0x5e83
shlq $0x4, 0x28(%rsp)
xorl %r12d, %r12d
movl %ebx, 0x24(%rsp)
movq 0x8(%rsp), %rax
movl 0x8(%rax,%r12), %esi
shll $0xb, %esi
movq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x5190
movq 0x18(%rsp), %rax
leaq (%rax,%r12), %r13
movl $0x4, %esi
movl $0x1, %edx
movq %r13, %rdi
movq %r14, %rcx
callq 0x5080
testb %bl, %bl
jns 0x5d8c
movq 0x8(%rsp), %rax
movq (%rax,%r12), %rdx
movq %r13, %rdi
movl $0x1, %esi
movl %ebx, %ecx
callq 0x5b5d
testb $0x14, %bl
je 0x5d9b
movl (%r13), %eax
bswapl %eax
movl %eax, (%r13)
testb $0x3, %bl
je 0x5da5
shll $0x3, (%r13)
testb $0x8, %bl
je 0x5db8
movl (%r13), %eax
shll $0x2, %eax
leal (%rax,%rax,2), %eax
movl %eax, (%r13)
testb $0x4, %bl
je 0x5dc2
shll $0x4, (%r13)
movq 0x18(%rsp), %r15
movl (%r15,%r12), %r13d
leal 0x4(%r13), %r14d
movl %r14d, (%r15,%r12)
movq %r13, %rdi
callq 0x5160
movq %rax, %rbp
movq %rax, 0x8(%r15,%r12)
testb %bl, %bl
js 0x5dfe
movl $0x1, %edx
movq %rbp, %rdi
movq %r13, %rsi
movq 0x10(%rsp), %rcx
callq 0x5080
jmp 0x5e74
movl %r14d, %edi
movq %rdi, 0x30(%rsp)
callq 0x5160
movq %rax, %r15
movq 0x8(%rsp), %rax
movl 0x8(%rax,%r12), %esi
shll $0xb, %esi
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x5190
movl $0x1, %edx
movq %r15, %rdi
movq 0x30(%rsp), %rsi
movq %rbx, %rcx
movl 0x24(%rsp), %ebx
callq 0x5080
shrl $0x2, %r14d
movq 0x8(%rsp), %rax
movq (%rax,%r12), %rdx
movq %r15, %rdi
movl %r14d, %esi
movl %ebx, %ecx
callq 0x5b5d
leaq 0x4(%r15), %rsi
movq %rbp, %rdi
movq %r13, %rdx
callq 0x5150
movq %r15, %rdi
callq 0x5030
addq $0x10, %r12
cmpq %r12, 0x28(%rsp)
jne 0x5d36
movq 0x18(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq