// File: skyshore2001[P]symfind/symfind.cpp
// Function: LoadRepofile(_IO_FILE*)
int LoadRepofile(FILE *fp)
{
	clock_t t0 = clock();
	char buf[1024];
	bool ismeta = false;
	SfRepo **pRepo = &g_repos;
	SfFolder **pFolder = NULL;
	SfFile **pFile = NULL;
	SfSymbol **pSymbol = NULL;

	// goto repo link end.
	while (*pRepo) {
		pRepo = &((*pRepo)->next);
	}
	int scnt = 0, fcnt = 0;
	SfRepo *repo = NULL;
	while (fgets(buf, 1024, fp)) {
		if (buf[0] == '!') {
			char *p = buf+1;
			if (strncmp(p, "ROOT ", 5) == 0) {
				if (!ismeta) {
					p += 5;
					repo = CALLOC_T(SfRepo);
					repo->root = strdup(strtok(p, " \r\n"));
					if (RootExists(repo->root)) {
						printf("!!! ignore repo loading as root dir exists: %s\n", repo->root);
						FREE(repo->root);
						FREE(repo);
						break;
					}
					if (strchr(repo->root, '/') != NULL) {
						SEP = '/';
					}
					else if (strchr(repo->root, '\\') != NULL) {
						SEP = '\\';
					}

					*pRepo = repo;
					pRepo = &repo->next;

					EndFolder();
					pFolder = &repo->folders;
				}
				ismeta = true;
			}
			else if (strncmp(p, "TAGSCAN_PAT ", 12) == 0) {
				p += 12;
				if (repo) {
					repo->tagscan_pat = strdup(strtok(p, " \r\n"));
				}
			}
			continue;
		}
		ismeta = false;

		// "  d/f  <name>  <time>"
		if (buf[0] == '\t' && buf[2] == '\t') {
			char *value = strtok(buf+3, "\t");
			if (buf[1] == 'd') {
				if (pFolder == NULL) {
					printf("*** bad format (no repo before folder)!\n");
					exit(-1);
				}
				SfFolder *folder = CALLOC_T(SfFolder);
				folder->name = strdup(value);

				*pFolder = folder;
				pFolder = &folder->next;

				EndFile();
				pFile = &folder->files;
			}
			else if (buf[1] == 'f') {
				if (pFile == NULL) {
					printf("*** bad format! (no folder before file)\n");
					exit(-1);
				}

				SfFile *file = CALLOC_T(SfFile);
				file->name = strdup(value);

				*pFile = file;
				pFile = &file->next;
				++ fcnt;

				EndSymbol();
				pSymbol = &file->symbols;
			}
		}
		else { // symbol
			if (pSymbol == NULL) {
				printf("*** bad format! (no file before symbol)\n");
				exit(-1);
			}

			SfSymbol *symbol = NewSymbol(buf);

			*pSymbol = symbol;
			pSymbol = &symbol->next;
			++ scnt;
		}
	}
	EndRepo();
	printf("load %d files, %d symbols in %.3fs.\n", fcnt, scnt, (double)(clock()-t0)/CLOCKS_PER_SEC);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x480, %rsp            # imm = 0x480
movq %rdi, -0x8(%rbp)
callq 0x2160
movq %rax, -0x10(%rbp)
movb $0x0, -0x411(%rbp)
leaq 0xe9ba(%rip), %rax      # 0x112e0
movq %rax, -0x420(%rbp)
movq $0x0, -0x428(%rbp)
movq $0x0, -0x430(%rbp)
movq $0x0, -0x438(%rbp)
movq -0x420(%rbp), %rax
cmpq $0x0, (%rax)
je 0x2972
movq -0x420(%rbp), %rax
movq (%rax), %rax
addq $0x18, %rax
movq %rax, -0x420(%rbp)
jmp 0x294e
movl $0x0, -0x43c(%rbp)
movl $0x0, -0x440(%rbp)
movq $0x0, -0x448(%rbp)
leaq -0x410(%rbp), %rdi
movq -0x8(%rbp), %rdx
movl $0x400, %esi            # imm = 0x400
callq 0x23b0
cmpq $0x0, %rax
je 0x2e53
movsbl -0x410(%rbp), %eax
cmpl $0x21, %eax
jne 0x2bf5
leaq -0x410(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x450(%rbp)
movq -0x450(%rbp), %rdi
leaq 0x8904(%rip), %rsi      # 0xb2e4
movl $0x5, %edx
callq 0x2100
cmpl $0x0, %eax
jne 0x2b8a
testb $0x1, -0x411(%rbp)
jne 0x2b81
movq -0x450(%rbp), %rax
addq $0x5, %rax
movq %rax, -0x450(%rbp)
movl $0x1, %edi
movl $0x20, %esi
callq 0x2150
movq %rax, -0x448(%rbp)
movq -0x450(%rbp), %rdi
leaq 0x88b4(%rip), %rsi      # 0xb2ea
callq 0x23a0
movq %rax, %rdi
callq 0x2420
movq %rax, %rcx
movq -0x448(%rbp), %rax
movq %rcx, (%rax)
movq -0x448(%rbp), %rax
movq (%rax), %rdi
callq 0x28a0
testb $0x1, %al
jne 0x2a65
jmp 0x2a9d
movq -0x448(%rbp), %rax
movq (%rax), %rsi
leaq 0x8878(%rip), %rdi      # 0xb2ee
movb $0x0, %al
callq 0x2030
movq -0x448(%rbp), %rax
movq (%rax), %rdi
callq 0x22d0
movq -0x448(%rbp), %rdi
callq 0x22d0
jmp 0x2e53
movq -0x448(%rbp), %rax
movq (%rax), %rdi
movl $0x2f, %esi
callq 0x20a0
cmpq $0x0, %rax
je 0x2ac0
movb $0x2f, 0xe802(%rip)     # 0x112c0
jmp 0x2ae3
movq -0x448(%rbp), %rax
movq (%rax), %rdi
movl $0x5c, %esi
callq 0x20a0
cmpq $0x0, %rax
je 0x2ae1
movb $0x5c, 0xe7df(%rip)     # 0x112c0
jmp 0x2ae3
movq -0x448(%rbp), %rcx
movq -0x420(%rbp), %rax
movq %rcx, (%rax)
movq -0x448(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x420(%rbp)
cmpq $0x0, -0x428(%rbp)
je 0x2b1e
movq -0x428(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x428(%rbp)
cmpq $0x0, -0x430(%rbp)
je 0x2b41
movq -0x430(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x430(%rbp)
cmpq $0x0, -0x438(%rbp)
je 0x2b64
movq -0x438(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x438(%rbp)
movq -0x448(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x428(%rbp)
movb $0x1, -0x411(%rbp)
jmp 0x2bf0
movq -0x450(%rbp), %rdi
leaq 0x8786(%rip), %rsi      # 0xb31e
movl $0xc, %edx
callq 0x2100
cmpl $0x0, %eax
jne 0x2bee
movq -0x450(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x450(%rbp)
cmpq $0x0, -0x448(%rbp)
je 0x2bec
movq -0x450(%rbp), %rdi
leaq 0x8719(%rip), %rsi      # 0xb2ea
callq 0x23a0
movq %rax, %rdi
callq 0x2420
movq %rax, %rcx
movq -0x448(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x2bee
jmp 0x2bf0
jmp 0x2991
movb $0x0, -0x411(%rbp)
movsbl -0x410(%rbp), %eax
cmpl $0x9, %eax
jne 0x2de7
movsbl -0x40e(%rbp), %eax
cmpl $0x9, %eax
jne 0x2de7
leaq -0x410(%rbp), %rdi
addq $0x3, %rdi
leaq 0x86fd(%rip), %rsi      # 0xb32b
callq 0x23a0
movq %rax, -0x458(%rbp)
movsbl -0x40f(%rbp), %eax
cmpl $0x64, %eax
jne 0x2d1b
cmpq $0x0, -0x428(%rbp)
jne 0x2c6c
leaq 0x86d2(%rip), %rdi      # 0xb32d
movb $0x0, %al
callq 0x2030
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x22e0
movl $0x1, %edi
movl $0x18, %esi
callq 0x2150
movq %rax, -0x460(%rbp)
movq -0x458(%rbp), %rdi
callq 0x2420
movq %rax, %rcx
movq -0x460(%rbp), %rax
movq %rcx, (%rax)
movq -0x460(%rbp), %rcx
movq -0x428(%rbp), %rax
movq %rcx, (%rax)
movq -0x460(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x428(%rbp)
cmpq $0x0, -0x430(%rbp)
je 0x2cd6
movq -0x430(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x430(%rbp)
cmpq $0x0, -0x438(%rbp)
je 0x2cf9
movq -0x438(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x438(%rbp)
movq -0x460(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x430(%rbp)
jmp 0x2de5
movsbl -0x40f(%rbp), %eax
cmpl $0x66, %eax
jne 0x2de3
cmpq $0x0, -0x430(%rbp)
jne 0x2d4d
leaq 0x861a(%rip), %rdi      # 0xb356
movb $0x0, %al
callq 0x2030
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x22e0
movl $0x1, %edi
movl $0x18, %esi
callq 0x2150
movq %rax, -0x468(%rbp)
movq -0x458(%rbp), %rdi
callq 0x2420
movq %rax, %rcx
movq -0x468(%rbp), %rax
movq %rcx, (%rax)
movq -0x468(%rbp), %rcx
movq -0x430(%rbp), %rax
movq %rcx, (%rax)
movq -0x468(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x430(%rbp)
movl -0x440(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x440(%rbp)
cmpq $0x0, -0x438(%rbp)
je 0x2dc6
movq -0x438(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x438(%rbp)
movq -0x468(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x438(%rbp)
jmp 0x2de5
jmp 0x2e4e
cmpq $0x0, -0x438(%rbp)
jne 0x2e09
leaq 0x8587(%rip), %rdi      # 0xb37f
movb $0x0, %al
callq 0x2030
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x22e0
leaq -0x410(%rbp), %rdi
callq 0x27c0
movq %rax, -0x470(%rbp)
movq -0x470(%rbp), %rcx
movq -0x438(%rbp), %rax
movq %rcx, (%rax)
movq -0x470(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x438(%rbp)
movl -0x43c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x43c(%rbp)
jmp 0x2991
cmpq $0x0, -0x420(%rbp)
je 0x2e6b
movq -0x420(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x420(%rbp)
cmpq $0x0, -0x428(%rbp)
je 0x2e8e
movq -0x428(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x428(%rbp)
cmpq $0x0, -0x430(%rbp)
je 0x2eb1
movq -0x430(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x430(%rbp)
cmpq $0x0, -0x438(%rbp)
je 0x2ed4
movq -0x438(%rbp), %rax
movq $0x0, (%rax)
movq $0x0, -0x438(%rbp)
movl -0x440(%rbp), %eax
movl %eax, -0x478(%rbp)
movl -0x43c(%rbp), %eax
movl %eax, -0x474(%rbp)
callq 0x2160
movl -0x478(%rbp), %esi
movl -0x474(%rbp), %edx
subq -0x10(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd 0x80ef(%rip), %xmm1     # 0xb008
divsd %xmm1, %xmm0
leaq 0x8484(%rip), %rdi      # 0xb3a8
movb $0x1, %al
callq 0x2030
xorl %eax, %eax
addq $0x480, %rsp            # imm = 0x480
popq %rbp
retq
nopw %cs:(%rax,%rax)