No person who has been discharged by order of a court or judge upon a writ of habeas corpus shall be again imprisoned, restrained, or kept in custody for the same cause, unless he be afterward prosecuted for the same offense; nor unless by the legal order or process of the court wherein he is bound by recognizance to appear. The following shall not be deemed to be the same cause:
(1) If after a discharge for a defect of proof, or on any material defect in the commitment in a criminal case, such person should be again arrested on sufficient proof, and committed by legal process for the same offense;(2) If in a civil suit such person has been discharged for any illegality in the judgment or process, and is afterward imprisoned by legal process for the same cause of action;(3) Generally, whenever the discharge has been ordered on account of the nonobservance of any of the forms required by law, such person may be a second time imprisoned, if the cause be legal, and the forms required by law observed. CCrimP 1877, § 677; CL 1887, § 7845; RCCrimP 1903, § 777; RC 1919, § 4983; SDC 1939 & Supp 1960, § 37.5506.