// File: /workspace/github_cpp_makefile_star2_O2/ke6jjj[P]r-dat/BasicGroup.cc
// Function: BasicGroup::AddSubFrame(DDSGroup1 const&)
bool
BasicGroup::AddSubFrame(const DDSGroup1& frame)
{
  //
  // Make certain that this sub-frame belongs to this group.
  //
  if (frame.BasicGroupID() != mBasicGroupID) {
    printf("Attempt to add sub-frame to wrong basic group!\n");
    return false;
  }

  //
  // Don't dump sub-frame zero, if you ever encounter one.
  //
  if (frame.SubFrameID() == 0)
    return true;

  //
  // ECC frames get special treatment.
  //
  bool is_ecc = frame.IsECCFrame();
    
  //
  // Derive pointers into the appropriate place in out data to capture
  // this new frame.
  // 
  uint8_t *data_existing;
  bool *valid_existing;
  if (!is_ecc) {
    //
    // Normal data frame. Frames are numbered starting at 1 and are
    // about 5k in size.
    //
    size_t pos = DDSGroup1::kSize * (frame.SubFrameID() - 1);
    data_existing = &mData[pos];
    valid_existing = &mDataIsValid[pos];
  } else {
    //
    // ECC frames go into their own buffer.
    //
    data_existing = &mECCData[0];
    valid_existing = &mECCDataIsValid[0];
  }
  
  //
  // Get pointers to the frame data and validity buffer.
  //
  const DDSGroup1::DataArray& data = frame.Data();
  const DDSGroup1::ValidArray& valid = frame.Valid();
  
  //
  // Incorporate the frame's data as long as it is better than the existing
  // data.
  //
  for (size_t i = 0; i < DDSGroup1::kSize; i++) {
    if (valid[i] && !valid_existing[i]) {
      //
      // New valid data that replaces invalid data. Use it!
      //
      data_existing[i] = data[i];
      valid_existing[i] = true;
    } else if (valid[i] && valid_existing[i]) {
      //
      // We already have data at this spot. Check to make sure that the
      // update is the same.
      //
      if (data[i] != data_existing[i]) {
        //
        // Oh oh, mistmatch.
        //
        printf("Reread mismatch at Basic Group %d, SubGroup %d, offset %zd"
               ". Old/New = %02x/%02x. Keeping existing data.\n",
               frame.BasicGroupID(), frame.SubFrameID(), i, data_existing[i],
               data[i]);
      }
    } else if (!valid[i] && !valid_existing[i]) {
      //
      // Existing data is invalid and incoming data is also marked invalid,
      // but the incoming data likely has more information than the existing.
      // Sometimes entire blocks get marked invalid due to error detection
      // but a good deal of the block is actually correct. We have enough
      // storage space to represent the data invalidity yet retain the likely
      // correct data.
      //
      // Copy in the supposedly invalid data.
      //
      data_existing[i] = data[i];
      valid_existing[i] = valid[i];
    }
  }
  
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r15
movq %rsi, %rdi
callq 0x64e6
movl (%r15), %ecx
movl %eax, 0x8(%rsp)
movl %ecx, 0x4(%rsp)
cmpl %ecx, %eax
jne 0x6a71
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x64fe
testb %al, %al
je 0x6a7d
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x64f6
testb %al, %al
je 0x6a97
leaq 0x3dd54(%r15), %r14
addq $0x3f3d0, %r15          # imm = 0x3F3D0
jmp 0x6abe
leaq 0x30a4(%rip), %rdi      # 0x9b1c
callq 0x21c0
movl 0x4(%rsp), %eax
cmpl %eax, 0x8(%rsp)
sete %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x64fe
movzbl %al, %eax
imulq $0x167c, %rax, %rax     # imm = 0x167C
leaq (%rax,%r15), %r14
addq $-0x1678, %r14          # imm = 0xE988
addq %rax, %r15
addq $0x1d830, %r15          # imm = 0x1D830
movq %rbx, %rdi
callq 0x64da
movq %rax, %r12
movq %rbx, %rdi
callq 0x64de
movq %rax, %r13
xorl %ebp, %ebp
movq 0x10(%rsp), %rbx
cmpq $0x167c, %rbp           # imm = 0x167C
je 0x6a7d
movb (%r15,%rbp), %al
cmpb $0x1, (%r13,%rbp)
jne 0x6b36
movb (%r12,%rbp), %cl
testb %al, %al
je 0x6b4d
cmpb (%r14,%rbp), %cl
je 0x6b56
movq %rbx, %rdi
callq 0x64e6
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
callq 0x64fe
movzbl %al, %edx
movzbl (%r14,%rbp), %r8d
movzbl (%r12,%rbp), %r9d
leaq 0x2f8d(%rip), %rdi      # 0x9ab3
movl 0xc(%rsp), %esi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2040
jmp 0x6b56
testb %al, %al
jne 0x6b56
movb (%r12,%rbp), %al
movb %al, (%r14,%rbp)
movb (%r13,%rbp), %al
movb %al, (%r15,%rbp)
jmp 0x6b56
movb %cl, (%r14,%rbp)
movb $0x1, (%r15,%rbp)
incq %rbp
jmp 0x6adb
nop