// File: /workspace/github_c_makefile_star2_O3/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 0x107ec5(%rip), %rdi    # 0x16b590
xorl %esi, %esi
callq 0x56bd0
testb %al, %al
je 0x636ea
movq 0x107eb3(%rip), %rsi    # 0x16b590
movq %r14, %rdi
callq 0x570b0
jmp 0x63b5e
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl $0x1001, %esi           # imm = 0x1001
callq 0x5cf6a
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x6371a
movq (%rax), %rdi
movslq 0x90(%rsp), %rdx
xorl %esi, %esi
callq 0x55f20
leaq 0x8ffbb(%rip), %rsi     # 0xf36dc
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 0x8ff87(%rip), %rsi     # 0xf36e3
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 0x6378d
movq (%rcx), %rsi
jmp 0x6378f
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 0x637eb
movq 0x88(%rsp), %rax
testq %rax, %rax
je 0x637dd
movq (%rax), %rsi
jmp 0x637df
xorl %esi, %esi
movq 0x107daa(%rip), %rdi    # 0x16b590
callq 0x55f40
movq 0x107d9e(%rip), %rdi    # 0x16b590
xorl %esi, %esi
callq 0x55260
testb %al, %al
je 0x63aec
leaq 0x8fee2(%rip), %rsi     # 0xf36ea
leaq 0x8(%rsp), %rdi
callq 0x579f0
movq 0x107eb7(%rip), %rdi    # 0x16b6d0
leaq 0x8(%rsp), %rsi
callq 0x577d0
testb %al, %al
je 0x63838
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x58e20
jmp 0x63845
leaq 0xa0(%rsp), %rdi
callq 0x58530
movq 0x107e84(%rip), %rsi    # 0x16b6d0
leaq 0xa0(%rsp), %rdi
callq 0x562f0
movq %rax, %rbx
leaq 0x8fe85(%rip), %rsi     # 0xf36e8
leaq 0x50(%rsp), %rdi
callq 0x579f0
leaq 0x8fe76(%rip), %rsi     # 0xf36ea
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 0x63ad1
leaq 0xaec07(%rip), %rdi     # 0x112527
callq 0x57720
leaq 0x50(%rsp), %rdi
movq %rax, %rsi
callq 0x579f0
leaq 0x8f7ae(%rip), %rsi     # 0xf30e7
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 0x63ab2
incl %esi
leaq 0xa0(%rsp), %rdi
movl %esi, 0x4(%rsp)
callq 0x64250
movq %rax, %r13
movb $0x1, %al
xorl %ecx, %ecx
movl %ecx, %r14d
testb $0x1, %al
je 0x63aa4
movq %r12, %rdi
movq %r13, %rsi
callq 0x570b0
leaq 0xf8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x8fd19(%rip), %rsi     # 0xf36ea
callq 0x579f0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x562f0
movq %rax, %rdi
movq 0x107ce5(%rip), %rsi    # 0x16b6d0
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 0x639a9
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x570b0
movq %r12, %rdi
leaq 0x8fc86(%rip), %rsi     # 0xf36ea
callq 0x579f0
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x562f0
movq %rax, %rdi
movq 0x107c50(%rip), %rsi    # 0x16b6d0
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 0x6397e
leaq 0xa0(%rsp), %rdi
callq 0x63e3c
leaq 0x50(%rsp), %rdi
callq 0x59200
movq 0x98(%rsp), %r14
movq 0x107ab8(%rip), %rdi    # 0x16b590
leaq 0x38(%rsp), %rsi
callq 0x555f0
leaq 0x38(%rsp), %rdi
callq 0x59200
callq 0x55370
leaq 0xa0(%rsp), %rdi
movq %rax, %rsi
callq 0x579f0
movq 0x107a88(%rip), %rdi    # 0x16b590
leaq 0xa0(%rsp), %rsi
callq 0x58a10
incl %eax
movq 0x107a72(%rip), %rdi    # 0x16b590
movl %eax, %esi
callq 0x58990
movl $0x0, (%rax)
leaq 0xa0(%rsp), %rdi
callq 0x59200
movq 0x107a51(%rip), %rsi    # 0x16b590
movq %r14, %rdi
callq 0x570b0
leaq 0xfc1aa(%rip), %rax     # 0x15fcf8
leaq 0x80(%rsp), %rdi
movq %rax, (%rdi)
callq 0x5d81a
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 0x63ca2
movq %rax, %rbx
jmp 0x63ca2
jmp 0x63bb0
jmp 0x63bb0
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x59178
jmp 0x63ba6
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
jmp 0x63ca7
movq %rax, %rbx
jmp 0x63cac
movq %rax, %rbx
jmp 0x63bd3
jmp 0x63bbf
movq %rax, %rbx
jmp 0x63bdd
jmp 0x63c33
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x59200
leaq 0x50(%rsp), %rdi
callq 0x59200
leaq 0xa0(%rsp), %rdi
callq 0x59200
jmp 0x63bef
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
jmp 0x63cb1
jmp 0x63bfb
movq %rax, %rbx
jmp 0x63c1f
jmp 0x63c0b
jmp 0x63c33
movq %rax, %rbx
jmp 0x63c47
jmp 0x63c74
movq %rax, %rbx
jmp 0x63c29
jmp 0x63c33
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x59200
leaq 0x8(%rsp), %rdi
callq 0x59200
leaq 0xa0(%rsp), %rdi
jmp 0x63cb1
movq %rax, %rbx
jmp 0x63cb6
jmp 0x63c74
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x59200
leaq 0x8(%rsp), %rdi
jmp 0x63c90
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x59178
jmp 0x63c65
movq %rax, %rbx
jmp 0x63c8b
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x59200
jmp 0x63c7e
movq %rax, %rbx
jmp 0x63c95
jmp 0x63c7b
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x59200
leaq 0x68(%rsp), %rdi
callq 0x59200
leaq 0xa0(%rsp), %rdi
callq 0x63e3c
leaq 0x50(%rsp), %rdi
callq 0x59200
leaq 0x38(%rsp), %rdi
callq 0x59200
leaq 0xfc03b(%rip), %rax     # 0x15fcf8
leaq 0x80(%rsp), %rdi
movq %rax, (%rdi)
callq 0x5d81a
movq %rbx, %rdi
callq 0x57090
nop