Shouldn't this be:
ret >= list_size

and below:
out_list[ret] = i;

?