static int hdcd_scan(HDCDContext *ctx, hdcd_state_t *state, const int32_t *samples, int max, int stride)

{

    int cdt_active = 0;

    /* code detect timer */

    int result;

    if (state->sustain > 0) {

        cdt_active = 1;

        if (state->sustain <= max) {

            state->control = 0;

            max = state->sustain;

        }

        state->sustain -= max;

    }

    result = 0;

    while (result < max) {

        int flag;

        int consumed = hdcd_integrate(ctx, state, &flag, samples, max - result, stride);

        result += consumed;

        if (flag > 0) {

            /* reset timer if code detected in channel */

            hdcd_sustain_reset(state);

            break;

        }

        samples += consumed * stride;

    }

    /* code detect timer expired */

    if (cdt_active && state->sustain == 0)

        state->count_sustain_expired++;

    return result;

}

static int hdcd_scan(HDCDContext *ctx, hdcd_state_t *state, const int32_t *samples, int max, int stride)

{

    int cdt_active = 0;

    

    int result;

    if (state->sustain > 0) {

        cdt_active = 1;

        if (state->sustain <= max) {

            state->control = 0;

            max = state->sustain;

        }

        state->sustain -= max;

    }

    result = 0;

    while (result < max) {

        int flag;

        int consumed = hdcd_integrate(ctx, state, &flag, samples, max - result, stride);

        result += consumed;

        if (flag > 0) {

            

            hdcd_sustain_reset(state);

            break;

        }

        samples += consumed * stride;

    }

    

    if (cdt_active && state->sustain == 0)

        state->count_sustain_expired++;

    return result;

}

static int FUNC_0(HDCDContext *VAR_0, hdcd_state_t *VAR_1, const int32_t *VAR_2, int VAR_3, int VAR_4)

{

    int VAR_5 = 0;

    

    int VAR_6;

    if (VAR_1->sustain > 0) {

        VAR_5 = 1;

        if (VAR_1->sustain <= VAR_3) {

            VAR_1->control = 0;

            VAR_3 = VAR_1->sustain;

        }

        VAR_1->sustain -= VAR_3;

    }

    VAR_6 = 0;

    while (VAR_6 < VAR_3) {

        int VAR_7;

        int VAR_8 = hdcd_integrate(VAR_0, VAR_1, &VAR_7, VAR_2, VAR_3 - VAR_6, VAR_4);

        VAR_6 += VAR_8;

        if (VAR_7 > 0) {

            

            hdcd_sustain_reset(VAR_1);

            break;

        }

        VAR_2 += VAR_8 * VAR_4;

    }

    

    if (VAR_5 && VAR_1->sustain == 0)

        VAR_1->count_sustain_expired++;

    return VAR_6;

}

static int FUNC_0(HDCDContext *VAR_0, hdcd_state_t *VAR_1, const int32_t *VAR_2, int VAR_3, int VAR_4)
{ int VAR_5 = 0; int VAR_6; if (VAR_1->sustain > 0) { VAR_5 = 1; if (VAR_1->sustain <= VAR_3) { VAR_1->control = 0; VAR_3 = VAR_1->sustain; } VAR_1->sustain -= VAR_3; } VAR_6 = 0; while (VAR_6 < VAR_3) { int VAR_7; int VAR_8 = hdcd_integrate(VAR_0, VAR_1, &VAR_7, VAR_2, VAR_3 - VAR_6, VAR_4); VAR_6 += VAR_8; if (VAR_7 > 0) { hdcd_sustain_reset(VAR_1); break; } VAR_2 += VAR_8 * VAR_4; } if (VAR_5 && VAR_1->sustain == 0)
VAR_1->count_sustain_expired++; return VAR_6; }