//! returns: 0
//! Test that loop-carried mutable variables work correctly.
//! This is the fundamental pattern used by sealBlock in the lowerer.
//! A mutable variable defined before a loop, modified inside the loop body,
//! must have its updated value propagated to the next iteration.

@default fn main() -> i32 {
    // Simple loop with mutable variable.
    let mut sum: i32 = 0;
    let mut i: i32 = 0;
    while i < 5 {
        set sum += i;
        set i += 1;
    }
    // sum = 0 + 1 + 2 + 3 + 4 = 10
    assert sum == 10;

    // For loop with mutable variable.
    let mut count: i32 = 0;
    for j in 0..4 {
        set count += 1;
    }
    assert count == 4;

    // Nested pattern: mutable var with function call in loop.
    let mut total: i32 = 0;
    for k in 0..3 {
        set total += addOne(k);
    }
    // total = 1 + 2 + 3 = 6
    assert total == 6;

    return 0;
}

fn addOne(x: i32) -> i32 {
    return x + 1;
}
