// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x650, %rsp            # imm = 0x650
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq %r8, -0x30(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movq $0x0, -0x648(%rbp)
leaq -0x240(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq 0xbe7(%rip), %rsi       # 0x2004
movb $0x0, %al
callq 0x1160
leaq -0x240(%rbp), %rdi
leaq 0xbdc(%rip), %rsi       # 0x200e
callq 0x1130
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x146d
leaq -0x240(%rbp), %rsi
leaq 0xbc1(%rip), %rdi       # 0x2010
movb $0x0, %al
callq 0x10a0
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x17c4
jmp 0x146f
leaq -0x440(%rbp), %rdi
movq -0x38(%rbp), %rdx
movl $0x200, %esi            # imm = 0x200
callq 0x10e0
cmpq $0x0, %rax
je 0x14f6
leaq -0x440(%rbp), %rdi
leaq -0x640(%rbp), %rdx
leaq 0xb8d(%rip), %rsi       # 0x202c
movb $0x0, %al
callq 0x1110
cmpl $0x0, %eax
jle 0x14e3
leaq -0x640(%rbp), %rdi
leaq 0xb7b(%rip), %rsi       # 0x2034
movl $0x2, %edx
callq 0x1050
cmpl $0x0, %eax
jne 0x14d8
leaq 0xb68(%rip), %rdi       # 0x2037
movb $0x0, %al
callq 0x10a0
jmp 0x14e1
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x14f1
leaq 0xb5f(%rip), %rdi       # 0x2049
movb $0x0, %al
callq 0x10a0
jmp 0x146f
movl -0x40(%rbp), %esi
leaq 0xb59(%rip), %rdi       # 0x2059
movb $0x0, %al
callq 0x10a0
movq -0x38(%rbp), %rdi
callq 0x10b0
movl -0x40(%rbp), %eax
imulq $0x108, %rax, %rdi      # imm = 0x108
callq 0x1100
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x179d
leaq -0x440(%rbp), %rdi
movq -0x38(%rbp), %rdx
movl $0x200, %esi            # imm = 0x200
callq 0x10e0
leaq -0x440(%rbp), %rdi
leaq -0x640(%rbp), %rdx
leaq 0xac6(%rip), %rsi       # 0x202c
movb $0x0, %al
callq 0x1110
cmpl $0x0, %eax
jle 0x1593
leaq -0x640(%rbp), %rdi
leaq 0xab4(%rip), %rsi       # 0x2034
movl $0x2, %edx
callq 0x1050
cmpl $0x0, %eax
jne 0x1591
jmp 0x1530
jmp 0x1595
jmp 0x1530
jmp 0x1597
leaq -0x640(%rbp), %rdi
leaq 0xacb(%rip), %rsi       # 0x2070
callq 0x10d0
movq %rax, -0x650(%rbp)
leaq -0x640(%rbp), %rdi
callq 0x1090
movq %rax, %rcx
movq -0x650(%rbp), %rax
cmpq %rcx, %rax
jae 0x15e9
leaq -0x640(%rbp), %rdi
leaq 0xa96(%rip), %rsi       # 0x2070
callq 0x10d0
movb $0x0, -0x640(%rbp,%rax)
jmp 0x1597
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movl -0x3c(%rbp), %eax
imulq $0x108, %rax, %rax      # imm = 0x108
addq %rax, %rdi
addq $0x8, %rdi
leaq -0x640(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x1040
leaq -0x640(%rbp), %rdi
leaq 0xa53(%rip), %rsi       # 0x2073
callq 0x1150
movq %rax, -0x648(%rbp)
movl -0x24(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x166f
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rdi
movl -0x3c(%rbp), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rdi
addq $0x8, %rdi
callq *%rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x3c(%rbp), %edx
imulq $0x108, %rdx, %rdx      # imm = 0x108
addq %rdx, %rax
movl %ecx, (%rax)
jmp 0x1694
movq -0x30(%rbp), %rax
movq -0x648(%rbp), %rdi
callq *%rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x3c(%rbp), %edx
imulq $0x108, %rdx, %rdx      # imm = 0x108
addq %rdx, %rax
movl %ecx, (%rax)
xorl %eax, %eax
movl %eax, %edi
leaq 0x9d4(%rip), %rsi       # 0x2073
callq 0x1150
movq %rax, -0x648(%rbp)
movl -0x24(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x16c1
movq -0x648(%rbp), %rax
movb $0x0, 0x1(%rax)
movl -0x24(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x16e9
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movl $0xffffffff, 0x4(%rax)  # imm = 0xFFFFFFFF
jmp 0x170f
movq -0x30(%rbp), %rax
movq -0x648(%rbp), %rdi
callq *%rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x3c(%rbp), %edx
imulq $0x108, %rdx, %rdx      # imm = 0x108
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl -0x24(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x1737
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movl 0x4(%rax), %ecx
subl $0x61, %ecx
movl %ecx, 0x4(%rax)
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
movl -0x3c(%rbp), %eax
imulq $0x108, %rax, %rax      # imm = 0x108
addq %rax, %rdx
addq $0x8, %rdx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x3c(%rbp), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movl (%rax), %ecx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x3c(%rbp), %edi
imulq $0x108, %rdi, %rdi      # imm = 0x108
addq %rdi, %rax
movl 0x4(%rax), %r8d
leaq 0x8ed(%rip), %rdi       # 0x2075
movb $0x0, %al
callq 0x10a0
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x1530
movl -0x40(%rbp), %esi
leaq -0x240(%rbp), %rdx
leaq 0x8f7(%rip), %rdi       # 0x20a5
movb $0x0, %al
callq 0x10a0
movq -0x38(%rbp), %rdi
callq 0x1080
movl -0x40(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x650, %rsp            # imm = 0x650
popq %rbp
retq