// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelScene.cpp
// Function: gaps::R3SurfelScene::RemoveLabel(gaps::R3SurfelLabel*)
void R3SurfelScene::
RemoveLabel(R3SurfelLabel *label)
{
  // Just checking
  assert(label);
  assert(label->scene == this);
  assert(label->scene_index >= 0);
  assert(labels.Kth(label->scene_index) == label);

  // Remove label properties from scene
  while (label->NLabelProperties() > 0) {
    R3SurfelLabelProperty *property = label->LabelProperty(label->NLabelProperties()-1);
    RemoveLabelProperty(property);
  }

  // Remove label relationships from scene
  while (label->NLabelRelationships() > 0) {
    R3SurfelLabelRelationship *relationship = label->LabelRelationship(label->NLabelRelationships()-1);
    RemoveLabelRelationship(relationship);
  }

  // Remove label assignments
  while (label->NLabelAssignments() > 0) {
    R3SurfelLabelAssignment *assignment = label->LabelAssignment(label->NLabelAssignments()-1);
    RemoveLabelAssignment(assignment);
  }

  // Update label
  label->UpdateBeforeRemove(this);

  // Remove label from parent
  if (label->parent) {
    label->parent->parts.Remove(label);
    label->parent = NULL;
  }

  // Remove label from parts
  for (int i = 0; i < label->NParts(); i++) {
    R3SurfelLabel *part = label->Part(i);
    part->parent = NULL;
  }

  // Remove parts from label
  label->parts.Empty();

  // Remove label from scene
  RNArrayEntry *entry = labels.KthEntry(label->scene_index);
  R3SurfelLabel *tail = labels.Tail();
  tail->scene_index = label->scene_index;
  labels.EntryContents(entry) = tail;
  labels.RemoveTail();
  label->scene_index = -1;
  label->scene = NULL;

  // Mark scene as dirty
  flags.Add(R3_SURFEL_SCENE_DIRTY_FLAG);
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x78(%rsp), %rdi
callq 0x30b20
cmpl $0x0, %eax
jle 0x1e8da
movq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x78(%rsp), %rdi
callq 0x30b20
movq 0x28(%rsp), %rdi
movl %eax, %esi
subl $0x1, %esi
callq 0x30b40
movq 0x30(%rsp), %rdi
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rsi
movq (%rdi), %rax
callq *0x58(%rax)
jmp 0x1e891
jmp 0x1e8dc
movq 0x78(%rsp), %rdi
callq 0x30b70
cmpl $0x0, %eax
jle 0x1e925
movq 0x78(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x78(%rsp), %rdi
callq 0x30b70
movq 0x20(%rsp), %rdi
movl %eax, %esi
subl $0x1, %esi
callq 0x30b90
movq 0x30(%rsp), %rdi
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rsi
movq (%rdi), %rax
callq *0x78(%rax)
jmp 0x1e8dc
jmp 0x1e927
movq 0x78(%rsp), %rdi
callq 0x30bc0
cmpl $0x0, %eax
jle 0x1e973
movq 0x78(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x78(%rsp), %rdi
callq 0x30bc0
movq 0x18(%rsp), %rdi
movl %eax, %esi
subl $0x1, %esi
callq 0x30be0
movq 0x30(%rsp), %rdi
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rsi
movq (%rdi), %rax
callq *0x88(%rax)
jmp 0x1e927
movq 0x30(%rsp), %rsi
movq 0x78(%rsp), %rdi
callq 0x30c10
movq 0x78(%rsp), %rax
cmpq $0x0, 0x18(%rax)
je 0x1e9b2
movq 0x78(%rsp), %rax
movq 0x18(%rax), %rdi
addq $0x20, %rdi
movq 0x78(%rsp), %rsi
callq 0x30c20
movq 0x78(%rsp), %rax
movq $0x0, 0x18(%rax)
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x78(%rsp), %rdi
callq 0x30c50
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x1ea03
movq 0x78(%rsp), %rdi
movl 0x5c(%rsp), %esi
callq 0x30c70
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq $0x0, 0x18(%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x1e9ba
movq 0x78(%rsp), %rdi
addq $0x20, %rdi
xorl %esi, %esi
callq 0x142550
movq 0x30(%rsp), %rdi
addq $0x20, %rdi
movq 0x78(%rsp), %rax
movl 0x10(%rax), %esi
callq 0x1c080
movq 0x30(%rsp), %rdi
movq %rax, 0x48(%rsp)
addq $0x20, %rdi
callq 0x30ca0
movq 0x30(%rsp), %rdi
movq %rax, 0x40(%rsp)
movq 0x78(%rsp), %rax
movl 0x10(%rax), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x10(%rax)
movq 0x40(%rsp), %rax
movq %rax, (%rsp)
addq $0x20, %rdi
movq 0x48(%rsp), %rsi
callq 0x30cb0
movq (%rsp), %rcx
movq 0x30(%rsp), %rdi
movq %rcx, (%rax)
addq $0x20, %rdi
callq 0x1c0c0
movq 0x30(%rsp), %rax
movq 0x78(%rsp), %rcx
movl $0xffffffff, 0x10(%rcx) # imm = 0xFFFFFFFF
movq 0x78(%rsp), %rcx
movq $0x0, 0x8(%rcx)
addq $0x1e8, %rax            # imm = 0x1E8
movq %rax, 0x8(%rsp)
leaq 0x38(%rsp), %rdi
movl $0x1, %esi
callq 0x1b970
movq 0x8(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x1c2d0
addq $0x88, %rsp
retq
nopw %cs:(%rax,%rax)