// File: /workspace/github_c_makefile_star2_O0/yui0[P]img2vec/img2vec.c
// Function: main
int main(int argc, char* argv[])
{
	char *name = argv[1];
	char *outfile = "img2vec.eps";
	int color = 32;
	int flag = 0;
	float scale = 2;
	int bit = 4;

	if (argc <=1) {
		usage(stderr, argv);
		return 0;
	}
	for (int i=1; i<argc; i++) {
		if (!strcmp(argv[i], "-o")) {
			outfile = argv[++i];
		} else if (!strcmp(argv[i], "-c")) {
			color = atoi(argv[++i]);
		} else if (!strcmp(argv[i], "-d")) {
			flag |= 1; // debug
		} else if (!strcmp(argv[i], "-x")) {
			flag |= 2; // dilate
		} else if (!strcmp(argv[i], "-a")) {
			flag |= 4; // alpha
		} else if (!strcmp(argv[i], "-b")) {
			flag |= 8; // blur
			scale = atof(argv[++i]);
		} else if (!strcmp(argv[i], "-cx")) {
			flag |= 16;
			bit = atoi(argv[++i]);
		} else if (!strcmp(argv[i], "-svg")) {
			flag |= 32;
		} else if (!strcmp(argv[i], "-s")) {
			flag |= 64;
			scale = atof(argv[++i]);
		} else if (!strcmp(argv[i], "-h")) {
			usage(stderr, argv);
			return 0;
		} else {
			name = argv[i];
			//printf("%s\n", name);
		}
	}

	uint8_t *pixels;
	int w, h, bpp;
	pixels = stbi_load(name, &w, &h, &bpp, 3);
	assert(pixels);

	/*uint8_t *posterized = malloc(w*h*6);
	_imgp_dilate(pixels, w, h, 3, posterized);
	stbi_write_jpg("original_dilated.jpg", w, h, 3, posterized, 0);
	color_quant(&(image_t){w, h, posterized}, 64);
	stbi_write_bmp("original_d64.bmp", w, h, 3, posterized);
	free(posterized);*/
	/*uint8_t *posterized = malloc(w*h*6);
	memcpy(posterized, pixels, w*h*3);
	color_quant(&(image_t){w, h, posterized}, 64);
	stbi_write_bmp("original_d64.bmp", w, h, 3, posterized);
	_imgp_dilate(posterized, w, h, 3, posterized+w*h*3);
	stbi_write_jpg("original_dilated.jpg", w, h, 3, posterized+w*h*3, 0);
	free(posterized);*/
	if (flag&8) { // blur
		int sx = w*scale;	// 2000
		int sy = h*scale;	// 2800
		uint8_t *posterized = malloc(sx*sy *3 *2);
		stbir_resize_uint8_srgb(pixels, w, h, 0, posterized+sx*sy*3, sx, sy, 0, 3, -1, 0);
		imgp_filter(posterized+sx*sy*3, sx, sy, posterized, magic_kernel, 4, 1, 0);
		stbir_resize_uint8_srgb(posterized, sx, sy, 0, pixels, w, h, 0, 3, -1, 0);
		if (flag&1) stbi_write_jpg("magic.jpg", w, h, 3, pixels, 0);
		free(posterized);
	}
	if (flag&16) { // 24bit -> 12bit
		//imgp_cq24to15(pixels, w, h, 3, pixels, 1);
		uint8_t *p = pixels;
		for (int n=0; n<w*h*3; n++) {
//			*p = *p & 0b11110000;
			*p = ((*p)>>bit)<<bit;
			p++;
		}
	}
	if (flag&64) { // scale
		int sx = w*scale;
		int sy = h*scale;
		uint8_t *posterized = malloc(sx*sy *3);
		if (scale<1) {
			uint8_t *pix = malloc(w*h *3);
			imgp_filter(pixels, w, h, pix, magic_kernel, 4, 1, 0);
			stbir_resize_uint8_srgb(pix, w, h, 0, posterized, sx, sy, 0, 3, -1, 0);
			free(pix);
		} else {
			posterized = malloc(sx*sy *3);
			stbir_resize_uint8_srgb(pixels, w, h, 0, posterized, sx, sy, 0, 3, -1, 0);
		}
		free(pixels);
		pixels = posterized;
		w = sx;
		h = sy;
	}
	color_quant(pixels, w, h, color, outfile, flag);

/*	uint8_t *gray = malloc(w*h*4);
	uint8_t *dilated = gray+w*h;
	uint8_t *diff = gray+w*h*2;
	uint8_t *contour = gray+w*h*3;
	imgp_gray(pixels, w, h, w, gray, w);
	color_quant(pixels, w, h, color, outfile, flag);
	//stbi_write_bmp("original64.bmp", w, h, 3, pixels);
	//stbi_write_bmp("gray.bmp", w, h, 1, gray);
	imgp_dilate(gray, w, h, 1, dilated);
	//stbi_write_jpg("dilated.jpg", w, h, 1, dilated, 0);
	imgp_absdiff(gray, dilated, w, h, diff);
	//stbi_write_jpg("diff.jpg", w, h, 1, diff, 0);
	imgp_reverse(diff, w, h, contour);
	//stbi_write_jpg("contour.jpg", w, h, 1, contour, 0);
	//stbi_write_bmp("contour.bmp", w, h, 1, contour);
	free(gray);*/
	stbi_image_free(pixels);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
leaq 0x241f2(%rip), %rax     # 0x3255e
movq %rax, -0x20(%rbp)
movl $0x20, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movss 0x21caa(%rip), %xmm0    # 0x30030
movss %xmm0, -0x2c(%rbp)
movl $0x4, -0x30(%rbp)
cmpl $0x1, -0x8(%rbp)
jg 0xe3b7
movq 0x2bc59(%rip), %rax     # 0x39ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0xe310
movl $0x0, -0x4(%rbp)
jmp 0xe9a5
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0xe63a
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x2418d(%rip), %rsi     # 0x3256a
callq 0x1130
cmpl $0x0, %eax
jne 0xe404
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x34(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
jmp 0xe62a
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x24156(%rip), %rsi     # 0x3256d
callq 0x1130
cmpl $0x0, %eax
jne 0xe442
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x34(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x11f0
movl %eax, -0x24(%rbp)
jmp 0xe628
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x2411b(%rip), %rsi     # 0x32570
callq 0x1130
cmpl $0x0, %eax
jne 0xe46d
movl -0x28(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xe626
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x240f3(%rip), %rsi     # 0x32573
callq 0x1130
cmpl $0x0, %eax
jne 0xe498
movl -0x28(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x28(%rbp)
jmp 0xe624
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x240cb(%rip), %rsi     # 0x32576
callq 0x1130
cmpl $0x0, %eax
jne 0xe4c3
movl -0x28(%rbp), %eax
orl $0x4, %eax
movl %eax, -0x28(%rbp)
jmp 0xe622
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x240a3(%rip), %rsi     # 0x32579
callq 0x1130
cmpl $0x0, %eax
jne 0xe510
movl -0x28(%rbp), %eax
orl $0x8, %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x34(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1090
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x2c(%rbp)
jmp 0xe620
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x24059(%rip), %rsi     # 0x3257c
callq 0x1130
cmpl $0x0, %eax
jne 0xe557
movl -0x28(%rbp), %eax
orl $0x10, %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x34(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x11f0
movl %eax, -0x30(%rbp)
jmp 0xe61e
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x24016(%rip), %rsi     # 0x32580
callq 0x1130
cmpl $0x0, %eax
jne 0xe582
movl -0x28(%rbp), %eax
orl $0x20, %eax
movl %eax, -0x28(%rbp)
jmp 0xe61c
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x23ff0(%rip), %rsi     # 0x32585
callq 0x1130
cmpl $0x0, %eax
jne 0xe5cc
movl -0x28(%rbp), %eax
orl $0x40, %eax
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, -0x34(%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rdi
callq 0x1090
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x2c(%rbp)
jmp 0xe61a
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x23fa9(%rip), %rsi     # 0x32588
callq 0x1130
cmpl $0x0, %eax
jne 0xe608
movq 0x2ba08(%rip), %rax     # 0x39ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0xe310
movl $0x0, -0x4(%rbp)
jmp 0xe9a5
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x18(%rbp)
jmp 0xe61a
jmp 0xe61c
jmp 0xe61e
jmp 0xe620
jmp 0xe622
jmp 0xe624
jmp 0xe626
jmp 0xe628
jmp 0xe62a
jmp 0xe62c
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xe3be
movq -0x18(%rbp), %rdi
leaq -0x44(%rbp), %rsi
leaq -0x48(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
movl $0x3, %r8d
callq 0x1400
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0xe662
jmp 0xe681
leaq 0x24404(%rip), %rdi     # 0x32a6d
leaq 0x23f1b(%rip), %rsi     # 0x3258b
movl $0x10e, %edx            # imm = 0x10E
leaq 0x23f19(%rip), %rcx     # 0x32595
callq 0x10e0
movl -0x28(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0xe7c3
cvtsi2ssl -0x44(%rbp), %xmm0
mulss -0x2c(%rbp), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x50(%rbp)
cvtsi2ssl -0x48(%rbp), %xmm0
mulss -0x2c(%rbp), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x54(%rbp)
movl -0x50(%rbp), %eax
imull -0x54(%rbp), %eax
imull $0x3, %eax, %eax
shll %eax
movslq %eax, %rdi
callq 0x1190
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %rdi
movl -0x44(%rbp), %esi
movl -0x48(%rbp), %edx
movq -0x60(%rbp), %r8
movl -0x50(%rbp), %eax
imull -0x54(%rbp), %eax
imull $0x3, %eax, %eax
cltq
addq %rax, %r8
movl -0x50(%rbp), %r9d
movl -0x54(%rbp), %eax
xorl %ecx, %ecx
movl %eax, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x3, 0x10(%rsp)
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x20(%rsp)
callq 0x69c0
movq -0x60(%rbp), %rdi
movl -0x50(%rbp), %eax
imull -0x54(%rbp), %eax
imull $0x3, %eax, %eax
cltq
addq %rax, %rdi
movl -0x50(%rbp), %esi
movl -0x54(%rbp), %edx
movq -0x60(%rbp), %rcx
leaq 0x2bae4(%rip), %r8      # 0x3a220
movl $0x4, %r9d
movsd 0x238ce(%rip), %xmm0    # 0x32018
xorps %xmm1, %xmm1
callq 0x77b0
movq -0x60(%rbp), %rdi
movl -0x50(%rbp), %esi
movl -0x54(%rbp), %edx
movq -0x40(%rbp), %r8
movl -0x44(%rbp), %r9d
movl -0x48(%rbp), %eax
xorl %ecx, %ecx
movl %eax, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x3, 0x10(%rsp)
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x20(%rsp)
callq 0x69c0
movl -0x28(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xe7ba
movl -0x44(%rbp), %esi
movl -0x48(%rbp), %edx
movq -0x40(%rbp), %r8
leaq 0x23dff(%rip), %rdi     # 0x325ac
movl $0x3, %ecx
xorl %r9d, %r9d
callq 0x6620
movq -0x60(%rbp), %rdi
callq 0x1030
movl -0x28(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0xe820
movq -0x40(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movl -0x44(%rbp), %ecx
imull -0x48(%rbp), %ecx
imull $0x3, %ecx, %ecx
cmpl %ecx, %eax
jge 0xe81e
movq -0x68(%rbp), %rax
movzbl (%rax), %eax
movl -0x30(%rbp), %ecx
sarl %cl, %eax
movl -0x30(%rbp), %ecx
shll %cl, %eax
movb %al, %cl
movq -0x68(%rbp), %rax
movb %cl, (%rax)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0xe7dd
jmp 0xe820
movl -0x28(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0xe982
cvtsi2ssl -0x44(%rbp), %xmm0
mulss -0x2c(%rbp), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x70(%rbp)
cvtsi2ssl -0x48(%rbp), %xmm0
mulss -0x2c(%rbp), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x74(%rbp)
movl -0x70(%rbp), %eax
imull -0x74(%rbp), %eax
imull $0x3, %eax, %eax
movslq %eax, %rdi
callq 0x1190
movq %rax, -0x80(%rbp)
movss 0x21795(%rip), %xmm0    # 0x30004
ucomiss -0x2c(%rbp), %xmm0
jbe 0xe910
movl -0x44(%rbp), %eax
imull -0x48(%rbp), %eax
imull $0x3, %eax, %eax
movslq %eax, %rdi
callq 0x1190
movq %rax, -0x88(%rbp)
movq -0x40(%rbp), %rdi
movl -0x44(%rbp), %esi
movl -0x48(%rbp), %edx
movq -0x88(%rbp), %rcx
leaq 0x2b976(%rip), %r8      # 0x3a220
movl $0x4, %r9d
movsd 0x23760(%rip), %xmm0    # 0x32018
xorps %xmm1, %xmm1
callq 0x77b0
movq -0x88(%rbp), %rdi
movl -0x44(%rbp), %esi
movl -0x48(%rbp), %edx
movq -0x80(%rbp), %r8
movl -0x70(%rbp), %r9d
movl -0x74(%rbp), %eax
xorl %ecx, %ecx
movl %eax, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x3, 0x10(%rsp)
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x20(%rsp)
callq 0x69c0
movq -0x88(%rbp), %rdi
callq 0x1030
jmp 0xe965
movl -0x70(%rbp), %eax
imull -0x74(%rbp), %eax
imull $0x3, %eax, %eax
movslq %eax, %rdi
callq 0x1190
movq %rax, -0x80(%rbp)
movq -0x40(%rbp), %rdi
movl -0x44(%rbp), %esi
movl -0x48(%rbp), %edx
movq -0x80(%rbp), %r8
movl -0x70(%rbp), %r9d
movl -0x74(%rbp), %eax
xorl %ecx, %ecx
movl %eax, (%rsp)
movl $0x0, 0x8(%rsp)
movl $0x3, 0x10(%rsp)
movl $0xffffffff, 0x18(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x20(%rsp)
callq 0x69c0
movq -0x40(%rbp), %rdi
callq 0x1030
movq -0x80(%rbp), %rax
movq %rax, -0x40(%rbp)
movl -0x70(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0x40(%rbp), %rdi
movl -0x44(%rbp), %esi
movl -0x48(%rbp), %edx
movl -0x24(%rbp), %ecx
movq -0x20(%rbp), %r8
movl -0x28(%rbp), %r9d
callq 0xdd10
movq -0x40(%rbp), %rdi
callq 0x1380
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)