// File: /workspace/github_cpp_makefile_star2_O1/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::skeletonize(Image&, Image&)
void ImageProc::skeletonize(Image &img_binary, Image &img_skeletonized)
{
	Image img_after_first("bw", img_binary.rows, img_binary.cols);

		int index  = 0, superflag = 1, flag = 0, black_pixel_count = 0, pixel_count = 0;
		std::string target;
		//char *target_char_arr = new char[9];

		for(int i = 0; i < img_binary.rows; i++)
			for(int j = 0; j < img_binary.cols; j++)
				for(int k = 0; k < img_binary.channels; k++)
					img_skeletonized.setvalue(i,j,k,img_binary.getvalue(i,j,k));

		for(int i = 0+1; i < img_skeletonized.rows-1; i++)
			for(int j = 0+1; j < img_skeletonized.cols-1; j++)
				for(int k = 0; k < img_skeletonized.channels; k++)
					if(img_skeletonized.getvalue(i,j,k) == 0) pixel_count++;
		black_pixel_count = pixel_count;

		//std::cout << " 1 " << black_pixel_count << std::endl;

		while(superflag == 1)
		{
			for(int i = 0; i < img_skeletonized.rows; i++)
				for(int j = 0; j < img_skeletonized.cols; j++)
					for(int k = 0; k < img_skeletonized.channels; k++)
						img_after_first.setvalue(i,j,k,255);

			for(int i = 0+1; i < img_skeletonized.rows-1; i++)
				for(int j = 0+1; j < img_skeletonized.cols-1; j++)
					for(int k = 0; k < img_skeletonized.channels; k++)
					{
						if(img_skeletonized.getvalue(i,j,k) == 0)
						{
							index = -1; flag = 0;
							target.clear();
							for(int p = i-1; p <= i+1; p++)
								for(int q = j-1; q <= j+1; q++)
								{
									//std::cout << img_shrinked.getvalue(p,q,k)/255 << std::endl;
									if(!(p==i && q==j))
										if(img_skeletonized.getvalue(p,q,0) == 0)
											target.append("1");
										else
											target.append("0");
									else
										continue;
								}
							//target_char_arr[8] = NULL;
							//target(target_char_arr);
							//std::cout << "target 1:" << target << std::endl;

							//std::string target_str(target);
							for(int t = 0; t < SKELETON_FIRST_PATTERNS; t++)
							{
								std::string testpattern(skeleton_first[t]);
								flag = 0;
								if(testpattern.find('D') == std::string::npos)
								{
									if(target.compare(testpattern) == 0)
									{
										//std::cout << "pattern match" << std::endl;
										flag = 1;
									}
									else
									{
										//std::cout << "pattern nomatch" << std::endl;
										flag = 0;
									}
								}
								else
								{
									for(int j = 0; j < 8; j++)
									{
										if(testpattern[j] == 'D')
											continue;
										else if(target[j] != testpattern[j])
										{
											//std::cout << target << " hey " << testpattern << std::endl;
											flag = 0;
											break;
										}
										else
											flag = 1;
									}
								}

								if(flag == 1)
									break;
								else
									continue;

							}

							if(flag == 1)
								img_after_first.setvalue(i,j,k,0*255);
							else
								img_after_first.setvalue(i,j,k,1*255);

						}
					}

				for(int i = 0+1; i < img_after_first.rows-1; i++)
					for(int j = 0+1; j < img_after_first.cols-1; j++)
						for(int k = 0; k < img_after_first.channels; k++)
						{
							if(img_after_first.getvalue(i,j,k) == 0)
							{
								target.clear();
								index = -1; flag = 0;
								for(int p = i-1; p <= i+1; p++)
									for(int q = j-1; q <= j+1; q++)
									{
										if(!(p==i && q==j))
											if(img_after_first.getvalue(p,q,0) == 0)
												target.append("M");
											else
												target.append("0");
									}

								//std::cout << "target1: " << target;

								for(int t = 0; t < SKELETON_SECOND_PATTERNS; t++)
								{
									std::string testpattern(skeleton_second[t]);
									flag = 1;
									//std::cout << " " << testpattern << std::endl;
									if(testpattern.find('D') == std::string::npos)
									{

										if(target.compare(testpattern) == 0)
										{
											//std::cout << "hi" << std::endl;
											flag = 1;
											break;
										}
										else
											flag = 0;
									}
									else
									{
										for(int j = 0; j < 8; j++)
										{
											if(testpattern[j] == 'D')
												continue;
											else if(target[j] == testpattern[j])
											{
												flag = 1;
												//std::cout << "match" << std::endl;
											}
											else
											{
												//std::cout << "breakpoint" << std::endl;
												flag = 0;
												break;
											}
										}
									}

									if(flag == 0)
										continue;
									else
										break;
								}
								//std::cout << flag << std::endl;
								if(flag == 0)
								{
									//std::cout << "reducing" << std::endl;
									img_skeletonized.setvalue(i,j,k,1*255);
								}
								else
									img_skeletonized.setvalue(i,j,k,img_skeletonized.getvalue(i,j,k));
							}


						}

			pixel_count = 0;
			for(int i = 0+1; i < img_skeletonized.rows-1; i++)
				for(int j = 0+1; j < img_skeletonized.cols-1; j++)
					for(int k = 0; k < img_skeletonized.channels; k++)
						if(img_skeletonized.getvalue(i,j,k) == 0) pixel_count++;



			if(pixel_count == black_pixel_count) superflag = 0;
			else								 black_pixel_count = pixel_count;
			//std::cout << black_pixel_count << std::endl;


		}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl 0x8(%rdi), %ecx
movl 0xc(%rdi), %edx
leaq 0x2d73(%rip), %rsi      # 0x121e7
leaq 0xb8(%rsp), %rdi
callq 0x4c6e
leaq 0x60(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpl $0x0, 0xc(%r14)
jle 0xf4fa
xorl %eax, %eax
cmpl $0x0, 0x8(%r14)
jle 0xf4f2
xorl %ecx, %ecx
movl 0x10(%r14), %esi
testl %esi, %esi
jle 0xf4ea
xorl %edx, %edx
movl 0x8(%r14), %edi
imull %eax, %edi
addl %ecx, %edi
imull %esi, %edi
movslq %edi, %rsi
addq 0x18(%r14), %rsi
movl 0x8(%rbx), %edi
imull %eax, %edi
addl %ecx, %edi
imull 0x10(%rbx), %edi
movb (%rdx,%rsi), %sil
movslq %edi, %rdi
addq 0x18(%rbx), %rdi
movb %sil, (%rdx,%rdi)
movl 0x10(%r14), %esi
incq %rdx
cmpl %esi, %edx
jl 0xf4b1
incl %ecx
cmpl 0x8(%r14), %ecx
jl 0xf4a7
incl %eax
cmpl 0xc(%r14), %eax
jl 0xf49e
movl 0xc(%rbx), %eax
xorl %r12d, %r12d
cmpl $0x3, %eax
jl 0xf56d
decl %eax
movl 0x8(%rbx), %ecx
movl 0x10(%rbx), %edx
leal -0x1(%rcx), %esi
movq 0x18(%rbx), %rdi
leal 0x1(%rcx), %r8d
imull %edx, %r8d
movl %ecx, %r9d
imull %edx, %r9d
xorl %r12d, %r12d
movl $0x1, %r10d
cmpl $0x3, %ecx
jl 0xf562
movl $0x1, %r11d
movl %r8d, %ebp
testl %edx, %edx
jle 0xf558
movslq %ebp, %r14
addq %rdi, %r14
xorl %r15d, %r15d
cmpb $0x1, (%r14,%r15)
adcl $0x0, %r12d
incq %r15
cmpq %r15, %rdx
jne 0xf547
incq %r11
addl %edx, %ebp
cmpq %rsi, %r11
jne 0xf53a
incq %r10
addl %r9d, %r8d
cmpq %rax, %r10
jne 0xf52c
movq 0xd0(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0xc8(%rsp), %r10d
movl 0xc0(%rsp), %eax
movl 0xc4(%rsp), %ecx
movq %rcx, 0x90(%rsp)
decl %ecx
movq %rcx, 0xa0(%rsp)
leal -0x2(%rax), %ecx
movq %rcx, 0xb0(%rsp)
movq %rax, 0x70(%rsp)
imull %r10d, %eax
movl %eax, 0xc(%rsp)
movq %r10, 0x28(%rsp)
cmpl $0x0, 0xc(%rbx)
jle 0xf60f
xorl %eax, %eax
xorl %ecx, %ecx
cmpl $0x0, 0x8(%rbx)
jle 0xf604
movl %eax, %edx
xorl %esi, %esi
cmpl $0x0, 0x10(%rbx)
jle 0xf5f5
movslq %edx, %rdi
addq 0x20(%rsp), %rdi
xorl %r8d, %r8d
movb $-0x1, (%rdi,%r8)
incq %r8
movslq 0x10(%rbx), %r9
cmpq %r9, %r8
jl 0xf5e4
incq %rsi
movslq 0x8(%rbx), %rdi
addl %r10d, %edx
cmpq %rdi, %rsi
jl 0xf5d3
incl %ecx
addl 0xc(%rsp), %eax
cmpl 0xc(%rbx), %ecx
jl 0xf5c9
movq %r12, 0x98(%rsp)
cmpl $0x3, 0xc(%rbx)
leaq 0x40(%rsp), %r12
jl 0xf870
movl $0x1, %r14d
movl $0x3, (%rsp)
cmpl $0x3, 0x8(%rbx)
jl 0xf85c
leal -0x1(%r14), %eax
movl %eax, 0x8(%rsp)
movq 0x70(%rsp), %rax
imull %r14d, %eax
movq %rax, 0x10(%rsp)
movl $0x1, %edx
xorl %r13d, %r13d
movl 0x10(%rbx), %eax
testl %eax, %eax
jle 0xf846
movq 0x10(%rsp), %rcx
addl %edx, %ecx
movl %ecx, 0x78(%rsp)
xorl %esi, %esi
movq %rdx, 0x18(%rsp)
movl 0x8(%rbx), %ecx
imull %r14d, %ecx
addl %edx, %ecx
imull %eax, %ecx
movslq %ecx, %rax
movq 0x18(%rbx), %rcx
addq %rsi, %rcx
cmpb $0x0, (%rax,%rcx)
jne 0xf836
movq %rsi, 0x80(%rsp)
movq $0x0, 0x58(%rsp)
movq 0x50(%rsp), %rax
movb $0x0, (%rax)
movl 0x8(%rsp), %ebp
xorl %r15d, %r15d
cmpl %r14d, %ebp
sete %al
cmpq $0x1, %r15
sete %cl
testb %cl, %al
jne 0xf6fe
movq 0x18(%rbx), %rax
movl 0x8(%rbx), %ecx
imull %ebp, %ecx
addl %r13d, %ecx
addl %r15d, %ecx
imull 0x10(%rbx), %ecx
movslq %ecx, %rcx
cmpb $0x0, (%rax,%rcx)
leaq 0x2312(%rip), %rsi      # 0x119fd
je 0xf6f4
leaq 0x2653(%rip), %rsi      # 0x11d47
leaq 0x50(%rsp), %rdi
callq 0x22f0
incq %r15
cmpl $0x3, %r15d
jne 0xf6b8
incl %ebp
cmpl (%rsp), %ebp
jne 0xf6b5
xorl %ebp, %ebp
leaq 0x30(%rsp), %r15
movq %rbp, %rax
shlq $0x5, %rax
movq %r12, 0x30(%rsp)
leaq 0x7818(%rip), %rcx      # 0x16f40
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0x10adc
movq %r15, %rdi
movl $0x44, %esi
xorl %edx, %edx
callq 0x21f0
cmpq $-0x1, %rax
je 0xf786
movq 0x30(%rsp), %rdi
xorl %eax, %eax
movq 0x50(%rsp), %rcx
xorl %edx, %edx
movb (%rdi,%rax), %sil
cmpb $0x44, %sil
je 0xf774
movl $0x1, %edx
cmpb %sil, (%rcx,%rax)
jne 0xf7b5
incq %rax
cmpq $0x8, %rax
jne 0xf75f
cmpl $0x1, %edx
sete %r15b
jmp 0xf7da
movq 0x58(%rsp), %r15
movq 0x38(%rsp), %r12
cmpq %r15, %r12
movq %r15, %rdx
cmovbq %r12, %rdx
testq %rdx, %rdx
je 0xf7c9
movq 0x30(%rsp), %rsi
movq 0x50(%rsp), %rdi
callq 0x21a0
testl %eax, %eax
sete %al
jmp 0xf7cb
cmpq %r12, %rdi
je 0xf7fb
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2190
jmp 0xf7fb
movb $0x1, %al
cmpq %r12, %r15
sete %r15b
andb %al, %r15b
leaq 0x40(%rsp), %r12
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0xf7f1
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2190
testb %r15b, %r15b
leaq 0x30(%rsp), %r15
jne 0xf80c
incq %rbp
cmpq $0x28, %rbp
jne 0xf715
movb $-0x1, %al
jmp 0xf80e
xorl %eax, %eax
movl 0x78(%rsp), %ecx
movq 0x28(%rsp), %r10
imull %r10d, %ecx
movslq %ecx, %rcx
movq 0x20(%rsp), %rdx
movq 0x80(%rsp), %rsi
addq %rsi, %rdx
movb %al, (%rcx,%rdx)
movq 0x18(%rsp), %rdx
incq %rsi
movslq 0x10(%rbx), %rax
cmpq %rax, %rsi
jl 0xf678
incq %rdx
movslq 0x8(%rbx), %rax
decq %rax
incq %r13
cmpq %rax, %rdx
jl 0xf65b
incl %r14d
movl 0xc(%rbx), %eax
decl %eax
incl (%rsp)
cmpl %eax, %r14d
jl 0xf633
cmpl $0x3, 0x90(%rsp)
jl 0xfb08
movl $0x3, (%rsp)
movl $0x1, %r14d
movl $0x0, 0x4(%rsp)
cmpl $0x3, 0x70(%rsp)
jl 0xfae8
movq %r14, %rax
imulq 0x70(%rsp), %rax
movq %rax, 0xa8(%rsp)
leal -0x1(%r14), %eax
movl %eax, 0x8(%rsp)
movl $0x1, %eax
movq %rax, 0x10(%rsp)
movl 0x4(%rsp), %eax
movl %eax, 0x18(%rsp)
movq $0x0, 0x88(%rsp)
testl %r10d, %r10d
jle 0xfabd
movq 0xa8(%rsp), %rax
movq 0x10(%rsp), %rcx
addl %ecx, %eax
imull %r10d, %eax
movslq %eax, %rdx
addq 0x20(%rsp), %rdx
xorl %esi, %esi
movq %rdx, 0x78(%rsp)
cmpb $0x0, (%rdx,%rsi)
jne 0xfab1
movq %rsi, 0x80(%rsp)
movq $0x0, 0x58(%rsp)
movq 0x50(%rsp), %rax
movb $0x0, (%rax)
movl 0x18(%rsp), %r12d
movl 0x8(%rsp), %eax
movl %eax, %r13d
movl %r13d, %r13d
movq $-0x1, %r15
movl %r12d, %ebp
movq %r14, %rax
xorq %r13, %rax
orq %r15, %rax
je 0xf96d
movslq %ebp, %rax
movq 0x20(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
leaq 0x20a5(%rip), %rsi      # 0x119ff
je 0xf963
leaq 0x23e4(%rip), %rsi      # 0x11d47
leaq 0x50(%rsp), %rdi
callq 0x22f0
incq %r15
addl 0x28(%rsp), %ebp
cmpq $0x2, %r15
jne 0xf93c
incl %r13d
addl 0xc(%rsp), %r12d
cmpl (%rsp), %r13d
jne 0xf92f
xorl %ebp, %ebp
leaq 0x40(%rsp), %r12
leaq 0x80ca(%rip), %r13      # 0x17a60
movq %rbp, %rax
shlq $0x5, %rax
movq %r12, 0x30(%rsp)
movq (%rax,%r13), %rsi
movq 0x8(%rax,%r13), %rdx
addq %rsi, %rdx
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x10adc
movq %r15, %rdi
movl $0x44, %esi
xorl %edx, %edx
callq 0x21f0
cmpq $-0x1, %rax
je 0xfa02
movq 0x30(%rsp), %rax
movq $-0x1, %rcx
movq 0x50(%rsp), %rdx
movb 0x1(%rax,%rcx), %sil
cmpb $0x44, %sil
je 0xf9f3
cmpb %sil, 0x1(%rdx,%rcx)
jne 0xfa32
incq %rcx
cmpq $0x7, %rcx
jne 0xf9e1
setb %r15b
jmp 0xfa53
movq 0x58(%rsp), %r12
movq 0x38(%rsp), %r13
cmpq %r12, %r13
movq %r12, %rdx
cmovbq %r13, %rdx
testq %rdx, %rdx
je 0xfa37
movq 0x30(%rsp), %rsi
movq 0x50(%rsp), %rdi
callq 0x21a0
testl %eax, %eax
sete %r15b
jmp 0xfa3a
movb $0x1, %r15b
jmp 0xfa53
movb $0x1, %r15b
cmpq %r13, %r12
setne %al
xorb $0x1, %r15b
orb %al, %r15b
leaq 0x40(%rsp), %r12
leaq 0x800d(%rip), %r13      # 0x17a60
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0xfa6a
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2190
cmpq $0x23, %rbp
leaq 0x1(%rbp), %rbp
setb %al
testb %al, %r15b
jne 0xf996
testb %r15b, %r15b
movq 0x28(%rsp), %r10
movq 0x78(%rsp), %rdx
movq 0x80(%rsp), %rsi
je 0xfab1
movl 0x8(%rbx), %eax
imull %r14d, %eax
addl 0x10(%rsp), %eax
imull 0x10(%rbx), %eax
cltq
movq 0x18(%rbx), %rcx
addq %rsi, %rcx
movb $-0x1, (%rax,%rcx)
incq %rsi
cmpq %r10, %rsi
jne 0xf900
incq 0x10(%rsp)
movq 0x88(%rsp), %rcx
incq %rcx
addl %r10d, 0x18(%rsp)
movq %rcx, 0x88(%rsp)
cmpq 0xb0(%rsp), %rcx
jne 0xf8d5
incq %r14
incl (%rsp)
movl 0x4(%rsp), %eax
addl 0xc(%rsp), %eax
movl %eax, 0x4(%rsp)
cmpq 0xa0(%rsp), %r14
jne 0xf893
movl 0xc(%rbx), %eax
xorl %r8d, %r8d
cmpl $0x3, %eax
jl 0xfb7b
decl %eax
movl 0x8(%rbx), %ecx
movl 0x10(%rbx), %edx
leal -0x1(%rcx), %esi
movq 0x18(%rbx), %rdi
leal 0x1(%rcx), %r9d
imull %edx, %r9d
movl %ecx, %r10d
imull %edx, %r10d
xorl %r8d, %r8d
movl $0x1, %r11d
cmpl $0x3, %ecx
jl 0xfb70
movl $0x1, %r14d
movl %r9d, %ebp
testl %edx, %edx
jle 0xfb66
movslq %ebp, %r15
addq %rdi, %r15
xorl %r12d, %r12d
cmpb $0x1, (%r15,%r12)
adcl $0x0, %r8d
incq %r12
cmpq %r12, %rdx
jne 0xfb55
incq %r14
addl %edx, %ebp
cmpq %rsi, %r14
jne 0xfb48
incq %r11
addl %r10d, %r9d
cmpq %rax, %r11
jne 0xfb3a
cmpl 0x98(%rsp), %r8d
movl %r8d, %r12d
movq 0x28(%rsp), %r10
jne 0xf5bf
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0xfbad
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2190
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xfbc5
jmp 0xfbc5
jmp 0xfbc5
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0xfbe4
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x22b0