// File: /workspace/github_c_makefile_star2_O2/aap[P]pdp11/meatball.c
// Function: metaballize
void
metaballize(Image *in, Image *out, int mag)
{
	u32 x, y;
	u8 *p;

//	int offx = 192;
//	int offy = 68;
	int offx = 0;
	int offy = 0;
	int sz = 3;

	for(y = 0; y < out->h; y++)
		for(x = 0; x < out->w; x++) {
			p = &out->pixels[y*out->stride + x*4];

			float fx = offx + (float)x/mag;
			float fy = offy + (float)y/mag;

			float f = 0.0f;
			for(int i = (int)fy - sz; i < (int)fy + sz; i++)
			for(int j = (int)fx - sz; j < (int)fx + sz; j++)
				if(i >= 0 && i < in->h &&
				   j >= 0 && j < in->w &&
				   in->pixels[i*in->stride + j] == 0) {
					float m = meta(fx,fy, 1.00f, j, i);
					m = powf(m, 2.2f);
					f += m;
				}

#if 0
			int c = 255;
			if(f > 1.0f) {
				c = 0;
			}
#else
			int c = smoothstep(1.0f, 0.7f, f)*255;
#endif
			p[0] = c;
			p[1] = c;
			p[2] = c;
			p[3] = 255;
		}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r14
cvtsi2ss %edx, %xmm0
movss %xmm0, 0xc(%rsp)
xorl %ecx, %ecx
movq %rsi, 0x28(%rsp)
cmpl 0x4(%rsi), %ecx
jae 0x11d40
movl %ecx, %eax
cvtsi2ss %rax, %xmm0
divss 0xc(%rsp), %xmm0
addss 0x5775(%rip), %xmm0     # 0x1732c
movss %xmm0, 0x18(%rsp)
cvttss2si %xmm0, %r15d
leal -0x3(%r15), %eax
movl %eax, 0x10(%rsp)
addl $0x3, %r15d
xorl %edx, %edx
movl %ecx, 0x14(%rsp)
cmpl (%rsi), %edx
jae 0x11d39
movq 0x10(%rsi), %rax
movq %rax, 0x38(%rsp)
movl 0x8(%rsi), %eax
imull %ecx, %eax
movl %edx, %ecx
cvtsi2ss %rcx, %xmm0
divss 0xc(%rsp), %xmm0
xorps %xmm1, %xmm1
addss %xmm1, %xmm0
movss %xmm0, 0x1c(%rsp)
cvttss2si %xmm0, %ebx
movq %rdx, 0x40(%rsp)
leal (%rax,%rdx,4), %eax
movq %rax, 0x30(%rsp)
leal -0x3(%rbx), %r12d
addl $0x3, %ebx
movl 0x10(%rsp), %ebp
cmpl %r15d, %ebp
jge 0x11cc0
cvtsi2ss %ebp, %xmm0
movss 0x18(%rsp), %xmm2
subss %xmm0, %xmm2
mulss %xmm2, %xmm2
movss %xmm2, 0x20(%rsp)
movl %r12d, %r13d
cmpl %ebx, %r13d
jge 0x11cb9
testl %ebp, %ebp
js 0x11cb4
testl %r13d, %r13d
js 0x11cb4
cmpl 0x4(%r14), %ebp
jae 0x11cb4
cmpl (%r14), %r13d
jae 0x11cb4
movq 0x10(%r14), %rax
movl 0x8(%r14), %ecx
imull %ebp, %ecx
addl %r13d, %ecx
cmpb $0x0, (%rax,%rcx)
jne 0x11cb4
cvtsi2ss %r13d, %xmm0
movss %xmm1, 0x24(%rsp)
movss 0x1c(%rsp), %xmm1
subss %xmm0, %xmm1
mulss %xmm1, %xmm1
addss 0x20(%rsp), %xmm1
movss 0x567b(%rip), %xmm0     # 0x17314
divss %xmm1, %xmm0
movss 0x5683(%rip), %xmm1     # 0x17328
callq 0x21f0
movss 0x24(%rsp), %xmm1
addss %xmm0, %xmm1
incl %r13d
jmp 0x11c45
incl %ebp
jmp 0x11c21
addss 0x5654(%rip), %xmm1     # 0x1731c
divss 0x5650(%rip), %xmm1     # 0x17320
xorps %xmm0, %xmm0
maxss %xmm1, %xmm0
movss 0x5635(%rip), %xmm1     # 0x17314
minss %xmm0, %xmm1
movaps %xmm1, %xmm0
mulss %xmm1, %xmm0
addss %xmm1, %xmm1
movss 0x5622(%rip), %xmm2     # 0x17318
subss %xmm1, %xmm2
mulss %xmm0, %xmm2
mulss 0x561e(%rip), %xmm2     # 0x17324
cvttss2si %xmm2, %eax
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %rdx
movb %al, (%rcx,%rdx)
movb %al, 0x1(%rcx,%rdx)
movb %al, 0x2(%rcx,%rdx)
movb $-0x1, 0x3(%rcx,%rdx)
movq 0x40(%rsp), %rdx
incl %edx
movq 0x28(%rsp), %rsi
movl 0x14(%rsp), %ecx
jmp 0x11bd4
incl %ecx
jmp 0x11b99
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq