// File: /workspace/github_c_makefile_star2_O3/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, 0x8(%rsp)
movl %ecx, 0x4(%rsp)
movq %rdx, %rcx
movq %rsi, %rdx
movq %rdi, %r15
leaq 0xcc2(%rip), %rsi       # 0x2004
leaq 0x430(%rsp), %rbx
movq %rbx, %rdi
movq %rcx, 0x28(%rsp)
xorl %eax, %eax
callq 0x1160
leaq 0xcae(%rip), %rsi       # 0x200e
movq %rbx, %rdi
callq 0x1130
testq %rax, %rax
je 0x15e3
movq %rax, %r13
movl $0x0, (%rsp)
leaq 0x230(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movq %rax, %rdx
callq 0x10e0
testq %rax, %rax
je 0x13ff
leaq 0xc90(%rip), %rbx       # 0x202c
leaq 0x230(%rsp), %r14
leaq 0x30(%rsp), %r12
leaq 0xcf3(%rip), %rbp       # 0x20a3
movl $0x0, (%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x1110
testl %eax, %eax
jle 0x13dd
cmpw $0x2f2f, 0x30(%rsp)     # imm = 0x2F2F
jne 0x13e7
leaq 0xcd7(%rip), %rdi       # 0x20b2
jmp 0x13e0
movq %rbp, %rdi
callq 0x1060
jmp 0x13ea
incl (%rsp)
movq %r14, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r13, %rdx
callq 0x10e0
testq %rax, %rax
jne 0x13b7
leaq 0xc2e(%rip), %rdi       # 0x2034
movl (%rsp), %ebx
movl %ebx, %esi
xorl %eax, %eax
callq 0x10a0
movq %r13, %rdi
callq 0x10b0
movl %ebx, %ecx
movq %rcx, %rax
shlq $0x8, %rax
movq %rcx, 0x20(%rsp)
leaq (%rax,%rcx,8), %rdi
callq 0x1100
movq %r15, 0x10(%rsp)
movq %rax, (%r15)
testl %ebx, %ebx
je 0x15be
leaq 0x230(%rsp), %rbp
leaq 0xbdc(%rip), %r12       # 0x202c
leaq 0x30(%rsp), %r14
leaq 0xbef(%rip), %rbx       # 0x204b
movq $0x0, 0x18(%rsp)
movq %rbp, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r13, %rdx
callq 0x10e0
movq %rbp, %rdi
movq %r12, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x1110
testl %eax, %eax
jle 0x1465
movzwl 0x30(%rsp), %eax
cmpl $0x2f2f, %eax           # imm = 0x2F2F
je 0x1465
jmp 0x149d
movb $0x0, 0x30(%rsp,%r15)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10d0
movq %rax, %r15
movq %r14, %rdi
callq 0x1090
cmpq %rax, %r15
jb 0x1497
movq 0x10(%rsp), %rbx
movq (%rbx), %rax
movq 0x18(%rsp), %rdx
movq %rdx, %rcx
shlq $0x8, %rcx
leaq (%rcx,%rdx,8), %r15
leaq (%rax,%r15), %rdi
addq $0x8, %rdi
movl $0x100, %edx            # imm = 0x100
movq %r14, %rsi
callq 0x1040
movq %r14, %rdi
leaq 0xc7a(%rip), %rsi       # 0x2169
callq 0x1150
testb $0x1, 0x4(%rsp)
jne 0x1500
movq %rax, %rdi
jmp 0x150b
movq (%rbx), %rax
leaq (%rax,%r15), %rdi
addq $0x8, %rdi
callq *0x8(%rsp)
movq (%rbx), %rcx
movl %eax, (%rcx,%r15)
xorl %edi, %edi
leaq 0xc4a(%rip), %rsi       # 0x2169
callq 0x1150
testb $0x2, 0x4(%rsp)
je 0x152f
movb $0x0, 0x1(%rax)
testb $0x4, 0x4(%rsp)
leaq 0xb10(%rip), %rbx       # 0x204b
jne 0x155a
movq %rax, %rdi
callq *0x8(%rsp)
movl %eax, %r8d
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl %r8d, 0x4(%rax,%r15)
addl $-0x61, %r8d
jmp 0x1571
movq 0x10(%rsp), %rax
movq (%rax), %rax
movl $0xffffffff, 0x4(%rax,%r15) # imm = 0xFFFFFFFF
movl $0xffffff9e, %r8d       # imm = 0xFFFFFF9E
leaq (%rax,%r15), %rcx
addq $0x4, %rcx
testb $0x8, 0x4(%rsp)
jne 0x1585
movl (%rcx), %r8d
jmp 0x1588
movl %r8d, (%rcx)
leaq (%rax,%r15), %rdx
addq $0x8, %rdx
movl -0x8(%rdx), %ecx
leaq 0xab4(%rip), %rdi       # 0x204e
movq 0x28(%rsp), %rsi
xorl %eax, %eax
callq 0x10a0
movq 0x18(%rsp), %rcx
incq %rcx
movq %rcx, 0x18(%rsp)
cmpq 0x20(%rsp), %rcx
jne 0x1465
leaq 0xab9(%rip), %rdi       # 0x207e
leaq 0x430(%rsp), %rdx
movl (%rsp), %ebx
movl %ebx, %esi
xorl %eax, %eax
callq 0x10a0
movq %r13, %rdi
callq 0x1080
jmp 0x1605
leaq 0xa26(%rip), %rdi       # 0x2010
leaq 0x430(%rsp), %rsi
xorl %eax, %eax
callq 0x10a0
movq $0x0, (%r15)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x638, %rsp            # imm = 0x638
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)