// File: /workspace/github_c_makefile_star2_O1/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, 0xc(%rsp)
movl %ecx, %r15d
movl %edx, %r12d
movq %rdi, %rbx
testl %edx, %edx
movq %rsi, 0x18(%rsp)
je 0xc575
leal -0x1(%r12), %eax
leal 0x3e(%r12), %ebp
testl %eax, %eax
cmovnsl %eax, %ebp
sarl $0x6, %ebp
incl %ebp
jmp 0xc577
xorl %ebp, %ebp
movl %ebp, %esi
negl %esi
cmovsl %ebp, %esi
movslq %r15d, %rdi
movq %rsi, %rcx
imulq %rdi, %rcx
movq $-0x1, %r14
testq %rcx, %rcx
js 0xc5bd
shlq $0x3, %rcx
testl %r15d, %r15d
sete %al
testl %ebp, %ebp
sete %dl
orb %al, %dl
jne 0xc5ba
movq %rcx, %rax
cqto
idivq %rdi
cqto
idivq %rsi
cmpq $0x8, %rax
jne 0xc5bd
movq %rcx, %r14
testq %r14, %r14
js 0xc607
movl $0x18, %edi
callq 0x1190
testq %rax, %rax
je 0xc612
movq %rax, %r13
testq %r14, %r14
movl $0x8, %esi
cmovneq %r14, %rsi
movl %r12d, (%rax)
movl %r15d, 0x4(%rax)
movl %ebp, 0x8(%rax)
movl $0x1, %edi
callq 0x1120
movq %rax, 0x10(%r13)
testq %rax, %rax
jne 0xc615
movq %r13, %rdi
callq 0x1030
jmp 0xc612
callq 0x1050
movl $0xc, (%rax)
xorl %r13d, %r13d
testq %r13, %r13
je 0xca74
movl %r15d, (%rsp)
testl %r15d, %r15d
jle 0xc6f9
movl (%rsp), %eax
movslq %r12d, %rcx
movl %r12d, %edx
leaq (%rcx,%rcx,2), %rsi
imulq %rax, %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x18(%rsp), %rdi
addq %rcx, %rdi
addq $0x2, %rdi
negq %rsi
xorl %r8d, %r8d
movabsq $-0x8000000000000000, %r9 # imm = 0x8000000000000000
testl %r12d, %r12d
jle 0xc6ea
movslq (%r13), %r10
movq %rdi, %r11
xorl %r15d, %r15d
movb $0x1, %bpl
cmpb $0x0, -0x2(%r11)
jne 0xc688
cmpb $0x0, -0x1(%r11)
jne 0xc688
cmpb $0x0, (%r11)
setne %bpl
cmpq %r10, %r15
jge 0xc6de
movslq 0x4(%r13), %r14
cmpq %r14, %r8
jge 0xc6de
movq %r9, %r14
movl %r15d, %ecx
shrq %cl, %r14
testb %bpl, %bpl
je 0xc6c1
movslq 0x8(%r13), %rbp
imulq %r8, %rbp
shlq $0x3, %rbp
addq 0x10(%r13), %rbp
movl %r15d, %ecx
shrl $0x6, %ecx
orq %r14, (%rbp,%rcx,8)
jmp 0xc6de
notq %r14
movslq 0x8(%r13), %rcx
imulq %r8, %rcx
shlq $0x3, %rcx
addq 0x10(%r13), %rcx
movl %r15d, %ebp
shrl $0x6, %ebp
andq %r14, (%rcx,%rbp,8)
incq %r15
addq $0x3, %r11
cmpq %r15, %rdx
jne 0xc66f
incq %r8
addq %rsi, %rdi
cmpq %rax, %r8
jne 0xc65c
movl $0x48, %edi
callq 0x1190
testq %rax, %rax
je 0xc71c
leaq 0x10e49(%rip), %rsi     # 0x1d558
movl $0x9, %ecx
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
jmp 0xc71e
xorl %eax, %eax
movl (%rsp), %ebp
testq %rax, %rax
je 0xca7b
movl $0x0, (%rax)
movq %rax, %rdi
movq %r13, %rsi
callq 0xc225
testq %rax, %rax
je 0xca6d
movq %rax, %r14
cmpl $0x0, (%rax)
jne 0xca6d
movq 0x10(%r13), %rdi
testq %rdi, %rdi
je 0xc77b
movslq 0x8(%r13), %rax
testq %rax, %rax
jns 0xc776
movslq 0x4(%r13), %rcx
testq %rcx, %rcx
je 0xc776
decq %rcx
imulq %rax, %rcx
leaq (%rdi,%rcx,8), %rdi
callq 0x1030
movl 0x70(%rsp), %r15d
movq %r13, %rdi
callq 0x1030
movl 0x78(%rsp), %eax
testb $0x20, %al
je 0xc7af
leaq 0x134d7(%rip), %rsi     # 0x1fc6e
movq %rbx, %rdi
movl 0xc(%rsp), %edx
movq 0x10(%rsp), %rcx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x1140
jmp 0xc7c8
leaq 0x134b1(%rip), %rdi     # 0x1fc67
movl $0x6, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1220
movq 0x8(%r14), %r12
testq %r12, %r12
je 0xca3a
cvtsi2sd %ebp, %xmm0
movsd %xmm0, (%rsp)
cvtsi2sdl 0xc(%rsp), %xmm1
movsd 0x13274(%rip), %xmm0    # 0x1fa60
divsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
xorps %xmm1, %xmm1
cvtsi2sdl 0x10(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
movslq 0x8(%r12), %r15
leaq (%r15,%r15,2), %rax
shlq $0x4, %rax
movq 0x10(%r12), %r14
movq 0x18(%r12), %rbp
movsd -0x10(%rbp,%rax), %xmm0
movsd -0x8(%rbp,%rax), %xmm1
movl 0x78(%rsp), %eax
testb $0x20, %al
movq %r12, 0x18(%rsp)
je 0xc864
movsd (%rsp), %xmm2
subsd %xmm1, %xmm2
movq %rbx, %rdi
leaq 0x13436(%rip), %rsi     # 0x1fc94
movapd %xmm2, %xmm1
jmp 0xc86e
movq %rbx, %rdi
leaq 0x13418(%rip), %rsi     # 0x1fc86
movb $0x2, %al
callq 0x1140
testl %r15d, %r15d
jle 0xc985
addq $0x28, %rbp
xorl %r12d, %r12d
movl (%r14,%r12,4), %eax
cmpl $0x1, %eax
je 0xc8d2
cmpl $0x2, %eax
jne 0xc975
movl 0x78(%rsp), %eax
testb $0x20, %al
je 0xc919
movsd (%rsp), %xmm3
movapd %xmm3, %xmm1
subsd -0x10(%rbp), %xmm1
movsd -0x18(%rbp), %xmm0
movsd -0x8(%rbp), %xmm2
subsd (%rbp), %xmm3
movq %rbx, %rdi
leaq 0x133f0(%rip), %rsi     # 0x1fcb6
movb $0x4, %al
callq 0x1140
jmp 0xc975
movsd -0x28(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
movl 0x78(%rsp), %eax
testb $0x20, %al
je 0xc950
movsd (%rsp), %xmm5
movapd %xmm5, %xmm6
subsd %xmm1, %xmm6
movapd %xmm5, %xmm3
subsd -0x10(%rbp), %xmm3
movsd -0x18(%rbp), %xmm2
movsd -0x8(%rbp), %xmm4
subsd (%rbp), %xmm5
movq %rbx, %rdi
leaq 0x133d5(%rip), %rsi     # 0x1fce8
movapd %xmm6, %xmm1
jmp 0xc96e
movsd -0x18(%rbp), %xmm0
movsd -0x10(%rbp), %xmm1
movq %rbx, %rdi
leaq 0x1337b(%rip), %r13     # 0x1fca8
movq %r13, %rsi
movb $0x2, %al
callq 0x1140
movsd -0x8(%rbp), %xmm0
movsd (%rbp), %xmm1
movq %rbx, %rdi
movq %r13, %rsi
movb $0x2, %al
callq 0x1140
jmp 0xc975
movsd -0x18(%rbp), %xmm2
movsd -0x10(%rbp), %xmm3
movsd -0x8(%rbp), %xmm4
movsd (%rbp), %xmm5
movq %rbx, %rdi
leaq 0x1335f(%rip), %rsi     # 0x1fccd
movb $0x6, %al
callq 0x1140
incq %r12
addq $0x30, %rbp
cmpq %r12, %r15
jne 0xc885
movq 0x18(%rsp), %r12
movq 0x20(%r12), %rax
testq %rax, %rax
je 0xca27
movl 0x78(%rsp), %ecx
testb $0x20, %cl
movl 0x70(%rsp), %r14d
jne 0xc9cf
cmpl $0x2b, 0x4(%rax)
jne 0xc9cf
movq %rbx, %rdi
leaq 0x13352(%rip), %rsi     # 0x1fd08
movsd 0x30(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
movb $0x3, %al
callq 0x1140
movq 0x20(%r12), %r12
movl 0x78(%rsp), %eax
testb $0x20, %al
je 0xca1c
movq %rbx, %rdi
leaq 0x1333d(%rip), %rsi     # 0x1fd23
movl 0xc(%rsp), %edx
movq 0x10(%rsp), %rcx
movl %r14d, %r8d
movl %edx, %r9d
xorl %eax, %eax
pushq %r14
pushq %rcx
callq 0x1140
addq $0x10, %rsp
movl $0x4, %esi
movl $0x1, %edx
leaq 0x1333c(%rip), %rdi     # 0x1fd50
movq %rbx, %rcx
callq 0x1220
testq %r12, %r12
jne 0xc81b
jmp 0xca3a
movl 0x78(%rsp), %eax
testb $0x20, %al
movl 0x70(%rsp), %r14d
je 0xc9ac
jmp 0xc9cf
movl 0x78(%rsp), %eax
testb $0x20, %al
je 0xca50
leaq 0x13316(%rip), %rdi     # 0x1fd5f
movl $0x5, %esi
jmp 0xca5c
leaq 0x132fe(%rip), %rdi     # 0x1fd55
movl $0x9, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1220
xorl %eax, %eax
jmp 0xca85
callq 0x1270
jmp 0xca80
callq 0x12c8
jmp 0xca80
callq 0x129c
movl $0x1, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq