// File: /workspace/github_c_makefile_star2_O2/aremmell[P]libsir/src/sirfilesystem.c
// Function: sir_pathgetstat
bool _sir_pathgetstat(const char* restrict path, struct stat* restrict st, sir_rel_to rel_to) {
    if (!_sir_validstr(path) || !_sir_validptr(st))
        return false;

    (void)memset(st, 0, sizeof(struct stat));

    int stat_ret          = -1;
    bool relative         = false;
    const char* base_path = NULL;

    if (!_sir_getrelbasepath(path, &relative, &base_path, rel_to))
        return false;

    if (relative) {
#if !defined(__WIN__)
# if defined(__MACOS__) || defined(_AIX) || defined(__EMSCRIPTEN__)
#  if !defined(O_SEARCH)
        int open_flags = O_DIRECTORY;
#  else
        int open_flags = O_SEARCH;
#  endif
# elif defined(__linux__) || defined(__HURD__) || defined(__QNX__) || \
       defined(__managarm__)
#  if !defined(__SUNPRO_C) && !defined(__SUNPRO_CC) && defined(O_PATH)
        int open_flags = O_PATH | O_DIRECTORY;
#  else
        int open_flags = O_DIRECTORY;
#  endif
# elif defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__serenity__)
        int open_flags = O_EXEC | O_DIRECTORY;
# elif defined(__SOLARIS__) || defined(__DragonFly__) || \
       defined(__NetBSD__) || defined(__HAIKU__) || defined(__OpenBSD__)
        int open_flags = O_DIRECTORY;
# else
#  error "unknown open_flags for your platform; please contact the developers"
# endif

# if !(defined(_AIX) || !defined(__PASE__))
        int fd = open(base_path, open_flags);
        if (-1 == fd) {
            _sir_safefree(&base_path);
            return _sir_handleerr(errno);
        }
        stat_ret = fstatat(fd, path, st, AT_SYMLINK_NOFOLLOW);
# else
        // HACKHACK: fstatat does not work properly for any fd other than AT_FDCWD.
        SIR_UNUSED(open_flags);
        int fd = AT_FDCWD;
        if (rel_to == SIR_PATH_REL_TO_CWD) {
            /* cppcheck-suppress invalidFunctionArg */
            stat_ret = fstatat(fd, path, st, AT_SYMLINK_NOFOLLOW);
        } else if (rel_to == SIR_PATH_REL_TO_APP) {
            char abs_path[SIR_MAXPATH] = {0};
            (void)snprintf(abs_path, SIR_MAXPATH, "%s/%s", base_path, path);
            stat_ret = stat(abs_path, st);
        }
# endif
        _sir_safeclose(&fd);
        _sir_safefree(&base_path);
    } else {
        stat_ret = stat(path, st);
    }
#else /* __WIN__ */
        char abs_path[SIR_MAXPATH] = {0};
        (void)snprintf(abs_path, SIR_MAXPATH, "%s\\%s", base_path, path);

# if defined(__EMBARCADEROC__) && (__clang_major__ < 15)
        /* Embarcadero <12 does not like paths that end in slashes, nor does it appreciate
         * paths like './' and '../'; this hack is needed for RAD Studio 11.3 and earlier. */
        char resolved_path[SIR_MAXPATH] = {0};
        if (!GetFullPathNameA(abs_path, SIR_MAXPATH, resolved_path, NULL)) {
            _sir_safefree(&base_path);
            return _sir_handlewin32err(GetLastError());
        }

        PathRemoveBackslashA(resolved_path);
        (void)_sir_strlcpy(abs_path, resolved_path, SIR_MAXPATH);
# endif

        stat_ret = stat(abs_path, st);
        _sir_safefree(&base_path);
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x1028, %rsp           # imm = 0x1028
movl %edx, %r15d
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x50c0(%rip), %rsi      # 0x99e1
pushq $0x2e
popq %rdx
callq 0x4a88
testb %al, %al
je 0x49ab
leaq 0x50ad(%rip), %rdx      # 0x99e1
pushq $0x1
popq %rsi
pushq $0x2e
popq %rcx
movq %rbx, %rdi
callq 0x4ab0
testb %al, %al
je 0x49ab
xorl %ebp, %ebp
movl $0x90, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x21b0
leaq 0x13(%rsp), %rsi
movb $0x0, (%rsi)
leaq 0x18(%rsp), %rdx
andq $0x0, (%rdx)
movq %r14, %rdi
movl %r15d, %ecx
callq 0x4ae2
testb %al, %al
je 0x49ad
cmpb $0x1, 0x13(%rsp)
jne 0x49bd
movl $0xffffff9c, 0x14(%rsp) # imm = 0xFFFFFF9C
cmpl $0x2, %r15d
je 0x49cd
cmpl $0x1, %r15d
jne 0x4a18
pushq $-0x64
popq %rdi
movq %r14, %rsi
movq %rbx, %rdx
movl $0x100, %ecx            # imm = 0x100
callq 0x2460
jmp 0x4a13
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2250
movl %eax, %r14d
jmp 0x4a30
leaq 0x20(%rsp), %r15
movl $0x1000, %edx           # imm = 0x1000
movq %r15, %rdi
xorl %esi, %esi
callq 0x21b0
movq 0x18(%rsp), %r9
movq %r14, (%rsp)
leaq 0x5011(%rip), %r8       # 0x9a02
pushq $0x1
popq %rdx
movl $0x1000, %esi           # imm = 0x1000
movl $0x1000, %ecx           # imm = 0x1000
movq %r15, %rdi
xorl %eax, %eax
callq 0x2040
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2250
movl %eax, %r14d
jmp 0x4a1c
pushq $-0x1
popq %r14
leaq 0x14(%rsp), %rdi
callq 0x50b4
leaq 0x18(%rsp), %rdi
callq 0x5098
movb $0x1, %bpl
cmpl $-0x1, %r14d
jne 0x49ad
callq 0x2080
movl (%rax), %edi
cmpl $0x14, %edi
je 0x4a4e
cmpl $0x2, %edi
jne 0x4a69
movl $0xffffff02, %eax       # imm = 0xFFFFFF02
movq %rax, 0x30(%rbx)
cmpl $0x2, %edi
je 0x49ad
cmpl $0x14, %edi
je 0x49ad
leaq 0x4f71(%rip), %rsi      # 0x99e1
leaq 0x4f7b(%rip), %rdx      # 0x99f2
movl $0x85, %ecx
callq 0x3231
movl %eax, %ebp
jmp 0x49ad