// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/../../pkgs/RNBasics/RNHeap.cpp
// Function: gaps::RNHeap<gaps::R3MeshBoundaryVertexData*>::BubbleDown(int)
int RNHeap<PtrType>::
BubbleDown(int index)
{
  // Move data down tree to rightful place
  while (index < nentries) {
    int child_index;
    int child1_index = (index << 1) + 1;
    int child2_index = child1_index + 1;
    RNScalar value = 0;
    RNScalar child_value = 0;
    RNScalar child1_value = 0;
    RNScalar child2_value =0;

    // Check child indices
    if (child1_index >= nentries) {
      break;
    }
    else if (child2_index >= nentries) {
      child_index = child1_index;
    }
    else {
      // Get children values
      if (value_offset >= 0) {
        child1_value = *((RNScalar *) ((unsigned char *) entries[child1_index] + value_offset));
        child2_value = *((RNScalar *) ((unsigned char *) entries[child2_index] + value_offset));
      }
      else if (value_callback) {
        child1_value = (*value_callback)(entries[child1_index], callback_data);
        child2_value = (*value_callback)(entries[child2_index], callback_data);
      }
      else RNAbort("No heap value callback or offset\n");

      // Determine child index
      child_index = child1_index;
      if (least_first) { if (child2_value < child1_value) child_index = child2_index; }
      else { if (child2_value > child1_value) child_index = child2_index; }
    }

    // Get values
    if (value_offset >= 0) {
      value  = *((RNScalar *) ((unsigned char *) entries[index] + value_offset));
      child_value  = *((RNScalar *) ((unsigned char *) entries[child_index] + value_offset));
    }
    else if (value_callback) {
      value = (*value_callback)(entries[index], callback_data);
      child_value = (*value_callback)(entries[child_index], callback_data);
    }
    else RNAbort("No heap value callback or offset\n");

    // Compare values
    if (least_first) { if (value <= child_value) break; }
    else { if (value >= child_value) break; }

    // Swap values
    Swap(index, child_index);

    // Move down tree
    index = child_index;
  }

  // Return new index
  return index;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movl %esi, 0x3c(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rcx
movl 0x3c(%rsp), %eax
cmpl 0x8(%rcx), %eax
jge 0x3bf00
movq 0x8(%rsp), %rcx
movl 0x3c(%rsp), %eax
shll %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x8(%rcx), %eax
jl 0x3bcf8
jmp 0x3bf00
movq 0x8(%rsp), %rcx
movl 0x30(%rsp), %eax
cmpl 0x8(%rcx), %eax
jl 0x3bd13
movl 0x34(%rsp), %eax
movl %eax, 0x38(%rsp)
jmp 0x3be04
movq 0x8(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jl 0x3bd5b
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movslq 0x34(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movslq 0x10(%rcx), %rdx
movsd (%rax,%rdx), %xmm0
movsd %xmm0, 0x18(%rsp)
movq (%rcx), %rax
movslq 0x30(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movslq 0x10(%rcx), %rcx
movsd (%rax,%rcx), %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0x3bdbb
movq 0x8(%rsp), %rax
cmpq $0x0, 0x18(%rax)
je 0x3bdab
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rax
movq (%rcx), %rdx
movslq 0x34(%rsp), %rsi
movq (%rdx,%rsi,8), %rdi
movq 0x28(%rcx), %rsi
callq *%rax
movq 0x8(%rsp), %rcx
movsd %xmm0, 0x18(%rsp)
movq 0x18(%rcx), %rax
movq (%rcx), %rdx
movslq 0x30(%rsp), %rsi
movq (%rdx,%rsi,8), %rdi
movq 0x28(%rcx), %rsi
callq *%rax
movsd %xmm0, 0x10(%rsp)
jmp 0x3bdb9
leaq 0xf7d8f(%rip), %rdi     # 0x133b41
movb $0x0, %al
callq 0xd4e20
jmp 0x3bdbb
movq 0x8(%rsp), %rax
movl 0x34(%rsp), %ecx
movl %ecx, 0x38(%rsp)
cmpl $0x0, 0x30(%rax)
je 0x3bdea
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3bde8
movl 0x30(%rsp), %eax
movl %eax, 0x38(%rsp)
jmp 0x3be02
movsd 0x10(%rsp), %xmm0
ucomisd 0x18(%rsp), %xmm0
jbe 0x3be00
movl 0x30(%rsp), %eax
movl %eax, 0x38(%rsp)
jmp 0x3be02
jmp 0x3be04
jmp 0x3be06
movq 0x8(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jl 0x3be4e
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movslq 0x3c(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movslq 0x10(%rcx), %rdx
movsd (%rax,%rdx), %xmm0
movsd %xmm0, 0x28(%rsp)
movq (%rcx), %rax
movslq 0x38(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movslq 0x10(%rcx), %rcx
movsd (%rax,%rcx), %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x3beae
movq 0x8(%rsp), %rax
cmpq $0x0, 0x18(%rax)
je 0x3be9e
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rax
movq (%rcx), %rdx
movslq 0x3c(%rsp), %rsi
movq (%rdx,%rsi,8), %rdi
movq 0x28(%rcx), %rsi
callq *%rax
movq 0x8(%rsp), %rcx
movsd %xmm0, 0x28(%rsp)
movq 0x18(%rcx), %rax
movq (%rcx), %rdx
movslq 0x38(%rsp), %rsi
movq (%rdx,%rsi,8), %rdi
movq 0x28(%rcx), %rsi
callq *%rax
movsd %xmm0, 0x20(%rsp)
jmp 0x3beac
leaq 0xf7c9c(%rip), %rdi     # 0x133b41
movb $0x0, %al
callq 0xd4e20
jmp 0x3beae
movq 0x8(%rsp), %rax
cmpl $0x0, 0x30(%rax)
je 0x3becf
movsd 0x28(%rsp), %xmm1
movsd 0x20(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x3becd
jmp 0x3bf00
jmp 0x3bee1
movsd 0x28(%rsp), %xmm0
ucomisd 0x20(%rsp), %xmm0
jb 0x3bedf
jmp 0x3bf00
jmp 0x3bee1
movq 0x8(%rsp), %rdi
movl 0x3c(%rsp), %esi
movl 0x38(%rsp), %edx
callq 0x3bac0
movl 0x38(%rsp), %eax
movl %eax, 0x3c(%rsp)
jmp 0x3bc97
movl 0x3c(%rsp), %eax
addq $0x48, %rsp
retq
nopl (%rax)