Zsh worked well if the prompt is in a single line. The bug comes from multi-line prompt, i.e., when emacs shell used with multi-line prompt, unexpected lines are generated.