// File: /workspace/github_cpp_makefile_star2_O1/rishabh-battulwar[P]imageprocessing/./include/ImageProc.h
// Function: ImageProc::thin(Image&, Image&)
void ImageProc::thin(Image &img_binary, Image &img_thinned)
{
	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_thinned.setvalue(i,j,k,img_binary.getvalue(i,j,k));

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

		

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

			for(int i = 0+1; i < img_thinned.rows-1; i++)
				for(int j = 0+1; j < img_thinned.cols-1; j++)
					for(int k = 0; k < img_thinned.channels; k++)
					{
						if(img_thinned.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_thinned.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 < THIN_FIRST_PATTERNS; t++)
							{
								std::string testpattern(thin_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 < SHRINK_THIN_SECOND_PATTERNS; t++)
								{
									std::string testpattern(shrink__thin_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_thinned.setvalue(i,j,k,1*255);
								}
								else
									img_thinned.setvalue(i,j,k,img_thinned.getvalue(i,j,k));
							}


						}

			pixel_count = 0;
			for(int i = 0+1; i < img_thinned.rows-1; i++)
				for(int j = 0+1; j < img_thinned.cols-1; j++)
					for(int k = 0; k < img_thinned.channels; k++)
						if(img_thinned.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 0x350f(%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 0xed5e
xorl %eax, %eax
cmpl $0x0, 0x8(%r14)
jle 0xed56
xorl %ecx, %ecx
movl 0x10(%r14), %esi
testl %esi, %esi
jle 0xed4e
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 0xed15
incl %ecx
cmpl 0x8(%r14), %ecx
jl 0xed0b
incl %eax
cmpl 0xc(%r14), %eax
jl 0xed02
movl 0xc(%rbx), %eax
xorl %r12d, %r12d
cmpl $0x3, %eax
jl 0xedd1
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 0xedc6
movl $0x1, %r11d
movl %r8d, %ebp
testl %edx, %edx
jle 0xedbc
movslq %ebp, %r14
addq %rdi, %r14
xorl %r15d, %r15d
cmpb $0x1, (%r14,%r15)
adcl $0x0, %r12d
incq %r15
cmpq %r15, %rdx
jne 0xedab
incq %r11
addl %edx, %ebp
cmpq %rsi, %r11
jne 0xed9e
incq %r10
addl %r9d, %r8d
cmpq %rax, %r10
jne 0xed90
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 0xee73
xorl %eax, %eax
xorl %ecx, %ecx
cmpl $0x0, 0x8(%rbx)
jle 0xee68
movl %eax, %edx
xorl %esi, %esi
cmpl $0x0, 0x10(%rbx)
jle 0xee59
movslq %edx, %rdi
addq 0x20(%rsp), %rdi
xorl %r8d, %r8d
movb $-0x1, (%rdi,%r8)
incq %r8
movslq 0x10(%rbx), %r9
cmpq %r9, %r8
jl 0xee48
incq %rsi
movslq 0x8(%rbx), %rdi
addl %r10d, %edx
cmpq %rdi, %rsi
jl 0xee37
incl %ecx
addl 0xc(%rsp), %eax
cmpl 0xc(%rbx), %ecx
jl 0xee2d
movq %r12, 0x98(%rsp)
cmpl $0x3, 0xc(%rbx)
leaq 0x40(%rsp), %r12
jl 0xf0d4
movl $0x1, %r14d
movl $0x3, (%rsp)
cmpl $0x3, 0x8(%rbx)
jl 0xf0c0
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 0xf0aa
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 0xf09a
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 0xef62
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 0x2aae(%rip), %rsi      # 0x119fd
je 0xef58
leaq 0x2def(%rip), %rsi      # 0x11d47
leaq 0x50(%rsp), %rdi
callq 0x22f0
incq %r15
cmpl $0x3, %r15d
jne 0xef1c
incl %ebp
cmpl (%rsp), %ebp
jne 0xef19
xorl %ebp, %ebp
leaq 0x30(%rsp), %r15
movq %rbp, %rax
shlq $0x5, %rax
movq %r12, 0x30(%rsp)
leaq 0x79f4(%rip), %rcx      # 0x16980
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 0xefea
movq 0x30(%rsp), %rdi
xorl %eax, %eax
movq 0x50(%rsp), %rcx
xorl %edx, %edx
movb (%rdi,%rax), %sil
cmpb $0x44, %sil
je 0xefd8
movl $0x1, %edx
cmpb %sil, (%rcx,%rax)
jne 0xf019
incq %rax
cmpq $0x8, %rax
jne 0xefc3
cmpl $0x1, %edx
sete %r15b
jmp 0xf03e
movq 0x58(%rsp), %r15
movq 0x38(%rsp), %r12
cmpq %r15, %r12
movq %r15, %rdx
cmovbq %r12, %rdx
testq %rdx, %rdx
je 0xf02d
movq 0x30(%rsp), %rsi
movq 0x50(%rsp), %rdi
callq 0x21a0
testl %eax, %eax
sete %al
jmp 0xf02f
cmpq %r12, %rdi
je 0xf05f
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2190
jmp 0xf05f
movb $0x1, %al
cmpq %r12, %r15
sete %r15b
andb %al, %r15b
leaq 0x40(%rsp), %r12
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0xf055
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2190
testb %r15b, %r15b
leaq 0x30(%rsp), %r15
jne 0xf070
incq %rbp
cmpq $0x2e, %rbp
jne 0xef79
movb $-0x1, %al
jmp 0xf072
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 0xeedc
incq %rdx
movslq 0x8(%rbx), %rax
decq %rax
incq %r13
cmpq %rax, %rdx
jl 0xeebf
incl %r14d
movl 0xc(%rbx), %eax
decl %eax
incl (%rsp)
cmpl %eax, %r14d
jl 0xee97
cmpl $0x3, 0x90(%rsp)
jl 0xf36c
movl $0x3, (%rsp)
movl $0x1, %r14d
movl $0x0, 0x4(%rsp)
cmpl $0x3, 0x70(%rsp)
jl 0xf34c
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 0xf321
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 0xf315
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 0xf1d1
movslq %ebp, %rax
movq 0x20(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
leaq 0x2841(%rip), %rsi      # 0x119ff
je 0xf1c7
leaq 0x2b80(%rip), %rsi      # 0x11d47
leaq 0x50(%rsp), %rdi
callq 0x22f0
incq %r15
addl 0x28(%rsp), %ebp
cmpq $0x2, %r15
jne 0xf1a0
incl %r13d
addl 0xc(%rsp), %r12d
cmpl (%rsp), %r13d
jne 0xf193
xorl %ebp, %ebp
leaq 0x40(%rsp), %r12
leaq 0x8246(%rip), %r13      # 0x17440
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 0xf266
movq 0x30(%rsp), %rax
movq $-0x1, %rcx
movq 0x50(%rsp), %rdx
movb 0x1(%rax,%rcx), %sil
cmpb $0x44, %sil
je 0xf257
cmpb %sil, 0x1(%rdx,%rcx)
jne 0xf296
incq %rcx
cmpq $0x7, %rcx
jne 0xf245
setb %r15b
jmp 0xf2b7
movq 0x58(%rsp), %r12
movq 0x38(%rsp), %r13
cmpq %r12, %r13
movq %r12, %rdx
cmovbq %r13, %rdx
testq %rdx, %rdx
je 0xf29b
movq 0x30(%rsp), %rsi
movq 0x50(%rsp), %rdi
callq 0x21a0
testl %eax, %eax
sete %r15b
jmp 0xf29e
movb $0x1, %r15b
jmp 0xf2b7
movb $0x1, %r15b
cmpq %r13, %r12
setne %al
xorb $0x1, %r15b
orb %al, %r15b
leaq 0x40(%rsp), %r12
leaq 0x8189(%rip), %r13      # 0x17440
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0xf2ce
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x2190
cmpq $0x30, %rbp
leaq 0x1(%rbp), %rbp
setb %al
testb %al, %r15b
jne 0xf1fa
testb %r15b, %r15b
movq 0x28(%rsp), %r10
movq 0x78(%rsp), %rdx
movq 0x80(%rsp), %rsi
je 0xf315
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 0xf164
incq 0x10(%rsp)
movq 0x88(%rsp), %rcx
incq %rcx
addl %r10d, 0x18(%rsp)
movq %rcx, 0x88(%rsp)
cmpq 0xb0(%rsp), %rcx
jne 0xf139
incq %r14
incl (%rsp)
movl 0x4(%rsp), %eax
addl 0xc(%rsp), %eax
movl %eax, 0x4(%rsp)
cmpq 0xa0(%rsp), %r14
jne 0xf0f7
movl 0xc(%rbx), %eax
xorl %r8d, %r8d
cmpl $0x3, %eax
jl 0xf3df
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 0xf3d4
movl $0x1, %r14d
movl %r9d, %ebp
testl %edx, %edx
jle 0xf3ca
movslq %ebp, %r15
addq %rdi, %r15
xorl %r12d, %r12d
cmpb $0x1, (%r15,%r12)
adcl $0x0, %r8d
incq %r12
cmpq %r12, %rdx
jne 0xf3b9
incq %r14
addl %edx, %ebp
cmpq %rsi, %r14
jne 0xf3ac
incq %r11
addl %r10d, %r9d
cmpq %rax, %r11
jne 0xf39e
cmpl 0x98(%rsp), %r8d
movl %r8d, %r12d
movq 0x28(%rsp), %r10
jne 0xee23
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0xf411
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 0xf429
jmp 0xf429
jmp 0xf429
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0xf448
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x22b0