// File: /workspace/github_cpp_makefile_star2_O3/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, 0xa0(%rsp)
movq $0x0, 0xb0(%rsp)
movups 0x1d8a(%rip), %xmm0     # 0x3060
leaq 0x60(%rsp), %rsi
movaps %xmm0, 0x30(%rsi)
movups 0x1d6a(%rip), %xmm0     # 0x3050
movaps %xmm0, 0x20(%rsi)
movups 0x1d4f(%rip), %xmm0     # 0x3040
movaps %xmm0, 0x10(%rsi)
movupd 0x1d33(%rip), %xmm0     # 0x3030
movapd %xmm0, (%rsi)
leaq 0xd8(%rsp), %rdi
leaq 0x48(%rsp), %rcx
movl $0x8, %edx
callq 0x1c3a
movups 0x1d81(%rip), %xmm0     # 0x30a0
leaq 0x60(%rsp), %rsi
movaps %xmm0, 0x30(%rsi)
movups 0x1d61(%rip), %xmm0     # 0x3090
movaps %xmm0, 0x20(%rsi)
movups 0x1d46(%rip), %xmm0     # 0x3080
movaps %xmm0, 0x10(%rsi)
movupd 0x1d2a(%rip), %xmm0     # 0x3070
movapd %xmm0, (%rsi)
leaq 0x48(%rsp), %rdi
leaq 0x30(%rsp), %rcx
movl $0x8, %edx
callq 0x1c3a
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %rsi
movapd %xmm0, (%rsi)
leaq 0x60(%rsp), %rdi
leaq 0xf(%rsp), %rcx
movl $0x2, %edx
callq 0x1c3a
leaq 0x78(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsi)
leaq 0xe(%rsp), %rcx
movl $0x2, %edx
callq 0x1c3a
leaq 0x30(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0xd(%rsp), %rcx
movl $0x2, %edx
callq 0x1c80
movl $0x30, %ebx
movq 0x48(%rsp,%rbx), %rdi
testq %rdi, %rdi
je 0x13d8
movq 0x58(%rsp,%rbx), %rsi
subq %rdi, %rsi
callq 0x10d0
addq $-0x18, %rbx
jne 0x13c1
xorpd %xmm0, %xmm0
leaq 0x60(%rsp), %rsi
movapd %xmm0, (%rsi)
leaq 0x10(%rsp), %rdi
leaq 0xf0(%rsp), %rcx
movl $0x2, %edx
callq 0x1c3a
movq 0x30(%rsp), %rcx
movq (%rcx), %rax
movq 0x18(%rcx), %rcx
movq 0xd8(%rsp), %rdx
movq 0x48(%rsp), %rsi
movq 0x10(%rsp), %rdi
xorl %r8d, %r8d
movsd 0x1bdd(%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 0x142b
leaq 0x128(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1cca
leaq 0xf0(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x19dc
leaq 0xa0(%rsp), %rbx
movq 0x10(%rbx), %rax
leaq 0xf0(%rsp), %r14
movapd (%r14), %xmm0
movq 0x10(%r14), %rcx
movq %rcx, 0x10(%rbx)
movapd (%rbx), %xmm1
movapd %xmm0, (%rbx)
leaq 0x60(%rsp), %rdi
movapd %xmm1, (%rdi)
movq %rax, 0x10(%rdi)
xorpd %xmm0, %xmm0
movapd %xmm0, (%r14)
movq $0x0, 0x10(%r14)
callq 0x1d64
movq %r14, %rdi
callq 0x1d64
leaq 0x128(%rsp), %rdi
callq 0x1d64
leaq 0x110(%rsp), %rdi
movq %rbx, %rsi
callq 0x1cca
leaq 0xc0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x1db8
leaq 0x60(%rsp), %rdi
leaq 0x110(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
callq 0x1ab3
movq 0x60(%rsp), %rbx
movq 0x70(%rsp), %r14
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movq $0x0, 0x70(%rsp)
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x1594
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0x110(%rsp), %rdi
callq 0x1d64
movq 0x3a30(%rip), %rdi      # 0x4fd8
leaq 0x1b01(%rip), %rsi      # 0x30b0
movl $0xc, %edx
callq 0x10e0
movq 0x30(%rsp), %rax
movq (%rax), %rax
movsd (%rax), %xmm0
movq 0x3a0c(%rip), %rdi      # 0x4fd8
callq 0x1110
movq %rax, %r15
leaq 0x1aec(%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 0x39aa(%rip), %rdi      # 0x4fd8
leaq 0x1a88(%rip), %rsi      # 0x30bd
movl $0xc, %edx
callq 0x10e0
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
movsd (%rax), %xmm0
movq 0x3985(%rip), %rdi      # 0x4fd8
callq 0x1110
movq %rax, %r15
leaq 0x1a65(%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 0x3922(%rip), %rdi      # 0x4fd8
leaq 0x1a0d(%rip), %rsi      # 0x30ca
movl $0xc, %edx
callq 0x10e0
movq 0x10(%rsp), %rax
movsd (%rax), %xmm0
movq 0x3901(%rip), %rdi      # 0x4fd8
callq 0x1110
movq %rax, %r15
leaq 0x19e1(%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 0x38a2(%rip), %r15      # 0x4fd8
leaq 0x199a(%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 0x3870(%rip), %rdi      # 0x4fd8
callq 0x1030
movq %rax, %rdi
callq 0x1080
movq 0x385c(%rip), %rdi      # 0x4fd8
leaq 0x195c(%rip), %rsi      # 0x30df
movl $0x6, %edx
callq 0x10e0
movsd (%rbx), %xmm0
movq 0x3840(%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 0x380a(%rip), %rdi      # 0x4fd8
leaq 0x1911(%rip), %rsi      # 0x30e6
movl $0x6, %edx
callq 0x10e0
movsd 0x8(%rbx), %xmm0
movq 0x37ed(%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 0x1831
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0x30(%rsp), %rdi
callq 0x1d64
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x1852
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x186f
movq 0xe8(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0xa0(%rsp), %rdi
callq 0x1d64
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 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x18be
movq 0xd0(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
jmp 0x18be
movq %rax, %r15
leaq 0x110(%rsp), %rdi
jmp 0x18d5
jmp 0x18dc
movq %rax, %r15
leaq 0x128(%rsp), %rdi
callq 0x1d64
jmp 0x18df
movq %rax, %r15
xorl %ebx, %ebx
xorl %r14d, %r14d
jmp 0x195f
movq %rax, %r15
xorl %ebx, %ebx
xorl %r14d, %r14d
jmp 0x1976
movq %rax, %r15
movl $0x30, %ebx
movq 0x48(%rsp,%rbx), %rdi
testq %rdi, %rdi
je 0x1912
movq 0x58(%rsp,%rbx), %rsi
subq %rdi, %rsi
callq 0x10d0
addq $-0x18, %rbx
jne 0x18fb
jmp 0x1939
movq %rax, %r15
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x1939
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
jmp 0x1939
movq %rax, %r15
xorl %ebx, %ebx
xorl %r14d, %r14d
jmp 0x1980
movq %rax, %r15
xorl %ebx, %ebx
xorl %r14d, %r14d
jmp 0x1997
movq %rax, %r15
leaq 0xa0(%rsp), %rdi
callq 0x1d64
jmp 0x19d4
movq %rax, %r15
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1976
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0x30(%rsp), %rdi
callq 0x1d64
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x1997
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x19b4
movq 0xe8(%rsp), %rsi
subq %rdi, %rsi
callq 0x10d0
leaq 0xa0(%rsp), %rdi
callq 0x1d64
testq %rbx, %rbx
je 0x19d4
subq %rbx, %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10d0
movq %r15, %rdi
callq 0x1140