//! returns: 0
//! Test a recursive function that receives stack arguments.
//!
//! Each frame must read its own incoming stack arguments after the nested call
//! returns, so nested calls cannot reuse the caller's argument area.

record Pair { a: u32, b: u32 }
union Err {
    Bad,
}

/// Descend `n` levels, adding to the arguments at each level. Each level adds
/// its own `a` and `g` after the nested call.
fn descend(n: u32, a: u32, b: u32, c: u32, d: u32, e: u32, f: u32, g: u32) -> Pair throws (Err) {
    if n == 0 {
        return Pair { a: a + b + c + d, b: e + f + g };
    }
    let r = try descend(n - 1, a + 1, b + 2, c + 3, d + 4, e + 5, f + 6, g + 7);
    return Pair { a: r.a + a, b: r.b + g };
}

@default fn main() -> i32 {
    let r = try descend(10, 1, 2, 3, 4, 5, 6, 7) catch {
        return 1;
    };
    // The base case returns 110 and 198. Levels 0 to 9 add `a = 1 + k` and
    // `g = 7 + 7 * k`, read after the nested call returns.
    assert r.a == 110 + 55;
    assert r.b == 198 + 385;
    return 0;
}
