diff --git a/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java b/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java index aacb58a46..d8d34e7e8 100644 --- a/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java +++ b/core/src/main/java/org/teavm/backend/c/generate/CodeGenerationVisitor.java @@ -1245,13 +1245,15 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor { int statementId = registerIdentifiedStatement(statement); - writer.print("while ("); + writer.println("while (1) {").indent(); + + // This can't be moved to 'while', since C11 standard allows removing infinite loops + // See https://www.iso-9899.info/n1570.html#6.8.5p6 if (statement.getCondition() != null) { + writer.print("if (!"); statement.getCondition().acceptVisitor(this); - } else { - writer.print("1"); + writer.println(") break;"); } - writer.println(") {").indent(); boolean oldEnd = end; for (Statement part : statement.getBody()) {