// File: /workspace/github_cpp_makefile_star2_O2/kingufl[P]Rmapper/src/KDTree.cpp
// Function: KDTree::neighborhood_(std::shared_ptr<KDNode> const&, std::vector<int, std::allocator<int>> const&, int const&, unsigned long const&)
pointIndexArr KDTree::neighborhood_(  //
    const KDNodePtr &branch,          //
    const point_t &pt,                //
    const int &rad,                //
    const size_t &level               //
) {
    int d, dx, dx2;

    if (!bool(*branch)) {
        // branch has no point, means it is a leaf,
        // no points to add
        return pointIndexArr();
    }

    size_t dim = pt.size();

    int r2 = rad * rad;

//    d = disth(point_t(*branch), pt, rad);
    int nodeval=point_t(*branch).at(level);
    int pointval=pt.at(level);
    dx = abs(point_t(*branch).at(level) - pt.at(level));
//    dx2 = dx * dx;

    KDNodePtr section = branch->left;
    KDNodePtr other = branch->right;

    pointIndexArr nbh, nbh_s, nbh_o;
    if (dx <= rad) {
//        std::cout<<dx<<" "<<nodeval<<" "<<pointval<<" "<<rad<<" "<<level<<"\n";
        if(disth(point_t(*branch), pt, rad)>0)
            nbh.push_back(pointIndex(*branch));
        nbh_o = neighborhood_(other, pt, rad, (level + 1) % dim);
        nbh.insert(nbh.end(), nbh_o.begin(), nbh_o.end());
        nbh_s = neighborhood_(section, pt, rad, (level + 1) % dim);
        nbh.insert(nbh.end(), nbh_s.begin(), nbh_s.end());
    }
    else{

        if(nodeval<pointval){

            //search right branch
            nbh_o = neighborhood_(other, pt, rad, (level + 1) % dim);
            nbh.insert(nbh.end(), nbh_o.begin(), nbh_o.end());

        }
        else{
            //search left branch

            nbh_s = neighborhood_(section, pt, rad, (level + 1) % dim);
            nbh.insert(nbh.end(), nbh_s.begin(), nbh_s.end());

        }
    }

    return nbh;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdx, %r12
movq %rsi, %rdx
movq (%r12), %rsi
movq 0x8(%rsi), %rax
cmpq 0x10(%rsi), %rax
je 0x40a5
movq %r9, %r13
movq %r8, %rbx
movq %rdx, -0x80(%rbp)
movq %rdi, -0x30(%rbp)
addq $0x8, %rsi
movq 0x8(%rcx), %rax
subq (%rcx), %rax
movq %rax, -0x40(%rbp)
leaq -0x60(%rbp), %r14
movq %r14, %rdi
movq %rcx, %r15
callq 0x45ba
movq (%r13), %rsi
movq %r14, %rdi
callq 0x4598
movq %rbx, -0x68(%rbp)
movl (%rax), %eax
movl %eax, -0x78(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x552c
movq (%r13), %rsi
movq %r15, %rdi
callq 0x4752
movl (%rax), %eax
movl %eax, -0x74(%rbp)
movq (%r12), %rsi
addq $0x8, %rsi
movq %r14, %rdi
callq 0x45ba
movq (%r13), %rsi
movq %r14, %rdi
callq 0x4598
movl (%rax), %r14d
movq (%r13), %rsi
movq %r15, %rdi
callq 0x4752
movq %r15, -0x38(%rbp)
movq -0x40(%rbp), %r15
sarq $0x2, %r15
subl (%rax), %r14d
movl %r14d, %ebx
negl %ebx
cmovsl %r14d, %ebx
leaq -0x60(%rbp), %rdi
callq 0x552c
movq (%r12), %rsi
addq $0x20, %rsi
leaq -0xa0(%rbp), %rdi
callq 0x54ee
movq (%r12), %rsi
addq $0x30, %rsi
leaq -0x90(%rbp), %rdi
callq 0x54ee
movq -0x30(%rbp), %rax
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
andq $0x0, -0xd0(%rbp)
movups %xmm0, (%rax)
andq $0x0, -0xb0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movq -0x68(%rbp), %rax
cmpl (%rax), %ebx
movq %rax, %rbx
jle 0x40b8
movl -0x74(%rbp), %eax
cmpl %eax, -0x78(%rbp)
movq -0x80(%rbp), %rsi
jge 0x41d3
movq (%r13), %rax
incq %rax
xorl %edx, %edx
divq %r15
leaq -0x70(%rbp), %r9
movq %rdx, (%r9)
leaq -0x60(%rbp), %rdi
leaq -0x90(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq %rbx, %r8
callq 0x3efa
leaq -0xc0(%rbp), %rbx
leaq -0x60(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5af4
movq %r14, %rdi
callq 0x46c2
movq -0x30(%rbp), %r14
movq 0x8(%r14), %rsi
movq (%rbx), %rdx
movq 0x8(%rbx), %rcx
movq %r14, %rdi
callq 0x47b8
jmp 0x4232
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movq %rdi, %rax
jmp 0x4265
movq %r15, -0x40(%rbp)
movq (%r12), %rsi
addq $0x8, %rsi
leaq -0x60(%rbp), %rdi
callq 0x45ba
movq -0x80(%rbp), %r15
movl (%rbx), %edx
leaq -0x60(%rbp), %r14
movq %r14, %rdi
movq -0x38(%rbp), %rsi
callq 0x4773
movl %eax, %ebx
movq %r14, %rdi
callq 0x552c
testl %ebx, %ebx
jle 0x4114
movq (%r12), %rsi
leaq -0x60(%rbp), %rdi
callq 0x36fc
leaq -0x60(%rbp), %rsi
movq -0x30(%rbp), %rdi
callq 0x5916
leaq -0x60(%rbp), %rdi
callq 0x552c
movq (%r13), %rax
incq %rax
xorl %edx, %edx
divq -0x40(%rbp)
leaq -0x70(%rbp), %r9
movq %rdx, (%r9)
leaq -0x60(%rbp), %rdi
leaq -0x90(%rbp), %rdx
movq %r15, %rsi
movq -0x38(%rbp), %rcx
movq -0x68(%rbp), %r8
callq 0x3efa
leaq -0xc0(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5af4
movq %r14, %rdi
callq 0x46c2
movq -0x30(%rbp), %rdi
movq 0x8(%rdi), %rsi
movq (%rbx), %rdx
movq 0x8(%rbx), %rcx
callq 0x47b8
movq (%r13), %rax
incq %rax
xorl %edx, %edx
divq -0x40(%rbp)
leaq -0x70(%rbp), %r9
movq %rdx, (%r9)
leaq -0x60(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
movq %r15, %rsi
movq -0x38(%rbp), %rcx
movq -0x68(%rbp), %r8
callq 0x3efa
leaq -0xe0(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5af4
movq %r14, %rdi
callq 0x46c2
movq -0x30(%rbp), %r14
movq 0x8(%r14), %rsi
movq (%rbx), %rdx
movq 0x8(%rbx), %rcx
movq %r14, %rdi
callq 0x47b8
jmp 0x4232
movq (%r13), %rax
incq %rax
xorl %edx, %edx
divq %r15
leaq -0x70(%rbp), %r9
movq %rdx, (%r9)
leaq -0x60(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq %rbx, %r8
callq 0x3efa
leaq -0xe0(%rbp), %rbx
leaq -0x60(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5af4
movq %r14, %rdi
callq 0x46c2
movq -0x30(%rbp), %r14
movq 0x8(%r14), %rsi
movq (%rbx), %rdx
movq 0x8(%rbx), %rcx
movq %r14, %rdi
callq 0x47b8
leaq -0xc0(%rbp), %rdi
callq 0x46c2
leaq -0xe0(%rbp), %rdi
callq 0x46c2
leaq -0x88(%rbp), %rdi
callq 0x4978
leaq -0x98(%rbp), %rdi
callq 0x4978
movq %r14, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4297
jmp 0x4297
jmp 0x4297
jmp 0x4297
movq %rax, %r14
leaq -0x60(%rbp), %rdi
callq 0x552c
jmp 0x429a
jmp 0x4297
jmp 0x4297
jmp 0x4297
jmp 0x4297
jmp 0x4297
movq %rax, %r14
leaq -0xc0(%rbp), %rdi
callq 0x46c2
leaq -0xe0(%rbp), %rdi
callq 0x46c2
movq -0x30(%rbp), %rdi
callq 0x46c2
leaq -0x88(%rbp), %rdi
callq 0x4978
leaq -0x98(%rbp), %rdi
callq 0x4978
jmp 0x42e3
jmp 0x42d7
movq %rax, %r14
leaq -0x60(%rbp), %rdi
callq 0x552c
movq %r14, %rdi
callq 0x3380
nop