// File: tylerwowen[P]movielens/./movielens/neighborsfinder.cpp
// Function: NeighborsLocator::cosineSimilarity(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::cosineSimilarity(Ratings_list &r1, Ratings_list &r2) {
  double dotProduct = 0, norm1SQ = 0, norm2SQ = 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++;
    }
    dotProduct += rating1 * rating2;
    norm1SQ += rating1 * rating1;
    norm2SQ += rating2 * rating2;
  }
  
  Rating r1Last = r1.back();
  while (iter2 != r2.cend()) {
    double rating1 = 0, rating2 = iter2->second;
    if (iter2->first != r1Last.first) {
      rating1 = defaultRatingForItem(iter2->first);
    }
    else {
      rating1 = r1Last.second;
    }
    iter2++;
    
    dotProduct += rating1 * rating2;
    norm1SQ += rating1 * rating1;
    norm2SQ += rating2 * rating2;
  }
  Rating r2Last = r2.back();
  while (iter1 != r1.cend()) {
    double rating1 = iter1->second, rating2 = 0;
    if (iter2->first != r1Last.first) {
      rating2 = defaultRatingForItem(iter1->first);
    }
    else {
      rating2 = r2Last.second;
    }
    iter1++;
    
    dotProduct += rating1 * rating2;
    norm1SQ += rating1 * rating1;
    norm2SQ += rating2 * rating2;
  }

  // TODO: handle non-0 situations
  return dotProduct/(sqrt(norm1SQ) * sqrt(norm2SQ));
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
movq %rdx, 0xe0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xd8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xd0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xc8(%rsp)
movq 0xe8(%rsp), %rdi
callq 0xc020
movq %rax, 0xc0(%rsp)
movq 0xe0(%rsp), %rdi
callq 0xc020
movq %rax, 0xb8(%rsp)
movq 0xe8(%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, 0x17(%rsp)
jne 0x9a86
jmp 0x9ab4
movq 0xe0(%rsp), %rdi
callq 0xc050
movq %rax, 0xa8(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x4f30
movb %al, 0x17(%rsp)
movb 0x17(%rsp), %al
testb $0x1, %al
jne 0x9ac1
jmp 0x9cb0
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, 0x10(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0xc080
movq %rax, %rcx
movl 0x10(%rsp), %eax
cmpl (%rcx), %eax
jge 0x9b5c
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 0x9c3f
leaq 0xc0(%rsp), %rdi
callq 0xc080
movl (%rax), %eax
movl %eax, 0xc(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0xc080
movq %rax, %rcx
movl 0xc(%rsp), %eax
cmpl (%rcx), %eax
jne 0x9bed
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 0x9c3d
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 0x9c3f
movsd 0xa0(%rsp), %xmm0
mulsd 0x98(%rsp), %xmm0
addsd 0xd8(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
movsd 0xa0(%rsp), %xmm0
mulsd 0xa0(%rsp), %xmm0
addsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0x98(%rsp), %xmm0
mulsd 0x98(%rsp), %xmm0
addsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
jmp 0x9a4d
movq 0xe8(%rsp), %rdi
callq 0xc0e0
movq (%rax), %rcx
movq %rcx, 0x68(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x70(%rsp)
movq 0xe0(%rsp), %rdi
callq 0xc050
movq %rax, 0x60(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4f30
testb $0x1, %al
jne 0x9cfb
jmp 0x9dd0
xorps %xmm0, %xmm0
movsd %xmm0, 0x58(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0xc080
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0xc080
movl (%rax), %eax
cmpl 0x68(%rsp), %eax
je 0x9d51
leaq 0xb8(%rsp), %rdi
callq 0xc080
movl (%rax), %edi
callq 0xbbb0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x58(%rsp)
jmp 0x9d5d
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
leaq 0xb8(%rsp), %rdi
xorl %esi, %esi
callq 0xc0b0
movq %rax, 0x48(%rsp)
movsd 0x58(%rsp), %xmm0
mulsd 0x50(%rsp), %xmm0
addsd 0xd8(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
movsd 0x58(%rsp), %xmm0
mulsd 0x58(%rsp), %xmm0
addsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0x50(%rsp), %xmm0
mulsd 0x50(%rsp), %xmm0
addsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
jmp 0x9cce
movq 0xe0(%rsp), %rdi
callq 0xc0e0
movq (%rax), %rcx
movq %rcx, 0x38(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0xe8(%rsp), %rdi
callq 0xc050
movq %rax, 0x30(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x4f30
testb $0x1, %al
jne 0x9e1b
jmp 0x9ef0
leaq 0xc0(%rsp), %rdi
callq 0xc080
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0xc080
movl (%rax), %eax
cmpl 0x68(%rsp), %eax
je 0x9e71
leaq 0xc0(%rsp), %rdi
callq 0xc080
movl (%rax), %edi
callq 0xbbb0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x20(%rsp)
jmp 0x9e7d
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0xc0(%rsp), %rdi
xorl %esi, %esi
callq 0xc0b0
movq %rax, 0x18(%rsp)
movsd 0x28(%rsp), %xmm0
mulsd 0x20(%rsp), %xmm0
addsd 0xd8(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
movsd 0x28(%rsp), %xmm0
mulsd 0x28(%rsp), %xmm0
addsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0x20(%rsp), %xmm0
mulsd 0x20(%rsp), %xmm0
addsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
jmp 0x9dee
movsd 0xd8(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm1
sqrtsd %xmm1, %xmm1
movsd 0xc8(%rsp), %xmm2
sqrtsd %xmm2, %xmm2
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
addq $0xf8, %rsp
retq
nopw %cs:(%rax,%rax)