// File: /workspace/github_c_makefile_star2_O3/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, 0x65b69f(%rip)    # 0x6c4194
jne 0x68ec5
movq %rdi, %r14
cmpb $0x1, 0x65b68b(%rip)    # 0x6c4190
jne 0x68b13
movb $0x77, 0xc(%rsp)
movb $0x0, 0x65b67d(%rip)    # 0x6c4190
leaq 0x13ab0(%rip), %rdi     # 0x7c5ca
leaq 0xc(%rsp), %rsi
callq 0x6ce0
testq %rax, %rax
je 0x68ea7
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 0x68e88
testq %r12, %r12
jne 0x68b90
movaps 0x13ada(%rip), %xmm0    # 0x7c640
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
movl $0x10, %r12d
movl $0x10, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x68e88
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 0x68e88
leaq 0x14(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x68e88
leaq 0x1c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x68e88
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x68e88
leaq 0x18(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x68e88
cmpl $0x0, 0x1c(%r14)
jle 0x68d1d
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 0x68e88
movl $0x4, %esi
movl $0x1, %edx
leaq 0x14(%rsp), %rdi
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x68e88
movl $0x4, %esi
movl $0x1, %edx
leaq 0x10(%rsp), %rdi
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x68e88
movq (%r13), %rdi
movslq -0x8(%r13), %rsi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x68e88
incq %rbp
movslq 0x1c(%r14), %rax
addq $0x18, %r13
cmpq %rax, %rbp
jl 0x68c6f
movq %r15, %rdi
callq 0x6480
leaq 0x138ac(%rip), %rdi     # 0x7c5d8
leaq 0xc(%rsp), %rsi
callq 0x6ce0
testq %rax, %rax
je 0x68ed9
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x6c70
movq %r15, %rdi
callq 0x6280
testq %rax, %rax
jne 0x68d89
movaps 0x138eb(%rip), %xmm0    # 0x7c650
leaq 0x20(%rsp), %rdi
movaps %xmm0, (%rdi)
movl $0x10, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x68e88
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 0x68e88
leaq 0x14(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x68e88
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x68e88
leaq 0x18(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x68e88
leaq 0x1c(%rsp), %rdi
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x6b90
cmpq $0x1, %rax
jne 0x68e88
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 0x68676
testq %rax, %rax
je 0x68ef0
movl %r12d, 0x10(%rax)
jmp 0x68f00
movb $0x1, 0x65b305(%rip)    # 0x6c4194
leaq 0x136f2(%rip), %rdi     # 0x7c588
xorl %eax, %eax
callq 0x25221
movq %r15, %rdi
callq 0x6480
jmp 0x68ec5
movb $0x1, 0x65b2e6(%rip)    # 0x6c4194
leaq 0x1359f(%rip), %rdi     # 0x7c454
leaq 0x1370e(%rip), %rsi     # 0x7c5ca
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x25221
movl %ebx, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, 0x65b2b4(%rip)    # 0x6c4194
leaq 0x1356d(%rip), %rdi     # 0x7c454
leaq 0x136ea(%rip), %rsi     # 0x7c5d8
jmp 0x68ebc
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
movl %r12d, %ecx
callq 0x686fc
movl $0x1, %ebx
jmp 0x68ec5