// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rdi, %rbx
movl %ecx, 0xc(%rsp)
cmpl $0x1f, %ecx
je 0x4d86
movq %rsi, %rbp
leaq 0x98(%rsi), %r12
leaq 0x20(%r14), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x8d3e
cmpl $0x5, 0x20(%r14)
jne 0x4d6e
leaq 0x7455(%rip), %rsi      # 0xc19e
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %r14, %rdx
callq 0x7282
movq %r14, %rdi
movq %r15, %rsi
callq 0x3320
movq %r15, %rdi
callq 0x3100
jmp 0x4d86
cmpl $0x1, %eax
je 0x4da6
testl %eax, %eax
jne 0x4e1d
movq %r14, %rdi
movq %r14, %rsi
callq 0x3150
movq %rbx, %rdi
movq %r14, %rsi
callq 0x74e0
movq %rbx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
movq %r15, %rsi
callq 0x8e3a
movq %rax, %r15
leaq 0x24(%rax), %rsi
leaq 0x20(%rsp), %r12
movq %r12, %rdi
movq %r14, %rdx
callq 0x7506
movq %r14, %rdi
movq %r12, %rsi
callq 0x3320
movq %r12, %rdi
callq 0x3100
movl 0x44(%r15), %eax
movl %eax, 0x20(%r14)
movl 0xc(%rsp), %r12d
incb %r12b
leaq 0x8c(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x7490
movzbl %r12b, %ecx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x4d02
leaq 0x8c(%rsp), %rdi
jmp 0x4f03
leaq 0x74f7(%rip), %rsi      # 0xc31b
leaq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x7072
movq %r12, %rdi
movq %r15, %rsi
callq 0x8e8c
movq %rax, %r13
movq %rdx, %r12
movq %rbp, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
xorl %r15d, %r15d
movabsq $0x7fffffffffffffff, %rbp # imm = 0x7FFFFFFFFFFFFFFF
cmpq %r12, %r13
je 0x4e99
movq 0x24(%r14), %rbx
subq 0x48(%r13), %rbx
movq %rbx, %rax
negq %rax
cmovsq %rbx, %rax
cmpq %rbp, %rax
jge 0x4e8c
leaq 0x24(%r13), %rsi
leaq 0x20(%rsp), %rdi
callq 0x3150
movl 0x44(%r13), %r15d
movq %rbx, %rbp
movq %r13, %rdi
callq 0x3180
movq %rax, %r13
jmp 0x4e5b
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movq %r14, %rdx
callq 0x7506
leaq 0x40(%rsp), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x3320
movq %r12, %rdi
callq 0x3100
movl %r15d, 0x20(%r14)
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x7490
movq 0x18(%rsp), %rbx
movq 0x10(%rsp), %rsi
movl 0xc(%rsp), %eax
incb %al
movzbl %al, %ecx
leaq 0x60(%rsp), %rdx
movq %rbx, %rdi
callq 0x4d02
leaq 0x60(%rsp), %rdi
callq 0x3100
leaq 0x20(%rsp), %rdi
callq 0x3100
jmp 0x4d91
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x3100
jmp 0x4f35
jmp 0x4f32
movq %rax, %rbx
jmp 0x4f3f
movq %rax, %rbx
leaq 0x8c(%rsp), %rdi
jmp 0x4f3a
jmp 0x4f32
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3100
movq %rbx, %rdi
callq 0x3480
nop