// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq (%rdx), %rbx
movq (%rcx), %r12
cmpq %r12, %rbx
je 0x3b86
movq %rcx, %r13
movq %rdx, %r15
movq %r14, -0x58(%rbp)
movq (%rbx), %rax
movq 0x8(%rbx), %rsi
movq %rsi, -0x70(%rbp)
movq %rax, -0x78(%rbp)
subq %rax, %rsi
cmpq $0x2, (%r8)
movq %r9, -0x60(%rbp)
jb 0x3a9b
movq (%r9), %r14
movq %r12, %rax
subq %rbx, %rax
sarq $0x5, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
leaq 0x16c1(%rip), %rax      # 0x50fa
leaq -0x90(%rbp), %rcx
movq %rax, (%rcx)
xorl %eax, %eax
movq %rax, 0x8(%rcx)
movq %r14, 0x10(%rcx)
movq %rbx, %rdi
movq %rsi, -0xa0(%rbp)
movq %r12, %rsi
movq %r8, -0x98(%rbp)
callq 0x5146
leaq -0x40(%rbp), %rdx
leaq 0x1689(%rip), %rax      # 0x50fa
movq %rax, (%rdx)
xorl %eax, %eax
movq %rax, 0x8(%rdx)
movq %r14, 0x10(%rdx)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x529a
movq -0x98(%rbp), %r8
movq -0xa0(%rbp), %rsi
movq -0x60(%rbp), %r9
sarq $0x2, %rsi
movq (%r8), %rax
movq %rax, %rbx
shrq %rbx
movq (%r15), %rcx
movq %rbx, %rdx
shlq $0x5, %rdx
leaq (%rcx,%rdx), %r15
movq %rcx, -0xd0(%rbp)
movq %r15, -0xc8(%rbp)
addq %rdx, %rcx
addq $0x20, %rcx
movq %rcx, -0xc0(%rbp)
movq (%r13), %rcx
movq %rcx, -0xb8(%rbp)
movq %rbx, -0xb0(%rbp)
notq %rbx
addq %rax, %rbx
movq %rbx, -0xa8(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
cmpq $0x2, %rax
setb %al
movq -0x70(%rbp), %rcx
cmpq -0x78(%rbp), %rcx
sete %cl
orb %al, %cl
jne 0x3bd0
movq (%r9), %rax
incq %rax
xorl %edx, %edx
movq %rsi, %r13
divq %rsi
leaq -0x50(%rbp), %r9
movq %rdx, (%r9)
leaq -0x40(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
leaq -0xc8(%rbp), %rcx
leaq -0xb0(%rbp), %r8
movq -0x68(%rbp), %r12
movq %r12, %rsi
callq 0x39c4
movq -0x58(%rbp), %r14
movaps -0x40(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0x40(%rbp)
movq -0x88(%rbp), %rdi
movaps %xmm0, -0x90(%rbp)
testq %rdi, %rdi
je 0x3b76
callq 0x508e
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x3bf5
callq 0x508e
jmp 0x3bf5
xorl %ebx, %ebx
movq %rbx, (%r14)
movl $0x50, %edi
callq 0x3250
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x8(%rax)
leaq 0x15136(%rip), %rcx     # 0x18ce0
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movups %xmm0, 0x28(%rax)
movups %xmm0, 0x38(%rax)
movq %rbx, 0x48(%rax)
movq %rax, 0x8(%r14)
addq $0x10, %rax
movq %rax, (%r14)
jmp 0x3cd5
movq %rsi, %r13
movq -0x68(%rbp), %r12
movq 0x10(%r12), %rax
leaq -0x88(%rbp), %rdi
movq %rax, -0x8(%rdi)
leaq 0x18(%r12), %rsi
callq 0x503a
movq -0x58(%rbp), %r14
movq -0x70(%rbp), %rax
cmpq -0x78(%rbp), %rax
sete %al
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
testq %rbx, %rbx
sete %cl
orb %al, %cl
movq -0x60(%rbp), %rax
jne 0x3c78
movq (%rax), %rax
incq %rax
xorl %edx, %edx
divq %r13
leaq -0xd8(%rbp), %r9
movq %rdx, (%r9)
leaq -0x50(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
leaq -0xb8(%rbp), %rcx
leaq -0xa8(%rbp), %r8
movq %r12, %rsi
callq 0x39c4
movaps -0x50(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0x50(%rbp)
movq -0x38(%rbp), %rdi
movaps %xmm0, -0x40(%rbp)
testq %rdi, %rdi
je 0x3c68
callq 0x508e
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x3c91
callq 0x508e
jmp 0x3c91
movq 0x10(%r12), %rax
leaq -0x38(%rbp), %rdi
movq %rax, -0x8(%rdi)
addq $0x18, %r12
movq %r12, %rsi
callq 0x503a
movq $0x0, (%r14)
leaq 0x8(%r14), %rdi
leaq -0x50(%rbp), %rdx
leaq -0x90(%rbp), %r8
leaq -0x40(%rbp), %r9
movq %r14, %rsi
movq %r15, %rcx
callq 0x5d24
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x3cc4
callq 0x508e
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0x3cd5
callq 0x508e
movq %r14, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3cf1
movq %rax, %rbx
jmp 0x3d02
movq %rax, %rbx
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0x3d02
callq 0x508e
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0x3d13
callq 0x508e
movq %rbx, %rdi
callq 0x33d0
nop