//! returns: 0 /// Carry changing and identical values through loop and conditional joins. fn run(limit: u32, choose: bool) -> u32 { let mut total: u32 = 3; let mut carry: u32 = 5; let mut stable: u32 = 11; for index in 0..limit { if choose and index % 2 == 0 { set carry += 7; set stable = 11; } else { set carry += 13; set stable = 11; } match index % 3 { case 0 => set total += carry, case 1 => set total += stable, else => set total += carry + stable, } } return total + carry + stable; } /// Merge identical values beside a changing loop counter. fn unchanged(limit: u32) -> u32 { let mut first: u32 = 7; let mut second: u32 = 11; let mut count: u32 = 0; for index in 0..limit { if index % 2 == 0 { set first = 7; } else { set second = 11; } set count += 1; } return first + second + count; } /// Seal nested loop headers with stable and changing carried values. fn nested(limit: u32) -> u32 { let mut count: u32 = 0; let mut stable: u32 = 23; for outer in 0..limit { for inner in 0..outer + 1 { set count += 1; if inner % 2 == 0 { set stable = 23; } } } return count + stable; } /// Compare both branch paths over empty and repeated loop iterations. @default fn main() -> u32 { for limit in 0..32 { assert unchanged(limit) == 18 + limit; assert nested(limit) == 23 + limit * (limit + 1) / 2; for choose in [false, true] { let mut total: u32 = 3; let mut carry: u32 = 5; for index in 0..limit { set carry += 7 if choose and index % 2 == 0 else 13; if index % 3 <> 1 { set total += carry; } if index % 3 <> 0 { set total += 11; } } assert run(limit, choose) == total + carry + 11; assert run(limit, choose) == total + carry + 11; } } return 0; }