// File: /workspace/github_cpp_makefile_star2_O3/kingufl[P]Rmapper/src/KDTree.cpp
// Function: KDTree::nearest_(std::shared_ptr<KDNode> const&, std::vector<int, std::allocator<int>> const&, unsigned long const&, std::shared_ptr<KDNode> const&, int const&)
KDNodePtr KDTree::nearest_(   //
    const KDNodePtr &branch,  //
    const point_t &pt,        //
    const size_t &level,      //
    const KDNodePtr &best,    //
    const int &best_dist   //
) {
    int d, dx, dx2;

    if (!bool(*branch)) {
        return NewKDNodePtr();  // basically, null
    }

    point_t branch_pt(*branch);
    size_t dim = branch_pt.size();

    d = dist2(branch_pt, pt);
    dx = branch_pt.at(level) - pt.at(level);
    dx2 = dx * dx;

    KDNodePtr best_l = best;
    int best_dist_l = best_dist;

    if (d < best_dist) {
        best_dist_l = d;
        best_l = branch;
    }

    size_t next_lv = (level + 1) % dim;
    KDNodePtr section;
    KDNodePtr other;

    // select which branch makes sense to check
    if (dx > 0) {
        section = branch->left;
        other = branch->right;
    } else {
        section = branch->right;
        other = branch->left;
    }

    // keep nearest neighbor from further down the tree
    KDNodePtr further = nearest_(section, pt, next_lv, best_l, best_dist_l);
    if (!further->x.empty()) {
        int dl = dist2(further->x, pt);
        if (dl < best_dist_l) {
            best_dist_l = dl;
            best_l = further;
        }
    }
    // only check the other branch if it makes sense to do so
    if (dx2 < best_dist_l) {
        further = nearest_(other, pt, next_lv, best_l, best_dist_l);
        if (!further->x.empty()) {
            int dl = dist2(further->x, pt);
            if (dl < best_dist_l) {
                best_dist_l = dl;
                best_l = further;
            }
        }
    }

    return best_l;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rcx, -0x38(%rbp)
movq %rsi, -0x88(%rbp)
movq %rdi, %rbx
movq (%rdx), %rsi
movq 0x8(%rsi), %rax
cmpq 0x10(%rsi), %rax
je 0x3faa
movq %r9, %r12
movq %rdx, %r13
movq %r8, -0x60(%rbp)
movq %rbx, -0x48(%rbp)
addq $0x8, %rsi
leaq -0xb8(%rbp), %rbx
movq %rbx, %rdi
callq 0x4e96
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movq %rcx, %r15
subq %rax, %r15
sarq $0x2, %r15
cmpq %rax, %rcx
je 0x3ff6
movq -0x38(%rbp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
cmpq $0x1, %r15
movq %r15, %rdi
adcq $0x0, %rdi
leaq -0x1(%rdi), %r8
movq %rsi, %rdx
cmpq %r8, %rsi
jbe 0x42e4
xorl %r14d, %r14d
xorl %edx, %edx
movq -0x60(%rbp), %r8
movl (%rax,%rdx,4), %esi
subl (%rcx,%rdx,4), %esi
imull %esi, %esi
addl %esi, %r14d
incq %rdx
cmpq %rdx, %rdi
jne 0x3f94
jmp 0x3ffd
movl $0x50, %edi
callq 0x3250
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x8(%rax)
leaq 0x14d4f(%rip), %rcx     # 0x18d18
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movups %xmm0, 0x28(%rax)
movups %xmm0, 0x38(%rax)
movq $0x0, 0x48(%rax)
movq %rax, 0x8(%rbx)
addq $0x10, %rax
movq %rax, (%rbx)
movq %rbx, %r13
jmp 0x42cc
xorl %r14d, %r14d
movq -0x60(%rbp), %r8
movq (%r8), %rsi
cmpq %rsi, %r15
jbe 0x42e1
movq -0x38(%rbp), %rcx
movq (%rcx), %rdi
movq 0x8(%rcx), %rdx
subq %rdi, %rdx
sarq $0x2, %rdx
cmpq %rsi, %rdx
jbe 0x42e4
movq 0x10(%rbp), %rcx
movl (%rax,%rsi,4), %ebx
subl (%rdi,%rsi,4), %ebx
movq (%r12), %rax
movq -0x48(%rbp), %rdx
movq %rax, (%rdx)
movq 0x8(%r12), %rax
movq %rax, 0x8(%rdx)
testq %rax, %rax
je 0x405c
movq 0x14f72(%rip), %rdx     # 0x18fc0
cmpb $0x0, (%rdx)
je 0x4058
incl 0x8(%rax)
jmp 0x405c
lock
incl 0x8(%rax)
movq -0x48(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x40(%rbp)
movl (%rcx), %r12d
movl %r12d, -0x2c(%rbp)
cmpl %r12d, %r14d
jge 0x4097
movl %r14d, -0x2c(%rbp)
movq (%r13), %rax
movq -0x48(%rbp), %rcx
movq %rax, (%rcx)
leaq 0x8(%r13), %rsi
movq -0x40(%rbp), %rdi
callq 0x5074
movq -0x60(%rbp), %r8
movl %r14d, %r12d
movq (%r8), %rax
incq %rax
xorl %edx, %edx
divq %r15
movq %rdx, -0x90(%rbp)
xorps %xmm0, %xmm0
leaq -0x78(%rbp), %rdi
movaps %xmm0, -0x8(%rdi)
leaq -0x68(%rbp), %r14
movaps %xmm0, -0x8(%r14)
movq (%r13), %rsi
testl %ebx, %ebx
jle 0x40e8
movq 0x20(%rsi), %rax
movq %rax, -0x80(%rbp)
addq $0x28, %rsi
callq 0x5074
movq (%r13), %rsi
movq 0x30(%rsi), %rax
movq %rax, -0x70(%rbp)
addq $0x38, %rsi
jmp 0x4109
movq 0x30(%rsi), %rax
movq %rax, -0x80(%rbp)
addq $0x38, %rsi
callq 0x5074
movq (%r13), %rsi
movq 0x20(%rsi), %rax
movq %rax, -0x70(%rbp)
addq $0x28, %rsi
movq %r14, %rdi
callq 0x5074
leaq -0x2c(%rbp), %r14
movq %r14, (%rsp)
leaq -0x58(%rbp), %rdi
leaq -0x80(%rbp), %rdx
leaq -0x90(%rbp), %r8
movq -0x88(%rbp), %rsi
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %r13
movq %r13, %r9
callq 0x3ef0
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rsi
cmpq %rsi, %rcx
je 0x41b7
subq %rcx, %rsi
sarq $0x2, %rsi
movq -0x38(%rbp), %rdx
movq (%rdx), %rdi
movq 0x8(%rdx), %rdx
subq %rdi, %rdx
sarq $0x2, %rdx
cmpq $0x1, %rsi
adcq $0x0, %rsi
leaq -0x1(%rsi), %r8
cmpq %r8, %rdx
jbe 0x42f2
xorl %r15d, %r15d
xorl %edx, %edx
movl (%rcx,%rdx,4), %r8d
subl (%rdi,%rdx,4), %r8d
imull %r8d, %r8d
addl %r8d, %r15d
incq %rdx
cmpq %rdx, %rsi
jne 0x4183
cmpl %r12d, %r15d
jge 0x41b7
movl %r15d, -0x2c(%rbp)
movq %rax, (%r13)
leaq -0x50(%rbp), %rsi
movq -0x40(%rbp), %rdi
callq 0x5074
movl %r15d, %r12d
imull %ebx, %ebx
cmpl %r12d, %ebx
jge 0x4291
movq %r14, (%rsp)
leaq -0xa0(%rbp), %rdi
leaq -0x70(%rbp), %rdx
leaq -0x90(%rbp), %r8
movq -0x88(%rbp), %rsi
movq -0x38(%rbp), %rcx
movq %r13, %r9
callq 0x3ef0
movaps -0xa0(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0xa0(%rbp)
movq -0x50(%rbp), %rdi
movups %xmm0, -0x58(%rbp)
testq %rdi, %rdi
je 0x4220
callq 0x50cc
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x4220
callq 0x50cc
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rdi
cmpq %rdi, %rcx
je 0x4291
subq %rcx, %rdi
sarq $0x2, %rdi
movq -0x38(%rbp), %rdx
movq (%rdx), %r8
movq 0x8(%rdx), %rdx
subq %r8, %rdx
sarq $0x2, %rdx
cmpq $0x1, %rdi
adcq $0x0, %rdi
leaq -0x1(%rdi), %rsi
cmpq %rsi, %rdx
jbe 0x4303
leaq -0x50(%rbp), %rsi
xorl %edx, %edx
xorl %r9d, %r9d
movl (%rcx,%r9,4), %r10d
subl (%r8,%r9,4), %r10d
imull %r10d, %r10d
addl %r10d, %edx
incq %r9
cmpq %r9, %rdi
jne 0x4268
cmpl %r12d, %edx
jge 0x4291
movq %rax, (%r13)
movq -0x40(%rbp), %rdi
callq 0x5074
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x429f
callq 0x50cc
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x42ad
callq 0x50cc
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x42bb
callq 0x50cc
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x42cc
callq 0x3230
movq %r13, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdx
leaq 0xed35(%rip), %rdi      # 0x13020
xorl %eax, %eax
callq 0x3330
leaq 0xed27(%rip), %rdi      # 0x13020
movq %rdx, %rsi
xorl %eax, %eax
callq 0x3330
leaq 0xed16(%rip), %rdi      # 0x13020
movq %rdx, %rsi
xorl %eax, %eax
callq 0x3330
jmp 0x4318
jmp 0x4318
movq %rax, %rbx
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x432e
callq 0x50cc
jmp 0x432e
movq %rax, %rbx
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x433c
callq 0x50cc
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x434a
callq 0x50cc
movq -0x40(%rbp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x4360
callq 0x50cc
jmp 0x4360
movq %rax, %rbx
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x4371
callq 0x3230
movq %rbx, %rdi
callq 0x33d0
nop