thread->sort_key = thread->message;
if (thread->next) { thread = thread->next; continue; } }
THREAD *mutt_sort_subthreads (THREAD *thread){  THREAD **array, *sort_key;  int i, array_size;    Sort ^= SORT_REVERSE;  if (!thread || !compare_threads (NULL, NULL))    return thread;  /* we put things into the array backwards to save some cycles,   * but we want to have to move less stuff around if we're    * resorting, so we sort backwards and then put them back   * in reverse order so they're forwards   */  array = safe_malloc ((array_size = 256) * sizeof (THREAD *));  while (1)  {    while (thread->child)    {      thread->sort_key = thread->message;      thread = thread->child;    }    while (thread->next && !thread->child)    {      thread->sort_key = thread->message;      thread = thread->next;    }    if (thread->child)      continue;    thread->sort_key = thread->message;    while (!thread->next)    {      if (thread->prev)      {	for (i = 0; thread; i++, thread = thread->prev)	{	  if (i >= array_size)	    safe_realloc ((void **) &array, (array_size *= 2) * sizeof (THREAD *));	  array[i] = thread;	}	qsort ((void *) array, i, sizeof (THREAD *), *compare_threads);	array[0]->next = NULL;	thread = array[i - 1];	thread->prev = NULL;	if (thread->parent)	{	  thread->parent->child = thread;	  sort_key = array[(!(Sort & SORT_LAST) ^ !(Sort & SORT_REVERSE)) ? i - 1 : 0];	}		while (--i)	{	  array[i - 1]->prev = array[i];	  array[i]->next = array[i - 1];	}      }      else	sort_key = thread;      if (thread->parent)      {	if (Sort & SORT_LAST)	{	  if (!thread->parent->sort_key	      || ((((Sort & SORT_REVERSE) ? 1 : -1)		   * compare_threads ((void *) &thread->parent,				      (void *) &sort_key))		  > 0))	    thread->parent->sort_key = sort_key->sort_key;	}	else if (!thread->parent->sort_key)	  thread->parent->sort_key = sort_key->sort_key;	thread = thread->parent;      }      else      {	Sort ^= SORT_REVERSE;	return (thread);      }    }    thread = thread->next;  }}