// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebx
movq %rdi, %r14
leaq 0x710a9(%rip), %r15     # 0x93ab8
movl 0x8(%r14), %eax
cmpl %eax, %ebx
jge 0x22b3b
leal 0x1(,%rbx,2), %ebp
cmpl %eax, %ebp
jge 0x22b3b
leal 0x2(,%rbx,2), %r12d
cmpl %eax, %r12d
jge 0x22ab0
movslq 0x10(%r14), %rcx
movslq %ebp, %rax
movslq %r12d, %r13
testq %rcx, %rcx
js 0x22a5d
movq (%r14), %rdx
movq (%rdx,%rax,8), %rax
movsd (%rax,%rcx), %xmm1
movq (%rdx,%r13,8), %rax
movsd (%rax,%rcx), %xmm0
jmp 0x22a8c
movq 0x18(%r14), %rcx
testq %rcx, %rcx
je 0x22aa6
movq (%r14), %rdx
movq 0x28(%r14), %rsi
movq (%rdx,%rax,8), %rdi
callq *%rcx
movsd %xmm0, (%rsp)
movq (%r14), %rax
movq 0x28(%r14), %rsi
movq (%rax,%r13,8), %rdi
callq *0x18(%r14)
movsd (%rsp), %xmm1
cmpl $0x0, 0x30(%r14)
je 0x22a9b
ucomisd %xmm0, %xmm1
ja 0x22aa1
jmp 0x22ab0
ucomisd %xmm1, %xmm0
jbe 0x22ab0
movl %r12d, %ebp
jmp 0x22ab0
movq %r15, %rdi
xorl %eax, %eax
callq 0x680f0
movslq 0x10(%r14), %rcx
movslq %ebx, %rax
movslq %ebp, %r12
testq %rcx, %rcx
js 0x22ad6
movq (%r14), %rdx
movq (%rdx,%rax,8), %rax
movsd (%rax,%rcx), %xmm1
movq (%rdx,%r12,8), %rax
movsd (%rax,%rcx), %xmm0
jmp 0x22b05
movq 0x18(%r14), %rcx
testq %rcx, %rcx
je 0x22b2d
movq (%r14), %rdx
movq 0x28(%r14), %rsi
movq (%rdx,%rax,8), %rdi
callq *%rcx
movsd %xmm0, (%rsp)
movq (%r14), %rax
movq 0x28(%r14), %rsi
movq (%rax,%r12,8), %rdi
callq *0x18(%r14)
movsd (%rsp), %xmm1
cmpl $0x0, 0x30(%r14)
je 0x22b14
ucomisd %xmm1, %xmm0
jb 0x22b1a
jmp 0x22b3b
ucomisd %xmm0, %xmm1
jae 0x22b3b
movq %r14, %rdi
movl %ebx, %esi
movl %ebp, %edx
callq 0x2292e
movl %ebp, %ebx
jmp 0x22a0f
leaq 0x70f84(%rip), %rdi     # 0x93ab8
xorl %eax, %eax
callq 0x680f0
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq