// File: /workspace/github_cpp_makefile_star2_O2/kingufl[P]Rmapper/src/KDTree.cpp
// Function: KDTree::make_tree(__gnu_cxx::__normal_iterator<std::pair<std::vector<int, std::allocator<int>>, unsigned long>*, std::vector<std::pair<std::vector<int, std::allocator<int>>, unsigned long>, std::allocator<std::pair<std::vector<int, std::allocator<int>>, unsigned long>>>> const&, __gnu_cxx::__normal_iterator<std::pair<std::vector<int, std::allocator<int>>, unsigned long>*, std::vector<std::pair<std::vector<int, std::allocator<int>>, unsigned long>, std::allocator<std::pair<std::vector<int, std::allocator<int>>, unsigned long>>>> const&, unsigned long const&, unsigned long const&)
KDNodePtr KDTree::make_tree(const pointIndexArr::iterator &begin,  //
                            const pointIndexArr::iterator &end,    //
                            const size_t &length,                  //
                            const size_t &level                    //
) {
    if (begin == end) {
        return NewKDNodePtr();  // empty tree
    }

    size_t dim = begin->first.size();

    if (length > 1) {
        sort_on_idx(begin, end, level);
    }

    auto middle = begin + (length / 2);

    auto l_begin = begin;
    auto l_end = middle;
    auto r_begin = middle + 1;
    auto r_end = end;

    size_t l_len = length / 2;
    size_t r_len = length - l_len - 1;

    KDNodePtr left;
    if (l_len > 0 && dim > 0) {
        left = make_tree(l_begin, l_end, l_len, (level + 1) % dim);
    } else {
        left = leaf;
    }
    KDNodePtr right;
    if (r_len > 0 && dim > 0) {
        right = make_tree(r_begin, r_end, r_len, (level + 1) % dim);
    } else {
        right = leaf;
    }

    // KDNode result = KDNode();
    return std::make_shared< KDNode >(*middle, left, right);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, -0x68(%rbp)
movq %rdi, %rbx
movq (%rdx), %rdx
movq (%rcx), %rcx
cmpq %rcx, %rdx
je 0x3864
movq %r8, %r13
movq %rbx, -0x58(%rbp)
movq (%rdx), %rax
movq 0x8(%rdx), %r14
movq %r14, %rbx
movq %rax, -0x70(%rbp)
subq %rax, %rbx
movq (%r8), %rax
cmpq $0x2, %rax
movq %r9, -0x60(%rbp)
jb 0x37b0
movq (%r9), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x464c
movq -0x60(%rbp), %r9
movq (%r13), %rax
movq (%r12), %rdx
movq (%r15), %rcx
sarq $0x2, %rbx
movq %rax, %r12
shrq %r12
movq %r12, %rsi
shlq $0x5, %rsi
leaq (%rdx,%rsi), %r15
movq %rdx, -0xb0(%rbp)
movq %r15, -0xa8(%rbp)
addq %rsi, %rdx
addq $0x20, %rdx
movq %rdx, -0xa0(%rbp)
movq %rcx, -0x98(%rbp)
movq %r12, -0x90(%rbp)
notq %r12
addq %rax, %r12
movq %r12, -0x88(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x50(%rbp)
cmpq $0x2, %rax
setb %al
cmpq -0x70(%rbp), %r14
sete %cl
orb %al, %cl
jne 0x3871
movq (%r9), %rax
incq %rax
xorl %edx, %edx
divq %rbx
leaq -0x80(%rbp), %r9
movq %rdx, (%r9)
leaq -0x40(%rbp), %rdi
leaq -0xb0(%rbp), %rdx
leaq -0xa8(%rbp), %rcx
leaq -0x90(%rbp), %r8
movq -0x68(%rbp), %r13
movq %r13, %rsi
callq 0x373e
leaq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x5790
leaq -0x38(%rbp), %rdi
callq 0x4978
jmp 0x3882
movq %rbx, %rdi
callq 0x4630
jmp 0x3925
movq -0x68(%rbp), %r13
leaq 0x10(%r13), %rsi
leaq -0x50(%rbp), %rdi
callq 0x487e
cmpq -0x70(%rbp), %r14
sete %al
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
testq %r12, %r12
sete %cl
orb %al, %cl
movq -0x60(%rbp), %rax
jne 0x38ec
movq (%rax), %rax
incq %rax
xorl %edx, %edx
divq %rbx
leaq -0xb8(%rbp), %r9
movq %rdx, (%r9)
leaq -0x80(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
leaq -0x98(%rbp), %rcx
leaq -0x88(%rbp), %r8
movq %r13, %rsi
callq 0x373e
leaq -0x40(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x5790
leaq -0x78(%rbp), %rdi
callq 0x4978
jmp 0x38fc
addq $0x10, %r13
leaq -0x40(%rbp), %rdi
movq %r13, %rsi
callq 0x487e
movq -0x58(%rbp), %rbx
leaq -0x50(%rbp), %rdx
leaq -0x40(%rbp), %rcx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x467f
leaq -0x38(%rbp), %rdi
callq 0x4978
leaq -0x48(%rbp), %rdi
callq 0x4978
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3941
movq %rax, %rbx
jmp 0x394d
movq %rax, %rbx
leaq -0x38(%rbp), %rdi
callq 0x4978
leaq -0x48(%rbp), %rdi
callq 0x4978
movq %rbx, %rdi
callq 0x3380