// File: /workspace/github_c_makefile_star2_O1/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 $0x648, %rsp            # imm = 0x648
movq %r8, 0x10(%rsp)
movl %ecx, 0xc(%rsp)
movq %rdx, %rcx
movq %rsi, %rdx
movq %rdi, %r14
leaq 0xcc2(%rip), %rsi       # 0x2004
leaq 0x440(%rsp), %rbx
movq %rbx, %rdi
movq %rcx, 0x38(%rsp)
xorl %eax, %eax
callq 0x1160
leaq 0xcae(%rip), %rsi       # 0x200e
movq %rbx, %rdi
callq 0x1130
testq %rax, %rax
je 0x15c5
movq %rax, %r13
movq %r14, 0x18(%rsp)
leaq 0x240(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movq %rax, %rdx
callq 0x10e0
testq %rax, %rax
movl $0x0, %ebx
je 0x13fc
leaq 0xc8d(%rip), %r14       # 0x202c
leaq 0x240(%rsp), %r15
leaq 0x40(%rsp), %r12
leaq 0xcf0(%rip), %rbp       # 0x20a3
xorl %ebx, %ebx
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x1110
testl %eax, %eax
jle 0x13db
cmpw $0x2f2f, 0x40(%rsp)     # imm = 0x2F2F
jne 0x13e5
leaq 0xcd9(%rip), %rdi       # 0x20b2
jmp 0x13de
movq %rbp, %rdi
callq 0x1060
jmp 0x13e7
incl %ebx
movq %r15, %rdi
movl $0x200, %esi            # imm = 0x200
movq %r13, %rdx
callq 0x10e0
testq %rax, %rax
jne 0x13b5
leaq 0xc31(%rip), %rdi       # 0x2034
movl %ebx, %esi
xorl %eax, %eax
callq 0x10a0
movq %r13, %rdi
callq 0x10b0
movl %ebx, %ecx
movq %rcx, %rax
shlq $0x8, %rax
movq %rcx, 0x30(%rsp)
leaq (%rax,%rcx,8), %rdi
callq 0x1100
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movl %ebx, 0x2c(%rsp)
testl %ebx, %ebx
je 0x159f
leaq 0x240(%rsp), %rbp
leaq 0xbde(%rip), %r12       # 0x202c
leaq 0x40(%rsp), %r14
leaq 0xbf1(%rip), %r15       # 0x204b
movq $0x0, 0x20(%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 0x1463
movzwl 0x40(%rsp), %eax
cmpl $0x2f2f, %eax           # imm = 0x2F2F
je 0x1463
jmp 0x149a
movb $0x0, 0x40(%rsp,%rbx)
movq %r14, %rdi
movq %r15, %rsi
callq 0x10d0
movq %rax, %rbx
movq %r14, %rdi
callq 0x1090
cmpq %rax, %rbx
jb 0x1495
movq 0x18(%rsp), %r15
movq (%r15), %rax
movq 0x20(%rsp), %rdx
movq %rdx, %rcx
shlq $0x8, %rcx
leaq (%rcx,%rdx,8), %rbx
leaq (%rax,%rbx), %rdi
addq $0x8, %rdi
movl $0x100, %edx            # imm = 0x100
movq %r14, %rsi
callq 0x1040
movq %r14, %rdi
leaq 0xc7d(%rip), %rsi       # 0x2169
callq 0x1150
testb $0x1, 0xc(%rsp)
jne 0x14fd
movq %rax, %rdi
jmp 0x1508
movq (%r15), %rax
leaq (%rax,%rbx), %rdi
addq $0x8, %rdi
callq *0x10(%rsp)
movq (%r15), %rcx
movl %eax, (%rcx,%rbx)
xorl %edi, %edi
leaq 0xc4e(%rip), %rsi       # 0x2169
callq 0x1150
testb $0x2, 0xc(%rsp)
je 0x152b
movb $0x0, 0x1(%rax)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
testb $0x4, 0xc(%rsp)
leaq 0xb0f(%rip), %r15       # 0x204b
jne 0x1547
movq %rax, %rdi
callq *0x10(%rsp)
movl %eax, %ecx
movq 0x18(%rsp), %rdx
movq (%rdx), %rax
movl %ecx, 0x4(%rax,%rbx)
testb $0x8, 0xc(%rsp)
je 0x1562
movq (%rdx), %rax
addl $-0x61, 0x4(%rax,%rbx)
movq (%rdx), %rax
leaq (%rax,%rbx), %rdx
addq $0x8, %rdx
movl -0x8(%rdx), %ecx
movl -0x4(%rdx), %r8d
leaq 0xad3(%rip), %rdi       # 0x204e
movq 0x38(%rsp), %rsi
xorl %eax, %eax
callq 0x10a0
movq 0x20(%rsp), %rcx
incq %rcx
movq %rcx, 0x20(%rsp)
cmpq 0x30(%rsp), %rcx
jne 0x1463
leaq 0xad8(%rip), %rdi       # 0x207e
leaq 0x440(%rsp), %rdx
movl 0x2c(%rsp), %ebx
movl %ebx, %esi
xorl %eax, %eax
callq 0x10a0
movq %r13, %rdi
callq 0x1080
jmp 0x15e7
leaq 0xa44(%rip), %rdi       # 0x2010
leaq 0x440(%rsp), %rsi
xorl %eax, %eax
callq 0x10a0
movq $0x0, (%r14)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x648, %rsp            # imm = 0x648
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop