// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, -0x10(%rbp)
movw 0x1b78f(%rip), %ax      # 0xb8235
movw %ax, -0x33(%rbp)
movb 0x1b787(%rip), %al      # 0xb8237
movb %al, -0x31(%rbp)
cmpl $0x0, 0x61a6ba(%rip)    # 0x6b7174
je 0x9cac8
movl $0x0, -0x4(%rbp)
jmp 0x9cfbb
cmpl $0x0, 0x61a6a1(%rip)    # 0x6b7170
je 0x9cadf
movb $0x77, -0x33(%rbp)
movl $0x0, 0x61a691(%rip)    # 0x6b7170
movq 0x4511a(%rip), %rdi     # 0xe1c00
leaq -0x33(%rbp), %rsi
callq 0x6c10
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x9cb25
movl $0x1, 0x61a670(%rip)    # 0x6b7174
movq 0x450f5(%rip), %rsi     # 0xe1c00
leaq 0x1b5f2(%rip), %rdi     # 0xb8104
movb $0x0, %al
callq 0x37c90
movl $0x0, -0x4(%rbp)
jmp 0x9cfbb
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x6ba0
movq -0x28(%rbp), %rdi
callq 0x6270
movq %rax, -0x30(%rbp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x30(%rbp)
jl 0x9cb54
jmp 0x9cf8c
cmpq $0x0, -0x30(%rbp)
jne 0x9cb9b
movq 0x1b58e(%rip), %rax     # 0xb80f0
movq %rax, -0x50(%rbp)
movq 0x1b58b(%rip), %rax     # 0xb80f8
movq %rax, -0x48(%rbp)
leaq -0x50(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x10, %esi
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
je 0x9cb93
jmp 0x9cf8c
movq $0x10, -0x30(%rbp)
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x9, %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x5c(%rbp)
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x54(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
jne 0x9cc67
movq -0x28(%rbp), %rcx
leaq -0x58(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
jne 0x9cc67
movq -0x28(%rbp), %rcx
leaq -0x60(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
jne 0x9cc67
movq -0x28(%rbp), %rcx
leaq -0x5c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
jne 0x9cc67
movq -0x28(%rbp), %rcx
leaq -0x64(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
je 0x9cc6c
jmp 0x9cf8c
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movslq 0x1c(%rcx), %rcx
cmpq %rcx, %rax
jge 0x9cd7a
movq -0x10(%rbp), %rax
addq $0x20, %rax
imulq $0x18, -0x18(%rbp), %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x68(%rbp)
movq -0x10(%rbp), %rax
addq $0x20, %rax
imulq $0x18, -0x18(%rbp), %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x6c(%rbp)
movq -0x10(%rbp), %rax
addq $0x20, %rax
imulq $0x18, -0x18(%rbp), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x70(%rbp)
movq -0x28(%rbp), %rcx
leaq -0x68(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
jne 0x9cd21
movq -0x28(%rbp), %rcx
leaq -0x6c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
jne 0x9cd21
movq -0x28(%rbp), %rcx
leaq -0x70(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
je 0x9cd26
jmp 0x9cf8c
movq -0x10(%rbp), %rax
addq $0x20, %rax
imulq $0x18, -0x18(%rbp), %rcx
addq %rcx, %rax
movq 0x10(%rax), %rdi
movq -0x10(%rbp), %rax
addq $0x20, %rax
imulq $0x18, -0x18(%rbp), %rcx
addq %rcx, %rax
movslq 0x8(%rax), %rsi
movq -0x28(%rbp), %rcx
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
je 0x9cd67
jmp 0x9cf8c
jmp 0x9cd69
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x9cc74
movq -0x28(%rbp), %rdi
callq 0x6420
movq 0x44e7e(%rip), %rdi     # 0xe1c08
leaq -0x33(%rbp), %rsi
callq 0x6c10
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x9cdc9
movl $0x1, 0x61a3cc(%rip)    # 0x6b7174
movq 0x44e59(%rip), %rsi     # 0xe1c08
leaq 0x1b34e(%rip), %rdi     # 0xb8104
movb $0x0, %al
callq 0x37c90
movl $0x0, -0x4(%rbp)
jmp 0x9cfbb
movq -0x28(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x6ba0
movq -0x28(%rbp), %rdi
callq 0x6270
cmpq $0x0, %rax
jne 0x9ce24
movq 0x1b2ef(%rip), %rax     # 0xb80e0
movq %rax, -0x80(%rbp)
movq 0x1b2ec(%rip), %rax     # 0xb80e8
movq %rax, -0x78(%rbp)
leaq -0x80(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x10, %esi
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
je 0x9ce22
jmp 0x9cf8c
jmp 0x9ce24
leaq -0x190(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movl $0x104, %edx            # imm = 0x104
callq 0x6180
movb $0x0, -0x8d(%rbp)
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x194(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x1, %eax
movl %eax, -0x198(%rbp)
movl -0x198(%rbp), %eax
movl %eax, -0x198(%rbp)
movq -0x30(%rbp), %rax
movl %eax, -0x1a0(%rbp)
movl $0x0, -0x19c(%rbp)
leaq -0x190(%rbp), %rdi
movq -0x28(%rbp), %rcx
movl $0x104, %esi            # imm = 0x104
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
jne 0x9cf24
movq -0x28(%rbp), %rcx
leaq -0x194(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
jne 0x9cf24
movq -0x28(%rbp), %rcx
leaq -0x198(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
jne 0x9cf24
movq -0x28(%rbp), %rcx
leaq -0x1a0(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
jne 0x9cf24
movq -0x28(%rbp), %rcx
leaq -0x19c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x6aa0
cmpq $0x1, %rax
je 0x9cf26
jmp 0x9cf8c
movq -0x28(%rbp), %rdi
callq 0x6420
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x9c460
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x9cf63
movq -0x30(%rbp), %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x10(%rax)
jmp 0x9cf83
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %edx
movq -0x30(%rbp), %rax
movl %eax, %ecx
callq 0x9c510
movl $0x1, -0x4(%rbp)
jmp 0x9cfbb
movl $0x1, 0x61a1de(%rip)    # 0x6b7174
leaq 0x1b29b(%rip), %rdi     # 0xb8238
movb $0x0, %al
callq 0x37c90
cmpq $0x0, -0x28(%rbp)
je 0x9cfb4
movq -0x28(%rbp), %rdi
callq 0x6420
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
nopw (%rax,%rax)