// File: /workspace/github_c_makefile_star2_O3/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 0x14261(%rip), %rdi     # 0x7c5ca
leaq 0x140e0(%rip), %rsi     # 0x7c450
callq 0x6ce0
movq %rax, %r14
testq %rax, %rax
je 0x683c4
leaq 0x10(%rsp), %rdi
movl $0x10, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x683b3
movdqa 0x10(%rsp), %xmm0
pcmpeqb 0x14088(%rip), %xmm0    # 0x7c430
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x683ba
movb $0x1, 0x65bdd6(%rip)    # 0x6c4190
movq %r14, %rdi
callq 0x6480
jmp 0x683d2
callq 0x6a90
cmpl $0x2, (%rax)
jne 0x685b3
leaq 0x141ff(%rip), %rdi     # 0x7c5d8
leaq 0x14070(%rip), %rsi     # 0x7c450
callq 0x6ce0
testq %rax, %rax
je 0x6856b
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
movl $0x10, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x68427
movdqa 0x10(%rsp), %xmm0
pcmpeqb 0x14024(%rip), %xmm0    # 0x7c440
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x6842e
movb $0x1, 0x65bd62(%rip)    # 0x6c4190
testq %r14, %r14
je 0x68585
cmpb $0x0, 0x65bd52(%rip)    # 0x6c4190
jne 0x6858c
movq %rbx, %rdi
callq 0x6090
xorl %r14d, %r14d
movl $0x0, %ebp
testl %eax, %eax
jne 0x685fb
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 0x68491
movq %rbx, %rdi
callq 0x6090
testl %eax, %eax
jne 0x685fb
movl $0x4, %esi
movl $0x1, %edx
leaq 0xc(%rsp), %rdi
movq %rbx, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x685e1
movl $0x4, %esi
movl $0x1, %edx
leaq 0x8(%rsp), %rdi
movq %rbx, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x685e1
movl $0x4, %esi
movl $0x1, %edx
leaq 0x4(%rsp), %rdi
movq %rbx, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x685e1
movl $0x4, %esi
movl $0x1, %edx
leaq 0x2c(%rsp), %rdi
movq %rbx, %rcx
callq 0x6550
cmpq $0x1, %rax
jne 0x685e1
movb $0x0, 0x133(%rsp)
movl 0xc(%rsp), %r13d
movl 0x8(%rsp), %r12d
movq %r15, %rdi
movl %r13d, %esi
movl %r12d, %edx
callq 0x68676
movl 0x4(%rsp), %ecx
testq %rax, %rax
je 0x68545
movl %ecx, 0x10(%rax)
incl %ebp
jmp 0x68553
movq %r15, %rdi
movl %r13d, %esi
movl %r12d, %edx
callq 0x686fc
incl %r14d
movq %rbx, %rdi
callq 0x6090
testl %eax, %eax
je 0x68466
jmp 0x685fb
callq 0x6a90
cmpl $0x2, (%rax)
je 0x685cf
leaq 0x13ed8(%rip), %rdi     # 0x7c454
leaq 0x14055(%rip), %rsi     # 0x7c5d8
jmp 0x685c1
movb $0x1, 0x65bc04(%rip)    # 0x6c4190
leaq 0x13efd(%rip), %rdi     # 0x7c490
xorl %eax, %eax
callq 0x25221
movq %rbx, %rdi
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6480
leaq 0x13e9a(%rip), %rdi     # 0x7c454
leaq 0x14009(%rip), %rsi     # 0x7c5ca
xorl %eax, %eax
callq 0x25221
movb $0x1, 0x65bbc5(%rip)    # 0x6c4194
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x13eda(%rip), %rdi     # 0x7c4c2
xorl %eax, %eax
callq 0x25221
movb $0x1, 0x65bb9a(%rip)    # 0x6c4190
callq 0x68627
movq %rbx, %rdi
callq 0x6480
leaq 0x13f08(%rip), %rdi     # 0x7c512
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 0x25221