@needs_dlfcn
def test_dlfcn_qsort(self):
    self.prep_dlfcn_lib()
    self.set_setting('EXPORTED_FUNCTIONS', ['_get_cmp'])
    lib_src = '\n      int lib_cmp(const void* left, const void* right) {\n        const int* a = (const int*) left;\n        const int* b = (const int*) right;\n        if(*a > *b) return 1;\n        else if(*a == *b) return  0;\n        else return -1;\n      }\n\n      typedef int (*CMP_TYPE)(const void*, const void*);\n\n      extern "C" CMP_TYPE get_cmp() {\n        return lib_cmp;\n      }\n      '
    dirname = self.get_dir()
    filename = os.path.join(dirname, 'liblib.cpp')
    self.build_dlfcn_lib(lib_src, dirname, filename)
    self.prep_dlfcn_main()
    self.set_setting('EXPORTED_FUNCTIONS', ['_main', '_malloc'])
    src = '\n      #include <stdio.h>\n      #include <stdlib.h>\n      #include <dlfcn.h>\n\n      typedef int (*CMP_TYPE)(const void*, const void*);\n\n      int main_cmp(const void* left, const void* right) {\n        const int* a = (const int*) left;\n        const int* b = (const int*) right;\n        if(*a < *b) return 1;\n        else if(*a == *b) return  0;\n        else return -1;\n      }\n\n      int main() {\n        void* lib_handle;\n        CMP_TYPE (*getter_ptr)();\n        CMP_TYPE lib_cmp_ptr;\n        int arr[5] = {4, 2, 5, 1, 3};\n\n        qsort((void*)arr, 5, sizeof(int), main_cmp);\n        printf("Sort with main comparison: ");\n        for (int i = 0; i < 5; i++) {\n          printf("%d ", arr[i]);\n        }\n        printf("\\n");\n\n        lib_handle = dlopen("liblib.so", RTLD_NOW);\n        if (lib_handle == NULL) {\n          printf("Could not load lib.\\n");\n          return 1;\n        }\n        getter_ptr = (CMP_TYPE (*)()) dlsym(lib_handle, "get_cmp");\n        if (getter_ptr == NULL) {\n          printf("Could not find func.\\n");\n          return 1;\n        }\n        lib_cmp_ptr = getter_ptr();\n        qsort((void*)arr, 5, sizeof(int), lib_cmp_ptr);\n        printf("Sort with lib comparison: ");\n        for (int i = 0; i < 5; i++) {\n          printf("%d ", arr[i]);\n        }\n        printf("\\n");\n\n        return 0;\n      }\n      '
    self.do_run(src, 'Sort with main comparison: 5 4 3 2 1 *Sort with lib comparison: 1 2 3 4 5 *', output_nicerizer=(lambda x, err: x.replace('\n', '*')))
    if (self.get_setting('ASM_JS') and SPIDERMONKEY_ENGINE and os.path.exists(SPIDERMONKEY_ENGINE[0]) and (not self.is_wasm())):
        out = run_js('liblib.so', engine=SPIDERMONKEY_ENGINE, full_output=True, stderr=STDOUT)
        if ('asm' in out):
            self.validate_asmjs(out)
@needs_dlfcn
def test_dlfcn_qsort(self):
    self.prep_dlfcn_lib()
    self.set_setting('EXPORTED_FUNCTIONS', ['_get_cmp'])
    lib_src = '\n      int lib_cmp(const void* left, const void* right) {\n        const int* a = (const int*) left;\n        const int* b = (const int*) right;\n        if(*a > *b) return 1;\n        else if(*a == *b) return  0;\n        else return -1;\n      }\n\n      typedef int (*CMP_TYPE)(const void*, const void*);\n\n      extern "C" CMP_TYPE get_cmp() {\n        return lib_cmp;\n      }\n      '
    dirname = self.get_dir()
    filename = os.path.join(dirname, 'liblib.cpp')
    self.build_dlfcn_lib(lib_src, dirname, filename)
    self.prep_dlfcn_main()
    self.set_setting('EXPORTED_FUNCTIONS', ['_main', '_malloc'])
    src = '\n      #include <stdio.h>\n      #include <stdlib.h>\n      #include <dlfcn.h>\n\n      typedef int (*CMP_TYPE)(const void*, const void*);\n\n      int main_cmp(const void* left, const void* right) {\n        const int* a = (const int*) left;\n        const int* b = (const int*) right;\n        if(*a < *b) return 1;\n        else if(*a == *b) return  0;\n        else return -1;\n      }\n\n      int main() {\n        void* lib_handle;\n        CMP_TYPE (*getter_ptr)();\n        CMP_TYPE lib_cmp_ptr;\n        int arr[5] = {4, 2, 5, 1, 3};\n\n        qsort((void*)arr, 5, sizeof(int), main_cmp);\n        printf("Sort with main comparison: ");\n        for (int i = 0; i < 5; i++) {\n          printf("%d ", arr[i]);\n        }\n        printf("\\n");\n\n        lib_handle = dlopen("liblib.so", RTLD_NOW);\n        if (lib_handle == NULL) {\n          printf("Could not load lib.\\n");\n          return 1;\n        }\n        getter_ptr = (CMP_TYPE (*)()) dlsym(lib_handle, "get_cmp");\n        if (getter_ptr == NULL) {\n          printf("Could not find func.\\n");\n          return 1;\n        }\n        lib_cmp_ptr = getter_ptr();\n        qsort((void*)arr, 5, sizeof(int), lib_cmp_ptr);\n        printf("Sort with lib comparison: ");\n        for (int i = 0; i < 5; i++) {\n          printf("%d ", arr[i]);\n        }\n        printf("\\n");\n\n        return 0;\n      }\n      '
    self.do_run(src, 'Sort with main comparison: 5 4 3 2 1 *Sort with lib comparison: 1 2 3 4 5 *', output_nicerizer=(lambda x, err: x.replace('\n', '*')))
    if (self.get_setting('ASM_JS') and SPIDERMONKEY_ENGINE and os.path.exists(SPIDERMONKEY_ENGINE[0]) and (not self.is_wasm())):
        out = run_js('liblib.so', engine=SPIDERMONKEY_ENGINE, full_output=True, stderr=STDOUT)
        if ('asm' in out):
            self.validate_asmjs(out)