// File: onfe[P]Completionist/src/SegmentTree.cpp
// Function: SegmentTree::build(unsigned int, unsigned int, unsigned int, std::vector<unsigned int, std::allocator<unsigned int>>)
uip_t SegmentTree::build (
  uint32_t heapidx,
  uint32_t idx1,
  uint32_t idx2,
  std::vector<uint32_t> weights
) {
  uint32_t midpoint = idx1 + (idx2 - idx1) / 2;
  // if idx1 = idx 2, must be pointing at 1 element.
  if (idx1 == idx2) {
    // add the pair [weight, index] to the heap.
    this->heap[heapidx] = uip_t(weights[idx1], idx1);
    return this->heap[heapidx];

  } else {
    // otherwise, get the child nodes (recursive step)
    uip_t left = this->build(heapidx*2 + 1, idx1, midpoint, weights);
    uip_t right = this->build(heapidx*2 + 2, midpoint+1, idx2, weights);
    // and set the values of this node to the best child weight
    this->heap[heapidx] = (left.first > right.first ? left : right);
    return this->heap[heapidx];
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %r8, -0x98(%rbp)
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0x18(%rbp), %eax
movl -0x1c(%rbp), %ecx
subl -0x18(%rbp), %ecx
shrl %ecx
addl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jne 0x9aa9
movq -0x98(%rbp), %rdi
movl -0x18(%rbp), %eax
movl %eax, %esi
callq 0x9ef0
movq %rax, %rsi
leaq -0x34(%rbp), %rdi
leaq -0x18(%rbp), %rdx
callq 0x9f10
movq -0x90(%rbp), %rdi
addq $0x8, %rdi
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x9f40
movq %rax, %rdi
leaq -0x34(%rbp), %rsi
callq 0x5850
movq -0x90(%rbp), %rdi
addq $0x8, %rdi
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x9f40
movq (%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x9c48
movq -0x98(%rbp), %rsi
movl -0x14(%rbp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %eax
movl %eax, -0xb4(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0xb0(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0xac(%rbp)
leaq -0x58(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x2f50
movq -0x90(%rbp), %rdi
movl -0xb4(%rbp), %esi
movl -0xb0(%rbp), %edx
movl -0xac(%rbp), %ecx
movq -0xa8(%rbp), %r8
callq 0x9a00
movq %rax, -0xa0(%rbp)
jmp 0x9b0f
movq -0xa0(%rbp), %rax
movq %rax, -0x3c(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x3040
movq -0x98(%rbp), %rsi
movl -0x14(%rbp), %ecx
movl %ecx, %eax
leal 0x2(%rax,%rax), %eax
movl %eax, -0xd4(%rbp)
movl -0x2c(%rbp), %eax
incl %eax
movl %eax, -0xd0(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0xcc(%rbp)
leaq -0x88(%rbp), %rdi
movq %rdi, -0xc8(%rbp)
callq 0x2f50
movq -0x90(%rbp), %rdi
movl -0xd4(%rbp), %esi
movl -0xd0(%rbp), %edx
movl -0xcc(%rbp), %ecx
movq -0xc8(%rbp), %r8
callq 0x9a00
movq %rax, -0xc0(%rbp)
jmp 0x9b8e
movq -0xc0(%rbp), %rax
movq %rax, -0x6c(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x3040
movl -0x3c(%rbp), %eax
cmpl -0x6c(%rbp), %eax
jbe 0x9bba
leaq -0x3c(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0x9bc7
leaq -0x6c(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0x9bc7
movq -0x90(%rbp), %rdi
movq -0xe0(%rbp), %rax
movq %rax, -0xe8(%rbp)
addq $0x8, %rdi
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x9f40
movq -0xe8(%rbp), %rsi
movq %rax, %rdi
callq 0x9f60
movq -0x90(%rbp), %rdi
addq $0x8, %rdi
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x9f40
movq (%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x9c48
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x3040
jmp 0x9c55
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x3040
jmp 0x9c55
movq -0x8(%rbp), %rax
addq $0xf0, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x2260
nop