// File: /workspace/github_cpp_makefile_star2_O1/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 0x5317
movq %r8, %r14
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r13
sarq $0x2, %rcx
movq %rcx, %rax
shrq $0x3f, %rax
addq %rcx, %rax
andq $-0x2, %rax
leaq (%rdi,%rax,2), %r12
cmpq $0x2, %rcx
jl 0x51a0
cmpq %rbx, %r13
je 0x51a0
cmpq %rbx, %r12
je 0x51a0
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 0x5552
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 0x3776
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 0x5a56
leaq 0x38(%rbx), %rdi
movq %rdi, 0x8(%rsp)
movq %r13, 0x10(%rsp)
movq %r13, %rsi
callq 0x5408
incl %ebp
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movl %ebp, %ecx
movq %rbx, %r8
callq 0x512a
movq %rax, %r13
addq $0x4, %r12
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq %r15, %rdx
movl %ebp, %ecx
movq %rbx, %r8
callq 0x512a
movq %r13, 0x10(%rbx)
movq %rax, 0x18(%rbx)
testq %r13, %r13
sete %cl
orb 0x7(%rsp), %cl
jne 0x52c5
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 0x5299
testq %rax, %rax
sete %cl
testl %r14d, %r14d
setle %dl
orb %cl, %dl
jne 0x5319
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 0x52ef
jmp 0x5319
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop