  We compute next-to-next-to-leading order corrections to the semileptonic
decay rate of $B$ mesons for arbitrary values of the final-state quark mass.
For the contribution with one massive quark in the final state, we extend the
literature result and obtain analytic expressions in terms of iterated
integrals. For the complete contribution, which also includes contributions
with three massive quarks in the final state, we present a semi-analytic
method, which leads to a precise approximation formula for the decay rate. Our
results agree with the expansions available for $b\to c\ell \bar{\nu}_\ell$ and
$b\to u\ell \bar{\nu}_\ell$ in the literature. The main emphasize of this paper
is on the technical aspects of the calculation which are useful for a wider
range of applications.
