// File: /workspace/github_cpp_makefile_star2_O2/tylerwowen[P]movielens/./movielens/main.cpp
// Function: main
int main(int argc, char ** argv) {
  struct arguments args;
  args.method = 0;
  args.matchedOnly = false;
  args.prettyPrint = false;
  args.maxRating = 5;
  
  argp_parse (&argp, argc, argv, 0, 0, &args);
  
  UsersMap trainUsers(args.userNum), testUsers;
  readData(args.trainFile, trainUsers);
  int testSize = readData(args.testFile, testUsers);
  int validTestSize = testSize;

  NeighborsLocator locator(&trainUsers, args.moiveNum, args.k, args.method, args.maxRating);
  double sum = 0, sumSQ = 0;
  int predictedCount = 0;
  int count_not_found = 0;
  if (args.matchedOnly) {
    for (auto& user: testUsers) {
      UsersMap::iterator trainUserItr = trainUsers.find(user.first);
      if (trainUserItr == trainUsers.end()) {
        cout << "not good " << count_not_found++ << " " << user.first << endl;
        validTestSize--;
        continue;
      }
      locator.setTargetUser(user.first, &trainUserItr->second);
      locator.calculateDistancesToNeighbors(args.userNum);
      
      for (auto& rating: user.second.r_list) {
        UsersPtr neighbors = locator.getMatchedKNeighbors(rating.first);
        double actual = rating.second;
        double prediction = getPredication(neighbors, rating.first);
        if (prediction == 0) {
          continue;
        }
        predictedCount++;
        sum += fabs(actual - prediction);
        sumSQ += pow(actual - prediction, 2);
      }
    }
  }
  else {
    for (auto& user: testUsers) {
      UsersMap::iterator trainUserItr = trainUsers.find(user.first);
      if (trainUserItr == trainUsers.end()) {
        cout << "not good\n" ;
        validTestSize--;
        continue;
      }
      locator.setTargetUser(user.first, &trainUserItr->second);
      UsersPtr neighbors = locator.getNeighbors();
      
      for (auto& rating: user.second.r_list) {
        double actual = rating.second;
        double prediction = getPredication(neighbors, rating.first);
        if (prediction == 0) {
          continue;
        }
        predictedCount++;
        sum += fabs(actual - prediction);
        sumSQ += pow(actual - prediction, 2);
      }
    }
  }
  double mae = sum / predictedCount,
  rmse = sqrt(sumSQ / predictedCount),
  recall = (double)predictedCount / (double)validTestSize;
  
  if (args.prettyPrint) {
    cout << "MAE = " << mae << endl;
    cout << "RMSE = " << rmse << endl;
    cout << "Recall = " << recall * 100 << "%" << endl;
  }
  else {
    cout << mae << " " << rmse << " " <<  recall << " ";
  }
  
  exit(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rsi, %rdx
movl %edi, %esi
leaq 0x68(%rsp), %rbx
andl $0x0, 0x18(%rbx)
andw $0x0, 0x24(%rbx)
movl $0x5, 0x20(%rbx)
leaq 0x6bf5(%rip), %rdi      # 0xa1b0
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rbx, %r9
callq 0x2040
movslq 0x10(%rbx), %rsi
leaq 0xd8(%rsp), %rdi
leaq 0x90(%rsp), %rdx
leaq 0x110(%rsp), %rcx
leaq 0x50(%rsp), %r8
callq 0x3c28
leaq 0x140(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
movq (%rbx), %rsi
leaq 0x90(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x20a0
leaq 0x90(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x2d74
leaq 0x90(%rsp), %rdi
callq 0x20c0
movq 0x70(%rsp), %rsi
leaq 0x90(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x20a0
leaq 0x90(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x2d74
movl %eax, %ebx
leaq 0x90(%rsp), %rdi
callq 0x20c0
movl 0x84(%rsp), %ecx
movl 0x7c(%rsp), %edx
movl 0x80(%rsp), %r8d
movl 0x88(%rsp), %r9d
leaq 0x90(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x3d20
movl %ebx, 0xc(%rsp)
movq 0x120(%rsp), %r14
cmpb $0x0, 0x8c(%rsp)
je 0x3840
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
leaq 0x90(%rsp), %r15
leaq 0x50(%rsp), %r12
leaq 0x160(%rsp), %r13
xorl %ebx, %ebx
testq %r14, %r14
je 0x3960
movq %r14, 0x48(%rsp)
addq $0x8, %r14
leaq 0xd8(%rsp), %rdi
movq %r14, %rsi
callq 0x3c90
testq %rax, %rax
je 0x37ea
movl (%r14), %esi
addq $0x10, %rax
movq %r15, %rdi
movq %rax, %rdx
callq 0x3d3e
movl 0x78(%rsp), %esi
movq %r15, %rdi
callq 0x44a4
movq 0x48(%rsp), %rax
leaq 0x10(%rax), %rbp
movq %rbp, %r14
movq (%r14), %r14
cmpq %rbp, %r14
je 0x3833
movl 0x10(%r14), %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x4578
movsd 0x18(%r14), %xmm0
movapd %xmm0, 0x30(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x3b30
movl 0x10(%r14), %esi
movq %r13, %rdi
callq 0x3500
movq %r13, %rdi
movsd %xmm0, 0x10(%rsp)
callq 0x3cae
movsd 0x10(%rsp), %xmm0
ucomisd 0x28e5(%rip), %xmm0     # 0x6090
je 0x37dd
incl %ebx
movapd 0x30(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
andpd 0x28ab(%rip), %xmm0     # 0x6070
mulsd %xmm1, %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movapd 0x20(%rsp), %xmm0
addpd %xmm1, %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r12, %rdi
callq 0x3cae
jmp 0x3751
movq 0x67df(%rip), %rdi      # 0x9fd0
leaq 0x28a0(%rip), %rsi      # 0x6098
callq 0x21b0
movq %rax, %rdi
movl 0x1c(%rsp), %esi
callq 0x22d0
movq %rax, %rdi
leaq 0x289e(%rip), %rsi      # 0x60b1
callq 0x21b0
movl (%r14), %esi
movq %rax, %rdi
callq 0x22d0
movq %rax, %rdi
callq 0x20d0
incl 0x1c(%rsp)
decl 0xc(%rsp)
movq 0x48(%rsp), %r14
movq (%r14), %r14
jmp 0x36fc
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %ebx, %ebx
leaq 0x90(%rsp), %r13
leaq 0x50(%rsp), %rbp
leaq 0x148(%rsp), %r15
testq %r14, %r14
je 0x3960
leaq 0x8(%r14), %r12
leaq 0xd8(%rsp), %rdi
movq %r12, %rsi
callq 0x3c90
testq %rax, %rax
je 0x3941
movl (%r12), %esi
addq $0x10, %rax
movq %r13, %rdi
movq %rax, %rdx
callq 0x3d3e
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3d46
movq %r14, %r13
addq $0x10, %r14
movq %r14, %r12
movq (%r12), %r12
cmpq %r14, %r12
je 0x392c
movsd 0x18(%r12), %xmm0
movaps %xmm0, 0x30(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x3b30
movl 0x10(%r12), %esi
movq %r15, %rdi
callq 0x3500
movq %r15, %rdi
movsd %xmm0, 0x10(%rsp)
callq 0x3cae
movsd 0x10(%rsp), %xmm0
ucomisd 0x2798(%rip), %xmm0     # 0x6090
je 0x38af
incl %ebx
movapd 0x30(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
andpd 0x275e(%rip), %xmm0     # 0x6070
mulsd %xmm1, %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movapd 0x20(%rsp), %xmm0
addpd %xmm1, %xmm0
movapd %xmm0, 0x20(%rsp)
jmp 0x38af
movq %rbp, %rdi
callq 0x3cae
movq %r13, %r14
leaq 0x90(%rsp), %r13
jmp 0x3958
movq 0x6688(%rip), %rdi      # 0x9fd0
leaq 0x2753(%rip), %rsi      # 0x60a2
callq 0x21b0
decl 0xc(%rsp)
movq (%r14), %r14
jmp 0x3861
cvtsi2sd %ebx, %xmm2
movapd 0x20(%rsp), %xmm1
movapd %xmm1, %xmm3
unpckhpd %xmm1, %xmm3            # xmm3 = xmm3[1],xmm1[1]
movsd 0x2706(%rip), %xmm0     # 0x6080
divsd %xmm2, %xmm0
mulsd %xmm0, %xmm3
mulsd %xmm1, %xmm0
cvtsi2sdl 0xc(%rsp), %xmm1
sqrtsd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
divsd %xmm1, %xmm2
movsd %xmm2, 0x30(%rsp)
cmpb $0x1, 0x8d(%rsp)
jne 0x3a48
movapd %xmm3, 0x20(%rsp)
movq 0x6615(%rip), %rdi      # 0x9fd0
leaq 0x26ea(%rip), %rsi      # 0x60ac
callq 0x21b0
movq %rax, %rdi
movapd 0x20(%rsp), %xmm0
callq 0x2280
movq %rax, %rdi
callq 0x20d0
movq 0x65ec(%rip), %rdi      # 0x9fd0
leaq 0x26c8(%rip), %rsi      # 0x60b3
callq 0x21b0
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x2280
movq %rax, %rdi
callq 0x20d0
movq 0x65c3(%rip), %rdi      # 0x9fd0
leaq 0x26a7(%rip), %rsi      # 0x60bb
callq 0x21b0
movsd 0x30(%rsp), %xmm0
mulsd 0x2661(%rip), %xmm0     # 0x6088
movq %rax, %rdi
callq 0x2280
leaq 0x268f(%rip), %rsi      # 0x60c5
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
callq 0x20d0
jmp 0x3aa1
movq 0x6581(%rip), %rdi      # 0x9fd0
movapd %xmm3, %xmm0
callq 0x2280
leaq 0x2652(%rip), %rsi      # 0x60b1
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x2280
leaq 0x2635(%rip), %rsi      # 0x60b1
movq %rax, %rdi
callq 0x21b0
movq %rax, %rdi
movsd 0x30(%rsp), %xmm0
callq 0x2280
leaq 0x2618(%rip), %rsi      # 0x60b1
movq %rax, %rdi
callq 0x21b0
xorl %edi, %edi
callq 0x21f0
jmp 0x3ab0
jmp 0x3ab0
jmp 0x3ab0
jmp 0x3ab0
movq %rax, %rbx
jmp 0x3ae3
jmp 0x3ac4
movq %rax, %rbx
leaq 0x160(%rsp), %rdi
jmp 0x3ad4
movq %rax, %rbx
jmp 0x3ad9
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
callq 0x3cae
leaq 0x50(%rsp), %rdi
callq 0x3cae
leaq 0xb8(%rsp), %rdi
callq 0x3b7e
jmp 0x3b0d
jmp 0x3b0a
jmp 0x3af8
jmp 0x3b0a
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x20c0
jmp 0x3b0d
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x3b8c
leaq 0xd8(%rsp), %rdi
callq 0x3b8c
movq %rbx, %rdi
callq 0x22f0
nop