// File: /workspace/github_c_makefile_star2_O0/yui0[P]img2vec/img2vec.c
// Function: color_quant
void color_quant(unsigned char *im, int w, int h, int n_colors, char *name, int flag)
{
	int i;
	unsigned char *pix = im;
	node_heap heap = { 0, 0, 0 };

	oct_node root = node_new(0, 0, 0);
	for (i=0; i < w * h; i++, pix += 3) {
		heap_add(&heap, node_insert(root, pix));
	}

	while (heap.n > n_colors + 1) {
		heap_add(&heap, node_fold(pop_heap(&heap)));
	}

	for (i=1; i < heap.n; i++) {
		oct_node got = heap.buf[i];
		double c = got->count;
		got->r = got->r / c + .5;
		got->g = got->g / c + .5;
		got->b = got->b / c + .5;
		printf("%2d | %3lu %3lu %3lu (%d pixels)\n",
		       i, got->r, got->g, got->b, got->count);
	}

	for (i=0, pix = im; i < w * h; i++, pix += 3) {
		color_replace(root, pix);
	}

	// output
	if (flag&1) stbi_write_jpg("posterized.jpg", w, h, 3, im, 0);
	FILE *fp = fopen(name, "w");
	if (!(flag&32)) fprintf(fp, "%%!PS-Adobe-3.0 EPSF-3.0\n");
	if (!(flag&32)) fprintf(fp, "%%%%BoundingBox: 0 0 %d %d\n", w, h);
	if (flag&32) fprintf(fp, "<svg id=\"illust\" xmlns=\"http://www.w3.org/2000/svg\" width=\"%dpx\" height=\"%dpx\" viewBox=\"0 0 %d %d\">\n", w, h, w, h);
	if (flag&32) fprintf(fp, "<!-- Generator: img2vec by Yuichiro Nakada -->");
	uint8_t *img = malloc(w * h *3*2);
	for (i=1; i < heap.n; i++) {
		memset(img, 0, w * h *3);
		oct_node got = heap.buf[i];
		int n;
		for (n=0, pix = im; n < w * h; n++, pix += 3) {
			if (got->r==pix[0] && got->g==pix[1] && got->b==pix[2]) {
				img[n*3] = got->r;
				img[n*3+1] = got->g;
				img[n*3+2] = got->b;
				/*img[n*3] = 255;
				img[n*3+1] = 255;
				img[n*3+2] = 255;*/
			}
		}
		if (flag&1) { // debug
			char str[256];
			snprintf(str, sizeof(str), "original_d%02d.bmp", i);
			stbi_write_bmp(str, w, h, 3, img);
		}
		if (flag&4) { // alpha
			if (got->r==255 && got->g==255 && got->b==255) continue;
		}
		if (flag&2) { // dilate
			imgp_dilate(img, w, h, 3, img+w * h *3);
			img2vec(fp, img+w * h *3, w, h, got->r, got->g, got->b, flag);
		} else {
			img2vec(fp, img, w, h, got->r, got->g, got->b, flag);
		}
	}
	free(img);
	if (!(flag&32)) fprintf(fp, "%%EOF\n");
	if (flag&32) fprintf(fp, "</svg>\n");
	fclose(fp);

	node_free();
	free(heap.buf);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movq %r8, -0x20(%rbp)
movl %r9d, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10f0
xorl %esi, %esi
xorl %eax, %eax
movl %eax, %edx
movl %esi, %edi
callq 0x8020
movq %rax, -0x48(%rbp)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl -0xc(%rbp), %ecx
imull -0x10(%rbp), %ecx
cmpl %ecx, %eax
jge 0xdd9e
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x8120
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0x7e70
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x30(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x30(%rbp)
jmp 0xdd60
jmp 0xdda0
movl -0x3c(%rbp), %eax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jle 0xddcc
leaq -0x40(%rbp), %rdi
callq 0x7f70
movq %rax, %rdi
callq 0x8260
movq %rax, %rsi
leaq -0x40(%rbp), %rdi
callq 0x7e70
jmp 0xdda0
movl $0x1, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0xdf3d
movq -0x38(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
cvtsi2sdl 0x18(%rax), %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x50(%rbp), %rax
movq (%rax), %xmm0
movaps 0x242b4(%rip), %xmm4    # 0x320c0
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
movapd 0x242b8(%rip), %xmm3    # 0x320d0
subpd %xmm3, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd -0x58(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd 0x241f0(%rip), %xmm2    # 0x32028
addsd %xmm2, %xmm0
movaps %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
movq %rcx, %rsi
sarq $0x3f, %rsi
movsd 0x24215(%rip), %xmm1    # 0x32068
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rdx
andq %rsi, %rdx
orq %rdx, %rcx
movq %rcx, (%rax)
movq -0x50(%rbp), %rax
movq 0x8(%rax), %xmm0
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
subpd %xmm3, %xmm0
movaps %xmm0, %xmm5
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm5, %xmm0
movsd -0x58(%rbp), %xmm5
divsd %xmm5, %xmm0
addsd %xmm2, %xmm0
movaps %xmm0, %xmm5
cvttsd2si %xmm5, %rcx
movq %rcx, %rsi
sarq $0x3f, %rsi
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rdx
andq %rsi, %rdx
orq %rdx, %rcx
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rax
movq 0x10(%rax), %xmm0
punpckldq %xmm4, %xmm0    # xmm0 = xmm0[0],xmm4[0],xmm0[1],xmm4[1]
subpd %xmm3, %xmm0
movaps %xmm0, %xmm3
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm3, %xmm0
movsd -0x58(%rbp), %xmm3
divsd %xmm3, %xmm0
addsd %xmm2, %xmm0
movaps %xmm0, %xmm2
cvttsd2si %xmm2, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
andq %rdx, %rax
orq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x28(%rbp), %esi
movq -0x50(%rbp), %rax
movq (%rax), %rdx
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x50(%rbp), %rax
movq 0x10(%rax), %r8
movq -0x50(%rbp), %rax
movl 0x18(%rax), %r9d
leaq 0x24412(%rip), %rdi     # 0x3233a
movb $0x0, %al
callq 0x10c0
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xddd3
movl $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x28(%rbp), %eax
movl -0xc(%rbp), %ecx
imull -0x10(%rbp), %ecx
cmpl %ecx, %eax
jge 0xdf7e
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x8300
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movq -0x30(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x30(%rbp)
jmp 0xdf4c
movl -0x24(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xdfa7
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movq -0x8(%rbp), %r8
leaq 0x243c2(%rip), %rdi     # 0x3235c
movl $0x3, %ecx
xorl %r9d, %r9d
callq 0x6620
movq -0x20(%rbp), %rdi
leaq 0x243b9(%rip), %rsi     # 0x3236b
callq 0x11d0
movq %rax, -0x60(%rbp)
movl -0x24(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
jne 0xdfd8
movq -0x60(%rbp), %rdi
leaq 0x2439c(%rip), %rsi     # 0x3236d
movb $0x0, %al
callq 0x1140
movl -0x24(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
jne 0xdffb
movq -0x60(%rbp), %rdi
movl -0xc(%rbp), %edx
movl -0x10(%rbp), %ecx
leaq 0x24393(%rip), %rsi     # 0x32387
movb $0x0, %al
callq 0x1140
movl -0x24(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0xe026
movq -0x60(%rbp), %rdi
movl -0xc(%rbp), %edx
movl -0x10(%rbp), %ecx
movl -0xc(%rbp), %r8d
movl -0x10(%rbp), %r9d
leaq 0x24384(%rip), %rsi     # 0x323a3
movb $0x0, %al
callq 0x1140
movl -0x24(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0xe043
movq -0x60(%rbp), %rdi
leaq 0x243cc(%rip), %rsi     # 0x32408
movb $0x0, %al
callq 0x1140
movl -0xc(%rbp), %eax
imull -0x10(%rbp), %eax
imull $0x3, %eax, %eax
shll %eax
movslq %eax, %rdi
callq 0x1190
movq %rax, -0x68(%rbp)
movl $0x1, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0xe2aa
movq -0x68(%rbp), %rdi
movl -0xc(%rbp), %eax
imull -0x10(%rbp), %eax
imull $0x3, %eax, %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x10f0
movq -0x38(%rbp), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x74(%rbp), %eax
movl -0xc(%rbp), %ecx
imull -0x10(%rbp), %ecx
cmpl %ecx, %eax
jge 0xe15d
movq -0x70(%rbp), %rax
movq (%rax), %rax
movq -0x30(%rbp), %rcx
movzbl (%rcx), %ecx
cmpq %rcx, %rax
jne 0xe141
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
cmpq %rcx, %rax
jne 0xe141
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movzbl 0x2(%rcx), %ecx
cmpq %rcx, %rax
jne 0xe141
movq -0x70(%rbp), %rax
movq (%rax), %rax
movb %al, %dl
movq -0x68(%rbp), %rax
imull $0x3, -0x74(%rbp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movb %al, %dl
movq -0x68(%rbp), %rax
imull $0x3, -0x74(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
movb %al, %dl
movq -0x68(%rbp), %rax
imull $0x3, -0x74(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0xe143
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
movq -0x30(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x30(%rbp)
jmp 0xe0a5
movl -0x24(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xe1a0
leaq -0x180(%rbp), %rdi
movl -0x28(%rbp), %ecx
movl $0x100, %esi            # imm = 0x100
leaq 0x242b9(%rip), %rdx     # 0x32437
movb $0x0, %al
callq 0x10d0
leaq -0x180(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movq -0x68(%rbp), %r8
movl $0x3, %ecx
callq 0x3040
movl -0x24(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0xe1db
movq -0x70(%rbp), %rax
cmpq $0xff, (%rax)
jne 0xe1d9
movq -0x70(%rbp), %rax
cmpq $0xff, 0x8(%rax)
jne 0xe1d9
movq -0x70(%rbp), %rax
cmpq $0xff, 0x10(%rax)
jne 0xe1d9
jmp 0xe29c
jmp 0xe1db
movl -0x24(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0xe25c
movq -0x68(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
movq -0x68(%rbp), %r8
movl -0xc(%rbp), %eax
imull -0x10(%rbp), %eax
imull $0x3, %eax, %eax
cltq
addq %rax, %r8
movl $0x3, %ecx
callq 0x7360
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
movl -0xc(%rbp), %eax
imull -0x10(%rbp), %eax
imull $0x3, %eax, %eax
cltq
addq %rax, %rsi
movl -0xc(%rbp), %edx
movl -0x10(%rbp), %ecx
movq -0x70(%rbp), %rax
movq (%rax), %rax
movl %eax, %r8d
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %r9d
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %r10d
movl -0x24(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0xd420
jmp 0xe29a
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
movl -0xc(%rbp), %edx
movl -0x10(%rbp), %ecx
movq -0x70(%rbp), %rax
movq (%rax), %rax
movl %eax, %r8d
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %r9d
movq -0x70(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %r10d
movl -0x24(%rbp), %eax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0xd420
jmp 0xe29c
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xe062
movq -0x68(%rbp), %rdi
callq 0x1030
movl -0x24(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
jne 0xe2d0
movq -0x60(%rbp), %rdi
leaq 0x24181(%rip), %rsi     # 0x3244a
movb $0x0, %al
callq 0x1140
movl -0x24(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0xe2ed
movq -0x60(%rbp), %rdi
leaq 0x2416b(%rip), %rsi     # 0x32451
movb $0x0, %al
callq 0x1140
movq -0x60(%rbp), %rdi
callq 0x10b0
callq 0x80e0
movq -0x38(%rbp), %rdi
callq 0x1030
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
nopl (%rax)