// File: /workspace/github_cpp_makefile_star2_O1/jamesgrimmett[P]orbital_determination/ex10-1_linear_least_squares.cc
// Function: main
int main() {

  int i;

  // The state vector (alpha and beta)
  std::vector<double> state;
  // The inverse of the information matrix (A^T * A)^-1
  std::vector<std::vector<double> > inv;

  // The observed values of x and y
  std::vector<double> x_obs = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0};
  std::vector<double> y_obs = {1.0, 1.0, 2.0, 3.0, 3.0, 4.0, 4.0, 6.0};

  // The two components of the general solution (A^T * A, and A^T * B)
  std::vector<std::vector<double> > ata = {{0.0, 0.0},{0.0, 0.0}};
  std::vector<double> atb = {0.0, 0.0};

  // fill ata and atb
  for ( i = 0; i < 8; i++ ) {
    ata[0][0] += 1.0;
    ata[0][1] += x_obs[i];
    ata[1][0] += x_obs[i];
    ata[1][1] += pow(x_obs[i], 2);

    atb[0] += y_obs[i];
    atb[1] += x_obs[i] * y_obs[i];
  }

  inv = inverse(ata);
  state = dot_product(inv, atb);

  std::cout << "ATA matrix  " << ata[0][0] << "  " << ata[0][1] << std::endl;
  std::cout << "            " << ata[1][0] << "  " << ata[1][1] << std::endl;
  std::cout << "ATb matrix  " << atb[0] << "  " << atb[1] << std::endl; 
  
  std::cout << "\nState:" << std::endl;
  std::cout << "alpha " << state[0] << std::endl;
  std::cout << "beta  " << state[1] << std::endl;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x140, %rsp            # imm = 0x140
xorps %xmm0, %xmm0
movaps %xmm0, 0xd0(%rsp)
movq $0x0, 0xe0(%rsp)
movups 0x1d5a(%rip), %xmm0     # 0x3060
leaq 0x60(%rsp), %rsi
movaps %xmm0, 0x30(%rsi)
movups 0x1d3a(%rip), %xmm0     # 0x3050
movaps %xmm0, 0x20(%rsi)
movups 0x1d1f(%rip), %xmm0     # 0x3040
movaps %xmm0, 0x10(%rsi)
movupd 0x1d03(%rip), %xmm0     # 0x3030
movapd %xmm0, (%rsi)
leaq 0xb8(%rsp), %rdi
leaq 0x48(%rsp), %rcx
movl $0x8, %edx
callq 0x1caa
movups 0x1d51(%rip), %xmm0     # 0x30a0
leaq 0x60(%rsp), %rsi
movaps %xmm0, 0x30(%rsi)
movups 0x1d31(%rip), %xmm0     # 0x3090
movaps %xmm0, 0x20(%rsi)
movups 0x1d16(%rip), %xmm0     # 0x3080
movaps %xmm0, 0x10(%rsi)
movupd 0x1cfa(%rip), %xmm0     # 0x3070
movapd %xmm0, (%rsi)
leaq 0x48(%rsp), %rdi
leaq 0x30(%rsp), %rcx
movl $0x8, %edx
callq 0x1caa
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movapd %xmm0, (%rsi)
leaq 0x60(%rsp), %r14
leaq 0xf(%rsp), %rcx
movl $0x2, %edx
movq %r14, %rdi
callq 0x1caa
leaq 0x78(%rsp), %rbx
leaq 0xf0(%rsp), %rsi
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsi)
leaq 0xe(%rsp), %rcx
movl $0x2, %edx
movq %rbx, %rdi
callq 0x1caa
leaq 0x30(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0xd(%rsp), %rcx
movl $0x2, %edx
callq 0x1cf0
movl $0x30, %ebx
movq 0x48(%rsp,%rbx), %rdi
testq %rdi, %rdi
je 0x140e
movq 0x58(%rsp,%rbx), %rsi
subq %rdi, %rsi
callq 0x10d0
addq $-0x18, %rbx
jne 0x13f7
xorpd %xmm0, %xmm0
leaq 0x60(%rsp), %rsi
movapd %xmm0, (%rsi)
leaq 0x10(%rsp), %rdi
leaq 0xf0(%rsp), %rcx
movl $0x2, %edx
callq 0x1caa
movq 0x30(%rsp), %rcx
movq (%rcx), %rax
movq 0x18(%rcx), %rcx
movq 0xb8(%rsp), %rdx
movq 0x48(%rsp), %rsi
movq 0x10(%rsp), %rdi
xorl %r8d, %r8d
movsd 0x1ba7(%rip), %xmm0     # 0x3008
movsd (%rax), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rax)
movsd (%rdx,%r8,8), %xmm1
addsd 0x8(%rax), %xmm1
movsd %xmm1, 0x8(%rax)
movsd (%rdx,%r8,8), %xmm1
addsd (%rcx), %xmm1
movsd %xmm1, (%rcx)
movsd (%rdx,%r8,8), %xmm1
mulsd %xmm1, %xmm1
addsd 0x8(%rcx), %xmm1
movsd %xmm1, 0x8(%rcx)
movsd (%rsi,%r8,8), %xmm1
addsd (%rdi), %xmm1
movsd %xmm1, (%rdi)
movsd (%rdx,%r8,8), %xmm1
mulsd (%rsi,%r8,8), %xmm1
addsd 0x8(%rdi), %xmm1
movsd %xmm1, 0x8(%rdi)
incq %r8
cmpq $0x8, %r8
jne 0x1461
leaq 0x128(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1d3a
leaq 0xf0(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x1a22
xorpd %xmm0, %xmm0
leaq 0x60(%rsp), %rdi
movapd %xmm0, (%rdi)
leaq 0xd0(%rsp), %rbx
movapd (%rbx), %xmm0
movq 0x10(%rbx), %rax
leaq 0xf0(%rsp), %r14
movups (%r14), %xmm1
movaps %xmm1, (%rbx)
movq 0x10(%r14), %rcx
movq %rcx, 0x10(%rbx)
movapd (%rdi), %xmm1
movapd %xmm0, (%rdi)
movq %rax, 0x10(%rdi)
movupd %xmm1, (%r14)
movq $0x0, 0x10(%r14)
callq 0x1dd4
movq %r14, %rdi
callq 0x1dd4
leaq 0x128(%rsp), %rdi
callq 0x1dd4
leaq 0x110(%rsp), %rdi
movq %rbx, %rsi
callq 0x1d3a
leaq 0xa0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1e22
leaq 0x60(%rsp), %rdi
leaq 0x110(%rsp), %rsi
leaq 0xa0(%rsp), %rdx
callq 0x1b0c
movq 0x60(%rsp), %rbx
movq 0x70(%rsp), %r14
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x15d0
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0x110(%rsp), %rdi
callq 0x1dd4
movq 0x39f4(%rip), %rdi      # 0x4fd8
leaq 0x1ac5(%rip), %rsi      # 0x30b0
movl $0xc, %edx
callq 0x10e0
movq 0x30(%rsp), %rax
movq (%rax), %rax
movsd (%rax), %xmm0
movq 0x39d0(%rip), %rdi      # 0x4fd8
callq 0x1110
movq %rax, %r15
leaq 0x1ab0(%rip), %rsi      # 0x30c7
movl $0x2, %edx
movq %rax, %rdi
callq 0x10e0
movq 0x30(%rsp), %rax
movq (%rax), %rax
movsd 0x8(%rax), %xmm0
movq %r15, %rdi
callq 0x1110
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1080
movq 0x396e(%rip), %rdi      # 0x4fd8
leaq 0x1a4c(%rip), %rsi      # 0x30bd
movl $0xc, %edx
callq 0x10e0
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
movsd (%rax), %xmm0
movq 0x3949(%rip), %rdi      # 0x4fd8
callq 0x1110
movq %rax, %r15
leaq 0x1a29(%rip), %rsi      # 0x30c7
movl $0x2, %edx
movq %rax, %rdi
callq 0x10e0
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
movsd 0x8(%rax), %xmm0
movq %r15, %rdi
callq 0x1110
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1080
movq 0x38e6(%rip), %rdi      # 0x4fd8
leaq 0x19d1(%rip), %rsi      # 0x30ca
movl $0xc, %edx
callq 0x10e0
movq 0x10(%rsp), %rax
movsd (%rax), %xmm0
movq 0x38c5(%rip), %rdi      # 0x4fd8
callq 0x1110
movq %rax, %r15
leaq 0x19a5(%rip), %rsi      # 0x30c7
movl $0x2, %edx
movq %rax, %rdi
callq 0x10e0
movq 0x10(%rsp), %rax
movsd 0x8(%rax), %xmm0
movq %r15, %rdi
callq 0x1110
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1080
movq 0x3866(%rip), %r15      # 0x4fd8
leaq 0x195e(%rip), %rsi      # 0x30d7
movl $0x7, %edx
movq %r15, %rdi
callq 0x10e0
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq 0x3834(%rip), %rdi      # 0x4fd8
callq 0x1030
movq %rax, %rdi
callq 0x1080
movq 0x3820(%rip), %rdi      # 0x4fd8
leaq 0x1920(%rip), %rsi      # 0x30df
movl $0x6, %edx
callq 0x10e0
movsd (%rbx), %xmm0
movq 0x3804(%rip), %rdi      # 0x4fd8
callq 0x1110
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1080
movq 0x37ce(%rip), %rdi      # 0x4fd8
leaq 0x18d5(%rip), %rsi      # 0x30e6
movl $0x6, %edx
callq 0x10e0
movsd 0x8(%rbx), %xmm0
movq 0x37b1(%rip), %rdi      # 0x4fd8
callq 0x1110
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1080
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x186d
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0x30(%rsp), %rdi
callq 0x1dd4
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x188e
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x18ab
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0xd0(%rsp), %rdi
callq 0x1dd4
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10d0
xorl %eax, %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbx
popq %r14
popq %r15
retq
movq %rax, %r15
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x18fa
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
jmp 0x18fa
movq %rax, %r15
leaq 0x110(%rsp), %rdi
jmp 0x1911
jmp 0x1918
movq %rax, %r15
leaq 0x128(%rsp), %rdi
callq 0x1dd4
jmp 0x191b
movq %rax, %r15
xorl %ebx, %ebx
xorl %r14d, %r14d
jmp 0x19a5
movq %rax, %r15
xorl %ebx, %ebx
xorl %r14d, %r14d
jmp 0x19bc
movq %rax, %r15
leaq 0x90(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1160
movb $0x1, %al
jmp 0x195a
movq %rax, %r15
xorl %eax, %eax
jmp 0x195a
movq %rax, %r15
xorl %eax, %eax
movq %r14, %rbx
cmpq %rbx, %r14
sete %cl
orb %al, %cl
jne 0x1987
leaq 0x60(%rsp), %r14
movq -0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x197e
movq -0x8(%rbx), %rsi
subq %rdi, %rsi
callq 0x10d0
addq $-0x18, %rbx
cmpq %r14, %rbx
jne 0x1969
xorl %ebx, %ebx
xorl %r14d, %r14d
jmp 0x19c6
movq %rax, %r15
xorl %ebx, %ebx
xorl %r14d, %r14d
jmp 0x19dd
movq %rax, %r15
xorl %ebx, %ebx
xorl %r14d, %r14d
jmp 0x19fa
movq %rax, %r15
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x19bc
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0x30(%rsp), %rdi
callq 0x1dd4
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x19dd
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x19fa
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0xd0(%rsp), %rdi
callq 0x1dd4
testq %rbx, %rbx
je 0x1a1a
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10d0
movq %r15, %rdi
callq 0x1140