// File: /workspace/github_cpp_makefile_star2_O2/tylerwowen[P]movielens/./movielens/neighborsfinder.cpp
// Function: NeighborsLocator::llr(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::llr(Ratings_list &r1, Ratings_list &r2) {
  vector<int> deltaCounter(maxRating, 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) {
      iter1++;
    }
    else if (iter1->first == iter2->first) {
      rating1 = iter1->second;
      rating2 = iter2->second;
      deltaCounter[(int)fabs(rating1 - rating2)]++;
      iter1++;
      iter2++;
    }
    else {
      iter2++;
    }
  }
  double accumulator = 1;
  accumulator *= pow(0.5, deltaCounter[0]) / pow(1.0 / (double)maxRating, deltaCounter[0]);
  
  for (int i = 1; i < maxRating - 1; i++) {
    double pA = pow(2, -i - 1),
    pRand = 2 * (maxRating - i) / (double)(maxRating * maxRating);
    accumulator *= pow(pA, deltaCounter[i]) / pow(pRand, deltaCounter[i]);
  }
  
  double pRand = 2 / (double)(maxRating * maxRating);
  accumulator *= pow(pow(2, -maxRating + 1), deltaCounter[maxRating-1]) / pow(pRand, deltaCounter[maxRating-1]);
  
  return log10(accumulator);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movslq 0x14(%rdi), %rsi
leaq 0x3c(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x40(%rsp), %r12
leaq 0x17(%rsp), %rcx
movq %r12, %rdi
callq 0x486c
movq (%r15), %rax
movq (%r12), %r12
movapd 0x1d5e(%rip), %xmm0     # 0x6070
movq %r14, %rcx
movq (%rcx), %rcx
cmpq %r15, %rax
je 0x434f
cmpq %r14, %rcx
je 0x434f
movl 0x10(%rcx), %edx
cmpl %edx, 0x10(%rax)
jge 0x432f
movq (%rax), %rax
jmp 0x4318
jne 0x4315
movsd 0x18(%rax), %xmm1
subsd 0x18(%rcx), %xmm1
andpd %xmm0, %xmm1
cvttsd2si %xmm1, %edx
movslq %edx, %rdx
incl (%r12,%rdx,4)
movq (%rax), %rax
jmp 0x4315
movl (%r12), %ebp
movsd 0x1f0d(%rip), %xmm0     # 0x6268
movl %ebp, %edi
callq 0x22c0
movsd %xmm0, 0x8(%rsp)
movslq 0x14(%rbx), %rbx
cvtsi2sd %ebx, %xmm1
movsd 0x1d08(%rip), %xmm0     # 0x6080
movsd %xmm0, 0x18(%rsp)
divsd %xmm1, %xmm0
negl %ebp
movl %ebp, %edi
callq 0x22c0
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq -0x1(%rbx), %r15
movl %ebx, %eax
imull %ebx, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x18(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movq %rbx, 0x30(%rsp)
leal -0x2(%rbx,%rbx), %r13d
pushq $0x1
popq %r14
pushq $-0x2
popq %rbx
movsd 0x1e9d(%rip), %xmm0     # 0x6270
cmpq %r15, %r14
jge 0x442d
movl %ebx, %edi
callq 0x22c0
cvtsi2sd %r13d, %xmm1
mulsd 0x18(%rsp), %xmm1
movsd %xmm1, 0x20(%rsp)
movl (%r12,%r14,4), %ebp
movl %ebp, %edi
callq 0x22c0
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
negl %ebp
movsd 0x20(%rsp), %xmm0
movl %ebp, %edi
callq 0x22c0
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
incq %r14
addl $-0x2, %r13d
decl %ebx
jmp 0x43cb
movapd %xmm0, %xmm1
divsd 0x28(%rsp), %xmm1
movsd %xmm1, 0x20(%rsp)
pushq $0x1
popq %rdi
movq 0x30(%rsp), %rbx
subl %ebx, %edi
callq 0x22c0
movl -0x4(%r12,%rbx,4), %ebx
movl %ebx, %edi
callq 0x22c0
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
negl %ebx
movsd 0x20(%rsp), %xmm0
movl %ebx, %edi
callq 0x22c0
mulsd 0x8(%rsp), %xmm0
callq 0x20e0
movsd %xmm0, 0x8(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x4e38
movsd 0x8(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop