In this work, we derive the stellar initial mass function (IMF) from the superposition of mass distributions of dense cores, generated through gravoturbulent fragmentation of unstable clumps in molecular clouds (MCs) and growing through competitive accretion. MCs are formed by the turbulent cascade in the interstellar medium at scales L from 100 down to ∼0.1 pc. Their internal turbulence is essentially supersonic and creates clumps with a lognormal distribution of densities n. Our model is based on the assumption of a power-law relationship between clump mass and clump density: n∝mx, where x is a scale-free parameter. Gravitationally unstable clumps are assumed to undergo isothermal fragmentation and produce protostellar cores with a lognormal mass distribution, centred around the clump Jeans mass. Masses of individual cores are then assumed to grow further through competitive accretion until the rest of the gas within the clump is being exhausted. The observed IMF is best reproduced for a choice of x= 0.25, for a characteristic star formation time-scale of ∼5 Myr and for a low star formation efficiency of ∼10 per cent.