// File: /workspace/github_c_makefile_star2_O2/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), %rax
movl 0xc(%rdi), %edx
leal -0x1(%rax), %r12d
decl %edx
testl %esi, %esi
je 0x5cacf
movl 0x10(%rdi), %r8d
movl 0x14(%rdi), %r9d
cmpl %r8d, %r12d
movl %r12d, %esi
cmovgel %r8d, %esi
cmpl %r9d, %edx
movl %edx, %ecx
cmovgel %r9d, %ecx
decl %r8d
decl %r9d
movl %r9d, %edx
movl %r8d, %r12d
jmp 0x5cad4
movl %r12d, %esi
movl %edx, %ecx
movslq %ecx, %rcx
movslq %edx, %rdx
movslq %esi, %r8
movl %r8d, %esi
movq %rcx, %r9
imulq %rax, %r9
leaq (,%r8,4), %r10
leaq (%r10,%r9,4), %r10
leaq (,%rax,4), %r9
movq %r9, -0x28(%rsp)
leaq 0x4(,%rcx,4), %r9
imulq %rax, %r9
leaq (%r9,%r8,4), %r11
addq $0x3, %r11
leaq -0x4(,%rcx,4), %r9
imulq %rax, %r9
leaq (%r9,%r8,4), %r8
addq $0x3, %r8
leaq 0x1440d(%rip), %r14     # 0x70f3c
movq %rdi, -0x20(%rsp)
testq %rcx, %rcx
js 0x5ccef
movq (%rdi), %r15
movq %r10, -0x8(%rsp)
leaq (%r15,%r10), %rbp
movq %r11, -0x10(%rsp)
leaq (%r15,%r11), %r13
movq %r8, -0x18(%rsp)
addq %r8, %r15
xorl %ebx, %ebx
leaq (%rsi,%rbx), %r11
testl %r11d, %r11d
js 0x5ccc5
cmpb $0x0, 0x3(%rbp,%rbx,4)
jne 0x5ccbd
xorl %r9d, %r9d
testl %r11d, %r11d
je 0x5cbaf
cmpb $0x0, -0x1(%rbp,%rbx,4)
movl $0x0, %r8d
movl $0x0, %edi
movl $0x0, %r10d
je 0x5cbb7
movzbl -0x4(%rbp,%rbx,4), %r9d
movzbl -0x3(%rbp,%rbx,4), %r8d
movzbl -0x2(%rbp,%rbx,4), %edi
movl $0x1, %eax
movl %eax, %r10d
jmp 0x5cbb7
xorl %r8d, %r8d
xorl %edi, %edi
xorl %r10d, %r10d
cmpl %r12d, %r11d
jge 0x5cbe1
cmpb $0x0, 0x7(%rbp,%rbx,4)
je 0x5cbe1
movzbl 0x4(%rbp,%rbx,4), %r11d
addl %r11d, %r9d
movzbl 0x5(%rbp,%rbx,4), %r11d
addl %r11d, %r8d
movzbl 0x6(%rbp,%rbx,4), %r11d
addl %r11d, %edi
incl %r10d
testq %rcx, %rcx
je 0x5cc0b
cmpb $0x0, (%r15,%rbx,4)
je 0x5cc0b
movzbl -0x3(%r15,%rbx,4), %r11d
addl %r11d, %r9d
movzbl -0x2(%r15,%rbx,4), %r11d
addl %r11d, %r8d
movzbl -0x1(%r15,%rbx,4), %r11d
addl %r11d, %edi
incl %r10d
cmpq %rdx, %rcx
jge 0x5cc36
cmpb $0x0, (%r13,%rbx,4)
je 0x5cc36
movzbl -0x3(%r13,%rbx,4), %r11d
addl %r11d, %r9d
movzbl -0x2(%r13,%rbx,4), %r11d
addl %r11d, %r8d
movzbl -0x1(%r13,%rbx,4), %r11d
addl %r11d, %edi
incl %r10d
decl %r10d
cmpl $0x3, %r10d
ja 0x5ccbd
movslq (%r14,%r10,4), %r10
addq %r14, %r10
jmpq *%r10
movb %r9b, (%rbp,%rbx,4)
movb %r8b, 0x1(%rbp,%rbx,4)
jmp 0x5ccb8
imull $0x55, %r9d, %eax
movl $0x80, %r9d
addl %r9d, %eax
movb %ah, (%rbp,%rbx,4)
imull $0x55, %r8d, %eax
addl %r9d, %eax
movb %ah, 0x1(%rbp,%rbx,4)
imull $0x55, %edi, %edi
addl %r9d, %edi
shrl $0x8, %edi
jmp 0x5ccb8
addl $0x2, %r9d
shrl $0x2, %r9d
movb %r9b, (%rbp,%rbx,4)
addl $0x2, %r8d
shrl $0x2, %r8d
movb %r8b, 0x1(%rbp,%rbx,4)
addl $0x2, %edi
shrl $0x2, %edi
jmp 0x5ccb8
incl %r9d
shrl %r9d
movb %r9b, (%rbp,%rbx,4)
incl %r8d
shrl %r8d
movb %r8b, 0x1(%rbp,%rbx,4)
incl %edi
shrl %edi
movb %dil, 0x2(%rbp,%rbx,4)
decq %rbx
jmp 0x5cb5c
decq %rcx
movq -0x8(%rsp), %r10
movq -0x28(%rsp), %rax
subq %rax, %r10
movq -0x10(%rsp), %r11
subq %rax, %r11
movq -0x18(%rsp), %r8
subq %rax, %r8
movq -0x20(%rsp), %rdi
jmp 0x5cb34
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq