// File: fhirschman[P]CANF-gp/source/CPU_functions.cc
// Function: getRootsMatrix(std::vector<std::vector<float, std::allocator<float>>, std::allocator<std::vector<float, std::allocator<float>>>>&)
void getRootsMatrix(vector<vector<float>>& rootsImage){
	for(int i=0; i<N_Y; i++){
		for(int j=0; j<N_X; j++){
			iter_matrix[i][j]=solve_cnewton_iter(cnumber_matrix[i][j], ACCURACY, N_ITER, POLY_COEFFS, POLY_N_TERMS, POLY_EXPONS);
		}
	}
	vector<complex<double>> roots;
	for(int i=0; i<N_Y; i++){
		vector<float> row;
		for(int j=0; j<N_X; j++){
			complex<double> root = solve_cnewton_roots(cnumber_matrix[i][j], ACCURACY, N_ITER, POLY_COEFFS, POLY_N_TERMS, POLY_EXPONS);
			if(abs(real(root)) > 10*INTERVAL_LENGTH_X || abs(imag(root)) > 10*INTERVAL_LENGTH_Y ){
				root = {0,0};
			} 
			if(i==0 && j==0){
				roots.push_back(root);
			}
			bool new_root = true;
			for (int k=0; k< roots.size(); k++){
				if( abs(real(root)-real(roots[k]))<0.01 && abs(imag(root)-imag(roots[k]))<0.01){
					roots_matrix[i][j]=k;
					row.push_back(roots_matrix[i][j]);
					new_root=false;
					break;
				}
			}
			if(new_root==true){
				roots_matrix[i][j]=roots.size()+1;
				row.push_back(roots_matrix[i][j]);
				roots.push_back(root);
			}
		}
		rootsImage.push_back(row);
	}
	cout << "Found " << roots.size() << " roots:" << endl;
	for (int k=0; k< roots.size(); k++){
		cout << " " << real(roots[k]) << " " << imag(roots[k]) << endl;
	}
	return;
}

