// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movl -0x14(%rbp), %eax
movl %eax, %edi
shlq $0x4, %rdi
callq 0x5160
movq %rax, -0x20(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0x6521
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shll $0xb, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x5190
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x8(%rbp), %rcx
movl $0x4, %esi
movl $0x1, %edx
callq 0x5080
movl -0x18(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x6320
movq -0x20(%rbp), %rdi
movl -0x24(%rbp), %eax
shlq $0x4, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movl -0x18(%rbp), %ecx
movl $0x1, %esi
callq 0x5fb0
movl -0x18(%rbp), %eax
andl $0x14, %eax
cmpl $0x0, %eax
je 0x6352
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %edi
callq 0x6530
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %edx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x18(%rbp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x6372
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %ecx
shll $0x3, %ecx
movl %ecx, (%rax)
movl -0x18(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x6390
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
imull $0xc, (%rax), %ecx
movl %ecx, (%rax)
movl -0x18(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x63b0
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %ecx
shll $0x4, %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %ecx
addl $0x4, %ecx
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
subl $0x4, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x5160
movq %rax, %rcx
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %edx
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
movl -0x18(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x64da
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, %edi
callq 0x5160
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
shll $0xb, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x5190
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, %esi
movq -0x8(%rbp), %rcx
movl $0x1, %edx
callq 0x5080
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %esi
shrl $0x2, %esi
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rdx
movl -0x18(%rbp), %ecx
callq 0x5fb0
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
movq -0x30(%rbp), %rsi
addq $0x4, %rsi
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
subl $0x4, %eax
movl %eax, %eax
movl %eax, %edx
callq 0x5150
movq -0x30(%rbp), %rdi
callq 0x5030
jmp 0x6511
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdi
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
subl $0x4, %eax
movl %eax, %eax
movl %eax, %esi
movq -0x8(%rbp), %rcx
movl $0x1, %edx
callq 0x5080
jmp 0x6513
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x6297
movq -0x20(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)