// File: /workspace/github_c_makefile_star2_O1/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), %eax
movl 0xc(%rdi), %ecx
sarl %eax
cmpl $0x2, %eax
movl $0x1, %edx
cmovll %edx, %eax
sarl %ecx
cmpl $0x2, %ecx
movl %ecx, -0x3c(%rsp)
cmovgel %ecx, %edx
movq %rdx, -0x20(%rsp)
leal (%rax,%rax), %r10d
movq %rax, -0x30(%rsp)
leaq (,%rax,4), %rax
movq %rax, -0x28(%rsp)
xorl %ecx, %ecx
xorl %edx, %edx
xorl %eax, %eax
movq %r10, -0x38(%rsp)
movq (%rdi), %rbp
movq %rax, -0x18(%rsp)
leaq 0x1(,%rax,2), %r15
movl 0x8(%rdi), %eax
movq %rcx, -0x8(%rsp)
imull %ecx, %eax
cltq
leaq (,%rax,4), %r12
addq %rbp, %r12
movq %rdx, -0x10(%rsp)
addq %rdx, %rbp
xorl %ecx, %ecx
movzbl 0x3(%r12,%rcx,4), %r13d
testl %r13d, %r13d
je 0x65c81
movzbl (%r12,%rcx,4), %edx
movzbl 0x1(%r12,%rcx,4), %r8d
movzbl 0x2(%r12,%rcx,4), %eax
movl $0x1, %r9d
jmp 0x65c8e
xorl %edx, %edx
xorl %r8d, %r8d
xorl %eax, %eax
xorl %r13d, %r13d
xorl %r9d, %r9d
leal 0x1(%rcx), %r14d
movslq 0x8(%rdi), %rbx
cmpl %ebx, %r14d
jge 0x65cc5
movzbl 0x7(%r12,%rcx,4), %r11d
testl %r11d, %r11d
je 0x65cc5
movzbl 0x4(%r12,%rcx,4), %esi
addl %esi, %edx
movzbl 0x5(%r12,%rcx,4), %esi
addl %esi, %r8d
movzbl 0x6(%r12,%rcx,4), %esi
addl %esi, %eax
addl %r11d, %r13d
incl %r9d
movslq 0xc(%rdi), %rsi
cmpq %rsi, %r15
jge 0x65d40
leaq (%r12,%rbx,4), %rsi
movzbl 0x3(%rsi,%rcx,4), %r11d
testl %r11d, %r11d
je 0x65d00
movzbl (%rsi,%rcx,4), %r10d
addl %r10d, %edx
movzbl 0x1(%rsi,%rcx,4), %r10d
addl %r10d, %r8d
movq -0x38(%rsp), %r10
movzbl 0x2(%rsi,%rcx,4), %esi
addl %esi, %eax
addl %r11d, %r13d
incl %r9d
cmpl %ebx, %r14d
jge 0x65d40
leaq (%r12,%rbx,4), %rsi
addq $0x4, %rsi
movzbl 0x3(%rsi,%rcx,4), %r14d
testl %r14d, %r14d
je 0x65d40
incq %rbx
movzbl (%rsi,%rcx,4), %esi
addl %esi, %edx
leaq (%r12,%rbx,4), %rsi
movzbl 0x1(%rsi,%rcx,4), %r10d
addl %r10d, %r8d
movq -0x38(%rsp), %r10
movzbl 0x2(%rsi,%rcx,4), %esi
addl %esi, %eax
addl %r14d, %r13d
incl %r9d
movl %r9d, %esi
leaq 0x15002(%rip), %r9      # 0x7ad4c
movslq (%r9,%rsi,4), %rsi
addq %r9, %rsi
jmpq *%rsi
movb %dl, (%rbp,%rcx,2)
movb %r8b, 0x1(%rbp,%rcx,2)
movb %al, 0x2(%rbp,%rcx,2)
jmp 0x65ddc
addl $0x2, %edx
shrl $0x2, %edx
movb %dl, (%rbp,%rcx,2)
addl $0x2, %r8d
shrl $0x2, %r8d
movb %r8b, 0x1(%rbp,%rcx,2)
addl $0x2, %eax
shrl $0x2, %eax
movb %al, 0x2(%rbp,%rcx,2)
addl $0x2, %r13d
shrl $0x2, %r13d
jmp 0x65ddc
incl %edx
shrl %edx
movb %dl, (%rbp,%rcx,2)
incl %r8d
shrl %r8d
movb %r8b, 0x1(%rbp,%rcx,2)
incl %eax
shrl %eax
movb %al, 0x2(%rbp,%rcx,2)
incl %r13d
shrl %r13d
jmp 0x65ddc
imull $0x55, %edx, %edx
movl $0x80, %esi
addl %esi, %edx
movb %dh, (%rbp,%rcx,2)
imull $0x55, %r8d, %edx
addl %esi, %edx
movb %dh, 0x1(%rbp,%rcx,2)
imull $0x55, %eax, %eax
addl %esi, %eax
movb %ah, 0x2(%rbp,%rcx,2)
imull $0x55, %r13d, %r13d
addl %esi, %r13d
shrl $0x8, %r13d
movb %r13b, 0x3(%rbp,%rcx,2)
addq $0x2, %rcx
cmpl %ecx, %r10d
jne 0x65c5d
movq -0x18(%rsp), %rax
incq %rax
movq -0x10(%rsp), %rdx
addq -0x28(%rsp), %rdx
movq -0x8(%rsp), %rcx
addl $0x2, %ecx
cmpq -0x20(%rsp), %rax
jne 0x65c2b
movl -0x3c(%rsp), %ecx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
movq -0x30(%rsp), %rcx
movl %ecx, 0x8(%rdi)
movl %eax, 0xc(%rdi)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq