// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/polymosttex.c
// Function: ptm_fixtransparency
static void ptm_fixtransparency(PTTexture * tex, int clamped)
{
	coltype *wpptr;
	int j, x, y, r, g, b;
	int dox, doy, naxsiz2;
	int daxsiz = tex->tsizx, daysiz = tex->tsizy;
	int daxsiz2 = tex->sizx, daysiz2 = tex->sizy;

	dox = daxsiz2-1;
	doy = daysiz2-1;
	if (clamped) {
		dox = min(dox,daxsiz);
		doy = min(doy,daysiz);
	} else {
		// Make repeating textures duplicate top/left parts
		daxsiz = daxsiz2;
		daysiz = daysiz2;
	}

	daxsiz--; daysiz--; naxsiz2 = -daxsiz2; // Hacks for optimization inside loop

	// Set transparent pixels to average color of neighboring opaque pixels
	// Doing this makes bilinear filtering look much better for masked textures (I.E. sprites)
	for (y = doy; y >= 0; y--) {
		wpptr = &tex->pic[y*daxsiz2+dox];
		for (x = dox; x >= 0; x--, wpptr--) {
			if (wpptr->a) {
				continue;
			}
			r = g = b = j = 0;
			if ((x>     0) && (wpptr[     -1].a)) {
				r += (int)wpptr[     -1].r;
				g += (int)wpptr[     -1].g;
				b += (int)wpptr[     -1].b;
				j++;
			}
			if ((x<daxsiz) && (wpptr[     +1].a)) {
				r += (int)wpptr[     +1].r;
				g += (int)wpptr[     +1].g;
				b += (int)wpptr[     +1].b;
				j++;
			}
			if ((y>     0) && (wpptr[naxsiz2].a)) {
				r += (int)wpptr[naxsiz2].r;
				g += (int)wpptr[naxsiz2].g;
				b += (int)wpptr[naxsiz2].b;
				j++;
			}
			if ((y<daysiz) && (wpptr[daxsiz2].a)) {
				r += (int)wpptr[daxsiz2].r;
				g += (int)wpptr[daxsiz2].g;
				b += (int)wpptr[daxsiz2].b;
				j++;
			}
			switch (j) {
				case 1: wpptr->r =   r            ;
					wpptr->g =   g            ;
					wpptr->b =   b            ;
					break;
				case 2: wpptr->r = ((r   +  1)>>1);
					wpptr->g = ((g   +  1)>>1);
					wpptr->b = ((b   +  1)>>1);
					break;
				case 3: wpptr->r = ((r*85+128)>>8);
					wpptr->g = ((g*85+128)>>8);
					wpptr->b = ((b*85+128)>>8);
					break;
				case 4: wpptr->r = ((r   +  2)>>2);
					wpptr->g = ((g   +  2)>>2);
					wpptr->b = ((b   +  2)>>2);
					break;
				default: break;
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movslq 0x8(%rdi), %rcx
movl 0xc(%rdi), %edx
leal -0x1(%rcx), %eax
decl %edx
testl %esi, %esi
movq %rdi, -0x10(%rsp)
je 0x676a9
movl 0x10(%rdi), %r9d
movl 0x14(%rdi), %r8d
cmpl %r9d, %eax
cmovgel %r9d, %eax
movl %eax, -0x34(%rsp)
cmpl %r8d, %edx
movl %edx, %esi
cmovgel %r8d, %esi
decl %r9d
decl %r8d
movl %r8d, %edx
movl %r9d, %eax
jmp 0x676af
movl %eax, -0x34(%rsp)
movl %edx, %esi
testl %esi, %esi
js 0x678eb
movl %esi, %r8d
movslq -0x34(%rsp), %r9
movslq %edx, %rsi
movq %rcx, %rdx
imulq %r8, %rdx
leaq (,%r9,4), %rdi
leaq (%rdi,%rdx,4), %rdx
movq %rdx, -0x20(%rsp)
leaq (,%rcx,4), %rdx
movq %rdx, -0x8(%rsp)
leaq -0x4(,%r8,4), %rdx
imulq %rcx, %rdx
leaq (%rdx,%r9,4), %rdx
addq $0x3, %rdx
movq %rdx, -0x28(%rsp)
leaq 0x4(,%r8,4), %rdx
imulq %rcx, %rdx
leaq (%rdx,%r9,4), %rcx
addq $0x3, %rcx
movq %rcx, -0x30(%rsp)
movl %r9d, %r14d
incl %r9d
movq %r9, -0x18(%rsp)
leaq 0x14ad1(%rip), %rbx     # 0x7c1fc
cmpl $0x0, -0x34(%rsp)
js 0x678c7
movq -0x10(%rsp), %rcx
movq (%rcx), %r12
movq -0x20(%rsp), %rcx
leaq (%r12,%rcx), %rdx
movq -0x28(%rsp), %rcx
leaq (%r12,%rcx), %r13
addq -0x30(%rsp), %r12
movq -0x18(%rsp), %rcx
movl %ecx, %edi
xorl %ecx, %ecx
cmpb $0x0, 0x3(%rdx,%rcx,4)
jne 0x678ba
leaq (%r14,%rcx), %r11
xorl %r15d, %r15d
testl %r11d, %r11d
je 0x677a6
cmpb $0x0, -0x1(%rdx,%rcx,4)
movl $0x0, %r10d
movl $0x0, %ebp
movl $0x0, %r9d
je 0x677ae
movzbl -0x4(%rdx,%rcx,4), %r15d
movzbl -0x3(%rdx,%rcx,4), %r10d
movzbl -0x2(%rdx,%rcx,4), %ebp
movl $0x1, %r9d
jmp 0x677ae
xorl %r10d, %r10d
xorl %ebp, %ebp
xorl %r9d, %r9d
cmpl %eax, %r11d
jge 0x677d8
cmpb $0x0, 0x7(%rdx,%rcx,4)
je 0x677d8
movzbl 0x4(%rdx,%rcx,4), %r11d
addl %r11d, %r15d
movzbl 0x5(%rdx,%rcx,4), %r11d
addl %r11d, %r10d
movzbl 0x6(%rdx,%rcx,4), %r11d
addl %r11d, %ebp
incl %r9d
testq %r8, %r8
je 0x67803
cmpb $0x0, (%r13,%rcx,4)
je 0x67803
movzbl -0x3(%r13,%rcx,4), %r11d
addl %r11d, %r15d
movzbl -0x2(%r13,%rcx,4), %r11d
addl %r11d, %r10d
movzbl -0x1(%r13,%rcx,4), %r11d
addl %r11d, %ebp
incl %r9d
cmpq %rsi, %r8
jge 0x6782d
cmpb $0x0, (%r12,%rcx,4)
je 0x6782d
movzbl -0x3(%r12,%rcx,4), %r11d
addl %r11d, %r15d
movzbl -0x2(%r12,%rcx,4), %r11d
addl %r11d, %r10d
movzbl -0x1(%r12,%rcx,4), %r11d
addl %r11d, %ebp
incl %r9d
decl %r9d
cmpl $0x3, %r9d
ja 0x678ba
movslq (%rbx,%r9,4), %r9
addq %rbx, %r9
jmpq *%r9
movb %r15b, (%rdx,%rcx,4)
movb %r10b, 0x1(%rdx,%rcx,4)
jmp 0x678b5
movq %rbx, %r9
imull $0x55, %r15d, %ebx
movl $0x80, %r11d
addl %r11d, %ebx
movb %bh, (%rdx,%rcx,4)
imull $0x55, %r10d, %ebx
addl %r11d, %ebx
movb %bh, 0x1(%rdx,%rcx,4)
movq %r9, %rbx
imull $0x55, %ebp, %ebp
addl %r11d, %ebp
shrl $0x8, %ebp
jmp 0x678b5
addl $0x2, %r15d
shrl $0x2, %r15d
movb %r15b, (%rdx,%rcx,4)
addl $0x2, %r10d
shrl $0x2, %r10d
movb %r10b, 0x1(%rdx,%rcx,4)
addl $0x2, %ebp
shrl $0x2, %ebp
jmp 0x678b5
incl %r15d
shrl %r15d
movb %r15b, (%rdx,%rcx,4)
incl %r10d
shrl %r10d
movb %r10b, 0x1(%rdx,%rcx,4)
incl %ebp
shrl %ebp
movb %bpl, 0x2(%rdx,%rcx,4)
decq %rcx
decl %edi
testl %edi, %edi
jg 0x6775e
leaq -0x1(%r8), %rcx
movq -0x8(%rsp), %rdx
subq %rdx, -0x20(%rsp)
subq %rdx, -0x28(%rsp)
subq %rdx, -0x30(%rsp)
testq %r8, %r8
movq %rcx, %r8
jg 0x6772b
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq