// File: /workspace/github_cpp_makefile_star2_O2/rachit31[P]CubeSatADCS/propagateANDupdate.cpp
// Function: update(double (&) [4], double (&) [3], double (&) [6][6], double const (&) [3], double const (&) [3], double)
void update(double (&q_BI)[4], double (&Bw_B)[3], double (&P)[6][6], const double (&v_B)[3], const double (&v_I)[3], const double sigmav)
{
  // Create DCM from estimate of q_BI
  double A_BI[3][3];
  q2A(q_BI,A_BI);

  // Project Inertial Vector to Body
  double vhat_B[3];
  matMult(A_BI,v_I,vhat_B);

  // Create Cross Body Matrix from vhat_B
  double vhat_BX[3][3];
  X(vhat_B,vhat_BX);

  // Transpose the Cross Product Matrix
  double vhat_BXT[3][3];
  trans3x3(vhat_BX,vhat_BXT);

  // Extract Upper Left Covariance Sub Matrix
  double P11[3][3];
  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < 3; j++)
    {
      P11[i][j] = P[i][j];
    }
  }

  // P(1:3,1:3)*vhat_BX'
  double P11vhat_BXT[3][3];
  mult3x3(P11,vhat_BXT,P11vhat_BXT);

  // vhat_BX*P(1:3,1:3)*vhat_BX'
  double vhat_BXP11vhat_BXT[3][3];
  mult3x3(vhat_BX,P11vhat_BXT,vhat_BXP11vhat_BXT);

  // Form measurment covariance matrix
  double R[3][3] = { { 0, 0, 0},
                     { 0, 0, 0},
                     { 0, 0, 0} };
  R[0][0] = pow(sigmav,2);
  R[1][1] = pow(sigmav,2);
  R[2][2] = pow(sigmav,2);

  // H*P*H' + R
  double HPHT_R[3][3];
  add3x3(vhat_BXP11vhat_BXT,R,HPHT_R);

  // inv(H*P*H'+R)
  double invHPHT_R[3][3];
  inv3x3(HPHT_R,invHPHT_R);

  // H'*inv(H*P*H'+R)
  double vhat_BXTinvHPHT_R[3][3];
  mult3x3(vhat_BXT,invHPHT_R,vhat_BXTinvHPHT_R);

  // P(1:3,1:3)*H'*inv(H*P*H'+R)
  double K1[3][3];
  mult3x3(P11,vhat_BXTinvHPHT_R,K1);

  // Extract Bottom Left Covariance Sub Matrix
  double P21[3][3];
  for (int i = 3; i < 6; i++)
  {
    for (int j = 0; j < 3; j++)
    {
      P21[i-3][j] = P[i][j];
    }
  }

  // P(4:6,1:3)*H'*inv(H*P*H'+R)
  double K2[3][3];
  mult3x3(P21,vhat_BXTinvHPHT_R,K2);

  // Calculate Residual
  double residual[3];
  residual[0] = v_B[0] - vhat_B[0];
  residual[1] = v_B[1] - vhat_B[1];
  residual[2] = v_B[2] - vhat_B[2];

  // Calculate Error Angles  K1*residual
  double dth[3];
  matMult(K1,residual,dth);

  // Calculate Error Bw K2*residual
  double dBw[3];
  matMult(K2,residual,dBw);

  // Update Quaternion
  double q[4];
  q[0] = q_BI[0] - (dth[0]*q_BI[1])/2.0 - (dth[1]*q_BI[2])/2.0 - (dth[2]*q_BI[3])/2.0;
  q[1] = q_BI[1] + (dth[0]*q_BI[0])/2.0 - (dth[1]*q_BI[3])/2.0 + (dth[2]*q_BI[2])/2.0;
  q[2] = q_BI[2] + (dth[1]*q_BI[0])/2.0 + (dth[0]*q_BI[3])/2.0 - (dth[2]*q_BI[1])/2.0;
  q[3] = q_BI[3] - (dth[0]*q_BI[2])/2.0 + (dth[1]*q_BI[1])/2.0 + (dth[2]*q_BI[0])/2.0;

  double qmag = sqrt(q[0]*q[0]+q[1]*q[1]+q[2]*q[2]+q[3]*q[3]);

  q_BI[0] = q[0]/qmag;
  q_BI[1] = q[1]/qmag;
  q_BI[2] = q[2]/qmag;
  q_BI[3] = q[3]/qmag;

  // K1*vhat_BX
  double K1vhat_BX[3][3];
  mult3x3(K1,vhat_BX,K1vhat_BX);

  // K2*vhat_BX
  double K2vhat_BX[3][3];
  mult3x3(K2,vhat_BX,K2vhat_BX);

  double KH[6][6] = { { 0, 0, 0, 0, 0, 0},
                      { 0, 0, 0, 0, 0, 0},
                      { 0, 0, 0, 0, 0, 0},
                      { 0, 0, 0, 0, 0, 0},
                      { 0, 0, 0, 0, 0, 0},
                      { 0, 0, 0, 0, 0, 0} };

  // For K*H
  for (int i = 0; i < 3; i++)
  {
    for (int j = 0; j < 3; j++)
    {
      KH[i][j] = K1vhat_BX[i][j];
    }
  }

  for (int i = 3; i < 6; i++)
  {
    for (int j = 0; j < 3; j++)
    {
      KH[i][j] = K2vhat_BX[i-3][j];
    }
  }

  // -1.0*K*H
  double nKH[6][6];
  mult6x6Scalar(KH,-1.0,nKH);

  // Form eye(6)
  double I6[6][6] = { { 1, 0, 0, 0, 0, 0},
                      { 0, 1, 0, 0, 0, 0},
                      { 0, 0, 1, 0, 0, 0},
                      { 0, 0, 0, 1, 0, 0},
                      { 0, 0, 0, 0, 1, 0},
                      { 0, 0, 0, 0, 0, 1} };

  // I6 - K*H
  double I6nKH[6][6];
  add6x6(I6,nKH,I6nKH);

  // (I6 - K*H)*P
  double Ptemp[6][6];
  mult6x6(I6nKH,P,Ptemp);

  // Transpose P
  double PtempT[6][6];
  trans6x6(Ptemp,PtempT);

  // P + P'
  double PSymmetric[6][6];
  add6x6(Ptemp,PtempT,PSymmetric);

  // (P + P)/2
  mult6x6Scalar(PSymmetric,0.5,P);

  // Update Gyro Bias
  Bw_B[0] += dBw[0];
  Bw_B[1] += dBw[1];
  Bw_B[2] += dBw[2];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd38, %rsp            # imm = 0xD38
movsd %xmm0, -0x40(%rbp)
movq %r8, %r14
movq %rcx, -0x38(%rbp)
movq %rdx, %r13
movq %rsi, -0xa8(%rbp)
movq %rdi, %r15
leaq -0x580(%rbp), %r12
movq %r12, %rsi
callq 0x6180
leaq -0x120(%rbp), %rbx
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x62fd
leaq -0x100(%rbp), %rdi
andq $0x0, (%rdi)
movsd 0x10(%rbx), %xmm2
movaps 0x2219(%rip), %xmm0     # 0x7440
movaps %xmm2, %xmm1
xorps %xmm0, %xmm1
movlps %xmm1, 0x8(%rdi)
movsd 0x8(%rbx), %xmm1
movsd %xmm1, -0x30(%rbp)
movsd %xmm1, 0x10(%rdi)
movaps %xmm2, -0x60(%rbp)
movsd %xmm2, 0x18(%rdi)
andq $0x0, 0x20(%rdi)
movsd (%rbx), %xmm1
xorps (%rbx), %xmm0
movups %xmm0, 0x28(%rdi)
movsd %xmm1, -0x48(%rbp)
movsd %xmm1, 0x38(%rdi)
andq $0x0, 0x40(%rdi)
leaq -0x2b0(%rbp), %rsi
callq 0x637f
leaq -0x260(%rbp), %rax
xorl %ecx, %ecx
movq %r13, -0x68(%rbp)
cmpq $0x3, %rcx
je 0x52ad
xorl %esi, %esi
cmpq $0x3, %rsi
je 0x52a0
movsd (%r13,%rsi,8), %xmm0
movsd %xmm0, (%rax,%rsi,8)
incq %rsi
jmp 0x5289
incq %rcx
addq $0x30, %r13
addq $0x18, %rax
jmp 0x5281
leaq -0x260(%rbp), %r12
leaq -0x2b0(%rbp), %r14
leaq -0x530(%rbp), %rbx
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x5fcc
leaq -0x100(%rbp), %rdi
leaq -0x4e0(%rbp), %r13
movq %rbx, %rsi
movq %r13, %rdx
callq 0x5fcc
xorps %xmm0, %xmm0
leaq -0x490(%rbp), %rsi
movaps %xmm0, (%rsi)
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x10(%rsi)
movsd -0x40(%rbp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, (%rsi)
movsd %xmm0, 0x20(%rsi)
movsd %xmm0, 0x40(%rsi)
leaq -0x440(%rbp), %rbx
movq %r13, %rdi
movq %rbx, %rdx
callq 0x6034
leaq -0x3f0(%rbp), %r13
movq %rbx, %rdi
movq %r13, %rsi
callq 0x63b7
leaq -0x210(%rbp), %rbx
movq %r14, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x5fcc
leaq -0x1c0(%rbp), %rdx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x5fcc
movq -0x68(%rbp), %rax
addq $0x90, %rax
pushq $0x3
popq %rcx
leaq -0x170(%rbp), %rdx
cmpq $0x6, %rcx
je 0x53a2
xorl %esi, %esi
cmpq $0x3, %rsi
je 0x5395
movsd (%rax,%rsi,8), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
incq %rsi
jmp 0x5380
incq %rcx
addq $0x30, %rax
addq $0x18, %rdx
jmp 0x5378
leaq -0x170(%rbp), %rdi
leaq -0x210(%rbp), %rsi
leaq -0x3a0(%rbp), %r12
movq %r12, %rdx
callq 0x5fcc
movq -0x38(%rbp), %rax
movsd (%rax), %xmm4
movsd 0x8(%rax), %xmm6
subsd -0x48(%rbp), %xmm4
subsd -0x30(%rbp), %xmm6
movsd 0x10(%rax), %xmm5
subsd -0x60(%rbp), %xmm5
leaq -0x1c0(%rbp), %rdi
movsd 0x8(%rdi), %xmm0
mulsd %xmm6, %xmm0
movsd (%rdi), %xmm2
mulsd %xmm4, %xmm2
addsd %xmm0, %xmm2
movsd 0x10(%rdi), %xmm1
mulsd %xmm5, %xmm1
addsd %xmm2, %xmm1
movsd 0x20(%rdi), %xmm0
mulsd %xmm6, %xmm0
movsd 0x18(%rdi), %xmm3
mulsd %xmm4, %xmm3
addsd %xmm0, %xmm3
movsd 0x28(%rdi), %xmm2
mulsd %xmm5, %xmm2
addsd %xmm3, %xmm2
movsd 0x38(%rdi), %xmm0
movsd %xmm6, -0xa0(%rbp)
mulsd %xmm6, %xmm0
movsd 0x30(%rdi), %xmm3
movsd %xmm4, -0x48(%rbp)
mulsd %xmm4, %xmm3
addsd %xmm0, %xmm3
movsd 0x40(%rdi), %xmm0
movsd %xmm5, -0x40(%rbp)
mulsd %xmm5, %xmm0
addsd %xmm3, %xmm0
movsd (%r15), %xmm6
movsd 0x8(%r15), %xmm4
movapd %xmm1, %xmm7
mulsd %xmm4, %xmm7
movsd 0x1f94(%rip), %xmm3     # 0x7410
mulsd %xmm3, %xmm7
movapd %xmm0, %xmm9
movapd %xmm1, %xmm8
mulsd %xmm6, %xmm8
movapd %xmm0, %xmm5
movapd %xmm2, %xmm10
mulsd %xmm6, %xmm10
movapd %xmm0, %xmm11
mulsd %xmm6, %xmm0
subsd %xmm7, %xmm6
movsd 0x10(%r15), %xmm13
movapd %xmm2, %xmm7
mulsd %xmm13, %xmm7
mulsd %xmm3, %xmm7
subsd %xmm7, %xmm6
movsd 0x18(%r15), %xmm12
mulsd %xmm12, %xmm9
mulsd %xmm3, %xmm9
subsd %xmm9, %xmm6
mulsd %xmm3, %xmm8
addsd %xmm4, %xmm8
movapd %xmm2, %xmm7
mulsd %xmm12, %xmm7
mulsd %xmm3, %xmm7
subsd %xmm7, %xmm8
movsd (%r12), %xmm7
movsd %xmm7, -0x98(%rbp)
mulsd %xmm13, %xmm5
mulsd %xmm3, %xmm5
addsd %xmm8, %xmm5
mulsd %xmm3, %xmm10
addsd %xmm13, %xmm10
movapd %xmm1, %xmm7
mulsd %xmm12, %xmm7
mulsd %xmm3, %xmm7
addsd %xmm10, %xmm7
movsd 0x8(%r12), %xmm8
movsd %xmm8, -0x90(%rbp)
mulsd %xmm4, %xmm11
mulsd %xmm3, %xmm11
subsd %xmm11, %xmm7
movsd 0x10(%r12), %xmm8
movsd %xmm8, -0x38(%rbp)
mulsd %xmm13, %xmm1
movsd 0x18(%r12), %xmm8
movsd %xmm8, -0x88(%rbp)
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm12
movsd 0x20(%r12), %xmm1
movsd %xmm1, -0x80(%rbp)
mulsd %xmm4, %xmm2
movsd 0x28(%r12), %xmm1
movsd %xmm1, -0x60(%rbp)
mulsd %xmm3, %xmm2
addsd %xmm12, %xmm2
movsd 0x30(%r12), %xmm1
movsd %xmm1, -0x78(%rbp)
mulsd %xmm3, %xmm0
addsd %xmm2, %xmm0
movapd %xmm5, %xmm1
mulsd %xmm5, %xmm1
movapd %xmm6, %xmm2
mulsd %xmm6, %xmm2
addsd %xmm1, %xmm2
movapd %xmm7, %xmm1
mulsd %xmm7, %xmm1
addsd %xmm2, %xmm1
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd %xmm1, %xmm2
movsd 0x38(%r12), %xmm1
movsd %xmm1, -0x70(%rbp)
sqrtsd %xmm2, %xmm1
divsd %xmm1, %xmm6
movsd 0x40(%r12), %xmm2
movsd %xmm2, -0x30(%rbp)
movsd %xmm6, (%r15)
divsd %xmm1, %xmm5
movsd %xmm5, 0x8(%r15)
divsd %xmm1, %xmm7
movsd %xmm7, 0x10(%r15)
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%r15)
leaq -0x100(%rbp), %rbx
leaq -0x350(%rbp), %r13
movq %rbx, %rsi
movq %r13, %rdx
callq 0x5fcc
leaq -0x300(%rbp), %r15
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x5fcc
leaq -0x6a0(%rbp), %r12
xorl %ebx, %ebx
movl $0x120, %edx            # imm = 0x120
movq %r12, %rdi
xorl %esi, %esi
callq 0x2130
cmpq $0x3, %rbx
je 0x568f
xorl %eax, %eax
cmpq $0x3, %rax
je 0x5682
movsd (%r13,%rax,8), %xmm0
movsd %xmm0, (%r12,%rax,8)
incq %rax
jmp 0x566a
incq %rbx
addq $0x18, %r13
addq $0x30, %r12
jmp 0x5662
pushq $0x3
popq %rax
leaq -0x610(%rbp), %rcx
cmpq $0x6, %rax
je 0x56c4
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x56b7
movsd (%r15,%rdx,8), %xmm0
movsd %xmm0, (%rcx,%rdx,8)
incq %rdx
jmp 0x56a1
incq %rax
addq $0x18, %r15
addq $0x30, %rcx
jmp 0x5699
movsd -0xa0(%rbp), %xmm2
movsd -0x70(%rbp), %xmm1
mulsd %xmm2, %xmm1
movsd -0x48(%rbp), %xmm0
movsd -0x78(%rbp), %xmm3
mulsd %xmm0, %xmm3
addsd %xmm1, %xmm3
movsd -0x40(%rbp), %xmm1
movsd -0x30(%rbp), %xmm4
mulsd %xmm1, %xmm4
addsd %xmm3, %xmm4
movsd %xmm4, -0x30(%rbp)
movsd -0x80(%rbp), %xmm4
mulsd %xmm2, %xmm4
movsd -0x88(%rbp), %xmm3
mulsd %xmm0, %xmm3
addsd %xmm4, %xmm3
movsd -0x60(%rbp), %xmm4
mulsd %xmm1, %xmm4
addsd %xmm3, %xmm4
movsd %xmm4, -0x60(%rbp)
movsd -0x90(%rbp), %xmm3
mulsd %xmm2, %xmm3
movsd -0x98(%rbp), %xmm2
mulsd %xmm0, %xmm2
addsd %xmm3, %xmm2
movsd -0x38(%rbp), %xmm0
mulsd %xmm1, %xmm0
addsd %xmm2, %xmm0
movsd %xmm0, -0x38(%rbp)
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
leaq -0x7c0(%rbp), %rsi
movq %rax, (%rsi)
leaq -0x6a0(%rbp), %rdi
leaq -0xd60(%rbp), %rbx
movq %rbx, %rdx
callq 0x614a
leaq -0x7c0(%rbp), %r14
movl $0x120, %edx            # imm = 0x120
movq %r14, %rdi
xorl %esi, %esi
callq 0x2130
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, (%r14)
movq %rax, 0x38(%r14)
movq %rax, 0x70(%r14)
movq %rax, 0xa8(%r14)
movq %rax, 0xe0(%r14)
movq %rax, 0x118(%r14)
leaq -0xc40(%rbp), %r15
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x610f
leaq -0xb20(%rbp), %rbx
movq %r15, %rdi
movq -0x68(%rbp), %r12
movq %r12, %rsi
movq %rbx, %rdx
callq 0x60a7
leaq -0xa00(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x606f
leaq -0x8e0(%rbp), %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x610f
movabsq $0x3fe0000000000000, %rax # imm = 0x3FE0000000000000
leaq -0xb0(%rbp), %rsi
movq %rax, (%rsi)
movq %r15, %rdi
movq %r12, %rdx
callq 0x614a
movq -0xa8(%rbp), %rax
movsd -0x38(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movsd -0x60(%rbp), %xmm0
addsd 0x8(%rax), %xmm0
movsd %xmm0, 0x8(%rax)
movsd -0x30(%rbp), %xmm0
addsd 0x10(%rax), %xmm0
movsd %xmm0, 0x10(%rax)
addq $0xd38, %rsp            # imm = 0xD38
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq