/* Search the bits provided for the next set bit after the value provided, and reset the calendar.  */
static unsigned int find_prev(uint8_t *bits, unsigned int max, unsigned int value, struct tm *calendar, unsigned int field, unsigned int nextField, int *lower_orders, int *res_out)
/* Search the bits provided for the next set bit after the value provided, and reset the calendar.  */
static unsigned int find_prev(uint8_t *bits, unsigned int max, unsigned int value, struct tm *calendar, unsigned int field, unsigned int nextField, int *lower_orders, int *res_out)
{
        err = add_to_field(calendar, nextField, -1);
        if (err) goto return_error;
        err = reset_max(calendar, field);
        if (err) goto return_error;
        notfound = 0;
        next_value = prev_set_bit(bits, max - 1, value, &notfound);
    }
    if (notfound || next_value != value) {
        err = set_field(calendar, field, next_value);
        if (err) goto return_error;
        err = reset_all_max(calendar, lower_orders);
        if (err) goto return_error;
    }
    return next_value;
    return_error:
    *res_out = 1;
    return 0;
}