Each sub-block of this needs to be conditioned on max_mode or min_mode, as appropriate.
Otherwise, you xstrdup() values for max and min, regardless of what statistic you're actually computing; and nothing is going to tidy them away.