// Assembly:
subq $0x178, %rsp            # imm = 0x178
movq %rdi, 0x170(%rsp)
movl $0x0, 0x16c(%rsp)
movl 0x16c(%rsp), %eax
leaq 0x11820(%rip), %rcx     # 0x298e8
cmpl (%rcx), %eax
jge 0x181a8
movl $0x0, 0x168(%rsp)
movl 0x168(%rsp), %eax
leaq 0x117fb(%rip), %rcx     # 0x298e4
cmpl (%rcx), %eax
jge 0x18190
leaq 0x11980(%rip), %rax     # 0x29a78
movq (%rax), %rax
movslq 0x16c(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movslq 0x168(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rdi
leaq 0x1185b(%rip), %rax     # 0x29978
movsd (%rax), %xmm0
leaq 0x117b8(%rip), %rax     # 0x298e0
movl (%rax), %esi
leaq 0x117ab(%rip), %rax     # 0x298dc
movl (%rax), %ecx
leaq 0x1189e(%rip), %rdx     # 0x299d8
leaq 0x118af(%rip), %r8      # 0x299f0
callq 0x17cb0
movl %eax, 0xe0(%rsp)
movslq 0x16c(%rsp), %rsi
leaq 0x118ac(%rip), %rdi     # 0x29a08
callq 0xbd50
movq %rax, %rdi
movslq 0x168(%rsp), %rsi
callq 0xbd70
movl 0xe0(%rsp), %ecx
movl %ecx, (%rax)
movl 0x168(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x168(%rsp)
jmp 0x180db
jmp 0x18192
movl 0x16c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x16c(%rsp)
jmp 0x180ba
leaq 0x150(%rsp), %rdi
callq 0x18e50
movl $0x0, 0x14c(%rsp)
movl 0x14c(%rsp), %eax
leaq 0x1171a(%rip), %rcx     # 0x298e8
cmpl (%rcx), %eax
jge 0x186ab
leaq 0x130(%rsp), %rdi
callq 0x18d40
movl $0x0, 0x12c(%rsp)
movl 0x12c(%rsp), %eax
leaq 0x116e8(%rip), %rcx     # 0x298e4
cmpl (%rcx), %eax
jge 0x18671
leaq 0x1186d(%rip), %rax     # 0x29a78
movq (%rax), %rax
movslq 0x14c(%rsp), %rcx
movq (%rax,%rcx,8), %rdi
movslq 0x12c(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rdi
leaq 0x11748(%rip), %rax     # 0x29978
movsd (%rax), %xmm0
leaq 0x116a5(%rip), %rax     # 0x298e0
movl (%rax), %esi
leaq 0x11698(%rip), %rax     # 0x298dc
movl (%rax), %ecx
leaq 0x1178b(%rip), %rdx     # 0x299d8
leaq 0x1179c(%rip), %r8      # 0x299f0
callq 0x17f60
movsd %xmm1, 0xd0(%rsp)
movsd %xmm0, 0xd8(%rsp)
jmp 0x1826d
movsd 0xd0(%rsp), %xmm0
movsd 0xd8(%rsp), %xmm1
movsd %xmm1, 0x118(%rsp)
movsd %xmm0, 0x120(%rsp)
leaq 0x118(%rsp), %rdi
callq 0xbd10
movsd %xmm0, 0xc8(%rsp)
jmp 0x182a9
movsd 0xc8(%rsp), %xmm0
callq 0xbe50
movsd %xmm0, 0xc0(%rsp)
jmp 0x182c2
movsd 0xc0(%rsp), %xmm0
movsd 0x6ccd(%rip), %xmm1     # 0x1efa0
leaq 0x116be(%rip), %rax     # 0x29998
mulsd (%rax), %xmm1
ucomisd %xmm1, %xmm0
ja 0x18337
leaq 0x118(%rsp), %rdi
callq 0xbd20
movsd %xmm0, 0xb8(%rsp)
jmp 0x182fc
movsd 0xb8(%rsp), %xmm0
callq 0xbe50
movsd %xmm0, 0xb0(%rsp)
jmp 0x18315
movsd 0xb0(%rsp), %xmm0
movsd 0x6c7a(%rip), %xmm1     # 0x1efa0
leaq 0x11673(%rip), %rax     # 0x299a0
mulsd (%rax), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x18394
leaq 0xf8(%rsp), %rdi
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0xc0c0
jmp 0x1834c
movq 0xf8(%rsp), %rax
movq %rax, 0x118(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x120(%rsp)
jmp 0x18394
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
leaq 0x130(%rsp), %rdi
callq 0x18df0
jmp 0x18847
cmpl $0x0, 0x14c(%rsp)
jne 0x183c1
cmpl $0x0, 0x12c(%rsp)
jne 0x183c1
leaq 0x150(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x18e60
jmp 0x183bf
jmp 0x183c1
movb $0x1, 0xf7(%rsp)
movl $0x0, 0xf0(%rsp)
movslq 0xf0(%rsp), %rax
movq %rax, 0xa8(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x18ed0
movq %rax, %rcx
movq 0xa8(%rsp), %rax
cmpq %rcx, %rax
jae 0x185a3
leaq 0x118(%rsp), %rdi
callq 0xbd10
movsd %xmm0, 0xa0(%rsp)
jmp 0x1841d
movslq 0xf0(%rsp), %rsi
leaq 0x150(%rsp), %rdi
callq 0x18ef0
movq %rax, %rdi
callq 0xbd10
movsd %xmm0, 0x98(%rsp)
jmp 0x18445
movsd 0x98(%rsp), %xmm1
movsd 0xa0(%rsp), %xmm0
subsd %xmm1, %xmm0
callq 0xbe50
movsd %xmm0, 0x90(%rsp)
jmp 0x1846b
movsd 0x90(%rsp), %xmm1
movsd 0x6b2c(%rip), %xmm0     # 0x1efa8
ucomisd %xmm1, %xmm0
jbe 0x1858b
leaq 0x118(%rsp), %rdi
callq 0xbd20
movsd %xmm0, 0x88(%rsp)
jmp 0x1849e
movslq 0xf0(%rsp), %rsi
leaq 0x150(%rsp), %rdi
callq 0x18ef0
movq %rax, %rdi
callq 0xbd20
movsd %xmm0, 0x80(%rsp)
jmp 0x184c6
movsd 0x80(%rsp), %xmm1
movsd 0x88(%rsp), %xmm0
subsd %xmm1, %xmm0
callq 0xbe50
movsd %xmm0, 0x78(%rsp)
jmp 0x184e9
movsd 0x78(%rsp), %xmm1
movsd 0x6ab1(%rip), %xmm0     # 0x1efa8
ucomisd %xmm1, %xmm0
jbe 0x1858b
movl 0xf0(%rsp), %eax
movl %eax, 0x6c(%rsp)
movslq 0x14c(%rsp), %rsi
leaq 0x11505(%rip), %rdi     # 0x29a20
movq %rdi, 0x70(%rsp)
callq 0xbd50
movq %rax, %rdi
movslq 0x12c(%rsp), %rsi
callq 0xbd70
movl 0x6c(%rsp), %ecx
movq 0x70(%rsp), %rdi
movl %ecx, (%rax)
movslq 0x14c(%rsp), %rsi
callq 0xbd50
movq %rax, %rdi
movslq 0x12c(%rsp), %rsi
callq 0xbd70
cvtsi2ssl (%rax), %xmm0
movss %xmm0, 0xec(%rsp)
leaq 0x130(%rsp), %rdi
leaq 0xec(%rsp), %rsi
callq 0x18d50
jmp 0x18581
movb $0x0, 0xf7(%rsp)
jmp 0x185a3
jmp 0x1858d
movl 0xf0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf0(%rsp)
jmp 0x183d4
movb 0xf7(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x18659
leaq 0x150(%rsp), %rdi
callq 0x18ed0
incl %eax
movl %eax, 0x5c(%rsp)
movslq 0x14c(%rsp), %rsi
leaq 0x11446(%rip), %rdi     # 0x29a20
movq %rdi, 0x60(%rsp)
callq 0xbd50
movq %rax, %rdi
movslq 0x12c(%rsp), %rsi
callq 0xbd70
movl 0x5c(%rsp), %ecx
movq 0x60(%rsp), %rdi
movl %ecx, (%rax)
movslq 0x14c(%rsp), %rsi
callq 0xbd50
movq %rax, %rdi
movslq 0x12c(%rsp), %rsi
callq 0xbd70
cvtsi2ssl (%rax), %xmm0
movss %xmm0, 0xe8(%rsp)
leaq 0x130(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x18d50
jmp 0x18640
leaq 0x150(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x18e60
jmp 0x18657
jmp 0x18659
jmp 0x1865b
movl 0x12c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x12c(%rsp)
jmp 0x181ee
movq 0x170(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x18d80
jmp 0x18688
leaq 0x130(%rsp), %rdi
callq 0x18df0
movl 0x14c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14c(%rsp)
jmp 0x181c0
movq 0x108f6(%rip), %rdi     # 0x28fa8
leaq 0x6907(%rip), %rsi      # 0x1efc0
callq 0x84e0
movq %rax, 0x50(%rsp)
jmp 0x186c5
leaq 0x150(%rsp), %rdi
callq 0x18ed0
movq 0x50(%rsp), %rdi
movq %rax, %rsi
callq 0x80e0
movq %rax, 0x48(%rsp)
jmp 0x186e6
movq 0x48(%rsp), %rdi
leaq 0x68d5(%rip), %rsi      # 0x1efc7
callq 0x84e0
movq %rax, 0x40(%rsp)
jmp 0x186fe
movq 0x40(%rsp), %rdi
movq 0x10866(%rip), %rsi     # 0x28f70
callq 0x8560
jmp 0x18711
movl $0x0, 0xe4(%rsp)
movslq 0xe4(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x18ed0
movq %rax, %rcx
movq 0x38(%rsp), %rax
cmpq %rcx, %rax
jae 0x18832
movq 0x1085a(%rip), %rdi     # 0x28fa8
leaq 0x69f9(%rip), %rsi      # 0x1f14e
callq 0x84e0
movq %rax, 0x30(%rsp)
jmp 0x18761
movslq 0xe4(%rsp), %rsi
leaq 0x150(%rsp), %rdi
callq 0x18ef0
movq %rax, %rdi
callq 0xbd10
movsd %xmm0, 0x28(%rsp)
jmp 0x18786
movsd 0x28(%rsp), %xmm0
movq 0x30(%rsp), %rdi
callq 0x8a10
movq %rax, 0x20(%rsp)
jmp 0x1879d
movq 0x20(%rsp), %rdi
leaq 0x69a5(%rip), %rsi      # 0x1f14e
callq 0x84e0
movq %rax, 0x18(%rsp)
jmp 0x187b5
movslq 0xe4(%rsp), %rsi
leaq 0x150(%rsp), %rdi
callq 0x18ef0
movq %rax, %rdi
callq 0xbd20
movsd %xmm0, 0x10(%rsp)
jmp 0x187da
movsd 0x10(%rsp), %xmm0
movq 0x18(%rsp), %rdi
callq 0x8a10
movq %rax, 0x8(%rsp)
jmp 0x187f1
movq 0x8(%rsp), %rdi
movq 0x10773(%rip), %rsi     # 0x28f70
callq 0x8560
jmp 0x18804
jmp 0x18806
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x1871c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
jmp 0x18847
leaq 0x150(%rsp), %rdi
callq 0x18f10
addq $0x178, %rsp            # imm = 0x178
retq
leaq 0x150(%rsp), %rdi
callq 0x18f10
movq 0x110(%rsp), %rdi
callq 0x8960
nopw %cs:(%rax,%rax)