static int mxf_get_sorted_table_segments(MXFContext *mxf, int *nb_sorted_segments, MXFIndexTableSegment ***sorted_segments)

{

    int i, j, nb_segments = 0;

    MXFIndexTableSegment **unsorted_segments;

    int last_body_sid = -1, last_index_sid = -1, last_index_start = -1;



    /* count number of segments, allocate arrays and copy unsorted segments */

    for (i = 0; i < mxf->metadata_sets_count; i++)

        if (mxf->metadata_sets[i]->type == IndexTableSegment)

            nb_segments++;



    if (!nb_segments)

        return AVERROR_INVALIDDATA;



    *sorted_segments  = av_mallocz(nb_segments * sizeof(**sorted_segments));

    unsorted_segments = av_mallocz(nb_segments * sizeof(*unsorted_segments));

    if (!sorted_segments || !unsorted_segments) {

        av_freep(sorted_segments);

        av_free(unsorted_segments);

        return AVERROR(ENOMEM);

    }



    for (i = j = 0; i < mxf->metadata_sets_count; i++)

        if (mxf->metadata_sets[i]->type == IndexTableSegment)

            unsorted_segments[j++] = (MXFIndexTableSegment*)mxf->metadata_sets[i];



    *nb_sorted_segments = 0;



    /* sort segments by {BodySID, IndexSID, IndexStartPosition}, remove duplicates while we're at it */

    for (i = 0; i < nb_segments; i++) {

        int best = -1, best_body_sid = -1, best_index_sid = -1, best_index_start = -1;



        for (j = 0; j < nb_segments; j++) {

            MXFIndexTableSegment *s = unsorted_segments[j];



            /* Require larger BosySID, IndexSID or IndexStartPosition then the previous entry. This removes duplicates.

             * We want the smallest values for the keys than what we currently have, unless this is the first such entry this time around.

             */

            if ((i == 0     || s->body_sid > last_body_sid || s->index_sid > last_index_sid || s->index_start_position > last_index_start) &&

                (best == -1 || s->body_sid < best_body_sid || s->index_sid < best_index_sid || s->index_start_position < best_index_start)) {

                best             = j;

                best_body_sid    = s->body_sid;

                best_index_sid   = s->index_sid;

                best_index_start = s->index_start_position;

            }

        }



        /* no suitable entry found -> we're done */

        if (best == -1)

            break;



        (*sorted_segments)[(*nb_sorted_segments)++] = unsorted_segments[best];

        last_body_sid    = best_body_sid;

        last_index_sid   = best_index_sid;

        last_index_start = best_index_start;

    }



    av_free(unsorted_segments);



    return 0;

}

static int mxf_get_sorted_table_segments(MXFContext *mxf, int *nb_sorted_segments, MXFIndexTableSegment ***sorted_segments)

{

    int i, j, nb_segments = 0;

    MXFIndexTableSegment **unsorted_segments;

    int last_body_sid = -1, last_index_sid = -1, last_index_start = -1;



    

    for (i = 0; i < mxf->metadata_sets_count; i++)

        if (mxf->metadata_sets[i]->type == IndexTableSegment)

            nb_segments++;



    if (!nb_segments)

        return AVERROR_INVALIDDATA;



    *sorted_segments  = av_mallocz(nb_segments * sizeof(**sorted_segments));

    unsorted_segments = av_mallocz(nb_segments * sizeof(*unsorted_segments));

    if (!sorted_segments || !unsorted_segments) {

        av_freep(sorted_segments);

        av_free(unsorted_segments);

        return AVERROR(ENOMEM);

    }



    for (i = j = 0; i < mxf->metadata_sets_count; i++)

        if (mxf->metadata_sets[i]->type == IndexTableSegment)

            unsorted_segments[j++] = (MXFIndexTableSegment*)mxf->metadata_sets[i];



    *nb_sorted_segments = 0;



    

    for (i = 0; i < nb_segments; i++) {

        int best = -1, best_body_sid = -1, best_index_sid = -1, best_index_start = -1;



        for (j = 0; j < nb_segments; j++) {

            MXFIndexTableSegment *s = unsorted_segments[j];



            





            if ((i == 0     || s->body_sid > last_body_sid || s->index_sid > last_index_sid || s->index_start_position > last_index_start) &&

                (best == -1 || s->body_sid < best_body_sid || s->index_sid < best_index_sid || s->index_start_position < best_index_start)) {

                best             = j;

                best_body_sid    = s->body_sid;

                best_index_sid   = s->index_sid;

                best_index_start = s->index_start_position;

            }

        }



        

        if (best == -1)

            break;



        (*sorted_segments)[(*nb_sorted_segments)++] = unsorted_segments[best];

        last_body_sid    = best_body_sid;

        last_index_sid   = best_index_sid;

        last_index_start = best_index_start;

    }



    av_free(unsorted_segments);



    return 0;

}

static int FUNC_0(MXFContext *VAR_0, int *VAR_1, MXFIndexTableSegment ***VAR_2)

{

    int VAR_3, VAR_4, VAR_5 = 0;

    MXFIndexTableSegment **unsorted_segments;

    int VAR_6 = -1, VAR_7 = -1, VAR_8 = -1;



    

    for (VAR_3 = 0; VAR_3 < VAR_0->metadata_sets_count; VAR_3++)

        if (VAR_0->metadata_sets[VAR_3]->type == IndexTableSegment)

            VAR_5++;



    if (!VAR_5)

        return AVERROR_INVALIDDATA;



    *VAR_2  = av_mallocz(VAR_5 * sizeof(**VAR_2));

    unsorted_segments = av_mallocz(VAR_5 * sizeof(*unsorted_segments));

    if (!VAR_2 || !unsorted_segments) {

        av_freep(VAR_2);

        av_free(unsorted_segments);

        return AVERROR(ENOMEM);

    }



    for (VAR_3 = VAR_4 = 0; VAR_3 < VAR_0->metadata_sets_count; VAR_3++)

        if (VAR_0->metadata_sets[VAR_3]->type == IndexTableSegment)

            unsorted_segments[VAR_4++] = (MXFIndexTableSegment*)VAR_0->metadata_sets[VAR_3];



    *VAR_1 = 0;



    

    for (VAR_3 = 0; VAR_3 < VAR_5; VAR_3++) {

        int VAR_9 = -1, VAR_10 = -1, VAR_11 = -1, VAR_12 = -1;



        for (VAR_4 = 0; VAR_4 < VAR_5; VAR_4++) {

            MXFIndexTableSegment *s = unsorted_segments[VAR_4];



            





            if ((VAR_3 == 0     || s->body_sid > VAR_6 || s->index_sid > VAR_7 || s->index_start_position > VAR_8) &&

                (VAR_9 == -1 || s->body_sid < VAR_10 || s->index_sid < VAR_11 || s->index_start_position < VAR_12)) {

                VAR_9             = VAR_4;

                VAR_10    = s->body_sid;

                VAR_11   = s->index_sid;

                VAR_12 = s->index_start_position;

            }

        }



        

        if (VAR_9 == -1)

            break;



        (*VAR_2)[(*VAR_1)++] = unsorted_segments[VAR_9];

        VAR_6    = VAR_10;

        VAR_7   = VAR_11;

        VAR_8 = VAR_12;

    }



    av_free(unsorted_segments);



    return 0;

}

static int FUNC_0(MXFContext *VAR_0, int *VAR_1, MXFIndexTableSegment ***VAR_2)
{ int VAR_3, VAR_4, VAR_5 = 0; MXFIndexTableSegment **unsorted_segments; int VAR_6 = -1, VAR_7 = -1, VAR_8 = -1; for (VAR_3 = 0; VAR_3 < VAR_0->metadata_sets_count; VAR_3++) if (VAR_0->metadata_sets[VAR_3]->type == IndexTableSegment)
VAR_5++; if (!VAR_5)
return AVERROR_INVALIDDATA; *VAR_2  = av_mallocz(VAR_5 * sizeof(**VAR_2)); unsorted_segments = av_mallocz(VAR_5 * sizeof(*unsorted_segments)); if (!VAR_2 || !unsorted_segments) { av_freep(VAR_2); av_free(unsorted_segments); return AVERROR(ENOMEM); } for (VAR_3 = VAR_4 = 0; VAR_3 < VAR_0->metadata_sets_count; VAR_3++) if (VAR_0->metadata_sets[VAR_3]->type == IndexTableSegment)
unsorted_segments[VAR_4++] = (MXFIndexTableSegment*)VAR_0->metadata_sets[VAR_3]; *VAR_1 = 0; for (VAR_3 = 0; VAR_3 < VAR_5; VAR_3++) { int VAR_9 = -1, VAR_10 = -1, VAR_11 = -1, VAR_12 = -1; for (VAR_4 = 0; VAR_4 < VAR_5; VAR_4++) { MXFIndexTableSegment *s = unsorted_segments[VAR_4]; if ((VAR_3 == 0     || s->body_sid > VAR_6 || s->index_sid > VAR_7 || s->index_start_position > VAR_8) &&
(VAR_9 == -1 || s->body_sid < VAR_10 || s->index_sid < VAR_11 || s->index_start_position < VAR_12)) { VAR_9             = VAR_4; VAR_10    = s->body_sid; VAR_11   = s->index_sid; VAR_12 = s->index_start_position; } } if (VAR_9 == -1)
break; (*VAR_2)[(*VAR_1)++] = unsorted_segments[VAR_9]; VAR_6    = VAR_10; VAR_7   = VAR_11; VAR_8 = VAR_12; } av_free(unsorted_segments); return 0; }