Problem 216

Count primes of form t(n)=2n^2-1 for 2<=n<=50e6.

Answer5437849
Output5437849
StatusPASS
Native helperno
Runtime1970 ms
Peak memory118976 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log log n)
Space complexityO(n)O(n)
ApproachFlow solutionSieve of Eratosthenes
VerdictSuboptimal

Flow source

# Project Euler 216
# Count primes of form t(n)=2n^2-1 for 2<=n<=50e6.

import euler.nt { isqrt }

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
}

function modpow(base: i64, exp: i64, mod: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = base % mod
    let mut e: i64 = exp
    while e > 0 {
        if e % 2 == 1 { r = (r * b) % mod }
        b = (b * b) % mod
        e = e / 2
    }
    return r
}

function tonelli(a0: i64, p: i64) -> i64 {
    let a: i64 = a0 % p
    if a == 0 { return 0 }
    if modpow(a, (p - 1) / 2, p) != 1 { return 0 }
    if p % 4 == 3 {
        return modpow(a, (p + 1) / 4, p)
    }
    let mut q: i64 = p - 1
    let mut s: i32 = 0
    while q % 2 == 0 {
        q = q / 2
        s = s + 1
    }
    let mut z: i64 = 2
    while modpow(z, (p - 1) / 2, p) != p - 1 {
        z = z + 1
    }
    let mut m: i32 = s
    let mut c: i64 = modpow(z, q, p)
    let mut t: i64 = modpow(a, q, p)
    let mut r: i64 = modpow(a, (q + 1) / 2, p)
    while t != 1 {
        let mut i: i32 = 1
        let mut t2: i64 = (t * t) % p
        while t2 != 1 {
            t2 = (t2 * t2) % p
            i = i + 1
        }
        let mut e: i32 = m - i - 1
        let mut b: i64 = c
        let mut j: i32 = 0
        while j < e {
            b = (b * b) % p
            j = j + 1
        }
        r = (r * b) % p
        t = (((t * b) % p) * b) % p
        c = (b * b) % p
        m = i
    }
    return r
}

function main() -> i32 {
    let LIMIT: i64 = 50000000
    let max_value: i64 = 2 * LIMIT * LIMIT - 1
    let plim: i64 = isqrt(max_value) + 1
    let sieve: ptr<i8> = calloc(plim + 1, 1)
    if sieve == null { return 1 }
    let mut i: i64 = 0
    while i <= plim {
        sieve[i] = 1
        i = i + 1
    }
    sieve[0] = 0
    sieve[1] = 0
    i = 2
    while i * i <= plim {
        if sieve[i] == 1 {
            let mut j: i64 = i * i
            while j <= plim {
                sieve[j] = 0
                j = j + i
            }
        }
        i = i + 1
    }
    let composite: ptr<i8> = calloc(LIMIT + 1, 1)
    if composite == null { return 1 }

    i = 3
    while i <= plim {
        if sieve[i] == 1 {
            let rem: i64 = i & 7
            if rem == 1 || rem == 7 {
                let inv2: i64 = (i + 1) / 2
                let mut root: i64 = 0
                if rem == 7 {
                    root = (modpow(2, (i + 1) / 4, i) * inv2) % i
                } else {
                    root = tonelli(inv2, i)
                }
                if root != 0 || rem == 7 {
                    # mark root, -root progressions
                    let mut start: i64 = root
                    if start < 2 {
                        start = start + ((2 - start + i - 1) / i) * i
                    }
                    let mut j: i64 = start
                    while j <= LIMIT {
                        composite[j] = 1
                        j = j + i
                    }
                    let other: i64 = (i - root) % i
                    if other != root {
                        start = other
                        if start < 2 {
                            start = start + ((2 - start + i - 1) / i) * i
                        }
                        j = start
                        while j <= LIMIT {
                            composite[j] = 1
                            j = j + i
                        }
                    }
                    let maybe: i64 = isqrt(inv2)
                    if maybe * maybe == inv2 && maybe <= LIMIT {
                        composite[maybe] = 0
                    }
                }
            }
        }
        i = i + 1
    }
    let mut count: i64 = 0
    i = 2
    while i <= LIMIT {
        if composite[i] == 0 { count = count + 1 }
        i = i + 1
    }
    printf("%lld\n", count)
    free(sieve); free(composite)
    return 0
}

Generated C

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Flow runtime helpers */
typedef struct flow_temp_node { struct flow_temp_node* next; } flow_temp_node;
static flow_temp_node* flow_temp_head = NULL;
static int flow_temp_atexit_set = 0;
__attribute__((unused)) static void flow_temp_free_all(void) {
    while (flow_temp_head) {
        flow_temp_node* n = flow_temp_head;
        flow_temp_head = n->next;
        free(n);
    }
}
__attribute__((unused)) static void* flow_temp_alloc(size_t nbytes) {
    flow_temp_node* node = (flow_temp_node*)malloc(sizeof(flow_temp_node) + nbytes);
    if (!node) return NULL;
    node->next = flow_temp_head;
    flow_temp_head = node;
    if (!flow_temp_atexit_set) {
        flow_temp_atexit_set = 1;
        atexit(flow_temp_free_all);
    }
    return (void*)(node + 1);
}
#ifndef FLOW_DIAG
#define FLOW_DIAG(msg) fprintf(stderr, "%s", (msg))
#endif
#ifndef FLOW_LOG
#define FLOW_LOG(fmt, ...) printf(fmt, __VA_ARGS__)
#endif
#ifndef FLOW_LOG_EMPTY
#define FLOW_LOG_EMPTY(fmt) printf(fmt)
#endif
static char* flow_strcat(const char* a, const char* b) {
    size_t la = strlen(a ? a : ""), lb = strlen(b ? b : "");
    char* r = (char*)flow_temp_alloc(la + lb + 1);
    if (!r) return NULL;
    if (la) memcpy(r, a, la);
    if (lb) memcpy(r + la, b, lb);
    r[la + lb] = '\0';
    return r;
}

#define __flow_in_arr(arr, val) __extension__ ({ \
    int _found = 0; \
    size_t _n = sizeof(arr)/sizeof((arr)[0]); \
    for (size_t _i = 0; _i < _n; _i++) { \
        if ((arr)[_i] == (val)) { _found = 1; break; } \
    } _found; })

/* Unified fault handler (MISRA #279) — override with -DFLOW_FAULT_HANDLER=fn */
#ifndef FLOW_FAULT_HANDLER
__attribute__((unused)) static inline void flow_fault_handler(const char* msg) {
    fprintf(stderr, "flow: %s\n", msg ? msg : "fault");
    abort();
#if defined(__GNUC__) || defined(__clang__)
    __builtin_unreachable();
#endif
}
#else
#define flow_fault_handler FLOW_FAULT_HANDLER
#endif
#define flow_div_by_zero_handler() flow_fault_handler("division by zero")
#define flow_shift_ub_handler() flow_fault_handler("invalid shift (amount out of range or left-shift of negative)")

#ifndef FLOW_CHECKED_DIV
#define FLOW_CHECKED_DIV(L, R) (((R) != 0) ? ((L) / (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_MOD
#define FLOW_CHECKED_MOD(L, R) (((R) != 0) ? ((L) % (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHL
#define FLOW_CHECKED_SHL(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull)) && ((L) >= 0)) ? ((L) << (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHR
#define FLOW_CHECKED_SHR(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull))) ? ((L) >> (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif

#include <math.h>

void* _ui_state = NULL;

static inline float i32_to_f32(int32_t v) { return (float)v; }

/* Host stub for @gpu kernels (device codegen replaces this). */
static inline int32_t gpu_thread_id(void) { return 0; }

int64_t gcd_i64_i64(int64_t a0, int64_t b0);
int64_t lcm_i64_i64(int64_t a, int64_t b);
int64_t isqrt_i64(int64_t n);
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
bool is_prime_i64(int64_t n);
int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
int64_t tonelli_i64_i64(int64_t a0, int64_t p);
int32_t main(void);

int64_t gcd_i64_i64(int64_t a0, int64_t b0) {
    int64_t a = a0;
    int64_t b = b0;
    while (b != 0) {
        int64_t t = FLOW_CHECKED_MOD((a), (b));
        a = b;
        b = t;
    }
    return a;
}

int64_t lcm_i64_i64(int64_t a, int64_t b) {
    if ((a == 0 || b == 0)) {
        return 0;
    }
    return (FLOW_CHECKED_DIV((a), (gcd_i64_i64(a, b))) * b);
}

int64_t isqrt_i64(int64_t n) {
    if (n < 2) {
        return n;
    }
    int64_t x = n;
    int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
    while (y < x) {
        x = y;
        y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
    }
    return x;
}

int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod) {
    int64_t a = FLOW_CHECKED_MOD((a0), (mod));
    int64_t b = FLOW_CHECKED_MOD((b0), (mod));
    int64_t result = 0;
    while (b > 0) {
        if (FLOW_CHECKED_MOD((b), (2)) == 1) {
            result = FLOW_CHECKED_MOD(((result + a)), (mod));
        }
        a = FLOW_CHECKED_MOD(((a * 2)), (mod));
        b = FLOW_CHECKED_DIV((b), (2));
    }
    return result;
}

int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
    if (mod == 1) {
        return 0;
    }
    int64_t result = 1;
    int64_t b = FLOW_CHECKED_MOD((base), (mod));
    int64_t e = exp;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            result = mulmod_i64_i64_i64(result, b, mod);
        }
        b = mulmod_i64_i64_i64(b, b, mod);
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return result;
}

bool is_prime_i64(int64_t n) {
    if (n < 2) {
        return 0;
    }
    if (n < 4) {
        return 1;
    }
    if ((FLOW_CHECKED_MOD((n), (2)) == 0 || FLOW_CHECKED_MOD((n), (3)) == 0)) {
        return 0;
    }
    int64_t i = 5;
    while ((i * i) <= n) {
        if ((FLOW_CHECKED_MOD((n), (i)) == 0 || FLOW_CHECKED_MOD((n), ((i + 2))) == 0)) {
            return 0;
        }
        i = (i + 6);
    }
    return 1;
}



int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
    int64_t r = 1;
    int64_t b = FLOW_CHECKED_MOD((base), (mod));
    int64_t e = exp;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            r = FLOW_CHECKED_MOD(((r * b)), (mod));
        }
        b = FLOW_CHECKED_MOD(((b * b)), (mod));
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return r;
}

int64_t tonelli_i64_i64(int64_t a0, int64_t p) {
    int64_t a = FLOW_CHECKED_MOD((a0), (p));
    if (a == 0) {
        return 0;
    }
    if (modpow_i64_i64_i64(a, FLOW_CHECKED_DIV(((p - 1)), (2)), p) != 1) {
        return 0;
    }
    if (FLOW_CHECKED_MOD((p), (4)) == 3) {
        return modpow_i64_i64_i64(a, FLOW_CHECKED_DIV(((p + 1)), (4)), p);
    }
    int64_t q = (p - 1);
    int32_t s = 0;
    while (FLOW_CHECKED_MOD((q), (2)) == 0) {
        q = FLOW_CHECKED_DIV((q), (2));
        s = (s + 1);
    }
    int64_t z = 2;
    while (modpow_i64_i64_i64(z, FLOW_CHECKED_DIV(((p - 1)), (2)), p) != (p - 1)) {
        z = (z + 1);
    }
    int32_t m = s;
    int64_t c = modpow_i64_i64_i64(z, q, p);
    int64_t t = modpow_i64_i64_i64(a, q, p);
    int64_t r = modpow_i64_i64_i64(a, FLOW_CHECKED_DIV(((q + 1)), (2)), p);
    while (t != 1) {
        int32_t i = 1;
        int64_t t2 = FLOW_CHECKED_MOD(((t * t)), (p));
        while (t2 != 1) {
            t2 = FLOW_CHECKED_MOD(((t2 * t2)), (p));
            i = (i + 1);
        }
        int32_t e = ((m - i) - 1);
        int64_t b = c;
        int32_t j = 0;
        while (j < e) {
            b = FLOW_CHECKED_MOD(((b * b)), (p));
            j = (j + 1);
        }
        r = FLOW_CHECKED_MOD(((r * b)), (p));
        t = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((t * b)), (p)) * b)), (p));
        c = FLOW_CHECKED_MOD(((b * b)), (p));
        m = i;
    }
    return r;
}

