// File: manikandan5[P]Image-Matching-Stitching-Warping/./Homography.h
// Function: linearSystemSolver(std::vector<std::pair<SiftDescriptor, SiftDescriptor>, std::allocator<std::pair<SiftDescriptor, SiftDescriptor>>>&)
CImg<double> linearSystemSolver(SiftDescriptorMap& mapping)
{
	CImg<double> A(8,8);
	CImg<double> B(1,8);
	
	int i = 0;
	
	srand(time(NULL));
	// To pick different 4 points everytime
	random_shuffle(mapping.begin(),mapping.end());
	
	SiftDescriptorMap::iterator start = mapping.begin();
	SiftDescriptorMap::iterator end = mapping.end();
	//Fill A and B matrix from the class slide
	while(start != end)
	{
		A(0,2*i) = start->first.col;
		A(1,2*i) = start->first.row;
		A(2,2*i) = 1;
		A(3,2*i) = A(4,2*i) = A(5,2*i) = 0;
		A(6,2*i) = -1.0 * start->first.col * start->second.col;
		A(7,2*i) = -1.0 * start->first.row * start->second.col;
		
		A(3,2*i+1) = start->first.col;
		A(4,2*i+1) = start->first.row;
		A(5,2*i+1) = 1;
		A(0,2*i+1) = A(1,2*i+1) = A(2,2*i+1) = 0;
		A(6,2*i+1) = -1.0 * start->first.col * start->second.row;
		A(7,2*i+1) = -1.0 * start->first.row * start->second.row;
		
		B(0,2*i) = start->second.col;
		B(0,2*i+1) = start->second.row;
		
		if(++i == 4)//Stop for 4 ponts.
			break;
		++start;
	}
	
	//AX = B, estimate X
	CImg<double> X = B.solve(A);
	
	/*CImg<double>::iterator ansStart = X.begin();
	CImg<double>::iterator ansEnd = X.end();
	while(ansStart != ansEnd)
	{
		cout<<*ansStart<<" ";
		++ansStart;
	}
	cout<<endl;*/
	
	return X;
}

// Assembly:
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rdi, 0x128(%rsp)
movq %rdi, %rax
movq %rax, 0x130(%rsp)
movq %rdi, 0x1c0(%rsp)
movq %rsi, 0x1b8(%rsp)
leaq 0x198(%rsp), %rdi
movl $0x8, %edx
movl %edx, 0x13c(%rsp)
movl $0x1, %r8d
movl %r8d, 0x140(%rsp)
movl %edx, %esi
movl %r8d, %ecx
callq 0xa580
movl 0x13c(%rsp), %edx
movl 0x140(%rsp), %r8d
leaq 0x178(%rsp), %rdi
movl %r8d, %esi
movl %r8d, %ecx
callq 0xa580
jmp 0x666a
movl $0x0, 0x168(%rsp)
xorl %eax, %eax
movl %eax, %edi
callq 0x54c0
movl %eax, %edi
callq 0x5500
movq 0x1b8(%rsp), %rdi
callq 0xa980
movq %rax, 0x160(%rsp)
movq 0x1b8(%rsp), %rdi
callq 0xa9b0
movq %rax, 0x158(%rsp)
movq 0x160(%rsp), %rdi
movq 0x158(%rsp), %rsi
callq 0xa8a0
jmp 0x66c6
movq 0x1b8(%rsp), %rdi
callq 0xa980
movq %rax, 0x150(%rsp)
movq 0x1b8(%rsp), %rdi
callq 0xa9b0
movq %rax, 0x148(%rsp)
leaq 0x150(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0xa9e0
testb $0x1, %al
jne 0x670e
jmp 0x6d53
leaq 0x150(%rsp), %rdi
callq 0xaa20
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x118(%rsp)
movl 0x168(%rsp), %edx
addl %edx, %edx
leaq 0x198(%rsp), %rdi
xorl %esi, %esi
callq 0x9850
movq %rax, 0x120(%rsp)
jmp 0x674f
movq 0x120(%rsp), %rax
movsd 0x118(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x150(%rsp), %rdi
callq 0xaa20
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x108(%rsp)
movl 0x168(%rsp), %edx
addl %edx, %edx
leaq 0x198(%rsp), %rdi
movl $0x1, %esi
callq 0x9850
movq %rax, 0x110(%rsp)
jmp 0x67a7
movq 0x110(%rsp), %rax
movsd 0x108(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x168(%rsp), %edx
addl %edx, %edx
leaq 0x198(%rsp), %rdi
movl $0x2, %esi
callq 0x9850
movq %rax, 0x100(%rsp)
jmp 0x67e1
movq 0x100(%rsp), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax)
movl 0x168(%rsp), %edx
addl %edx, %edx
leaq 0x198(%rsp), %rdi
movl $0x5, %esi
callq 0x9850
movq %rax, 0xf8(%rsp)
jmp 0x681b
movq 0xf8(%rsp), %rax
movq $0x0, (%rax)
movl 0x168(%rsp), %edx
addl %edx, %edx
leaq 0x198(%rsp), %rdi
movl $0x4, %esi
callq 0x9850
movq %rax, 0xf0(%rsp)
jmp 0x684f
movq 0xf0(%rsp), %rax
movq $0x0, (%rax)
movl 0x168(%rsp), %edx
addl %edx, %edx
leaq 0x198(%rsp), %rdi
movl $0x3, %esi
callq 0x9850
movq %rax, 0xe8(%rsp)
jmp 0x6883
movq 0xe8(%rsp), %rax
movq $0x0, (%rax)
leaq 0x150(%rsp), %rdi
movq %rdi, 0xc8(%rsp)
callq 0xaa20
movq 0xc8(%rsp), %rdi
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movaps 0xc17f1(%rip), %xmm1    # 0xc80b0
pxor %xmm1, %xmm0
movsd %xmm0, 0xd0(%rsp)
callq 0xaa20
movsd 0xd0(%rsp), %xmm0
movss 0x2c(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0xd8(%rsp)
movl 0x168(%rsp), %edx
addl %edx, %edx
leaq 0x198(%rsp), %rdi
movl $0x6, %esi
callq 0x9850
movq %rax, 0xe0(%rsp)
jmp 0x6915
movq 0xe0(%rsp), %rax
movsd 0xd8(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x150(%rsp), %rdi
movq %rdi, 0xa8(%rsp)
callq 0xaa20
movq 0xa8(%rsp), %rdi
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movaps 0xc175a(%rip), %xmm1    # 0xc80b0
pxor %xmm1, %xmm0
movsd %xmm0, 0xb0(%rsp)
callq 0xaa20
movsd 0xb0(%rsp), %xmm0
movss 0x2c(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0xb8(%rsp)
movl 0x168(%rsp), %edx
addl %edx, %edx
leaq 0x198(%rsp), %rdi
movl $0x7, %esi
callq 0x9850
movq %rax, 0xc0(%rsp)
jmp 0x69ac
movq 0xc0(%rsp), %rax
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x150(%rsp), %rdi
callq 0xaa20
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x98(%rsp)
movl 0x168(%rsp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %edx
leaq 0x198(%rsp), %rdi
movl $0x3, %esi
callq 0x9850
movq %rax, 0xa0(%rsp)
jmp 0x6a09
movq 0xa0(%rsp), %rax
movsd 0x98(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x150(%rsp), %rdi
callq 0xaa20
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x88(%rsp)
movl 0x168(%rsp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %edx
leaq 0x198(%rsp), %rdi
movl $0x4, %esi
callq 0x9850
movq %rax, 0x90(%rsp)
jmp 0x6a65
movq 0x90(%rsp), %rax
movsd 0x88(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x168(%rsp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %edx
leaq 0x198(%rsp), %rdi
movl $0x5, %esi
callq 0x9850
movq %rax, 0x80(%rsp)
jmp 0x6aa3
movq 0x80(%rsp), %rax
movabsq $0x3ff0000000000000, %rcx # imm = 0x3FF0000000000000
movq %rcx, (%rax)
movl 0x168(%rsp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %edx
leaq 0x198(%rsp), %rdi
movl $0x2, %esi
callq 0x9850
movq %rax, 0x78(%rsp)
jmp 0x6ade
movq 0x78(%rsp), %rax
movq $0x0, (%rax)
movl 0x168(%rsp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %edx
leaq 0x198(%rsp), %rdi
movl $0x1, %esi
callq 0x9850
movq %rax, 0x70(%rsp)
jmp 0x6b10
movq 0x70(%rsp), %rax
movq $0x0, (%rax)
movl 0x168(%rsp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %edx
leaq 0x198(%rsp), %rdi
xorl %esi, %esi
callq 0x9850
movq %rax, 0x68(%rsp)
jmp 0x6b3f
movq 0x68(%rsp), %rax
movq $0x0, (%rax)
leaq 0x150(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0xaa20
movq 0x48(%rsp), %rdi
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movaps 0xc153e(%rip), %xmm1    # 0xc80b0
pxor %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
callq 0xaa20
movsd 0x50(%rsp), %xmm0
movss 0x28(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movl 0x168(%rsp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %edx
leaq 0x198(%rsp), %rdi
movl $0x6, %esi
callq 0x9850
movq %rax, 0x60(%rsp)
jmp 0x6bc0
movq 0x60(%rsp), %rax
movsd 0x58(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x150(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0xaa20
movq 0x28(%rsp), %rdi
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movaps 0xc14bb(%rip), %xmm1    # 0xc80b0
pxor %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
callq 0xaa20
movsd 0x30(%rsp), %xmm0
movss 0x28(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0x168(%rsp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %edx
leaq 0x198(%rsp), %rdi
movl $0x7, %esi
callq 0x9850
movq %rax, 0x40(%rsp)
jmp 0x6c43
movq 0x40(%rsp), %rax
movsd 0x38(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x150(%rsp), %rdi
callq 0xaa20
movss 0x2c(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0x168(%rsp), %edx
addl %edx, %edx
leaq 0x178(%rsp), %rdi
xorl %esi, %esi
callq 0x9850
movq %rax, 0x20(%rsp)
jmp 0x6c8d
movq 0x20(%rsp), %rax
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x150(%rsp), %rdi
callq 0xaa20
movss 0x28(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x168(%rsp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %edx
leaq 0x178(%rsp), %rdi
xorl %esi, %esi
callq 0x9850
movq %rax, 0x10(%rsp)
jmp 0x6cdb
movq 0x10(%rsp), %rax
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x168(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x168(%rsp)
cmpl $0x4, %eax
jne 0x6d41
jmp 0x6d53
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
jmp 0x6dd2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x178(%rsp), %rdi
callq 0xa540
jmp 0x6dd2
leaq 0x150(%rsp), %rdi
callq 0xaa30
jmp 0x66f0
movb $0x0, 0x147(%rsp)
leaq 0x178(%rsp), %rdi
leaq 0x198(%rsp), %rsi
callq 0xaa50
movq %rax, (%rsp)
jmp 0x6d76
movq (%rsp), %rsi
movq 0x128(%rsp), %rdi
callq 0xadb0
jmp 0x6d89
movb $0x1, 0x147(%rsp)
testb $0x1, 0x147(%rsp)
jne 0x6da8
movq 0x128(%rsp), %rdi
callq 0xa540
leaq 0x178(%rsp), %rdi
callq 0xa540
leaq 0x198(%rsp), %rdi
callq 0xa540
movq 0x130(%rsp), %rax
addq $0x1c8, %rsp            # imm = 0x1C8
retq
leaq 0x198(%rsp), %rdi
callq 0xa540
movq 0x170(%rsp), %rdi
callq 0x5970
nopl (%rax)