// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-mgs/stage-dictionary.c
// Function: loaddic
int loaddic(dicentry **dic, char *path, char *name, unsigned int flags, uint32_t (*hfunc)(char *)) {
	FILE *f;
	unsigned int i = 0, countedlines = 0;
	char dicname[512], scanline[512], filterline[512], *token = NULL;
	sprintf(dicname, "%s/%s.txt", path, name);
	
	if( !(f = fopen( dicname, "r" ))) {
		printf("Couldnt open dictionary %s\n", dicname);
		*dic = NULL;
		return -1;
	}
	
	while(fgets(scanline, 512, f)) {
		if(sscanf(scanline, "%s//%*s", filterline) > 0) {
			if(!(strncmp(filterline, "//", 2))) printf("skipping comment\n");
			else countedlines++;
		}
		else printf("skipping empty\n");
	}
	
	printf("found %u useful lines\n", countedlines);
	
	rewind(f);
	
	*dic = malloc(sizeof(dicentry)*countedlines);
	for(i = 0; i < countedlines; ) {
		fgets(scanline, 512, f);
		
		if(sscanf(scanline, "%s//%*s", filterline) > 0) {
			if(!(strncmp(filterline, "//", 2))) continue;
		}
		else continue;
		
		while(strcspn(filterline, "\r\n") < strlen(filterline)) filterline[strcspn(filterline,"\r\n")] = 0;
		strncpy((*dic)[i].name, filterline, 256);
		
		token = strtok(filterline, ".");
		
		if(flags & DIC_HASH_FULL_NAME) (*dic)[i].hash = (*hfunc)((*dic)[i].name);
		else (*dic)[i].hash = (*hfunc)(token);
		
		token = strtok(NULL, ".");
		
		if(flags & DIC_HASH_SINGLE_EXT) token[1] = 0;
		if(flags & DIC_USE_EXTERNAL) (*dic)[i].extension = 0xFFFFFFFF;
		else (*dic)[i].extension = (*hfunc)(token);
		if(flags & DIC_ADJUST_EXT_MGS2) (*dic)[i].extension -= 0x61; // 'a'
		
		printf("Added entry to dictionary %s: %s (%08x - %08x)\n", name, (*dic)[i].name, (*dic)[i].hash, (*dic)[i].extension);
		i++;
	}
	printf("Read %d entries from dictionary %s.\n", countedlines, dicname);
	
	fclose(f);
	
	return countedlines;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x638, %rsp            # imm = 0x638
movq %r8, 0x10(%rsp)
movl %ecx, (%rsp)
movq %rdx, %rcx
movq %rsi, %rdx
movq %rdi, %r14
leaq 0xce3(%rip), %rsi       # 0x2004
leaq 0x230(%rsp), %rbx
movq %rbx, %rdi
movq %rcx, 0x20(%rsp)
xorl %eax, %eax
callq 0x1160
leaq 0xccf(%rip), %rsi       # 0x200e
movq %rbx, %rdi
callq 0x1130
testq %rax, %rax
je 0x1593
movq %rax, %r12
movq %r14, 0x8(%rsp)
leaq 0x430(%rsp), %rbx
leaq 0xcc5(%rip), %r14       # 0x202c
leaq 0x30(%rsp), %r15
leaq 0xd3f(%rip), %r13       # 0x20b2
leaq 0xd29(%rip), %rbp       # 0x20a3
movl $0x0, 0x4(%rsp)
movq %rbx, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r12, %rdx
callq 0x10e0
testq %rax, %rax
je 0x13ce
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x1110
testl %eax, %eax
jle 0x13be
cmpw $0x2f2f, 0x30(%rsp)     # imm = 0x2F2F
jne 0x13c8
movq %r13, %rdi
callq 0x1060
jmp 0x1382
movq %rbp, %rdi
callq 0x1060
jmp 0x1382
incl 0x4(%rsp)
jmp 0x1382
leaq 0xc5f(%rip), %rdi       # 0x2034
movl 0x4(%rsp), %ebx
movl %ebx, %esi
xorl %eax, %eax
callq 0x10a0
movq %r12, %rdi
callq 0x10b0
movl %ebx, %eax
movq %rax, 0x28(%rsp)
imulq $0x108, %rax, %rdi      # imm = 0x108
callq 0x1100
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x430(%rsp), %r13
leaq 0xc18(%rip), %r15       # 0x202c
leaq 0x30(%rsp), %r14
leaq 0xc2b(%rip), %rbx       # 0x204b
xorl %eax, %eax
cmpq 0x28(%rsp), %rax
je 0x156d
movq %rax, 0x18(%rsp)
movq %r13, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r12, %rdx
callq 0x10e0
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1110
testl %eax, %eax
jle 0x1432
cmpw $0x2f2f, 0x30(%rsp)     # imm = 0x2F2F
je 0x1432
jmp 0x1466
movb $0x0, 0x30(%rsp,%rbp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10d0
movq %rax, %rbp
movq %r14, %rdi
callq 0x1090
cmpq %rax, %rbp
jb 0x1461
movq 0x8(%rsp), %rax
movq (%rax), %rax
imulq $0x108, 0x18(%rsp), %rbp # imm = 0x108
leaq (%rax,%rbp), %rdi
addq $0x8, %rdi
movl $0x100, %edx            # imm = 0x100
movq %r14, %rsi
callq 0x1040
movq %r14, %rdi
leaq 0xcb8(%rip), %rsi       # 0x2169
callq 0x1150
testb $0x1, (%rsp)
jne 0x14c1
movq %rax, %rdi
jmp 0x14d1
movq 0x8(%rsp), %rax
movq (%rax), %rax
leaq (%rax,%rbp), %rdi
addq $0x8, %rdi
callq *0x10(%rsp)
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movl %eax, (%rcx,%rbp)
xorl %edi, %edi
leaq 0xc80(%rip), %rsi       # 0x2169
callq 0x1150
testb $0x2, (%rsp)
je 0x14f8
movb $0x0, 0x1(%rax)
testb $0x4, (%rsp)
jne 0x151b
movq %rax, %rdi
callq *0x10(%rsp)
movl %eax, %r8d
movq 0x8(%rsp), %rax
movq (%rax), %rax
movl %r8d, 0x4(%rax,%rbp)
addl $-0x61, %r8d
jmp 0x152c
movq 0x8(%rsp), %rax
movq (%rax), %rax
orl $-0x1, 0x4(%rax,%rbp)
pushq $-0x62
popq %r8
leaq (%rax,%rbp), %rcx
addq $0x4, %rcx
testb $0x8, (%rsp)
jne 0x153f
movl (%rcx), %r8d
jmp 0x1542
movl %r8d, (%rcx)
leaq (%rax,%rbp), %rdx
addq $0x8, %rdx
movl -0x8(%rdx), %ecx
leaq 0xafa(%rip), %rdi       # 0x204e
movq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x10a0
movq 0x18(%rsp), %rax
incq %rax
jmp 0x1422
leaq 0xb0a(%rip), %rdi       # 0x207e
leaq 0x230(%rsp), %rdx
movl 0x4(%rsp), %ebx
movl %ebx, %esi
xorl %eax, %eax
callq 0x10a0
movq %r12, %rdi
callq 0x1080
jmp 0x15b0
leaq 0xa76(%rip), %rdi       # 0x2010
leaq 0x230(%rsp), %rsi
xorl %eax, %eax
callq 0x10a0
andq $0x0, (%r14)
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x638, %rsp            # imm = 0x638
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq