// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movq %r9, 0x30(%rsp)
movl %r8d, 0x4(%rsp)
movq %rdi, %rbx
testl %edx, %edx
movq %rdx, 0x10(%rsp)
je 0xc472
leal -0x1(%rdx), %eax
leal 0x3e(%rdx), %r12d
testl %eax, %eax
cmovnsl %eax, %r12d
sarl $0x6, %r12d
incl %r12d
jmp 0xc475
xorl %r12d, %r12d
movl %r12d, %edi
negl %edi
cmovsl %r12d, %edi
movslq %ecx, %rbp
movq %rdi, %r15
imulq %rbp, %r15
testq %r15, %r15
js 0xc99c
leaq (,%r15,8), %r13
testl %ecx, %ecx
sete %al
testl %r12d, %r12d
sete %dl
orb %al, %dl
jne 0xc4bf
movq %r13, %rax
cqto
idivq %rbp
cqto
idivq %rdi
cmpq $0x8, %rax
jne 0xc99c
movq %rcx, 0x18(%rsp)
movq %rsi, 0x40(%rsp)
movl $0x18, %edi
callq 0x1180
testq %rax, %rax
je 0xc9b3
movq %rax, %r14
testq %r15, %r15
movl $0x8, %eax
movq %r13, %rsi
cmoveq %rax, %rsi
movq 0x10(%rsp), %r15
movl %r15d, (%r14)
movq 0x18(%rsp), %r13
movl %r13d, 0x4(%r14)
movl %r12d, 0x8(%r14)
movl $0x1, %edi
callq 0x1110
movq %r14, 0x20(%rsp)
movq %rax, 0x10(%r14)
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0xc9a9
movl %r12d, 0x3c(%rsp)
movslq %r12d, %rax
movq %rax, 0x28(%rsp)
testl %r13d, %r13d
jle 0xc5fb
movslq %r15d, %rax
movl 0x18(%rsp), %edx
movl %r15d, %esi
movq %rbp, %rcx
imulq %rax, %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x40(%rsp), %rdi
leaq (%rdi,%rcx), %r12
addq $0x2, %r12
leaq (%rax,%rax,2), %r8
negq %r8
xorl %r9d, %r9d
movabsq $-0x8000000000000000, %r13 # imm = 0x8000000000000000
testl %r15d, %r15d
jle 0xc5e7
movq %r9, %rcx
imulq 0x28(%rsp), %rcx
movq 0x8(%rsp), %rdi
leaq (%rdi,%rcx,8), %r11
movq %r12, %rdi
xorl %ecx, %ecx
movb $0x1, %r15b
cmpb $0x0, -0x2(%rdi)
jne 0xc5a2
cmpb $0x0, -0x1(%rdi)
jne 0xc5a2
cmpb $0x0, (%rdi)
setne %r15b
cmpq %rbp, %r9
setge %r10b
cmpq %rax, %rcx
setge %r14b
orb %r10b, %r14b
jne 0xc5db
movq %r13, %r10
shrq %cl, %r10
testb %r15b, %r15b
je 0xc5cd
movl %ecx, %r14d
shrl $0x6, %r14d
orq %r10, (%r11,%r14,8)
jmp 0xc5db
notq %r10
movl %ecx, %r14d
shrl $0x6, %r14d
andq %r10, (%r11,%r14,8)
incq %rcx
addq $0x3, %rdi
cmpq %rcx, %rsi
jne 0xc58c
incq %r9
addq %r8, %r12
cmpq %rdx, %r9
movq 0x10(%rsp), %r15
jne 0xc570
movl $0x48, %edi
callq 0x1180
testq %rax, %rax
je 0xc9cc
leaq 0x12083(%rip), %rsi     # 0x1e698
movl $0x9, %ecx
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
movl $0x0, (%rax)
movq %rax, %rdi
movq 0x20(%rsp), %r13
movq %r13, %rsi
callq 0xc120
testq %rax, %rax
je 0xc995
movq %rax, %r14
cmpl $0x0, (%rax)
jne 0xc995
cmpl $0x0, 0x3c(%rsp)
setns %al
movq 0x18(%rsp), %r12
testl %r12d, %r12d
sete %cl
orb %al, %cl
jne 0xc67d
leal -0x1(%r12), %eax
cltq
imulq 0x28(%rsp), %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax,8), %rcx
movq %rcx, 0x8(%rsp)
movl 0x80(%rsp), %r15d
movq 0x8(%rsp), %rdi
callq 0x1030
movq %r13, %rdi
callq 0x1030
movl 0x88(%rsp), %eax
testb $0x20, %al
je 0xc6c1
leaq 0x14705(%rip), %rsi     # 0x20dae
movq %rbx, %rdi
movl 0x4(%rsp), %edx
movq 0x30(%rsp), %rcx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x1130
jmp 0xc6da
leaq 0x146df(%rip), %rdi     # 0x20da7
movl $0x6, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1210
movq 0x8(%r14), %rcx
testq %rcx, %rcx
je 0xc95f
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x10(%rsp)
cvtsi2sdl 0x4(%rsp), %xmm1
movsd 0x14490(%rip), %xmm0    # 0x20b90
divsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
xorps %xmm1, %xmm1
cvtsi2sdl 0x30(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movl 0x80(%rsp), %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
movslq 0x8(%rcx), %r12
leaq (%r12,%r12,2), %rax
shlq $0x4, %rax
movq 0x10(%rcx), %r15
movq 0x18(%rcx), %r14
movsd -0x10(%r14,%rax), %xmm0
movsd -0x8(%r14,%rax), %xmm1
movl 0x88(%rsp), %eax
testb $0x20, %al
movq %rcx, 0x8(%rsp)
je 0xc781
movsd 0x10(%rsp), %xmm2
subsd %xmm1, %xmm2
movq %rbx, %rdi
leaq 0x14659(%rip), %rsi     # 0x20dd4
movapd %xmm2, %xmm1
jmp 0xc78b
movq %rbx, %rdi
leaq 0x1463b(%rip), %rsi     # 0x20dc6
movb $0x2, %al
callq 0x1130
testl %r12d, %r12d
jle 0xc8bb
addq $0x28, %r14
xorl %ebp, %ebp
movl (%r15,%rbp,4), %eax
cmpl $0x1, %eax
je 0xc7f9
cmpl $0x2, %eax
jne 0xc8ab
movl 0x88(%rsp), %eax
testb $0x20, %al
je 0xc849
movsd 0x10(%rsp), %xmm3
movapd %xmm3, %xmm1
subsd -0x10(%r14), %xmm1
movsd -0x18(%r14), %xmm0
movsd -0x8(%r14), %xmm2
subsd (%r14), %xmm3
movq %rbx, %rdi
leaq 0x14609(%rip), %rsi     # 0x20df6
movb $0x4, %al
callq 0x1130
jmp 0xc8ab
movsd -0x28(%r14), %xmm0
movsd -0x20(%r14), %xmm1
movl 0x88(%rsp), %eax
testb $0x20, %al
je 0xc883
movsd 0x10(%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 0x145e5(%rip), %rsi     # 0x20e28
movapd %xmm6, %xmm1
jmp 0xc8a4
movsd -0x18(%r14), %xmm0
movsd -0x10(%r14), %xmm1
movq %rbx, %rdi
leaq 0x14589(%rip), %r13     # 0x20de8
movq %r13, %rsi
movb $0x2, %al
callq 0x1130
movsd -0x8(%r14), %xmm0
movsd (%r14), %xmm1
movq %rbx, %rdi
movq %r13, %rsi
movb $0x2, %al
callq 0x1130
jmp 0xc8ab
movsd -0x18(%r14), %xmm2
movsd -0x10(%r14), %xmm3
movsd -0x8(%r14), %xmm4
movsd (%r14), %xmm5
movq %rbx, %rdi
leaq 0x14569(%rip), %rsi     # 0x20e0d
movb $0x6, %al
callq 0x1130
incq %rbp
addq $0x30, %r14
cmpq %rbp, %r12
jne 0xc7a1
movq 0x8(%rsp), %r15
movq 0x20(%r15), %r14
movl 0x88(%rsp), %eax
testq %r14, %r14
je 0xc8e1
testb $0x20, %al
jne 0xc8db
cmpl $0x2b, 0x4(%r14)
je 0xc92c
testb $0x20, %al
jne 0xc8e5
jmp 0xc953
testb $0x20, %al
je 0xc92c
movq %rbx, %rdi
leaq 0x14574(%rip), %rsi     # 0x20e63
movl 0x4(%rsp), %edx
movq 0x30(%rsp), %rcx
movl 0x80(%rsp), %r8d
movl %edx, %r9d
xorl %eax, %eax
pushq %r8
pushq %rcx
callq 0x1130
addq $0x10, %rsp
movl $0x4, %esi
movl $0x1, %edx
leaq 0x1456e(%rip), %rdi     # 0x20e90
movq %rbx, %rcx
callq 0x1210
jmp 0xc953
movq %rbx, %rdi
leaq 0x14512(%rip), %rsi     # 0x20e48
movsd 0x28(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
movb $0x3, %al
callq 0x1130
movq 0x20(%r15), %r14
movq %r14, %rcx
testq %r14, %r14
jne 0xc735
movl 0x88(%rsp), %eax
testb $0x20, %al
je 0xc978
leaq 0x1452e(%rip), %rdi     # 0x20e9f
movl $0x5, %esi
jmp 0xc984
leaq 0x14516(%rip), %rdi     # 0x20e95
movl $0x9, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1210
xorl %eax, %eax
jmp 0xc9bd
callq 0x1260
jmp 0xc9b8
callq 0x1050
movl $0xc, (%rax)
jmp 0xc9b3
movq 0x20(%rsp), %rdi
callq 0x1030
callq 0x12b8
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x128c
jmp 0xc9b8