//! returns: 0
//! Array repeat literals of more than 32 aggregate elements of 1, 2, 4, or 8 bytes fill whole words
//! from the bits of the item. The items are records, arrays, and an optional with padding, for
//! lengths around the word and chunk boundaries. Guard arrays check that the fill stays inside the
//! array.

/// Record of two 32-bit fields, 8 bytes.
record Pair: Copy {
    /// First field.
    a: u32,
    /// Second field.
    b: u32,
}

/// Record of a halfword and a byte with a padding byte, 4 bytes with alignment 2.
record Small: Copy {
    /// Halfword field.
    prefix: u16,
    /// Byte field.
    suffix: u8,
}

/// Record of two bytes, 2 bytes with alignment 1.
record Bytes: Copy {
    /// First byte.
    x: u8,
    /// Second byte.
    y: u8,
}

/// Record of a byte and a 32-bit field, 8 bytes with padding.
record Padded: Copy {
    /// Byte field.
    tag: u8,
    /// Word field.
    value: u32,
}

/// Check that a guard array still holds the value it was created with.
fn intact(guard: &[u8]) -> bool {
    for x in guard {
        if x <> 0x77 {
            return false;
        }
    }
    return true;
}

/// Fill `[Pair; N]` literals and check every element and the guards.
fn fillPairs(seed: u32) -> i32 {
    let before: [u8; 8] = [0x77; 8];
    let zero: [Pair; 33] = [Pair { a: 0, b: 0 }; 33];
    let run: [Pair; 40] = [Pair { a: seed, b: seed + 1 }; 40];
    let mixed: [Pair; 73] = [Pair { a: 0xFFFFFFFF, b: 5 }; 73];
    let after: [u8; 8] = [0x77; 8];
    for i in 0 .. 33 {
        if zero[i].a <> 0 or zero[i].b <> 0 {
            return 1;
        }
    }
    for i in 0 .. 40 {
        if run[i].a <> seed or run[i].b <> seed + 1 {
            return 2;
        }
    }
    for i in 0 .. 73 {
        if mixed[i].a <> 0xFFFFFFFF or mixed[i].b <> 5 {
            return 3;
        }
    }
    if not intact(&before[..]) or not intact(&after[..]) {
        return 4;
    }
    return 0;
}

/// Fill `[Small; N]` literals, whose items have a padding byte, and check the fields.
fn fillSmalls(seed: u32) -> i32 {
    let before: [u8; 8] = [0x77; 8];
    let a: [Small; 33] = [Small { prefix: 0xFFFF, suffix: 0 }; 33];
    let b: [Small; 75] = [Small { prefix: seed as u16, suffix: 0x80 }; 75];
    let after: [u8; 8] = [0x77; 8];
    for i in 0 .. 33 {
        if a[i].prefix <> 0xFFFF or a[i].suffix <> 0 {
            return 1;
        }
    }
    for i in 0 .. 75 {
        if b[i].prefix <> seed as u16 or b[i].suffix <> 0x80 {
            return 2;
        }
    }
    if not intact(&before[..]) or not intact(&after[..]) {
        return 3;
    }
    return 0;
}

/// Fill `[Bytes; N]` and `[[u8; 4]; N]` literals and check the elements.
fn fillBytes(seed: u32) -> i32 {
    let before: [u8; 8] = [0x77; 8];
    let a: [Bytes; 35] = [Bytes { x: seed as u8, y: 0x81 }; 35];
    let b: [[u8; 4]; 41] = [[1, 2, seed as u8, 4]; 41];
    let c: [[u8; 8]; 34] = [[9, 8, 7, 6, 5, 4, 3, seed as u8]; 34];
    let after: [u8; 8] = [0x77; 8];
    for i in 0 .. 35 {
        if a[i].x <> seed as u8 or a[i].y <> 0x81 {
            return 1;
        }
    }
    for i in 0 .. 41 {
        if b[i][0] <> 1 or b[i][1] <> 2 or b[i][2] <> seed as u8 or b[i][3] <> 4 {
            return 2;
        }
    }
    for i in 0 .. 34 {
        if c[i][0] <> 9 or c[i][3] <> 6 or c[i][7] <> seed as u8 {
            return 3;
        }
    }
    if not intact(&before[..]) or not intact(&after[..]) {
        return 4;
    }
    return 0;
}

/// Fill `[Padded; N]` and `[?u32; N]` literals, whose items have padding, and check the fields.
fn fillPadded(seed: u32) -> i32 {
    let before: [u8; 8] = [0x77; 8];
    let a: [Padded; 37] = [Padded { tag: 7, value: seed }; 37];
    let b: [?u32; 36] = [seed; 36];
    let c: [?u32; 36] = [nil; 36];
    let after: [u8; 8] = [0x77; 8];
    for i in 0 .. 37 {
        if a[i].tag <> 7 or a[i].value <> seed {
            return 1;
        }
    }
    for i in 0 .. 36 {
        let v = b[i] else {
            return 2;
        };
        if v <> seed {
            return 3;
        }
        if let _ = c[i] {
            return 4;
        }
    }
    if not intact(&before[..]) or not intact(&after[..]) {
        return 5;
    }
    return 0;
}

@default fn main() -> i32 {
    let mut r: i32 = 0;
    for seed in 0 .. 4 {
        set r = fillPairs(seed * 3 + 1);
        if r <> 0 {
            return 10 + r;
        }
        set r = fillSmalls(seed * 5 + 1);
        if r <> 0 {
            return 20 + r;
        }
        set r = fillBytes(seed * 7 + 1);
        if r <> 0 {
            return 30 + r;
        }
        set r = fillPadded(seed * 11 + 1);
        if r <> 0 {
            return 40 + r;
        }
    }
    return 0;
}
