// File: /workspace/github_cpp_makefile_star2_O1/4n6ist[P]usn_analytics/lib/usnjrnl.cpp
// Function: UsnJrnl::GetHistoricalFileName(historical_dir, unsigned char)
historical_dir UsnJrnl::GetHistoricalFileName(historical_dir hdir, uint8_t i) {
  uint32_t count;
  
  if(i == 31) // prevent from infinite loop
    return hdir;
  count = dir_table.count(hdir.pid);  
  if (hdir.pid == 5) { // reach root directory
    hdir.name = "\\" + hdir.name;
    return hdir;
  }
  else if (count == 0) { // parent directory not found
    hdir.name = hdir.name;
    return hdir;
  }
  else if (count == 1) { // unique parent directory found
    auto itr = dir_table.find(hdir.pid);    
    hdir.name = itr->second.name + hdir.name;
    hdir.pid = itr->second.pid;
    i++;
    return GetHistoricalFileName(hdir, i);
  } 
  else { // multiple parent directory found
    int64_t diff = INT64_MAX;
    uint32_t pid = 0;
    string pname = "";
    auto itr = dir_table.equal_range(hdir.pid);
    for (auto iterator = itr.first; iterator != itr.second; iterator++) {
//      if (hdir.usn - iterator->second.usn > 0) {
        if (abs(int64_t(hdir.usn - iterator->second.usn)) < diff) {
          diff = hdir.usn - iterator->second.usn;
          pname = iterator->second.name;
          pid = iterator->second.pid;          
        }         
    }
    hdir.name = pname + hdir.name;
    hdir.pid = pid;
    i++;
    return GetHistoricalFileName(hdir, i);
  }  
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdx, %r15
movq %rdi, %rbx
cmpl $0x1f, %ecx
jne 0x5af4
leaq 0x10(%rbx), %rcx
movq %rcx, (%rbx)
movq (%r15), %rdx
leaq 0x10(%r15), %rax
cmpq %rax, %rdx
je 0x5b93
movq %rdx, (%rbx)
movq (%rax), %rcx
movq %rcx, 0x10(%rbx)
jmp 0x5b99
movq %rsi, %r12
movl %ecx, 0xc(%rsp)
leaq 0x98(%rsi), %r14
leaq 0x20(%r15), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0xaafe
movl 0x20(%r15), %ecx
cmpl $0x5, %ecx
jne 0x5b58
leaq 0x967d(%rip), %rsi      # 0xf19e
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x8fa1
movq %r15, %rdi
movq %r14, %rsi
callq 0x3300
movq (%r14), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x5b74
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x5b74
cmpl $0x1, %eax
je 0x5bff
testl %eax, %eax
jne 0x5ce8
movq %r15, %rdi
movq %r15, %rsi
callq 0x3120
leaq 0x10(%rbx), %rcx
movq %rcx, (%rbx)
movq (%r15), %rdx
leaq 0x10(%r15), %rax
cmpq %rax, %rdx
je 0x5bbe
movq %rdx, (%rbx)
movq (%rax), %rcx
movq %rcx, 0x10(%rbx)
jmp 0x5bc4
movups (%rax), %xmm0
movups %xmm0, (%rcx)
movq 0x8(%r15), %rcx
movq %rcx, 0x8(%rbx)
movq %rax, (%r15)
movq $0x0, 0x8(%r15)
movb $0x0, 0x10(%r15)
movl 0x28(%r15), %eax
movl %eax, 0x28(%rbx)
movq 0x20(%r15), %rax
jmp 0x5be6
movups (%rax), %xmm0
movups %xmm0, (%rcx)
movq 0x8(%r15), %rcx
movq %rcx, 0x8(%rbx)
movq %rax, (%r15)
movq $0x0, 0x8(%r15)
movb $0x0, 0x10(%r15)
movl 0x8(%rbp), %eax
movl %eax, 0x28(%rbx)
movq (%rbp), %rax
movq %rax, 0x20(%rbx)
movq %rbx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa8(%r12), %rdx
movq %r12, 0x10(%rsp)
addq $0xa0, %r12
movq %r12, %rax
testq %rdx, %rdx
je 0x5c35
movq %r12, %rax
xorl %esi, %esi
cmpl %ecx, 0x20(%rdx)
setb %sil
cmovaeq %rdx, %rax
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x5c1e
cmpq %r12, %rax
je 0x5c41
cmpl 0x20(%rax), %ecx
cmovaeq %rax, %r12
leaq 0x24(%r12), %rsi
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x9248
movq %r15, %rdi
movq %r14, %rsi
callq 0x3300
movq (%r14), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x5c7b
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3230
movl 0x44(%r12), %eax
movl %eax, 0x20(%r15)
movl 0xc(%rsp), %r13d
incb %r13b
leaq 0x7c(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x6c(%rsp), %r14
movq %r14, %rdi
callq 0x9574
movl 0x8(%rbp), %eax
movl %eax, 0x28(%r14)
movq (%rbp), %rax
movq %rax, 0x20(%r14)
movzbl %r13b, %ecx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x5ab2
movq 0x6c(%rsp), %rdi
cmpq %r12, %rdi
je 0x5bea
movq 0x7c(%rsp), %rsi
jmp 0x5e4d
movq %r12, 0x10(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x961e(%rip), %rdx      # 0xf31b
leaq 0x18(%rsp), %rdi
movq %rdx, %rsi
callq 0x97be
movq %r14, %rdi
movq %rbp, %rsi
callq 0xaba8
movq %rax, %r14
movq %rdx, %r13
movq %rbp, 0x38(%rsp)
cmpq %rdx, %rax
je 0x5d79
movabsq $0x7fffffffffffffff, %r12 # imm = 0x7FFFFFFFFFFFFFFF
movl $0x0, 0x8(%rsp)
movq 0x24(%r15), %rbp
subq 0x48(%r14), %rbp
movq %rbp, %rax
negq %rax
cmovsq %rbp, %rax
cmpq %r12, %rax
jge 0x5d67
leaq 0x24(%r14), %rsi
leaq 0x18(%rsp), %rdi
callq 0x3120
movl 0x44(%r14), %eax
movl %eax, 0x8(%rsp)
movq %rbp, %r12
movq %r14, %rdi
callq 0x3150
movq %rax, %r14
cmpq %r13, %rax
jne 0x5d37
jmp 0x5d81
movl $0x0, 0x8(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movq %r15, %rdx
callq 0x9248
leaq 0x98(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x3300
movq (%r14), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
movq 0x38(%rsp), %r12
je 0x5dce
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x3230
movl 0x8(%rsp), %eax
movl %eax, 0x20(%r15)
leaq 0x50(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0x9574
movl 0x8(%r12), %eax
leaq 0x40(%rsp), %rdx
movl %eax, 0x28(%rdx)
movq (%r12), %rax
movq %rax, 0x20(%rdx)
movl 0xc(%rsp), %eax
incb %al
movzbl %al, %ecx
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0x5ab2
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x5e35
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3230
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x5bea
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x5bea
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x5e96
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x5e96
jmp 0x5e93
movq %rax, %rbx
jmp 0x5eb2
movq %rax, %rbx
movq 0x6c(%rsp), %rdi
cmpq %r12, %rdi
je 0x5eb2
movq 0x7c(%rsp), %rsi
jmp 0x5eaa
jmp 0x5e93
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x5eb2
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3230
movq %rbx, %rdi
callq 0x3470