// File: tylerwowen[P]movielens/./movielens/neighborsfinder.cpp
// Function: NeighborsLocator::euclideanDistance(std::__cxx11::list<std::pair<int, double>, std::allocator<std::pair<int, double>>>&, std::__cxx11::list<std::pair<int, double>, std::allocator<std::pair<int, double>>>&)
double NeighborsLocator::euclideanDistance(Ratings_list &r1, Ratings_list &r2) {
  double distance = 0;
  Ratings_list::const_iterator iter1 = r1.cbegin(), iter2 = r2.cbegin();
  while (iter1 != r1.cend() && iter2 != r2.cend()) {
    double rating1 = 0, rating2 = 0;
    
    if (iter1->first < iter2->first) {
      rating1 = iter1->second;
      rating2 = defaultRatingForItem(iter1->first);
      iter1++;
    }
    else if (iter1->first == iter2->first) {
      rating1 = iter1->second;
      rating2 = iter2->second;
      iter1++;
      iter2++;
    }
    else {
      rating1 = defaultRatingForItem(iter2->first);
      rating2 = iter2->second;
      iter2++;
    }
    distance += pow(rating1 - rating2, 2);
  }
  
  Rating last = iter1 == r1.cend() ? r1.back() : r2.back();
  Ratings_list::const_iterator activeIter = iter1 == r1.cend() ? iter2 : iter1,
  activeIterCend = iter1 == r1.cend() ? r2.cend() : r1.cend();
  
  while (activeIter != activeIterCend) {
    double rating1 = activeIter->second, rating2 = 0;
    if (activeIter->first != last.first) {
      rating2 = defaultRatingForItem(activeIter->first);
    }
    else {
      rating2 = last.second;
    }
    activeIter++;
    distance += pow(rating1 - rating2, 2);
  }
  return sqrt(distance);
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
movq %rdx, 0xd0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xc8(%rsp)
movq 0xd8(%rsp), %rdi
callq 0xc020
movq %rax, 0xc0(%rsp)
movq 0xd0(%rsp), %rdi
callq 0xc020
movq %rax, 0xb8(%rsp)
movq 0xd8(%rsp), %rdi
callq 0xc050
movq %rax, 0xb0(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x4f30
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x27(%rsp)
jne 0xa5ae
jmp 0xa5dc
movq 0xd0(%rsp), %rdi
callq 0xc050
movq %rax, 0xa8(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x4f30
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0xa5e9
jmp 0xa79a
xorps %xmm0, %xmm0
movsd %xmm0, 0xa0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x98(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0xc080
movl (%rax), %eax
movl %eax, 0x20(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0xc080
movq %rax, %rcx
movl 0x20(%rsp), %eax
cmpl (%rcx), %eax
jge 0xa684
leaq 0xc0(%rsp), %rdi
callq 0xc080
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0xa0(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0xc080
movl (%rax), %edi
callq 0xbbb0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x98(%rsp)
leaq 0xc0(%rsp), %rdi
xorl %esi, %esi
callq 0xc0b0
movq %rax, 0x90(%rsp)
jmp 0xa767
leaq 0xc0(%rsp), %rdi
callq 0xc080
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0xc080
movq %rax, %rcx
movl 0x1c(%rsp), %eax
cmpl (%rcx), %eax
jne 0xa715
leaq 0xc0(%rsp), %rdi
callq 0xc080
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0xa0(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0xc080
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x98(%rsp)
leaq 0xc0(%rsp), %rdi
xorl %esi, %esi
callq 0xc0b0
movq %rax, 0x88(%rsp)
leaq 0xb8(%rsp), %rdi
xorl %esi, %esi
callq 0xc0b0
movq %rax, 0x80(%rsp)
jmp 0xa765
leaq 0xb8(%rsp), %rdi
callq 0xc080
movl (%rax), %edi
callq 0xbbb0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0xa0(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0xc080
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x98(%rsp)
leaq 0xb8(%rsp), %rdi
xorl %esi, %esi
callq 0xc0b0
movq %rax, 0x78(%rsp)
jmp 0xa767
movsd 0xa0(%rsp), %xmm0
subsd 0x98(%rsp), %xmm0
movl $0x2, %edi
callq 0x8330
addsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
jmp 0xa575
movq 0xd8(%rsp), %rdi
callq 0xc050
movq %rax, 0x60(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4940
testb $0x1, %al
jne 0xa7c4
jmp 0xa7d8
movq 0xd8(%rsp), %rdi
callq 0xc0e0
movq %rax, 0x10(%rsp)
jmp 0xa7ea
movq 0xd0(%rsp), %rdi
callq 0xc0e0
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x68(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x70(%rsp)
movq 0xd8(%rsp), %rdi
callq 0xc050
movq %rax, 0x50(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x4940
testb $0x1, %al
jne 0xa82a
jmp 0xa839
leaq 0xb8(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0xa848
leaq 0xc0(%rsp), %rax
movq %rax, 0x8(%rsp)
jmp 0xa848
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq 0xd8(%rsp), %rdi
callq 0xc050
movq %rax, 0x40(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x4940
testb $0x1, %al
jne 0xa87f
jmp 0xa893
movq 0xd0(%rsp), %rdi
callq 0xc050
movq %rax, 0x48(%rsp)
jmp 0xa8a5
movq 0xd8(%rsp), %rdi
callq 0xc050
movq %rax, 0x48(%rsp)
jmp 0xa8a7
leaq 0x58(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x4f30
testb $0x1, %al
jne 0xa8bf
jmp 0xa956
leaq 0x58(%rsp), %rdi
callq 0xc080
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x58(%rsp), %rdi
callq 0xc080
movl (%rax), %eax
cmpl 0x68(%rsp), %eax
je 0xa90c
leaq 0x58(%rsp), %rdi
callq 0xc080
movl (%rax), %edi
callq 0xbbb0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x30(%rsp)
jmp 0xa918
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x58(%rsp), %rdi
xorl %esi, %esi
callq 0xc0b0
movq %rax, 0x28(%rsp)
movsd 0x38(%rsp), %xmm0
subsd 0x30(%rsp), %xmm0
movl $0x2, %edi
callq 0x8330
addsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
jmp 0xa8a7
movsd 0xc8(%rsp), %xmm0
sqrtsd %xmm0, %xmm0
addq $0xe8, %rsp
retq
nopl (%rax,%rax)