// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movq %rdi, %r15
callq 0x20f0
movq %rax, 0x28(%rsp)
leaq 0x6b4f(%rip), %rax      # 0x9250
movq %rax, %rbx
movq (%rax), %rcx
leaq 0x18(%rcx), %rax
testq %rcx, %rcx
jne 0x2701
xorl %ebp, %ebp
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
xorl %r14d, %r14d
xorl %eax, %eax
movq $0x0, 0x18(%rsp)
movq %rax, 0x8(%rsp)
movq %rbp, 0x10(%rsp)
xorl %ebp, %ebp
leaq 0x30(%rsp), %r13
movq %r13, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdx
callq 0x22b0
testq %rax, %rax
je 0x2a26
movb 0x30(%rsp), %al
cmpb $0x21, %al
jne 0x28f2
movl 0x31(%rsp), %eax
movl $0x544f4f52, %ecx       # imm = 0x544F4F52
xorl %ecx, %eax
movzbl 0x35(%rsp), %ecx
xorl $0x20, %ecx
orl %eax, %ecx
je 0x27ce
movq 0x31(%rsp), %rax
movabsq $0x5f4e414353474154, %rcx # imm = 0x5F4E414353474154
xorq %rcx, %rax
movl 0x39(%rsp), %ecx
xorq $0x20544150, %rcx       # imm = 0x20544150
orq %rax, %rcx
jne 0x28dd
testq %r14, %r14
je 0x28da
leaq 0x3d(%rsp), %rdi
leaq 0x3b42(%rip), %rsi      # 0x62fa
callq 0x22a0
movq %rax, %rdi
callq 0x2310
movq %rax, 0x8(%r14)
jmp 0x28dd
movb $0x1, %r12b
testb $0x1, %bpl
je 0x27de
xorl %eax, %eax
jmp 0x28e2
movl $0x1, %edi
movl $0x20, %esi
callq 0x20e0
movq %rax, %r14
leaq 0x36(%rsp), %rdi
leaq 0x3afe(%rip), %rsi      # 0x62fa
callq 0x22a0
movq %rax, %rdi
callq 0x2310
movq %rax, %r13
movq %rax, (%r14)
movq %rax, %rdi
callq 0x2686
testb %al, %al
je 0x284b
leaq 0x3adc(%rip), %rdi      # 0x62fe
movq %r13, %rsi
xorl %eax, %eax
callq 0x2030
movq (%r14), %rdi
callq 0x21e0
movq %r14, %rdi
callq 0x21e0
movb $0x1, %al
movl %ebp, %r12d
leaq 0x30(%rsp), %r13
jmp 0x28e2
movq %r13, %rdi
movl $0x2f, %esi
callq 0x2090
movb $0x2f, %cl
testq %rax, %rax
jne 0x2873
movq %r13, %rdi
movl $0x5c, %esi
callq 0x2090
movb $0x5c, %cl
testq %rax, %rax
je 0x2879
movb %cl, 0x69b7(%rip)       # 0x9230
movq %r14, (%rbx)
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x288d
movq $0x0, (%rax)
movq 0x8(%rsp), %rax
testq %rax, %rax
leaq 0x30(%rsp), %r13
je 0x28a3
movq $0x0, (%rax)
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x28b4
movq $0x0, (%rax)
leaq 0x18(%r14), %rbx
movq %r14, %rax
addq $0x10, %rax
movq %rax, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
xorl %eax, %eax
movq $0x0, 0x8(%rsp)
jmp 0x28e2
xorl %r14d, %r14d
xorl %eax, %eax
movl %ebp, %r12d
movl %r12d, %ebp
testb %al, %al
je 0x2741
jmp 0x2a26
cmpb $0x9, %al
jne 0x29f7
cmpb $0x9, 0x32(%rsp)
jne 0x29f7
leaq 0x33(%rsp), %rdi
leaq 0x3a2a(%rip), %rsi      # 0x633b
callq 0x22a0
movq %rax, %r13
movzbl 0x31(%rsp), %eax
cmpl $0x64, %eax
je 0x292e
cmpl $0x66, %eax
jne 0x273a
jmp 0x29a0
movq 0x18(%rsp), %r12
testq %r12, %r12
je 0x2aa6
movl $0x1, %edi
movl $0x18, %esi
callq 0x20e0
movq %rax, %rbp
movq %r13, %rdi
callq 0x2310
movq %rax, (%rbp)
movq %rbp, (%r12)
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x296f
movq $0x0, (%rax)
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x2980
movq $0x0, (%rax)
leaq 0x10(%rbp), %rax
movq %rax, 0x18(%rsp)
addq $0x8, %rbp
movq $0x0, 0x10(%rsp)
movq %rbp, 0x8(%rsp)
jmp 0x273a
movq 0x8(%rsp), %r12
testq %r12, %r12
je 0x2ac5
movl $0x1, %edi
movl $0x18, %esi
callq 0x20e0
movq %rax, %rbp
movq %r13, %rdi
callq 0x2310
movq %rax, (%rbp)
movq %rbp, (%r12)
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x29e1
movq $0x0, (%rax)
leaq 0x10(%rbp), %rax
movq %rax, 0x8(%rsp)
incl 0x20(%rsp)
addq $0x8, %rbp
jmp 0x2735
movq 0x10(%rsp), %r12
testq %r12, %r12
je 0x2abc
movq %r13, %rdi
callq 0x25c0
movq %rax, %rbp
movq %rax, (%r12)
addq $0x8, %rbp
incl 0x24(%rsp)
movq 0x8(%rsp), %rax
jmp 0x2730
testq %rbx, %rbx
je 0x2a32
movq $0x0, (%rbx)
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x2a43
movq $0x0, (%rax)
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x2a54
movq $0x0, (%rax)
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x2a65
movq $0x0, (%rax)
callq 0x20f0
subq 0x28(%rsp), %rax
cvtsi2sd %rax, %xmm0
divsd 0x358c(%rip), %xmm0     # 0x6008
leaq 0x38ba(%rip), %rdi      # 0x633d
movl 0x20(%rsp), %esi
movl 0x24(%rsp), %edx
movb $0x1, %al
callq 0x2030
xorl %eax, %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3be6(%rip), %rdi      # 0x6693
callq 0x2280
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x21f0
leaq 0x3b80(%rip), %rdi      # 0x6643
jmp 0x2aad
leaq 0x3b9f(%rip), %rdi      # 0x666b
jmp 0x2aad