// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/jfbuild/src/polymosttex.c
// Function: pt_load_art
static int pt_load_art(PTHead * pth)
{
	PTTexture tex, fbtex;
	coltype * wpptr, * fpptr;
	int x, y, x2, y2;
	int dacol;
	int hasalpha = 0, hasfullbright = 0;
    PTMIdent id;

	tex.tsizx = tilesizx[pth->picnum];
	tex.tsizy = tilesizy[pth->picnum];
	if (!glinfo.texnpot) {
		for (tex.sizx = 1; tex.sizx < tex.tsizx; tex.sizx += tex.sizx) ;
		for (tex.sizy = 1; tex.sizy < tex.tsizy; tex.sizy += tex.sizy) ;
	} else {
		if ((tex.tsizx | tex.tsizy) == 0) {
			tex.sizx = tex.sizy = 1;
		} else {
			tex.sizx = tex.tsizx;
			tex.sizy = tex.tsizy;
		}
	}

	tex.rawfmt = GL_RGBA;

	memcpy(&fbtex, &tex, sizeof(PTTexture));

	if (!waloff[pth->picnum]) {
		loadtile(pth->picnum);
	}

	tex.pic = (coltype *) malloc(tex.sizx * tex.sizy * sizeof(coltype));
	if (!tex.pic) {
		return 0;
	}

	// fullbright is initialised transparent
	fbtex.pic = (coltype *) malloc(tex.sizx * tex.sizy * sizeof(coltype));
	if (!fbtex.pic) {
		free(tex.pic);
		return 0;
	}
	memset(fbtex.pic, 0, tex.sizx * tex.sizy * sizeof(coltype));

	if (!waloff[pth->picnum]) {
		// Force invalid textures to draw something - an almost purely transparency texture
		// This allows the Z-buffer to be updated for mirrors (which are invalidated textures)
		tex.pic[0].r = tex.pic[0].g = tex.pic[0].b = 0; tex.pic[0].a = 1;
		tex.tsizx = tex.tsizy = 1;
		hasalpha = 1;
	} else {
		for (y = 0; y < tex.sizy; y++) {
			if (y < tex.tsizy) {
				y2 = y;
			} else {
				y2 = y-tex.tsizy;
			}
			wpptr = &tex.pic[y*tex.sizx];
			fpptr = &fbtex.pic[y*tex.sizx];
			for (x = 0; x < tex.sizx; x++, wpptr++, fpptr++) {
				if ((pth->flags & PTH_CLAMPED) && (x >= tex.tsizx || y >= tex.tsizy)) {
					// Clamp texture
					wpptr->r = wpptr->g = wpptr->b = wpptr->a = 0;
					continue;
				}
				if (x < tex.tsizx) {
					x2 = x;
				} else {
					// wrap around to fill the repeated region
					x2 = x-tex.tsizx;
				}
				dacol = (int) (*(unsigned char *)(waloff[pth->picnum]+x2*tex.tsizy+y2));
				if (dacol == 255) {
					wpptr->a = 0;
					hasalpha = 1;
				} else {
					wpptr->a = 255;
					dacol = (int) ((unsigned char)palookup[pth->palnum][dacol]);
				}
				if (usegammabrightness) {
					wpptr->r = curpalette[dacol].r;
					wpptr->g = curpalette[dacol].g;
					wpptr->b = curpalette[dacol].b;
				} else {
					wpptr->r = britable[curbrightness][ curpalette[dacol].r ];
					wpptr->g = britable[curbrightness][ curpalette[dacol].g ];
					wpptr->b = britable[curbrightness][ curpalette[dacol].b ];
				}

				if (dacol >= polymosttexfullbright && dacol < 255) {
					*fpptr = *wpptr;
					hasfullbright = 1;
				}
			}
		}
	}

	pth->scalex = 1.0;
	pth->scaley = 1.0;
	pth->flags &= ~(PTH_HASALPHA | PTH_SKYBOX);
	pth->flags |= (PTH_NOCOMPRESS | PTH_NOMIPLEVEL);
	tex.hasalpha = hasalpha;

    PTM_InitIdent(&id, pth);
    id.layer = PTHPIC_BASE;
	pth->pic[PTHPIC_BASE] = PTM_GetHead(&id);
	pth->pic[PTHPIC_BASE]->tsizx = tex.tsizx;
	pth->pic[PTHPIC_BASE]->tsizy = tex.tsizy;
	pth->pic[PTHPIC_BASE]->sizx  = tex.sizx;
	pth->pic[PTHPIC_BASE]->sizy  = tex.sizy;
	ptm_uploadtexture(pth->pic[PTHPIC_BASE], pth->flags, &tex, 0);

	if (hasfullbright) {
        id.layer = PTHPIC_GLOW;
		pth->pic[PTHPIC_GLOW] = PTM_GetHead(&id);
		pth->pic[PTHPIC_GLOW]->tsizx = tex.tsizx;
		pth->pic[PTHPIC_GLOW]->tsizy = tex.tsizy;
		pth->pic[PTHPIC_GLOW]->sizx  = tex.sizx;
		pth->pic[PTHPIC_GLOW]->sizy  = tex.sizy;
		fbtex.hasalpha = 1;
		ptm_uploadtexture(pth->pic[PTHPIC_GLOW], pth->flags, &fbtex, 0);
	} else {
		// it might be that after reloading an invalidated texture, the
		// glow map might not be needed anymore, so release it
		pth->pic[PTHPIC_GLOW] = 0;//FIXME should really call a disposal function
	}
	pt_load_applyparameters(pth);

	if (tex.pic) {
		free(tex.pic);
	}
	if (fbtex.pic) {
		free(fbtex.pic);
	}

	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4a0, %rsp            # imm = 0x4A0
movq %rdi, -0x10(%rbp)
movl $0x0, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movq -0x10(%rbp), %rax
movslq 0x30(%rax), %rcx
leaq 0x3af664(%rip), %rax    # 0x44ad80
movswl (%rax,%rcx,2), %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movslq 0x30(%rax), %rcx
leaq 0x3a0e0e(%rip), %rax    # 0x43c540
movswl (%rax,%rcx,2), %eax
movl %eax, -0x1c(%rbp)
leaq 0x11f670(%rip), %rax    # 0x1badb0
cmpb $0x0, 0x1d(%rax)
jne 0x9b780
movl $0x1, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x9b762
jmp 0x9b757
movl -0x28(%rbp), %eax
addl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x9b74d
movl $0x1, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x9b77e
jmp 0x9b773
movl -0x24(%rbp), %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x9b769
jmp 0x9b7a9
movl -0x20(%rbp), %eax
orl -0x1c(%rbp), %eax
cmpl $0x0, %eax
jne 0x9b79b
movl $0x1, -0x24(%rbp)
movl $0x1, -0x28(%rbp)
jmp 0x9b7a7
movl -0x20(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x9b7a9
movl $0x1908, -0x18(%rbp)    # imm = 0x1908
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movslq 0x30(%rax), %rcx
leaq 0x33a661(%rip), %rax    # 0x3d5e40
cmpq $0x0, (%rax,%rcx,8)
jne 0x9b7f5
movq -0x10(%rbp), %rax
movl 0x30(%rax), %eax
movswl %ax, %edi
callq 0x44df0
movl -0x28(%rbp), %eax
imull -0x24(%rbp), %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x68f0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x9b81f
movl $0x0, -0x4(%rbp)
jmp 0x9bcbe
movl -0x28(%rbp), %eax
imull -0x24(%rbp), %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x68f0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x9b852
movq -0x30(%rbp), %rdi
callq 0x61c0
movl $0x0, -0x4(%rbp)
jmp 0x9bcbe
movq -0x50(%rbp), %rdi
movl -0x28(%rbp), %eax
imull -0x24(%rbp), %eax
movslq %eax, %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x62d0
movq -0x10(%rbp), %rax
movslq 0x30(%rax), %rcx
leaq 0x33a5c6(%rip), %rax    # 0x3d5e40
cmpq $0x0, (%rax,%rcx,8)
jne 0x9b8ba
movq -0x30(%rbp), %rax
movb $0x0, 0x2(%rax)
movq -0x30(%rbp), %rax
movb $0x0, 0x1(%rax)
movq -0x30(%rbp), %rax
movb $0x0, (%rax)
movq -0x30(%rbp), %rax
movb $0x1, 0x3(%rax)
movl $0x1, -0x1c(%rbp)
movl $0x1, -0x20(%rbp)
movl $0x1, -0x78(%rbp)
jmp 0x9bb3b
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x9bb39
movl -0x68(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x9b8dd
movl -0x68(%rbp), %eax
movl %eax, -0x70(%rbp)
jmp 0x9b8e6
movl -0x68(%rbp), %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x70(%rbp)
movq -0x30(%rbp), %rax
movl -0x68(%rbp), %ecx
imull -0x28(%rbp), %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
movl -0x68(%rbp), %ecx
imull -0x28(%rbp), %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x9bb29
movq -0x10(%rbp), %rax
movzwl 0x38(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x9b96f
movl -0x64(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x9b94b
movl -0x68(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jl 0x9b96f
movq -0x58(%rbp), %rax
movb $0x0, 0x3(%rax)
movq -0x58(%rbp), %rax
movb $0x0, 0x2(%rax)
movq -0x58(%rbp), %rax
movb $0x0, 0x1(%rax)
movq -0x58(%rbp), %rax
movb $0x0, (%rax)
jmp 0x9bb03
movl -0x64(%rbp), %eax
cmpl -0x20(%rbp), %eax
jge 0x9b97f
movl -0x64(%rbp), %eax
movl %eax, -0x6c(%rbp)
jmp 0x9b988
movl -0x64(%rbp), %eax
subl -0x20(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x10(%rbp), %rax
movslq 0x30(%rax), %rcx
leaq 0x33a4a9(%rip), %rax    # 0x3d5e40
movq (%rax,%rcx,8), %rax
movl -0x6c(%rbp), %ecx
imull -0x1c(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movslq -0x70(%rbp), %rcx
addq %rcx, %rax
movzbl (%rax), %eax
movl %eax, -0x74(%rbp)
cmpl $0xff, -0x74(%rbp)
jne 0x9b9cf
movq -0x58(%rbp), %rax
movb $0x0, 0x3(%rax)
movl $0x1, -0x78(%rbp)
jmp 0x9b9f5
movq -0x58(%rbp), %rax
movb $-0x1, 0x3(%rax)
movq -0x10(%rbp), %rax
movslq 0x34(%rax), %rcx
leaq 0x339c5a(%rip), %rax    # 0x3d5640
movq (%rax,%rcx,8), %rax
movslq -0x74(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x74(%rbp)
leaq 0x45280(%rip), %rax     # 0xe0c7c
cmpl $0x0, (%rax)
je 0x9ba46
movslq -0x74(%rbp), %rcx
leaq 0x3d57f4(%rip), %rax    # 0x471200
movb (%rax,%rcx,4), %cl
movq -0x58(%rbp), %rax
movb %cl, (%rax)
movslq -0x74(%rbp), %rcx
leaq 0x3d57e0(%rip), %rax    # 0x471200
movb 0x1(%rax,%rcx,4), %cl
movq -0x58(%rbp), %rax
movb %cl, 0x1(%rax)
movslq -0x74(%rbp), %rcx
leaq 0x3d57ca(%rip), %rax    # 0x471200
movb 0x2(%rax,%rcx,4), %cl
movq -0x58(%rbp), %rax
movb %cl, 0x2(%rax)
jmp 0x9bada
leaq 0x159b17(%rip), %rax    # 0x1f5564
movslq (%rax), %rcx
leaq 0x3d5fa9(%rip), %rax    # 0x471a00
shlq $0x8, %rcx
addq %rcx, %rax
movslq -0x74(%rbp), %rdx
leaq 0x3d5797(%rip), %rcx    # 0x471200
movzbl (%rcx,%rdx,4), %ecx
movb (%rax,%rcx), %cl
movq -0x58(%rbp), %rax
movb %cl, (%rax)
leaq 0x159ae7(%rip), %rax    # 0x1f5564
movslq (%rax), %rcx
leaq 0x3d5f79(%rip), %rax    # 0x471a00
shlq $0x8, %rcx
addq %rcx, %rax
movslq -0x74(%rbp), %rdx
leaq 0x3d5767(%rip), %rcx    # 0x471200
movzbl 0x1(%rcx,%rdx,4), %ecx
movb (%rax,%rcx), %cl
movq -0x58(%rbp), %rax
movb %cl, 0x1(%rax)
leaq 0x159ab5(%rip), %rax    # 0x1f5564
movslq (%rax), %rcx
leaq 0x3d5f47(%rip), %rax    # 0x471a00
shlq $0x8, %rcx
addq %rcx, %rax
movslq -0x74(%rbp), %rdx
leaq 0x3d5735(%rip), %rcx    # 0x471200
movzbl 0x2(%rcx,%rdx,4), %ecx
movb (%rax,%rcx), %cl
movq -0x58(%rbp), %rax
movb %cl, 0x2(%rax)
movl -0x74(%rbp), %eax
cmpl 0x460f1(%rip), %eax     # 0xe1bd4
jl 0x9bb01
cmpl $0xff, -0x74(%rbp)
jge 0x9bb01
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, (%rax)
movl $0x1, -0x7c(%rbp)
jmp 0x9bb03
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
movq -0x58(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x58(%rbp)
movq -0x60(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x60(%rbp)
jmp 0x9b91f
jmp 0x9bb2b
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x9b8c1
jmp 0x9bb3b
movq -0x10(%rbp), %rax
movss 0x172b9(%rip), %xmm0    # 0xb2e00
movss %xmm0, 0x48(%rax)
movq -0x10(%rbp), %rax
movss 0x172a8(%rip), %xmm0    # 0xb2e00
movss %xmm0, 0x4c(%rax)
movq -0x10(%rbp), %rax
movzwl 0x38(%rax), %ecx
andl $-0xd, %ecx
movw %cx, 0x38(%rax)
movq -0x10(%rbp), %rax
movzwl 0x38(%rax), %ecx
orl $0x30, %ecx
movw %cx, 0x38(%rax)
movl -0x78(%rbp), %eax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x494(%rbp), %rdi
callq 0x98920
movl $0x0, -0x484(%rbp)
leaq -0x494(%rbp), %rdi
callq 0x98a80
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movl -0x20(%rbp), %ecx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl %ecx, 0x10(%rax)
movl -0x1c(%rbp), %ecx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl %ecx, 0x14(%rax)
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl %ecx, 0x8(%rax)
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
leaq -0x30(%rbp), %rdx
xorl %ecx, %ecx
movzwl 0x38(%rax), %esi
callq 0x997f0
cmpl $0x0, -0x7c(%rbp)
je 0x9bc82
movl $0x1, -0x484(%rbp)
leaq -0x494(%rbp), %rdi
callq 0x98a80
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x20(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %ecx, 0x10(%rax)
movl -0x1c(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %ecx, 0x14(%rax)
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %ecx, 0x8(%rax)
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl %ecx, 0xc(%rax)
movl $0x1, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
leaq -0x50(%rbp), %rdx
xorl %ecx, %ecx
movzwl 0x38(%rax), %esi
callq 0x997f0
jmp 0x9bc8e
movq -0x10(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x10(%rbp), %rdi
callq 0x9bcd0
cmpq $0x0, -0x30(%rbp)
je 0x9bca7
movq -0x30(%rbp), %rdi
callq 0x61c0
cmpq $0x0, -0x50(%rbp)
je 0x9bcb7
movq -0x50(%rbp), %rdi
callq 0x61c0
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x4a0, %rsp            # imm = 0x4A0
popq %rbp
retq
nopw (%rax,%rax)