// File: /workspace/github_c_makefile_star2_O2/aremmell[P]libsir/tests/tests.c
// Function: sirtest_filesystem
bool sirtest_filesystem(void) {
    INIT(si, SIRL_ALL, 0, 0, 0);
    bool pass = si_init;

    /* Wine version */
    TEST_MSG("Running under Wine: %s",
            get_wineversion() ? get_wineversion() : "no");

    /* current working directory. */
    char* cwd = _sir_getcwd();
    _sir_eqland(pass, NULL != cwd);
    TEST_MSG("_sir_getcwd: '%s'", PRN_STR(cwd));

    if (NULL != cwd) {
        /* path to this binary file. */
        char* filename = _sir_getappfilename();
        _sir_eqland(pass, NULL != filename);
        TEST_MSG("_sir_getappfilename: '%s'", PRN_STR(filename));

        if (NULL != filename) {
            /* _sir_get[base|dir]name() can potentially modify filename,
             * so make a copy for each call. */
            char* filename2 = strndup(filename, strnlen(filename, SIR_MAXPATH));
            _sir_eqland(pass, NULL != filename2);

            if (NULL != filename2) {
                /* filename, stripped of directory component(s). */
                char* _basename = _sir_getbasename(filename2);
                TEST_MSG("_sir_getbasename: '%s'", PRN_STR(_basename));

                if (!_basename) {
                    pass = false;
                } else {
                    /* the last strlen(_basename) chars of filename should match. */
                    size_t len    = strnlen(_basename, SIR_MAXPATH);
                    size_t offset = strnlen(filename, SIR_MAXPATH) - len;
                    size_t n      = 0;

                    while (n < len) {
                        if (filename[offset++] != _basename[n++]) {
                            pass = false;
                            break;
                        }
                    }
                }
            }

            /* directory this binary file resides in. */
            char* appdir = _sir_getappdir();
            _sir_eqland(pass, NULL != appdir);
            TEST_MSG("_sir_getappdir: '%s'", PRN_STR(appdir));

            /* _sir_get[base|dir]name can potentially modify filename,
             * so make a copy for each call. */
            char* filename3 = strndup(filename, strnlen(filename, SIR_MAXPATH));
            _sir_eqland(pass, NULL != filename3);

            if (NULL != appdir && NULL != filename3) {
                /* should yield the same result as _sir_getappdir(). */
                char* _dirname = _sir_getdirname(filename3);
                TEST_MSG("_sir_getdirname: '%s'", PRN_STR(_dirname));

                _sir_eqland(pass, 0 == strncmp(filename, appdir, strnlen(appdir, SIR_MAXPATH)));
                _sir_eqland(pass, NULL != _dirname &&
                    0 == strncmp(filename, _dirname, strnlen(_dirname, SIR_MAXPATH)));
            }

            _sir_safefree(&appdir);
            _sir_safefree(&filename);
            _sir_safefree(&filename2);
            _sir_safefree(&filename3);
        }

        _sir_safefree(&cwd);
    }

    /* this next section doesn't really yield any useful boolean pass/fail
     * information, but could be helpful to review manually. */
    char* dubious_dirnames[] = {
#if !defined(__WIN__)
        "/foo",
        "/foo/",
        "/foo/bar",
        "/foo/bar/bad:filename",
        "/",
        ""
#else /* __WIN__ */
        "C:\\foo",
        "C:\\foo\\",
        "C:\\foo\\bar",
        "C:\\foo\\bar\\bad:>filename",
        "C:\\",
        "//network-share/myfolder",
        ""
#endif
    };

    for (size_t n = 0; n < _sir_countof(dubious_dirnames); n++) {
        char* tmp = strndup(dubious_dirnames[n], strnlen(dubious_dirnames[n], SIR_MAXPATH));
        if (NULL != tmp) {
            TEST_MSG("_sir_getdirname(" SIR_WHITE("'%s'") ") = " SIR_WHITE("'%s'") "",
                tmp, _sir_getdirname(tmp));
            _sir_safefree(&tmp);
        }
    }

    char* dubious_filenames[] = {
#if !defined(__WIN__)
        "foo/bar/file-or-directory",
        "/foo/bar/file-or-directory",
        "/foo/bar/illegal:filename",
        "/",
        ""
#else /* __WIN__ */
        "foo\\bar\\file.with.many.full.stops",
        "C:\\foo\\bar\\poorly-renamed.txt.pdf",
        "C:\\foo\\bar\\illegal>filename.txt",
        "C:\\",
        "\\Program Files\\foo.bar",
        ""
#endif
    };

    for (size_t n = 0; n < _sir_countof(dubious_filenames); n++) {
        char* tmp = strndup(dubious_filenames[n], strnlen(dubious_filenames[n], SIR_MAXPATH));
        if (NULL != tmp) {
            TEST_MSG("_sir_getbasename(" SIR_WHITE("'%s'") ") = " SIR_WHITE("'%s'") "",
                tmp, _sir_getbasename(tmp));
            _sir_safefree(&tmp);
        }
    }

    /* absolute/relative paths. */
    static const struct {
        const char* const path;
        bool abs;
    } abs_or_rel_paths[] = {
        {"this/is/relative", false},
        {"relative", false},
        {"./relative", false},
        {"../../relative", false},
#if !defined(__WIN__)
        {"/usr/local/bin", true},
        {"/", true},
        {"/home/foo/.config", true},
        {"~/.config", true}
#else /* __WIN__ */
        {"D:\\absolute", true},
        {"C:\\Program Files\\FooBar", true},
        {"C:\\", true},
        {"\\absolute", true},
#endif
    };

    for (size_t n = 0; n < _sir_countof(abs_or_rel_paths); n++) {
        bool relative = false;
        bool ret      = _sir_ispathrelative(abs_or_rel_paths[n].path, &relative);

        if (relative == abs_or_rel_paths[n].abs) {
            pass = false;
            TEST_MSG(SIR_RED("_sir_ispathrelative('%s') = %s"), abs_or_rel_paths[n].path,
                relative ? "true" : "false");
        } else {
            TEST_MSG(SIR_GREEN("_sir_ispathrelative('%s') = %s"), abs_or_rel_paths[n].path,
                relative ? "true" : "false");
        }

        _sir_eqland(pass, ret);
        if (!ret) {
            bool unused = print_test_error(false, false);
            SIR_UNUSED(unused);
        }
    }

    /* file existence. */
    static const struct {
        const char* const path;
        bool exists;
    } real_or_not[] = {
        {"../foobarbaz", false},
        {"foobarbaz", false},
#if !defined(__WIN__)
        {"/", true},
# if !defined(__HAIKU__)
        {"/usr/bin", true},
# else
        {"/bin", true},
# endif
        {"/dev", true},
#else /* __WIN__ */
        {"C:\\Windows", true},
        {"C:\\Program Files", true},
        {"\\", true},
        {".\\", true},
        {"..\\", true},
#endif
        {"../../LICENSES/MIT.txt", true},
        {"../../msvs/libsir.sln", true},
        {"./", true},
        {"../", true},
        {"file.exists", true}
    };

    for (size_t n = 0; n < _sir_countof(real_or_not); n++) {
        bool exists = false;
        bool ret    = _sir_pathexists(real_or_not[n].path, &exists, SIR_PATH_REL_TO_APP);

        if (exists != real_or_not[n].exists) {
            pass = false;
            TEST_MSG(SIR_RED("_sir_pathexists('%s') = %s"), real_or_not[n].path,
                exists ? "true" : "false");
        } else {
            TEST_MSG(SIR_GREEN("_sir_pathexists('%s') = %s"), real_or_not[n].path,
                exists ? "true" : "false");
        }

        _sir_eqland(pass, ret);
        if (!ret) {
            bool unused = print_test_error(false, false);
            SIR_UNUSED(unused);
        }
    }

    /* checking file descriptors. */
    static int bad_fds[] = {
        0,
        1,
        2,
        1234
    };

    if (get_wineversion()) {
        bad_fds[3] = 0;
    }

    for (size_t n = 0; n < _sir_countof(bad_fds); n++) {
        if (_sir_validfd(bad_fds[n])) {
            pass = false;
            TEST_MSG(SIR_RED("_sir_validfd(%d) = true"), bad_fds[n]);
        } else {
            TEST_MSG(SIR_GREEN("_sir_validfd(%d) = false"), bad_fds[n]);
        }
    }

    FILE* f = NULL;
    bool ret = _sir_openfile(&f, "file.exists", "r", SIR_PATH_REL_TO_APP);
    if (!ret) {
        pass = false;
        HANDLE_OS_ERROR(true, "fopen(%s) failed!", "file.exists");
    } else {
        int fd = fileno(f);
        if (!_sir_validfd(fd)) {
            pass = false;
            TEST_MSG(SIR_RED("_sir_validfd(%d) = false"), fd);
        } else {
            TEST_MSG(SIR_GREEN("_sir_validfd(%d) = true"), fd);
        }
    }

    _sir_safefclose(&f);

    _sir_eqland(pass, sir_cleanup());
    return PRINT_RESULT_RETURN(pass);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
leaq 0x30(%rsp), %rbx
movl $0x108, %edx            # imm = 0x108
movq %rbx, %rdi
xorl %esi, %esi
callq 0x31f0
movl $0x100000, %eax         # imm = 0x100000
movl %eax, 0x4(%rbx)
movw $0xff, (%rbx)
movl %eax, 0xc(%rbx)
movq %rbx, %rdi
callq 0x94a9
movl %eax, %ebp
leaq 0xa843(%rip), %rsi      # 0x124f2
leaq 0xa855(%rip), %rdx      # 0x1250b
pushq $0x1
popq %r14
movl %r14d, %edi
xorl %eax, %eax
callq 0x3410
callq 0xb983
movq %rax, %r15
movq %rax, 0x28(%rsp)
testq %rax, %rax
setne %r13b
leaq 0xa843(%rip), %rbx      # 0x12522
movq %rbx, %rdx
cmovneq %rax, %rdx
leaq 0xa821(%rip), %rsi      # 0x1250e
movl %r14d, %edi
xorl %eax, %eax
callq 0x3410
testq %r15, %r15
je 0x7dd5
callq 0xb9b5
movq %rax, 0x8(%rsp)
testq %rax, %rax
setne %r13b
cmoveq %rbx, %rax
andb %bpl, %r13b
leaq 0xa81c(%rip), %rsi      # 0x1253b
pushq $0x1
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3410
movq 0x8(%rsp), %r14
testq %r14, %r14
je 0x7eed
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdi
callq 0x3210
movq %r14, %rdi
movq %rax, %rsi
callq 0x3280
movq %rax, 0x20(%rsp)
testq %rax, %rax
setne %bpl
je 0x7dd0
movq %rax, %rdi
callq 0xbb3a
movq %rax, %r14
testq %rax, %rax
movq %rax, %rdx
cmoveq %rbx, %rdx
leaq 0xa7db(%rip), %rsi      # 0x12557
xorl %r15d, %r15d
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x3410
testq %r14, %r14
je 0x7de0
andb %r13b, %bpl
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdi
callq 0x3210
movq %rax, %r15
movq 0x8(%rsp), %r12
movl $0x1000, %esi           # imm = 0x1000
movq %r12, %rdi
callq 0x3210
subq %r15, %rax
addq %r12, %rax
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x7ddd
movb (%rax,%rcx), %dl
leaq 0x1(%rcx), %rsi
cmpb (%r14,%rcx), %dl
movq %rsi, %rcx
je 0x7dbb
xorl %r15d, %r15d
jmp 0x7de0
andb %bpl, %r13b
jmp 0x7ef7
movl %ebp, %r15d
callq 0xbb65
movq %rax, 0x10(%rsp)
testq %rax, %rax
setne %r12b
cmoveq %rbx, %rax
leaq 0xa774(%rip), %rsi      # 0x12570
pushq $0x1
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3410
movq 0x8(%rsp), %r14
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdi
callq 0x3210
movq %r14, %rdi
movq %rax, %rsi
callq 0x3280
testq %rax, %rax
setne %cl
andb %r15b, %r12b
andb %cl, %r12b
cmpq $0x0, 0x10(%rsp)
movq %rax, 0x18(%rsp)
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x7ec2
movq %rax, %rdi
callq 0xbbcc
movq %rax, %r14
testq %rax, %rax
cmovneq %rax, %rbx
leaq 0xa727(%rip), %rsi      # 0x12587
xorl %r13d, %r13d
pushq $0x1
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3410
testq %r14, %r14
je 0x7ec5
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %r15
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdi
callq 0x3210
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x30a0
movl %eax, %ebp
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdi
callq 0x3210
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x30a0
orl %ebp, %eax
sete %r13b
andb %r12b, %r13b
jmp 0x7ec5
movl %r12d, %r13d
leaq 0x10(%rsp), %rdi
callq 0xbcac
leaq 0x8(%rsp), %rdi
callq 0xbcac
leaq 0x20(%rsp), %rdi
callq 0xbcac
leaq 0x18(%rsp), %rdi
callq 0xbcac
leaq 0x28(%rsp), %rdi
callq 0xbcac
leaq 0x12bd2(%rip), %r12     # 0x1aad0
pushq $0x1
popq %rbp
leaq 0x8(%rsp), %r14
xorl %ebx, %ebx
cmpq $0x30, %rbx
je 0x7f63
movq (%rbx,%r12), %r15
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdi
callq 0x3210
movq %r15, %rdi
movq %rax, %rsi
callq 0x3280
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x7f5d
movq %rax, %r15
movq %rax, %rdi
callq 0xbbcc
movl %ebp, %edi
leaq 0xa681(%rip), %rsi      # 0x125c9
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x3410
movq %r14, %rdi
callq 0xbcac
addq $0x8, %rbx
jmp 0x7f08
leaq 0x12b96(%rip), %r12     # 0x1ab00
pushq $0x1
popq %rbp
leaq 0x8(%rsp), %r14
xorl %ebx, %ebx
cmpq $0x28, %rbx
je 0x7fcf
movq (%rbx,%r12), %r15
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdi
callq 0x3210
movq %r15, %rdi
movq %rax, %rsi
callq 0x3280
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x7fc9
movq %rax, %r15
movq %rax, %rdi
callq 0xbb3a
movl %ebp, %edi
leaq 0xa691(%rip), %rsi      # 0x12645
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x3410
movq %r14, %rdi
callq 0xbcac
addq $0x8, %rbx
jmp 0x7f74
leaq 0x12b5a(%rip), %rbx     # 0x1ab30
xorl %r12d, %r12d
cmpq $0x80, %r12
je 0x8055
movb $0x0, 0x8(%rsp)
movq (%r12,%rbx), %r15
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0xbbf7
movl %eax, %r14d
movb 0x8(%rsp), %al
testb %al, %al
leaq 0xa70a(%rip), %rcx      # 0x12712
leaq 0xa6fe(%rip), %rdx      # 0x1270d
cmovneq %rdx, %rcx
cmpb 0x8(%r12,%rbx), %al
setne %bpl
leaq 0xa6b5(%rip), %rsi      # 0x126d8
leaq 0xa6ee(%rip), %rax      # 0x12718
cmovneq %rax, %rsi
pushq $0x1
popq %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x3410
testb %r14b, %r14b
jne 0x8049
xorl %edi, %edi
xorl %esi, %esi
callq 0x3ac9
andb %bpl, %r14b
andb %r14b, %r13b
addq $0x10, %r12
jmp 0x7fd9
pushq $0x8
popq %r14
leaq 0x12b50(%rip), %rbx     # 0x1abb0
cmpq $0xa8, %r14
je 0x80df
movb $0x0, 0x8(%rsp)
movq -0x8(%r14,%rbx), %r12
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
pushq $0x2
popq %rdx
callq 0xb7bc
movl %eax, %r15d
movb 0x8(%rsp), %al
testb %al, %al
leaq 0xa67f(%rip), %rcx      # 0x12712
leaq 0xa673(%rip), %rdx      # 0x1270d
cmovneq %rdx, %rcx
cmpb (%r14,%rbx), %al
sete %bpl
leaq 0xa6f8(%rip), %rsi      # 0x127a5
leaq 0xa722(%rip), %rax      # 0x127d6
cmoveq %rax, %rsi
pushq $0x1
popq %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x3410
testb %r15b, %r15b
jne 0x80d3
xorl %edi, %edi
xorl %esi, %esi
callq 0x3ac9
andb %bpl, %r15b
andb %r15b, %r13b
addq $0x10, %r14
jmp 0x8060
leaq 0xa721(%rip), %r15      # 0x12807
leaq 0xa748(%rip), %r12      # 0x12835
pushq $0x1
popq %rbx
xorl %r14d, %r14d
cmpq $0x10, %r14
je 0x812a
leaq 0x83c0(%rip), %rax      # 0x104c0
movl (%r14,%rax), %ebp
movl %ebp, %edi
callq 0xbd40
testb %al, %al
movq %r12, %rsi
cmovneq %r15, %rsi
xorb $0x1, %al
andb %al, %r13b
movl %ebx, %edi
movl %ebp, %edx
xorl %eax, %eax
callq 0x3410
addq $0x4, %r14
jmp 0x80f3
leaq 0x8(%rsp), %rdi
andq $0x0, (%rdi)
leaq 0xa65f(%rip), %rsi      # 0x12799
leaq 0xa470(%rip), %rdx      # 0x125b1
pushq $0x2
popq %rcx
callq 0xb848
testb %al, %al
je 0x816d
movq 0x8(%rsp), %rdi
callq 0x3350
movl %eax, %ebx
movl %eax, %edi
callq 0xbd40
testb %al, %al
je 0x81b2
leaq 0xa751(%rip), %rsi      # 0x128bc
jmp 0x81bc
callq 0x3080
movl (%rax), %edi
leaq 0xa364(%rip), %rsi      # 0x124df
leaq 0x838c(%rip), %rdx      # 0x1050e
movl $0x707, %ecx            # imm = 0x707
callq 0x9e45
leaq 0xa6d1(%rip), %rsi      # 0x12864
leaq 0xa5ff(%rip), %rdx      # 0x12799
xorl %r13d, %r13d
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x3410
xorl %edi, %edi
xorl %esi, %esi
callq 0x3ac9
jmp 0x81c8
leaq 0xa6d4(%rip), %rsi      # 0x1288d
xorl %r13d, %r13d
pushq $0x1
popq %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0x3410
leaq 0x8(%rsp), %rdi
callq 0xbd03
callq 0x94ae
andb %r13b, %al
movzbl %al, %edi
xorl %esi, %esi
callq 0x3ac9
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq