// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/polymosttex.c
// Function: pt_findhash
static PTHash * pt_findhash(int picnum, int palnum, unsigned short flags, int create)
{
	int i = pt_gethashhead(picnum);
	PTHash * pth;

	unsigned short flagmask = flags & (PTH_HIGHTILE | PTH_CLAMPED | PTH_SKYBOX);

	// first, try and find an existing match for our parameters
	pth = pthashhead[i];
	while (pth) {
		if (pth->head.picnum == picnum &&
		    pth->head.palnum == palnum &&
		    (pth->head.flags & (PTH_HIGHTILE | PTH_CLAMPED | PTH_SKYBOX)) == flagmask
		   ) {
			while (pth->deferto) {
				pth = pth->deferto;	// find the end of the chain
			}
			return pth;
		}

		pth = pth->next;
	}

	if (!create) {
		return 0;
	} else {
		// we didn't find one, so we have to create one
		hicreplctyp * replc = 0;

		if ((flags & PTH_HIGHTILE)) {
			replc = hicfindsubst(picnum, palnum, (flags & PTH_SKYBOX));
		}

		pth = (PTHash *) malloc(sizeof(PTHash));
		if (!pth) {
			return 0;
		}
		memset(pth, 0, sizeof(PTHash));

		pth->next = pthashhead[i];
		pth->head.picnum  = picnum;
		pth->head.palnum  = palnum;
		pth->head.flags   = flagmask;
		pth->head.repldef = replc;

		pthashhead[i] = pth;

		if (replc && replc->palnum != palnum) {
			// we were given a substitute by hightile, so
			if (hictinting[palnum].f & HICEFFECTMASK) {
				// if effects are defined for the palette we actually want
				// we DO NOT defer immediately to the substitute. instead
				// we apply effects to the replacement and treat it as a
				// distinct texture
				;
			} else {
				// we defer to the substitute
				pth->deferto = pt_findhash(picnum, replc->palnum, flags, create);
				while (pth->deferto) {
					pth = pth->deferto;	// find the end of the chain
				}
			}
		} else if ((flags & PTH_HIGHTILE) && !replc) {
			// there is no replacement, so defer to ART
			if (flags & PTH_SKYBOX) {
				return 0;
			} else {
				pth->deferto = pt_findhash(picnum, palnum, (flags & ~PTH_HIGHTILE), create);
				while (pth->deferto) {
					pth = pth->deferto;	// find the end of the chain
				}
			}
		}
	}

	return pth;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %r14d
movl %edi, %ebp
movl %edi, %r13d
andl $0xfff, %r13d           # imm = 0xFFF
movl %edx, %ebx
andl $0x7, %ebx
leaq 0x655053(%rip), %rax    # 0x6bc180
movq (%rax,%r13,8), %rsi
testq %rsi, %rsi
je 0x67163
cmpl %ebp, 0x38(%rsi)
jne 0x6714d
cmpl %r14d, 0x3c(%rsi)
jne 0x6714d
movzwl 0x40(%rsi), %eax
andl $0x7, %eax
cmpw %bx, %ax
je 0x67152
movq (%rsi), %rsi
jmp 0x67131
movq %rsi, %rax
movq 0x58(%rsi), %rsi
testq %rsi, %rsi
jne 0x67152
jmp 0x67247
testl %ecx, %ecx
je 0x67245
testb $0x2, %dl
movl %edx, 0x4(%rsp)
jne 0x67179
xorl %r12d, %r12d
jmp 0x67189
andl $0x4, %edx
movl %ebp, %edi
movl %r14d, %esi
callq 0x5f984
movq %rax, %r12
movl $0x1, %edi
movl $0x68, %esi
callq 0x6930
testq %rax, %rax
je 0x67245
movq %rax, %r15
leaq 0x654fd5(%rip), %rcx    # 0x6bc180
movq (%rcx,%r13,8), %rax
movq %rax, (%r15)
movl %ebp, 0x38(%r15)
movl %r14d, 0x3c(%r15)
movw %bx, 0x40(%r15)
movq %r12, 0x48(%r15)
movq %r15, (%rcx,%r13,8)
testq %r12, %r12
je 0x6720b
movzbl 0x8(%r12), %esi
cmpl %r14d, %esi
je 0x67214
movslq %r14d, %rax
leaq 0x5e1a3f(%rip), %rcx    # 0x648c20
testb $0x3, 0x3(%rcx,%rax,4)
jne 0x67214
movzwl 0x4(%rsp), %edx
movl %ebp, %edi
movl $0x1, %ecx
callq 0x67107
movq %rax, 0x58(%r15)
movq %r15, %rax
movq 0x58(%r15), %r15
testq %r15, %r15
jne 0x671fd
jmp 0x67247
movl 0x4(%rsp), %edx
testb $0x2, %dl
jne 0x67219
movq %r15, %rax
jmp 0x67247
testb $0x4, %dl
jne 0x67245
andl $0xfff9, %edx           # imm = 0xFFF9
movl %ebp, %edi
movl %r14d, %esi
movl $0x1, %ecx
callq 0x67107
movq %rax, 0x58(%r15)
movq %r15, %rax
movq 0x58(%r15), %r15
testq %r15, %r15
jne 0x67237
jmp 0x67247
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq