// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/polymosttex.c
// Function: ptm_mipscale
static void ptm_mipscale(PTTexture * tex)
{
	GLsizei newx, newy;
	GLsizei x, y;
	coltype *wpptr, *rpptr;
	int r, g, b, a, k;

	newx = max(1, (tex->sizx >> 1));
	newy = max(1, (tex->sizy >> 1));

	for (y = 0; y < newy; y++) {
		wpptr = &tex->pic[y * newx];
		rpptr = &tex->pic[(y << 1) * tex->sizx];

		for (x = 0; x < newx; x++, wpptr++, rpptr += 2) {
			r = g = b = a = k = 0;
			if (rpptr[0].a) {
				r += (int)rpptr[0].r;
				g += (int)rpptr[0].g;
				b += (int)rpptr[0].b;
				a += (int)rpptr[0].a;
				k++;
			}
			if ((x+x+1 < tex->sizx) && (rpptr[1].a)) {
				r += (int)rpptr[1].r;
				g += (int)rpptr[1].g;
				b += (int)rpptr[1].b;
				a += (int)rpptr[1].a;
				k++;
			}
			if (y+y+1 < tex->sizy) {
				if (rpptr[tex->sizx].a) {
					r += (int)rpptr[tex->sizx  ].r;
					g += (int)rpptr[tex->sizx  ].g;
					b += (int)rpptr[tex->sizx  ].b;
					a += (int)rpptr[tex->sizx  ].a;
					k++;
				}
				if ((x+x+1 < tex->sizx) && (rpptr[tex->sizx+1].a)) {
					r += (int)rpptr[tex->sizx+1].r;
					g += (int)rpptr[tex->sizx+1].g;
					b += (int)rpptr[tex->sizx+1].b;
					a += (int)rpptr[tex->sizx+1].a;
					k++;
				}
			}
			switch(k) {
				case 0:
				case 1: wpptr->r = r;
					wpptr->g = g;
					wpptr->b = b;
					wpptr->a = a;
					break;
				case 2: wpptr->r = ((r+1)>>1);
					wpptr->g = ((g+1)>>1);
					wpptr->b = ((b+1)>>1);
					wpptr->a = ((a+1)>>1);
					break;
				case 3: wpptr->r = ((r*85+128)>>8);
					wpptr->g = ((g*85+128)>>8);
					wpptr->b = ((b*85+128)>>8);
					wpptr->a = ((a*85+128)>>8);
					break;
				case 4: wpptr->r = ((r+2)>>2);
					wpptr->g = ((g+2)>>2);
					wpptr->b = ((b+2)>>2);
					wpptr->a = ((a+2)>>2);
					break;
				default: break;
			}
			//if (wpptr->a) wpptr->a = 255;
		}
	}

	tex->sizx = newx;
	tex->sizy = newy;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl 0x8(%rdi), %ecx
movl 0xc(%rdi), %eax
sarl %ecx
cmpl $0x2, %ecx
movl $0x1, %edx
cmovll %edx, %ecx
sarl %eax
cmpl $0x2, %eax
cmovll %edx, %eax
movq %rax, -0x30(%rsp)
leal (%rcx,%rcx), %esi
movq %rcx, -0x20(%rsp)
leaq (,%rcx,4), %rax
movq %rax, -0x18(%rsp)
xorl %ecx, %ecx
movq $0x0, -0x38(%rsp)
xorl %edx, %edx
movq %rsi, -0x28(%rsp)
cmpq -0x30(%rsp), %rdx
je 0x5cf4c
movq (%rdi), %rbp
movq %rdx, -0x10(%rsp)
leaq 0x1(,%rdx,2), %r15
movl 0x8(%rdi), %eax
movq %rcx, -0x8(%rsp)
imull %ecx, %eax
cltq
leaq (,%rax,4), %r12
addq %rbp, %r12
addq -0x38(%rsp), %rbp
xorl %ecx, %ecx
cmpl %ecx, %esi
je 0x5cf28
movzbl 0x3(%r12,%rcx,4), %r13d
testl %r13d, %r13d
je 0x5cdb0
movzbl (%r12,%rcx,4), %r9d
movzbl 0x1(%r12,%rcx,4), %eax
movzbl 0x2(%r12,%rcx,4), %r8d
movl $0x1, %edx
jmp 0x5cdbd
xorl %r9d, %r9d
xorl %eax, %eax
xorl %r8d, %r8d
xorl %r13d, %r13d
xorl %edx, %edx
leal 0x1(%rcx), %ebx
movslq 0x8(%rdi), %r14
cmpl %r14d, %ebx
jge 0x5cdf4
movzbl 0x7(%r12,%rcx,4), %r11d
testl %r11d, %r11d
je 0x5cdf4
movzbl 0x4(%r12,%rcx,4), %r10d
addl %r10d, %r9d
movzbl 0x5(%r12,%rcx,4), %r10d
addl %r10d, %eax
movzbl 0x6(%r12,%rcx,4), %r10d
addl %r10d, %r8d
addl %r11d, %r13d
incl %edx
movslq 0xc(%rdi), %r10
cmpq %r10, %r15
jge 0x5ce70
leaq (%r12,%r14,4), %r10
movzbl 0x3(%r10,%rcx,4), %r11d
testl %r11d, %r11d
je 0x5ce2f
movzbl (%r10,%rcx,4), %esi
addl %esi, %r9d
movzbl 0x1(%r10,%rcx,4), %esi
addl %esi, %eax
movzbl 0x2(%r10,%rcx,4), %esi
addl %esi, %r8d
movq -0x28(%rsp), %rsi
addl %r11d, %r13d
incl %edx
cmpl %r14d, %ebx
jge 0x5ce70
leaq (%r12,%r14,4), %r10
addq $0x4, %r10
movzbl 0x3(%r10,%rcx,4), %ebx
testl %ebx, %ebx
je 0x5ce70
incq %r14
movzbl (%r10,%rcx,4), %esi
addl %esi, %r9d
leaq (%r12,%r14,4), %rsi
movzbl 0x1(%rsi,%rcx,4), %r10d
addl %r10d, %eax
movzbl 0x2(%rsi,%rcx,4), %esi
addl %esi, %r8d
movq -0x28(%rsp), %rsi
addl %ebx, %r13d
incl %edx
movl %edx, %edx
leaq 0x140d3(%rip), %r10     # 0x70f4c
movslq (%r10,%rdx,4), %rdx
addq %r10, %rdx
jmpq *%rdx
movb %r9b, (%rbp,%rcx,2)
movb %al, 0x1(%rbp,%rcx,2)
movb %r8b, 0x2(%rbp,%rcx,2)
jmp 0x5cf1a
addl $0x2, %r9d
shrl $0x2, %r9d
movb %r9b, (%rbp,%rcx,2)
addl $0x2, %eax
shrl $0x2, %eax
movb %al, 0x1(%rbp,%rcx,2)
addl $0x2, %r8d
shrl $0x2, %r8d
movb %r8b, 0x2(%rbp,%rcx,2)
addl $0x2, %r13d
shrl $0x2, %r13d
jmp 0x5cf1a
incl %r9d
shrl %r9d
movb %r9b, (%rbp,%rcx,2)
incl %eax
shrl %eax
movb %al, 0x1(%rbp,%rcx,2)
incl %r8d
shrl %r8d
movb %r8b, 0x2(%rbp,%rcx,2)
incl %r13d
shrl %r13d
jmp 0x5cf1a
imull $0x55, %r9d, %edx
movl $0x80, %r9d
addl %r9d, %edx
movb %dh, (%rbp,%rcx,2)
imull $0x55, %eax, %eax
addl %r9d, %eax
movb %ah, 0x1(%rbp,%rcx,2)
imull $0x55, %r8d, %eax
addl %r9d, %eax
movb %ah, 0x2(%rbp,%rcx,2)
imull $0x55, %r13d, %r13d
addl %r9d, %r13d
shrl $0x8, %r13d
movb %r13b, 0x3(%rbp,%rcx,2)
addq $0x2, %rcx
jmp 0x5cd85
movq -0x10(%rsp), %rdx
incq %rdx
movq -0x38(%rsp), %rax
addq -0x18(%rsp), %rax
movq %rax, -0x38(%rsp)
movq -0x8(%rsp), %rcx
addl $0x2, %ecx
jmp 0x5cd4b
movq -0x20(%rsp), %rax
movl %eax, 0x8(%rdi)
movq -0x30(%rsp), %rax
movl %eax, 0xc(%rdi)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq