// File: jstaursky[P]hutch/src/Sleigh/filemanage.cc
// Function: FileManage::discoverGhidraRoot[abi:cxx11](char const*)
string FileManage::discoverGhidraRoot(const char *argv0)

{ // Find the root of the ghidra distribution based on current working directory and passed in path
  vector<string> pathels;
  string cur(argv0);
  string base;
  int skiplevel = 0;
  bool isAbs = isAbsolutePath(cur);

  for(;;) {
    int sizebefore = cur.size();
    splitPath(cur,cur,base);
    if (cur.size() == sizebefore) break;
    if (base == ".")
      skiplevel += 1;
    else if (base == "..")
      skiplevel += 2;
    if (skiplevel > 0)
      skiplevel -= 1;
    else
      pathels.push_back(base);
  }
  if (!isAbs) {
    FileManage curdir;
    curdir.addCurrentDir();
    cur = curdir.pathlist[0];
    for(;;) {
      int sizebefore = cur.size();
      splitPath(cur,cur,base);
      if (cur.size() == sizebefore) break;
      pathels.push_back(base);
    }
  }

  for(int i=0;i<pathels.size();++i) {
    if (pathels[i] != "Ghidra") continue;
    string root;
    if (testDevelopmentPath(pathels,i,root))
      return root;
    if (testInstallPath(pathels,i,root))
      return root;
  }
  return "";
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x48(%rsp)
movq %rdi, %rax
movq %rax, 0x50(%rsp)
movq %rdi, 0x120(%rsp)
movq %rsi, 0x118(%rsp)
leaq 0x100(%rsp), %rdi
callq 0xe0e60
movq 0x118(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0xdf(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0xe0c80
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
leaq 0xe0(%rsp), %rdi
callq 0xe00a0
jmp 0x10b3b9
leaq 0xdf(%rsp), %rdi
callq 0xd8ec0
leaq 0xa8(%rsp), %rdi
callq 0xdc610
movl $0x0, 0xa4(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0xd5b00
movb %al, 0x47(%rsp)
jmp 0x10b3f1
movb 0x47(%rsp), %al
andb $0x1, %al
movb %al, 0xa3(%rsp)
leaq 0xe0(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0xd5290
movq 0x38(%rsp), %rsi
movl %eax, 0x9c(%rsp)
leaq 0xa8(%rsp), %rdx
movq %rsi, %rdi
callq 0xd3ae0
jmp 0x10b42e
leaq 0xe0(%rsp), %rdi
callq 0xd5290
movslq 0x9c(%rsp), %rcx
cmpq %rcx, %rax
jne 0x10b48c
jmp 0x10b535
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
leaq 0xdf(%rsp), %rdi
callq 0xd8ec0
jmp 0x10b7fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
jmp 0x10b7e1
leaq 0x7c919(%rip), %rsi     # 0x187dac
leaq 0xa8(%rsp), %rdi
callq 0xe30f0
movb %al, 0x37(%rsp)
jmp 0x10b4a6
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0x10b4b0
jmp 0x10b4c3
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x10b4fa
leaq 0x7c8e1(%rip), %rsi     # 0x187dab
leaq 0xa8(%rsp), %rdi
callq 0xe30f0
movb %al, 0x36(%rsp)
jmp 0x10b4dd
movb 0x36(%rsp), %al
testb $0x1, %al
jne 0x10b4e7
jmp 0x10b4f8
movl 0xa4(%rsp), %eax
addl $0x2, %eax
movl %eax, 0xa4(%rsp)
jmp 0x10b4fa
cmpl $0x0, 0xa4(%rsp)
jle 0x10b517
movl 0xa4(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x10b530
leaq 0x100(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0xd3180
jmp 0x10b52e
jmp 0x10b530
jmp 0x10b3fe
testb $0x1, 0xa3(%rsp)
jne 0x10b61b
leaq 0x80(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0xe0660
movq 0x28(%rsp), %rdi
callq 0xd32b0
jmp 0x10b561
xorl %eax, %eax
movl %eax, %esi
leaq 0x80(%rsp), %rdi
callq 0xe1770
movq %rax, %rsi
leaq 0xe0(%rsp), %rdi
callq 0xce450
jmp 0x10b584
jmp 0x10b586
leaq 0xe0(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0xd5290
movq 0x20(%rsp), %rsi
movl %eax, 0x7c(%rsp)
leaq 0xa8(%rsp), %rdx
movq %rsi, %rdi
callq 0xd3ae0
jmp 0x10b5b3
leaq 0xe0(%rsp), %rdi
callq 0xd5290
movslq 0x7c(%rsp), %rcx
cmpq %rcx, %rax
jne 0x10b5f2
jmp 0x10b60e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
leaq 0x80(%rsp), %rdi
callq 0xcf190
jmp 0x10b7e1
leaq 0x100(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0xd3180
jmp 0x10b609
jmp 0x10b586
leaq 0x80(%rsp), %rdi
callq 0xcf190
movl $0x0, 0x78(%rsp)
movslq 0x78(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0x100(%rsp), %rdi
callq 0xe1670
movq %rax, %rcx
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
jae 0x10b752
movslq 0x78(%rsp), %rsi
leaq 0x100(%rsp), %rdi
callq 0xe1770
movq %rax, %rdi
leaq 0x7c75a(%rip), %rsi     # 0x187dc1
callq 0xd6530
movb %al, 0x17(%rsp)
jmp 0x10b672
movb 0x17(%rsp), %al
testb $0x1, %al
jne 0x10b67c
jmp 0x10b681
jmp 0x10b742
movq 0x48(%rsp), %rdi
movb $0x0, 0x77(%rsp)
callq 0xdc610
movq 0x48(%rsp), %rdx
movl 0x78(%rsp), %esi
leaq 0x100(%rsp), %rdi
callq 0xdd1d0
movb %al, 0x16(%rsp)
jmp 0x10b6ac
movb 0x16(%rsp), %al
testb $0x1, %al
jne 0x10b6b6
jmp 0x10b6e8
movb $0x1, 0x77(%rsp)
movl $0x1, 0x70(%rsp)
jmp 0x10b725
movq 0x48(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
callq 0xd4550
jmp 0x10b7e1
movq 0x48(%rsp), %rdx
movl 0x78(%rsp), %esi
leaq 0x100(%rsp), %rdi
callq 0xdb900
movb %al, 0x15(%rsp)
jmp 0x10b704
movb 0x15(%rsp), %al
testb $0x1, %al
jne 0x10b70e
jmp 0x10b71d
movb $0x1, 0x77(%rsp)
movl $0x1, 0x70(%rsp)
jmp 0x10b725
movl $0x0, 0x70(%rsp)
testb $0x1, 0x77(%rsp)
jne 0x10b736
movq 0x48(%rsp), %rdi
callq 0xd4550
movl 0x70(%rsp), %eax
testl %eax, %eax
jne 0x10b7ad
jmp 0x10b740
jmp 0x10b742
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x10b623
leaq 0x6f(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0xe0c80
movq 0x48(%rsp), %rdi
movq 0x8(%rsp), %rdx
leaq 0x7d91e(%rip), %rsi     # 0x189090
callq 0xe00a0
jmp 0x10b779
leaq 0x6f(%rsp), %rdi
callq 0xd8ec0
movl $0x1, 0x70(%rsp)
jmp 0x10b7ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
leaq 0x6f(%rsp), %rdi
callq 0xd8ec0
jmp 0x10b7e1
leaq 0xa8(%rsp), %rdi
callq 0xd4550
leaq 0xe0(%rsp), %rdi
callq 0xd4550
leaq 0x100(%rsp), %rdi
callq 0xd5480
movq 0x50(%rsp), %rax
addq $0x128, %rsp            # imm = 0x128
retq
leaq 0xa8(%rsp), %rdi
callq 0xd4550
leaq 0xe0(%rsp), %rdi
callq 0xd4550
leaq 0x100(%rsp), %rdi
callq 0xd5480
movq 0xd0(%rsp), %rdi
callq 0xe0be0
nopw %cs:(%rax,%rax)
nop