// File: /workspace/github_cpp_makefile_star2_O3/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 $0x168, %rsp            # imm = 0x168
movq %rsi, %rdx
movl %edi, %esi
leaq 0x58(%rsp), %rbx
movl $0x0, 0x18(%rbx)
movw $0x0, 0x24(%rbx)
movl $0x5, 0x20(%rbx)
leaq 0x5da7(%rip), %rdi      # 0x91c0
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rbx, %r9
callq 0x2050
movslq 0x10(%rbx), %rsi
leaq 0xf8(%rsp), %rdi
leaq 0x80(%rsp), %rdx
leaq 0x130(%rsp), %rcx
leaq 0x40(%rsp), %r8
callq 0x3bce
leaq 0x160(%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 0x80(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x20b0
leaq 0x80(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x2d14
leaq 0x90(%rsp), %r14
movq -0x10(%r14), %rdi
cmpq %r14, %rdi
je 0x34b5
callq 0x21b0
movq 0x60(%rsp), %rsi
leaq 0x80(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x20b0
leaq 0x80(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x2d14
movl %eax, %ebx
movq 0x80(%rsp), %rdi
cmpq %r14, %rdi
je 0x34f5
callq 0x21b0
movl 0x74(%rsp), %ecx
movl 0x6c(%rsp), %edx
movl 0x70(%rsp), %r8d
movl 0x78(%rsp), %r9d
leaq 0x80(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x3cdc
movq 0x140(%rsp), %r12
cmpb $0x1, 0x7c(%rsp)
movl %ebx, 0xc(%rsp)
jne 0x36e9
testq %r12, %r12
je 0x36f2
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %r13d, %r13d
leaq 0x40(%rsp), %r15
leaq 0xe0(%rsp), %rbp
movl $0x0, 0x1c(%rsp)
leaq 0x8(%r12), %r14
leaq 0xf8(%rsp), %rdi
movq %r14, %rsi
callq 0x3c64
testq %rax, %rax
je 0x365e
movl (%r14), %esi
addq $0x10, %rax
leaq 0x80(%rsp), %rdi
movq %rax, %rdx
callq 0x3cfe
movl 0x68(%rsp), %esi
leaq 0x80(%rsp), %rdi
callq 0x471e
leaq 0x10(%r12), %rbx
movq 0x10(%r12), %r14
cmpq %rbx, %r14
je 0x36da
movl 0x10(%r14), %edx
movq %r15, %rdi
leaq 0x80(%rsp), %rsi
callq 0x48d8
movsd 0x18(%r14), %xmm0
movapd %xmm0, 0x30(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x3a60
movl 0x10(%r14), %esi
movq %rbp, %rdi
callq 0x3358
movq 0xe0(%rsp), %rdi
testq %rdi, %rdi
je 0x360c
movsd %xmm0, 0x10(%rsp)
callq 0x21b0
movsd 0x10(%rsp), %xmm0
ucomisd 0x2a7c(%rip), %xmm0     # 0x6090
je 0x3647
incl %r13d
movapd 0x30(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
andpd 0x2a41(%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 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x3656
callq 0x21b0
movq (%r14), %r14
jmp 0x35ae
movl $0x9, %edx
movq 0x5966(%rip), %rdi      # 0x8fd0
leaq 0x2a27(%rip), %rsi      # 0x6098
callq 0x2210
movq 0x5953(%rip), %rdi      # 0x8fd0
movl 0x1c(%rsp), %esi
callq 0x2300
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0x2a19(%rip), %rsi      # 0x60b1
callq 0x2210
movl (%r14), %esi
movq %rbx, %rdi
callq 0x2300
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
incl 0x1c(%rsp)
decl 0xc(%rsp)
movq (%r12), %r12
testq %r12, %r12
jne 0x355e
jmp 0x36ff
testq %r12, %r12
jne 0x387b
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %r13d, %r13d
cvtsi2sd %r13d, %xmm2
movapd 0x20(%rsp), %xmm1
movapd %xmm1, %xmm3
unpckhpd %xmm1, %xmm3            # xmm3 = xmm3[1],xmm1[1]
movsd 0x2966(%rip), %xmm0     # 0x6080
divsd %xmm2, %xmm0
mulsd %xmm0, %xmm3
mulsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0xc(%rsp), %xmm1
sqrtsd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
divsd %xmm1, %xmm2
movsd %xmm2, 0x10(%rsp)
cmpb $0x1, 0x7d(%rsp)
jne 0x380b
movapd %xmm3, 0x20(%rsp)
movq 0x5875(%rip), %rdi      # 0x8fd0
leaq 0x294a(%rip), %rsi      # 0x60ac
movl $0x6, %edx
callq 0x2210
movq 0x585d(%rip), %rdi      # 0x8fd0
movapd 0x20(%rsp), %xmm0
callq 0x22b0
movq %rax, %rdi
callq 0x20d0
movq 0x5843(%rip), %rdi      # 0x8fd0
leaq 0x291f(%rip), %rsi      # 0x60b3
movl $0x7, %edx
callq 0x2210
movq 0x582b(%rip), %rdi      # 0x8fd0
movsd 0x30(%rsp), %xmm0
callq 0x22b0
movq %rax, %rdi
callq 0x20d0
movq 0x5811(%rip), %rdi      # 0x8fd0
leaq 0x28f5(%rip), %rsi      # 0x60bb
movl $0x9, %edx
callq 0x2210
movsd 0x10(%rsp), %xmm0
mulsd 0x28aa(%rip), %xmm0     # 0x6088
movq 0x57eb(%rip), %rdi      # 0x8fd0
callq 0x22b0
movq %rax, %rbx
leaq 0x28d1(%rip), %rsi      # 0x60c5
movl $0x1, %edx
movq %rax, %rdi
callq 0x2210
movq %rbx, %rdi
callq 0x20d0
jmp 0x3874
movq 0x57be(%rip), %rdi      # 0x8fd0
movapd %xmm3, %xmm0
callq 0x22b0
movq %rax, %rbx
leaq 0x288c(%rip), %rsi      # 0x60b1
movl $0x1, %edx
movq %rax, %rdi
callq 0x2210
movq %rbx, %rdi
movsd 0x30(%rsp), %xmm0
callq 0x22b0
movq %rax, %rbx
leaq 0x2867(%rip), %rsi      # 0x60b1
movl $0x1, %edx
movq %rax, %rdi
callq 0x2210
movq %rbx, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x22b0
leaq 0x2845(%rip), %rsi      # 0x60b1
movq %rax, %rdi
callq 0x21d0
xorl %edi, %edi
callq 0x2220
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %r13d, %r13d
leaq 0x80(%rsp), %r14
leaq 0x40(%rsp), %rbp
leaq 0xc8(%rsp), %r15
leaq 0x8(%r12), %rbx
leaq 0xf8(%rsp), %rdi
movq %rbx, %rsi
callq 0x3c64
testq %rax, %rax
je 0x3987
movl (%rbx), %esi
addq $0x10, %rax
movq %r14, %rdi
movq %rax, %rdx
callq 0x3cfe
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3d06
leaq 0x10(%r12), %rbx
movq 0x10(%r12), %r14
cmpq %rbx, %r14
je 0x396e
movsd 0x18(%r14), %xmm0
movapd %xmm0, 0x30(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x3a60
movl 0x10(%r14), %esi
movq %r15, %rdi
callq 0x3358
movq 0xc8(%rsp), %rdi
testq %rdi, %rdi
je 0x392b
movsd %xmm0, 0x10(%rsp)
callq 0x21b0
movsd 0x10(%rsp), %xmm0
ucomisd 0x275d(%rip), %xmm0     # 0x6090
je 0x3966
incl %r13d
movapd 0x30(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
andpd 0x2722(%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 (%r14), %r14
jmp 0x38e1
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x397d
callq 0x21b0
leaq 0x80(%rsp), %r14
jmp 0x39a3
movl $0x9, %edx
movq 0x563d(%rip), %rdi      # 0x8fd0
leaq 0x2708(%rip), %rsi      # 0x60a2
callq 0x2210
decl 0xc(%rsp)
movq (%r12), %r12
testq %r12, %r12
jne 0x389d
jmp 0x36ff
jmp 0x39f7
jmp 0x39f7
jmp 0x39f7
movq %rax, %rbx
movq 0xe0(%rsp), %rdi
jmp 0x39dc
jmp 0x39f7
jmp 0x39cc
movq %rax, %rbx
jmp 0x39e6
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
testq %rdi, %rdi
je 0x39e6
callq 0x21b0
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x39fa
callq 0x21b0
jmp 0x39fa
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
jne 0x3a33
jmp 0x3a3d
jmp 0x3a3a
movq %rax, %rbx
movq 0x80(%rsp), %rdi
cmpq %r14, %rdi
jne 0x3a33
jmp 0x3a3d
jmp 0x3a3a
movq %rax, %rbx
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3a3d
callq 0x21b0
jmp 0x3a3d
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x3ae0
leaq 0xf8(%rsp), %rdi
callq 0x3ae0
movq %rbx, %rdi
callq 0x2320
nop