//! returns: 0

/// External declaration retained without a lowered body.
fn externalValue(value: u64) -> u64;

/// A void leaf function has an initialized descriptor and return block.
fn empty() {}

/// Input for method calls with branch-local values.
record Input: Copy {
    /// Full-width input retained by the receiver.
    value: u64,
}

/// Construct an aggregate through a regular function return.
fn makeInput(value: u64) -> Input {
    return Input { value };
}

/// Return an aggregate after nested function calls from a method.
fn (input: &Input) advanced() -> Input {
    return makeInput(advance(input.value));
}

/// Lower a method with a receiver, explicit parameter, and nested calls.
fn (input: &Input) combine(selected: bool) -> u64 {
    return combine(input.value, selected);
}

/// Leaf computation used by a non-leaf function.
fn advance(value: u64) -> u64 {
    return value + 7;
}

/// Retain values across calls and merge their arithmetic results.
fn combine(value: u64, selected: bool) -> u64 {
    let first = advance(value);
    let second = advance(first);
    let third = advance(second);
    let mut result = first * 3 + second * 5;
    if selected {
        set result += third * 11;
    } else {
        set result += third * 13;
    }
    return result;
}

/// Check call preservation, leaf state, and repeated instruction emission.
@default fn main() -> u32 {
    for index in 0..32 {
        empty();
        let value = (index as u64) + 0x100000000;
        let base = (value + 7) * 3 + (value + 14) * 5;
        assert combine(value, true) == base + (value + 21) * 11;
        assert combine(value, false) == base + (value + 21) * 13;
        let input = Input { value };
        let advanced = input.advanced();
        assert advanced.value == value + 7;
        assert input.combine(true) == base + (value + 21) * 11;
        assert input.combine(false) == base + (value + 21) * 13;
    }
    return 0;
}
