// File: /workspace/github_c_makefile_star2_O1/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
movl 0x8(%rdi), %edx
movl 0xc(%rdi), %eax
leal -0x1(%rdx), %r9d
leal -0x1(%rax), %ecx
testl %esi, %esi
movq %rdi, -0x10(%rsp)
je 0x6599f
movl 0x10(%rdi), %eax
cmpl %eax, %r9d
movl %eax, %r11d
cmovgel %eax, %r9d
movl 0x14(%rdi), %eax
cmpl %eax, %ecx
cmovgel %eax, %ecx
jmp 0x659a2
movl %edx, %r11d
testl %ecx, %ecx
js 0x65bd1
movslq %edx, %rdx
decl %r11d
decl %eax
movl %ecx, %r8d
movslq %r9d, %r10
movslq %eax, %rsi
leaq (,%rdx,4), %rax
movq %rax, -0x8(%rsp)
leaq -0x4(,%r8,4), %rax
imulq %rdx, %rax
leaq 0x4(,%r8,4), %rcx
imulq %rdx, %rcx
imulq %r8, %rdx
leaq (,%r10,4), %rdi
leaq (%rdi,%rdx,4), %rdx
movq %rdx, -0x20(%rsp)
leaq (%rax,%r10,4), %rax
addq $0x3, %rax
movq %rax, -0x28(%rsp)
leaq (%rcx,%r10,4), %rbx
addq $0x3, %rbx
movl %r10d, %r14d
incl %r10d
movq %r10, -0x18(%rsp)
movl %r9d, -0x2c(%rsp)
testl %r9d, %r9d
js 0x65baa
movq -0x10(%rsp), %rax
movq (%rax), %r12
movq -0x20(%rsp), %rax
leaq (%r12,%rax), %rdx
movq -0x28(%rsp), %rax
leaq (%r12,%rax), %r13
addq %rbx, %r12
movq -0x18(%rsp), %rax
movl %eax, %edi
xorl %eax, %eax
cmpb $0x0, 0x3(%rdx,%rax,4)
jne 0x65b9d
leaq (%r14,%rax), %r10
xorl %r15d, %r15d
testl %r10d, %r10d
je 0x65a8f
cmpb $0x0, -0x1(%rdx,%rax,4)
movl $0x0, %r9d
movl $0x0, %ebp
movl $0x0, %ecx
je 0x65a96
movzbl -0x4(%rdx,%rax,4), %r15d
movzbl -0x3(%rdx,%rax,4), %r9d
movzbl -0x2(%rdx,%rax,4), %ebp
movl $0x1, %ecx
jmp 0x65a96
xorl %r9d, %r9d
xorl %ebp, %ebp
xorl %ecx, %ecx
cmpl %r11d, %r10d
jge 0x65abf
cmpb $0x0, 0x7(%rdx,%rax,4)
je 0x65abf
movzbl 0x4(%rdx,%rax,4), %r10d
addl %r10d, %r15d
movzbl 0x5(%rdx,%rax,4), %r10d
addl %r10d, %r9d
movzbl 0x6(%rdx,%rax,4), %r10d
addl %r10d, %ebp
incl %ecx
testq %r8, %r8
je 0x65ae9
cmpb $0x0, (%r13,%rax,4)
je 0x65ae9
movzbl -0x3(%r13,%rax,4), %r10d
addl %r10d, %r15d
movzbl -0x2(%r13,%rax,4), %r10d
addl %r10d, %r9d
movzbl -0x1(%r13,%rax,4), %r10d
addl %r10d, %ebp
incl %ecx
cmpq %rsi, %r8
jge 0x65b12
cmpb $0x0, (%r12,%rax,4)
je 0x65b12
movzbl -0x3(%r12,%rax,4), %r10d
addl %r10d, %r15d
movzbl -0x2(%r12,%rax,4), %r10d
addl %r10d, %r9d
movzbl -0x1(%r12,%rax,4), %r10d
addl %r10d, %ebp
incl %ecx
decl %ecx
cmpl $0x3, %ecx
ja 0x65b9d
leaq 0x15218(%rip), %r10     # 0x7ad3c
movslq (%r10,%rcx,4), %rcx
addq %r10, %rcx
jmpq *%rcx
movb %r15b, (%rdx,%rax,4)
movb %r9b, 0x1(%rdx,%rax,4)
jmp 0x65b98
imull $0x55, %r15d, %ecx
movl $0x80, %r10d
addl %r10d, %ecx
movb %ch, (%rdx,%rax,4)
imull $0x55, %r9d, %ecx
addl %r10d, %ecx
movb %ch, 0x1(%rdx,%rax,4)
imull $0x55, %ebp, %ebp
addl %r10d, %ebp
shrl $0x8, %ebp
jmp 0x65b98
addl $0x2, %r15d
shrl $0x2, %r15d
movb %r15b, (%rdx,%rax,4)
addl $0x2, %r9d
shrl $0x2, %r9d
movb %r9b, 0x1(%rdx,%rax,4)
addl $0x2, %ebp
shrl $0x2, %ebp
jmp 0x65b98
incl %r15d
shrl %r15d
movb %r15b, (%rdx,%rax,4)
incl %r9d
shrl %r9d
movb %r9b, 0x1(%rdx,%rax,4)
incl %ebp
shrl %ebp
movb %bpl, 0x2(%rdx,%rax,4)
decq %rax
decl %edi
testl %edi, %edi
jg 0x65a49
leaq -0x1(%r8), %rax
movq -0x8(%rsp), %rcx
subq %rcx, -0x20(%rsp)
subq %rcx, -0x28(%rsp)
subq %rcx, %rbx
testq %r8, %r8
movq %rax, %r8
movl -0x2c(%rsp), %r9d
jg 0x65a1a
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq