// File: mitchellsayer[P]FastPointCloudSegmentation/src/./point_cloud_segmenter.cc
// Function: PointCloudSegmenter::UpdateLabels(Scanline&, Scanline&)
void PointCloudSegmenter::UpdateLabels(Scanline &scan_current, Scanline &scan_above) {
  std::vector<int> labels_to_merge;
  std::vector<int>::iterator i_it;
  int start_index;
  int end_index;

  if (scan_current.s_queue.size() != scan_current.e_queue.size()) {
    std:: cout << "Start & End: " << scan_current.s_queue.size() << "  " << scan_current.e_queue.size() << std::endl;
    return;
  }

  for (int i = 0; i < scan_current.s_queue.size(); i++) {
    start_index = scan_current.s_queue[i];
    end_index = scan_current.e_queue[i];

    for (int k = start_index; k <= end_index; k++) {
      if (scan_current.points[k].label != -3) {
        int nearest_label = FindNearestNeighbor(scan_current, scan_above, start_index, end_index, k);

        if (nearest_label != -1 && std::find(labels_to_merge.begin(), labels_to_merge.end(), nearest_label) == labels_to_merge.end()) {
          labels_to_merge.push_back( nearest_label );
        }
      }
    }

    if (labels_to_merge.size() == 0) {
      SetRunLabel(scan_current, start_index, end_index, this->new_label);
      this->new_label += 1;
      this->next.push_back(-1);
      this->tail.push_back(this->new_label);
      this->rtable.push_back(this->new_label);
    }

    else {
      int min_label = *std::min_element(labels_to_merge.begin(), labels_to_merge.end());;

      SetRunLabel(scan_current, start_index, end_index, min_label);
      MergeLabels(labels_to_merge, min_label);
    }

    labels_to_merge.clear();
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb0(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x6010
leaq -0x38(%rbp), %rdi
callq 0x5f60
movq -0x10(%rbp), %rdi
callq 0x68a0
movq %rax, -0xa8(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x68a0
movq %rax, %rcx
movq -0xa8(%rbp), %rax
cmpq %rcx, %rax
je 0x462d
movq 0x15a45(%rip), %rdi     # 0x19fc0
leaq 0xdb60(%rip), %rsi      # 0x120e2
callq 0x21d0
movq %rax, -0xb8(%rbp)
jmp 0x4590
movq -0x10(%rbp), %rdi
callq 0x68a0
movq -0xb8(%rbp), %rdi
movq %rax, %rsi
callq 0x2040
movq %rax, -0xc0(%rbp)
jmp 0x45b1
movq -0xc0(%rbp), %rdi
leaq 0xdb31(%rip), %rsi      # 0x120f0
callq 0x21d0
movq %rax, -0xc8(%rbp)
jmp 0x45cd
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x68a0
movq -0xc8(%rbp), %rdi
movq %rax, %rsi
callq 0x2040
movq %rax, -0xd0(%rbp)
jmp 0x45f2
movq -0xd0(%rbp), %rdi
movq 0x159a0(%rip), %rsi     # 0x19fa0
callq 0x2200
jmp 0x4607
movl $0x1, -0x50(%rbp)
jmp 0x48ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x6060
jmp 0x4900
movl $0x0, -0x54(%rbp)
movslq -0x54(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x10(%rbp), %rdi
callq 0x68a0
movq %rax, %rcx
movq -0xd8(%rbp), %rax
cmpq %rcx, %rax
jae 0x48e7
movq -0x10(%rbp), %rdi
movslq -0x54(%rbp), %rsi
callq 0x68c0
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movslq -0x54(%rbp), %rsi
callq 0x68c0
movl (%rax), %eax
movl %eax, -0x40(%rbp)
movl -0x3c(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
cmpl -0x40(%rbp), %eax
jg 0x4795
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
movslq -0x58(%rbp), %rsi
callq 0x69e0
movss 0x18(%rax), %xmm0
movss 0xd959(%rip), %xmm1     # 0x1200c
ucomiss %xmm1, %xmm0
jne 0x46bf
jp 0x46bf
jmp 0x4785
movq -0xb0(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movl -0x3c(%rbp), %ecx
movl -0x40(%rbp), %r8d
movl -0x58(%rbp), %r9d
callq 0x4ba0
movl %eax, -0xdc(%rbp)
jmp 0x46e6
movl -0xdc(%rbp), %eax
movl %eax, -0x5c(%rbp)
xorl %eax, %eax
cmpl $-0x1, -0x5c(%rbp)
movb %al, -0xdd(%rbp)
je 0x4766
leaq -0x30(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x6b10
movq -0xf0(%rbp), %rdi
movq %rax, -0x70(%rbp)
callq 0x6b40
movq %rax, -0x78(%rbp)
movq -0x70(%rbp), %rdi
movq -0x78(%rbp), %rsi
leaq -0x5c(%rbp), %rdx
callq 0x6ac0
movq %rax, -0xe8(%rbp)
jmp 0x473b
movq -0xe8(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x6b40
movq %rax, -0x80(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x6a80
movb %al, -0xdd(%rbp)
movb -0xdd(%rbp), %al
testb $0x1, %al
jne 0x4772
jmp 0x4783
leaq -0x30(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
callq 0x5eb0
jmp 0x4781
jmp 0x4783
jmp 0x4785
jmp 0x4787
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x4689
leaq -0x30(%rbp), %rdi
callq 0x68a0
cmpq $0x0, %rax
jne 0x482f
movq -0xb0(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x3c(%rbp), %edx
movl -0x40(%rbp), %ecx
movl 0x68(%rdi), %r8d
callq 0x44b0
jmp 0x47c4
movq -0xb0(%rbp), %rdi
movl 0x68(%rdi), %eax
incl %eax
movl %eax, 0x68(%rdi)
addq $0x88, %rdi
movl $0xffffffff, -0x84(%rbp) # imm = 0xFFFFFFFF
leaq -0x84(%rbp), %rsi
callq 0x5e80
jmp 0x47f2
movq -0xb0(%rbp), %rsi
movq %rsi, %rdi
addq $0xa0, %rdi
addq $0x68, %rsi
callq 0x5eb0
jmp 0x480e
movq -0xb0(%rbp), %rsi
movq %rsi, %rdi
addq $0xb8, %rdi
addq $0x68, %rsi
callq 0x5eb0
jmp 0x482a
jmp 0x48d0
leaq -0x30(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x6b10
movq -0x100(%rbp), %rdi
movq %rax, -0x98(%rbp)
callq 0x6b40
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0x6b70
movq %rax, -0xf8(%rbp)
jmp 0x4875
movq -0xf8(%rbp), %rax
movq %rax, -0x90(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x6bb0
movq -0xb0(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x88(%rbp)
movq -0x10(%rbp), %rsi
movl -0x3c(%rbp), %edx
movl -0x40(%rbp), %ecx
movl -0x88(%rbp), %r8d
callq 0x44b0
jmp 0x48b6
movq -0xb0(%rbp), %rdi
movl -0x88(%rbp), %edx
leaq -0x30(%rbp), %rsi
callq 0x4d70
jmp 0x48ce
jmp 0x48d0
leaq -0x30(%rbp), %rdi
callq 0x6bd0
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x4634
movl $0x0, -0x50(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x6060
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x2370
nopl (%rax)