// Hand-C reference for bench_compute_collatz.
//
// Same algorithm as examples/bench_compute_collatz.ailx — for each
// starting value in [1..N], count Collatz steps to reach 1, sum.
// Three sizes: 10k / 100k / 500k starting values.
//
// Data-dependent control flow (n % 2 branch) prevents LLVM from
// reducing this to closed form. The AILang/C wall-time ratio
// directly reflects integer-arithmetic + branch-prediction codegen
// quality.
//
// Build: clang -O2 -o compute_collatz compute_collatz.c
// Expected stdout (one int per line):
//   849666
//   10753840
//   62134795

#include <stdio.h>

static long collatz_steps(long n) {
    long steps = 0;
    while (n != 1) {
        if ((n % 2) == 0) {
            n = n / 2;
        } else {
            n = n * 3 + 1;
        }
        steps += 1;
    }
    return steps;
}

static long sum_steps(long n) {
    long total = 0;
    for (long i = n; i > 0; i--) {
        total += collatz_steps(i);
    }
    return total;
}

static void run_one(long n) {
    printf("%ld\n", sum_steps(n));
}

int main(void) {
    run_one(10000);
    run_one(100000);
    run_one(500000);
    return 0;
}
