// File: /workspace/github_c_makefile_star2_O2/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 0xfd385(%rip), %rdi     # 0x161590
xorl %esi, %esi
callq 0x57d40
testb %al, %al
je 0x6422a
movq 0xfd373(%rip), %rsi     # 0x161590
movq %r14, %rdi
callq 0x58280
jmp 0x646a0
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movl $0x1001, %esi           # imm = 0x1001
callq 0x5df48
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x6425a
movq (%rax), %rdi
movslq 0x90(%rsp), %rdx
xorl %esi, %esi
callq 0x56ff0
leaq 0x7f439(%rip), %rsi     # 0xe369a
leaq 0xa0(%rsp), %rdi
callq 0x58c40
callq 0x59e70
movslq %eax, %rsi
leaq 0x38(%rsp), %rdi
callq 0x57e80
leaq 0xa0(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x573e0
movq %rax, %rbx
leaq 0x7f405(%rip), %rsi     # 0xe36a1
leaq 0x20(%rsp), %rdi
callq 0x58c40
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x573e0
movq %rax, %rdi
callq 0x598d0
movq 0x88(%rsp), %rcx
testq %rcx, %rcx
je 0x642cd
movq (%rcx), %rsi
jmp 0x642cf
xorl %esi, %esi
movslq 0x90(%rsp), %rdx
decq %rdx
movq %rax, %rdi
callq 0x56ef0
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x5a588
leaq 0x38(%rsp), %rdi
callq 0x5a588
leaq 0xa0(%rsp), %rdi
callq 0x5a588
testq %rbx, %rbx
jle 0x6432b
movq 0x88(%rsp), %rax
testq %rax, %rax
je 0x6431d
movq (%rax), %rsi
jmp 0x6431f
xorl %esi, %esi
movq 0xfd26a(%rip), %rdi     # 0x161590
callq 0x57010
movq 0xfd25e(%rip), %rdi     # 0x161590
xorl %esi, %esi
callq 0x56280
testb %al, %al
je 0x6463b
leaq 0x7f360(%rip), %rsi     # 0xe36a8
leaq 0x38(%rsp), %rdi
callq 0x58c40
movq 0xfd377(%rip), %rdi     # 0x1616d0
leaq 0x38(%rsp), %rsi
callq 0x58a10
testb %al, %al
je 0x64378
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x5a180
jmp 0x64385
leaq 0xa0(%rsp), %rdi
callq 0x59820
movq 0xfd344(%rip), %rsi     # 0x1616d0
leaq 0xa0(%rsp), %rdi
callq 0x573e0
movq %rax, %rbx
leaq 0x7f303(%rip), %rsi     # 0xe36a6
leaq 0x68(%rsp), %rdi
callq 0x58c40
leaq 0x7f2f4(%rip), %rsi     # 0xe36a8
leaq 0x8(%rsp), %rdi
callq 0x58c40
leaq 0x68(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x58800
leaq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x58280
leaq 0x8(%rsp), %rdi
callq 0x5a588
leaq 0x68(%rsp), %rdi
callq 0x5a588
leaq 0xa0(%rsp), %rdi
callq 0x5a588
leaq 0x38(%rsp), %rdi
callq 0x5a588
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
callq 0x5a180
leaq 0xa0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x57940
leaq 0xa0(%rsp), %rdi
callq 0x580a0
movl %eax, %ebx
leaq 0xa0(%rsp), %rdi
callq 0x5a500
leaq 0x38(%rsp), %rdi
callq 0x5a588
testb %bl, %bl
jne 0x64620
leaq 0x9dfb7(%rip), %rdi     # 0x102417
callq 0x58950
leaq 0x68(%rsp), %rdi
movq %rax, %rsi
callq 0x58c40
leaq 0x7ec55(%rip), %rsi     # 0xe30ce
leaq 0x38(%rsp), %rdi
callq 0x58c40
movq %r14, 0x98(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x591a0
leaq 0x38(%rsp), %r15
movq %r15, %rdi
callq 0x5a588
pushq $-0x1
popq %rax
xorl %r14d, %r14d
leaq 0x110(%rsp), %r12
movl %eax, 0x4(%rsp)
testl %r14d, %r14d
movl 0x4(%rsp), %esi
jne 0x64601
movl 0xa8(%rsp), %eax
decl %eax
cmpl %eax, %esi
jge 0x64601
incl %esi
leaq 0xa0(%rsp), %rdi
movl %esi, 0x4(%rsp)
callq 0x64d9c
movq %rax, %rbx
movb $0x1, %al
pushq $-0x1
popq %rcx
movl %ecx, %r14d
testb $0x1, %al
je 0x644c1
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x58280
leaq 0xf8(%rsp), %r13
movq %r13, %rdi
leaq 0x7f188(%rip), %rsi     # 0xe36a8
callq 0x58c40
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x573e0
movq %rax, %rdi
movq 0xfd194(%rip), %rsi     # 0x1616d0
callq 0x573e0
movq %rax, %r13
movq %r12, %rdi
xorl %esi, %esi
callq 0x5a180
movq %r12, %rdx
movq %r15, %rbp
movq %r15, %rdi
movq %r13, %rsi
movq %rdx, %r12
callq 0x57940
movq %rbp, %rdi
callq 0x580a0
movl %eax, %r13d
movq %rbp, %rdi
callq 0x5a500
movq %r12, %rdi
callq 0x5a588
leaq 0xf8(%rsp), %rdi
callq 0x5a588
leaq 0x8(%rsp), %rdi
callq 0x5a588
xorl %eax, %eax
movl $0x0, %ecx
testb %r13b, %r13b
je 0x644fa
movq %r15, %rdi
movq %rbx, %rsi
callq 0x58280
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x7f0ea(%rip), %rsi     # 0xe36a8
callq 0x58c40
movq %r15, %rdi
movq %rbx, %rsi
callq 0x573e0
movq %rax, %rdi
movq 0xfd0f8(%rip), %rsi     # 0x1616d0
callq 0x573e0
leaq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x56630
leaq 0x8(%rsp), %rdi
callq 0x5a588
movq %r15, %rdi
callq 0x5a588
jmp 0x644c1
leaq 0xa0(%rsp), %rdi
callq 0x64988
leaq 0x68(%rsp), %rdi
callq 0x5a588
movq 0x98(%rsp), %r14
movq 0xfcf69(%rip), %rdi     # 0x161590
leaq 0x20(%rsp), %rsi
callq 0x56630
leaq 0x20(%rsp), %rdi
callq 0x5a588
callq 0x563a0
leaq 0xa0(%rsp), %rdi
movq %rax, %rsi
callq 0x58c40
movq 0xfcf39(%rip), %rdi     # 0x161590
leaq 0xa0(%rsp), %rsi
callq 0x59d50
incl %eax
movq 0xfcf23(%rip), %rdi     # 0x161590
movl %eax, %esi
callq 0x59cc0
andl $0x0, (%rax)
leaq 0xa0(%rsp), %rdi
callq 0x5a588
movq 0xfcf05(%rip), %rsi     # 0x161590
movq %r14, %rdi
callq 0x58280
leaq 0x80(%rsp), %rdi
callq 0x5df9a
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 0x38(%rsp), %rdi
callq 0x5a588
jmp 0x647e4
movq %rax, %rbx
jmp 0x647e4
jmp 0x646f2
jmp 0x646f2
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x5a500
jmp 0x646e8
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
jmp 0x647e9
movq %rax, %rbx
jmp 0x647ee
movq %rax, %rbx
jmp 0x64715
jmp 0x64701
movq %rax, %rbx
jmp 0x6471f
jmp 0x64775
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x5a588
leaq 0x68(%rsp), %rdi
callq 0x5a588
leaq 0xa0(%rsp), %rdi
callq 0x5a588
jmp 0x64731
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
jmp 0x647f3
jmp 0x6473d
movq %rax, %rbx
jmp 0x64761
jmp 0x6474d
jmp 0x64775
jmp 0x64795
movq %rax, %rbx
jmp 0x64789
movq %rax, %rbx
jmp 0x6476b
jmp 0x64775
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x5a588
leaq 0x38(%rsp), %rdi
callq 0x5a588
leaq 0xa0(%rsp), %rdi
jmp 0x647f3
movq %rax, %rbx
jmp 0x647f8
jmp 0x64795
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x5a588
leaq 0x38(%rsp), %rdi
jmp 0x647d2
movq %rax, %rbx
jmp 0x647cd
movq %rax, %rbx
jmp 0x647d7
movq %rax, %rbx
jmp 0x647ac
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x5a500
leaq 0x110(%rsp), %rdi
callq 0x5a588
jmp 0x647c0
jmp 0x647bd
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x5a588
leaq 0x8(%rsp), %rdi
callq 0x5a588
leaq 0xa0(%rsp), %rdi
callq 0x64988
leaq 0x68(%rsp), %rdi
callq 0x5a588
leaq 0x20(%rsp), %rdi
callq 0x5a588
leaq 0x80(%rsp), %rdi
callq 0x5df9a
movq %rbx, %rdi
callq 0x58260
nop