module cla_4bit(
    input [4:1] A,
    input [4:1] B,
    input cin,
    output [4:1] S,
    output cout
);

    wire [4:1] g; // generate
    wire [4:1] p; // propagate
    wire [4:0] c; // carries, c[0] is cin

    // Generate and propagate terms
    assign g = A & B;
    assign p = A ^ B;

    // Calculate carries
    assign c[0] = cin;
    assign c[1] = g[1] | (p[1] & c[0]);
    assign c[2] = g[2] | (p[2] & c[1]);
    assign c[3] = g[3] | (p[3] & c[2]);
    assign c[4] = g[4] | (p[4] & c[3]);

    // Calculate the sum
    assign S = p ^ c[4:1];

    // Carry-out is the last carry bit
    assign cout = c[4];

endmodule