// File: /workspace/github_cpp_makefile_star2_O1/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, %r15d
movq %rdi, %rbx
leaq 0x7dc32(%rip), %r14     # 0xa2b0b
movl 0x8(%rbx), %eax
cmpl %eax, %r15d
jge 0x2502e
leal 0x1(,%r15,2), %ebp
cmpl %eax, %ebp
jge 0x2501c
leal 0x2(,%r15,2), %r12d
cmpl %eax, %r12d
jge 0x24f81
movslq 0x10(%rbx), %rax
testq %rax, %rax
js 0x24f28
movq (%rbx), %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 0x24f72
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x24f60
movslq %ebp, %rcx
movq (%rbx), %rdx
movq 0x28(%rbx), %rsi
movq (%rdx,%rcx,8), %rdi
callq *%rax
movsd %xmm0, 0x8(%rsp)
movslq %r12d, %rax
movq (%rbx), %rcx
movq 0x28(%rbx), %rsi
movq (%rcx,%rax,8), %rdi
callq *0x18(%rbx)
movsd 0x8(%rsp), %xmm1
jmp 0x24f72
movq %r14, %rdi
xorl %eax, %eax
callq 0x72942
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
cmpl $0x0, 0x30(%rbx)
je 0x24fdf
ucomisd %xmm0, %xmm1
jbe 0x24f81
movl %r12d, %ebp
movslq 0x10(%rbx), %rax
testq %rax, %rax
js 0x24fa7
movq (%rbx), %rcx
movslq %r15d, %rdx
movq (%rcx,%rdx,8), %rdx
movsd (%rdx,%rax), %xmm1
movslq %ebp, %rdx
movq (%rcx,%rdx,8), %rcx
movsd (%rcx,%rax), %xmm0
jmp 0x24ff9
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x24fe7
movslq %r15d, %rcx
movq (%rbx), %rdx
movq 0x28(%rbx), %rsi
movq (%rdx,%rcx,8), %rdi
callq *%rax
movsd %xmm0, 0x8(%rsp)
movslq %ebp, %rax
movq (%rbx), %rcx
movq 0x28(%rbx), %rsi
movq (%rcx,%rax,8), %rdi
callq *0x18(%rbx)
movsd 0x8(%rsp), %xmm1
jmp 0x24ff9
ucomisd %xmm1, %xmm0
ja 0x24f7e
jmp 0x24f81
movq %r14, %rdi
xorl %eax, %eax
callq 0x72942
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
cmpl $0x0, 0x30(%rbx)
je 0x25016
ucomisd %xmm1, %xmm0
jae 0x2501c
movq %rbx, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0x24df6
movb $0x1, %al
jmp 0x25021
ucomisd %xmm0, %xmm1
jb 0x25005
xorl %eax, %eax
movl %r15d, %ebp
movl %ebp, %r15d
testb %al, %al
jne 0x24ed9
jmp 0x25031
movl %r15d, %ebp
movl %ebp, %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq