// File: /workspace/github_c_makefile_star2_O1/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 $0x488, %rsp            # imm = 0x488
movslq 0x30(%rdi), %rax
leaq 0x3a8fb8(%rip), %rcx    # 0x40f020
movswl (%rcx,%rax,2), %ebp
movl %ebp, 0x10(%rsp)
leaq 0x39a769(%rip), %rcx    # 0x4007e0
movswl (%rcx,%rax,2), %r13d
movl %r13d, 0x14(%rsp)
leaq 0x1171c8(%rip), %rcx    # 0x17d250
cmpb $0x0, 0x1d(%rcx)
je 0x660a0
movl %r13d, %ecx
orl %ebp, %ecx
je 0x660c7
movl %ebp, 0x8(%rsp)
movl %r13d, 0xc(%rsp)
jmp 0x660d6
movl $0x1, %edx
movl %edx, %ecx
leal (%rcx,%rcx), %edx
cmpl %ebp, %ecx
jl 0x660a5
movl %ecx, 0x8(%rsp)
movl $0x1, %edx
movl %edx, %ecx
leal (%rcx,%rcx), %edx
cmpl %r13d, %ecx
jl 0x660b7
movl %ecx, 0xc(%rsp)
jmp 0x660d6
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x8(%rsp)
movq %rdi, 0x28(%rsp)
movl $0x1908, 0x18(%rsp)     # imm = 0x1908
movq (%rsp), %rcx
movq %rcx, 0x40(%rsp)
movl 0x8(%rsp), %ecx
movl %ecx, 0x48(%rsp)
movl 0xc(%rsp), %ecx
movl %ecx, 0x4c(%rsp)
movq 0x10(%rsp), %rcx
movq %rcx, 0x50(%rsp)
movl 0x18(%rsp), %ecx
movl %ecx, 0x58(%rsp)
movl 0x1c(%rsp), %ecx
movl %ecx, 0x5c(%rsp)
leaq 0x3321c3(%rip), %rcx    # 0x3982e0
cmpq $0x0, (%rcx,%rax,8)
jne 0x6612c
movswl %ax, %edi
callq 0x2f106
movl 0x8(%rsp), %ebx
movl 0xc(%rsp), %eax
movq %rax, 0x30(%rsp)
imull %ebx, %eax
movslq %eax, %r12
shlq $0x2, %r12
movq %r12, %rdi
callq 0x69a0
movq %rax, (%rsp)
testq %rax, %rax
je 0x66353
movq %rax, %r14
movq %r12, %rdi
callq 0x69a0
movq %rax, 0x40(%rsp)
testq %rax, %rax
je 0x6635a
movq %rax, %r15
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x62f0
movq 0x28(%rsp), %r12
movslq 0x30(%r12), %rax
leaq 0x33214e(%rip), %rcx    # 0x3982e0
movq (%rcx,%rax,8), %rax
movq %rax, 0x60(%rsp)
testq %rax, %rax
je 0x66361
cmpl $0x0, 0x30(%rsp)
jle 0x66381
movslq %r13d, %rcx
movslq %ebx, %rdx
leaq 0x3d5d0(%rip), %rax     # 0xa378c
movl (%rax), %esi
leaq 0x15183f(%rip), %rax    # 0x1b7a04
movslq (%rax), %rdi
movl 0x3e3c2(%rip), %eax     # 0xa4590
movl %eax, 0x3c(%rsp)
shlq $0x2, %rdx
movq %rdx, 0x68(%rsp)
shlq $0x8, %rdi
addq 0x2bdf2(%rip), %rdi     # 0x91fd8
xorl %r12d, %r12d
movl $0x0, 0x24(%rsp)
movl $0x0, 0x20(%rsp)
testl %ebx, %ebx
jle 0x66326
cmpq %rcx, %r12
movq %rcx, %rax
movl $0x0, %edx
cmovlq %rdx, %rax
movq %r12, %rdx
subq %rax, %rdx
movq 0x28(%rsp), %rax
movzwl 0x38(%rax), %r11d
addq 0x60(%rsp), %rdx
xorl %r10d, %r10d
cmpq %rcx, %r12
setl %al
cmpl %ebp, %r10d
setl %r8b
testb $0x1, %r11b
je 0x6624d
andb %al, %r8b
jne 0x6624d
movl $0x0, (%r14,%r10,4)
jmp 0x6631a
cmpl %ebp, %r10d
movl %ebp, %eax
movl $0x0, %r8d
cmovll %r8d, %eax
movl %r10d, %r8d
subl %eax, %r8d
imull %r13d, %r8d
movslq %r8d, %rax
movzbl (%rdx,%rax), %eax
cmpq $0xff, %rax
je 0x66296
movb $-0x1, 0x3(%r14,%r10,4)
movq 0x28(%rsp), %r8
movslq 0x34(%r8), %r8
leaq 0x331855(%rip), %r9     # 0x397ae0
movq (%r9,%r8,8), %r8
movzbl (%r8,%rax), %r9d
jmp 0x662aa
movb $0x0, 0x3(%r14,%r10,4)
movl $0x1, 0x20(%rsp)
movl $0xff, %r9d
movl %r9d, %eax
leaq 0x3cf6fc(%rip), %r8     # 0x4359b0
leaq (%r8,%rax,4), %rax
movb (%rax), %r8b
testl %esi, %esi
je 0x662d2
movb %r8b, (%r14,%r10,4)
movb 0x1(%rax), %r8b
movb %r8b, 0x1(%r14,%r10,4)
addq $0x2, %rax
jmp 0x662f3
movzbl %r8b, %r8d
movb (%r8,%rdi), %r8b
movb %r8b, (%r14,%r10,4)
movzbl 0x1(%rax), %r8d
movb (%r8,%rdi), %r8b
movb %r8b, 0x1(%r14,%r10,4)
movzbl 0x2(%rax), %eax
addq %rdi, %rax
movb (%rax), %al
movb %al, 0x2(%r14,%r10,4)
cmpl $0xff, %r9d
je 0x6631a
cmpl 0x3c(%rsp), %r9d
jl 0x6631a
movl (%r14,%r10,4), %eax
movl %eax, (%r15,%r10,4)
movl $0x1, 0x24(%rsp)
incq %r10
cmpl %r10d, %ebx
jne 0x66228
incq %r12
movq 0x68(%rsp), %rax
addq %rax, %r15
addq %rax, %r14
cmpq 0x30(%rsp), %r12
jne 0x661f9
cmpl $0x0, 0x24(%rsp)
sete %bpl
movq 0x28(%rsp), %r12
movl 0x20(%rsp), %ecx
jmp 0x66386
xorl %ebx, %ebx
jmp 0x664a3
xorl %ebx, %ebx
jmp 0x6649b
movl $0x1000000, (%r14)      # imm = 0x1000000
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x10(%rsp)
movb $0x1, %bpl
movl $0x1, %ecx
jmp 0x66386
movb $0x1, %bpl
xorl %ecx, %ecx
movabsq $0x3f8000003f800000, %rax # imm = 0x3F8000003F800000
movq %rax, 0x48(%r12)
movl 0x38(%r12), %eax
andl $-0x3d, %eax
orl $0x30, %eax
movw %ax, 0x38(%r12)
movq %rsp, %r14
movl %ecx, 0x1c(%r14)
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x64420
movl $0x0, 0x10(%r15)
movq %r15, %rdi
callq 0x644ee
movq %rax, (%r12)
movl 0x10(%r14), %ecx
movl %ecx, 0x10(%rax)
movl 0x14(%r14), %eax
movq (%r12), %rcx
movl %eax, 0x14(%rcx)
movq (%r12), %rax
movl %ebx, 0x8(%rax)
movq (%r12), %rax
movq 0x30(%rsp), %rcx
movl %ecx, 0xc(%rax)
movq (%r12), %rdi
movzwl 0x38(%r12), %esi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x64d3d
testb %bpl, %bpl
je 0x6641a
movq $0x0, 0x8(%r12)
jmp 0x66476
movl $0x1, %ebx
leaq 0x70(%rsp), %rdi
movl %ebx, 0x10(%rdi)
callq 0x644ee
movq %rax, 0x8(%r12)
movl 0x10(%rsp), %ecx
movl %ecx, 0x10(%rax)
movl 0x14(%rsp), %eax
movq 0x8(%r12), %rcx
movl %eax, 0x14(%rcx)
movl 0x8(%rsp), %eax
movq 0x8(%r12), %rcx
movl %eax, 0x8(%rcx)
movl 0xc(%rsp), %eax
movq 0x8(%r12), %rcx
movl %eax, 0xc(%rcx)
leaq 0x40(%rsp), %rdx
movl %ebx, 0x1c(%rdx)
movq 0x8(%r12), %rdi
movzwl 0x38(%r12), %esi
xorl %ecx, %ecx
callq 0x64d3d
movq %r12, %rdi
callq 0x664b7
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x6648c
callq 0x61c0
movq 0x40(%rsp), %r14
movl $0x1, %ebx
testq %r14, %r14
je 0x664a3
movq %r14, %rdi
callq 0x61c0
movl %ebx, %eax
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq