// File: /workspace/github_c_makefile_star2_O1/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)
movw $0x0, 0x8(%rbx)
movq %rbx, %rdi
callq 0x9a31
movl %eax, %ebp
leaq 0xb483(%rip), %rsi      # 0x13532
leaq 0xb495(%rip), %rdx      # 0x1354b
movl $0x1, %edi
xorl %eax, %eax
callq 0x3400
callq 0xc27e
movq %rax, 0x18(%rsp)
testq %rax, %rax
setne %r12b
leaq 0xb488(%rip), %rbx      # 0x13562
cmoveq %rbx, %rax
andb %bpl, %r12b
leaq 0xb466(%rip), %rsi      # 0x1354e
movl $0x1, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3400
cmpq $0x0, 0x18(%rsp)
je 0x82fd
callq 0xc2b0
movq %rax, 0x8(%rsp)
testq %rax, %rax
setne %bpl
cmoveq %rbx, %rax
andb %r12b, %bpl
leaq 0xb459(%rip), %rsi      # 0x1357b
movl $0x1, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x3400
movq 0x8(%rsp), %r14
testq %r14, %r14
je 0x82f0
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdi
callq 0x3210
movq %r14, %rdi
movq %rax, %rsi
callq 0x3280
testq %rax, %rax
setne %r13b
andb %bpl, %r13b
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x81dd
movq %rax, %rdi
callq 0xc472
movq %rax, %r14
testq %rax, %rax
movq %rax, %rdx
cmoveq %rbx, %rdx
leaq 0xb410(%rip), %rsi      # 0x13597
xorl %ebp, %ebp
movl $0x1, %edi
xorl %eax, %eax
callq 0x3400
testq %r14, %r14
je 0x81e0
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 0x81dd
movb (%rax,%rcx), %dl
leaq 0x1(%rcx), %rsi
cmpb (%r14,%rcx), %dl
movq %rsi, %rcx
je 0x81c4
xorl %ebp, %ebp
jmp 0x81e0
movl %r13d, %ebp
callq 0xc4a7
movq %rax, 0x10(%rsp)
testq %rax, %rax
setne %r12b
cmoveq %rbx, %rax
leaq 0xb3b4(%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 %bpl, %r12b
andb %cl, %r12b
cmpq $0x0, 0x10(%rsp)
movq %rax, 0x20(%rsp)
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x82c5
movq %rax, %rdi
callq 0xc51c
movq %rax, %r14
testq %rax, %rax
cmovneq %rax, %rbx
leaq 0xb365(%rip), %rsi      # 0x135c7
xorl %ebp, %ebp
movl $0x1, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3400
testq %r14, %r14
je 0x82c8
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 %bpl
andb %r12b, %bpl
jmp 0x82c8
movl %r12d, %ebp
leaq 0x10(%rsp), %rdi
callq 0xc618
leaq 0x8(%rsp), %rdi
callq 0xc618
leaq 0x28(%rsp), %rdi
callq 0xc618
leaq 0x20(%rsp), %rdi
callq 0xc618
leaq 0x18(%rsp), %rdi
callq 0xc618
movl %ebp, %r12d
leaq 0x1278c(%rip), %r13     # 0x1aa90
leaq 0xb2fe(%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 0x8361
movq %rax, %r15
movq %rax, %rdi
callq 0xc51c
movl $0x1, %edi
movq %rbx, %rsi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x3400
movq %r14, %rdi
callq 0xc618
addq $0x8, %rbp
cmpq $0x30, %rbp
jne 0x8312
leaq 0x1274e(%rip), %r13     # 0x1aac0
leaq 0xb30c(%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 0x83cf
movq %rax, %r15
movq %rax, %rdi
callq 0xc472
movl $0x1, %edi
movq %rbx, %rsi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0x3400
movq %r14, %rdi
callq 0xc618
addq $0x8, %rbp
cmpq $0x28, %rbp
jne 0x8380
xorl %ebx, %ebx
leaq 0x1270e(%rip), %r15     # 0x1aaf0
xorl %r13d, %r13d
movb $0x0, 0x8(%rsp)
movq (%r13,%r15), %r14
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
callq 0xc551
movb 0x8(%rsp), %dl
testb %dl, %dl
leaq 0xb349(%rip), %rcx      # 0x13752
leaq 0xb33d(%rip), %rsi      # 0x1374d
cmovneq %rsi, %rcx
cmpb 0x8(%r13,%r15), %dl
leaq 0xb338(%rip), %rsi      # 0x13758
leaq 0xb2f1(%rip), %rdx      # 0x13718
cmoveq %rdx, %rsi
movl %eax, %ebp
movzbl %r12b, %r12d
cmovel %ebx, %r12d
movl $0x1, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x3400
testb %bpl, %bpl
cmovel %ebx, %r12d
jne 0x8456
xorl %edi, %edi
xorl %esi, %esi
callq 0x3b04
addq $0x10, %r13
cmpq $0x80, %r13
jne 0x83e5
movl $0x8, %ebx
leaq 0x12701(%rip), %r15     # 0x1ab70
xorl %r13d, %r13d
movb $0x0, 0x8(%rsp)
movq -0x8(%rbx,%r15), %r14
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
movl $0x2, %edx
callq 0xc0b4
movl %eax, %ebp
movb 0x8(%rsp), %al
testb %al, %al
leaq 0xb2b5(%rip), %rcx      # 0x13752
leaq 0xb2a9(%rip), %rdx      # 0x1374d
cmovneq %rdx, %rcx
cmpb (%rbx,%r15), %al
leaq 0xb332(%rip), %rsi      # 0x137e5
leaq 0xb35c(%rip), %rax      # 0x13816
cmoveq %rax, %rsi
movzbl %r12b, %r12d
cmovnel %r13d, %r12d
movl $0x1, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x3400
testb %bpl, %bpl
cmovel %r13d, %r12d
jne 0x84e7
xorl %edi, %edi
xorl %esi, %esi
callq 0x3b04
addq $0x10, %rbx
cmpq $0xa8, %rbx
jne 0x8472
leaq 0x9001(%rip), %r15      # 0x11500
leaq 0xb341(%rip), %r13      # 0x13847
leaq 0xb368(%rip), %rbp      # 0x13875
xorl %r14d, %r14d
movl (%r14,%r15), %ebx
movl %ebx, %edi
callq 0xc6b9
testb %al, %al
movq %rbp, %rsi
cmovneq %r13, %rsi
movzbl %r12b, %r12d
movl $0x0, %eax
cmovnel %eax, %r12d
movl $0x1, %edi
movl %ebx, %edx
xorl %eax, %eax
callq 0x3400
addq $0x4, %r14
cmpq $0x10, %r14
jne 0x8510
leaq 0x8(%rsp), %rdi
movq $0x0, (%rdi)
leaq 0xb27d(%rip), %rsi      # 0x137d9
leaq 0xb08e(%rip), %rdx      # 0x135f1
movl $0x2, %ecx
callq 0xc151
testb %al, %al
je 0x8591
movq 0x8(%rsp), %rdi
callq 0x3350
movl %eax, %ebx
movl %eax, %edi
callq 0xc6b9
testb %al, %al
je 0x85d8
leaq 0xb36d(%rip), %rsi      # 0x138fc
jmp 0x85e2
callq 0x3080
movl (%rax), %edi
leaq 0xaf80(%rip), %rsi      # 0x1351f
leaq 0x8fa8(%rip), %rdx      # 0x1154e
movl $0x707, %ecx            # imm = 0x707
callq 0xa444
leaq 0xb2ed(%rip), %rsi      # 0x138a4
leaq 0xb21b(%rip), %rdx      # 0x137d9
xorl %r12d, %r12d
movl $0x1, %edi
xorl %eax, %eax
callq 0x3400
xorl %edi, %edi
xorl %esi, %esi
callq 0x3b04
jmp 0x85f0
leaq 0xb2ee(%rip), %rsi      # 0x138cd
xorl %r12d, %r12d
movl $0x1, %edi
movl %ebx, %edx
xorl %eax, %eax
callq 0x3400
leaq 0x8(%rsp), %rdi
callq 0xc677
callq 0x9a36
andb %r12b, %al
movzbl %al, %edi
xorl %esi, %esi
callq 0x3b04
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq