// File: /workspace/github_cpp_makefile_star2_O1/manikandan5[P]Image-Matching-Stitching-Warping/./SiftHelpers.h
// Function: Image::MatchSIFT(Image const&, Image const&, std::vector<std::pair<SiftDescriptor, SiftDescriptor>, std::allocator<std::pair<SiftDescriptor, SiftDescriptor>>>&, bool)
static int MatchSIFT(const Image& queryImage,const Image& image,SiftDescriptorMap& mapping,bool reduced = false)
	{
		vector<SiftDescriptor> queryDescriptors = queryImage.getDescriptors();
		vector<SiftDescriptor> imageDescriptors = image.getDescriptors();
		
		if(reduced)
		{
			queryDescriptors = queryImage.getNewDescriptors();
			imageDescriptors = image.getNewDescriptors();
		}
		
		int count = 0;
			
		for(int i = 0;i<queryDescriptors.size();++i)
		{
			double min = DBL_MAX;
			double second_min = DBL_MAX;
			int descriptor = -1;
			for(int j =0;j<imageDescriptors.size();++j)
			{
				double sum = 0;
				//Euclidean Distance 
				for(int k = 0;k<imageDescriptors[j].descriptor.size();++k)
				{
					sum+= pow(queryDescriptors[i].descriptor[k] - imageDescriptors[j].descriptor[k],2);
				}
				
				sum = sqrt(sum);
				if(sum < min)
				{
					second_min = min;
					min = sum;
					descriptor = j;
				}
				else if(sum < second_min)
				{
					second_min = sum;
				}
			}
			
			//ratio of the Euclidean distances between the closest match and the second-closest match, and comparing to a threshold.
			if(min/second_min < 0.7)
			{
				count++;
			
				SiftDescriptor q = queryDescriptors[i];
				SiftDescriptor im = imageDescriptors[descriptor];
				mapping.push_back(make_pair(q,im));
			}
			
		}
		
		//cout<<"Count = "<<count<<endl;
		Image::DrawImage(queryImage,image,mapping,reduced);
		
		return count;
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movl %ecx, %ebp
movq %rdx, 0x48(%rsp)
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x20(%rdi), %rsi
leaq 0x20(%rsp), %rdi
callq 0xa436
leaq 0x20(%rbx), %rsi
movq %rsp, %rdi
callq 0xa436
testb %bpl, %bpl
je 0x8d65
leaq 0x58(%r15), %rsi
leaq 0x70(%rsp), %rdi
callq 0xa436
xorps %xmm0, %xmm0
leaq 0xd0(%rsp), %rdi
movaps %xmm0, (%rdi)
movapd 0x20(%rsp), %xmm0
movq 0x30(%rsp), %rax
leaq 0x70(%rsp), %r14
movaps (%r14), %xmm1
movaps %xmm1, 0x20(%rsp)
movq 0x10(%r14), %rcx
movq %rcx, 0x30(%rsp)
movq %rax, 0x10(%rdi)
movapd (%rdi), %xmm1
movapd %xmm0, (%rdi)
movapd %xmm1, (%r14)
movq $0x0, 0x10(%r14)
callq 0xa09a
movq %r14, %rdi
callq 0xa09a
leaq 0x58(%rbx), %rsi
leaq 0x70(%rsp), %rdi
callq 0xa436
leaq 0xd0(%rsp), %rdi
xorpd %xmm0, %xmm0
movapd %xmm0, (%rdi)
movapd (%rsp), %xmm0
movq 0x10(%rsp), %rax
leaq 0x70(%rsp), %r14
movaps (%r14), %xmm1
movaps %xmm1, (%rsp)
movq 0x10(%r14), %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x10(%rdi)
movapd (%rdi), %xmm1
movapd %xmm0, (%rdi)
movapd %xmm1, (%r14)
movq $0x0, 0x10(%r14)
callq 0xa09a
movq %r14, %rdi
callq 0xa09a
movq %r15, 0x50(%rsp)
movl %ebp, 0x44(%rsp)
movq %rbx, 0x58(%rsp)
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0x8f9a
xorl %ebp, %ebp
leaq 0xd0(%rsp), %r14
movabsq $-0x3333333333333333, %r15 # imm = 0xCCCCCCCCCCCCCCCD
xorl %r12d, %r12d
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
je 0x8ea6
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movsd 0x6826f(%rip), %xmm1    # 0x71028
movapd %xmm1, %xmm2
movapd %xmm1, %xmm3
movq (%rsp), %rcx
leaq (%rbx,%rbx,4), %rdx
movq 0x10(%rcx,%rdx,8), %rax
movq 0x18(%rcx,%rdx,8), %rcx
subq %rax, %rcx
je 0x8e1f
sarq $0x2, %rcx
movq 0x20(%rsp), %rdx
leaq (,%rbp,4), %rsi
addq %rbp, %rsi
movq 0x10(%rdx,%rsi,8), %rdx
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorpd %xmm0, %xmm0
xorl %esi, %esi
movss (%rdx,%rsi,4), %xmm1
subss (%rax,%rsi,4), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rsi
cmpq %rsi, %rcx
jne 0x8dff
jmp 0x8e23
xorpd %xmm0, %xmm0
ucomisd 0x6820d(%rip), %xmm0    # 0x71038
jb 0x8e33
sqrtsd %xmm0, %xmm0
jmp 0x8e50
movsd %xmm2, 0x68(%rsp)
movsd %xmm3, 0x60(%rsp)
callq 0x4420
movsd 0x60(%rsp), %xmm3
movsd 0x68(%rsp), %xmm2
ucomisd %xmm0, %xmm3
jbe 0x8e63
movapd %xmm0, %xmm1
movapd %xmm3, %xmm2
movl %ebx, %r13d
jmp 0x8e71
movapd %xmm3, %xmm1
ucomisd %xmm0, %xmm2
jbe 0x8e71
movapd %xmm0, %xmm2
movq 0x8(%rsp), %rax
subq (%rsp), %rax
incq %rbx
sarq $0x3, %rax
imulq %r15, %rax
cmpq %rbx, %rax
ja 0x8dbd
divsd %xmm2, %xmm1
movsd 0x68196(%rip), %xmm0    # 0x71030
ucomisd %xmm1, %xmm0
seta %al
movslq %r13d, %rbx
jmp 0x8eaf
movq $-0x1, %rbx
xorl %eax, %eax
testb %al, %al
je 0x8f7a
movq 0x20(%rsp), %rax
leaq (,%rbp,4), %rcx
addq %rbp, %rcx
leaq 0x10(%rax,%rcx,8), %rsi
movups -0x10(%rsi), %xmm0
movaps %xmm0, 0x70(%rsp)
leaq 0x80(%rsp), %rdi
callq 0xa598
movq (%rsp), %rax
leaq (%rbx,%rbx,4), %rcx
leaq (%rax,%rcx,8), %rsi
addq $0x10, %rsi
movupd -0x10(%rsi), %xmm0
movapd %xmm0, 0xa0(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0xa598
movq %r14, %rdi
leaq 0x70(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
callq 0xa8e0
movq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0xa634
movq 0x108(%rsp), %rdi
testq %rdi, %rdi
je 0x8f41
callq 0x44e0
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x8f53
callq 0x44e0
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x8f65
callq 0x44e0
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x8f77
callq 0x44e0
incl %r12d
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
incq %rbp
sarq $0x3, %rax
imulq %r15, %rax
cmpq %rbp, %rax
ja 0x8d9a
jmp 0x8f9d
xorl %r12d, %r12d
movzbl 0x44(%rsp), %ecx
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0xa0fe
movq %rsp, %rdi
callq 0xa09a
leaq 0x20(%rsp), %rdi
callq 0xa09a
movl %r12d, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8fed
jmp 0x8fed
jmp 0x8fed
movq %rax, %rbx
jmp 0x9033
movq %rax, %rbx
jmp 0x9002
movq %rax, %rbx
jmp 0x902b
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0xa0da
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x9019
callq 0x44e0
jmp 0x9019
movq %rax, %rbx
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x902b
callq 0x44e0
movq %rsp, %rdi
callq 0xa09a
leaq 0x20(%rsp), %rdi
callq 0xa09a
movq %rbx, %rdi
callq 0x4950
nop