// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
leaq 0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1070
movq -0x58(%rbp), %rdi
addq $0x1, %rax
movq %rax, -0x60(%rbp)
callq 0x1070
movq -0x60(%rbp), %rdi
addq %rax, %rdi
addq $0x1, %rdi
addq $0x10, %rdi
addq $0x1, %rdi
callq 0x10b0
movq -0x58(%rbp), %rcx
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq 0xcf8(%rip), %rsi       # 0x20b4
movb $0x0, %al
callq 0x10f0
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
callq 0x10a0
movq -0x50(%rbp), %rsi
movq %rax, %rdx
leaq 0xc70(%rip), %rdi       # 0x2052
movb $0x0, %al
callq 0x1080
movq -0x48(%rbp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1040
movl $0x0, -0x38(%rbp)
movq -0x58(%rbp), %rcx
movl -0x38(%rbp), %eax
cmpl 0x10(%rcx), %eax
jae 0x151b
movq -0x58(%rbp), %rax
movq -0x8(%rbp), %rdi
movl 0x14(%rax), %eax
shll $0xb, %eax
movl -0x38(%rbp), %ecx
shll $0x5, %ecx
addl %ecx, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10c0
movq -0x8(%rbp), %rcx
leaq -0x34(%rbp), %rdi
movl $0x20, %esi
movl $0x1, %edx
callq 0x1050
cmpb $0x0, -0x34(%rbp)
jne 0x146c
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %esi
addl $0x1, %esi
movl 0x10(%rax), %edx
leaq 0xc2c(%rip), %rdi       # 0x208c
movb $0x0, %al
callq 0x1080
jmp 0x150d
cmpl $0x0, -0x14(%rbp)
je 0x147b
movl -0x24(%rbp), %eax
subl $0x18, %eax
movl %eax, -0x24(%rbp)
movq -0x58(%rbp), %rcx
movq -0x48(%rbp), %rdi
movq -0x10(%rbp), %rdx
leaq -0x34(%rbp), %r8
leaq 0xc1f(%rip), %rsi       # 0x20b1
movb $0x0, %al
callq 0x10f0
movq -0x48(%rbp), %rsi
movl -0x24(%rbp), %edx
shll $0xb, %edx
leaq 0xc10(%rip), %rdi       # 0x20ba
movb $0x0, %al
callq 0x1080
movq -0x48(%rbp), %rdi
leaq 0xc18(%rip), %rsi       # 0x20d4
callq 0x10d0
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
jne 0x14df
movq -0x48(%rbp), %rsi
leaq 0xc01(%rip), %rdi       # 0x20d7
movb $0x0, %al
callq 0x1080
jmp 0x1524
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %eax
shll $0xb, %eax
movl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x10c0
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %esi
movq -0x40(%rbp), %rdx
callq 0x1210
movq -0x40(%rbp), %rdi
callq 0x1060
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x13fe
movq -0x48(%rbp), %rdi
callq 0x1030
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)