// File: /workspace/github_cpp_makefile_star2_O3/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 %r12
pushq %rbx
subq $0x10, %rsp
movl %esi, %ebx
movl 0x8(%rdi), %eax
cmpl %esi, %eax
jle 0x24e8c
movq %rdi, %r14
leaq 0x7ddb9(%rip), %r15     # 0xa2b08
leal 0x1(,%rbx,2), %ebp
cmpl %eax, %ebp
jge 0x24e8c
leal 0x2(,%rbx,2), %r12d
cmpl %eax, %r12d
jge 0x24df0
movslq 0x10(%r14), %rax
testq %rax, %rax
js 0x24d95
movq (%r14), %rcx
movslq %ebp, %rdx
movq (%rcx,%rdx,8), %rdx
movsd (%rdx,%rax), %xmm1
movslq %r12d, %rdx
movq (%rcx,%rdx,8), %rcx
movsd (%rcx,%rax), %xmm0
jmp 0x24dcc
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x24de6
movslq %ebp, %rcx
movq (%r14), %rdx
movq 0x28(%r14), %rsi
movq (%rdx,%rcx,8), %rdi
callq *%rax
movsd %xmm0, 0x8(%rsp)
movslq %r12d, %rax
movq (%r14), %rcx
movq 0x28(%r14), %rsi
movq (%rcx,%rax,8), %rdi
callq *0x18(%r14)
movsd 0x8(%rsp), %xmm1
cmpl $0x0, 0x30(%r14)
je 0x24ddb
ucomisd %xmm0, %xmm1
ja 0x24de1
jmp 0x24df0
ucomisd %xmm1, %xmm0
jbe 0x24df0
movl %r12d, %ebp
jmp 0x24df0
movq %r15, %rdi
xorl %eax, %eax
callq 0x718f6
movslq 0x10(%r14), %rax
testq %rax, %rax
js 0x24e16
movq (%r14), %rcx
movslq %ebx, %rdx
movq (%rcx,%rdx,8), %rdx
movsd (%rdx,%rax), %xmm1
movslq %ebp, %rdx
movq (%rcx,%rdx,8), %rcx
movsd (%rcx,%rax), %xmm0
jmp 0x24e4d
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x24e7e
movslq %ebx, %rcx
movq (%r14), %rdx
movq 0x28(%r14), %rsi
movq (%rdx,%rcx,8), %rdi
callq *%rax
movsd %xmm0, 0x8(%rsp)
movslq %ebp, %rax
movq (%r14), %rcx
movq 0x28(%r14), %rsi
movq (%rcx,%rax,8), %rdi
callq *0x18(%r14)
movsd 0x8(%rsp), %xmm1
cmpl $0x0, 0x30(%r14)
je 0x24e5c
ucomisd %xmm1, %xmm0
jb 0x24e62
jmp 0x24e8c
ucomisd %xmm0, %xmm1
jae 0x24e8c
movq %r14, %rdi
movl %ebx, %esi
movl %ebp, %edx
callq 0x24c62
movl 0x8(%r14), %eax
movl %ebp, %ebx
cmpl %eax, %ebp
jl 0x24d4f
jmp 0x24e8e
leaq 0x7dc83(%rip), %rdi     # 0xa2b08
xorl %eax, %eax
callq 0x718f6
movl %ebx, %ebp
movl %ebp, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop