// File: /workspace/github_cpp_makefile_star2_O3/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 $0xb8, %rsp
movq %rsi, -0x68(%rbp)
movq %rdi, %rbx
movq (%rdx), %r12
movq (%rcx), %r13
cmpq %r13, %r12
je 0x3b8d
movq %rbx, -0x58(%rbp)
movq (%r12), %rax
movq 0x8(%r12), %rbx
movq %rbx, -0x70(%rbp)
movq %rax, -0xa8(%rbp)
subq %rax, %rbx
movq (%r8), %rax
cmpq $0x2, %rax
movq %r9, -0x60(%rbp)
jb 0x3aa6
movq %rcx, %r15
movq %rdx, %r14
movq (%r9), %rsi
movq %rsi, -0x98(%rbp)
movq %r13, %rax
subq %r12, %rax
sarq $0x5, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x16f2(%rip), %rax      # 0x5138
leaq -0x90(%rbp), %rcx
movq %rax, (%rcx)
xorl %eax, %eax
movq %rax, 0x8(%rcx)
movq %rsi, 0x10(%rcx)
movq %r12, %rdi
movq %r13, %rsi
movq %r8, -0xa0(%rbp)
callq 0x5184
leaq -0x40(%rbp), %rdx
leaq 0x16c1(%rip), %rax      # 0x5138
movq %rax, (%rdx)
xorl %eax, %eax
movq %rax, 0x8(%rdx)
movq -0x98(%rbp), %rax
movq %rax, 0x10(%rdx)
movq %r12, %rdi
movq %r13, %rsi
callq 0x52d8
movq -0xa0(%rbp), %rax
movq (%rax), %rax
movq (%r14), %r12
movq (%r15), %r13
movq -0x68(%rbp), %r14
sarq $0x2, %rbx
movq %rbx, %rsi
movq %rax, %rbx
shrq %rbx
movq %rbx, %rcx
shlq $0x5, %rcx
leaq (%r12,%rcx), %r15
movq %r12, -0xd8(%rbp)
movq %r15, -0xd0(%rbp)
addq %r12, %rcx
addq $0x20, %rcx
movq %rcx, -0xc8(%rbp)
movq %r13, -0xc0(%rbp)
movq %rbx, -0xb8(%rbp)
notq %rbx
addq %rax, %rbx
movq %rbx, -0xb0(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
cmpq $0x2, %rax
setb %al
movq -0xa8(%rbp), %r13
cmpq %r13, -0x70(%rbp)
sete %cl
orb %al, %cl
jne 0x3bd6
movq -0x60(%rbp), %rax
movq (%rax), %rax
incq %rax
xorl %edx, %edx
movq %rsi, %r12
divq %rsi
leaq -0x50(%rbp), %r9
movq %rdx, (%r9)
leaq -0x40(%rbp), %rdi
leaq -0xd8(%rbp), %rdx
leaq -0xd0(%rbp), %rcx
leaq -0xb8(%rbp), %r8
movq %r14, %rsi
callq 0x39c2
movaps -0x40(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0x40(%rbp)
movq -0x88(%rbp), %rdi
movaps %xmm0, -0x90(%rbp)
testq %rdi, %rdi
je 0x3bf1
callq 0x50cc
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x3bf1
callq 0x50cc
jmp 0x3bf1
movl $0x50, %edi
callq 0x3250
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x8(%rax)
leaq 0x1516c(%rip), %rcx     # 0x18d18
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movups %xmm0, 0x28(%rax)
movups %xmm0, 0x38(%rax)
movq $0x0, 0x48(%rax)
movq %rax, 0x8(%rbx)
addq $0x10, %rax
movq %rax, (%rbx)
jmp 0x3cd4
movq %rsi, %r12
movq 0x10(%r14), %rax
leaq -0x88(%rbp), %rdi
movq %rax, -0x8(%rdi)
leaq 0x18(%r14), %rsi
callq 0x5074
cmpq %r13, -0x70(%rbp)
sete %al
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
testq %rbx, %rbx
sete %cl
orb %al, %cl
movq -0x60(%rbp), %rax
jne 0x3c74
movq (%rax), %rax
incq %rax
xorl %edx, %edx
divq %r12
leaq -0xe0(%rbp), %r9
movq %rdx, (%r9)
leaq -0x50(%rbp), %rdi
leaq -0xc8(%rbp), %rdx
leaq -0xc0(%rbp), %rcx
leaq -0xb0(%rbp), %r8
movq %r14, %rsi
callq 0x39c2
movq -0x58(%rbp), %rbx
movaps -0x50(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0x50(%rbp)
movq -0x38(%rbp), %rdi
movaps %xmm0, -0x40(%rbp)
testq %rdi, %rdi
je 0x3c90
callq 0x50cc
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x3c90
callq 0x50cc
jmp 0x3c90
movq 0x10(%r14), %rax
leaq -0x38(%rbp), %rdi
movq %rax, -0x8(%rdi)
addq $0x18, %r14
movq %r14, %rsi
callq 0x5074
movq -0x58(%rbp), %rbx
movq $0x0, (%rbx)
leaq 0x8(%rbx), %rdi
leaq -0x50(%rbp), %rdx
leaq -0x90(%rbp), %r8
leaq -0x40(%rbp), %r9
movq %rbx, %rsi
movq %r15, %rcx
callq 0x5d1a
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x3cc3
callq 0x50cc
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0x3cd4
callq 0x50cc
movq %rbx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3cf0
movq %rax, %rbx
jmp 0x3d01
movq %rax, %rbx
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x3d01
callq 0x50cc
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0x3d12
callq 0x50cc
movq %rbx, %rdi
callq 0x33d0