/* Does the partition: Pivot P is at the top of the stack. precondition: a <= P == a <= a, so it only needs to do the partition from lo + 1 to up - 2. Pos-condition: a <= a == P <= a returns 'i'.  */
static IdxT partition(lua_State *L, IdxT lo, IdxT up)
/* Does the partition: Pivot P is at the top of the stack. precondition: a <= P == a <= a, so it only needs to do the partition from lo + 1 to up - 2. Pos-condition: a <= a == P <= a returns 'i'.  */
static IdxT partition(lua_State *L, IdxT lo, IdxT up)
{
    while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
      if (l_unlikely(i == up - 1))
        luaL_error(L, "invalid order function for sorting");
      lua_pop(L, 1);
    }
    while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
      if (l_unlikely(j < i))
        luaL_error(L, "invalid order function for sorting");
      lua_pop(L, 1);
    }
    if (j < i) {
      lua_pop(L, 1);
      set2(L, up - 1, i);
      return i;
    }
    set2(L, i, j);
  }
}