// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/polymosttexcache.c
// Function: PTCacheWriteTile
int PTCacheWriteTile(PTCacheTile * tdef)
{
	long i;
	PTCacheIndex * pci;

	FILE * fh;
	off_t offset;
	char createmode[] = "ab";

	if (cachedisabled) {
		return 0;
	}

	if (cachereplace) {
		createmode[0] = 'w';
		cachereplace = 0;
	}

	// 1. write the tile data to the storage file
	fh = fopen(CACHESTORAGEFILE, createmode);
	if (!fh) {
		cachedisabled = 1;
		buildprintf("PolymostTexCache: error opening %s, texture cache disabled\n", CACHESTORAGEFILE);
		return 0;
	}

	// apparently opening in append doesn't actually put the
	// file pointer at the end of the file like you would
	// imagine, so the ftell doesn't return the length of the
	// file like you'd expect it should
	fseek(fh, 0, SEEK_END);
	offset = ftell(fh);

	if (offset >= UINT32_MAX) goto fail;
	if (offset == 0) {
		// new file
		const int8_t storagesig[16] = { 'P','o','l','y','m','o','s','t','T','e','x','S','t','o','r',CACHEVER };
		if (fwrite(storagesig, 16, 1, fh) != 1) {
			goto fail;
		}

		offset = 16;
	}

	{
		int32_t tsizx, tsizy;
		int32_t format, flags, nmipmaps;

		tsizx = B_LITTLE32(tdef->tsizx);
		tsizy = B_LITTLE32(tdef->tsizy);
		flags = tdef->flags & (PTH_CLAMPED | PTH_HASALPHA);
		flags = B_LITTLE32(flags);
		format = B_LITTLE32(tdef->format);
		nmipmaps = B_LITTLE32(tdef->nummipmaps);

		if (fwrite(&tsizx, 4, 1, fh) != 1 ||
		    fwrite(&tsizy, 4, 1, fh) != 1 ||
		    fwrite(&flags, 4, 1, fh) != 1 ||
		    fwrite(&format, 4, 1, fh) != 1 ||
		    fwrite(&nmipmaps, 4, 1, fh) != 1) {
			goto fail;
		}
	}

	for (i = 0; i < tdef->nummipmaps; i++) {
		int32_t sizx, sizy;
		int32_t length;

		sizx = B_LITTLE32(tdef->mipmap[i].sizx);
		sizy = B_LITTLE32(tdef->mipmap[i].sizy);
		length = B_LITTLE32(tdef->mipmap[i].length);

		if (fwrite(&sizx, 4, 1, fh) != 1 ||
		    fwrite(&sizy, 4, 1, fh) != 1 ||
		    fwrite(&length, 4, 1, fh) != 1) {
			goto fail;
		}

		if (fwrite(tdef->mipmap[i].data, tdef->mipmap[i].length, 1, fh) != 1) {
			// truncated data
			goto fail;
		}
	}

	fclose(fh);

	// 2. append to the index
	fh = fopen(CACHEINDEXFILE, createmode);
	if (!fh) {
		cachedisabled = 1;
		buildprintf("PolymostTexCache: error opening %s, texture cache disabled\n", CACHEINDEXFILE);
		return 0;
	}

	fseek(fh, 0, SEEK_END);
	if (ftell(fh) == 0) {
		// new file
		const int8_t indexsig[16] = { 'P','o','l','y','m','o','s','t','T','e','x','I','n','d','x',CACHEVER };
		if (fwrite(indexsig, 16, 1, fh) != 1) {
			goto fail;
		}
	}

	{
		char filename[PTCACHEINDEXFILENAMELEN];
		int32_t effects, flags, mtime;
		uint32_t offs;

		strncpy(filename, tdef->filename, sizeof(filename));
		filename[sizeof(filename)-1] = 0;
		effects = B_LITTLE32(tdef->effects);
		flags   = tdef->flags & (PTH_CLAMPED);	// we don't want the informational flags in the index
		flags   = B_LITTLE32(flags);
		offs    = B_LITTLE32((uint32_t)offset);
		mtime = 0;

		if (fwrite(filename, sizeof(filename), 1, fh) != 1 ||
		    fwrite(&effects, 4, 1, fh) != 1 ||
		    fwrite(&flags, 4, 1, fh) != 1 ||
		    fwrite(&offs, 4, 1, fh) != 1 ||
		    fwrite(&mtime, 4, 1, fh) != 1) {
			goto fail;
		}
	}

	fclose(fh);

	// stow the data into the index in memory
	pci = ptcache_findhash(tdef->filename, tdef->effects, tdef->flags);
	if (pci) {
		// superseding an old hash entry
		pci->offset = (unsigned)offset;
	} else {
		ptcache_addhash(tdef->filename, tdef->effects, tdef->flags, (unsigned)offset);
	}

	return 1;
fail:
	cachedisabled = 1;
	buildprintf("PolymostTexCache: error writing to cache, texture cache disabled\n");
	if (fh) fclose(fh);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movb $0x0, 0x6(%rsp)
movw $0x6261, 0x4(%rsp)      # imm = 0x6261
xorl %ebx, %ebx
cmpb $0x0, 0x65f35d(%rip)    # 0x6bd194
jne 0x5e1c6
movq %rdi, %r14
cmpb $0x1, 0x65f349(%rip)    # 0x6bd190
jne 0x5de55
movb $0x77, 0x4(%rsp)
movb $0x0, 0x65f33b(%rip)    # 0x6bd190
leaq 0x1349e(%rip), %rdi     # 0x712fa
leaq 0x4(%rsp), %rsi
callq 0x6ce0
testq %rax, %rax
je 0x5e03a
movq %rax, %r15
xorl %ebx, %ebx
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x6c70
movq %r15, %rdi
callq 0x6280
movq %rax, %r12
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpq %rax, %r12
jg 0x5e1a9
testq %r12, %r12
jne 0x5deca
pushq $0x10
popq %r12
movaps 0x134c6(%rip), %xmm0    # 0x71370
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
pushq $0x1
popq %rdx
movq %r12, %rsi
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
movl 0x14(%r14), %eax
leaq 0x20(%rsp), %rdi
movl %eax, (%rdi)
movl 0x18(%r14), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%r14), %eax
andl $0x9, %eax
movl %eax, 0x14(%rsp)
movl 0x10(%r14), %eax
movl %eax, 0x8(%rsp)
movl 0x1c(%r14), %eax
movl %eax, 0x10(%rsp)
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
leaq 0x14(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
leaq 0x8(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
leaq 0x10(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
leaq 0x30(%r14), %rbp
xorl %ecx, %ecx
pushq $0x1
popq %r13
movslq 0x1c(%r14), %rax
cmpq %rax, %rcx
jge 0x5e05d
movq %rcx, 0x18(%rsp)
movl -0x10(%rbp), %eax
movl %eax, 0x20(%rsp)
movl -0xc(%rbp), %eax
movl %eax, 0xc(%rsp)
movl -0x8(%rbp), %eax
movl %eax, 0x8(%rsp)
leaq 0x20(%rsp), %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
leaq 0x8(%rsp), %rdi
pushq $0x4
popq %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
movq (%rbp), %rdi
movslq -0x8(%rbp), %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
movq 0x18(%rsp), %rcx
incq %rcx
addq $0x18, %rbp
jmp 0x5df8e
movb $0x1, 0x65f153(%rip)    # 0x6bd194
leaq 0x1313c(%rip), %rdi     # 0x71184
leaq 0x132ab(%rip), %rsi     # 0x712fa
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x22427
jmp 0x5e1c6
movq %r15, %rdi
callq 0x6480
leaq 0x1329c(%rip), %rdi     # 0x71308
leaq 0x4(%rsp), %rsi
callq 0x6ce0
testq %rax, %rax
je 0x5e1da
movq %rax, %r15
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x6c70
movq %r15, %rdi
callq 0x6280
testq %rax, %rax
jne 0x5e0c3
movaps 0x132dd(%rip), %xmm0    # 0x71380
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
movq (%r14), %rsi
leaq 0x20(%rsp), %r13
movl $0x104, %edx            # imm = 0x104
movq %r13, %rdi
callq 0x6190
movb $0x0, 0x103(%r13)
movl 0x8(%r14), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%r14), %eax
andl $0x1, %eax
movl %eax, 0x8(%rsp)
movl %r12d, 0x10(%rsp)
andl $0x0, 0x14(%rsp)
pushq $0x1
popq %rdx
movl $0x104, %esi            # imm = 0x104
movq %r13, %rdi
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
leaq 0xc(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
leaq 0x8(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
leaq 0x10(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
leaq 0x14(%rsp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x5e1a9
movq %r15, %rdi
callq 0x6480
movq (%r14), %rbx
movl 0x8(%r14), %ebp
movl 0xc(%r14), %r14d
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
callq 0x5da47
testq %rax, %rax
je 0x5e1f4
movl %r12d, 0x10(%rax)
jmp 0x5e204
movb $0x1, 0x65efe4(%rip)    # 0x6bd194
leaq 0x13101(%rip), %rdi     # 0x712b8
xorl %eax, %eax
callq 0x22427
movq %r15, %rdi
callq 0x6480
movl %ebx, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, 0x65efb3(%rip)    # 0x6bd194
leaq 0x12f9c(%rip), %rdi     # 0x71184
leaq 0x13119(%rip), %rsi     # 0x71308
jmp 0x5e04f
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
movl %r12d, %ecx
callq 0x5daa6
pushq $0x1
popq %rbx
jmp 0x5e1c6