// File: /workspace/github_c_makefile_star2_O1/Joy-Division[P]tools-zoe/dat-extract.c
// Function: processDirectoryListing
void processDirectoryListing( FILE *f, tocDirectoryEntry entry, char *curpath, unsigned int tgsfix ) {
	tocFileEntry file;
	unsigned int i;
	FILE *o;
	char *filepath = NULL;
	
	filepath = malloc(strlen(curpath)+1+strlen(entry.name)+1+16+1); // max filename length is 16
	sprintf(filepath, "%s/%s", curpath, entry.name);
	
	printf("processDirectoryListing - Current directory: %s at %08lx\n", filepath, ftell(f) );
	createDirectory( filepath );
	for( i = 0; i < entry.subFileCount; i++ ) {
		fseek( f, (entry.subFileOffset*BLOCKSIZE)+(32*i), SEEK_SET );
		fread( &file, sizeof(file), 1, f );
		if(!file.name[0]) {
			printf("skipping invalid toc entry %d of %d\n", i+1, entry.subFileCount);
			continue;
		}
		if( tgsfix ) file.fileOffset -= 0x18;
		sprintf(filepath, "%s/%s/%s", curpath, entry.name, file.name);
		printf("Current file: %s at %08x\n", filepath, file.fileOffset*BLOCKSIZE );
		if( !(o = fopen( filepath, "wb" ))) {
			printf("Couldnt open file %s\n", filepath);
			return;
		}
		fseek( f, file.fileOffset*BLOCKSIZE, SEEK_SET );
		writeFile( f, file.fileSize, o );
		fclose(o);
	}
	free(filepath);
	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, 0x14(%rsp)
movq %rsi, %r14
movq %rdi, %r15
leaq 0x80(%rsp), %r12
movq %rsi, %rdi
callq 0x1080
movq %rax, %rbx
movq %r12, %rdi
callq 0x1080
leaq (%rbx,%rax), %rdi
addq $0x13, %rdi
callq 0x10b0
movq %rax, %rbx
leaq 0xd8f(%rip), %rsi       # 0x20b4
movq %rax, %rdi
movq %r14, 0x20(%rsp)
movq %r14, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x10f0
movq %r15, %rdi
callq 0x10a0
leaq 0xd09(%rip), %rdi       # 0x2052
movq %rbx, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1090
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1040
cmpl $0x0, 0x10(%r12)
je 0x1478
movl $0x1, %ebx
leaq 0x28(%rsp), %r14
xorl %ebp, %ebp
movq %r15, 0x18(%rsp)
movl 0x14(%r12), %esi
shll $0xb, %esi
addl %ebp, %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x10c0
movl $0x20, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x1060
cmpb $0x0, 0x28(%rsp)
je 0x1450
cmpl $0x0, 0x14(%rsp)
je 0x13c5
addl $-0x18, 0x38(%rsp)
movq 0x8(%rsp), %r15
movq %r15, %rdi
leaq 0xcdd(%rip), %rsi       # 0x20b1
movq 0x20(%rsp), %rdx
movq %r12, %r13
movq %r12, %rcx
movq %r14, %r12
movq %r14, %r8
xorl %eax, %eax
callq 0x10f0
movl 0x38(%rsp), %edx
shll $0xb, %edx
leaq 0xcc0(%rip), %rdi       # 0x20ba
movq %r15, %rsi
xorl %eax, %eax
callq 0x1090
movq %r15, %rdi
leaq 0xcc6(%rip), %rsi       # 0x20d4
callq 0x10d0
testq %rax, %rax
je 0x1484
movq %rax, %r14
movl 0x38(%rsp), %esi
shll $0xb, %esi
movq 0x18(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x10c0
movl 0x3c(%rsp), %esi
movq %r15, %rdi
movq %r14, %rdx
callq 0x120c
movq %r14, %rdi
callq 0x1070
movq %r12, %r14
movq %r13, %r12
jmp 0x1465
movl 0x10(%r12), %edx
leaq 0xc30(%rip), %rdi       # 0x208c
movl %ebx, %esi
xorl %eax, %eax
callq 0x1090
addl $0x20, %ebp
leal 0x1(%rbx), %eax
cmpl 0x10(%r12), %ebx
movl %eax, %ebx
jb 0x1385
movq 0x8(%rsp), %rdi
callq 0x1030
jmp 0x1497
leaq 0xc4c(%rip), %rdi       # 0x20d7
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x1090
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq