// File: /workspace/github_cpp_makefile_star2_O3/ke6jjj[P]r-dat/Track.cc
// Function: Track::Complete()
void
Track::Complete()
{
  ECC_C1 Vp;
    
  //
  // Iterate over each pair of blocks, correcting the C1 errors in each.
  //
  for (ECCFill_C1 c1_fill(*this); !c1_fill.End(); c1_fill.Next()) {
    //
    // Fill the error check vector.
    //
    Vp.Fill(c1_fill);
    
    //
    // Detect errors in this vector and correct them.
    //
    ECC_C1::Status ok = Vp.Correct();
    switch (ok) {
    case ECC_C1::NO_ERRORS:
      //
      // No errors. Nothing to be done.
      //
      break;
    case ECC_C1::UNCORRECTABLE:
      mC1UncorrectableErrors++;
    case ECC_C1::CORRECTED:
      //
      // There were errors, they were either corrected or the whole vector
      // has been marked bad. Put the corrected or invalidated data back into
      // the track.
      //
      mC1Errors++;
      Vp.Dump(c1_fill);
      break;
    }
  }

  //
  // Now iterate over each block 4-group to perform C2 error correction.
  //
  ECC_C2 Vq;

  for (ECCFill_C2 c2_fill(*this); !c2_fill.End(); c2_fill.Next()) {
    //
    // Fill the error check vector.
    //      
    Vq.Fill(c2_fill);

    //
    // Detect errors in this vector and correct them.
    //
    ECC_C2::Status c2ok = Vq.Correct();
    switch (c2ok) {
    case ECC_C2::NO_ERRORS:
      //
      // No errors. Nothing to be done.
      //
      break;
    case ECC_C2::CORRECTED:
      //
      // There were errors but they were corrected.
      // Put the corrected data back into the track.
      //
      Vq.Dump(c2_fill);
      break;
    case ECC_C2::UNCORRECTABLE:
      //
      // Slice was uncorrectable. Leave it as is. The next level of
      // error handling (interpolation for Audio, C3 for DDS) will have
      // to deal with it.
      //
      mC2UncorrectableErrors++;
      break;
    }
  }

  //
  // Error correction is complete. Now dather data from the sub-code blocks.
  //
  //
  // Now all the sub-code blocks have had as much error correction applied
  // as they ever will. Build the sub-code signature.
  //
  // Examine blocks 0x80-0x8f, the sub-code blocks, and build up a complete
  // list of the seven sub-code identifiers used in this track and the order
  // in which they are specified.
  //
  bool have_subcode_slot[7];
  for (size_t i = 0; i < 7; i++)
    have_subcode_slot[i] = false;
  
  for (size_t i = 0; i < 16; i++) {
    //
    // Odd-numbered blocks have three sub-codes. Even blocks have four.
    //
    size_t limit = (i & 1) ? 3 : 4;
    size_t block_number = 0x80 + i;
    int    slot_start = (i & 1) ? 4 : 0;

    //
    // Grab the ControlID and DataID from the first valid even-numbered
    // Subcode block.
    //
    if ((block_number & 1) == 0) {
      if (!mHaveControlID && mHeaderIsValid[block_number]) {
        mControlID = (mHeader[block_number] & 0xf0) >> 4;
        mDataID    = (mHeader[block_number] & 0x0f);
        mHaveControlID = true;
        mHaveDataID = true;
      }
    }
    
    //
    // Examine the individal sub-codes in this block.
    //
    for (size_t j = 0; j < limit; j++) {
      //
      // Point straight at the 8-byte item.
      //
      const uint8_t *item = &(mData[block_number][8*j]);
      const bool    *validity = &(mDataIsValid[block_number][8*j]);

      //
      // What's the sub-code id?
      //
      if (!validity[0])
        //
        // Sub-code id byte isn't even valid. Don't bother.
        //
        continue;

      uint8_t subcode_id = (item[0] & 0xf0) >> 4;

      //
      // Do we already have this sub-code?
      //
      if (mSubcodeIsValid[subcode_id] && have_subcode_slot[slot_start+j])
        //
        // Already have it. Skip it.
        //
        continue;

      //
      // Check the whole subcode item parity and make sure there
      // are no erasure symbols here.
      //
      uint8_t parity;
      bool valid;
      size_t k;
      for (k = 0, parity = 0, valid = true; k < 8; k++) {
        parity ^= item[k] & 0xff;
        valid = valid && validity[k];
      }

      if (!valid || parity != 0)
        //
        // Item parity or validity is bad.
        //
        continue;
    
      if (!mSubcodeIsValid[subcode_id]) {
        //
        // Copy the sub-code into its slot and mark it valid.
        //
        for (k = 0; k < 7; k++)
          mSubcode[subcode_id][k] = item[k];
    
        mSubcodeIsValid[subcode_id] = true;
      }
      
      if (!have_subcode_slot[slot_start+j]) {
        //
        // Copy the sub-code identifier into the subcode signature.
        //
        mSubcodeSignature[slot_start+j] = subcode_id;
        have_subcode_slot[slot_start+j] = true;
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdi, %rbx
leaq 0x94(%rsp), %rdi
callq 0x52fc
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x6328
leaq 0x50(%rsp), %r14
leaq 0x94(%rsp), %r15
movq %r14, %rdi
callq 0x63cc
testb %al, %al
jne 0x3695
movq %r15, %rdi
movq %r14, %rsi
callq 0x5300
movq %r15, %rdi
callq 0x53b2
cmpl $0x1, %eax
je 0x3679
cmpl $0x2, %eax
jne 0x368b
incq 0x25c0(%rbx)
incq 0x25b8(%rbx)
movq %r15, %rdi
movq %r14, %rsi
callq 0x55a0
movq %r14, %rdi
callq 0x6398
jmp 0x3649
leaq 0x50(%rsp), %rdi
callq 0x5cd4
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x6438
leaq 0x30(%rsp), %r14
leaq 0x50(%rsp), %r15
movq %r14, %rdi
callq 0x64e8
testb %al, %al
jne 0x36fd
movq %r15, %rdi
movq %r14, %rsi
callq 0x5cd8
movq %r15, %rdi
callq 0x5d8a
cmpl $0x1, %eax
je 0x36e8
cmpl $0x2, %eax
jne 0x36f3
incq 0x25c8(%rbx)
jmp 0x36f3
movq %r15, %rdi
movq %r14, %rsi
callq 0x5f68
movq %r14, %rdi
callq 0x64a2
jmp 0x36b6
xorl %r11d, %r11d
movl %r11d, 0x33(%rsp)
movl %r11d, 0x30(%rsp)
leaq 0x8f(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x128f(%rbx), %rax
movq %rax, (%rsp)
leaq 0x228f(%rbx), %r13
leaq 0x108f(%rbx), %r12
leaq 0x4(%rbx), %rax
movq %rax, 0x28(%rsp)
movl %r11d, %r10d
andl $0x1, %r10d
movq %r11, 0x20(%rsp)
orq $0x80, %r11
testq %r10, %r10
jne 0x378d
cmpb $0x0, 0x85(%rbx)
jne 0x378d
cmpb $0x1, 0x251f(%rbx,%r11)
jne 0x378d
movb 0x248f(%rbx,%r11), %al
movl %eax, %ecx
shrb $0x4, %cl
movb %cl, 0x84(%rbx)
andb $0xf, %al
movb %al, 0x86(%rbx)
movb $0x1, %al
movb %al, 0x85(%rbx)
movb %al, 0x87(%rbx)
movl $0x4, %r14d
subq %r10, %r14
shll $0x2, %r10d
shlq $0x5, %r11
movq 0x8(%rsp), %rax
leaq (%rax,%r11), %r15
addq (%rsp), %r11
movq %r12, 0x10(%rsp)
movq %r13, 0x18(%rsp)
xorl %ebp, %ebp
cmpb $0x1, (%r11,%rbp,8)
jne 0x3861
leaq (%r15,%rbp,8), %rax
movzbl (%rax), %r9d
shrl $0x4, %r9d
movb 0x74(%rbx,%r9), %cl
cmpb $0x1, %cl
jne 0x37e3
leaq (%r10,%rbp), %rax
cmpb $0x0, 0x30(%rsp,%rax)
jne 0x3861
movb $0x1, %dl
xorl %r8d, %r8d
xorl %eax, %eax
movl %eax, %esi
testb $0x1, %dl
je 0x37f8
movb (%r13,%r8), %dl
jmp 0x37fa
xorl %edx, %edx
movb -0x1200(%r13,%r8), %dil
movl %edi, %eax
xorb %sil, %al
incq %r8
cmpq $0x8, %r8
jne 0x37ea
cmpb %sil, %dil
sete %al
andb %al, %dl
cmpb $0x1, %dl
jne 0x3861
testb %cl, %cl
jne 0x3849
leaq (,%r9,8), %rcx
subq %r9, %rcx
addq 0x28(%rsp), %rcx
xorl %eax, %eax
movb (%r12,%rax), %dl
movb %dl, (%rcx,%rax)
incq %rax
cmpq $0x7, %rax
jne 0x3833
movb $0x1, 0x74(%rbx,%r9)
leaq (%r10,%rbp), %rax
cmpb $0x0, 0x30(%rsp,%rax)
jne 0x3861
movb %r9b, 0x88(%rbx,%rax)
movb $0x1, 0x30(%rsp,%rax)
incq %rbp
addq $0x8, %r13
addq $0x8, %r12
cmpq %r14, %rbp
jne 0x37b7
movq 0x20(%rsp), %r11
incq %r11
movq 0x18(%rsp), %r13
addq $0x20, %r13
movq 0x10(%rsp), %r12
addq $0x20, %r12
cmpq $0x10, %r11
jne 0x3738
leaq 0x50(%rsp), %rdi
callq 0x5cd6
leaq 0x94(%rsp), %rdi
callq 0x52fe
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x38cc
jmp 0x38db
jmp 0x38db
jmp 0x38cc
jmp 0x38db
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x5cd6
jmp 0x38de
movq %rax, %rbx
leaq 0x94(%rsp), %rdi
callq 0x52fe
movq %rbx, %rdi
callq 0x21d0
nop