Mergesort Criticism in C I have written a merge sort in C and would like any advice on how to improve it. Any advice helps to make the code better!
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdint.h&gt;
#include &lt;string.h&gt;

static void mergesort_helper(void *array, uint32_t index, uint32_t length, size_t size_element, int32_t compare(void *, void *), void *storage)
{
   uint32_t i, j, k;
   if(length == 1)
   {
      /*printf(&quot;\n%d, %d\n&quot;, index, length);*/
      return;
   }

   mergesort_helper(array, index, length / 2, size_element, compare, storage);

   if(length % 2 == 0)
      mergesort_helper(array, (index + (index + length)) / 2, length / 2, size_element, compare, storage);
   else
      mergesort_helper(array, (index + (index + length)) / 2, length / 2 + 1, size_element, compare, storage);

   /*sorting part*/
   i = index;
   j = (index + (index + length)) / 2;
   k = 0;
   while(i &lt; (index + (index + length)) / 2 || j &lt; (index + length))
   {
      if(i &lt; (index + (index + length)) / 2 &amp;&amp; (compare(array + i * size_element, array + j * size_element) &lt;= 0 || j &gt;= (index + length)))
      {
          memcpy(storage + k * size_element, array + i * size_element, size_element);
          i++;
      }
      else if(j &lt; (index + length) &amp;&amp; (compare(array + i * size_element, array + j * size_element) &gt; 0) || i &gt;= (index + (index + length)) / 2)
      {
          memcpy(storage + k * size_element, array + j * size_element, size_element);
          j++;
      }
      k++;
   }
   memcpy(array + index * size_element, storage, length * size_element);
}

/*
   array -  to be sorted
   length - exclusive
   size_element - how big is one element
*/
void mergesort(void *array, uint32_t length, size_t size_element, int32_t compare(void *, void *))
{
   void *storage;
   if(length == 0)
      return;

   storage = malloc(size_element * length);
   mergesort_helper(array, 0, length, size_element, compare, storage);
   free(storage);
}


/*user code*/
int32_t compare(void *ptr1, void *ptr2)
{
   int32_t *temp_ptr1, *temp_ptr2;
   temp_ptr1 = (int32_t *)ptr1;
   temp_ptr2 = (int32_t *)ptr2;

   if(*temp_ptr1 &lt; *temp_ptr2)
      return -1;
   else if(*temp_ptr1 &gt; *temp_ptr2)
      return 1;

   return 0;
}

int main(void)
{
   uint32_t i;
   int32_t array[] = {6, 4, 3, 2, 7, -100, 1, 0, 5, 8, -100, 8};

   for(i = 0; i &lt; sizeof(array) / sizeof(array[0]); i++)
   {
      printf(&quot;%d &quot;, array[i]);
   }
   printf(&quot;\n&quot;);

   mergesort(array, sizeof(array) / sizeof(array[0]), sizeof(array[0]), &amp;compare);

   for(i = 0; i &lt; sizeof(array) / sizeof(array[0]); i++)
   {
      printf(&quot;%d &quot;, array[i]);
   }
   printf(&quot;\n&quot;);
   return 0;
}
Avoid code duplication
Your code is more verbose than necessary, because you are repeating a lot of things unnecessarily, or write things in a more complex way than necessary. For example:
(index + (index + length)) / 2

This looks really weird, and is actually equivalent to:
index + length / 2

Which makes much more sense. Even with that change, that expression is used a lot in the code, so it's best to give it a name:
uint32_t middle = index + length / 2;

You also duplicated a call to mergesort_helper() in order to handle odd and even cases of length. However, you don't have to if you write it like so:
uint32_t middle = index + length / 2;
uint32_t end = index + length;

mergesort_helper(array, index, middle - index, size_element, compare, storage);
mergesort_helper(array, middle, end - middle, size_element, compare, storage);

You might also want to consider renaming index to start.
Use const for pointer arguments in compare()
Since the function compare() should not modify the elements it is comparing, the arguments should be pointers to const:
int32_t compare(const void *ptr1, const void *ptr2) {
   const int32_t *temp_ptr1 = ptr1;
   const int32_t *temp_ptr2 = ptr2;
   ...
}

Optimizing the while-loop
There are a few minor changes that can be made to speed up the while-loop a little bit. First, calling compare() is probably going to be the most expensive operation, so you would want to avoid calling it if possible. Rearrange the order in which you check things to make use of short-circuiting:
if(i &lt; middle &amp;&amp; (j &gt;= end || compare(...)))

In the above, compare() will only be called if i &lt; middle is true and j &gt;= end is false.
Another possible optimization is realizing that if i &gt;= middle || j &gt;= end, you know you only need to copy the remainder of one side of the midpoint. Instead of doing that element by element, you could do that in one large memcpy(). It also simplifies the if-statements in the while-loop, like so:
while (i &lt; middle &amp;&amp; j &lt; end) {
    if (compare(array + i * size_element, array + j * size_element) &lt;= 0) {
        memcpy(storage + k * size_element, array + i * size_element, size_element);
        i++;
    } else {
        memcpy(storage + k * size_element, array + j * size_element, size_element);
        j++;
    }

    k++;
}

/* Copy the remainder */
if (i &lt; middle) {
    memcpy(storage + k * size_element, array + i * size_element, size_element * (middle - i));
} else {
    memcpy(storage + k * size_element, array + j * size_element, size_element * (end - j));
}

The above has also the advantage that compare() is only called once every iteration, whereas in your original code it could have been called twice.
Avoid void pointer arithmetic
Lau G mentioned void pointers, and indeed doing arithmetic on void pointers is illegal. The simple workaround is to cast it to a char * first:
static void mergesort_helper(void *array_ptr, ...)
{
    char *array = array_ptr;
    ...

To catch these sort of issues, enable strict compiler warnings. For GCC and Clang, use -Wall -pedantic. Fix all the warnings the compiler produces.