// File: mitchellsayer[P]FastPointCloudSegmentation/src/./point_cloud_segmenter.cc
// Function: PointCloudSegmenter::FindNearestNeighbor(Scanline&, Scanline&, int, int, int)
int PointCloudSegmenter::FindNearestNeighbor(Scanline& scan_current, Scanline& scan_above, int start_index, int end_index, int point_index) {
  double conversion_factor = scan_above.points.size() / scan_current.points.size();
  int above_start = floor(start_index * conversion_factor);
  int above_end = floor(end_index * conversion_factor);

  // Determine nearest point to search at
  Vec3 current = scan_current.points[point_index];
  float query[3] = {current.x, current.y, current.z};

  // Set up KNN result set
  size_t result_index;
  float result_dist_squared;
  nanoflann::KNNResultSet<float> results(1);
  results.init(&result_index, &result_dist_squared);

  // Perform search
  scan_above.tree->findNeighbors(results, &query[0], nanoflann::SearchParams(10));

  if (result_dist_squared < this->th_merge * this->th_merge) {
    return scan_above.tree_point_cloud->get_label(result_index);
  }

  return  -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x20(%rbp), %rdi
addq $0x48, %rdi
callq 0x5780
movq %rax, -0xc8(%rbp)
movq -0x18(%rbp), %rdi
addq $0x48, %rdi
callq 0x5780
movq %rax, %rcx
movq -0xc8(%rbp), %rax
xorl %edx, %edx
divq %rcx
movq %rax, %xmm0
movaps 0xd477(%rip), %xmm1     # 0x12080
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xd47b(%rip), %xmm1     # 0x12090
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
cvtsi2sdl -0x24(%rbp), %xmm0
mulsd -0x38(%rbp), %xmm0
callq 0x2330
cvttsd2si %xmm0, %eax
movl %eax, -0x3c(%rbp)
cvtsi2sdl -0x28(%rbp), %xmm0
mulsd -0x38(%rbp), %xmm0
callq 0x2330
cvttsd2si %xmm0, %eax
movl %eax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
addq $0x48, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x69e0
movq (%rax), %rcx
movq %rcx, -0x5c(%rbp)
movq 0x8(%rax), %rcx
movq %rcx, -0x54(%rbp)
movq 0x10(%rax), %rcx
movq %rcx, -0x4c(%rbp)
movl 0x18(%rax), %eax
movl %eax, -0x44(%rbp)
movss -0x5c(%rbp), %xmm0
movss %xmm0, -0x68(%rbp)
movss -0x58(%rbp), %xmm0
movss %xmm0, -0x64(%rbp)
movss -0x54(%rbp), %xmm0
movss %xmm0, -0x60(%rbp)
leaq -0x98(%rbp), %rdi
movl $0x1, %esi
callq 0x6c30
leaq -0x98(%rbp), %rdi
leaq -0x70(%rbp), %rsi
leaq -0x74(%rbp), %rdx
callq 0x6c70
movq -0x20(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0xc0(%rbp)
leaq -0x68(%rbp), %rax
movq %rax, -0xb8(%rbp)
leaq -0xa4(%rbp), %rdi
movl $0xa, %esi
xorps %xmm0, %xmm0
movl $0x1, %edx
callq 0x6e20
movq -0xc0(%rbp), %rdi
movq -0xb8(%rbp), %rdx
leaq -0x98(%rbp), %rsi
leaq -0xa4(%rbp), %rcx
callq 0x6cd0
movq -0xb0(%rbp), %rax
movss -0x74(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x78(%rax), %xmm0
mulsd 0x78(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x4d54
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rdi
movq -0x70(%rbp), %rsi
callq 0x6e60
cvttss2si %xmm0, %eax
movl %eax, -0x4(%rbp)
jmp 0x4d5b
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopw (%rax,%rax)