int32_t main(void) {
    int64_t LIMIT = 50000000;
    int64_t max_value = (((2 * LIMIT) * LIMIT) - 1);
    int64_t plim = (isqrt_i64(max_value) + 1);
    int8_t* sieve = (int8_t*)(calloc((plim + 1), 1));
    if (sieve == NULL) {
        return 1;
    }
    int64_t i = 0;
    while (i <= plim) {
        sieve[i] = 1;
        i = (i + 1);
    }
    sieve[0] = 0;
    sieve[1] = 0;
    i = 2;
    while ((i * i) <= plim) {
        if (sieve[i] == 1) {
            int64_t j = (i * i);
            while (j <= plim) {
                sieve[j] = 0;
                j = (j + i);
            }
        }
        i = (i + 1);
    }
    int8_t* composite = (int8_t*)(calloc((LIMIT + 1), 1));
    if (composite == NULL) {
        return 1;
    }
    i = 3;
    while (i <= plim) {
        if (sieve[i] == 1) {
            int64_t rem = (i & 7);
            if ((rem == 1 || rem == 7)) {
                int64_t inv2 = FLOW_CHECKED_DIV(((i + 1)), (2));
                int64_t root = 0;
                if (rem == 7) {
                    root = FLOW_CHECKED_MOD(((modpow_i64_i64_i64(2, FLOW_CHECKED_DIV(((i + 1)), (4)), i) * inv2)), (i));
                } else {
                    root = tonelli_i64_i64(inv2, i);
                }
                if ((root != 0 || rem == 7)) {
                    int64_t start = root;
                    if (start < 2) {
                        start = (start + (FLOW_CHECKED_DIV(((((2 - start) + i) - 1)), (i)) * i));
                    }
                    int64_t j = start;
                    while (j <= LIMIT) {
                        composite[j] = 1;
                        j = (j + i);
                    }
                    int64_t other = FLOW_CHECKED_MOD(((i - root)), (i));
                    if (other != root) {
                        start = other;
                        if (start < 2) {
                            start = (start + (FLOW_CHECKED_DIV(((((2 - start) + i) - 1)), (i)) * i));
                        }
                        j = start;
                        while (j <= LIMIT) {
                            composite[j] = 1;
                            j = (j + i);
                        }
                    }
                    int64_t maybe = isqrt_i64(inv2);
                    if (((maybe * maybe) == inv2 && maybe <= LIMIT)) {
                        composite[maybe] = 0;
                    }
                }
            }
        }
        i = (i + 1);
    }
    int64_t count = 0;
    i = 2;
    while (i <= LIMIT) {
        if (composite[i] == 0) {
            count = (count + 1);
        }
        i = (i + 1);
    }
    printf("%lld\n", count);
    free(sieve);
    free(composite);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func @gcd(%arg0: i64, %arg1: i64) -> i64 {
    %0 = llvm.mlir.constant(1 : i64) : i64
    %1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %1 : i64, !llvm.ptr
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %3 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %4 = llvm.load %3 : !llvm.ptr -> i64
    %5 = arith.constant 0 : i32
    %7 = arith.extsi %5 : i32 to i64
    %6 = arith.cmpi ne, %4, %7 : i64
    cf.cond_br %6, ^bb1, ^bb2
    ^bb1:
      %8 = llvm.load %1 : !llvm.ptr -> i64
      %9 = llvm.load %3 : !llvm.ptr -> i64
      %10 = arith.remsi %8, %9 : i64
      %11 = llvm.load %3 : !llvm.ptr -> i64
      llvm.store %11, %1 : i64, !llvm.ptr
      llvm.store %10, %3 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %12 = llvm.load %1 : !llvm.ptr -> i64
    func.return %12 : i64
  }
  func.func @lcm(%arg0: i64, %arg1: i64) -> i64 {
    %13 = arith.constant 0 : i32
    %15 = arith.extsi %13 : i32 to i64
    %14 = arith.cmpi eq, %arg0, %15 : i64
    %16 = scf.if %14 -> (i1) {
      %17 = arith.constant true
      scf.yield %17 : i1
    } else {
      %18 = arith.constant 0 : i32
      %20 = arith.extsi %18 : i32 to i64
      %19 = arith.cmpi eq, %arg1, %20 : i64
      scf.yield %19 : i1
    }
    cf.cond_br %16, ^bb3, ^bb4
    ^bb3:
      %21 = arith.constant 0 : i32
      %22 = arith.extsi %21 : i32 to i64
      func.return %22 : i64
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %23 = func.call @gcd(%arg0, %arg1) : (i64, i64) -> i64
    %24 = arith.divsi %arg0, %23 : i64
    %25 = arith.muli %24, %arg1 : i64
    func.return %25 : i64
  }
  func.func @isqrt(%arg0: i64) -> i64 {
    %26 = arith.constant 2 : i32
    %28 = arith.extsi %26 : i32 to i64
    %27 = arith.cmpi slt, %arg0, %28 : i64
    cf.cond_br %27, ^bb6, ^bb7
    ^bb6:
      func.return %arg0 : i64
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %29 = llvm.mlir.constant(1 : i64) : i64
    %30 = llvm.alloca %29 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %30 : i64, !llvm.ptr
    %31 = llvm.load %30 : !llvm.ptr -> i64
    %32 = arith.constant 1 : i32
    %34 = arith.extsi %32 : i32 to i64
    %33 = arith.addi %31, %34 : i64
    %35 = arith.constant 2 : i32
    %37 = arith.extsi %35 : i32 to i64
    %36 = arith.divsi %33, %37 : i64
    %38 = llvm.mlir.constant(1 : i64) : i64
    %39 = llvm.alloca %38 x i64 : (i64) -> !llvm.ptr
    llvm.store %36, %39 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %40 = llvm.load %39 : !llvm.ptr -> i64
    %41 = llvm.load %30 : !llvm.ptr -> i64
    %42 = arith.cmpi slt, %40, %41 : i64
    cf.cond_br %42, ^bb10, ^bb11
    ^bb10:
      %43 = llvm.load %39 : !llvm.ptr -> i64
      llvm.store %43, %30 : i64, !llvm.ptr
      %44 = llvm.load %30 : !llvm.ptr -> i64
      %45 = llvm.load %30 : !llvm.ptr -> i64
      %46 = arith.divsi %arg0, %45 : i64
      %47 = arith.addi %44, %46 : i64
      %48 = arith.constant 2 : i32
      %50 = arith.extsi %48 : i32 to i64
      %49 = arith.divsi %47, %50 : i64
      llvm.store %49, %39 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %51 = llvm.load %30 : !llvm.ptr -> i64
    func.return %51 : i64
  }
  func.func @mulmod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %52 = arith.remsi %arg0, %arg2 : i64
    %53 = llvm.mlir.constant(1 : i64) : i64
    %54 = llvm.alloca %53 x i64 : (i64) -> !llvm.ptr
    llvm.store %52, %54 : i64, !llvm.ptr
    %55 = arith.remsi %arg1, %arg2 : i64
    %56 = llvm.mlir.constant(1 : i64) : i64
    %57 = llvm.alloca %56 x i64 : (i64) -> !llvm.ptr
    llvm.store %55, %57 : i64, !llvm.ptr
    %58 = arith.constant 0 : i32
    %59 = arith.extsi %58 : i32 to i64
    %60 = llvm.mlir.constant(1 : i64) : i64
    %61 = llvm.alloca %60 x i64 : (i64) -> !llvm.ptr
    llvm.store %59, %61 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %62 = llvm.load %57 : !llvm.ptr -> i64
    %63 = arith.constant 0 : i32
    %65 = arith.extsi %63 : i32 to i64
    %64 = arith.cmpi sgt, %62, %65 : i64
    cf.cond_br %64, ^bb13, ^bb14
    ^bb13:
      %66 = llvm.load %57 : !llvm.ptr -> i64
      %67 = arith.constant 2 : i32
      %69 = arith.extsi %67 : i32 to i64
      %68 = arith.remsi %66, %69 : i64
      %70 = arith.constant 1 : i32
      %72 = arith.extsi %70 : i32 to i64
      %71 = arith.cmpi eq, %68, %72 : i64
      cf.cond_br %71, ^bb15, ^bb16
      ^bb15:
        %73 = llvm.load %61 : !llvm.ptr -> i64
        %74 = llvm.load %54 : !llvm.ptr -> i64
        %75 = arith.addi %73, %74 : i64
        %76 = arith.remsi %75, %arg2 : i64
        llvm.store %76, %61 : i64, !llvm.ptr
        cf.br ^bb17
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %77 = llvm.load %54 : !llvm.ptr -> i64
      %78 = arith.constant 2 : i32
      %80 = arith.extsi %78 : i32 to i64
      %79 = arith.muli %77, %80 : i64
      %81 = arith.remsi %79, %arg2 : i64
      llvm.store %81, %54 : i64, !llvm.ptr
      %82 = llvm.load %57 : !llvm.ptr -> i64
      %83 = arith.constant 2 : i32
      %85 = arith.extsi %83 : i32 to i64
      %84 = arith.divsi %82, %85 : i64
      llvm.store %84, %57 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %86 = llvm.load %61 : !llvm.ptr -> i64
    func.return %86 : i64
  }
  func.func @mod_pow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %87 = arith.constant 1 : i32
    %89 = arith.extsi %87 : i32 to i64
    %88 = arith.cmpi eq, %arg2, %89 : i64
    cf.cond_br %88, ^bb18, ^bb19
    ^bb18:
      %90 = arith.constant 0 : i32
      %91 = arith.extsi %90 : i32 to i64
      func.return %91 : i64
    ^bb19:
      cf.br ^bb20
    ^bb20:
    %92 = arith.constant 1 : i32
    %93 = arith.extsi %92 : i32 to i64
    %94 = llvm.mlir.constant(1 : i64) : i64
    %95 = llvm.alloca %94 x i64 : (i64) -> !llvm.ptr
    llvm.store %93, %95 : i64, !llvm.ptr
    %96 = arith.remsi %arg0, %arg2 : i64
    %97 = llvm.mlir.constant(1 : i64) : i64
    %98 = llvm.alloca %97 x i64 : (i64) -> !llvm.ptr
    llvm.store %96, %98 : i64, !llvm.ptr
    %99 = llvm.mlir.constant(1 : i64) : i64
    %100 = llvm.alloca %99 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %100 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %101 = llvm.load %100 : !llvm.ptr -> i64
    %102 = arith.constant 0 : i32
    %104 = arith.extsi %102 : i32 to i64
    %103 = arith.cmpi sgt, %101, %104 : i64
    cf.cond_br %103, ^bb22, ^bb23
    ^bb22:
      %105 = llvm.load %100 : !llvm.ptr -> i64
      %106 = arith.constant 2 : i32
      %108 = arith.extsi %106 : i32 to i64
      %107 = arith.remsi %105, %108 : i64
      %109 = arith.constant 1 : i32
      %111 = arith.extsi %109 : i32 to i64
      %110 = arith.cmpi eq, %107, %111 : i64
      cf.cond_br %110, ^bb24, ^bb25
      ^bb24:
        %113 = llvm.load %95 : !llvm.ptr -> i64
        %114 = llvm.load %98 : !llvm.ptr -> i64
        %112 = func.call @mulmod(%113, %114, %arg2) : (i64, i64, i64) -> i64
        llvm.store %112, %95 : i64, !llvm.ptr
        cf.br ^bb26
      ^bb25:
        cf.br ^bb26
      ^bb26:
      %116 = llvm.load %98 : !llvm.ptr -> i64
      %117 = llvm.load %98 : !llvm.ptr -> i64
      %115 = func.call @mulmod(%116, %117, %arg2) : (i64, i64, i64) -> i64
      llvm.store %115, %98 : i64, !llvm.ptr
      %118 = llvm.load %100 : !llvm.ptr -> i64
      %119 = arith.constant 2 : i32
      %121 = arith.extsi %119 : i32 to i64
      %120 = arith.divsi %118, %121 : i64
      llvm.store %120, %100 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %122 = llvm.load %95 : !llvm.ptr -> i64
    func.return %122 : i64
  }
  func.func @is_prime(%arg0: i64) -> i1 {
    %123 = arith.constant 2 : i32
    %125 = arith.extsi %123 : i32 to i64
    %124 = arith.cmpi slt, %arg0, %125 : i64
    cf.cond_br %124, ^bb27, ^bb28
    ^bb27:
      %126 = arith.constant 0 : i1
      func.return %126 : i1
    ^bb28:
      cf.br ^bb29
    ^bb29:
    %127 = arith.constant 4 : i32
    %129 = arith.extsi %127 : i32 to i64
    %128 = arith.cmpi slt, %arg0, %129 : i64
    cf.cond_br %128, ^bb30, ^bb31
    ^bb30:
      %130 = arith.constant 1 : i1
      func.return %130 : i1
    ^bb31:
      cf.br ^bb32
    ^bb32:
    %131 = arith.constant 2 : i32
    %133 = arith.extsi %131 : i32 to i64
    %132 = arith.remsi %arg0, %133 : i64
    %134 = arith.constant 0 : i32
    %136 = arith.extsi %134 : i32 to i64
    %135 = arith.cmpi eq, %132, %136 : i64
    %137 = scf.if %135 -> (i1) {
      %138 = arith.constant true
      scf.yield %138 : i1
    } else {
      %139 = arith.constant 3 : i32
      %141 = arith.extsi %139 : i32 to i64
      %140 = arith.remsi %arg0, %141 : i64
      %142 = arith.constant 0 : i32
      %144 = arith.extsi %142 : i32 to i64
      %143 = arith.cmpi eq, %140, %144 : i64
      scf.yield %143 : i1
    }
    cf.cond_br %137, ^bb33, ^bb34
    ^bb33:
      %145 = arith.constant 0 : i1
      func.return %145 : i1
    ^bb34:
      cf.br ^bb35
    ^bb35:
    %146 = arith.constant 5 : i32
    %147 = arith.extsi %146 : i32 to i64
    %148 = llvm.mlir.constant(1 : i64) : i64
    %149 = llvm.alloca %148 x i64 : (i64) -> !llvm.ptr
    llvm.store %147, %149 : i64, !llvm.ptr
    cf.br ^bb36
    ^bb36:
    %150 = llvm.load %149 : !llvm.ptr -> i64
    %151 = llvm.load %149 : !llvm.ptr -> i64
    %152 = arith.muli %150, %151 : i64
    %153 = arith.cmpi sle, %152, %arg0 : i64
    cf.cond_br %153, ^bb37, ^bb38
    ^bb37:
      %154 = llvm.load %149 : !llvm.ptr -> i64
      %155 = arith.remsi %arg0, %154 : i64
      %156 = arith.constant 0 : i32
      %158 = arith.extsi %156 : i32 to i64
      %157 = arith.cmpi eq, %155, %158 : i64
      %159 = scf.if %157 -> (i1) {
        %160 = arith.constant true
        scf.yield %160 : i1
      } else {
        %161 = llvm.load %149 : !llvm.ptr -> i64
        %162 = arith.constant 2 : i32
        %164 = arith.extsi %162 : i32 to i64
        %163 = arith.addi %161, %164 : i64
        %165 = arith.remsi %arg0, %163 : i64
        %166 = arith.constant 0 : i32
        %168 = arith.extsi %166 : i32 to i64
        %167 = arith.cmpi eq, %165, %168 : i64
        scf.yield %167 : i1
      }
      cf.cond_br %159, ^bb39, ^bb40
      ^bb39:
        %169 = arith.constant 0 : i1
        func.return %169 : i1
      ^bb40:
        cf.br ^bb41
      ^bb41:
      %170 = llvm.load %149 : !llvm.ptr -> i64
      %171 = arith.constant 6 : i32
      %173 = arith.extsi %171 : i32 to i64
      %172 = arith.addi %170, %173 : i64
      llvm.store %172, %149 : i64, !llvm.ptr
      cf.br ^bb36
    ^bb38:
    %174 = arith.constant 1 : i1
    func.return %174 : i1
  }
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @modpow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %175 = arith.constant 1 : i32
    %176 = arith.extsi %175 : i32 to i64
    %177 = llvm.mlir.constant(1 : i64) : i64
    %178 = llvm.alloca %177 x i64 : (i64) -> !llvm.ptr
    llvm.store %176, %178 : i64, !llvm.ptr
    %179 = arith.remsi %arg0, %arg2 : i64
    %180 = llvm.mlir.constant(1 : i64) : i64
    %181 = llvm.alloca %180 x i64 : (i64) -> !llvm.ptr
    llvm.store %179, %181 : i64, !llvm.ptr
    %182 = llvm.mlir.constant(1 : i64) : i64
    %183 = llvm.alloca %182 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %183 : i64, !llvm.ptr
    cf.br ^bb42
    ^bb42:
    %184 = llvm.load %183 : !llvm.ptr -> i64
    %185 = arith.constant 0 : i32
    %187 = arith.extsi %185 : i32 to i64
    %186 = arith.cmpi sgt, %184, %187 : i64
    cf.cond_br %186, ^bb43, ^bb44
    ^bb43:
      %188 = llvm.load %183 : !llvm.ptr -> i64
      %189 = arith.constant 2 : i32
      %191 = arith.extsi %189 : i32 to i64
      %190 = arith.remsi %188, %191 : i64
      %192 = arith.constant 1 : i32
      %194 = arith.extsi %192 : i32 to i64
      %193 = arith.cmpi eq, %190, %194 : i64
      cf.cond_br %193, ^bb45, ^bb46
      ^bb45:
        %195 = llvm.load %178 : !llvm.ptr -> i64
        %196 = llvm.load %181 : !llvm.ptr -> i64
        %197 = arith.muli %195, %196 : i64
        %198 = arith.remsi %197, %arg2 : i64
        llvm.store %198, %178 : i64, !llvm.ptr
        cf.br ^bb47
      ^bb46:
        cf.br ^bb47
      ^bb47:
      %199 = llvm.load %181 : !llvm.ptr -> i64
      %200 = llvm.load %181 : !llvm.ptr -> i64
      %201 = arith.muli %199, %200 : i64
      %202 = arith.remsi %201, %arg2 : i64
      llvm.store %202, %181 : i64, !llvm.ptr
      %203 = llvm.load %183 : !llvm.ptr -> i64
      %204 = arith.constant 2 : i32
      %206 = arith.extsi %204 : i32 to i64
      %205 = arith.divsi %203, %206 : i64
      llvm.store %205, %183 : i64, !llvm.ptr
      cf.br ^bb42
    ^bb44:
    %207 = llvm.load %178 : !llvm.ptr -> i64
    func.return %207 : i64
  }
  func.func @tonelli(%arg0: i64, %arg1: i64) -> i64 {
    %208 = arith.remsi %arg0, %arg1 : i64
    %209 = arith.constant 0 : i32
    %211 = arith.extsi %209 : i32 to i64
    %210 = arith.cmpi eq, %208, %211 : i64
    cf.cond_br %210, ^bb48, ^bb49
    ^bb48:
      %212 = arith.constant 0 : i32
      %213 = arith.extsi %212 : i32 to i64
      func.return %213 : i64
    ^bb49:
      cf.br ^bb50
    ^bb50:
    %215 = arith.constant 1 : i32
    %217 = arith.extsi %215 : i32 to i64
    %216 = arith.subi %arg1, %217 : i64
    %218 = arith.constant 2 : i32
    %220 = arith.extsi %218 : i32 to i64
    %219 = arith.divsi %216, %220 : i64
    %214 = func.call @modpow(%208, %219, %arg1) : (i64, i64, i64) -> i64
    %221 = arith.constant 1 : i32
    %223 = arith.extsi %221 : i32 to i64
    %222 = arith.cmpi ne, %214, %223 : i64
    cf.cond_br %222, ^bb51, ^bb52
    ^bb51:
      %224 = arith.constant 0 : i32
      %225 = arith.extsi %224 : i32 to i64
      func.return %225 : i64
    ^bb52:
      cf.br ^bb53
    ^bb53:
    %226 = arith.constant 4 : i32
    %228 = arith.extsi %226 : i32 to i64
    %227 = arith.remsi %arg1, %228 : i64
    %229 = arith.constant 3 : i32
    %231 = arith.extsi %229 : i32 to i64
    %230 = arith.cmpi eq, %227, %231 : i64
    cf.cond_br %230, ^bb54, ^bb55
    ^bb54:
      %233 = arith.constant 1 : i32
      %235 = arith.extsi %233 : i32 to i64
      %234 = arith.addi %arg1, %235 : i64
      %236 = arith.constant 4 : i32
      %238 = arith.extsi %236 : i32 to i64
      %237 = arith.divsi %234, %238 : i64
      %232 = func.call @modpow(%208, %237, %arg1) : (i64, i64, i64) -> i64
      func.return %232 : i64
    ^bb55:
      cf.br ^bb56
    ^bb56:
    %239 = arith.constant 1 : i32
    %241 = arith.extsi %239 : i32 to i64
    %240 = arith.subi %arg1, %241 : i64
    %242 = llvm.mlir.constant(1 : i64) : i64
    %243 = llvm.alloca %242 x i64 : (i64) -> !llvm.ptr
    llvm.store %240, %243 : i64, !llvm.ptr
    %244 = arith.constant 0 : i32
    %245 = llvm.mlir.constant(1 : i64) : i64
    %246 = llvm.alloca %245 x i32 : (i64) -> !llvm.ptr
    llvm.store %244, %246 : i32, !llvm.ptr
    cf.br ^bb57
    ^bb57:
    %247 = llvm.load %243 : !llvm.ptr -> i64
    %248 = arith.constant 2 : i32
    %250 = arith.extsi %248 : i32 to i64
    %249 = arith.remsi %247, %250 : i64
    %251 = arith.constant 0 : i32
    %253 = arith.extsi %251 : i32 to i64
    %252 = arith.cmpi eq, %249, %253 : i64
    cf.cond_br %252, ^bb58, ^bb59
    ^bb58:
      %254 = llvm.load %243 : !llvm.ptr -> i64
      %255 = arith.constant 2 : i32
      %257 = arith.extsi %255 : i32 to i64
      %256 = arith.divsi %254, %257 : i64
      llvm.store %256, %243 : i64, !llvm.ptr
      %258 = llvm.load %246 : !llvm.ptr -> i32
      %259 = arith.constant 1 : i32
      %260 = arith.addi %258, %259 : i32
      llvm.store %260, %246 : i32, !llvm.ptr
      cf.br ^bb57
    ^bb59:
    %261 = arith.constant 2 : i32
    %262 = arith.extsi %261 : i32 to i64
    %263 = llvm.mlir.constant(1 : i64) : i64
    %264 = llvm.alloca %263 x i64 : (i64) -> !llvm.ptr
    llvm.store %262, %264 : i64, !llvm.ptr
    cf.br ^bb60
    ^bb60:
    %266 = llvm.load %264 : !llvm.ptr -> i64
    %267 = arith.constant 1 : i32
    %269 = arith.extsi %267 : i32 to i64
    %268 = arith.subi %arg1, %269 : i64
    %270 = arith.constant 2 : i32
    %272 = arith.extsi %270 : i32 to i64
    %271 = arith.divsi %268, %272 : i64
    %265 = func.call @modpow(%266, %271, %arg1) : (i64, i64, i64) -> i64
    %273 = arith.constant 1 : i32
    %275 = arith.extsi %273 : i32 to i64
    %274 = arith.subi %arg1, %275 : i64
    %276 = arith.cmpi ne, %265, %274 : i64
    cf.cond_br %276, ^bb61, ^bb62
    ^bb61:
      %277 = llvm.load %264 : !llvm.ptr -> i64
      %278 = arith.constant 1 : i32
      %280 = arith.extsi %278 : i32 to i64
      %279 = arith.addi %277, %280 : i64
      llvm.store %279, %264 : i64, !llvm.ptr
      cf.br ^bb60
    ^bb62:
    %281 = llvm.load %246 : !llvm.ptr -> i32
    %282 = llvm.mlir.constant(1 : i64) : i64
    %283 = llvm.alloca %282 x i32 : (i64) -> !llvm.ptr
    llvm.store %281, %283 : i32, !llvm.ptr
    %285 = llvm.load %264 : !llvm.ptr -> i64
    %286 = llvm.load %243 : !llvm.ptr -> i64
    %284 = func.call @modpow(%285, %286, %arg1) : (i64, i64, i64) -> i64
    %287 = llvm.mlir.constant(1 : i64) : i64
    %288 = llvm.alloca %287 x i64 : (i64) -> !llvm.ptr
    llvm.store %284, %288 : i64, !llvm.ptr
    %290 = llvm.load %243 : !llvm.ptr -> i64
    %289 = func.call @modpow(%208, %290, %arg1) : (i64, i64, i64) -> i64
    %291 = llvm.mlir.constant(1 : i64) : i64
    %292 = llvm.alloca %291 x i64 : (i64) -> !llvm.ptr
    llvm.store %289, %292 : i64, !llvm.ptr
    %294 = llvm.load %243 : !llvm.ptr -> i64
    %295 = arith.constant 1 : i32
    %297 = arith.extsi %295 : i32 to i64
    %296 = arith.addi %294, %297 : i64
    %298 = arith.constant 2 : i32
    %300 = arith.extsi %298 : i32 to i64
    %299 = arith.divsi %296, %300 : i64
    %293 = func.call @modpow(%208, %299, %arg1) : (i64, i64, i64) -> i64
    %301 = llvm.mlir.constant(1 : i64) : i64
    %302 = llvm.alloca %301 x i64 : (i64) -> !llvm.ptr
    llvm.store %293, %302 : i64, !llvm.ptr
    cf.br ^bb63
    ^bb63:
    %303 = llvm.load %292 : !llvm.ptr -> i64
    %304 = arith.constant 1 : i32
    %306 = arith.extsi %304 : i32 to i64
    %305 = arith.cmpi ne, %303, %306 : i64
    cf.cond_br %305, ^bb64, ^bb65
    ^bb64:
      %307 = arith.constant 1 : i32
      %308 = llvm.mlir.constant(1 : i64) : i64
      %309 = llvm.alloca %308 x i32 : (i64) -> !llvm.ptr
      llvm.store %307, %309 : i32, !llvm.ptr
      %310 = llvm.load %292 : !llvm.ptr -> i64
      %311 = llvm.load %292 : !llvm.ptr -> i64
      %312 = arith.muli %310, %311 : i64
      %313 = arith.remsi %312, %arg1 : i64
      %314 = llvm.mlir.constant(1 : i64) : i64
      %315 = llvm.alloca %314 x i64 : (i64) -> !llvm.ptr
      llvm.store %313, %315 : i64, !llvm.ptr
      cf.br ^bb66
      ^bb66:
      %316 = llvm.load %315 : !llvm.ptr -> i64
      %317 = arith.constant 1 : i32
      %319 = arith.extsi %317 : i32 to i64
      %318 = arith.cmpi ne, %316, %319 : i64
      cf.cond_br %318, ^bb67, ^bb68
      ^bb67:
        %320 = llvm.load %315 : !llvm.ptr -> i64
        %321 = llvm.load %315 : !llvm.ptr -> i64
        %322 = arith.muli %320, %321 : i64
        %323 = arith.remsi %322, %arg1 : i64
        llvm.store %323, %315 : i64, !llvm.ptr
        %324 = llvm.load %309 : !llvm.ptr -> i32
        %325 = arith.constant 1 : i32
        %326 = arith.addi %324, %325 : i32
        llvm.store %326, %309 : i32, !llvm.ptr
        cf.br ^bb66
      ^bb68:
      %327 = llvm.load %283 : !llvm.ptr -> i32
      %328 = llvm.load %309 : !llvm.ptr -> i32
      %329 = arith.subi %327, %328 : i32
      %330 = arith.constant 1 : i32
      %331 = arith.subi %329, %330 : i32
      %332 = llvm.mlir.constant(1 : i64) : i64
      %333 = llvm.alloca %332 x i32 : (i64) -> !llvm.ptr
      llvm.store %331, %333 : i32, !llvm.ptr
      %334 = llvm.load %288 : !llvm.ptr -> i64
      %335 = llvm.mlir.constant(1 : i64) : i64
      %336 = llvm.alloca %335 x i64 : (i64) -> !llvm.ptr
      llvm.store %334, %336 : i64, !llvm.ptr
      %337 = arith.constant 0 : i32
      %338 = llvm.mlir.constant(1 : i64) : i64
      %339 = llvm.alloca %338 x i32 : (i64) -> !llvm.ptr
      llvm.store %337, %339 : i32, !llvm.ptr
      cf.br ^bb69
      ^bb69:
      %340 = llvm.load %339 : !llvm.ptr -> i32
      %341 = llvm.load %333 : !llvm.ptr -> i32
      %342 = arith.cmpi slt, %340, %341 : i32
      cf.cond_br %342, ^bb70, ^bb71
      ^bb70:
        %343 = llvm.load %336 : !llvm.ptr -> i64
        %344 = llvm.load %336 : !llvm.ptr -> i64
        %345 = arith.muli %343, %344 : i64
        %346 = arith.remsi %345, %arg1 : i64
        llvm.store %346, %336 : i64, !llvm.ptr
        %347 = llvm.load %339 : !llvm.ptr -> i32
        %348 = arith.constant 1 : i32
        %349 = arith.addi %347, %348 : i32
        llvm.store %349, %339 : i32, !llvm.ptr
        cf.br ^bb69
      ^bb71:
      %350 = llvm.load %302 : !llvm.ptr -> i64
      %351 = llvm.load %336 : !llvm.ptr -> i64
      %352 = arith.muli %350, %351 : i64
      %353 = arith.remsi %352, %arg1 : i64
      llvm.store %353, %302 : i64, !llvm.ptr
      %354 = llvm.load %292 : !llvm.ptr -> i64
      %355 = llvm.load %336 : !llvm.ptr -> i64
      %356 = arith.muli %354, %355 : i64
      %357 = arith.remsi %356, %arg1 : i64
      %358 = llvm.load %336 : !llvm.ptr -> i64
      %359 = arith.muli %357, %358 : i64
      %360 = arith.remsi %359, %arg1 : i64
      llvm.store %360, %292 : i64, !llvm.ptr
      %361 = llvm.load %336 : !llvm.ptr -> i64
      %362 = llvm.load %336 : !llvm.ptr -> i64
      %363 = arith.muli %361, %362 : i64
      %364 = arith.remsi %363, %arg1 : i64
      llvm.store %364, %288 : i64, !llvm.ptr
      %365 = llvm.load %309 : !llvm.ptr -> i32
      llvm.store %365, %283 : i32, !llvm.ptr
      cf.br ^bb63
    ^bb65:
    %366 = llvm.load %302 : !llvm.ptr -> i64
    func.return %366 : i64
  }
  func.func @main() -> i32 {
    %367 = arith.constant 50000000 : i32
    %368 = arith.extsi %367 : i32 to i64
    %369 = arith.constant 2 : i32
    %371 = arith.extsi %369 : i32 to i64
    %370 = arith.muli %371, %368 : i64
    %372 = arith.muli %370, %368 : i64
    %373 = arith.constant 1 : i32
    %375 = arith.extsi %373 : i32 to i64
    %374 = arith.subi %372, %375 : i64
    %376 = func.call @isqrt(%374) : (i64) -> i64
    %377 = arith.constant 1 : i32
    %379 = arith.extsi %377 : i32 to i64
    %378 = arith.addi %376, %379 : i64
    %381 = arith.constant 1 : i32
    %383 = arith.extsi %381 : i32 to i64
    %382 = arith.addi %378, %383 : i64
    %384 = arith.constant 1 : i32
    %385 = arith.extsi %384 : i32 to i64
    %380 = func.call @calloc(%382, %385) : (i64, i64) -> !llvm.ptr
    %386 = llvm.mlir.zero : !llvm.ptr
    %387 = llvm.icmp "eq" %380, %386 : !llvm.ptr
    cf.cond_br %387, ^bb72, ^bb73
    ^bb72:
      %388 = arith.constant 1 : i32
      func.return %388 : i32
    ^bb73:
      cf.br ^bb74
    ^bb74:
    %389 = arith.constant 0 : i32
    %390 = arith.extsi %389 : i32 to i64
    %391 = llvm.mlir.constant(1 : i64) : i64
    %392 = llvm.alloca %391 x i64 : (i64) -> !llvm.ptr
    llvm.store %390, %392 : i64, !llvm.ptr
    cf.br ^bb75
    ^bb75:
    %393 = llvm.load %392 : !llvm.ptr -> i64
    %394 = arith.cmpi sle, %393, %378 : i64
    cf.cond_br %394, ^bb76, ^bb77
    ^bb76:
      %395 = arith.constant 1 : i32
      %396 = llvm.load %392 : !llvm.ptr -> i64
      %397 = arith.trunci %395 : i32 to i8
      %398 = llvm.getelementptr %380[%396] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %397, %398 : i8, !llvm.ptr
      %399 = llvm.load %392 : !llvm.ptr -> i64
      %400 = arith.constant 1 : i32
      %402 = arith.extsi %400 : i32 to i64
      %401 = arith.addi %399, %402 : i64
      llvm.store %401, %392 : i64, !llvm.ptr
      cf.br ^bb75
    ^bb77:
    %403 = arith.constant 0 : i32
    %404 = arith.constant 0 : i32
    %405 = arith.trunci %403 : i32 to i8
    %406 = arith.extsi %404 : i32 to i64
    %407 = llvm.getelementptr %380[%406] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %405, %407 : i8, !llvm.ptr
    %408 = arith.constant 0 : i32
    %409 = arith.constant 1 : i32
    %410 = arith.trunci %408 : i32 to i8
    %411 = arith.extsi %409 : i32 to i64
    %412 = llvm.getelementptr %380[%411] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %410, %412 : i8, !llvm.ptr
    %413 = arith.constant 2 : i32
    %414 = arith.extsi %413 : i32 to i64
    llvm.store %414, %392 : i64, !llvm.ptr
    cf.br ^bb78
    ^bb78:
    %415 = llvm.load %392 : !llvm.ptr -> i64
    %416 = llvm.load %392 : !llvm.ptr -> i64
    %417 = arith.muli %415, %416 : i64
    %418 = arith.cmpi sle, %417, %378 : i64
    cf.cond_br %418, ^bb79, ^bb80
    ^bb79:
      %420 = llvm.load %392 : !llvm.ptr -> i64
      %421 = llvm.getelementptr %380[%420] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %419 = llvm.load %421 : !llvm.ptr -> i8
      %422 = arith.constant 1 : i32
      %424 = arith.extsi %419 : i8 to i32
      %423 = arith.cmpi eq, %424, %422 : i32
      cf.cond_br %423, ^bb81, ^bb82
      ^bb81:
        %425 = llvm.load %392 : !llvm.ptr -> i64
        %426 = llvm.load %392 : !llvm.ptr -> i64
        %427 = arith.muli %425, %426 : i64
        %428 = llvm.mlir.constant(1 : i64) : i64
        %429 = llvm.alloca %428 x i64 : (i64) -> !llvm.ptr
        llvm.store %427, %429 : i64, !llvm.ptr
        cf.br ^bb84
        ^bb84:
        %430 = llvm.load %429 : !llvm.ptr -> i64
        %431 = arith.cmpi sle, %430, %378 : i64
        cf.cond_br %431, ^bb85, ^bb86
        ^bb85:
          %432 = arith.constant 0 : i32
          %433 = llvm.load %429 : !llvm.ptr -> i64
          %434 = arith.trunci %432 : i32 to i8
          %435 = llvm.getelementptr %380[%433] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %434, %435 : i8, !llvm.ptr
          %436 = llvm.load %429 : !llvm.ptr -> i64
          %437 = llvm.load %392 : !llvm.ptr -> i64
          %438 = arith.addi %436, %437 : i64
          llvm.store %438, %429 : i64, !llvm.ptr
          cf.br ^bb84
        ^bb86:
        cf.br ^bb83
      ^bb82:
        cf.br ^bb83
      ^bb83:
      %439 = llvm.load %392 : !llvm.ptr -> i64
      %440 = arith.constant 1 : i32
      %442 = arith.extsi %440 : i32 to i64
      %441 = arith.addi %439, %442 : i64
      llvm.store %441, %392 : i64, !llvm.ptr
      cf.br ^bb78
    ^bb80:
    %444 = arith.constant 1 : i32
    %446 = arith.extsi %444 : i32 to i64
    %445 = arith.addi %368, %446 : i64
    %447 = arith.constant 1 : i32
    %448 = arith.extsi %447 : i32 to i64
    %443 = func.call @calloc(%445, %448) : (i64, i64) -> !llvm.ptr
    %449 = llvm.mlir.zero : !llvm.ptr
    %450 = llvm.icmp "eq" %443, %449 : !llvm.ptr
    cf.cond_br %450, ^bb87, ^bb88
    ^bb87:
      %451 = arith.constant 1 : i32
      func.return %451 : i32
    ^bb88:
      cf.br ^bb89
    ^bb89:
    %452 = arith.constant 3 : i32
    %453 = arith.extsi %452 : i32 to i64
    llvm.store %453, %392 : i64, !llvm.ptr
    cf.br ^bb90
    ^bb90:
    %454 = llvm.load %392 : !llvm.ptr -> i64
    %455 = arith.cmpi sle, %454, %378 : i64
    cf.cond_br %455, ^bb91, ^bb92
    ^bb91:
      %457 = llvm.load %392 : !llvm.ptr -> i64
      %458 = llvm.getelementptr %380[%457] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %456 = llvm.load %458 : !llvm.ptr -> i8
      %459 = arith.constant 1 : i32
      %461 = arith.extsi %456 : i8 to i32
      %460 = arith.cmpi eq, %461, %459 : i32
      cf.cond_br %460, ^bb93, ^bb94
      ^bb93:
        %462 = llvm.load %392 : !llvm.ptr -> i64
        %463 = arith.constant 7 : i32
        %465 = arith.extsi %463 : i32 to i64
        %464 = arith.andi %462, %465 : i64
        %466 = arith.constant 1 : i32
        %468 = arith.extsi %466 : i32 to i64
        %467 = arith.cmpi eq, %464, %468 : i64
        %469 = scf.if %467 -> (i1) {
          %470 = arith.constant true
          scf.yield %470 : i1
        } else {
          %471 = arith.constant 7 : i32
          %473 = arith.extsi %471 : i32 to i64
          %472 = arith.cmpi eq, %464, %473 : i64
          scf.yield %472 : i1
        }
        cf.cond_br %469, ^bb96, ^bb97
        ^bb96:
          %474 = llvm.load %392 : !llvm.ptr -> i64
          %475 = arith.constant 1 : i32
          %477 = arith.extsi %475 : i32 to i64
          %476 = arith.addi %474, %477 : i64
          %478 = arith.constant 2 : i32
          %480 = arith.extsi %478 : i32 to i64
          %479 = arith.divsi %476, %480 : i64
          %481 = arith.constant 0 : i32
          %482 = arith.extsi %481 : i32 to i64
          %483 = llvm.mlir.constant(1 : i64) : i64
          %484 = llvm.alloca %483 x i64 : (i64) -> !llvm.ptr
          llvm.store %482, %484 : i64, !llvm.ptr
          %485 = arith.constant 7 : i32
          %487 = arith.extsi %485 : i32 to i64
          %486 = arith.cmpi eq, %464, %487 : i64
          cf.cond_br %486, ^bb99, ^bb100
          ^bb99:
            %489 = arith.constant 2 : i32
            %490 = llvm.load %392 : !llvm.ptr -> i64
            %491 = arith.constant 1 : i32
            %493 = arith.extsi %491 : i32 to i64
            %492 = arith.addi %490, %493 : i64
            %494 = arith.constant 4 : i32
            %496 = arith.extsi %494 : i32 to i64
            %495 = arith.divsi %492, %496 : i64
            %497 = llvm.load %392 : !llvm.ptr -> i64
            %498 = arith.extsi %489 : i32 to i64
            %488 = func.call @modpow(%498, %495, %497) : (i64, i64, i64) -> i64
            %499 = arith.muli %488, %479 : i64
            %500 = llvm.load %392 : !llvm.ptr -> i64
            %501 = arith.remsi %499, %500 : i64
            llvm.store %501, %484 : i64, !llvm.ptr
            cf.br ^bb101
          ^bb100:
            %503 = llvm.load %392 : !llvm.ptr -> i64
            %502 = func.call @tonelli(%479, %503) : (i64, i64) -> i64
            llvm.store %502, %484 : i64, !llvm.ptr
            cf.br ^bb101
          ^bb101:
          %504 = llvm.load %484 : !llvm.ptr -> i64
          %505 = arith.constant 0 : i32
          %507 = arith.extsi %505 : i32 to i64
          %506 = arith.cmpi ne, %504, %507 : i64
          %508 = scf.if %506 -> (i1) {
            %509 = arith.constant true
            scf.yield %509 : i1
          } else {
            %510 = arith.constant 7 : i32
            %512 = arith.extsi %510 : i32 to i64
            %511 = arith.cmpi eq, %464, %512 : i64
            scf.yield %511 : i1
          }
          cf.cond_br %508, ^bb102, ^bb103
          ^bb102:
            %513 = llvm.load %484 : !llvm.ptr -> i64
            %514 = llvm.mlir.constant(1 : i64) : i64
            %515 = llvm.alloca %514 x i64 : (i64) -> !llvm.ptr
            llvm.store %513, %515 : i64, !llvm.ptr
            %516 = llvm.load %515 : !llvm.ptr -> i64
            %517 = arith.constant 2 : i32
            %519 = arith.extsi %517 : i32 to i64
            %518 = arith.cmpi slt, %516, %519 : i64
            cf.cond_br %518, ^bb105, ^bb106
            ^bb105:
              %520 = llvm.load %515 : !llvm.ptr -> i64
              %521 = arith.constant 2 : i32
              %522 = llvm.load %515 : !llvm.ptr -> i64
              %524 = arith.extsi %521 : i32 to i64
              %523 = arith.subi %524, %522 : i64
              %525 = llvm.load %392 : !llvm.ptr -> i64
              %526 = arith.addi %523, %525 : i64
              %527 = arith.constant 1 : i32
              %529 = arith.extsi %527 : i32 to i64
              %528 = arith.subi %526, %529 : i64
              %530 = llvm.load %392 : !llvm.ptr -> i64
              %531 = arith.divsi %528, %530 : i64
              %532 = llvm.load %392 : !llvm.ptr -> i64
              %533 = arith.muli %531, %532 : i64
              %534 = arith.addi %520, %533 : i64
              llvm.store %534, %515 : i64, !llvm.ptr
              cf.br ^bb107
            ^bb106:
              cf.br ^bb107
            ^bb107:
            %535 = llvm.load %515 : !llvm.ptr -> i64
            %536 = llvm.mlir.constant(1 : i64) : i64
            %537 = llvm.alloca %536 x i64 : (i64) -> !llvm.ptr
            llvm.store %535, %537 : i64, !llvm.ptr
            cf.br ^bb108
            ^bb108:
            %538 = llvm.load %537 : !llvm.ptr -> i64
            %539 = arith.cmpi sle, %538, %368 : i64
            cf.cond_br %539, ^bb109, ^bb110
            ^bb109:
              %540 = arith.constant 1 : i32
              %541 = llvm.load %537 : !llvm.ptr -> i64
              %542 = arith.trunci %540 : i32 to i8
              %543 = llvm.getelementptr %443[%541] : (!llvm.ptr, i64) -> !llvm.ptr, i8
              llvm.store %542, %543 : i8, !llvm.ptr
              %544 = llvm.load %537 : !llvm.ptr -> i64
              %545 = llvm.load %392 : !llvm.ptr -> i64
              %546 = arith.addi %544, %545 : i64
              llvm.store %546, %537 : i64, !llvm.ptr
              cf.br ^bb108
            ^bb110:
            %547 = llvm.load %392 : !llvm.ptr -> i64
            %548 = llvm.load %484 : !llvm.ptr -> i64
            %549 = arith.subi %547, %548 : i64
            %550 = llvm.load %392 : !llvm.ptr -> i64
            %551 = arith.remsi %549, %550 : i64
            %552 = llvm.load %484 : !llvm.ptr -> i64
            %553 = arith.cmpi ne, %551, %552 : i64
            cf.cond_br %553, ^bb111, ^bb112
            ^bb111:
              llvm.store %551, %515 : i64, !llvm.ptr
              %554 = llvm.load %515 : !llvm.ptr -> i64
              %555 = arith.constant 2 : i32
              %557 = arith.extsi %555 : i32 to i64
              %556 = arith.cmpi slt, %554, %557 : i64
              cf.cond_br %556, ^bb114, ^bb115
              ^bb114:
                %558 = llvm.load %515 : !llvm.ptr -> i64
                %559 = arith.constant 2 : i32
                %560 = llvm.load %515 : !llvm.ptr -> i64
                %562 = arith.extsi %559 : i32 to i64
                %561 = arith.subi %562, %560 : i64
                %563 = llvm.load %392 : !llvm.ptr -> i64
                %564 = arith.addi %561, %563 : i64
                %565 = arith.constant 1 : i32
                %567 = arith.extsi %565 : i32 to i64
                %566 = arith.subi %564, %567 : i64
                %568 = llvm.load %392 : !llvm.ptr -> i64
                %569 = arith.divsi %566, %568 : i64
                %570 = llvm.load %392 : !llvm.ptr -> i64
                %571 = arith.muli %569, %570 : i64
                %572 = arith.addi %558, %571 : i64
                llvm.store %572, %515 : i64, !llvm.ptr
                cf.br ^bb116
              ^bb115:
                cf.br ^bb116
              ^bb116:
              %573 = llvm.load %515 : !llvm.ptr -> i64
              llvm.store %573, %537 : i64, !llvm.ptr
              cf.br ^bb117
              ^bb117:
              %574 = llvm.load %537 : !llvm.ptr -> i64
              %575 = arith.cmpi sle, %574, %368 : i64
              cf.cond_br %575, ^bb118, ^bb119
              ^bb118:
                %576 = arith.constant 1 : i32
                %577 = llvm.load %537 : !llvm.ptr -> i64
                %578 = arith.trunci %576 : i32 to i8
                %579 = llvm.getelementptr %443[%577] : (!llvm.ptr, i64) -> !llvm.ptr, i8
                llvm.store %578, %579 : i8, !llvm.ptr
                %580 = llvm.load %537 : !llvm.ptr -> i64
                %581 = llvm.load %392 : !llvm.ptr -> i64
                %582 = arith.addi %580, %581 : i64
                llvm.store %582, %537 : i64, !llvm.ptr
                cf.br ^bb117
              ^bb119:
              cf.br ^bb113
            ^bb112:
              cf.br ^bb113
            ^bb113:
            %583 = func.call @isqrt(%479) : (i64) -> i64
            %584 = arith.muli %583, %583 : i64
            %585 = arith.cmpi eq, %584, %479 : i64
            %586 = scf.if %585 -> (i1) {
              %587 = arith.cmpi sle, %583, %368 : i64
              scf.yield %587 : i1
            } else {
              %588 = arith.constant false
              scf.yield %588 : i1
            }
            cf.cond_br %586, ^bb120, ^bb121
            ^bb120:
              %589 = arith.constant 0 : i32
              %590 = arith.trunci %589 : i32 to i8
              %591 = llvm.getelementptr %443[%583] : (!llvm.ptr, i64) -> !llvm.ptr, i8
              llvm.store %590, %591 : i8, !llvm.ptr
              cf.br ^bb122
            ^bb121:
              cf.br ^bb122
            ^bb122:
            cf.br ^bb104
          ^bb103:
            cf.br ^bb104
          ^bb104:
          cf.br ^bb98
        ^bb97:
          cf.br ^bb98
        ^bb98:
        cf.br ^bb95
      ^bb94:
        cf.br ^bb95
      ^bb95:
      %592 = llvm.load %392 : !llvm.ptr -> i64
      %593 = arith.constant 1 : i32
      %595 = arith.extsi %593 : i32 to i64
      %594 = arith.addi %592, %595 : i64
      llvm.store %594, %392 : i64, !llvm.ptr
      cf.br ^bb90
    ^bb92:
    %596 = arith.constant 0 : i32
    %597 = arith.extsi %596 : i32 to i64
    %598 = llvm.mlir.constant(1 : i64) : i64
    %599 = llvm.alloca %598 x i64 : (i64) -> !llvm.ptr
    llvm.store %597, %599 : i64, !llvm.ptr
    %600 = arith.constant 2 : i32
    %601 = arith.extsi %600 : i32 to i64
    llvm.store %601, %392 : i64, !llvm.ptr
    cf.br ^bb123
    ^bb123:
    %602 = llvm.load %392 : !llvm.ptr -> i64
    %603 = arith.cmpi sle, %602, %368 : i64
    cf.cond_br %603, ^bb124, ^bb125
    ^bb124:
      %605 = llvm.load %392 : !llvm.ptr -> i64
      %606 = llvm.getelementptr %443[%605] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %604 = llvm.load %606 : !llvm.ptr -> i8
      %607 = arith.constant 0 : i32
      %609 = arith.extsi %604 : i8 to i32
      %608 = arith.cmpi eq, %609, %607 : i32
      cf.cond_br %608, ^bb126, ^bb127
      ^bb126:
        %610 = llvm.load %599 : !llvm.ptr -> i64
        %611 = arith.constant 1 : i32
        %613 = arith.extsi %611 : i32 to i64
        %612 = arith.addi %610, %613 : i64
        llvm.store %612, %599 : i64, !llvm.ptr
        cf.br ^bb128
      ^bb127:
        cf.br ^bb128
      ^bb128:
      %614 = llvm.load %392 : !llvm.ptr -> i64
      %615 = arith.constant 1 : i32
      %617 = arith.extsi %615 : i32 to i64
      %616 = arith.addi %614, %617 : i64
      llvm.store %616, %392 : i64, !llvm.ptr
      cf.br ^bb123
    ^bb125:
    %618 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %619 = llvm.load %599 : !llvm.ptr -> i64
    %620 = llvm.call @printf(%618, %619) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%380) : (!llvm.ptr) -> ()
    func.call @free(%443) : (!llvm.ptr) -> ()
    %623 = arith.constant 0 : i32
    func.return %623 : i32
  }
}