// File: /workspace/github_c_makefile_star2_O3/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), %edx
movl 0xc(%rdi), %ecx
sarl %edx
cmpl $0x2, %edx
movl $0x1, %eax
cmovll %eax, %edx
sarl %ecx
cmpl $0x2, %ecx
cmovll %eax, %ecx
movq %rcx, -0x38(%rsp)
leal (%rdx,%rdx), %r9d
movq %rdx, -0x28(%rsp)
leaq (,%rdx,4), %rax
movq %rax, -0x20(%rsp)
xorl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
movq %r9, -0x30(%rsp)
movq (%rdi), %rbp
movq %rax, -0x18(%rsp)
leaq 0x1(,%rax,2), %r14
movl 0x8(%rdi), %eax
movq %rcx, -0x8(%rsp)
imull %ecx, %eax
cltq
leaq (,%rax,4), %r15
addq %rbp, %r15
movq %rdx, -0x10(%rsp)
addq %rdx, %rbp
xorl %ecx, %ecx
movzbl 0x3(%r15,%rcx,4), %r13d
testl %r13d, %r13d
je 0x67997
movzbl (%r15,%rcx,4), %eax
movzbl 0x1(%r15,%rcx,4), %esi
movzbl 0x2(%r15,%rcx,4), %r12d
movl $0x1, %r8d
jmp 0x679a4
xorl %eax, %eax
xorl %esi, %esi
xorl %r12d, %r12d
xorl %r13d, %r13d
xorl %r8d, %r8d
leal 0x1(%rcx), %ebx
movslq 0x8(%rdi), %r11
cmpl %r11d, %ebx
jge 0x679da
movzbl 0x7(%r15,%rcx,4), %r10d
testl %r10d, %r10d
je 0x679da
movzbl 0x4(%r15,%rcx,4), %edx
addl %edx, %eax
movzbl 0x5(%r15,%rcx,4), %edx
addl %edx, %esi
movzbl 0x6(%r15,%rcx,4), %edx
addl %edx, %r12d
addl %r10d, %r13d
incl %r8d
movslq 0xc(%rdi), %rdx
cmpq %rdx, %r14
jge 0x67a55
leaq (%r15,%r11,4), %rdx
movzbl 0x3(%rdx,%rcx,4), %r10d
testl %r10d, %r10d
je 0x67a16
movzbl (%rdx,%rcx,4), %r9d
addl %r9d, %eax
movzbl 0x1(%rdx,%rcx,4), %r9d
addl %r9d, %esi
movq -0x30(%rsp), %r9
movzbl 0x2(%rdx,%rcx,4), %edx
addl %edx, %r12d
addl %r10d, %r13d
incl %r8d
cmpl %r11d, %ebx
jge 0x67a55
leaq (%r15,%r11,4), %rdx
addq $0x4, %rdx
movzbl 0x3(%rdx,%rcx,4), %ebx
testl %ebx, %ebx
je 0x67a55
incq %r11
movzbl (%rdx,%rcx,4), %edx
addl %edx, %eax
leaq (%r15,%r11,4), %rdx
movzbl 0x1(%rdx,%rcx,4), %r9d
addl %r9d, %esi
movq -0x30(%rsp), %r9
movzbl 0x2(%rdx,%rcx,4), %edx
addl %edx, %r12d
addl %ebx, %r13d
incl %r8d
movl %r8d, %edx
leaq 0x147ad(%rip), %r8      # 0x7c20c
movslq (%r8,%rdx,4), %rdx
addq %r8, %rdx
jmpq *%rdx
movb %al, (%rbp,%rcx,2)
movb %sil, 0x1(%rbp,%rcx,2)
movb %r12b, 0x2(%rbp,%rcx,2)
jmp 0x67af4
addl $0x2, %eax
shrl $0x2, %eax
movb %al, (%rbp,%rcx,2)
addl $0x2, %esi
shrl $0x2, %esi
movb %sil, 0x1(%rbp,%rcx,2)
addl $0x2, %r12d
shrl $0x2, %r12d
movb %r12b, 0x2(%rbp,%rcx,2)
addl $0x2, %r13d
shrl $0x2, %r13d
jmp 0x67af4
incl %eax
shrl %eax
movb %al, (%rbp,%rcx,2)
incl %esi
shrl %esi
movb %sil, 0x1(%rbp,%rcx,2)
incl %r12d
shrl %r12d
movb %r12b, 0x2(%rbp,%rcx,2)
incl %r13d
shrl %r13d
jmp 0x67af4
imull $0x55, %eax, %eax
movl $0x80, %edx
addl %edx, %eax
movb %ah, (%rbp,%rcx,2)
imull $0x55, %esi, %eax
addl %edx, %eax
movb %ah, 0x1(%rbp,%rcx,2)
imull $0x55, %r12d, %eax
addl %edx, %eax
movb %ah, 0x2(%rbp,%rcx,2)
imull $0x55, %r13d, %r13d
addl %edx, %r13d
shrl $0x8, %r13d
movb %r13b, 0x3(%rbp,%rcx,2)
addq $0x2, %rcx
cmpl %ecx, %r9d
jne 0x67973
movq -0x18(%rsp), %rax
incq %rax
movq -0x10(%rsp), %rdx
addq -0x20(%rsp), %rdx
movq -0x8(%rsp), %rcx
addl $0x2, %ecx
cmpq -0x38(%rsp), %rax
jne 0x67941
movq -0x28(%rsp), %rax
movl %eax, 0x8(%rdi)
movq -0x38(%rsp), %rax
movl %eax, 0xc(%rdi)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq