The number of idempotent endomorphisms of a finite group Recall that a endomorphism $f:G\longrightarrow G$ of a group $G$ is called idempotent if $f\circ f=f$.   
If $G$ is a finite group of order $n$, then how many idempotent endomorphisms $f:G\longrightarrow G$ are there? Is there a good upper bound for the set of all  idempotent endomorphisms of $G$?
Thanks in advance.

The number $e(G)$ of idempotent endomorphisms over $G$ is the number of pairs of subgroups $H,K$ such that $G$ is the semidirect product of $H$ and $K$.
A theorem of Borovik, Pyber and Shalev (Corollary 1.6) shows that the number of subgroups of a group $G$ of order $n=\lvert G\rvert$ is bounded by $n^{(\frac{1}{4}+o(1)) \log_2(n)}$. Squaring gives an upper bound on the total number of pairs $(H,K)$ and hence on $e(G)$:
$$e(G)\leq n^{(\frac{1}{2}+o(1)) \log_2(n)}\text{ for all $G$ with $n=|G|$}.$$
This upper bound is essentially matched by elementary abelian $2$-groups, which satisfy
$$e((\mathbb Z/2\mathbb Z)^d)\geq 2^{\tfrac 1 2 d^2} = n^{\tfrac 1 2 \log_2(n)}\text{ with $n=2^d$}.$$
Calculations for this last claim are below.

The number $ob(\mathbb F_q^d)$ of ordered bases of a finite vector space $\mathbb F_q^d$ is
$$ob(\mathbb F_q^d) = q^{\binom d 2}(q^d-1)(q^{d-1}-1)\cdots (q-1).$$
The number of pairs $(H,K)$ of complementary subspaces with $\dim H=d-k$ and $\dim K=k$ is therefore given by
$$\frac{ob((\mathbb Z/q\mathbb Z)^d)}{ob((\mathbb Z/q\mathbb Z)^{d-k})ob((\mathbb Z/q\mathbb Z)^k)}=q^{k(d-k)}
\frac{q^d-1}{q^k-1}
\frac{q^{d-1}-1}{q^{k-1}-1}
\cdots
\frac{q^{d-k+1}-1}{q^1-1}
\geq q^{2k(d-k)}.
$$
Setting $q=2$ and $k=d/2$ we get
$$e((\mathbb Z/2\mathbb Z)^{d})\geq 2^{d^2/2}.$$
