// File: /workspace/github_cpp_makefile_star2_O2/sharmaeklavya2[P]ctmeans/src/lib/kdtree.cpp
// Function: get_kd_tree(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int>>>, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int>>>, Matrix const&, int, KDNode*)
KDNode* get_kd_tree(
    std::vector<int>::iterator begin,
    std::vector<int>::iterator end,
    const Matrix& X, int depth, KDNode* parent
    ) {
    auto size = end - begin;
    if(size == 0) {
        return nullptr;
    }
    else {
        auto size1 = size / 2;
        std::vector<int>::iterator mid = begin + size1;
        if(size > 1) {
            std::nth_element(begin, mid, end, ColComparator(X, depth % X.cols()));
        }
        KDNode* root = new KDNode(depth, *mid, parent);
        int d = X.cols();
        root->cmin.reserve(d);
        root->cmin.assign(X.begin_row(root->point), X.end_row(root->point));
        root->cmax = root->cmin;

        KDNode* left = get_kd_tree(begin, mid, X, depth+1, root);
        KDNode* right = get_kd_tree(mid+1, end, X, depth+1, root);
        root->left = left;
        root->right = right;
        if(left != nullptr) {
            for(int j=0; j<d; ++j) {
                root->cmin[j] = std::min(root->cmin[j], left->cmin[j]);
                root->cmax[j] = std::max(root->cmax[j], left->cmax[j]);
            }
        }
        if(right != nullptr) {
            for(int j=0; j<d; ++j) {
                root->cmin[j] = std::min(root->cmin[j], right->cmin[j]);
                root->cmax[j] = std::max(root->cmax[j], right->cmax[j]);
            }
        }
        return root;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %ecx, %ebp
movq %rsi, %rcx
subq %rdi, %rcx
je 0x4cbe
movq %r8, %r14
movq %rdx, %r13
movq %rsi, %rbx
movq %rdi, %r12
sarq $0x2, %rcx
pushq $0x2
popq %rsi
movq %rcx, %rax
cqto
idivq %rsi
leaq (%rdi,%rax,4), %r15
cmpq $0x2, %rcx
jl 0x4b60
movl %ebp, %eax
cltd
idivl 0x4(%r13)
movl %edx, %r8d
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x4dc9
pushq $0x50
popq %rdi
callq 0x2170
movq %rbx, 0x10(%rsp)
movq %rax, %rbx
movl (%r15), %eax
movl %ebp, (%rbx)
movl %eax, 0x4(%rbx)
movq %r14, 0x8(%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x40(%rbx)
movupd %xmm0, 0x30(%rbx)
movupd %xmm0, 0x20(%rbx)
movupd %xmm0, 0x10(%rbx)
movq %r15, 0x8(%rsp)
movq %r13, %r15
movl 0x4(%r13), %r13d
leaq 0x20(%rbx), %r14
movslq %r13d, %rsi
movq %r14, %rdi
callq 0x31f6
movl 0x4(%rbx), %eax
movq %r15, (%rsp)
movq 0x10(%r15), %rcx
movl 0x4(%r15), %edx
movl %edx, %esi
imull %eax, %esi
movslq %esi, %rsi
leaq (%rcx,%rsi,8), %rsi
incl %eax
imull %edx, %eax
cltq
leaq (%rcx,%rax,8), %rdx
movq %r14, %rdi
callq 0x5496
movq %r12, %r15
leaq 0x38(%rbx), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x4dfc
incl %ebp
movq %r15, %rdi
movq 0x8(%rsp), %r15
movq %r15, %rsi
movq (%rsp), %rdx
movl %ebp, %ecx
movq %rbx, %r8
callq 0x4b04
movl %ebp, %ecx
movq %rax, %rbp
addq $0x4, %r15
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq (%rsp), %rdx
movq %rbx, %r8
callq 0x4b04
movq %rbp, 0x10(%rbx)
movq %rax, 0x18(%rbx)
testq %rbp, %rbp
je 0x4c79
xorl %ecx, %ecx
testl %r13d, %r13d
movl $0x0, %edx
cmovgl %r13d, %edx
cmpq %rcx, %rdx
je 0x4c79
movq (%r14), %rsi
movq 0x20(%rbp), %rdi
movsd (%rdi,%rcx,8), %xmm0
minsd (%rsi,%rcx,8), %xmm0
movsd %xmm0, (%rsi,%rcx,8)
movq (%r12), %rsi
movq 0x38(%rbp), %rdi
movsd (%rdi,%rcx,8), %xmm0
maxsd (%rsi,%rcx,8), %xmm0
movsd %xmm0, (%rsi,%rcx,8)
incq %rcx
jmp 0x4c42
testq %rax, %rax
je 0x4cc0
xorl %ecx, %ecx
testl %r13d, %r13d
cmovlel %ecx, %r13d
cmpq %rcx, %r13
je 0x4cc0
movq (%r14), %rdx
movq 0x20(%rax), %rsi
movsd (%rsi,%rcx,8), %xmm0
minsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, (%rdx,%rcx,8)
movq (%r12), %rdx
movq 0x38(%rax), %rsi
movsd (%rsi,%rcx,8), %xmm0
maxsd (%rdx,%rcx,8), %xmm0
movsd %xmm0, (%rdx,%rcx,8)
incq %rcx
jmp 0x4c87
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq