// File: /workspace/github_c_makefile_star2_O2/yui0[P]img2vec/img2vec.c
// Function: img2vec
int img2vec(FILE *fp, uint8_t *s, int w, int h, int r, int g, int b, int flag)
{
	// create a bitmap
	potrace_bitmap_t *bm = bm_new(w, h);
	if (!bm) {
		fprintf(stderr, "Error allocating bitmap: %s\n", strerror(errno));
		return 1;
	}

	for (int y=0; y<h; y++) {
		for (int x=0; x<w; x++) {
			int n = ((h-y)*w+x)*3;
			int c = (s[n]||s[n+1]||s[n+2] ? 255 : 0);
			BM_PUT(bm, x, y, c);
		}
	}

	// set tracing parameters, starting from defaults
	potrace_param_t *param = potrace_param_default();
	if (!param) {
		fprintf(stderr, "Error allocating parameters: %s\n", strerror(errno));
		return 1;
	}
	param->turdsize = 0;

	// trace the bitmap
	potrace_state_t *st = potrace_trace(param, bm);
	if (!st || st->status != POTRACE_STATUS_OK) {
		fprintf(stderr, "Error tracing bitmap: %s\n", strerror(errno));
		return 1;
	}
	bm_free(bm);

	// output vector data, e.g. as a rudimentary EPS file
	//printf("%%!PS-Adobe-3.0 EPSF-3.0\n");
	//printf("%%%%BoundingBox: 0 0 %d %d\n", w, h);
	if (!(flag&32)) fprintf(fp, "gsave\n");
	if (flag&32) fprintf(fp, "<g id=\"#%02x%02x%02x\">\n", r, g, b);
	potrace_path_t *p = st->plist;
	potrace_dpoint_t (*c)[3];
	while (p != NULL) {
		int n = p->curve.n;
		int *tag = p->curve.tag;
		c = p->curve.c;
		if (!(flag&32)) fprintf(fp, "%f %f moveto\n", c[n-1][2].x, c[n-1][2].y);
		//if (flag&32) fprintf(fp, "<path d=\"M%f,%f", c[n-1][2].x, h-c[n-1][2].y);
		if (flag&32) fprintf(fp, "<path d=\"M" ACCURACY "," ACCURACY "", c[n-1][2].x, h-c[n-1][2].y);
		for (int i=0; i<n; i++) {
			switch (tag[i]) {
			case POTRACE_CORNER:
				if (!(flag&32)) fprintf(fp, "%f %f lineto\n", c[i][1].x, c[i][1].y);
				if (!(flag&32)) fprintf(fp, "%f %f lineto\n", c[i][2].x, c[i][2].y);
				//if (flag&32) fprintf(fp, " L%f,%f L%f,%f", c[i][1].x, h-c[i][1].y, c[i][2].x, h-c[i][2].y);
				if (flag&32) fprintf(fp, " L" ACCURACY "," ACCURACY " L" ACCURACY "," ACCURACY "", c[i][1].x, h-c[i][1].y, c[i][2].x, h-c[i][2].y);
				break;
			case POTRACE_CURVETO:
				if (!(flag&32)) fprintf(fp, "%f %f %f %f %f %f curveto\n",
				       c[i][0].x, c[i][0].y,
				       c[i][1].x, c[i][1].y,
				       c[i][2].x, c[i][2].y);
				//if (flag&32) fprintf(fp, " C%f,%f %f,%f %f,%f",
				if (flag&32) fprintf(fp, " C" ACCURACY "," ACCURACY " " ACCURACY "," ACCURACY " " ACCURACY "," ACCURACY "",
					c[i][0].x, h-c[i][0].y, c[i][1].x, h-c[i][1].y, c[i][2].x, h-c[i][2].y);
				break;
			}
		}
		/* at the end of a group of a positive path and its negative
		   children, fill. */
		if (p->next == NULL || p->next->sign == '+') {
			//printf("0 setgray fill\n");
			if (!(flag&32)) fprintf(fp, "%f %f %f setrgbcolor fill\n", r/255.0, g/255.0, b/255.0);
//			if (flag&32) fprintf(fp, "\" stroke=\"#%02x%02x%02x\"/>", r, g, b);
//		} else {
//			if (flag&32) fprintf(fp, "\" fill=\"#%02x%02x%02x\"/>", r, g, b);
		}
		p = p->next;
		if (flag&32) fprintf(fp, "\" fill=\"#%02x%02x%02x\" stroke=\"#%02x%02x%02x", r, g, b, r, g, b);
		if (flag&32) fprintf(fp, "\"/>\n");
//		if (flag&32) fprintf(fp, "\" stroke=\"#%02x%02x%02x\"/>", r, g, b);
//		if (flag&32) fprintf(fp, "\" fill=\"#%02x%02x%02x\"/>", r, g, b);
	}
	if (!(flag&32)) fprintf(fp, "grestore\n");
	if (flag&32) fprintf(fp, "</g>\n");
	//printf("%%EOF\n");

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x10(%rsp)
movl %r8d, 0x4(%rsp)
movl %ecx, %r14d
movl %edx, %r13d
movq %rsi, %r12
movq %rdi, %rbx
movl %edx, %edi
movl %ecx, %esi
callq 0xb876
testq %rax, %rax
je 0xb81b
movq %rax, %r15
xorl %eax, %eax
testl %r13d, %r13d
movl $0x0, %edx
cmovgl %r13d, %edx
movslq %r14d, %rcx
movslq %r13d, %r8
testl %r14d, %r14d
movl $0x0, %esi
cmovgl %r14d, %esi
imulq %r8, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%r12,%rcx), %rdi
addq $0x2, %rdi
imulq $-0x3, %r8, %r8
movabsq $-0x8000000000000000, %r9 # imm = 0x8000000000000000
cmpq %rsi, %rax
je 0xb4c2
movq %rdi, %r10
xorl %ecx, %ecx
cmpq %rcx, %rdx
je 0xb4b7
movb $0x1, %bpl
cmpb $0x0, -0x2(%r10)
jne 0xb457
cmpb $0x0, -0x1(%r10)
jne 0xb457
cmpb $0x0, (%r10)
setne %bpl
movslq (%r15), %r11
cmpq %r11, %rcx
jge 0xb4ae
movslq 0x4(%r15), %r11
cmpq %r11, %rax
jge 0xb4ae
movq %r9, %r11
shrq %cl, %r11
testb %bpl, %bpl
je 0xb490
movslq 0x8(%r15), %r12
imulq %rax, %r12
shlq $0x3, %r12
addq 0x10(%r15), %r12
movl %ecx, %r13d
shrl $0x6, %r13d
orq %r11, (%r12,%r13,8)
jmp 0xb4ae
notq %r11
movslq 0x8(%r15), %r12
imulq %rax, %r12
shlq $0x3, %r12
addq 0x10(%r15), %r12
movl %ecx, %r13d
shrl $0x6, %r13d
andq %r11, (%r12,%r13,8)
incq %rcx
addq $0x3, %r10
jmp 0xb439
incq %rax
addq %r8, %rdi
jmp 0xb42b
callq 0xb13e
testq %rax, %rax
je 0xb83a
andl $0x0, (%rax)
movq %rax, %rdi
movq %r15, %rsi
callq 0xb15e
testq %rax, %rax
je 0xb7fc
movq %rax, %r12
cmpl $0x0, (%rax)
jne 0xb7fc
movl %r14d, 0x8(%rsp)
movq %r15, %rdi
callq 0xb115
movl 0x78(%rsp), %eax
testb $0x20, %al
je 0xb531
leaq 0x10797(%rip), %rsi     # 0x1bca6
movq %rbx, %rdi
movl 0x4(%rsp), %ebp
movl %ebp, %edx
movq 0x10(%rsp), %r13
movl %r13d, %ecx
movl 0x70(%rsp), %r14d
movl %r14d, %r8d
xorl %eax, %eax
callq 0x1130
jmp 0xb554
leaq 0x10767(%rip), %rdi     # 0x1bc9f
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x1210
movq 0x10(%rsp), %r13
movl 0x4(%rsp), %ebp
movl 0x70(%rsp), %r14d
movq 0x8(%r12), %r15
cvtsi2sdl 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
cvtsi2sd %ebp, %xmm1
movsd 0x1050f(%rip), %xmm0    # 0x1ba80
divsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
cvtsi2sd %r13d, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
cvtsi2sd %r14d, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
testq %r15, %r15
je 0xb7d0
movslq 0x8(%r15), %r12
imulq $0x30, %r12, %rax
movq 0x10(%r15), %r13
movq 0x18(%r15), %r14
movsd -0x10(%r14,%rax), %xmm0
movsd -0x8(%r14,%rax), %xmm1
movl 0x78(%rsp), %eax
testb $0x20, %al
movq %r15, 0x30(%rsp)
je 0xb5e7
movsd 0x8(%rsp), %xmm2
subsd %xmm1, %xmm2
movq %rbx, %rdi
leaq 0x106eb(%rip), %rsi     # 0x1bccc
movapd %xmm2, %xmm1
jmp 0xb5f1
movq %rbx, %rdi
leaq 0x106cd(%rip), %rsi     # 0x1bcbe
movb $0x2, %al
callq 0x1130
testl %r12d, %r12d
movl $0x0, %eax
cmovlel %eax, %r12d
addq $0x28, %r14
xorl %ebp, %ebp
cmpq %rbp, %r12
je 0xb724
movl (%r13,%rbp,4), %eax
cmpl $0x1, %eax
je 0xb669
cmpl $0x2, %eax
jne 0xb718
movl 0x78(%rsp), %eax
testb $0x20, %al
je 0xb6b6
movsd 0x8(%rsp), %xmm3
movapd %xmm3, %xmm1
subsd -0x10(%r14), %xmm1
movsd -0x18(%r14), %xmm0
movsd -0x8(%r14), %xmm2
subsd (%r14), %xmm3
movq %rbx, %rdi
leaq 0x10691(%rip), %rsi     # 0x1bcee
movb $0x4, %al
callq 0x1130
jmp 0xb718
movsd -0x28(%r14), %xmm0
movsd -0x20(%r14), %xmm1
movl 0x78(%rsp), %eax
testb $0x20, %al
je 0xb6f0
movsd 0x8(%rsp), %xmm5
movapd %xmm5, %xmm6
subsd %xmm1, %xmm6
movapd %xmm5, %xmm3
subsd -0x10(%r14), %xmm3
movsd -0x18(%r14), %xmm2
movsd -0x8(%r14), %xmm4
subsd (%r14), %xmm5
movq %rbx, %rdi
leaq 0x10670(%rip), %rsi     # 0x1bd20
movapd %xmm6, %xmm1
jmp 0xb711
movsd -0x18(%r14), %xmm0
movsd -0x10(%r14), %xmm1
movq %rbx, %rdi
leaq 0x10614(%rip), %r15     # 0x1bce0
movq %r15, %rsi
movb $0x2, %al
callq 0x1130
movsd -0x8(%r14), %xmm0
movsd (%r14), %xmm1
movq %rbx, %rdi
movq %r15, %rsi
movb $0x2, %al
callq 0x1130
jmp 0xb718
movsd -0x18(%r14), %xmm2
movsd -0x10(%r14), %xmm3
movsd -0x8(%r14), %xmm4
movsd (%r14), %xmm5
movq %rbx, %rdi
leaq 0x105f4(%rip), %rsi     # 0x1bd05
movb $0x6, %al
callq 0x1130
incq %rbp
addq $0x30, %r14
jmp 0xb60a
movq 0x30(%rsp), %r15
movq 0x20(%r15), %r14
movl 0x78(%rsp), %eax
testq %r14, %r14
je 0xb75c
testb $0x20, %al
movq 0x10(%rsp), %rcx
movl 0x4(%rsp), %edx
movl 0x70(%rsp), %r8d
jne 0xb74f
cmpl $0x2b, 0x4(%r14)
je 0xb7a4
movq %r14, %r15
testb $0x20, %al
je 0xb599
jmp 0xb76e
testb $0x20, %al
movq 0x10(%rsp), %rcx
movl 0x4(%rsp), %edx
movl 0x70(%rsp), %r8d
je 0xb7a4
movq %rbx, %rdi
leaq 0x105e3(%rip), %rsi     # 0x1bd5b
movl %edx, %r9d
xorl %eax, %eax
pushq %r8
pushq %rcx
callq 0x1130
popq %rcx
popq %rdx
leaq 0x105fa(%rip), %rdi     # 0x1bd88
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x1210
movq %r14, %r15
jmp 0xb599
movq %rbx, %rdi
leaq 0x10592(%rip), %rsi     # 0x1bd40
movsd 0x28(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
movb $0x3, %al
callq 0x1130
movq 0x20(%r15), %r15
jmp 0xb599
movl 0x78(%rsp), %eax
testb $0x20, %al
je 0xb7e3
leaq 0x105b8(%rip), %rdi     # 0x1bd97
pushq $0x5
jmp 0xb7ec
leaq 0x105a3(%rip), %rdi     # 0x1bd8d
pushq $0x9
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x1210
xorl %eax, %eax
jmp 0xb867
movq 0x177f5(%rip), %rax     # 0x22ff8
movq (%rax), %rbx
callq 0x1050
movl (%rax), %edi
callq 0x1230
leaq 0x1046c(%rip), %rsi     # 0x1bc85
jmp 0xb857
movq 0x177d6(%rip), %rax     # 0x22ff8
movq (%rax), %rbx
callq 0x1050
movl (%rax), %edi
callq 0x1230
leaq 0x1040f(%rip), %rsi     # 0x1bc47
jmp 0xb857
movq 0x177b7(%rip), %rax     # 0x22ff8
movq (%rax), %rbx
callq 0x1050
movl (%rax), %edi
callq 0x1230
leaq 0x1040d(%rip), %rsi     # 0x1bc64
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1130
pushq $0x1
popq %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq