A computer code for gain optimization of high-gain free-electron lasers (FELs) is described. The electron motion is along precalculated period-averaged trajectories, and the finite-emittance electron beam is represented by a set of thin partial beams. The radiation field amplitudes are calculated at these thin beams only. The system of linear integral equations for these field amplitudes and the Fourier harmonics of the current of each thin beam is solved numerically. The code is aimed for design optimization of high-gain short-wavelength FELs with nonideal magnetic systems (breaks between undulators with quadrupoles and magnetic bunchers; field and steering errors). Both self-amplified spontaneous emission (SASE) and external input signal options can be treated. A typical run for a UV FEL, several gain lengths long, takes only one minute on a Pentium II personal computer (333 MHz) which makes it possible to run the code in optimization loops. Results for the Advanced Photon Source FEL project are presented