-void
-qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
+int __heapsort_r(void *, size_t, size_t, void *, int (*)(void *, const void *, const void *));
+#endif
+
+/*
+ * Simple insertion sort routine.
+ */
+static bool
+_isort(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp, int swap_limit, int swaptype_long, int swaptype_int)
+{
+ int swap_cnt = 0;
+ for (char *pm = (char *)a + es; pm < (char *)a + n * es; pm += es) {
+ for (char *pl = pm; pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
+ pl -= es) {
+ swap(pl, pl - es);
+ if (swap_limit && ++swap_cnt > swap_limit) return false;
+ }
+ }
+ return true;
+}
+
+#ifdef I_AM_QSORT_R
+static void
+_qsort(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp, int depth_limit)