// File: /workspace/github_cpp_makefile_star2_O3/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 $0xe8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl 0x8(%rdi), %ecx
movl 0xc(%rdi), %edx
leaq 0x3527(%rip), %rsi      # 0x12253
leaq 0xc8(%rsp), %rdi
callq 0x4c56
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl 0xc(%r14), %ecx
testl %ecx, %ecx
jle 0xedb8
movl 0x8(%r14), %edx
xorl %eax, %eax
testl %edx, %edx
jle 0xedb2
movl 0x10(%r14), %esi
xorl %ecx, %ecx
testl %esi, %esi
jle 0xeda8
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 0xed6b
movl 0x8(%r14), %edx
incl %ecx
cmpl %edx, %ecx
jl 0xed65
movl 0xc(%r14), %ecx
incl %eax
cmpl %ecx, %eax
jl 0xed5b
movq %rbx, 0x8(%rsp)
movl 0xc(%rbx), %eax
xorl %r12d, %r12d
cmpl $0x3, %eax
jl 0xee38
leal -0x1(%rax), %ecx
movq 0x8(%rsp), %r8
movl 0x8(%r8), %edx
movl 0x10(%r8), %esi
leal -0x1(%rdx), %edi
movq 0x18(%r8), %r8
leal 0x1(%rdx), %r9d
imull %esi, %r9d
movl %edx, %r10d
imull %esi, %r10d
xorl %r12d, %r12d
movl $0x1, %r11d
cmpl $0x3, %edx
jl 0xee2d
movl $0x1, %r14d
movl %r9d, %ebp
testl %esi, %esi
jle 0xee23
movslq %ebp, %rbx
addq %r8, %rbx
xorl %r15d, %r15d
cmpb $0x1, (%rbx,%r15)
adcl $0x0, %r12d
incq %r15
cmpq %r15, %rsi
jne 0xee12
incq %r14
addl %esi, %ebp
cmpq %rdi, %r14
jne 0xee05
incq %r11
addl %r10d, %r9d
cmpq %rcx, %r11
jne 0xedf7
movq 0xe0(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movl 0xd0(%rsp), %ecx
movl 0xd4(%rsp), %edx
movl 0xd8(%rsp), %ebp
movq %rdx, 0xa0(%rsp)
decl %edx
movq %rdx, 0xb0(%rsp)
leal -0x2(%rcx), %edx
movq %rdx, 0xc0(%rsp)
movl %ebp, %edx
movq %rcx, 0x78(%rsp)
imull %ecx, %edx
movl %edx, 0x1c(%rsp)
movq %r12, 0xa8(%rsp)
testl %eax, %eax
jle 0xf166
movq 0x8(%rsp), %rbx
movl 0x8(%rbx), %esi
xorl %ecx, %ecx
xorl %edx, %edx
testl %esi, %esi
jle 0xeee1
movl 0x10(%rbx), %r8d
movl %ecx, %eax
xorl %edi, %edi
testl %r8d, %r8d
jle 0xeed1
movslq %eax, %rsi
addq 0x30(%rsp), %rsi
xorl %r9d, %r9d
movb $-0x1, (%rsi,%r9)
incq %r9
movslq 0x10(%rbx), %r8
cmpq %r8, %r9
jl 0xeebd
movl 0x8(%rbx), %esi
incq %rdi
movslq %esi, %r9
addl %ebp, %eax
cmpq %r9, %rdi
jl 0xeead
movl 0xc(%rbx), %eax
incl %edx
addl 0x1c(%rsp), %ecx
cmpl %eax, %edx
jl 0xeea1
cmpl $0x3, %eax
jl 0xf16b
movl 0x8(%rbx), %ecx
movl $0x1, %r14d
movl $0x3, 0x4(%rsp)
cmpl $0x3, %ecx
jl 0xf151
leal -0x1(%r14), %eax
movl %eax, 0x18(%rsp)
movq 0x78(%rsp), %rax
imull %r14d, %eax
movq %rax, 0x20(%rsp)
movl 0x10(%rbx), %eax
movl $0x1, %esi
xorl %r15d, %r15d
testl %eax, %eax
jle 0xf139
movq 0x20(%rsp), %rcx
addl %esi, %ecx
imull %ebp, %ecx
movslq %ecx, %rcx
addq 0x30(%rsp), %rcx
movq %rcx, 0x80(%rsp)
xorl %edx, %edx
movq %rsi, 0x28(%rsp)
movl 0x8(%rbx), %ecx
imull %r14d, %ecx
addl %esi, %ecx
imull %eax, %ecx
movslq %ecx, %rax
movq 0x18(%rbx), %rcx
addq %rdx, %rcx
cmpb $0x0, (%rax,%rcx)
jne 0xf126
movq %rdx, 0x88(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x38(%rsp), %rax
movb $0x0, (%rax)
movl 0x18(%rsp), %r12d
xorl %r13d, %r13d
cmpl %r14d, %r12d
sete %al
cmpq $0x1, %r13
sete %cl
testb %cl, %al
jne 0xefe0
movq 0x18(%rbx), %rax
movl 0x8(%rbx), %ecx
imull %r12d, %ecx
addl %r15d, %ecx
addl %r13d, %ecx
imull 0x10(%rbx), %ecx
movslq %ecx, %rcx
cmpb $0x0, (%rax,%rcx)
leaq 0x2a9c(%rip), %rsi      # 0x11a69
je 0xefd6
leaq 0x2ddd(%rip), %rsi      # 0x11db3
leaq 0x38(%rsp), %rdi
callq 0x22e0
incq %r13
cmpl $0x3, %r13d
jne 0xef99
incl %r12d
cmpl 0x4(%rsp), %r12d
jne 0xef96
xorl %r13d, %r13d
movq %r13, %rax
shlq $0x5, %rax
leaq 0x68(%rsp), %rcx
movq %rcx, 0x58(%rsp)
leaq 0x7972(%rip), %rcx      # 0x16980
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %r12
movq %r12, %rdi
callq 0x10b98
movq %r12, %rdi
movl $0x44, %esi
xorl %edx, %edx
callq 0x21e0
cmpq $-0x1, %rax
je 0xf071
movq 0x58(%rsp), %r12
xorl %eax, %eax
movq 0x38(%rsp), %rcx
xorl %edx, %edx
movb (%r12,%rax), %sil
cmpb $0x44, %sil
je 0xf063
movl $0x1, %edx
cmpb %sil, (%rcx,%rax)
jne 0xf0ec
incq %rax
cmpq $0x8, %rax
jne 0xf04a
cmpl $0x1, %edx
jmp 0xf0b7
movq 0x40(%rsp), %rax
movq 0x58(%rsp), %r12
movq 0x60(%rsp), %rbx
cmpq %rax, %rbx
movq %rax, %rdx
cmovbq %rbx, %rdx
testq %rdx, %rdx
je 0xf0b4
movq 0x38(%rsp), %rdi
movq %r12, %rsi
movq %rax, 0x90(%rsp)
callq 0x2190
testl %eax, %eax
movq 0x90(%rsp), %rax
je 0xf0b4
xorl %ebx, %ebx
jmp 0xf0ba
cmpq %rbx, %rax
sete %bl
leaq 0x68(%rsp), %rax
cmpq %rax, %r12
je 0xf0d4
movq 0x68(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x2180
testb %bl, %bl
movq 0x8(%rsp), %rbx
jne 0xf10c
incq %r13
cmpq $0x2e, %r13
jne 0xeff6
jmp 0xf108
leaq 0x68(%rsp), %rax
cmpq %rax, %r12
je 0xf0dd
movq 0x68(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x2180
jmp 0xf0dd
movb $-0x1, %al
jmp 0xf10e
xorl %eax, %eax
movq 0x80(%rsp), %rcx
movq 0x88(%rsp), %rdx
movb %al, (%rcx,%rdx)
movq 0x28(%rsp), %rsi
incq %rdx
movslq 0x10(%rbx), %rax
cmpq %rax, %rdx
jl 0xef58
movl 0x8(%rbx), %ecx
incq %rsi
leal -0x1(%rcx), %edx
movslq %edx, %rdx
incq %r15
cmpq %rdx, %rsi
jl 0xef2f
movl 0xc(%rbx), %eax
incl %r14d
leal -0x1(%rax), %edx
incl 0x4(%rsp)
cmpl %edx, %r14d
jl 0xef05
jmp 0xf16b
movq 0x8(%rsp), %rbx
cmpl $0x3, 0xa0(%rsp)
jl 0xf405
movl $0x3, 0x4(%rsp)
movl $0x1, %r15d
movl $0x0, 0x14(%rsp)
cmpl $0x3, 0x78(%rsp)
jl 0xf3e1
movq %r15, %rax
imulq 0x78(%rsp), %rax
movq %rax, 0xb8(%rsp)
leal -0x1(%r15), %eax
movl %eax, 0x18(%rsp)
movl $0x1, %eax
movq %rax, 0x20(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x28(%rsp)
movq $0x0, 0x98(%rsp)
testl %ebp, %ebp
jle 0xf3b7
movq 0xb8(%rsp), %rax
movq 0x20(%rsp), %rcx
addl %ecx, %eax
imull %ebp, %eax
movslq %eax, %rdx
addq 0x30(%rsp), %rdx
xorl %esi, %esi
movq %rdx, 0x80(%rsp)
cmpb $0x0, (%rdx,%rsi)
jne 0xf3ab
movq %rsi, 0x88(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x38(%rsp), %rax
movb $0x0, (%rax)
movl 0x28(%rsp), %ebx
movl 0x18(%rsp), %eax
movl %eax, %r13d
movl %r13d, %r13d
movq $-0x1, %r14
movl %ebx, %r12d
movq %r15, %rax
xorq %r13, %rax
orq %r14, %rax
je 0xf269
movslq %r12d, %rax
movq 0x30(%rsp), %rcx
cmpb $0x0, (%rcx,%rax)
leaq 0x2815(%rip), %rsi      # 0x11a6b
je 0xf25f
leaq 0x2b54(%rip), %rsi      # 0x11db3
leaq 0x38(%rsp), %rdi
callq 0x22e0
incq %r14
addl %ebp, %r12d
cmpq $0x2, %r14
jne 0xf238
incl %r13d
addl 0x1c(%rsp), %ebx
cmpl 0x4(%rsp), %r13d
jne 0xf22b
xorl %r13d, %r13d
movq 0x8(%rsp), %rbx
movq %r13, %rax
shlq $0x5, %rax
leaq 0x68(%rsp), %rcx
movq %rcx, 0x58(%rsp)
leaq 0x819d(%rip), %rcx      # 0x17440
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %r14
movq %r14, %rdi
callq 0x10b98
movq %r14, %rdi
movl $0x44, %esi
xorl %edx, %edx
callq 0x21e0
cmpq $-0x1, %rax
je 0xf2f9
movq 0x58(%rsp), %r12
movq 0x38(%rsp), %rax
xorl %ecx, %ecx
movb (%r12,%rcx), %dl
cmpb $0x44, %dl
je 0xf2eb
cmpb %dl, (%rax,%rcx)
jne 0xf349
incq %rcx
cmpq $0x8, %rcx
jne 0xf2dd
xorl %r14d, %r14d
jmp 0xf34c
movq 0x40(%rsp), %rax
movq 0x58(%rsp), %r12
movq 0x60(%rsp), %rbx
cmpq %rax, %rbx
movq %rax, %rdx
cmovbq %rbx, %rdx
testq %rdx, %rdx
je 0xf33b
movq 0x38(%rsp), %rdi
movq %r12, %rsi
movq %rax, 0x90(%rsp)
callq 0x2190
movb $0x1, %r14b
testl %eax, %eax
movq 0x90(%rsp), %rax
jne 0xf342
cmpq %rbx, %rax
setne %r14b
movq 0x8(%rsp), %rbx
jmp 0xf34c
movb $0x1, %r14b
leaq 0x68(%rsp), %rax
cmpq %rax, %r12
je 0xf366
movq 0x68(%rsp), %rsi
incq %rsi
movq %r12, %rdi
callq 0x2180
cmpq $0x30, %r13
leaq 0x1(%r13), %r13
setb %al
testb %al, %r14b
jne 0xf28b
testb %r14b, %r14b
movq 0x80(%rsp), %rdx
movq 0x88(%rsp), %rsi
je 0xf3ab
movl 0x8(%rbx), %eax
imull %r15d, %eax
addl 0x20(%rsp), %eax
imull 0x10(%rbx), %eax
cltq
movq 0x18(%rbx), %rcx
addq %rsi, %rcx
movb $-0x1, (%rax,%rcx)
incq %rsi
cmpq %rbp, %rsi
jne 0xf1fd
incq 0x20(%rsp)
movq 0x98(%rsp), %rcx
incq %rcx
addl %ebp, 0x28(%rsp)
movq %rcx, 0x98(%rsp)
cmpq 0xc0(%rsp), %rcx
jne 0xf1d1
incq %r15
incl 0x4(%rsp)
movl 0x14(%rsp), %eax
addl 0x1c(%rsp), %eax
movl %eax, 0x14(%rsp)
cmpq 0xb0(%rsp), %r15
jne 0xf18f
movl 0xc(%rbx), %eax
xorl %r9d, %r9d
cmpl $0x3, %eax
jl 0xf47e
leal -0x1(%rax), %ecx
movq 0x8(%rsp), %r8
movl 0x8(%r8), %edx
movl 0x10(%r8), %esi
leal -0x1(%rdx), %edi
movq 0x18(%r8), %r8
leal 0x1(%rdx), %r10d
imull %esi, %r10d
movl %edx, %r11d
imull %esi, %r11d
xorl %r9d, %r9d
movl $0x1, %r14d
cmpl $0x3, %edx
jl 0xf473
movl $0x1, %r15d
movl %r10d, %r12d
testl %esi, %esi
jle 0xf468
movslq %r12d, %rbx
addq %r8, %rbx
xorl %r13d, %r13d
cmpb $0x1, (%rbx,%r13)
adcl $0x0, %r9d
incq %r13
cmpq %r13, %rsi
jne 0xf457
incq %r15
addl %esi, %r12d
cmpq %rdi, %r15
jne 0xf44a
incq %r14
addl %r11d, %r10d
cmpq %rcx, %r14
jne 0xf43c
cmpl 0xa8(%rsp), %r9d
movl %r9d, %r12d
jne 0xee85
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0xf4ab
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x2180
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xf4c3
jmp 0xf4c3
jmp 0xf4c3
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0xf4e2
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x2180
movq %rbx, %rdi
callq 0x22a0