// File: /workspace/github_cpp_makefile_star2_O3/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, 0x10(%rsp)
leaq 0x38(%rsp), %rdi
movl $0x8, %esi
movl $0x8, %edx
movl $0x1, %ecx
movl $0x1, %r8d
callq 0x8666
leaq 0x18(%rsp), %rdi
movl $0x1, %esi
movl $0x8, %edx
movl $0x1, %ecx
movl $0x1, %r8d
callq 0x8666
xorl %edi, %edi
callq 0x44d0
movl %eax, %edi
callq 0x4520
movq (%r14), %r15
movq 0x8(%r14), %r12
cmpq %r12, %r15
je 0x616d
leaq 0x50(%r15), %rax
cmpq %r12, %rax
je 0x603b
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 0x6023
leaq (%r15,%r13), %rdi
addq %r15, %rsi
callq 0x69a9a
leaq (%r15,%r13), %rax
addq $0x50, %rax
addq $0x50, %r13
cmpq %r12, %rax
jne 0x5ff0
movq (%r14), %r15
movq 0x8(%r14), %r12
cmpq %r12, %r15
je 0x616d
movq 0x50(%rsp), %rax
movl 0x38(%rsp), %ecx
movq 0x30(%rsp), %rdx
movl 0x18(%rsp), %esi
leal (%rcx,%rcx), %edi
movl %edi, 0xc(%rsp)
leal (%rsi,%rsi), %r8d
addq $0x50, %r15
xorl %r9d, %r9d
movl $0x3, %r10d
xorps %xmm0, %xmm0
movsd 0x67f97(%rip), %xmm1    # 0x6e010
movaps 0x68040(%rip), %xmm2    # 0x6e0c0
xorl %r14d, %r14d
xorl %r13d, %r13d
movl %r13d, %ebp
leal 0x1(%r13), %ebx
leal 0x4(%r13), %r11d
movups %xmm0, (%rax,%r11,8)
xorps %xmm3, %xmm3
cvtss2sd -0x24(%r15), %xmm3
leal 0x2(%r13), %r11d
leal 0x6(%r13), %edi
cvtps2pd -0x50(%r15), %xmm5
movhps %xmm5, (%rax,%rbp,8)
movlps %xmm5, (%rax,%rbx,8)
movups %xmm1, (%rax,%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, (%rax,%rdi,8)
leal (%rcx,%r13), %edi
leal 0x3(%rcx,%r13), %r11d
movhps %xmm5, (%rax,%r11,8)
leal 0x4(%rcx,%r13), %r11d
movlps %xmm5, (%rax,%r11,8)
leal 0x5(%rcx,%r13), %r11d
movabsq $0x3ff0000000000000, %rbx # imm = 0x3FF0000000000000
movq %rbx, (%rax,%r11,8)
leal 0x2(%rcx,%r13), %r11d
movq %r9, (%rax,%r11,8)
leal 0x1(%rcx,%r13), %r11d
movq %r9, (%rax,%r11,8)
movq %r9, (%rax,%rdi,8)
xorps %xmm5, %xmm5
cvtss2sd -0x28(%r15), %xmm5
pshufd $0xee, %xmm4, %xmm6     # xmm6 = xmm4[2,3,2,3]
mulsd %xmm5, %xmm6
leal 0x6(%rcx,%r13), %edi
movsd %xmm6, (%rax,%rdi,8)
mulsd %xmm5, %xmm4
leal 0x7(%rcx,%r13), %edi
movsd %xmm4, (%rax,%rdi,8)
movl %r14d, %edi
movsd %xmm3, (%rdx,%rdi,8)
leal (%rsi,%r14), %edi
movsd %xmm5, (%rdx,%rdi,8)
subq $0x1, %r10
jb 0x616d
addl 0xc(%rsp), %r13d
addl %r8d, %r14d
leaq 0x50(%r15), %rdi
cmpq %r12, %r15
movq %rdi, %r15
jne 0x6086
leaq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x87f4
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x8a36
cmpb $0x0, 0x28(%rsp)
jne 0x61a2
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x61a2
callq 0x4690
cmpb $0x0, 0x48(%rsp)
jne 0x61b8
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x61b8
callq 0x4690
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x61e8
movq %rax, %rbx
cmpb $0x0, 0x28(%rsp)
jne 0x61e8
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x61e8
callq 0x4690
cmpb $0x0, 0x48(%rsp)
jne 0x61fe
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x61fe
callq 0x4690
movq %rbx, %rdi
callq 0x4960