// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rsp, %rdi
callq 0xa3da
leaq 0x20(%rbx), %rsi
leaq 0x20(%rsp), %rdi
callq 0xa3da
testb %bpl, %bpl
je 0x8cfd
leaq 0x58(%r15), %rsi
leaq 0x70(%rsp), %rdi
callq 0xa3da
movq 0x10(%rsp), %rax
leaq 0x70(%rsp), %r14
movapd (%r14), %xmm0
movq 0x10(%r14), %rcx
movq %rcx, 0x10(%rsp)
movapd (%rsp), %xmm1
movapd %xmm0, (%rsp)
leaq 0xd0(%rsp), %rdi
movapd %xmm1, (%rdi)
movq %rax, 0x10(%rdi)
xorpd %xmm0, %xmm0
movapd %xmm0, (%r14)
movq $0x0, 0x10(%r14)
callq 0xa038
movq %r14, %rdi
callq 0xa038
leaq 0x58(%rbx), %rsi
leaq 0x70(%rsp), %rdi
callq 0xa3da
movq 0x30(%rsp), %rax
leaq 0x70(%rsp), %r14
movapd (%r14), %xmm0
movq 0x10(%r14), %rcx
movq %rcx, 0x30(%rsp)
movapd 0x20(%rsp), %xmm1
movapd %xmm0, 0x20(%rsp)
leaq 0xd0(%rsp), %rdi
movapd %xmm1, (%rdi)
movq %rax, 0x10(%rdi)
xorpd %xmm0, %xmm0
movapd %xmm0, (%r14)
movq $0x0, 0x10(%r14)
callq 0xa038
movq %r14, %rdi
callq 0xa038
movq %r15, 0x50(%rsp)
movl %ebp, 0x44(%rsp)
movq %rbx, 0x58(%rsp)
movq (%rsp), %rax
cmpq %rax, 0x8(%rsp)
je 0x8f23
xorl %ebp, %ebp
movabsq $-0x3333333333333333, %r15 # imm = 0xCCCCCCCCCCCCCCCD
leaq 0xd0(%rsp), %r14
xorl %r12d, %r12d
movq 0x20(%rsp), %rcx
cmpq %rcx, 0x28(%rsp)
je 0x8f05
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movsd 0x652cf(%rip), %xmm1    # 0x6e020
movapd %xmm1, %xmm2
movapd %xmm1, %xmm3
leaq (%rbx,%rbx,4), %rdx
movq 0x10(%rcx,%rdx,8), %rax
movq 0x18(%rcx,%rdx,8), %rcx
subq %rax, %rcx
je 0x8db2
sarq $0x2, %rcx
movq (%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 0x8d92
jmp 0x8db6
xorpd %xmm0, %xmm0
ucomisd 0x65272(%rip), %xmm0    # 0x6e030
jb 0x8dc6
sqrtsd %xmm0, %xmm0
jmp 0x8de3
movsd %xmm2, 0x68(%rsp)
movsd %xmm3, 0x60(%rsp)
callq 0x4430
movsd 0x60(%rsp), %xmm3
movsd 0x68(%rsp), %xmm2
ucomisd %xmm0, %xmm3
jbe 0x8df6
movapd %xmm0, %xmm1
movapd %xmm3, %xmm2
movl %ebx, %r13d
jmp 0x8e04
movapd %xmm3, %xmm1
ucomisd %xmm0, %xmm2
jbe 0x8e04
movapd %xmm0, %xmm2
incq %rbx
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rax
subq %rcx, %rax
sarq $0x3, %rax
imulq %r15, %rax
cmpq %rbx, %rax
ja 0x8d55
movq (%rsp), %rax
divsd %xmm2, %xmm1
movsd 0x651f3(%rip), %xmm0    # 0x6e028
ucomisd %xmm1, %xmm0
jbe 0x8f05
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 0xa53c
movslq %r13d, %rax
movq 0x20(%rsp), %rcx
leaq (%rax,%rax,4), %rax
leaq (%rcx,%rax,8), %rsi
addq $0x10, %rsi
movupd -0x10(%rsi), %xmm0
movapd %xmm0, 0xa0(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0xa53c
movq %r14, %rdi
leaq 0x70(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
callq 0xa884
movq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0xa5d8
movq 0x108(%rsp), %rdi
testq %rdi, %rdi
je 0x8ec8
callq 0x44f0
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x8eda
callq 0x44f0
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x8eec
callq 0x44f0
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x8efe
callq 0x44f0
incl %r12d
movq (%rsp), %rax
incq %rbp
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
imulq %r15, %rcx
cmpq %rbp, %rcx
ja 0x8d31
jmp 0x8f26
xorl %r12d, %r12d
movzbl 0x44(%rsp), %ecx
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0xa0a2
leaq 0x20(%rsp), %rdi
callq 0xa038
movq %rsp, %rdi
callq 0xa038
movl %r12d, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8f76
jmp 0x8f76
jmp 0x8f76
movq %rax, %rbx
jmp 0x8fbe
movq %rax, %rbx
jmp 0x8fa2
movq %rax, %rbx
jmp 0x8fb4
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0xa07e
jmp 0x8f90
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x8fa2
callq 0x44f0
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x8fb4
callq 0x44f0
leaq 0x20(%rsp), %rdi
callq 0xa038
movq %rsp, %rdi
callq 0xa038
movq %rbx, %rdi
callq 0x4960