// File: /workspace/github_c_makefile_star2_O2/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, %eax
andl $0xfff, %eax            # imm = 0xFFF
movl %edx, %r13d
andl $0x7, %r13d
leaq 0x658bb4(%rip), %rsi    # 0x6b5180
leaq (%rsi,%rax,8), %rbx
movq %rbx, %rsi
movq (%rsi), %rsi
testq %rsi, %rsi
je 0x5c604
cmpl %ebp, 0x38(%rsi)
jne 0x5c5d3
cmpl %r14d, 0x3c(%rsi)
jne 0x5c5d3
movzwl 0x40(%rsi), %eax
andl $0x7, %eax
cmpw %r13w, %ax
jne 0x5c5d3
movq %rsi, %rax
movq 0x58(%rsi), %rsi
testq %rsi, %rsi
jne 0x5c5f3
jmp 0x5c6d7
testl %ecx, %ecx
je 0x5c6d5
testb $0x2, %dl
movl %edx, 0x4(%rsp)
jne 0x5c61a
xorl %r12d, %r12d
jmp 0x5c62a
andl $0x4, %edx
movl %ebp, %edi
movl %r14d, %esi
callq 0x5541c
movq %rax, %r12
pushq $0x1
popq %rdi
pushq $0x68
popq %rsi
callq 0x6930
testq %rax, %rax
je 0x5c6d5
movq %rax, %r15
movq (%rbx), %rax
movq %rax, (%r15)
movl %ebp, 0x38(%r15)
movl %r14d, 0x3c(%r15)
movw %r13w, 0x40(%r15)
movq %r12, 0x48(%r15)
movq %r15, (%rbx)
testq %r12, %r12
je 0x5c69d
movzbl 0x8(%r12), %esi
cmpl %r14d, %esi
je 0x5c6a6
movslq %r14d, %rax
leaq 0x5e55ab(%rip), %rcx    # 0x641c20
testb $0x3, 0x3(%rcx,%rax,4)
jne 0x5c6a6
movzwl 0x4(%rsp), %edx
pushq $0x1
popq %rcx
movl %ebp, %edi
callq 0x5c5a7
movq %rax, 0x58(%r15)
movq %r15, %rax
movq 0x58(%r15), %r15
testq %r15, %r15
jne 0x5c68f
jmp 0x5c6d7
movl 0x4(%rsp), %edx
testb $0x2, %dl
jne 0x5c6ab
movq %r15, %rax
jmp 0x5c6d7
testb $0x4, %dl
jne 0x5c6d5
andl $0xfff9, %edx           # imm = 0xFFF9
pushq $0x1
popq %rcx
movl %ebp, %edi
movl %r14d, %esi
callq 0x5c5a7
movq %rax, 0x58(%r15)
movq %r15, %rax
movq 0x58(%r15), %r15
testq %r15, %r15
jne 0x5c6c7
jmp 0x5c6d7
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq