// File: /workspace/github_c_makefile_star2_O1/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, 0xe(%rsp)
movw $0x6261, 0xc(%rsp)      # imm = 0x6261
xorl %ebx, %ebx
cmpb $0x0, 0x65c3b2(%rip)    # 0x6c31a4
jne 0x671c9
movq %rdi, %r14
cmpb $0x1, 0x65c39e(%rip)    # 0x6c31a0
jne 0x66e10
movb $0x77, 0xc(%rsp)
movb $0x0, 0x65c390(%rip)    # 0x6c31a0
leaq 0x142c3(%rip), %rdi     # 0x7b0da
leaq 0xc(%rsp), %rsi
callq 0x6ce0
testq %rax, %rax
je 0x671ab
movq %rax, %r15
xorl %ebx, %ebx
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x6c70
movq %r15, %rdi
callq 0x6280
movq %rax, %r12
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpq %rax, %r12
jg 0x6718c
testq %r12, %r12
jne 0x66e94
movaps 0x142ed(%rip), %xmm0    # 0x7b150
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
movl $0x10, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
xorl %r12d, %r12d
cmpq $0x1, %rax
sete %al
jne 0x6718c
movb %al, %r12b
shll $0x4, %r12d
movl 0x14(%r14), %eax
leaq 0x20(%rsp), %rdi
movl %eax, (%rdi)
movl 0x18(%r14), %eax
movl %eax, 0x14(%rsp)
movl 0xc(%r14), %eax
andl $0x9, %eax
movl %eax, 0x1c(%rsp)
movl 0x10(%r14), %eax
movl %eax, 0x10(%rsp)
movl 0x1c(%r14), %eax
movl %eax, 0x18(%rsp)
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
leaq 0x14(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
leaq 0x1c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
leaq 0x18(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
cmpl $0x0, 0x1c(%r14)
jle 0x67021
leaq 0x30(%r14), %r13
xorl %ebp, %ebp
movl -0x10(%r13), %eax
movl %eax, 0x20(%rsp)
movl -0xc(%r13), %eax
movl %eax, 0x14(%rsp)
movl -0x8(%r13), %eax
movl %eax, 0x10(%rsp)
movl $0x4, %esi
movl $0x1, %edx
leaq 0x20(%rsp), %rdi
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
movl $0x4, %esi
movl $0x1, %edx
leaq 0x14(%rsp), %rdi
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
movl $0x4, %esi
movl $0x1, %edx
leaq 0x10(%rsp), %rdi
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
movq (%r13), %rdi
movslq -0x8(%r13), %rsi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
incq %rbp
movslq 0x1c(%r14), %rax
addq $0x18, %r13
cmpq %rax, %rbp
jl 0x66f73
movq %r15, %rdi
callq 0x6480
leaq 0x140b8(%rip), %rdi     # 0x7b0e8
leaq 0xc(%rsp), %rsi
callq 0x6ce0
testq %rax, %rax
je 0x671dd
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x6c70
movq %r15, %rdi
callq 0x6280
testq %rax, %rax
jne 0x6708d
movaps 0x140f7(%rip), %xmm0    # 0x7b160
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
movl $0x10, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
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, 0x14(%rsp)
movl 0xc(%r14), %eax
andl $0x1, %eax
movl %eax, 0x10(%rsp)
movl %r12d, 0x18(%rsp)
movl $0x0, 0x1c(%rsp)
movl $0x104, %esi            # imm = 0x104
movl $0x1, %edx
movq %r13, %rdi
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
leaq 0x14(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
leaq 0x18(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
leaq 0x1c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x6718c
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 0x66932
testq %rax, %rax
je 0x671f4
movl %r12d, 0x10(%rax)
jmp 0x67204
movb $0x1, 0x65c011(%rip)    # 0x6c31a4
leaq 0x13efe(%rip), %rdi     # 0x7b098
xorl %eax, %eax
callq 0x24cb4
movq %r15, %rdi
callq 0x6480
jmp 0x671c9
movb $0x1, 0x65bff2(%rip)    # 0x6c31a4
leaq 0x13dab(%rip), %rdi     # 0x7af64
leaq 0x13f1a(%rip), %rsi     # 0x7b0da
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x24cb4
movl %ebx, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, 0x65bfc0(%rip)    # 0x6c31a4
leaq 0x13d79(%rip), %rdi     # 0x7af64
leaq 0x13ef6(%rip), %rsi     # 0x7b0e8
jmp 0x671c0
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
movl %r12d, %ecx
callq 0x669bc
movl $0x1, %ebx
jmp 0x671c9