/*
 * Scenario 1: C implementation of is_prime
 * =========================================
 * Compile as shared library:
 *   gcc -O2 -fPIC -shared -o libprime.so prime.c
 * 
 * Or with debug symbols for profiling:
 *   gcc -O2 -g -fPIC -shared -o libprime.so prime.c
 */

#include <stdint.h>

/* Check if n is prime using trial division */
int is_prime(int64_t n) {
    if (n < 2) return 0;
    if (n == 2) return 1;
    if (n % 2 == 0) return 0;
    
    int64_t i = 3;
    while (i * i <= n) {
        if (n % i == 0) return 0;
        i += 2;
    }
    return 1;
}

/* Count primes up to limit - can also be called from Python */
int64_t count_primes(int64_t limit) {
    int64_t count = 0;
    for (int64_t n = 2; n <= limit; n++) {
        if (is_prime(n)) {
            count++;
        }
    }
    return count;
}
