// File: /workspace/github_c_makefile_star2_O3/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 0x9a21
movl %eax, %ebp
leaq 0xb45f(%rip), %rsi      # 0x13532
leaq 0xb471(%rip), %rdx      # 0x1354b
movl $0x1, %edi
xorl %eax, %eax
callq 0x3400
callq 0xc202
movq %rax, %r14
movq %rax, 0x28(%rsp)
testq %rax, %rax
setne %r12b
leaq 0xb461(%rip), %rbx      # 0x13562
movq %rbx, %rdx
cmovneq %rax, %rdx
leaq 0xb43f(%rip), %rsi      # 0x1354e
movl $0x1, %edi
xorl %eax, %eax
callq 0x3400
testq %r14, %r14
je 0x81fd
callq 0xc234
movq %rax, 0x8(%rsp)
testq %rax, %rax
setne %r12b
cmoveq %rbx, %rax
andb %bpl, %r12b
leaq 0xb438(%rip), %rsi      # 0x1357b
movl $0x1, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3400
movq 0x8(%rsp), %r14
testq %r14, %r14
je 0x8319
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 0x81f8
movq %rax, %rdi
callq 0xc3bc
movq %rax, %r14
testq %rax, %rax
movq %rax, %rdx
cmoveq %rbx, %rdx
leaq 0xb3f5(%rip), %rsi      # 0x13597
xorl %r15d, %r15d
movl $0x1, %edi
xorl %eax, %eax
callq 0x3400
testq %r14, %r14
je 0x8208
andb %r12b, %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 0x8205
movb (%rax,%rcx), %dl
leaq 0x1(%rcx), %rsi
cmpb (%r14,%rcx), %dl
movq %rsi, %rcx
je 0x81e3
xorl %r15d, %r15d
jmp 0x8208
andb %bpl, %r12b
jmp 0x8323
movl %ebp, %r15d
callq 0xc3f1
movq %rax, 0x10(%rsp)
testq %rax, %rax
setne %r13b
cmoveq %rbx, %rax
leaq 0xb38c(%rip), %rsi      # 0x135b0
movl $0x1, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3400
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, %r13b
andb %cl, %r13b
cmpq $0x0, 0x10(%rsp)
movq %rax, 0x18(%rsp)
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x82ee
movq %rax, %rdi
callq 0xc464
movq %rax, %r14
testq %rax, %rax
cmovneq %rax, %rbx
leaq 0xb33d(%rip), %rsi      # 0x135c7
xorl %r12d, %r12d
movl $0x1, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3400
testq %r14, %r14
je 0x82f1
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 %r12b
andb %r13b, %r12b
jmp 0x82f1
movl %r13d, %r12d
leaq 0x10(%rsp), %rdi
callq 0xc558
leaq 0x8(%rsp), %rdi
callq 0xc558
leaq 0x20(%rsp), %rdi
callq 0xc558
leaq 0x18(%rsp), %rdi
callq 0xc558
leaq 0x28(%rsp), %rdi
callq 0xc558
leaq 0x12766(%rip), %r13     # 0x1aa90
leaq 0xb2d8(%rip), %rbx      # 0x13609
leaq 0x8(%rsp), %r14
xorl %ebp, %ebp
movq (%rbp,%r13), %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 0x8387
movq %rax, %r15
movq %rax, %rdi
callq 0xc464
movl $0x1, %edi
movq %rbx, %rsi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x3400
movq %r14, %rdi
callq 0xc558
addq $0x8, %rbp
cmpq $0x30, %rbp
jne 0x8338
leaq 0x12728(%rip), %r13     # 0x1aac0
leaq 0xb2e6(%rip), %rbx      # 0x13685
leaq 0x8(%rsp), %r14
xorl %ebp, %ebp
movq (%rbp,%r13), %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 0x83f5
movq %rax, %r15
movq %rax, %rdi
callq 0xc3bc
movl $0x1, %edi
movq %rbx, %rsi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x3400
movq %r14, %rdi
callq 0xc558
addq $0x8, %rbp
cmpq $0x28, %rbp
jne 0x83a6
leaq 0x126ea(%rip), %r15     # 0x1aaf0
xorl %ebx, %ebx
movb $0x0, 0x8(%rsp)
movq (%rbx,%r15), %r14
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
callq 0xc499
movl %eax, %ebp
movb 0x8(%rsp), %al
testb %al, %al
leaq 0xb325(%rip), %rcx      # 0x13752
leaq 0xb319(%rip), %rdx      # 0x1374d
cmovneq %rdx, %rcx
cmpb 0x8(%rbx,%r15), %al
setne %r13b
leaq 0xb2d0(%rip), %rsi      # 0x13718
leaq 0xb309(%rip), %rax      # 0x13758
cmovneq %rax, %rsi
movl $0x1, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x3400
testb %bpl, %bpl
jne 0x8470
xorl %edi, %edi
xorl %esi, %esi
callq 0x3b6d
andb %r13b, %bpl
andb %bpl, %r12b
addq $0x10, %rbx
cmpq $0x80, %rbx
jne 0x8408
movl $0x8, %ebx
leaq 0x126e1(%rip), %r15     # 0x1ab70
movb $0x0, 0x8(%rsp)
movq -0x8(%rbx,%r15), %r14
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
movl $0x2, %edx
callq 0xc038
movl %eax, %ebp
movb 0x8(%rsp), %al
testb %al, %al
leaq 0xb298(%rip), %rcx      # 0x13752
leaq 0xb28c(%rip), %rdx      # 0x1374d
cmovneq %rdx, %rcx
cmpb (%rbx,%r15), %al
sete %r13b
leaq 0xb311(%rip), %rsi      # 0x137e5
leaq 0xb33b(%rip), %rax      # 0x13816
cmoveq %rax, %rsi
movl $0x1, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x3400
testb %bpl, %bpl
jne 0x84fc
xorl %edi, %edi
xorl %esi, %esi
callq 0x3b6d
andb %r13b, %bpl
andb %bpl, %r12b
addq $0x10, %rbx
cmpq $0xa8, %rbx
jne 0x848f
leaq 0x8fea(%rip), %r14      # 0x11500
leaq 0xb32a(%rip), %r15      # 0x13847
leaq 0xb351(%rip), %r13      # 0x13875
xorl %ebp, %ebp
movl (%rbp,%r14), %ebx
movl %ebx, %edi
callq 0xc5f9
testb %al, %al
movq %r13, %rsi
cmovneq %r15, %rsi
xorb $0x1, %al
andb %al, %r12b
movl $0x1, %edi
movl %ebx, %edx
xorl %eax, %eax
callq 0x3400
addq $0x4, %rbp
cmpq $0x10, %rbp
jne 0x8526
leaq 0x8(%rsp), %rdi
movq $0x0, (%rdi)
leaq 0xb26e(%rip), %rsi      # 0x137d9
leaq 0xb07f(%rip), %rdx      # 0x135f1
movl $0x2, %ecx
callq 0xc0d5
testb %al, %al
je 0x85a0
movq 0x8(%rsp), %rdi
callq 0x3350
movl %eax, %ebx
movl %eax, %edi
callq 0xc5f9
testb %al, %al
je 0x85e7
leaq 0xb35e(%rip), %rsi      # 0x138fc
jmp 0x85f1
callq 0x3080
movl (%rax), %edi
leaq 0xaf71(%rip), %rsi      # 0x1351f
leaq 0x8f99(%rip), %rdx      # 0x1154e
movl $0x707, %ecx            # imm = 0x707
callq 0xa434
leaq 0xb2de(%rip), %rsi      # 0x138a4
leaq 0xb20c(%rip), %rdx      # 0x137d9
xorl %r12d, %r12d
movl $0x1, %edi
xorl %eax, %eax
callq 0x3400
xorl %edi, %edi
xorl %esi, %esi
callq 0x3b6d
jmp 0x85ff
leaq 0xb2df(%rip), %rsi      # 0x138cd
xorl %r12d, %r12d
movl $0x1, %edi
movl %ebx, %edx
xorl %eax, %eax
callq 0x3400
leaq 0x8(%rsp), %rdi
callq 0xc5b7
callq 0x9a26
andb %r12b, %al
movzbl %al, %edi
xorl %esi, %esi
callq 0x3b6d
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq