// File: /workspace/github_cpp_makefile_star2_O1/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
movl $0x8, %esi
movl $0x8, %edx
movl $0x1, %ecx
movl $0x1, %r8d
callq 0x86d2
leaq 0x18(%rsp), %rdi
movl $0x1, %esi
movl $0x8, %edx
movl $0x1, %ecx
movl $0x1, %r8d
callq 0x86d2
xorl %edi, %edi
callq 0x44c0
movl %eax, %edi
callq 0x4510
movq (%r14), %r15
movq 0x8(%r14), %r12
cmpq %r12, %r15
je 0x601e
leaq 0x50(%r15), %rax
cmpq %r12, %rax
je 0x601e
movl $0x50, %r13d
movabsq $-0x3333333333333333, %rbp # imm = 0xCCCCCCCCCCCCCCCD
callq 0x40f0
cltq
movq %r13, %rcx
sarq $0x4, %rcx
imulq %rbp, %rcx
incq %rcx
cqto
idivq %rcx
shlq $0x4, %rdx
leaq (%rdx,%rdx,4), %rsi
cmpq %rsi, %r13
je 0x600d
leaq (%r15,%r13), %rdi
addq %r15, %rsi
callq 0x6caac
leaq (%r15,%r13), %rax
addq $0x50, %rax
addq $0x50, %r13
cmpq %r12, %rax
jne 0x5fda
movq %rbx, 0x8(%rsp)
movq (%r14), %rcx
movq 0x8(%r14), %rax
movq %rax, 0x10(%rsp)
cmpq %rax, %rcx
je 0x615e
movq 0x50(%rsp), %rdx
movl 0x38(%rsp), %esi
movq 0x30(%rsp), %rdi
movl 0x18(%rsp), %r8d
leal (%r8,%r8), %r9d
leal (%rsi,%rsi), %r10d
addq $0x50, %rcx
xorl %r11d, %r11d
movl $0x3, %r14d
xorps %xmm0, %xmm0
movsd 0x6afad(%rip), %xmm1    # 0x71018
movaps 0x6b04e(%rip), %xmm2    # 0x710c0
xorl %r12d, %r12d
xorl %r13d, %r13d
movl %r12d, %ebp
leal 0x1(%r12), %ebx
leal 0x4(%r12), %r15d
movups %xmm0, (%rdx,%r15,8)
xorps %xmm3, %xmm3
cvtss2sd -0x24(%rcx), %xmm3
leal 0x2(%r12), %r15d
leal 0x6(%r12), %eax
cvtps2pd -0x50(%rcx), %xmm5
movhps %xmm5, (%rdx,%rbp,8)
movlps %xmm5, (%rdx,%rbx,8)
movups %xmm1, (%rdx,%r15,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 (%rsi,%r12), %eax
leal 0x3(%rsi,%r12), %ebx
movhps %xmm5, (%rdx,%rbx,8)
leal 0x4(%rsi,%r12), %ebx
movlps %xmm5, (%rdx,%rbx,8)
leal 0x5(%rsi,%r12), %ebx
movabsq $0x3ff0000000000000, %r15 # imm = 0x3FF0000000000000
movq %r15, (%rdx,%rbx,8)
leal 0x2(%rsi,%r12), %ebx
movq %r11, (%rdx,%rbx,8)
leal 0x1(%rsi,%r12), %ebx
movq %r11, (%rdx,%rbx,8)
movq %r11, (%rdx,%rax,8)
xorps %xmm5, %xmm5
cvtss2sd -0x28(%rcx), %xmm5
pshufd $0xee, %xmm4, %xmm6     # xmm6 = xmm4[2,3,2,3]
mulsd %xmm5, %xmm6
leal 0x6(%rsi,%r12), %eax
movsd %xmm6, (%rdx,%rax,8)
mulsd %xmm5, %xmm4
leal 0x7(%rsi,%r12), %eax
movsd %xmm4, (%rdx,%rax,8)
movl %r13d, %eax
movsd %xmm3, (%rdi,%rax,8)
leal (%r8,%r13), %eax
movsd %xmm5, (%rdi,%rax,8)
subq $0x1, %r14
jb 0x615e
addl %r9d, %r13d
addl %r10d, %r12d
leaq 0x50(%rcx), %rax
cmpq 0x10(%rsp), %rcx
movq %rax, %rcx
jne 0x6078
leaq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x8852
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x8a94
cmpb $0x0, 0x28(%rsp)
jne 0x6193
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x6193
callq 0x4680
cmpb $0x0, 0x48(%rsp)
jne 0x61a9
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x61a9
callq 0x4680
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x61d9
movq %rax, %rbx
cmpb $0x0, 0x28(%rsp)
jne 0x61d9
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x61d9
callq 0x4680
cmpb $0x0, 0x48(%rsp)
jne 0x61ef
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x61ef
callq 0x4680
movq %rbx, %rdi
callq 0x4950