// File: /workspace/github_cpp_makefile_star2_O2/4n6ist[P]usn_analytics/lib/usnjrnl.cpp
// Function: UsnJrnl::GetAllDirName()
int UsnJrnl::GetAllDirName() {

  historical_dir hdir;
  uint32_t pid;
  
  // root directory
  hdir.name = "\\";
  hdir.pid = 0;
  hdir.usn = 0;
  path_table.insert(make_pair(5, hdir));

  // directory table
  for(UsnMain x: usnmain_set) {
    if (x.attrs_i & FOLDER) {
      if (x.file_name.size() == 0)
        continue;
      if (x.file_name.find("\\", x.file_name.size()-1) == string::npos) // filename doesn't end with "\"
        continue;
      if (x.file_name == "<Can't Convert>")
        continue;
      if (x.cid == x.pid) // ignore unusual pattern
        continue;
      hdir.name = x.file_name;
      hdir.pid = x.pid;
      hdir.usn = x.usn;
      dir_table.insert(make_pair(x.cid, hdir));
      // hard coding
      if (hdir.name == "Public") {
        hdir.name = "Users";
        hdir.pid = 5;
        hdir.usn = 0;
        dir_table.insert(make_pair(x.cid, hdir));
      } else if (hdir.name == "Default") {
        hdir.name = "Users";
        hdir.pid = 5;
        hdir.usn = 0;
        dir_table.insert(make_pair(x.cid, hdir));
      } else if (hdir.name == "System32") {
        hdir.name = "Windows";
        hdir.pid = 5;
        hdir.usn = 0;
        dir_table.insert(make_pair(x.cid, hdir));
      } else if (hdir.name == "Prefetch") {
        hdir.name = "Windows";
        hdir.pid = 5;
        hdir.usn = 0;
        dir_table.insert(make_pair(x.cid, hdir));
      }
      //fprintf(fp_ofexecuted, "%lld, %s, %lld, %lld\n", x.cid, hdir.name.c_str(), hdir.pid, hdir.usn);
    }
  }
  uint32_t dir_table_size = dir_table.size();

  uint64_t i=1;
  uint64_t progress = dir_table_size / 10;
    
  // fullpath table
  for(auto x : dir_table) {  // x.first is cid, x.second is hdir
    if (i >= progress) {
  	  printf(".");
      progress += dir_table_size / 10;
    }
    ++i;

    hdir = GetHistoricalFileName(x.second, 0);
    path_table.insert(make_pair(x.first, hdir));
    //fprintf(fp_ofopened, "%lld, %s, %lld, %lld\n", x.first, hdir.name.c_str(), hdir.pid, hdir.usn);
  }

  uint64_t usnmain_set_size = usnmain_set.size();
  progress = usnmain_set_size / 10;
  
  // store into file_path with usnmain_set
  for(i=0; i+1 < usnmain_set_size; i++) {
    if (i >= progress) {
  	  printf(".");
      progress += usnmain_set_size / 10;
    }
    // examine correct path
    size_t count;
    count = path_table.count(usnmain_set[i].pid);
    if (count == 0) {
      usnmain_set[i].file_path = "";
    }
    else if (count == 1) {
      auto itr = path_table.find(usnmain_set[i].pid);   
      usnmain_set[i].file_path = itr->second.name;
    }
    else {
      int64_t diff = INT64_MAX;
      string pname;
      auto itr = path_table.equal_range(usnmain_set[i].pid);
      for (auto iterator = itr.first; iterator != itr.second; iterator++) {
      //  if (usn_record.Usn - iterator->second.usn > 0) {
        if (abs(int64_t(usnmain_set[i].usn - iterator->second.usn)) < diff) {
          diff = usnmain_set[i].usn - iterator->second.usn;
          pname = iterator->second.name;
        }
      }
      usnmain_set[i].file_path = pname;
    }
  }
  
  printf("Done\n");
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rdi, %rbx
leaq 0x74(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x7b1c(%rip), %rsi      # 0xc19e
leaq 0x64(%rsp), %rdi
callq 0x34c0
leaq 0x64(%rsp), %rsi
andl $0x0, 0x20(%rsi)
andq $0x0, 0x24(%rsi)
leaq 0x94(%rsp), %r14
movl $0x5, -0x4(%r14)
movq %r14, %rdi
callq 0x7490
leaq 0xc8(%rbx), %rdi
leaq 0x90(%rsp), %rsi
movq %rdi, 0x10(%rsp)
callq 0x8b52
movq %r14, %rdi
callq 0x3100
movq 0x50(%rbx), %r13
movq 0x58(%rbx), %r14
leaq 0xca(%rsp), %rbp
leaq 0x98(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x90(%rsp), %r15
cmpq %r14, %r13
je 0x4978
movq %r15, %rdi
movq %r13, %rsi
callq 0x737e
testb $0x10, 0x10e(%rsp)
je 0x4964
movq 0xd2(%rsp), %rdx
testq %rdx, %rdx
je 0x4964
decq %rdx
movq %rbp, %rdi
leaq 0x7a67(%rip), %rsi      # 0xc19e
callq 0x34f0
cmpq $-0x1, %rax
je 0x4964
movq %rbp, %rdi
leaq 0x7a50(%rip), %rsi      # 0xc1a0
callq 0x7470
testb %al, %al
jne 0x4964
movl 0x132(%rsp), %eax
cmpl 0x136(%rsp), %eax
je 0x4964
leaq 0x64(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3150
movl 0x136(%rsp), %eax
movl %eax, 0x84(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x132(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x1c(%rsp), %rdi
movq %r12, %rsi
callq 0x7490
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x8ca4
leaq 0x1c(%rsp), %rdi
callq 0x3100
leaq 0x64(%rsp), %r12
movq %r12, %rdi
leaq 0x79d1(%rip), %rsi      # 0xc1b0
callq 0x7470
testb %al, %al
je 0x4837
movq %r12, %rdi
leaq 0x79c5(%rip), %rsi      # 0xc1b7
callq 0x34c0
movl $0x5, 0x84(%rsp)
andq $0x0, 0x88(%rsp)
movl 0x132(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x1c(%rsp), %rdi
movq %r12, %rsi
callq 0x7490
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x8ca4
jmp 0x495a
movq %r12, %rdi
leaq 0x797c(%rip), %rsi      # 0xc1bd
callq 0x7470
testb %al, %al
je 0x4899
movq %r12, %rdi
leaq 0x7963(%rip), %rsi      # 0xc1b7
callq 0x34c0
movl $0x5, 0x84(%rsp)
andq $0x0, 0x88(%rsp)
movl 0x132(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x1c(%rsp), %rdi
movq %r12, %rsi
callq 0x7490
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x8ca4
jmp 0x495a
movq %r12, %rdi
leaq 0x7922(%rip), %rsi      # 0xc1c5
callq 0x7470
testb %al, %al
je 0x48f8
movq %r12, %rdi
leaq 0x7918(%rip), %rsi      # 0xc1ce
callq 0x34c0
movl $0x5, 0x84(%rsp)
andq $0x0, 0x88(%rsp)
movl 0x132(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x1c(%rsp), %rdi
movq %r12, %rsi
callq 0x7490
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x8ca4
jmp 0x495a
movq %r12, %rdi
leaq 0x78d4(%rip), %rsi      # 0xc1d6
callq 0x7470
testb %al, %al
je 0x4964
leaq 0x64(%rsp), %r12
movq %r12, %rdi
leaq 0x78b4(%rip), %rsi      # 0xc1ce
callq 0x34c0
movl $0x5, 0x84(%rsp)
andq $0x0, 0x88(%rsp)
movl 0x132(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x1c(%rsp), %rdi
movq %r12, %rsi
callq 0x7490
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x8ca4
leaq 0x1c(%rsp), %rdi
callq 0x3100
movq %r15, %rdi
callq 0x7154
addq $0xca, %r13
jmp 0x46f7
movl 0xc0(%rbx), %eax
pushq $0xa
popq %rcx
xorl %edx, %edx
divl %ecx
movl %eax, %ebp
movq 0xb0(%rbx), %rdx
movq %rbx, %r14
leaq 0xa0(%rbx), %rcx
movq %rcx, 0x50(%rsp)
pushq $0x1
popq %rcx
movq %rcx, 0x8(%rsp)
leaq 0x15c(%rsp), %r12
leaq 0x18(%rsp), %r15
movq %rbp, 0x48(%rsp)
cmpq 0x50(%rsp), %rdx
je 0x4a91
movq %rdx, 0x58(%rsp)
leaq 0x20(%rdx), %rsi
leaq 0x90(%rsp), %rdi
callq 0x747e
movq %r14, %rbx
cmpq %rbp, 0x8(%rsp)
jb 0x49ef
pushq $0x2e
popq %rdi
callq 0x3370
addq 0x48(%rsp), %rbp
movq %r12, %rdi
leaq 0x94(%rsp), %rsi
callq 0x7490
movq %r15, %rdi
movq %rbx, %r14
movq %rbx, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x4d02
leaq 0x64(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x74b6
movq %r15, %rdi
callq 0x3100
movq %r12, %r13
movq %r12, %rdi
callq 0x3100
movl 0x90(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x1c(%rsp), %rdi
movq %rbx, %rsi
callq 0x7490
movq 0x10(%rsp), %rdi
movq %r15, %rbx
movq %r15, %rsi
callq 0x8ca4
leaq 0x1c(%rsp), %rdi
callq 0x3100
leaq 0x94(%rsp), %rdi
callq 0x3100
movq 0x58(%rsp), %rdi
callq 0x3180
movq %rax, %rdx
incq 0x8(%rsp)
movq %r13, %r12
movq %rbx, %r15
jmp 0x49b7
movq %r14, %rbx
movq 0x58(%r14), %rax
subq 0x50(%r14), %rax
movl $0xca, %ecx
cqto
idivq %rcx
movq %rax, %r15
xorl %r14d, %r14d
pushq $0xa
popq %rcx
xorl %edx, %edx
divq %rcx
movq %rax, 0x50(%rsp)
movq %rax, %r13
movq %r15, 0x48(%rsp)
leaq 0x1(%r14), %rax
cmpq %r15, %rax
jae 0x4c3f
cmpq %r13, %r14
movq %rax, 0x8(%rsp)
jb 0x4ae5
pushq $0x2e
popq %rdi
callq 0x3370
addq 0x50(%rsp), %r13
movq 0x50(%rbx), %rax
imulq $0xca, %r14, %r12
leaq (%rax,%r12), %rsi
addq $0xa6, %rsi
movq 0x10(%rsp), %rdi
callq 0x8d3e
cmpq $0x1, %rax
je 0x4b32
testq %rax, %rax
jne 0x4b70
movq 0x50(%rbx), %rax
leaq (%rax,%r12), %rdi
addq $0xaa, %rdi
leaq 0x77f5(%rip), %rsi      # 0xc31b
callq 0x34c0
movq 0x8(%rsp), %r14
jmp 0x4ac1
movq 0x50(%rbx), %rax
leaq (%rax,%r12), %rsi
addq $0xa6, %rsi
movq 0x10(%rsp), %rdi
callq 0x8e3a
addq $0x24, %rax
movq 0x50(%rbx), %rcx
leaq (%rcx,%r12), %rdi
addq $0xaa, %rdi
movq %rax, %rsi
callq 0x3150
movq 0x8(%rsp), %r14
jmp 0x4ac1
movq %r13, 0x58(%rsp)
leaq 0xa0(%rsp), %rax
movq %rax, 0x90(%rsp)
andq $0x0, 0x98(%rsp)
movb $0x0, 0xa0(%rsp)
movq %rbx, %r15
movq 0x50(%rbx), %rax
leaq (%rax,%r12), %rsi
addq $0xa6, %rsi
movq 0x10(%rsp), %rdi
callq 0x8e8c
movq %rax, %r13
movq %rdx, %r14
movabsq $0x7fffffffffffffff, %rbx # imm = 0x7FFFFFFFFFFFFFFF
movq 0x50(%r15), %rax
cmpq %r14, %r13
je 0x4c03
movq (%rax,%r12), %rbp
subq 0x48(%r13), %rbp
movq %rbp, %rax
negq %rax
cmovsq %rbp, %rax
cmpq %rbx, %rax
jge 0x4bf6
leaq 0x24(%r13), %rsi
leaq 0x90(%rsp), %rdi
callq 0x3150
movq %rbp, %rbx
movq %r13, %rdi
callq 0x3180
movq %rax, %r13
jmp 0x4bc2
leaq (%rax,%r12), %rdi
addq $0xaa, %rdi
leaq 0x90(%rsp), %rsi
callq 0x3150
leaq 0x90(%rsp), %rdi
callq 0x3100
movq 0x8(%rsp), %r14
movq %r15, %rbx
movq 0x48(%rsp), %r15
movq 0x58(%rsp), %r13
jmp 0x4ac1
leaq 0x79ea(%rip), %rdi      # 0xc630
callq 0x33f0
leaq 0x64(%rsp), %rdi
callq 0x3100
xorl %eax, %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4c85
jmp 0x4c96
jmp 0x4c85
jmp 0x4c96
jmp 0x4c85
jmp 0x4c96
jmp 0x4c85
jmp 0x4c96
movq %rax, %rbx
movq %r14, %rdi
jmp 0x4ceb
jmp 0x4cdb
jmp 0x4cdb
movq %rax, %rbx
leaq 0x1c(%rsp), %rdi
callq 0x3100
jmp 0x4c99
jmp 0x4c96
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x7154
jmp 0x4cf0
jmp 0x4ce0
jmp 0x4cdb
jmp 0x4cbb
movq %rax, %rbx
leaq 0x15c(%rsp), %rdi
jmp 0x4cc8
movq %rax, %rbx
jmp 0x4ccd
movq %rax, %rbx
leaq 0x1c(%rsp), %rdi
callq 0x3100
leaq 0x94(%rsp), %rdi
jmp 0x4ceb
jmp 0x4cdb
jmp 0x4cdb
movq %rax, %rbx
jmp 0x4cf0
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x3100
leaq 0x64(%rsp), %rdi
callq 0x3100
movq %rbx, %rdi
callq 0x3480