// File: /workspace/github_c_makefile_star2_O1/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 0x655d89(%rip), %rax    # 0x6bb190
movq (%rax,%r13,8), %rsi
testq %rsi, %rsi
je 0x6543d
cmpl %ebp, 0x38(%rsi)
jne 0x65427
cmpl %r14d, 0x3c(%rsi)
jne 0x65427
movzwl 0x40(%rsi), %eax
andl $0x7, %eax
cmpw %bx, %ax
je 0x6542c
movq (%rsi), %rsi
jmp 0x6540b
movq %rsi, %rax
movq 0x58(%rsi), %rsi
testq %rsi, %rsi
jne 0x6542c
jmp 0x65526
testl %ecx, %ecx
je 0x6550e
testb $0x2, %dl
movl %edx, 0x4(%rsp)
jne 0x65453
xorl %r12d, %r12d
jmp 0x65463
andl $0x4, %edx
movl %ebp, %edi
movl %r14d, %esi
callq 0x5d8cc
movq %rax, %r12
movl $0x68, %edi
callq 0x69a0
testq %rax, %rax
je 0x6550e
movq %rax, %r15
xorps %xmm0, %xmm0
movups %xmm0, 0x50(%rax)
movups %xmm0, 0x40(%rax)
movups %xmm0, 0x30(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq $0x0, 0x60(%rax)
leaq 0x655cee(%rip), %rcx    # 0x6bb190
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 0x654ce
movzbl 0x8(%r12), %esi
cmpl %r14d, %esi
jne 0x65512
testq %r12, %r12
setne %al
movl 0x4(%rsp), %edx
testb $0x2, %dl
sete %cl
orb %al, %cl
jne 0x65523
testb $0x4, %dl
jne 0x6550e
andl $0xfff9, %edx           # imm = 0xFFF9
movl %ebp, %edi
movl %r14d, %esi
movl $0x1, %ecx
callq 0x653e1
movq %rax, 0x58(%r15)
movq %r15, %rax
movq 0x58(%r15), %r15
testq %r15, %r15
jne 0x65500
jmp 0x65526
xorl %eax, %eax
jmp 0x65526
movslq %r14d, %rax
leaq 0x5e2714(%rip), %rcx    # 0x647c30
testb $0x3, 0x3(%rcx,%rax,4)
je 0x65535
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzwl 0x4(%rsp), %edx
movl %ebp, %edi
movl $0x1, %ecx
callq 0x653e1
movq %rax, 0x58(%r15)
movq %r15, %rax
movq 0x58(%r15), %r15
testq %r15, %r15
jne 0x6554a
jmp 0x65526