// File: /workspace/github_cpp_makefile_star2_O1/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, -0xa0(%rbp)
movq %rdi, %r15
movq (%rdx), %rsi
movq 0x8(%rsi), %rax
cmpq 0x10(%rsi), %rax
je 0x3f97
movq %r9, %r12
movq %r8, %r14
movq %rdx, %r13
movq %r15, -0x98(%rbp)
addq $0x8, %rsi
leaq -0xb8(%rbp), %rbx
movq %rbx, %rdi
callq 0x4e70
movq (%rbx), %rcx
movq 0x8(%rbx), %rax
movq %rax, %r15
subq %rcx, %r15
sarq $0x2, %r15
cmpq %rcx, %rax
je 0x3fe4
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rdx
subq %rdi, %rdx
sarq $0x2, %rdx
cmpq $0x1, %r15
movq %r15, %r8
adcq $0x0, %r8
xorl %eax, %eax
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x42d1
movl (%rcx,%rsi,4), %r9d
subl (%rdi,%rsi,4), %r9d
imull %r9d, %r9d
addl %r9d, %eax
incq %rsi
cmpq %rsi, %r8
jne 0x3f75
jmp 0x3fe6
xorl %ebx, %ebx
movq %rbx, (%r15)
movl $0x50, %edi
callq 0x3250
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0x8(%rax)
leaq 0x14d25(%rip), %rcx     # 0x18ce0
movq %rcx, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rax)
movups %xmm0, 0x28(%rax)
movups %xmm0, 0x38(%rax)
movq %rbx, 0x48(%rax)
movq %rax, 0x8(%r15)
addq $0x10, %rax
movq %rax, (%r15)
movq %r15, %r12
jmp 0x429a
xorl %eax, %eax
movq (%r14), %rsi
cmpq %rsi, %r15
jbe 0x42ce
movq -0x38(%rbp), %rdx
movq (%rdx), %rdi
movq 0x8(%rdx), %rdx
subq %rdi, %rdx
sarq $0x2, %rdx
cmpq %rsi, %rdx
jbe 0x42d1
movq 0x10(%rbp), %rdx
movl (%rcx,%rsi,4), %ebx
subl (%rdi,%rsi,4), %ebx
movq (%r12), %rcx
movq -0x98(%rbp), %rsi
movq %rcx, (%rsi)
movq 0x8(%r12), %rcx
movq %rsi, %r12
movq %rcx, 0x8(%rsi)
testq %rcx, %rcx
je 0x404b
movq 0x14f83(%rip), %rsi     # 0x18fc0
cmpb $0x0, (%rsi)
je 0x4047
incl 0x8(%rcx)
jmp 0x404b
lock
incl 0x8(%rcx)
leaq 0x8(%r12), %rcx
movq %rcx, -0x40(%rbp)
movl (%rdx), %ecx
movl %ecx, -0x2c(%rbp)
cmpl %ecx, %eax
jge 0x4075
movl %eax, -0x2c(%rbp)
movq (%r13), %rax
movq %rax, (%r12)
leaq 0x8(%r13), %rsi
movq -0x40(%rbp), %rdi
callq 0x503a
movq (%r14), %rax
incq %rax
xorl %edx, %edx
divq %r15
movq %rdx, -0x78(%rbp)
xorps %xmm0, %xmm0
leaq -0x68(%rbp), %rdi
movaps %xmm0, -0x8(%rdi)
leaq -0x58(%rbp), %r14
movaps %xmm0, -0x8(%r14)
movq (%r13), %rsi
testl %ebx, %ebx
jle 0x40c3
movq 0x20(%rsi), %rax
movq %rax, -0x70(%rbp)
addq $0x28, %rsi
callq 0x503a
movq (%r13), %rsi
movq 0x30(%rsi), %rax
movq %rax, -0x60(%rbp)
addq $0x38, %rsi
jmp 0x40e4
movq 0x30(%rsi), %rax
movq %rax, -0x70(%rbp)
addq $0x38, %rsi
callq 0x503a
movq (%r13), %rsi
movq 0x20(%rsi), %rax
movq %rax, -0x60(%rbp)
addq $0x28, %rsi
movq %r14, %rdi
callq 0x503a
leaq -0x2c(%rbp), %r15
movq %r15, (%rsp)
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rdx
leaq -0x78(%rbp), %r8
movq -0xa0(%rbp), %r13
movq %r13, %rsi
movq -0x38(%rbp), %rcx
movq %r12, %r9
callq 0x3ee4
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rdi
cmpq %rdi, %rcx
je 0x4188
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
xorl %r9d, %r9d
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x42af
movl (%rcx,%rsi,4), %r10d
subl (%r8,%rsi,4), %r10d
imull %r10d, %r10d
addl %r10d, %r9d
incq %rsi
cmpq %rsi, %rdi
jne 0x414d
cmpl -0x2c(%rbp), %r9d
jge 0x4188
movl %r9d, -0x2c(%rbp)
movq %rax, (%r12)
leaq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdi
callq 0x503a
imull %ebx, %ebx
movl -0x2c(%rbp), %r14d
cmpl %r14d, %ebx
jge 0x425f
movq %r15, (%rsp)
leaq -0x90(%rbp), %rdi
leaq -0x60(%rbp), %rdx
leaq -0x78(%rbp), %r8
movq %r13, %rsi
movq -0x38(%rbp), %rcx
movq %r12, %r9
callq 0x3ee4
movaps -0x90(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0x90(%rbp)
movq -0x48(%rbp), %rdi
movups %xmm0, -0x50(%rbp)
testq %rdi, %rdi
je 0x41dd
callq 0x508e
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0x41ee
callq 0x508e
movq -0x50(%rbp), %rcx
movq 0x8(%rcx), %rdi
movq 0x10(%rcx), %r8
cmpq %r8, %rdi
je 0x425f
leaq -0x48(%rbp), %rsi
subq %rdi, %r8
sarq $0x2, %r8
movq -0x38(%rbp), %rax
movq (%rax), %r9
movq 0x8(%rax), %rdx
subq %r9, %rdx
sarq $0x2, %rdx
cmpq $0x1, %r8
adcq $0x0, %r8
xorl %r10d, %r10d
xorl %eax, %eax
cmpq %rax, %rdx
je 0x42bd
movl (%rdi,%rax,4), %r11d
subl (%r9,%rax,4), %r11d
imull %r11d, %r11d
addl %r11d, %r10d
incq %rax
cmpq %rax, %r8
jne 0x4229
cmpl %r14d, %r10d
jge 0x425f
movl %r10d, -0x2c(%rbp)
movq %rcx, (%r12)
movq -0x40(%rbp), %rdi
callq 0x503a
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x426d
callq 0x508e
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x427b
callq 0x508e
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x4289
callq 0x508e
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x429a
callq 0x3230
movq %r12, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xed6a(%rip), %rdi      # 0x13020
xorl %eax, %eax
callq 0x3330
leaq 0xed5c(%rip), %rdi      # 0x13020
movq %rax, %rsi
xorl %eax, %eax
callq 0x3330
movq %r15, %rdx
leaq 0xed48(%rip), %rdi      # 0x13020
xorl %eax, %eax
callq 0x3330
jmp 0x42e8
movq %rax, %rbx
jmp 0x42f9
jmp 0x42e8
movq %rax, %rbx
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x42f9
callq 0x508e
movq -0x58(%rbp), %rdi
testq %rdi, %rdi
je 0x4307
callq 0x508e
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0x4315
callq 0x508e
movq -0x40(%rbp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x432b
callq 0x508e
jmp 0x432b
movq %rax, %rbx
movq -0xb8(%rbp), %rdi
testq %rdi, %rdi
je 0x433c
callq 0x3230
movq %rbx, %rdi
callq 0x33d0