// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4a8, %rsp            # imm = 0x4A8
movq %rdi, %rbx
movslq 0x30(%rdi), %rax
leaq 0x3abe9b(%rip), %rcx    # 0x409020
movswl (%rcx,%rax,2), %r13d
movl %r13d, 0x20(%rsp)
leaq 0x39d64a(%rip), %rcx    # 0x3fa7e0
movswl (%rcx,%rax,2), %r12d
movl %r12d, 0x24(%rsp)
leaq 0x11a0a9(%rip), %rcx    # 0x177250
cmpb $0x0, 0x1d(%rcx)
movabsq $0x100000001, %rsi      # imm = 0x100000001
je 0x5d1d1
movl %r12d, %ecx
orl %r13d, %ecx
je 0x5d1fb
movl %r13d, 0x18(%rsp)
movl %r12d, 0x1c(%rsp)
movl %r12d, %r14d
movl %r13d, %r15d
jmp 0x5d207
pushq $0x1
popq %rcx
movl %ecx, %r15d
leal (%r15,%r15), %ecx
cmpl %r13d, %r15d
jl 0x5d1d4
movl %r15d, 0x18(%rsp)
pushq $0x1
popq %rcx
movl %ecx, %r14d
leal (%r14,%r14), %ecx
cmpl %r12d, %r14d
jl 0x5d1e8
movl %r14d, 0x1c(%rsp)
jmp 0x5d207
pushq $0x1
popq %r14
movq %rsi, 0x18(%rsp)
movl %r14d, %r15d
movl $0x1908, 0x28(%rsp)     # imm = 0x1908
movq 0x10(%rsp), %rcx
movq %rcx, 0x50(%rsp)
movl 0x18(%rsp), %ecx
movl %ecx, 0x58(%rsp)
movl 0x1c(%rsp), %ecx
movl %ecx, 0x5c(%rsp)
movq 0x20(%rsp), %rcx
movq %rcx, 0x60(%rsp)
movl 0x28(%rsp), %ecx
movl %ecx, 0x68(%rsp)
movl 0x2c(%rsp), %ecx
movl %ecx, 0x6c(%rsp)
leaq 0x335096(%rip), %rbp    # 0x3922e0
cmpq $0x0, (%rbp,%rax,8)
jne 0x5d25a
movswl %ax, %edi
callq 0x2cc4a
movq %r14, 0x38(%rsp)
movl %r14d, %eax
movq %r15, 0x40(%rsp)
imull %r15d, %eax
movslq %eax, %r14
shlq $0x2, %r14
movq %r14, %rdi
callq 0x69a0
movq %rax, %r15
movq %rax, 0x10(%rsp)
xorl %eax, %eax
testq %r15, %r15
je 0x5d5ca
pushq $0x1
popq %rdi
movq %r14, %rsi
callq 0x6930
movq %rax, 0x50(%rsp)
testq %rax, %rax
je 0x5d499
movq %rax, %r14
movslq 0x30(%rbx), %rax
movq (%rbp,%rax,8), %rax
movq %rax, 0x80(%rsp)
testq %rax, %rax
je 0x5d4a8
movslq %r12d, %rcx
leaq 0x404bf(%rip), %rax     # 0x9d78c
movl (%rax), %edx
leaq 0x15472e(%rip), %rax    # 0x1b1a04
movslq (%rax), %rsi
movl 0x412b1(%rip), %eax     # 0x9e590
movl %eax, 0x34(%rsp)
movq 0x40(%rsp), %rax
testl %eax, %eax
movl $0x0, %r9d
cmovgl %eax, %r9d
movq 0x38(%rsp), %rdi
testl %edi, %edi
movl $0x0, %r8d
cmovgl %edi, %r8d
movq %r8, 0x88(%rsp)
cltq
shlq $0x2, %rax
movq %rax, 0x78(%rsp)
shlq $0x8, %rsi
addq 0x2ecad(%rip), %rsi     # 0x8bfd0
movq %r14, 0x70(%rsp)
movq %r14, 0x48(%rsp)
xorl %r14d, %r14d
movl $0x0, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
cmpq 0x88(%rsp), %r14
je 0x5d485
cmpq %rcx, %r14
movq %rcx, %rax
movl $0x0, %edi
cmovlq %rdi, %rax
movq %r14, %r11
subq %rax, %r11
addq 0x80(%rsp), %r11
xorl %r8d, %r8d
cmpl %r8d, %r9d
je 0x5d470
cmpq %rcx, %r14
setl %al
cmpl %r13d, %r8d
setl %r10b
testb $0x1, 0x38(%rbx)
je 0x5d399
andb %al, %r10b
jne 0x5d399
andl $0x0, (%r15,%r8,4)
jmp 0x5d468
cmpl %r13d, %r8d
movl %r13d, %eax
movl $0x0, %edi
cmovll %edi, %eax
movl %r8d, %r10d
subl %eax, %r10d
imull %r12d, %r10d
movslq %r10d, %rax
movzbl (%r11,%rax), %eax
cmpq $0xff, %rax
je 0x5d3dd
movb $-0x1, 0x3(%r15,%r8,4)
movslq 0x34(%rbx), %r10
leaq 0x33470e(%rip), %rdi    # 0x391ae0
movq (%rdi,%r10,8), %r10
movzbl (%r10,%rax), %eax
jmp 0x5d3ef
movb $0x0, 0x3(%r15,%r8,4)
movl $0xff, %eax
pushq $0x1
popq %rdi
movl %edi, 0x8(%rsp)
movl %r12d, %edi
leal (,%rax,4), %r10d
addq 0x2ebc7(%rip), %r10     # 0x8bfc8
testl %edx, %edx
je 0x5d40f
leaq 0x1(%r10), %r12
leaq 0x2(%r10), %rbp
jmp 0x5d426
movzbl 0x1(%r10), %r12d
movzbl 0x2(%r10), %ebp
movzbl (%r10), %r10d
addq %rsi, %r10
addq %rsi, %r12
addq %rsi, %rbp
movb (%rbp), %bpl
movb (%r12), %r12b
movb (%r10), %r10b
movb %r10b, (%r15,%r8,4)
movb %r12b, 0x1(%r15,%r8,4)
movb %bpl, 0x2(%r15,%r8,4)
cmpl $0xff, %eax
je 0x5d465
cmpl 0x34(%rsp), %eax
movl %edi, %r12d
jl 0x5d468
movl (%r15,%r8,4), %eax
movq 0x48(%rsp), %rdi
movl %eax, (%rdi,%r8,4)
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
jmp 0x5d468
movl %edi, %r12d
incq %r8
jmp 0x5d36e
incq %r14
movq 0x78(%rsp), %rax
addq %rax, %r15
addq %rax, 0x48(%rsp)
jmp 0x5d340
cmpl $0x0, 0xc(%rsp)
sete %bpl
movq 0x70(%rsp), %r14
movl 0x8(%rsp), %ecx
jmp 0x5d4cb
movq %r15, %rdi
callq 0x61c0
xorl %eax, %eax
jmp 0x5d5ca
movl $0x1000000, (%r15)      # imm = 0x1000000
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x20(%rsp)
movb $0x1, %bpl
pushq $0x1
popq %r12
movl %r12d, %r13d
movl %r12d, %ecx
movsd 0x11fcd(%rip), %xmm0    # 0x6f4a0
movsd %xmm0, 0x48(%rbx)
movl 0x38(%rbx), %eax
andl $-0x3d, %eax
orl $0x30, %eax
movw %ax, 0x38(%rbx)
leaq 0x10(%rsp), %rax
movl %ecx, 0x1c(%rax)
leaq 0x90(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5b6e8
andl $0x0, 0x10(%r15)
movq %r15, %rdi
callq 0x5b7b0
movq %rax, (%rbx)
movl %r13d, 0x10(%rax)
movq (%rbx), %rax
movl %r12d, 0x14(%rax)
movq (%rbx), %rax
movq 0x40(%rsp), %rcx
movl %ecx, 0x8(%rax)
movq (%rbx), %rax
movq 0x38(%rsp), %rcx
movl %ecx, 0xc(%rax)
movq (%rbx), %rdi
movzwl 0x38(%rbx), %esi
leaq 0x10(%rsp), %rdx
xorl %ecx, %ecx
callq 0x5bf2e
testb %bpl, %bpl
je 0x5d550
andq $0x0, 0x8(%rbx)
jmp 0x5d5ad
pushq $0x1
popq %rbp
leaq 0x90(%rsp), %rdi
movl %ebp, 0x10(%rdi)
callq 0x5b7b0
movq %rax, 0x8(%rbx)
movl 0x20(%rsp), %ecx
movl %ecx, 0x10(%rax)
movl 0x24(%rsp), %eax
movq 0x8(%rbx), %rcx
movl %eax, 0x14(%rcx)
movl 0x18(%rsp), %eax
movq 0x8(%rbx), %rcx
movl %eax, 0x8(%rcx)
movl 0x1c(%rsp), %eax
movq 0x8(%rbx), %rcx
movl %eax, 0xc(%rcx)
leaq 0x50(%rsp), %r14
movl %ebp, 0x1c(%r14)
movq 0x8(%rbx), %rdi
movzwl 0x38(%rbx), %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x5bf2e
movq (%r14), %r14
movq %rbx, %rdi
callq 0x5d5dc
movq 0x10(%rsp), %rdi
callq 0x61c0
movq %r14, %rdi
callq 0x61c0
pushq $0x1
popq %rax
addq $0x4a8, %rsp            # imm = 0x4A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq