static int vlv_results(struct vlv_context *ac, struct ldb_reply *ares)
{
	struct ldb_vlv_resp_control *vlv;
	unsigned int num_ctrls;
	int ret, i, first_i, last_i;
	struct ldb_vlv_req_control *vlv_details;
	struct ldb_server_sort_control *sort_details;
	int target = 0;

	if (ac->store == NULL) {
		ret = LDB_ERR_OPERATIONS_ERROR;
		return ldb_module_done(
			ac->req, ac->controls, ares->response, ret);
	}

	if (ac->store->first_ref) {
		/* There is no right place to put references in the sorted
		   results, so we send them as soon as possible.
		*/
		ret = send_referrals(ac->store, ac->req);
		if (ret != LDB_SUCCESS) {
			/*
			 * send_referrals will have called ldb_module_done
			 * if there was an error.
			 */
			return ret;
		}
	}

	vlv_details = ac->store->vlv_details;
	sort_details = ac->store->sort_details;

	if (ac->store->num_entries != 0) {
		if (vlv_details->type == 1) {
			target = vlv_gt_eq_to_index(ac, ac->store->results,
						    vlv_details,
						    sort_details, &ret);
			if (ret != LDB_SUCCESS) {
				return ldb_module_done(
					ac->req,
					ac->controls,
					ares->response,
					ret);
			}
		} else {
			target = vlv_calc_real_offset(vlv_details->match.byOffset.offset,
						      vlv_details->match.byOffset.contentCount,
						      ac->store->num_entries);
			if (target == -1) {
				ret = LDB_ERR_OPERATIONS_ERROR;
				return ldb_module_done(
					ac->req,
					ac->controls,
					ares->response,
					ret);
			}
		}

		/* send the results */
		first_i = MAX(target - vlv_details->beforeCount, 0);
		last_i = MIN(target + vlv_details->afterCount,
			     ac->store->num_entries - 1);

		for (i = first_i; i <= last_i; i++) {
			struct ldb_result *result = NULL;
			struct GUID *guid = &ac->store->results[i];

			ret = vlv_search_by_dn_guid(ac->module, ac, &result, guid,
						    ac->req->op.search.attrs);

			if (ret == LDAP_NO_SUCH_OBJECT
			    || result->count != 1) {
				/*
				 * The thing isn't there, which we quietly
				 * ignore and go on to send an extra one
				 * instead.
				 *
				 * result->count == 0 or > 1 can only
				 * happen if ASQ (which breaks all the
				 * rules) is somehow invoked (as this
				 * is a BASE search).
				 *
				 * (We skip the ASQ cookie for the
				 * GUID searches)
				 */
				if (last_i < ac->store->num_entries - 1) {
					last_i++;
				}
				continue;
			} else if (ret != LDB_SUCCESS) {
				return ldb_module_done(
					ac->req,
					ac->controls,
					ares->response,
					ret);
			}

			ret = ldb_module_send_entry(ac->req, result->msgs[0],
						    NULL);
			if (ret != LDB_SUCCESS) {
				/*
				 * ldb_module_send_entry will have called
				 * ldb_module_done if there was an error
				 */
				return ret;
			}
		}
	} else {
		target = -1;
	}

	/* return result done */
	num_ctrls = 1;
	i = 0;

	if (ac->store->controls != NULL) {
		while (ac->store->controls[i]){
			i++; /* counting */
		}
		num_ctrls += i;
	}

	ac->controls = talloc_array(ac, struct ldb_control *, num_ctrls + 1);
	if (ac->controls == NULL) {
		ret = LDB_ERR_OPERATIONS_ERROR;
		return ldb_module_done(
			ac->req, ac->controls, ares->response, ret);
	}
	ac->controls[num_ctrls] = NULL;

	for (i = 0; i < (num_ctrls -1); i++) {
		ac->controls[i] = talloc_reference(ac->controls, ac->store->controls[i]);
	}

	ac->controls[i] = talloc(ac->controls, struct ldb_control);
	if (ac->controls[i] == NULL) {
		ret = LDB_ERR_OPERATIONS_ERROR;
		return ldb_module_done(
			ac->req, ac->controls, ares->response, ret);
	}

	ac->controls[i]->oid = talloc_strdup(ac->controls[i],
					     LDB_CONTROL_VLV_RESP_OID);
	if (ac->controls[i]->oid == NULL) {
		ret = LDB_ERR_OPERATIONS_ERROR;
		return ldb_module_done(
			ac->req, ac->controls, ares->response, ret);
	}

	ac->controls[i]->critical = 0;

	vlv = talloc(ac->controls[i], struct ldb_vlv_resp_control);
	if (vlv == NULL) {
		ret = LDB_ERR_OPERATIONS_ERROR;
		return ldb_module_done(
			ac->req, ac->controls, ares->response, ret);
	}
	ac->controls[i]->data = vlv;

	ac->store->timestamp = time(NULL);

	ac->store->contextId = ac->priv->next_free_id;
	ac->priv->next_free_id++;
	vlv->contextId = talloc_memdup(vlv, &ac->store->contextId, sizeof(uint32_t));
	vlv->ctxid_len = sizeof(uint32_t);
	vlv->vlv_result = 0;
	vlv->contentCount = ac->store->num_entries;
	if (target >= 0) {
		vlv->targetPosition = target + 1;
	} else if (vlv_details->type == 1) {
		vlv->targetPosition = ac->store->num_entries + 1;
	} else {
		vlv->targetPosition = 0;
	}
	return LDB_SUCCESS;
}