Free energy of an inhomogeneous superconductor: A wave-function approach