// File: /workspace/github_cpp_makefile_star2_O3/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 0x6708
movl (%r15), %ecx
movl %eax, 0x8(%rsp)
movl %ecx, 0x4(%rsp)
cmpl %ecx, %eax
jne 0x6cd5
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x6720
testb %al, %al
je 0x6da8
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x6718
testb %al, %al
je 0x6ce6
leaq 0x3dd54(%r15), %r14
addq $0x3f3d0, %r15          # imm = 0x3F3D0
jmp 0x6d0d
leaq 0x3e50(%rip), %rdi      # 0xab2c
callq 0x21b0
jmp 0x6da8
movq %rbx, %rdi
callq 0x6720
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 0x66fc
movq %rax, %r12
movq %rbx, %rdi
callq 0x6700
movq %rax, %r13
xorl %ebp, %ebp
movq 0x10(%rsp), %rbx
movb (%r15,%rbp), %al
cmpb $0x1, (%r13,%rbp)
jne 0x6d7c
movb (%r12,%rbp), %cl
testb %al, %al
je 0x6d93
cmpb (%r14,%rbp), %cl
je 0x6d9c
movq %rbx, %rdi
callq 0x6708
movl %eax, 0xc(%rsp)
movq %rbx, %rdi
callq 0x6720
movzbl %al, %edx
movzbl (%r14,%rbp), %r8d
movzbl (%r12,%rbp), %r9d
leaq 0x3d57(%rip), %rdi      # 0xaac3
movl 0xc(%rsp), %esi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2040
jmp 0x6d9c
testb %al, %al
jne 0x6d9c
movb (%r12,%rbp), %al
movb %al, (%r14,%rbp)
movb (%r13,%rbp), %al
movb %al, (%r15,%rbp)
jmp 0x6d9c
movb %cl, (%r14,%rbp)
movb $0x1, (%r15,%rbp)
incq %rbp
cmpq $0x167c, %rbp           # imm = 0x167C
jne 0x6d2a
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