// File: tylerwowen[P]movielens/./movielens/neighborsfinder.cpp
// Function: NeighborsLocator::getMatchedKNeighbors(int)
UsersPtr NeighborsLocator::getMatchedKNeighbors(int targetItem) {
  this->targetItemId = targetItem;
  Distances distances;
  distances.reserve(100);
  
  calculateDistances(distances);
  if (distances.size() > k) {
    switch (method) {
      case COS:
      case PCC:
      case LLR:
        sort_nth_elemet(distances, k + 1, false);
        break;
      case L1:
      case L2:
        sort_nth_elemet(distances, k + 1, true);
        break;
      default:
        break;
    }
  }
  
  UsersPtr neighbors;
  int end = k < distances.size() ? k : (int)distances.size();
  for (int i = 0, j = 0; i < end; j++) {
    int userId = distances[j].first;
    if (userId == targetUserId) {
      if (end != k) {
        end--;
      }
      continue;
    }
    neighbors.push_back(&((*trainUsers)[userId]));
    i++;
  }
  return neighbors;
}

// Assembly:
subq $0x98, %rsp
movq %rdi, 0x20(%rsp)
movq %rdi, %rax
movq %rax, 0x28(%rsp)
movq %rdi, 0x90(%rsp)
movq %rsi, 0x88(%rsp)
movl %edx, 0x84(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x30(%rsp)
movl 0x84(%rsp), %ecx
movl %ecx, 0x40(%rax)
leaq 0x68(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0xbe20
movq 0x38(%rsp), %rdi
movl $0x64, %esi
callq 0xbe30
jmp 0xb872
movq 0x30(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0xba80
jmp 0xb883
leaq 0x68(%rsp), %rdi
callq 0xbf40
movq 0x30(%rsp), %rcx
movslq 0xc(%rcx), %rcx
cmpq %rcx, %rax
jbe 0xb916
movq 0x30(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0x1c(%rsp)
testl %eax, %eax
je 0xb8db
jmp 0xb8ad
movl 0x1c(%rsp), %eax
decl %eax
subl $0x2, %eax
jb 0xb8f5
jmp 0xb8ba
movl 0x1c(%rsp), %eax
addl $-0x3, %eax
subl $0x2, %eax
jb 0xb8db
jmp 0xb912
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
jmp 0xba5e
movq 0x30(%rsp), %rax
movl 0xc(%rax), %esi
incl %esi
leaq 0x68(%rsp), %rdi
xorl %edx, %edx
callq 0x9f30
jmp 0xb8f3
jmp 0xb914
movq 0x30(%rsp), %rax
movl 0xc(%rax), %esi
incl %esi
leaq 0x68(%rsp), %rdi
movl $0x1, %edx
callq 0x9f30
jmp 0xb910
jmp 0xb914
jmp 0xb914
jmp 0xb916
movq 0x20(%rsp), %rdi
movb $0x0, 0x5b(%rsp)
callq 0xbf60
movq 0x30(%rsp), %rax
movslq 0xc(%rax), %rax
movq %rax, 0x10(%rsp)
leaq 0x68(%rsp), %rdi
callq 0xbf40
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
jae 0xb958
movq 0x30(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0xc(%rsp)
jmp 0xb966
leaq 0x68(%rsp), %rdi
callq 0xbf40
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x54(%rsp)
movl $0x0, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
movl 0x50(%rsp), %eax
cmpl 0x54(%rsp), %eax
jge 0xba31
movslq 0x4c(%rsp), %rsi
leaq 0x68(%rsp), %rdi
callq 0xbd70
movq 0x30(%rsp), %rcx
movl (%rax), %eax
movl %eax, 0x48(%rsp)
movl 0x48(%rsp), %eax
cmpl 0x18(%rcx), %eax
jne 0xb9ca
movq 0x30(%rsp), %rcx
movl 0x54(%rsp), %eax
cmpl 0xc(%rcx), %eax
je 0xb9c8
movl 0x54(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0xba07
movq 0x30(%rsp), %rax
movq (%rax), %rdi
leaq 0x48(%rsp), %rsi
callq 0x5640
movq %rax, (%rsp)
jmp 0xb9e2
movq 0x20(%rsp), %rdi
movq (%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0x40(%rsp), %rsi
callq 0xbf70
jmp 0xb9fc
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0xb97e
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
callq 0x82d0
jmp 0xba5e
movb $0x1, 0x5b(%rsp)
testb $0x1, 0x5b(%rsp)
jne 0xba47
movq 0x20(%rsp), %rdi
callq 0x82d0
leaq 0x68(%rsp), %rdi
callq 0xbd90
movq 0x28(%rsp), %rax
addq $0x98, %rsp
retq
leaq 0x68(%rsp), %rdi
callq 0xbd90
movq 0x60(%rsp), %rdi
callq 0x2310
nopw %cs:(%rax,%rax)