// File: /workspace/github_c_makefile_star2_O3/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
movslq 0x30(%rdi), %rax
leaq 0x3a82ad(%rip), %rcx    # 0x410020
movswl (%rcx,%rax,2), %r13d
movl %r13d, 0x28(%rsp)
leaq 0x399a5c(%rip), %rcx    # 0x4017e0
movswl (%rcx,%rax,2), %ebp
movl %ebp, 0x2c(%rsp)
leaq 0x1164bd(%rip), %rcx    # 0x17e250
cmpb $0x0, 0x1d(%rcx)
je 0x67db1
movl %ebp, %ecx
orl %r13d, %ecx
je 0x67ddc
movl %r13d, 0x20(%rsp)
movl %ebp, 0x24(%rsp)
movl %ebp, %r14d
movl %r13d, %ebx
jmp 0x67df6
movl $0x1, %ecx
movl %ecx, %ebx
leal (%rbx,%rbx), %ecx
cmpl %r13d, %ebx
jl 0x67db6
movl %ebx, 0x20(%rsp)
movl $0x1, %ecx
movl %ecx, %r14d
leal (%r14,%r14), %ecx
cmpl %ebp, %r14d
jl 0x67dc9
movl %r14d, 0x24(%rsp)
jmp 0x67df6
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x20(%rsp)
movl $0x1, %r14d
movl $0x1, %ebx
movq %rdi, 0x8(%rsp)
movl $0x1908, 0x30(%rsp)     # imm = 0x1908
movq 0x18(%rsp), %rcx
movq %rcx, 0x50(%rsp)
movl 0x20(%rsp), %ecx
movl %ecx, 0x58(%rsp)
movl 0x24(%rsp), %ecx
movl %ecx, 0x5c(%rsp)
movq 0x28(%rsp), %rcx
movq %rcx, 0x60(%rsp)
movl 0x30(%rsp), %ecx
movl %ecx, 0x68(%rsp)
movl 0x34(%rsp), %ecx
movl %ecx, 0x6c(%rsp)
leaq 0x3314a2(%rip), %r12    # 0x3992e0
cmpq $0x0, (%r12,%rax,8)
jne 0x67e4d
movswl %ax, %edi
callq 0x2fc7e
movq %r14, 0x48(%rsp)
movl %r14d, %eax
movq %rbx, 0x38(%rsp)
imull %ebx, %eax
movslq %eax, %r15
shlq $0x2, %r15
movq %r15, %rdi
callq 0x69a0
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
testq %rax, %rax
je 0x681d7
movq %rax, %r14
movl $0x1, %edi
movq %r15, %rsi
callq 0x6930
movq %rax, 0x50(%rsp)
testq %rax, %rax
je 0x6808d
movq 0x8(%rsp), %rdx
movslq 0x30(%rdx), %rcx
movq (%r12,%rcx,8), %rcx
movq %rcx, 0x78(%rsp)
testq %rcx, %rcx
je 0x68095
movq 0x48(%rsp), %rdi
testl %edi, %edi
jle 0x680bf
movslq %ebp, %rsi
leaq 0x3c8c0(%rip), %rcx     # 0xa478c
movl (%rcx), %ecx
movl %ecx, 0x44(%rsp)
leaq 0x150b2b(%rip), %rcx    # 0x1b8a04
movslq (%rcx), %r8
movl 0x3d6ae(%rip), %ecx     # 0xa5590
movl %ecx, 0x40(%rsp)
movslq 0x38(%rsp), %rcx
movl %edi, %edx
movq %rdx, 0x80(%rsp)
movl %ecx, %r15d
shlq $0x2, %rcx
movq %rcx, 0x88(%rsp)
shlq $0x8, %r8
addq 0x2b0c9(%rip), %r8      # 0x92fd8
xorl %r10d, %r10d
movl $0x0, 0x14(%rsp)
movl $0x0, 0x10(%rsp)
cmpl $0x0, 0x38(%rsp)
jle 0x6805b
cmpq %rsi, %r10
movq %rsi, %rcx
movl $0x0, %edx
cmovlq %rdx, %rcx
movq %r10, %r11
subq %rcx, %r11
movq 0x8(%rsp), %rcx
movzwl 0x38(%rcx), %ecx
addq 0x78(%rsp), %r11
xorl %edx, %edx
cmpq %rsi, %r10
setl %r9b
cmpl %r13d, %edx
setl %bl
testb $0x1, %cl
je 0x67f76
andb %r9b, %bl
jne 0x67f76
movl $0x0, (%r14,%rdx,4)
jmp 0x6804f
cmpl %r13d, %edx
movl %r13d, %r9d
movl $0x0, %edi
cmovll %edi, %r9d
movl %edx, %ebx
subl %r9d, %ebx
imull %ebp, %ebx
movslq %ebx, %r9
movzbl (%r11,%r9), %r9d
cmpq $0xff, %r9
je 0x67fbf
movb $-0x1, 0x3(%r14,%rdx,4)
movq 0x8(%rsp), %rbx
movslq 0x34(%rbx), %rbx
leaq 0x330b2c(%rip), %rdi    # 0x398ae0
movq (%rdi,%rbx,8), %rbx
movzbl (%rbx,%r9), %r9d
jmp 0x67fd3
movb $0x0, 0x3(%r14,%rdx,4)
movl $0x1, 0x10(%rsp)
movl $0xff, %r9d
movl %ebp, %edi
leal (,%r9,4), %r12d
addq 0x2afec(%rip), %r12     # 0x92fd0
cmpl $0x0, 0x44(%rsp)
je 0x67ff7
leaq 0x1(%r12), %rbx
leaq 0x2(%r12), %rbp
jmp 0x68011
movzbl 0x1(%r12), %ebx
movzbl 0x2(%r12), %ebp
movzbl (%r12), %r12d
addq %r8, %r12
addq %r8, %rbx
addq %r8, %rbp
movb (%rbp), %bpl
movb (%rbx), %bl
movb (%r12), %r12b
movb %r12b, (%r14,%rdx,4)
movb %bl, 0x1(%r14,%rdx,4)
movb %bpl, 0x2(%r14,%rdx,4)
cmpl $0xff, %r9d
je 0x6804d
cmpl 0x40(%rsp), %r9d
movl %edi, %ebp
jl 0x6804f
movl (%r14,%rdx,4), %r9d
movl %r9d, (%rax,%rdx,4)
movl $0x1, 0x14(%rsp)
jmp 0x6804f
movl %edi, %ebp
incq %rdx
cmpl %edx, %r15d
jne 0x67f52
incq %r10
movq 0x88(%rsp), %rcx
addq %rcx, %rax
addq %rcx, %r14
cmpq 0x80(%rsp), %r10
jne 0x67f22
cmpl $0x0, 0x14(%rsp)
sete %bl
movq 0x8(%rsp), %rdx
movl 0x10(%rsp), %ecx
jmp 0x680c3
movq %r14, %rdi
jmp 0x681d2
movl $0x1000000, (%r14)      # imm = 0x1000000
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x28(%rsp)
movb $0x1, %bl
movl $0x1, %ebp
movl $0x1, %r13d
movl $0x1, %ecx
jmp 0x680c3
movb $0x1, %bl
xorl %ecx, %ecx
movsd 0x12665(%rip), %xmm0    # 0x7a730
movsd %xmm0, 0x48(%rdx)
movl 0x38(%rdx), %eax
andl $-0x3d, %eax
orl $0x30, %eax
movw %ax, 0x38(%rdx)
leaq 0x18(%rsp), %r14
movl %ecx, 0x1c(%r14)
leaq 0x90(%rsp), %r15
movq %r15, %rdi
movq 0x8(%rsp), %rsi
callq 0x66184
movl $0x0, 0x10(%r15)
movq %r15, %rdi
movq 0x8(%rsp), %r15
callq 0x6624c
movq %rax, (%r15)
movl %r13d, 0x10(%rax)
movq (%r15), %rax
movl %ebp, 0x14(%rax)
movq (%r15), %rax
movq 0x38(%rsp), %rcx
movl %ecx, 0x8(%rax)
movq (%r15), %rax
movq 0x48(%rsp), %rcx
movl %ecx, 0xc(%rax)
movq (%r15), %rdi
movzwl 0x38(%r15), %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x66a6c
testb %bl, %bl
je 0x68153
movq $0x0, 0x8(%r15)
jmp 0x681ac
movl $0x1, %ebx
leaq 0x90(%rsp), %rdi
movl %ebx, 0x10(%rdi)
callq 0x6624c
movq %rax, 0x8(%r15)
movl 0x28(%rsp), %ecx
movl %ecx, 0x10(%rax)
movl 0x2c(%rsp), %eax
movq 0x8(%r15), %rcx
movl %eax, 0x14(%rcx)
movl 0x20(%rsp), %eax
movq 0x8(%r15), %rcx
movl %eax, 0x8(%rcx)
movl 0x24(%rsp), %eax
movq 0x8(%r15), %rcx
movl %eax, 0xc(%rcx)
leaq 0x50(%rsp), %rdx
movl %ebx, 0x1c(%rdx)
movq 0x8(%r15), %rdi
movzwl 0x38(%r15), %esi
xorl %ecx, %ecx
callq 0x66a6c
movq %r15, %rdi
callq 0x681eb
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x681c3
callq 0x61c0
movq 0x50(%rsp), %rdi
movl $0x1, %ebx
testq %rdi, %rdi
je 0x681d7
callq 0x61c0
movl %ebx, %eax
addq $0x4a8, %rsp            # imm = 0x4A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq