// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x38(%rsp), %rdi
pushq $0x8
popq %rbp
pushq $0x1
popq %r15
movl %ebp, %esi
movl %ebp, %edx
movl %r15d, %ecx
movl %r15d, %r8d
callq 0x6caa
leaq 0x18(%rsp), %rdi
movl %r15d, %esi
movl %ebp, %edx
movl %r15d, %ecx
movl %r15d, %r8d
callq 0x6caa
movq %rbx, 0x8(%rsp)
xorl %r15d, %r15d
xorl %edi, %edi
callq 0x44b0
movl %eax, %edi
callq 0x4500
movq (%r14), %rdi
movq 0x8(%r14), %rsi
callq 0x6e32
movq (%r14), %rax
movq %rax, 0x10(%rsp)
movq 0x8(%r14), %rcx
movq 0x50(%rsp), %rdx
movl 0x38(%rsp), %esi
movq 0x30(%rsp), %rdi
movl 0x18(%rsp), %r8d
leal (%rsi,%rsi), %r9d
leal (%r8,%r8), %r10d
xorps %xmm0, %xmm0
movsd 0x4eecf(%rip), %xmm1    # 0x54010
movaps 0x4ef78(%rip), %xmm2    # 0x540c0
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
leaq (%rax,%r14), %r13
cmpq %rcx, %r13
je 0x5244
movl %r12d, %ebp
leal 0x1(%r12), %ebx
leal 0x4(%r12), %r11d
movups %xmm0, (%rdx,%r11,8)
cvtss2sd 0x2c(%r13), %xmm3
leal 0x2(%r12), %r11d
leal 0x6(%r12), %eax
cvtps2pd (%r13), %xmm5
movhps %xmm5, (%rdx,%rbp,8)
movlps %xmm5, (%rdx,%rbx,8)
movups %xmm1, (%rdx,%r11,8)
movaps %xmm5, %xmm4
xorps %xmm2, %xmm4
movaps %xmm3, %xmm6
movlhps %xmm3, %xmm6            # xmm6 = xmm6[0],xmm3[0]
pshufd $0x4e, %xmm4, %xmm7     # xmm7 = xmm4[2,3,0,1]
mulpd %xmm6, %xmm7
movupd %xmm7, (%rdx,%rax,8)
leal 0x3(%rsi,%r12), %eax
movhps %xmm5, (%rdx,%rax,8)
leal 0x4(%rsi,%r12), %eax
movlps %xmm5, (%rdx,%rax,8)
leal 0x5(%rsi,%r12), %eax
movabsq $0x3ff0000000000000, %r11 # imm = 0x3FF0000000000000
movq %r11, (%rdx,%rax,8)
leal (%rsi,%r12), %eax
addl $0x2, %eax
andq $0x0, (%rdx,%rax,8)
leal (%rsi,%r12), %eax
incl %eax
andq $0x0, (%rdx,%rax,8)
leal (%rsi,%r12), %eax
andq $0x0, (%rdx,%rax,8)
cvtss2sd 0x28(%r13), %xmm5
pshufd $0xee, %xmm4, %xmm6     # xmm6 = xmm4[2,3,2,3]
mulsd %xmm5, %xmm6
leal (%rsi,%r12), %eax
addl $0x6, %eax
movsd %xmm6, (%rdx,%rax,8)
mulsd %xmm5, %xmm4
leal 0x7(%rsi,%r12), %eax
movsd %xmm4, (%rdx,%rax,8)
movl %r15d, %eax
movsd %xmm3, (%rdi,%rax,8)
leal (%r8,%r15), %eax
movsd %xmm5, (%rdi,%rax,8)
addl %r9d, %r12d
addl %r10d, %r15d
addq $0x50, %r14
cmpq $0x140, %r14            # imm = 0x140
jne 0x514e
leaq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x6e98
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x7064
leaq 0x18(%rsp), %rdi
callq 0x6c96
leaq 0x38(%rsp), %rdi
callq 0x6c96
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5290
movq %rax, %rbx
jmp 0x529d
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x6c96
leaq 0x38(%rsp), %rdi
callq 0x6c96
movq %rbx, %rdi
callq 0x4950