// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x1048, %rsp           # imm = 0x1048
movq %rdi, 0x1038(%rsp)
movq %rsi, 0x1030(%rsp)
movl %edx, 0x102c(%rsp)
movq 0x1038(%rsp), %rdi
movl $0x1, %esi
leaq 0xa29d(%rip), %rdx      # 0x10dff
leaq 0xa2a7(%rip), %rcx      # 0x10e10
movl $0x2e, %r8d
callq 0x6dd0
testb $0x1, %al
jne 0x6b7a
jmp 0x6ba4
movq 0x1030(%rsp), %rdi
movl $0x1, %esi
leaq 0xa271(%rip), %rdx      # 0x10dff
leaq 0xa27b(%rip), %rcx      # 0x10e10
movl $0x2e, %r8d
callq 0x6e50
testb $0x1, %al
jne 0x6bb1
movb $0x0, 0x1047(%rsp)
jmp 0x6dbe
movq 0x1030(%rsp), %rdi
xorl %esi, %esi
movl $0x90, %edx
callq 0x21c0
movl $0xffffffff, 0x1028(%rsp) # imm = 0xFFFFFFFF
movb $0x0, 0x1027(%rsp)
movq $0x0, 0x1018(%rsp)
movq 0x1038(%rsp), %rdi
movl 0x102c(%rsp), %ecx
leaq 0x1027(%rsp), %rsi
leaq 0x1018(%rsp), %rdx
callq 0x6ec0
testb $0x1, %al
jne 0x6c19
movb $0x0, 0x1047(%rsp)
jmp 0x6dbe
testb $0x1, 0x1027(%rsp)
je 0x6ceb
movl $0x210000, 0x1014(%rsp) # imm = 0x210000
movl $0xffffff9c, 0x1010(%rsp) # imm = 0xFFFFFF9C
cmpl $0x1, 0x102c(%rsp)
jne 0x6c71
movl 0x1010(%rsp), %edi
movq 0x1038(%rsp), %rsi
movq 0x1030(%rsp), %rdx
movl $0x100, %ecx            # imm = 0x100
callq 0x2460
movl %eax, 0x1028(%rsp)
jmp 0x6ccf
cmpl $0x2, 0x102c(%rsp)
jne 0x6ccd
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x21c0
leaq 0x10(%rsp), %rdi
movq 0x1018(%rsp), %rcx
movq 0x1038(%rsp), %r8
movl $0x1000, %esi           # imm = 0x1000
leaq 0xa173(%rip), %rdx      # 0x10e20
movb $0x0, %al
callq 0x2170
leaq 0x10(%rsp), %rdi
movq 0x1030(%rsp), %rsi
callq 0x2270
movl %eax, 0x1028(%rsp)
jmp 0x6ccf
leaq 0x1010(%rsp), %rdi
callq 0x7950
leaq 0x1018(%rsp), %rdi
callq 0x7910
jmp 0x6d07
movq 0x1038(%rsp), %rdi
movq 0x1030(%rsp), %rsi
callq 0x2270
movl %eax, 0x1028(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x1028(%rsp), %eax
jne 0x6d48
callq 0x2070
movq %rax, %rcx
movl $0x2, %eax
cmpl (%rcx), %eax
je 0x6d37
callq 0x2070
movq %rax, %rcx
movl $0x14, %eax
cmpl (%rcx), %eax
jne 0x6d48
movq 0x1030(%rsp), %rax
movl $0xffffff02, %ecx       # imm = 0xFFFFFF02
movq %rcx, 0x30(%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x1028(%rsp), %eax
jne 0x6d78
callq 0x2070
movq %rax, %rcx
movl $0x2, %eax
cmpl (%rcx), %eax
je 0x6d78
callq 0x2070
movq %rax, %rcx
movl $0x14, %eax
cmpl (%rcx), %eax
jne 0x6d83
movl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x6dab
callq 0x2070
movl (%rax), %edi
leaq 0xa06e(%rip), %rsi      # 0x10dff
leaq 0xa078(%rip), %rdx      # 0x10e10
movl $0x85, %ecx
callq 0x3c00
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, 0x1047(%rsp)
movb 0x1047(%rsp), %al
andb $0x1, %al
addq $0x1048, %rsp           # imm = 0x1048
retq
nop