// File: /workspace/github_c_makefile_star2_O1/jonof[P]jftekwar/jfbuild/src/polymosttexcache.c
// Function: PTCacheLoadIndex
void PTCacheLoadIndex(void)
{
	FILE * fh = 0;
	int8_t sig[16];
	const int8_t indexsig[16] = { 'P','o','l','y','m','o','s','t','T','e','x','I','n','d','x',CACHEVER };
	const int8_t storagesig[16] = { 'P','o','l','y','m','o','s','t','T','e','x','S','t','o','r',CACHEVER };

	char filename[PTCACHEINDEXFILENAMELEN];
	int32_t effects;
	int32_t flags;
	uint32_t offset;
	int32_t mtime;
	PTCacheIndex * pci;

	int total = 0, dups = 0;
	int haveindex = 0, havestore = 0;

	memset(filename, 0, sizeof(filename));

	// first, check the cache storage file's signature.
	// we open for reading and writing to test permission
	fh = fopen(CACHESTORAGEFILE, "r+b");
	if (fh) {
		havestore = 1;

		if (fread(sig, 16, 1, fh) != 1 || memcmp(sig, storagesig, 16)) {
			cachereplace = 1;
		}
		fclose(fh);
	} else {
		if (errno == ENOENT) {
			// file doesn't exist, which is fine
			;
		} else {
			buildprintf("PolymostTexCache: error opening %s, texture cache disabled\n", CACHESTORAGEFILE);
			cachedisabled = 1;
			return;
		}
	}

	// next, check the index
	fh = fopen(CACHEINDEXFILE, "r+b");
	if (fh) {
		haveindex = 1;

		if (fread(sig, 16, 1, fh) != 1 || memcmp(sig, indexsig, 16)) {
			cachereplace = 1;
		}
	} else {
		if (errno == ENOENT) {
			// file doesn't exist, which is fine
			return;
		} else {
			buildprintf("PolymostTexCache: error opening %s, texture cache disabled\n", CACHEINDEXFILE);
			cachedisabled = 1;
			return;
		}
	}

	// if we're missing either the index or the store, but not both at the same
	// time, the cache is broken and should be replaced
	if ((!haveindex || !havestore) && !(!haveindex && !havestore)) {
		cachereplace = 1;
	}

	if (cachereplace) {
		buildprintf("PolymostTexCache: texture cache will be replaced\n");
		if (fh) {
			fclose(fh);
		}
		return;
	}

	// now that the index is sitting at the first entry, load everything
	while (!feof(fh)) {
		if (fread(filename, PTCACHEINDEXFILENAMELEN, 1, fh) != 1 && feof(fh)) {
			break;
		}
		if (fread(&effects, 4,         1, fh) != 1 ||
		    fread(&flags,   4,         1, fh) != 1 ||
		    fread(&offset,  4,         1, fh) != 1 ||
		    fread(&mtime,   4,         1, fh) != 1) {
			// truncated entry, so throw the whole cache away
			buildprintf("PolymostTexCache: corrupt texture cache index detected, cache will be replaced\n");
			cachereplace = 1;
			PTCacheUnloadIndex();
			break;
		}

		effects = B_LITTLE32(effects);
		flags   = B_LITTLE32(flags);
		offset  = B_LITTLE32(offset);
		mtime   = B_LITTLE32(mtime);

		filename[sizeof(filename)-1] = 0;
		pci = ptcache_findhash(filename, (int) effects, (int) flags);
		if (pci) {
			// superseding an old hash entry
			pci->offset = offset;
			dups++;
		} else {
			ptcache_addhash(filename, (int) effects, (int) flags, offset);
		}
		total++;
	}

	fclose(fh);

	buildprintf("PolymostTexCache: cache index loaded (%d entries, %d old entries skipped)\n", total, dups);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
leaq 0x30(%rsp), %rdi
movl $0x104, %edx            # imm = 0x104
xorl %esi, %esi
callq 0x62f0
leaq 0x14aad(%rip), %rdi     # 0x7b0da
leaq 0x1492c(%rip), %rsi     # 0x7af60
callq 0x6ce0
movq %rax, %r14
testq %rax, %rax
je 0x66688
leaq 0x10(%rsp), %rdi
movl $0x10, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x66677
movdqa 0x10(%rsp), %xmm0
pcmpeqb 0x148d4(%rip), %xmm0    # 0x7af40
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x6667e
movb $0x1, 0x65cb22(%rip)    # 0x6c31a0
movq %r14, %rdi
callq 0x6480
jmp 0x66696
callq 0x6a90
cmpl $0x2, (%rax)
jne 0x6686f
leaq 0x14a4b(%rip), %rdi     # 0x7b0e8
leaq 0x148bc(%rip), %rsi     # 0x7af60
callq 0x6ce0
testq %rax, %rax
je 0x6672a
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
movl $0x10, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x666e7
movdqa 0x10(%rsp), %xmm0
pcmpeqb 0x14874(%rip), %xmm0    # 0x7af50
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x666ee
movb $0x1, 0x65cab2(%rip)    # 0x6c31a0
testq %r14, %r14
jne 0x666fa
movb $0x1, 0x65caa6(%rip)    # 0x6c31a0
cmpb $0x0, 0x65ca9f(%rip)    # 0x6c31a0
je 0x6674b
leaq 0x14896(%rip), %rdi     # 0x7afa0
xorl %eax, %eax
callq 0x24cb4
movq %rbx, %rdi
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6480
callq 0x6a90
cmpl $0x2, (%rax)
je 0x6688b
leaq 0x14825(%rip), %rdi     # 0x7af64
leaq 0x149a2(%rip), %rsi     # 0x7b0e8
jmp 0x6687d
movq %rbx, %rdi
callq 0x6090
xorl %r14d, %r14d
movl $0x0, %ebp
testl %eax, %eax
jne 0x668b7
xorl %ebp, %ebp
leaq 0x30(%rsp), %r15
xorl %r14d, %r14d
movl $0x104, %esi            # imm = 0x104
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x6550
cmpq $0x1, %rax
je 0x66798
movq %rbx, %rdi
callq 0x6090
testl %eax, %eax
jne 0x668b7
movl $0x4, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
movq %rbx, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x6689d
movl $0x4, %esi
movl $0x1, %edx
leaq 0x8(%rsp), %rdi
movq %rbx, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x6689d
movl $0x4, %esi
movl $0x1, %edx
leaq 0x4(%rsp), %rdi
movq %rbx, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x6689d
movl $0x4, %esi
movl $0x1, %edx
leaq 0x2c(%rsp), %rdi
movq %rbx, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x6689d
movb $0x0, 0x133(%rsp)
movl 0xc(%rsp), %r13d
movl 0x8(%rsp), %r12d
movq %r15, %rdi
movl %r13d, %esi
movl %r12d, %edx
callq 0x66932
movl 0x4(%rsp), %ecx
testq %rax, %rax
je 0x6684c
movl %ecx, 0x10(%rax)
incl %ebp
jmp 0x6685a
movq %r15, %rdi
movl %r13d, %esi
movl %r12d, %edx
callq 0x669bc
incl %r14d
movq %rbx, %rdi
callq 0x6090
testl %eax, %eax
je 0x6676d
jmp 0x668b7
leaq 0x146ee(%rip), %rdi     # 0x7af64
leaq 0x1485d(%rip), %rsi     # 0x7b0da
xorl %eax, %eax
callq 0x24cb4
movb $0x1, 0x65c919(%rip)    # 0x6c31a4
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1472e(%rip), %rdi     # 0x7afd2
xorl %eax, %eax
callq 0x24cb4
movb $0x1, 0x65c8ee(%rip)    # 0x6c31a0
callq 0x668e3
movq %rbx, %rdi
callq 0x6480
leaq 0x1475c(%rip), %rdi     # 0x7b022
movl %r14d, %esi
movl %ebp, %edx
xorl %eax, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x24cb4