// File: /workspace/github_cpp_makefile_star2_O3/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 0x5af2
leaq 0x10(%rbx), %rcx
movq %rcx, (%rbx)
movq (%r15), %rdx
leaq 0x10(%r15), %rax
cmpq %rax, %rdx
je 0x5b91
movq %rdx, (%rbx)
movq (%rax), %rcx
movq %rcx, 0x10(%rbx)
jmp 0x5b97
movq %rsi, %r12
movl %ecx, 0xc(%rsp)
leaq 0x98(%rsi), %r14
leaq 0x20(%r15), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0xaaf8
movl 0x20(%r15), %ecx
cmpl $0x5, %ecx
jne 0x5b56
leaq 0x967f(%rip), %rsi      # 0xf19e
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x8ffb
movq %r15, %rdi
movq %r14, %rsi
callq 0x3300
movq (%r14), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x5b72
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x5b72
cmpl $0x1, %eax
je 0x5bfd
testl %eax, %eax
jne 0x5ce3
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 0x5bbc
movq %rdx, (%rbx)
movq (%rax), %rcx
movq %rcx, 0x10(%rbx)
jmp 0x5bc2
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 0x5be4
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
testq %rdx, %rdx
je 0x5c3c
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 0x5c19
cmpq %r12, %rax
je 0x5c3c
cmpl 0x20(%rax), %ecx
cmovaeq %rax, %r12
leaq 0x24(%r12), %rsi
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x92a2
movq %r15, %rdi
movq %r14, %rsi
callq 0x3300
movq (%r14), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x5c76
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 0x95ce
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 0x5ab0
movq 0x6c(%rsp), %rdi
cmpq %r12, %rdi
je 0x5be8
movq 0x7c(%rsp), %rsi
jmp 0x5e48
movq %r12, 0x10(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x9623(%rip), %rdx      # 0xf31b
leaq 0x18(%rsp), %rdi
movq %rdx, %rsi
callq 0x981e
movq %r14, %rdi
movq %rbp, %rsi
callq 0xaba8
movq %rax, %r14
movq %rdx, %r13
movq %rbp, 0x38(%rsp)
cmpq %rdx, %rax
je 0x5d74
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 0x5d62
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 0x5d32
jmp 0x5d7c
movl $0x0, 0x8(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movq %r15, %rdx
callq 0x92a2
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 0x5dc9
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 0x95ce
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 0x5ab0
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x5e30
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3230
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x5be8
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x5be8
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %r14, %rdi
je 0x5e91
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3230
jmp 0x5e91
jmp 0x5e8e
movq %rax, %rbx
jmp 0x5ead
movq %rax, %rbx
movq 0x6c(%rsp), %rdi
cmpq %r12, %rdi
je 0x5ead
movq 0x7c(%rsp), %rsi
jmp 0x5ea5
jmp 0x5e8e
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x5ead
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3230
movq %rbx, %rdi
callq 0x3460
nop