// File: /workspace/github_c_makefile_star2_O2/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 0x13ba5(%rip), %rdi     # 0x712fa
leaq 0x13a24(%rip), %rsi     # 0x71180
callq 0x6ce0
movq %rax, %r14
testq %rax, %rax
je 0x5d7ac
leaq 0x10(%rsp), %rdi
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x5d79b
movdqa 0x10(%rsp), %xmm0
pcmpeqb 0x139d0(%rip), %xmm0    # 0x71160
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x5d7a2
movb $0x1, 0x65f9ee(%rip)    # 0x6bd190
movq %r14, %rdi
callq 0x6480
jmp 0x5d7ba
callq 0x6a90
cmpl $0x2, (%rax)
jne 0x5d987
leaq 0x13b47(%rip), %rdi     # 0x71308
leaq 0x139b8(%rip), %rsi     # 0x71180
callq 0x6ce0
testq %rax, %rax
je 0x5d93f
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x5d80b
movdqa 0x10(%rsp), %xmm0
pcmpeqb 0x13970(%rip), %xmm0    # 0x71170
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x5d812
movb $0x1, 0x65f97e(%rip)    # 0x6bd190
testq %r14, %r14
je 0x5d959
cmpb $0x0, 0x65f96e(%rip)    # 0x6bd190
jne 0x5d960
xorl %r13d, %r13d
leaq 0x30(%rsp), %r14
pushq $0x1
popq %r12
pushq $0x4
popq %r15
movl $0x0, (%rsp)
movq %rbx, %rdi
callq 0x6090
testl %eax, %eax
jne 0x5d9cf
movl $0x104, %esi            # imm = 0x104
movq %r14, %rdi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x6550
cmpq $0x1, %rax
je 0x5d878
movq %rbx, %rdi
callq 0x6090
testl %eax, %eax
jne 0x5d9cf
leaq 0xc(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x5d9b5
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x5d9b5
leaq 0x4(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x5d9b5
leaq 0x2c(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x5d9b5
movq %r15, %r12
movb $0x0, 0x133(%rsp)
movl 0xc(%rsp), %r15d
movl 0x8(%rsp), %ebp
movq %r14, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x5da47
movl 0x4(%rsp), %ecx
testq %rax, %rax
je 0x5d91e
movl %ecx, 0x10(%rax)
incl (%rsp)
jmp 0x5d92b
movq %r14, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x5daa6
incl %r13d
leaq 0x30(%rsp), %r14
movq %r12, %r15
pushq $0x1
popq %r12
jmp 0x5d83f
callq 0x6a90
cmpl $0x2, (%rax)
je 0x5d9a3
leaq 0x13834(%rip), %rdi     # 0x71184
leaq 0x139b1(%rip), %rsi     # 0x71308
jmp 0x5d995
movb $0x1, 0x65f830(%rip)    # 0x6bd190
leaq 0x13859(%rip), %rdi     # 0x711c0
xorl %eax, %eax
callq 0x22427
movq %rbx, %rdi
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6480
leaq 0x137f6(%rip), %rdi     # 0x71184
leaq 0x13965(%rip), %rsi     # 0x712fa
xorl %eax, %eax
callq 0x22427
movb $0x1, 0x65f7f1(%rip)    # 0x6bd194
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x13836(%rip), %rdi     # 0x711f2
xorl %eax, %eax
callq 0x22427
movb $0x1, 0x65f7c6(%rip)    # 0x6bd190
callq 0x5d9fc
movq %rbx, %rdi
callq 0x6480
leaq 0x13864(%rip), %rdi     # 0x71242
movl %r13d, %esi
movl (%rsp), %edx
xorl %eax, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22427