// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movl %ecx, %ebp
movq %rsi, %rcx
subq %rdi, %rcx
je 0x52c3
movq %r8, %r14
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r13
sarq $0x2, %rcx
movq %rcx, %rax
shrq $0x3f, %rax
addq %rcx, %rax
sarq %rax
leaq (%rdi,%rax,4), %r12
cmpq $0x2, %rcx
setl %al
cmpq %rsi, %r12
sete %dl
orb %al, %dl
jne 0x514c
movl %ebp, %eax
cltd
idivl 0x4(%r15)
movl %edx, %r9d
bsrq %rcx, %rcx
xorl $0x3f, %ecx
addl %ecx, %ecx
xorq $0x7e, %rcx
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r15, %r8
callq 0x5510
movl $0x50, %edi
callq 0x2160
movq %rbx, 0x20(%rsp)
movq %rax, %rbx
movl (%r12), %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)
movslq 0x4(%r15), %r14
testq %r14, %r14
setle 0x7(%rsp)
movq %r13, 0x18(%rsp)
leaq 0x20(%rbx), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x3744
movl 0x4(%rbx), %eax
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 %r13, %rdi
callq 0x59f2
leaq 0x38(%rbx), %rdi
movq %rdi, 0x8(%rsp)
movq %r13, 0x10(%rsp)
movq %r13, %rsi
callq 0x53b8
incl %ebp
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movl %ebp, %ecx
movq %rbx, %r8
callq 0x50d6
movq %rax, %r13
addq $0x4, %r12
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
movl %ebp, %ecx
movq %rbx, %r8
callq 0x50d6
movq %r13, 0x10(%rbx)
movq %rax, 0x18(%rbx)
testq %r13, %r13
sete %cl
orb 0x7(%rsp), %cl
jne 0x5271
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq 0x20(%r13), %rdx
movq 0x38(%r13), %rsi
movq 0x8(%rsp), %rdi
movq (%rdi), %rdi
xorl %r8d, %r8d
movsd (%rdx,%r8,8), %xmm0
minsd (%rcx,%r8,8), %xmm0
movsd %xmm0, (%rcx,%r8,8)
movsd (%rsi,%r8,8), %xmm0
maxsd (%rdi,%r8,8), %xmm0
movsd %xmm0, (%rdi,%r8,8)
incq %r8
cmpq %r8, %r14
jne 0x5245
testl %r14d, %r14d
setle %cl
testq %rax, %rax
sete %dl
orb %cl, %dl
jne 0x52c5
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq 0x20(%rax), %rdx
movq 0x38(%rax), %rax
movq 0x8(%rsp), %rsi
movq (%rsi), %rsi
xorl %edi, %edi
movsd (%rdx,%rdi,8), %xmm0
minsd (%rcx,%rdi,8), %xmm0
movsd %xmm0, (%rcx,%rdi,8)
movsd (%rax,%rdi,8), %xmm0
maxsd (%rsi,%rdi,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdi
cmpq %rdi, %r14
jne 0x529b
jmp 0x52c5
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop