// File: 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
subq $0x2f0, %rsp            # imm = 0x2F0
movq %rdi, -0x258(%rbp)
movq %rdi, %rax
movq %rax, -0x250(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x248(%rbp)
movq -0x18(%rbp), %rdi
callq 0x5a60
movq %rax, %rdi
callq 0x3800
testb $0x1, %al
jne 0x4528
movq -0x258(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x3150
movq -0x258(%rbp), %rdi
callq 0x58a0
jmp 0x4c3a
movq -0x20(%rbp), %rdi
callq 0x56e0
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %eax
imull %eax, %eax
movl %eax, -0x4c(%rbp)
movq -0x18(%rbp), %rdi
callq 0x5a60
movq %rax, %rsi
leaq -0x68(%rbp), %rdi
movq %rdi, -0x268(%rbp)
callq 0x3830
movq -0x268(%rbp), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
callq 0x5460
movq %rax, -0x260(%rbp)
jmp 0x4579
movq -0x260(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x27c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x5400
movl -0x27c(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
callq 0x5b20
movl (%rax), %eax
movl %eax, -0x78(%rbp)
movq -0x18(%rbp), %rdi
callq 0x5a60
movq %rax, %rsi
leaq -0x90(%rbp), %rdi
movq %rdi, -0x278(%rbp)
callq 0x3830
movq -0x278(%rbp), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
callq 0x5460
movq %rax, -0x270(%rbp)
jmp 0x45ea
movq -0x270(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x28c(%rbp)
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rax
movq (%rax), %rsi
callq 0x5b20
movq %rax, -0x288(%rbp)
jmp 0x4612
movq -0x288(%rbp), %rcx
movl -0x28c(%rbp), %eax
subl (%rcx), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
movl %eax, -0x38(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x5400
movq -0x18(%rbp), %rdi
callq 0x5b90
movq %rax, %rsi
addq $0x20, %rsi
leaq -0xa0(%rbp), %rdi
callq 0x5b60
movq -0x18(%rbp), %rdi
callq 0x5b90
movq %rax, %rsi
addq $0x30, %rsi
leaq -0xb0(%rbp), %rdi
callq 0x5b60
movq -0x258(%rbp), %rdi
movb $0x0, -0xb1(%rbp)
callq 0x58a0
leaq -0xd0(%rbp), %rdi
callq 0x58a0
leaq -0xe8(%rbp), %rdi
callq 0x58a0
movl -0x38(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl (%rcx), %eax
jg 0x49af
movq -0x18(%rbp), %rdi
callq 0x5a60
movq %rax, %rsi
leaq -0x100(%rbp), %rdi
callq 0x3830
jmp 0x46c3
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rax
movl (%rax), %edx
leaq -0x100(%rbp), %rdi
callq 0x5bf0
movl %eax, -0x290(%rbp)
jmp 0x46e1
leaq -0x100(%rbp), %rdi
callq 0x5400
movl -0x290(%rbp), %eax
cmpl $0x0, %eax
jle 0x47be
movq -0x18(%rbp), %rdi
callq 0x5a60
movq %rax, %rsi
leaq -0x120(%rbp), %rdi
callq 0x3880
jmp 0x4716
movq -0x258(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x58f0
jmp 0x472b
leaq -0x120(%rbp), %rdi
callq 0x5960
jmp 0x47be
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x5400
jmp 0x4c4a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x5400
jmp 0x4c4a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x4bd8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x5400
jmp 0x4bd8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x5960
jmp 0x4bd8
movq -0x248(%rbp), %rsi
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x30(%rbp), %rax
movq (%rax), %rax
incq %rax
movq -0x48(%rbp), %rdi
xorl %edx, %edx
divq %rdi
movq %rdx, -0x140(%rbp)
leaq -0x138(%rbp), %rdi
leaq -0xb0(%rbp), %rdx
leaq -0x140(%rbp), %r9
callq 0x44b0
jmp 0x4803
leaq -0xe8(%rbp), %rdi
movq %rdi, -0x2a0(%rbp)
leaq -0x138(%rbp), %rsi
movq %rsi, -0x2a8(%rbp)
callq 0x5ca0
movq -0x2a8(%rbp), %rdi
callq 0x5a00
movq -0x258(%rbp), %rdi
callq 0x59b0
movq %rax, -0x150(%rbp)
leaq -0x148(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x5d80
movq -0x2a0(%rbp), %rdi
callq 0x5980
movq -0x2a0(%rbp), %rdi
movq %rax, -0x158(%rbp)
callq 0x59b0
movq -0x258(%rbp), %rdi
movq %rax, -0x160(%rbp)
movq -0x148(%rbp), %rsi
movq -0x158(%rbp), %rdx
movq -0x160(%rbp), %rcx
callq 0x5cd0
movq %rax, -0x298(%rbp)
jmp 0x48a6
movq -0x248(%rbp), %rsi
movq -0x298(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x30(%rbp), %rax
movq (%rax), %rax
incq %rax
movq -0x48(%rbp), %rdi
xorl %edx, %edx
divq %rdi
movq %rdx, -0x188(%rbp)
leaq -0x180(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
leaq -0x188(%rbp), %r9
callq 0x44b0
jmp 0x48f9
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x2b8(%rbp)
leaq -0x180(%rbp), %rsi
movq %rsi, -0x2c0(%rbp)
callq 0x5ca0
movq -0x2c0(%rbp), %rdi
callq 0x5a00
movq -0x258(%rbp), %rdi
callq 0x59b0
movq %rax, -0x198(%rbp)
leaq -0x190(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x5d80
movq -0x2b8(%rbp), %rdi
callq 0x5980
movq -0x2b8(%rbp), %rdi
movq %rax, -0x1a0(%rbp)
callq 0x59b0
movq -0x258(%rbp), %rdi
movq %rax, -0x1a8(%rbp)
movq -0x190(%rbp), %rsi
movq -0x1a0(%rbp), %rdx
movq -0x1a8(%rbp), %rcx
callq 0x5cd0
movq %rax, -0x2b0(%rbp)
jmp 0x499c
movq -0x2b0(%rbp), %rax
movq %rax, -0x1b0(%rbp)
jmp 0x4bae
movl -0x50(%rbp), %eax
cmpl -0x78(%rbp), %eax
jge 0x4ab6
movq -0x248(%rbp), %rsi
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x30(%rbp), %rax
movq (%rax), %rax
incq %rax
movq -0x48(%rbp), %rdi
xorl %edx, %edx
divq %rdi
movq %rdx, -0x1d0(%rbp)
leaq -0x1c8(%rbp), %rdi
leaq -0xb0(%rbp), %rdx
leaq -0x1d0(%rbp), %r9
callq 0x44b0
jmp 0x4a00
leaq -0xe8(%rbp), %rdi
movq %rdi, -0x2d0(%rbp)
leaq -0x1c8(%rbp), %rsi
movq %rsi, -0x2d8(%rbp)
callq 0x5ca0
movq -0x2d8(%rbp), %rdi
callq 0x5a00
movq -0x258(%rbp), %rdi
callq 0x59b0
movq %rax, -0x1e0(%rbp)
leaq -0x1d8(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x5d80
movq -0x2d0(%rbp), %rdi
callq 0x5980
movq -0x2d0(%rbp), %rdi
movq %rax, -0x1e8(%rbp)
callq 0x59b0
movq -0x258(%rbp), %rdi
movq %rax, -0x1f0(%rbp)
movq -0x1d8(%rbp), %rsi
movq -0x1e8(%rbp), %rdx
movq -0x1f0(%rbp), %rcx
callq 0x5cd0
movq %rax, -0x2c8(%rbp)
jmp 0x4aa3
movq -0x2c8(%rbp), %rax
movq %rax, -0x1f8(%rbp)
jmp 0x4bac
movq -0x248(%rbp), %rsi
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x30(%rbp), %rax
movq (%rax), %rax
incq %rax
movq -0x48(%rbp), %rdi
xorl %edx, %edx
divq %rdi
movq %rdx, -0x218(%rbp)
leaq -0x210(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
leaq -0x218(%rbp), %r9
callq 0x44b0
jmp 0x4afb
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x2e8(%rbp)
leaq -0x210(%rbp), %rsi
movq %rsi, -0x2f0(%rbp)
callq 0x5ca0
movq -0x2f0(%rbp), %rdi
callq 0x5a00
movq -0x258(%rbp), %rdi
callq 0x59b0
movq %rax, -0x228(%rbp)
leaq -0x220(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x5d80
movq -0x2e8(%rbp), %rdi
callq 0x5980
movq -0x2e8(%rbp), %rdi
movq %rax, -0x230(%rbp)
callq 0x59b0
movq -0x258(%rbp), %rdi
movq %rax, -0x238(%rbp)
movq -0x220(%rbp), %rsi
movq -0x230(%rbp), %rdx
movq -0x238(%rbp), %rcx
callq 0x5cd0
movq %rax, -0x2e0(%rbp)
jmp 0x4b9e
movq -0x2e0(%rbp), %rax
movq %rax, -0x240(%rbp)
jmp 0x4bae
movb $0x1, -0xb1(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x5a00
leaq -0xd0(%rbp), %rdi
callq 0x5a00
testb $0x1, -0xb1(%rbp)
jne 0x4c22
jmp 0x4c16
leaq -0xe8(%rbp), %rdi
callq 0x5a00
leaq -0xd0(%rbp), %rdi
callq 0x5a00
movq -0x258(%rbp), %rdi
callq 0x5a00
leaq -0xb0(%rbp), %rdi
callq 0x53e0
leaq -0xa0(%rbp), %rdi
callq 0x53e0
jmp 0x4c4a
movq -0x258(%rbp), %rdi
callq 0x5a00
leaq -0xb0(%rbp), %rdi
callq 0x53e0
leaq -0xa0(%rbp), %rdi
callq 0x53e0
movq -0x250(%rbp), %rax
addq $0x2f0, %rsp            # imm = 0x2F0
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x33d0
nopw %cs:(%rax,%rax)