// File: /workspace/github_c_makefile_star2_O1/enzo1982[P]smooth/classes/gui/application/application.cpp
// Function: smooth::GUI::Application::GetApplicationDirectory()
S::String S::GUI::Application::GetApplicationDirectory()
{
	if (applicationDirectory != NIL) return applicationDirectory;

#if defined __WIN32__
	Buffer<wchar_t>	 buffer(32768 + 1);
#else
	Buffer<char>	 buffer(PATH_MAX + 1);
#endif

	buffer.Zero();

#if defined __WIN32__
	/* On Windows, use GetModuleFileName to get the exe file name.
	 */
	GetModuleFileName(NIL, buffer, buffer.Size() - 1);

	applicationDirectory = buffer;
#elif defined __APPLE__
	/* On macOS, get the path using the proc_pidpath call.
	 */
	if (proc_pidpath(getpid(), buffer, buffer.Size()) > 0) applicationDirectory = buffer;

	/* Resort to using lsof -p <pid>, which returns the path to the current binary in the first
	 * txt section, in case lookup failed or we got the path to the Rosetta binary translator.
	 */
	if (applicationDirectory == NIL || applicationDirectory == "/usr/libexec/oah/translate")
	{
		FILE	*pstdin = popen(String("lsof -p ").Append(String::FromInt(getpid())).Append(" | awk '$4 == \"txt\" { print substr($0, index($0, $9)) }'"), "r");

		if (fscanf(pstdin, String("%[^\n]").Append(String::FromInt(buffer.Size() - 1)), (char *) buffer) > 0) applicationDirectory = buffer;

		pclose(pstdin);
	}
#elif defined __FreeBSD__
	/* On FreeBSD, the KERN_PROC_PATHNAME sysctl with pid -1 provides the path to the current binary.
	 */
	int	 mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
	size_t	 len	= buffer.Size();

	if (sysctl(mib, 4, buffer, &len, NULL, 0) == 0) applicationDirectory = buffer;
#elif defined __sun
	/* On Solaris, /proc/<pid>/path/a.out links to the current binary.
	 */
	if (readlink(String("/proc/").Append(String::FromInt(getpid())).Append("/path/a.out"), buffer, buffer.Size() - 1) > 0) applicationDirectory = buffer;
#elif defined __linux__ || defined __NetBSD__
	/* On Linux and NetBSD, /proc/<pid>/exe links to the current binary.
	 */
	if (readlink(String("/proc/").Append(String::FromInt(getpid())).Append("/exe"), buffer, buffer.Size() - 1) > 0) applicationDirectory = buffer;
#elif defined __HAIKU__
	/* On Haiku, get the path from application info.
	 */
	BApplication	*app = Backends::BackendHaiku::GetApplication();
	app_info	 ai;

	if (app->GetAppInfo(&ai) == B_OK) applicationDirectory.ImportFrom("UTF-8", BPath(&ai.ref).Path());
#endif

	if (applicationDirectory == NIL)
	{
		/* No system specific way to get the current binary path.
		 * Try concatenating the startup directory and command.
		 */
		String	 binary	= (command.StartsWith("/") ? String() : GetStartupDirectory()).Append(command).Replace("/./", "/");

		if (!File(binary).Exists())
		{
			/* Search the path for command.
			 */
			String			 path  = getenv("PATH");
			const Array<String>	&paths = path.Explode(":");

			foreach (const String &path, paths)
			{
				/* Check for command in this path.
				 */
				if (File(String(path).Append("/").Append(command)).Exists())
				{
					binary = String(path).Append("/").Append(command);

					break;
				}
			}
		}

		applicationDirectory = binary;
	}

	applicationDirectory[applicationDirectory.FindLast(Directory::GetDirectoryDelimiter()) + 1] = 0;

#if defined __APPLE__
	/* Change the returned path to Resources for macOS application bundles.
	 */
	if (applicationDirectory.EndsWith(".app/Contents/MacOS/")) applicationDirectory.Replace(".app/Contents/MacOS/", ".app/Contents/Resources/");
#endif

	return applicationDirectory;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdi, %r14
movq 0x108ef9(%rip), %rdi    # 0x16c590
xorl %esi, %esi
callq 0x56bd0
testb %al, %al
je 0x636b6
movq 0x108ee7(%rip), %rsi    # 0x16c590
movq %r14, %rdi
callq 0x570b0
jmp 0x63b2a
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl $0x1001, %esi           # imm = 0x1001
callq 0x5cfaa
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x636e6
movq (%rax), %rdi
movslq 0x90(%rsp), %rdx
xorl %esi, %esi
callq 0x55f20
leaq 0x90f8d(%rip), %rsi     # 0xf467a
leaq 0xa0(%rsp), %rdi
callq 0x579f0
callq 0x58b30
movslq %eax, %rsi
leaq 0x8(%rsp), %rdi
callq 0x56cf0
leaq 0xa0(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x562f0
movq %rax, %rbx
leaq 0x90f59(%rip), %rsi     # 0xf4681
leaq 0x38(%rsp), %rdi
callq 0x579f0
leaq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0x562f0
movq %rax, %rdi
callq 0x585e0
movq 0x88(%rsp), %rcx
testq %rcx, %rcx
je 0x63759
movq (%rcx), %rsi
jmp 0x6375b
xorl %esi, %esi
movslq 0x90(%rsp), %rdx
decq %rdx
movq %rax, %rdi
callq 0x55e20
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x59200
leaq 0x8(%rsp), %rdi
callq 0x59200
leaq 0xa0(%rsp), %rdi
callq 0x59200
testq %rbx, %rbx
jle 0x637b7
movq 0x88(%rsp), %rax
testq %rax, %rax
je 0x637a9
movq (%rax), %rsi
jmp 0x637ab
xorl %esi, %esi
movq 0x108dde(%rip), %rdi    # 0x16c590
callq 0x55f40
movq 0x108dd2(%rip), %rdi    # 0x16c590
xorl %esi, %esi
callq 0x55260
testb %al, %al
je 0x63ab8
leaq 0x90eb4(%rip), %rsi     # 0xf4688
leaq 0x8(%rsp), %rdi
callq 0x579f0
movq 0x108eeb(%rip), %rdi    # 0x16c6d0
leaq 0x8(%rsp), %rsi
callq 0x577d0
testb %al, %al
je 0x63804
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x58e20
jmp 0x63811
leaq 0xa0(%rsp), %rdi
callq 0x58530
movq 0x108eb8(%rip), %rsi    # 0x16c6d0
leaq 0xa0(%rsp), %rdi
callq 0x562f0
movq %rax, %rbx
leaq 0x90e57(%rip), %rsi     # 0xf4686
leaq 0x50(%rsp), %rdi
callq 0x579f0
leaq 0x90e48(%rip), %rsi     # 0xf4688
leaq 0x68(%rsp), %rdi
callq 0x579f0
leaq 0x50(%rsp), %rsi
leaq 0x68(%rsp), %rdx
movq %rbx, %rdi
callq 0x575f0
leaq 0x38(%rsp), %rdi
movq %rax, %rsi
callq 0x570b0
leaq 0x68(%rsp), %rdi
callq 0x59200
leaq 0x50(%rsp), %rdi
callq 0x59200
leaq 0xa0(%rsp), %rdi
callq 0x59200
leaq 0x8(%rsp), %rdi
callq 0x59200
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x58e20
leaq 0xa0(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x56800
leaq 0xa0(%rsp), %rdi
callq 0x56ef0
movl %eax, %ebx
leaq 0xa0(%rsp), %rdi
callq 0x59178
leaq 0x8(%rsp), %rdi
callq 0x59200
testb %bl, %bl
jne 0x63a9d
leaq 0xafa8b(%rip), %rdi     # 0x113377
callq 0x57720
leaq 0x50(%rsp), %rdi
movq %rax, %rsi
callq 0x579f0
leaq 0x907e2(%rip), %rsi     # 0xf40e7
leaq 0x8(%rsp), %rdi
callq 0x579f0
movq %r14, 0x98(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x57f10
leaq 0x8(%rsp), %rdi
callq 0x59200
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
leaq 0x68(%rsp), %r12
leaq 0x110(%rsp), %rbp
movl 0xa8(%rsp), %eax
decl %eax
cmpl %eax, %esi
jge 0x63a7e
incl %esi
leaq 0xa0(%rsp), %rdi
movl %esi, 0x4(%rsp)
callq 0x64224
movq %rax, %r13
movb $0x1, %al
xorl %ecx, %ecx
movl %ecx, %r14d
testb $0x1, %al
je 0x63a70
movq %r12, %rdi
movq %r13, %rsi
callq 0x570b0
leaq 0xf8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x90ceb(%rip), %rsi     # 0xf4688
callq 0x579f0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x562f0
movq %rax, %rdi
movq 0x108d19(%rip), %rsi    # 0x16c6d0
callq 0x562f0
movq %rax, %r15
movq %rbp, %rdi
xorl %esi, %esi
callq 0x58e20
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x56800
movq %rbx, %rdi
callq 0x56ef0
movl %eax, %r15d
movq %rbx, %rdi
callq 0x59178
movq %rbp, %rdi
callq 0x59200
leaq 0xf8(%rsp), %rdi
callq 0x59200
movq %r12, %rdi
callq 0x59200
movb $0x1, %cl
xorl %eax, %eax
testb %r15b, %r15b
je 0x63975
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x570b0
movq %r12, %rdi
leaq 0x90c58(%rip), %rsi     # 0xf4688
callq 0x579f0
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x562f0
movq %rax, %rdi
movq 0x108c84(%rip), %rsi    # 0x16c6d0
callq 0x562f0
leaq 0x38(%rsp), %rdi
movq %rax, %rsi
callq 0x555f0
movq %r12, %rdi
callq 0x59200
leaq 0x8(%rsp), %rdi
callq 0x59200
testb $0x1, %r14b
movl 0x4(%rsp), %esi
jne 0x6394a
leaq 0xa0(%rsp), %rdi
callq 0x63e08
leaq 0x50(%rsp), %rdi
callq 0x59200
movq 0x98(%rsp), %r14
movq 0x108aec(%rip), %rdi    # 0x16c590
leaq 0x38(%rsp), %rsi
callq 0x555f0
leaq 0x38(%rsp), %rdi
callq 0x59200
callq 0x55370
leaq 0xa0(%rsp), %rdi
movq %rax, %rsi
callq 0x579f0
movq 0x108abc(%rip), %rdi    # 0x16c590
leaq 0xa0(%rsp), %rsi
callq 0x58a10
incl %eax
movq 0x108aa6(%rip), %rdi    # 0x16c590
movl %eax, %esi
callq 0x58990
movl $0x0, (%rax)
leaq 0xa0(%rsp), %rdi
callq 0x59200
movq 0x108a85(%rip), %rsi    # 0x16c590
movq %r14, %rdi
callq 0x570b0
leaq 0xfd1be(%rip), %rax     # 0x160cd8
leaq 0x80(%rsp), %rdi
movq %rax, (%rdi)
callq 0x5d862
movq %r14, %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x59200
jmp 0x63c6e
movq %rax, %rbx
jmp 0x63c6e
jmp 0x63b7c
jmp 0x63b7c
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x59178
jmp 0x63b72
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
jmp 0x63c73
movq %rax, %rbx
jmp 0x63c78
movq %rax, %rbx
jmp 0x63b9f
jmp 0x63b8b
movq %rax, %rbx
jmp 0x63ba9
jmp 0x63bff
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x59200
leaq 0x50(%rsp), %rdi
callq 0x59200
leaq 0xa0(%rsp), %rdi
callq 0x59200
jmp 0x63bbb
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
jmp 0x63c7d
jmp 0x63bc7
movq %rax, %rbx
jmp 0x63beb
jmp 0x63bd7
jmp 0x63bff
movq %rax, %rbx
jmp 0x63c13
jmp 0x63c40
movq %rax, %rbx
jmp 0x63bf5
jmp 0x63bff
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x59200
leaq 0x8(%rsp), %rdi
callq 0x59200
leaq 0xa0(%rsp), %rdi
jmp 0x63c7d
movq %rax, %rbx
jmp 0x63c82
jmp 0x63c40
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x59200
leaq 0x8(%rsp), %rdi
jmp 0x63c5c
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x59178
jmp 0x63c31
movq %rax, %rbx
jmp 0x63c57
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x59200
jmp 0x63c4a
movq %rax, %rbx
jmp 0x63c61
jmp 0x63c47
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x59200
leaq 0x68(%rsp), %rdi
callq 0x59200
leaq 0xa0(%rsp), %rdi
callq 0x63e08
leaq 0x50(%rsp), %rdi
callq 0x59200
leaq 0x38(%rsp), %rdi
callq 0x59200
leaq 0xfd04f(%rip), %rax     # 0x160cd8
leaq 0x80(%rsp), %rdi
movq %rax, (%rdi)
callq 0x5d862
movq %rbx, %rdi
callq 0x57090
nop