Problem 457

Polynomial mod p^2 — sum R(p) for primes p ≤ 10^7. Tonelli–Shanks + Hensel lift; i128 mulmod for modpow.

Answer2647787126797397063
Output2647787126797397063
StatusPASS
Native helperno
Runtime700 ms
Peak memory10864 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)?
Space complexityO(n)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 457
# Polynomial mod p^2 — sum R(p) for primes p ≤ 10^7.
# Tonelli–Shanks + Hensel lift; i128 mulmod for modpow.

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

function modpow(base0: i64, exp0: i64, mod: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = base0 % mod
    if b < 0 { b = b + mod }
    let mut e: i64 = exp0
    while e > 0 {
        if (e & 1) == 1 {
            r = ((r as i128) * (b as i128) % (mod as i128)) as i64
        }
        b = ((b as i128) * (b as i128) % (mod as i128)) as i64
        e = e / 2
    }
    return r
}

function modinv(a0: i64, m: i64) -> i64 {
    let mut a: i64 = a0 % m
    if a < 0 { a = a + m }
    let mut b: i64 = m
    let mut x0: i64 = 1
    let mut x1: i64 = 0
    while b != 0 {
        let q: i64 = a / b
        let t: i64 = a - q * b
        a = b
        b = t
        let tx: i64 = x0 - q * x1
        x0 = x1
        x1 = tx
    }
    if x0 < 0 { x0 = x0 + m }
    return x0
}

function legendre(a0: i64, p: i64) -> i64 {
    let t: i64 = modpow(a0, (p - 1) / 2, p)
    if t == p - 1 { return 0 - 1 }
    return t
}

function tonelli(a0: i64, p: i64) -> i64 {
    let mut a: i64 = a0 % p
    if a < 0 { a = a + p }
    if legendre(a, p) != 1 { return 0 }
    if a == 0 { return 0 }
    if p == 2 { return a }
    if p % 4 == 3 {
        return modpow(a, (p + 1) / 4, p)
    }
    let mut s: i64 = p - 1
    let mut e: i64 = 0
    while s % 2 == 0 {
        s = s / 2
        e = e + 1
    }
    let mut n: i64 = 2
    while legendre(n, p) != 0 - 1 {
        n = n + 1
    }
    let mut x: i64 = modpow(a, (s + 1) / 2, p)
    let mut b: i64 = modpow(a, s, p)
    let mut g: i64 = modpow(n, s, p)
    let mut r: i64 = e
    while true {
        let mut t: i64 = b
        let mut m: i64 = 0
        while m < r {
            if t == 1 { break }
            t = ((t as i128) * (t as i128) % (p as i128)) as i64
            m = m + 1
        }
        if m == 0 { return x }
        let mut gs: i64 = g
        let mut k: i64 = 0
        while k < r - m - 1 {
            gs = ((gs as i128) * (gs as i128) % (p as i128)) as i64
            k = k + 1
        }
        g = ((gs as i128) * (gs as i128) % (p as i128)) as i64
        x = ((x as i128) * (gs as i128) % (p as i128)) as i64
        b = ((b as i128) * (g as i128) % (p as i128)) as i64
        r = m
    }
    return 0
}

function R_p(p: i64) -> i64 {
    if p == 3 { return 5 }
    let r: i64 = tonelli(13, p)
    if r == 0 { return 0 }
    let fpr: i64 = (2 * r) % p
    if fpr == 0 { return 0 }
    let rr13: i64 = r * r - 13
    let quot: i64 = rr13 / p
    let inv: i64 = modinv(fpr, p)
    let mut t: i64 = 0 - ((inv as i128) * (quot as i128) % (p as i128)) as i64
    t = t % p
    if t < 0 { t = t + p }
    let pp: i64 = p * p
    let mut n: i64 = (r + t * p) % pp
    if n < 0 { n = n + pp }
    if n % 2 != 0 {
        return (n + 3) / 2
    }
    return (pp - n + 3) / 2
}

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

    let mut total: i64 = 0
    let mut p: i64 = 3
    while p <= LIMIT {
        if sieve[p] == 1 {
            if legendre(13, p) == 1 {
                total = total + R_p(p)
            }
        }
        p = p + 2
    }
    free(sieve)
    printf("%lld\n", total)
    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 modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int64_t modinv_i64_i64(int64_t a0, int64_t m);
int64_t legendre_i64_i64(int64_t a0, int64_t p);
int64_t tonelli_i64_i64(int64_t a0, int64_t p);
int64_t R_p_i64(int64_t p);
int32_t main(void);



int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod) {
    int64_t r = 1;
    int64_t b = FLOW_CHECKED_MOD((base0), (mod));
    if (b < 0) {
        b = (b + mod);
    }
    int64_t e = exp0;
    while (e > 0) {
        if ((e & 1) == 1) {
            r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))))));
        }
        b = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))))));
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return r;
}

int64_t modinv_i64_i64(int64_t a0, int64_t m) {
    int64_t a = FLOW_CHECKED_MOD((a0), (m));
    if (a < 0) {
        a = (a + m);
    }
    int64_t b = m;
    int64_t x0 = 1;
    int64_t x1 = 0;
    while (b != 0) {
        int64_t q = FLOW_CHECKED_DIV((a), (b));
        int64_t t = (a - (q * b));
        a = b;
        b = t;
        int64_t tx = (x0 - (q * x1));
        x0 = x1;
        x1 = tx;
    }
    if (x0 < 0) {
        x0 = (x0 + m);
    }
    return x0;
}

int64_t legendre_i64_i64(int64_t a0, int64_t p) {
    int64_t t = modpow_i64_i64_i64(a0, FLOW_CHECKED_DIV(((p - 1)), (2)), p);
    if (t == (p - 1)) {
        return (0 - 1);
    }
    return t;
}

int64_t tonelli_i64_i64(int64_t a0, int64_t p) {
    int64_t a = FLOW_CHECKED_MOD((a0), (p));
    if (a < 0) {
        a = (a + p);
    }
    if (legendre_i64_i64(a, p) != 1) {
        return 0;
    }
    if (a == 0) {
        return 0;
    }
    if (p == 2) {
        return a;
    }
    if (FLOW_CHECKED_MOD((p), (4)) == 3) {
        return modpow_i64_i64_i64(a, FLOW_CHECKED_DIV(((p + 1)), (4)), p);
    }
    int64_t s = (p - 1);
    int64_t e = 0;
    while (FLOW_CHECKED_MOD((s), (2)) == 0) {
        s = FLOW_CHECKED_DIV((s), (2));
        e = (e + 1);
    }
    int64_t n = 2;
    while (legendre_i64_i64(n, p) != (0 - 1)) {
        n = (n + 1);
    }
    int64_t x = modpow_i64_i64_i64(a, FLOW_CHECKED_DIV(((s + 1)), (2)), p);
    int64_t b = modpow_i64_i64_i64(a, s, p);
    int64_t g = modpow_i64_i64_i64(n, s, p);
    int64_t r = e;
    while (1) {
        int64_t t = b;
        int64_t m = 0;
        while (m < r) {
            if (t == 1) {
                break;
            }
            t = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(t)) * ((__int128)(t)))), (((__int128)(p))))));
            m = (m + 1);
        }
        if (m == 0) {
            return x;
        }
        int64_t gs = g;
        int64_t k = 0;
        while (k < ((r - m) - 1)) {
            gs = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(gs)) * ((__int128)(gs)))), (((__int128)(p))))));
            k = (k + 1);
        }
        g = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(gs)) * ((__int128)(gs)))), (((__int128)(p))))));
        x = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(x)) * ((__int128)(gs)))), (((__int128)(p))))));
        b = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(g)))), (((__int128)(p))))));
        r = m;
    }
    return 0;
}

int64_t R_p_i64(int64_t p) {
    if (p == 3) {
        return 5;
    }
    int64_t r = tonelli_i64_i64(13, p);
    if (r == 0) {
        return 0;
    }
    int64_t fpr = FLOW_CHECKED_MOD(((2 * r)), (p));
    if (fpr == 0) {
        return 0;
    }
    int64_t rr13 = ((r * r) - 13);
    int64_t quot = FLOW_CHECKED_DIV((rr13), (p));
    int64_t inv = modinv_i64_i64(fpr, p);
    int64_t t = (0 - ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(inv)) * ((__int128)(quot)))), (((__int128)(p)))))));
    t = FLOW_CHECKED_MOD((t), (p));
    if (t < 0) {
        t = (t + p);
    }
    int64_t pp = (p * p);
    int64_t n = FLOW_CHECKED_MOD(((r + (t * p))), (pp));
    if (n < 0) {
        n = (n + pp);
    }
    if (FLOW_CHECKED_MOD((n), (2)) != 0) {
        return FLOW_CHECKED_DIV(((n + 3)), (2));
    }
    return FLOW_CHECKED_DIV((((pp - n) + 3)), (2));
}

int32_t main(void) {
    int64_t LIMIT = 10000000;
    int8_t* sieve = (int8_t*)(calloc((LIMIT + 1), 1));
    if (sieve == NULL) {
        return 1;
    }
    int64_t i = 0;
    while (i <= LIMIT) {
        sieve[i] = 1;
        i = (i + 1);
    }
    sieve[0] = 0;
    sieve[1] = 0;
    i = 2;
    while ((i * i) <= LIMIT) {
        if (sieve[i] == 1) {
            int64_t j = (i * i);
            while (j <= LIMIT) {
                sieve[j] = 0;
                j = (j + i);
            }
        }
        i = (i + 1);
    }
    int64_t total = 0;
    int64_t p = 3;
    while (p <= LIMIT) {
        if (sieve[p] == 1) {
            if (legendre_i64_i64(13, p) == 1) {
                total = (total + R_p_i64(p));
            }
        }
        p = (p + 2);
    }
    free(sieve);
    printf("%lld\n", total);
    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 private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @modpow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %0 = arith.constant 1 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
    llvm.store %1, %3 : i64, !llvm.ptr
    %4 = arith.remsi %arg0, %arg2 : i64
    %5 = llvm.mlir.constant(1 : i64) : i64
    %6 = llvm.alloca %5 x i64 : (i64) -> !llvm.ptr
    llvm.store %4, %6 : i64, !llvm.ptr
    %7 = llvm.load %6 : !llvm.ptr -> i64
    %8 = arith.constant 0 : i32
    %10 = arith.extsi %8 : i32 to i64
    %9 = arith.cmpi slt, %7, %10 : i64
    cf.cond_br %9, ^bb0, ^bb1
    ^bb0:
      %11 = llvm.load %6 : !llvm.ptr -> i64
      %12 = arith.addi %11, %arg2 : i64
      llvm.store %12, %6 : i64, !llvm.ptr
      cf.br ^bb2
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %13 = llvm.mlir.constant(1 : i64) : i64
    %14 = llvm.alloca %13 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %14 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %15 = llvm.load %14 : !llvm.ptr -> i64
    %16 = arith.constant 0 : i32
    %18 = arith.extsi %16 : i32 to i64
    %17 = arith.cmpi sgt, %15, %18 : i64
    cf.cond_br %17, ^bb4, ^bb5
    ^bb4:
      %19 = llvm.load %14 : !llvm.ptr -> i64
      %20 = arith.constant 1 : i32
      %22 = arith.extsi %20 : i32 to i64
      %21 = arith.andi %19, %22 : i64
      %23 = arith.constant 1 : i32
      %25 = arith.extsi %23 : i32 to i64
      %24 = arith.cmpi eq, %21, %25 : i64
      cf.cond_br %24, ^bb6, ^bb7
      ^bb6:
        %26 = llvm.load %3 : !llvm.ptr -> i64
        %27 = arith.extsi %26 : i64 to i128
        %28 = llvm.load %6 : !llvm.ptr -> i64
        %29 = arith.extsi %28 : i64 to i128
        %31 = arith.trunci %27 : i128 to i64
        %32 = arith.trunci %29 : i128 to i64
        %30 = arith.muli %31, %32 : i64
        %33 = arith.extsi %arg2 : i64 to i128
        %35 = arith.trunci %33 : i128 to i64
        %34 = arith.remsi %30, %35 : i64
        llvm.store %34, %3 : i64, !llvm.ptr
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %36 = llvm.load %6 : !llvm.ptr -> i64
      %37 = arith.extsi %36 : i64 to i128
      %38 = llvm.load %6 : !llvm.ptr -> i64
      %39 = arith.extsi %38 : i64 to i128
      %41 = arith.trunci %37 : i128 to i64
      %42 = arith.trunci %39 : i128 to i64
      %40 = arith.muli %41, %42 : i64
      %43 = arith.extsi %arg2 : i64 to i128
      %45 = arith.trunci %43 : i128 to i64
      %44 = arith.remsi %40, %45 : i64
      llvm.store %44, %6 : i64, !llvm.ptr
      %46 = llvm.load %14 : !llvm.ptr -> i64
      %47 = arith.constant 2 : i32
      %49 = arith.extsi %47 : i32 to i64
      %48 = arith.divsi %46, %49 : i64
      llvm.store %48, %14 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %50 = llvm.load %3 : !llvm.ptr -> i64
    func.return %50 : i64
  }
  func.func @modinv(%arg0: i64, %arg1: i64) -> i64 {
    %51 = arith.remsi %arg0, %arg1 : i64
    %52 = llvm.mlir.constant(1 : i64) : i64
    %53 = llvm.alloca %52 x i64 : (i64) -> !llvm.ptr
    llvm.store %51, %53 : i64, !llvm.ptr
    %54 = llvm.load %53 : !llvm.ptr -> i64
    %55 = arith.constant 0 : i32
    %57 = arith.extsi %55 : i32 to i64
    %56 = arith.cmpi slt, %54, %57 : i64
    cf.cond_br %56, ^bb9, ^bb10
    ^bb9:
      %58 = llvm.load %53 : !llvm.ptr -> i64
      %59 = arith.addi %58, %arg1 : i64
      llvm.store %59, %53 : i64, !llvm.ptr
      cf.br ^bb11
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %60 = llvm.mlir.constant(1 : i64) : i64
    %61 = llvm.alloca %60 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %61 : i64, !llvm.ptr
    %62 = arith.constant 1 : i32
    %63 = arith.extsi %62 : i32 to i64
    %64 = llvm.mlir.constant(1 : i64) : i64
    %65 = llvm.alloca %64 x i64 : (i64) -> !llvm.ptr
    llvm.store %63, %65 : i64, !llvm.ptr
    %66 = arith.constant 0 : i32
    %67 = arith.extsi %66 : i32 to i64
    %68 = llvm.mlir.constant(1 : i64) : i64
    %69 = llvm.alloca %68 x i64 : (i64) -> !llvm.ptr
    llvm.store %67, %69 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %70 = llvm.load %61 : !llvm.ptr -> i64
    %71 = arith.constant 0 : i32
    %73 = arith.extsi %71 : i32 to i64
    %72 = arith.cmpi ne, %70, %73 : i64
    cf.cond_br %72, ^bb13, ^bb14
    ^bb13:
      %74 = llvm.load %53 : !llvm.ptr -> i64
      %75 = llvm.load %61 : !llvm.ptr -> i64
      %76 = arith.divsi %74, %75 : i64
      %77 = llvm.load %53 : !llvm.ptr -> i64
      %78 = llvm.load %61 : !llvm.ptr -> i64
      %79 = arith.muli %76, %78 : i64
      %80 = arith.subi %77, %79 : i64
      %81 = llvm.load %61 : !llvm.ptr -> i64
      llvm.store %81, %53 : i64, !llvm.ptr
      llvm.store %80, %61 : i64, !llvm.ptr
      %82 = llvm.load %65 : !llvm.ptr -> i64
      %83 = llvm.load %69 : !llvm.ptr -> i64
      %84 = arith.muli %76, %83 : i64
      %85 = arith.subi %82, %84 : i64
      %86 = llvm.load %69 : !llvm.ptr -> i64
      llvm.store %86, %65 : i64, !llvm.ptr
      llvm.store %85, %69 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %87 = llvm.load %65 : !llvm.ptr -> i64
    %88 = arith.constant 0 : i32
    %90 = arith.extsi %88 : i32 to i64
    %89 = arith.cmpi slt, %87, %90 : i64
    cf.cond_br %89, ^bb15, ^bb16
    ^bb15:
      %91 = llvm.load %65 : !llvm.ptr -> i64
      %92 = arith.addi %91, %arg1 : i64
      llvm.store %92, %65 : i64, !llvm.ptr
      cf.br ^bb17
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %93 = llvm.load %65 : !llvm.ptr -> i64
    func.return %93 : i64
  }
  func.func @legendre(%arg0: i64, %arg1: i64) -> i64 {
    %95 = arith.constant 1 : i32
    %97 = arith.extsi %95 : i32 to i64
    %96 = arith.subi %arg1, %97 : i64
    %98 = arith.constant 2 : i32
    %100 = arith.extsi %98 : i32 to i64
    %99 = arith.divsi %96, %100 : i64
    %94 = func.call @modpow(%arg0, %99, %arg1) : (i64, i64, i64) -> i64
    %101 = arith.constant 1 : i32
    %103 = arith.extsi %101 : i32 to i64
    %102 = arith.subi %arg1, %103 : i64
    %104 = arith.cmpi eq, %94, %102 : i64
    cf.cond_br %104, ^bb18, ^bb19
    ^bb18:
      %105 = arith.constant 0 : i32
      %106 = arith.constant 1 : i32
      %107 = arith.subi %105, %106 : i32
      %108 = arith.extsi %107 : i32 to i64
      func.return %108 : i64
    ^bb19:
      cf.br ^bb20
    ^bb20:
    func.return %94 : i64
  }
  func.func @tonelli(%arg0: i64, %arg1: i64) -> i64 {
    %109 = arith.remsi %arg0, %arg1 : i64
    %110 = llvm.mlir.constant(1 : i64) : i64
    %111 = llvm.alloca %110 x i64 : (i64) -> !llvm.ptr
    llvm.store %109, %111 : i64, !llvm.ptr
    %112 = llvm.load %111 : !llvm.ptr -> i64
    %113 = arith.constant 0 : i32
    %115 = arith.extsi %113 : i32 to i64
    %114 = arith.cmpi slt, %112, %115 : i64
    cf.cond_br %114, ^bb21, ^bb22
    ^bb21:
      %116 = llvm.load %111 : !llvm.ptr -> i64
      %117 = arith.addi %116, %arg1 : i64
      llvm.store %117, %111 : i64, !llvm.ptr
      cf.br ^bb23
    ^bb22:
      cf.br ^bb23
    ^bb23:
    %119 = llvm.load %111 : !llvm.ptr -> i64
    %118 = func.call @legendre(%119, %arg1) : (i64, i64) -> i64
    %120 = arith.constant 1 : i32
    %122 = arith.extsi %120 : i32 to i64
    %121 = arith.cmpi ne, %118, %122 : i64
    cf.cond_br %121, ^bb24, ^bb25
    ^bb24:
      %123 = arith.constant 0 : i32
      %124 = arith.extsi %123 : i32 to i64
      func.return %124 : i64
    ^bb25:
      cf.br ^bb26
    ^bb26:
    %125 = llvm.load %111 : !llvm.ptr -> i64
    %126 = arith.constant 0 : i32
    %128 = arith.extsi %126 : i32 to i64
    %127 = arith.cmpi eq, %125, %128 : i64
    cf.cond_br %127, ^bb27, ^bb28
    ^bb27:
      %129 = arith.constant 0 : i32
      %130 = arith.extsi %129 : i32 to i64
      func.return %130 : i64
    ^bb28:
      cf.br ^bb29
    ^bb29:
    %131 = arith.constant 2 : i32
    %133 = arith.extsi %131 : i32 to i64
    %132 = arith.cmpi eq, %arg1, %133 : i64
    cf.cond_br %132, ^bb30, ^bb31
    ^bb30:
      %134 = llvm.load %111 : !llvm.ptr -> i64
      func.return %134 : i64
    ^bb31:
      cf.br ^bb32
    ^bb32:
    %135 = arith.constant 4 : i32
    %137 = arith.extsi %135 : i32 to i64
    %136 = arith.remsi %arg1, %137 : i64
    %138 = arith.constant 3 : i32
    %140 = arith.extsi %138 : i32 to i64
    %139 = arith.cmpi eq, %136, %140 : i64
    cf.cond_br %139, ^bb33, ^bb34
    ^bb33:
      %142 = llvm.load %111 : !llvm.ptr -> i64
      %143 = arith.constant 1 : i32
      %145 = arith.extsi %143 : i32 to i64
      %144 = arith.addi %arg1, %145 : i64
      %146 = arith.constant 4 : i32
      %148 = arith.extsi %146 : i32 to i64
      %147 = arith.divsi %144, %148 : i64
      %141 = func.call @modpow(%142, %147, %arg1) : (i64, i64, i64) -> i64
      func.return %141 : i64
    ^bb34:
      cf.br ^bb35
    ^bb35:
    %149 = arith.constant 1 : i32
    %151 = arith.extsi %149 : i32 to i64
    %150 = arith.subi %arg1, %151 : i64
    %152 = llvm.mlir.constant(1 : i64) : i64
    %153 = llvm.alloca %152 x i64 : (i64) -> !llvm.ptr
    llvm.store %150, %153 : i64, !llvm.ptr
    %154 = arith.constant 0 : i32
    %155 = arith.extsi %154 : i32 to i64
    %156 = llvm.mlir.constant(1 : i64) : i64
    %157 = llvm.alloca %156 x i64 : (i64) -> !llvm.ptr
    llvm.store %155, %157 : i64, !llvm.ptr
    cf.br ^bb36
    ^bb36:
    %158 = llvm.load %153 : !llvm.ptr -> i64
    %159 = arith.constant 2 : i32
    %161 = arith.extsi %159 : i32 to i64
    %160 = arith.remsi %158, %161 : i64
    %162 = arith.constant 0 : i32
    %164 = arith.extsi %162 : i32 to i64
    %163 = arith.cmpi eq, %160, %164 : i64
    cf.cond_br %163, ^bb37, ^bb38
    ^bb37:
      %165 = llvm.load %153 : !llvm.ptr -> i64
      %166 = arith.constant 2 : i32
      %168 = arith.extsi %166 : i32 to i64
      %167 = arith.divsi %165, %168 : i64
      llvm.store %167, %153 : i64, !llvm.ptr
      %169 = llvm.load %157 : !llvm.ptr -> i64
      %170 = arith.constant 1 : i32
      %172 = arith.extsi %170 : i32 to i64
      %171 = arith.addi %169, %172 : i64
      llvm.store %171, %157 : i64, !llvm.ptr
      cf.br ^bb36
    ^bb38:
    %173 = arith.constant 2 : i32
    %174 = arith.extsi %173 : i32 to i64
    %175 = llvm.mlir.constant(1 : i64) : i64
    %176 = llvm.alloca %175 x i64 : (i64) -> !llvm.ptr
    llvm.store %174, %176 : i64, !llvm.ptr
    cf.br ^bb39
    ^bb39:
    %178 = llvm.load %176 : !llvm.ptr -> i64
    %177 = func.call @legendre(%178, %arg1) : (i64, i64) -> i64
    %179 = arith.constant 0 : i32
    %180 = arith.constant 1 : i32
    %181 = arith.subi %179, %180 : i32
    %183 = arith.extsi %181 : i32 to i64
    %182 = arith.cmpi ne, %177, %183 : i64
    cf.cond_br %182, ^bb40, ^bb41
    ^bb40:
      %184 = llvm.load %176 : !llvm.ptr -> i64
      %185 = arith.constant 1 : i32
      %187 = arith.extsi %185 : i32 to i64
      %186 = arith.addi %184, %187 : i64
      llvm.store %186, %176 : i64, !llvm.ptr
      cf.br ^bb39
    ^bb41:
    %189 = llvm.load %111 : !llvm.ptr -> i64
    %190 = llvm.load %153 : !llvm.ptr -> i64
    %191 = arith.constant 1 : i32
    %193 = arith.extsi %191 : i32 to i64
    %192 = arith.addi %190, %193 : i64
    %194 = arith.constant 2 : i32
    %196 = arith.extsi %194 : i32 to i64
    %195 = arith.divsi %192, %196 : i64
    %188 = func.call @modpow(%189, %195, %arg1) : (i64, i64, i64) -> i64
    %197 = llvm.mlir.constant(1 : i64) : i64
    %198 = llvm.alloca %197 x i64 : (i64) -> !llvm.ptr
    llvm.store %188, %198 : i64, !llvm.ptr
    %200 = llvm.load %111 : !llvm.ptr -> i64
    %201 = llvm.load %153 : !llvm.ptr -> i64
    %199 = func.call @modpow(%200, %201, %arg1) : (i64, i64, i64) -> i64
    %202 = llvm.mlir.constant(1 : i64) : i64
    %203 = llvm.alloca %202 x i64 : (i64) -> !llvm.ptr
    llvm.store %199, %203 : i64, !llvm.ptr
    %205 = llvm.load %176 : !llvm.ptr -> i64
    %206 = llvm.load %153 : !llvm.ptr -> i64
    %204 = func.call @modpow(%205, %206, %arg1) : (i64, i64, i64) -> i64
    %207 = llvm.mlir.constant(1 : i64) : i64
    %208 = llvm.alloca %207 x i64 : (i64) -> !llvm.ptr
    llvm.store %204, %208 : i64, !llvm.ptr
    %209 = llvm.load %157 : !llvm.ptr -> i64
    %210 = llvm.mlir.constant(1 : i64) : i64
    %211 = llvm.alloca %210 x i64 : (i64) -> !llvm.ptr
    llvm.store %209, %211 : i64, !llvm.ptr
    cf.br ^bb42
    ^bb42:
    %212 = arith.constant 1 : i1
    cf.cond_br %212, ^bb43, ^bb44
    ^bb43:
      %213 = llvm.load %203 : !llvm.ptr -> i64
      %214 = llvm.mlir.constant(1 : i64) : i64
      %215 = llvm.alloca %214 x i64 : (i64) -> !llvm.ptr
      llvm.store %213, %215 : i64, !llvm.ptr
      %216 = arith.constant 0 : i32
      %217 = arith.extsi %216 : i32 to i64
      %218 = llvm.mlir.constant(1 : i64) : i64
      %219 = llvm.alloca %218 x i64 : (i64) -> !llvm.ptr
      llvm.store %217, %219 : i64, !llvm.ptr
      cf.br ^bb45
      ^bb45:
      %220 = llvm.load %219 : !llvm.ptr -> i64
      %221 = llvm.load %211 : !llvm.ptr -> i64
      %222 = arith.cmpi slt, %220, %221 : i64
      cf.cond_br %222, ^bb46, ^bb47
      ^bb46:
        %223 = llvm.load %215 : !llvm.ptr -> i64
        %224 = arith.constant 1 : i32
        %226 = arith.extsi %224 : i32 to i64
        %225 = arith.cmpi eq, %223, %226 : i64
        cf.cond_br %225, ^bb48, ^bb49
        ^bb48:
          cf.br ^bb47
        ^bb49:
          cf.br ^bb50
        ^bb50:
        %227 = llvm.load %215 : !llvm.ptr -> i64
        %228 = arith.extsi %227 : i64 to i128
        %229 = llvm.load %215 : !llvm.ptr -> i64
        %230 = arith.extsi %229 : i64 to i128
        %232 = arith.trunci %228 : i128 to i64
        %233 = arith.trunci %230 : i128 to i64
        %231 = arith.muli %232, %233 : i64
        %234 = arith.extsi %arg1 : i64 to i128
        %236 = arith.trunci %234 : i128 to i64
        %235 = arith.remsi %231, %236 : i64
        llvm.store %235, %215 : i64, !llvm.ptr
        %237 = llvm.load %219 : !llvm.ptr -> i64
        %238 = arith.constant 1 : i32
        %240 = arith.extsi %238 : i32 to i64
        %239 = arith.addi %237, %240 : i64
        llvm.store %239, %219 : i64, !llvm.ptr
        cf.br ^bb45
      ^bb47:
      %241 = llvm.load %219 : !llvm.ptr -> i64
      %242 = arith.constant 0 : i32
      %244 = arith.extsi %242 : i32 to i64
      %243 = arith.cmpi eq, %241, %244 : i64
      cf.cond_br %243, ^bb51, ^bb52
      ^bb51:
        %245 = llvm.load %198 : !llvm.ptr -> i64
        func.return %245 : i64
      ^bb52:
        cf.br ^bb53
      ^bb53:
      %246 = llvm.load %208 : !llvm.ptr -> i64
      %247 = llvm.mlir.constant(1 : i64) : i64
      %248 = llvm.alloca %247 x i64 : (i64) -> !llvm.ptr
      llvm.store %246, %248 : i64, !llvm.ptr
      %249 = arith.constant 0 : i32
      %250 = arith.extsi %249 : i32 to i64
      %251 = llvm.mlir.constant(1 : i64) : i64
      %252 = llvm.alloca %251 x i64 : (i64) -> !llvm.ptr
      llvm.store %250, %252 : i64, !llvm.ptr
      cf.br ^bb54
      ^bb54:
      %253 = llvm.load %252 : !llvm.ptr -> i64
      %254 = llvm.load %211 : !llvm.ptr -> i64
      %255 = llvm.load %219 : !llvm.ptr -> i64
      %256 = arith.subi %254, %255 : i64
      %257 = arith.constant 1 : i32
      %259 = arith.extsi %257 : i32 to i64
      %258 = arith.subi %256, %259 : i64
      %260 = arith.cmpi slt, %253, %258 : i64
      cf.cond_br %260, ^bb55, ^bb56
      ^bb55:
        %261 = llvm.load %248 : !llvm.ptr -> i64
        %262 = arith.extsi %261 : i64 to i128
        %263 = llvm.load %248 : !llvm.ptr -> i64
        %264 = arith.extsi %263 : i64 to i128
        %266 = arith.trunci %262 : i128 to i64
        %267 = arith.trunci %264 : i128 to i64
        %265 = arith.muli %266, %267 : i64
        %268 = arith.extsi %arg1 : i64 to i128
        %270 = arith.trunci %268 : i128 to i64
        %269 = arith.remsi %265, %270 : i64
        llvm.store %269, %248 : i64, !llvm.ptr
        %271 = llvm.load %252 : !llvm.ptr -> i64
        %272 = arith.constant 1 : i32
        %274 = arith.extsi %272 : i32 to i64
        %273 = arith.addi %271, %274 : i64
        llvm.store %273, %252 : i64, !llvm.ptr
        cf.br ^bb54
      ^bb56:
      %275 = llvm.load %248 : !llvm.ptr -> i64
      %276 = arith.extsi %275 : i64 to i128
      %277 = llvm.load %248 : !llvm.ptr -> i64
      %278 = arith.extsi %277 : i64 to i128
      %280 = arith.trunci %276 : i128 to i64
      %281 = arith.trunci %278 : i128 to i64
      %279 = arith.muli %280, %281 : i64
      %282 = arith.extsi %arg1 : i64 to i128
      %284 = arith.trunci %282 : i128 to i64
      %283 = arith.remsi %279, %284 : i64
      llvm.store %283, %208 : i64, !llvm.ptr
      %285 = llvm.load %198 : !llvm.ptr -> i64
      %286 = arith.extsi %285 : i64 to i128
      %287 = llvm.load %248 : !llvm.ptr -> i64
      %288 = arith.extsi %287 : i64 to i128
      %290 = arith.trunci %286 : i128 to i64
      %291 = arith.trunci %288 : i128 to i64
      %289 = arith.muli %290, %291 : i64
      %292 = arith.extsi %arg1 : i64 to i128
      %294 = arith.trunci %292 : i128 to i64
      %293 = arith.remsi %289, %294 : i64
      llvm.store %293, %198 : i64, !llvm.ptr
      %295 = llvm.load %203 : !llvm.ptr -> i64
      %296 = arith.extsi %295 : i64 to i128
      %297 = llvm.load %208 : !llvm.ptr -> i64
      %298 = arith.extsi %297 : i64 to i128
      %300 = arith.trunci %296 : i128 to i64
      %301 = arith.trunci %298 : i128 to i64
      %299 = arith.muli %300, %301 : i64
      %302 = arith.extsi %arg1 : i64 to i128
      %304 = arith.trunci %302 : i128 to i64
      %303 = arith.remsi %299, %304 : i64
      llvm.store %303, %203 : i64, !llvm.ptr
      %305 = llvm.load %219 : !llvm.ptr -> i64
      llvm.store %305, %211 : i64, !llvm.ptr
      cf.br ^bb42
    ^bb44:
    %306 = arith.constant 0 : i32
    %307 = arith.extsi %306 : i32 to i64
    func.return %307 : i64
  }
  func.func @R_p(%arg0: i64) -> i64 {
    %308 = arith.constant 3 : i32
    %310 = arith.extsi %308 : i32 to i64
    %309 = arith.cmpi eq, %arg0, %310 : i64
    cf.cond_br %309, ^bb57, ^bb58
    ^bb57:
      %311 = arith.constant 5 : i32
      %312 = arith.extsi %311 : i32 to i64
      func.return %312 : i64
    ^bb58:
      cf.br ^bb59
    ^bb59:
    %314 = arith.constant 13 : i32
    %315 = arith.extsi %314 : i32 to i64
    %313 = func.call @tonelli(%315, %arg0) : (i64, i64) -> i64
    %316 = arith.constant 0 : i32
    %318 = arith.extsi %316 : i32 to i64
    %317 = arith.cmpi eq, %313, %318 : i64
    cf.cond_br %317, ^bb60, ^bb61
    ^bb60:
      %319 = arith.constant 0 : i32
      %320 = arith.extsi %319 : i32 to i64
      func.return %320 : i64
    ^bb61:
      cf.br ^bb62
    ^bb62:
    %321 = arith.constant 2 : i32
    %323 = arith.extsi %321 : i32 to i64
    %322 = arith.muli %323, %313 : i64
    %324 = arith.remsi %322, %arg0 : i64
    %325 = arith.constant 0 : i32
    %327 = arith.extsi %325 : i32 to i64
    %326 = arith.cmpi eq, %324, %327 : i64
    cf.cond_br %326, ^bb63, ^bb64
    ^bb63:
      %328 = arith.constant 0 : i32
      %329 = arith.extsi %328 : i32 to i64
      func.return %329 : i64
    ^bb64:
      cf.br ^bb65
    ^bb65:
    %330 = arith.muli %313, %313 : i64
    %331 = arith.constant 13 : i32
    %333 = arith.extsi %331 : i32 to i64
    %332 = arith.subi %330, %333 : i64
    %334 = arith.divsi %332, %arg0 : i64
    %335 = func.call @modinv(%324, %arg0) : (i64, i64) -> i64
    %336 = arith.constant 0 : i32
    %337 = arith.extsi %335 : i64 to i128
    %338 = arith.extsi %334 : i64 to i128
    %340 = arith.trunci %337 : i128 to i64
    %341 = arith.trunci %338 : i128 to i64
    %339 = arith.muli %340, %341 : i64
    %342 = arith.extsi %arg0 : i64 to i128
    %344 = arith.trunci %342 : i128 to i64
    %343 = arith.remsi %339, %344 : i64
    %346 = arith.extsi %336 : i32 to i64
    %345 = arith.subi %346, %343 : i64
    %347 = llvm.mlir.constant(1 : i64) : i64
    %348 = llvm.alloca %347 x i64 : (i64) -> !llvm.ptr
    llvm.store %345, %348 : i64, !llvm.ptr
    %349 = llvm.load %348 : !llvm.ptr -> i64
    %350 = arith.remsi %349, %arg0 : i64
    llvm.store %350, %348 : i64, !llvm.ptr
    %351 = llvm.load %348 : !llvm.ptr -> i64
    %352 = arith.constant 0 : i32
    %354 = arith.extsi %352 : i32 to i64
    %353 = arith.cmpi slt, %351, %354 : i64
    cf.cond_br %353, ^bb66, ^bb67
    ^bb66:
      %355 = llvm.load %348 : !llvm.ptr -> i64
      %356 = arith.addi %355, %arg0 : i64
      llvm.store %356, %348 : i64, !llvm.ptr
      cf.br ^bb68
    ^bb67:
      cf.br ^bb68
    ^bb68:
    %357 = arith.muli %arg0, %arg0 : i64
    %358 = llvm.load %348 : !llvm.ptr -> i64
    %359 = arith.muli %358, %arg0 : i64
    %360 = arith.addi %313, %359 : i64
    %361 = arith.remsi %360, %357 : i64
    %362 = llvm.mlir.constant(1 : i64) : i64
    %363 = llvm.alloca %362 x i64 : (i64) -> !llvm.ptr
    llvm.store %361, %363 : i64, !llvm.ptr
    %364 = llvm.load %363 : !llvm.ptr -> i64
    %365 = arith.constant 0 : i32
    %367 = arith.extsi %365 : i32 to i64
    %366 = arith.cmpi slt, %364, %367 : i64
    cf.cond_br %366, ^bb69, ^bb70
    ^bb69:
      %368 = llvm.load %363 : !llvm.ptr -> i64
      %369 = arith.addi %368, %357 : i64
      llvm.store %369, %363 : i64, !llvm.ptr
      cf.br ^bb71
    ^bb70:
      cf.br ^bb71
    ^bb71:
    %370 = llvm.load %363 : !llvm.ptr -> i64
    %371 = arith.constant 2 : i32
    %373 = arith.extsi %371 : i32 to i64
    %372 = arith.remsi %370, %373 : i64
    %374 = arith.constant 0 : i32
    %376 = arith.extsi %374 : i32 to i64
    %375 = arith.cmpi ne, %372, %376 : i64
    cf.cond_br %375, ^bb72, ^bb73
    ^bb72:
      %377 = llvm.load %363 : !llvm.ptr -> i64
      %378 = arith.constant 3 : i32
      %380 = arith.extsi %378 : i32 to i64
      %379 = arith.addi %377, %380 : i64
      %381 = arith.constant 2 : i32
      %383 = arith.extsi %381 : i32 to i64
      %382 = arith.divsi %379, %383 : i64
      func.return %382 : i64
    ^bb73:
      cf.br ^bb74
    ^bb74:
    %384 = llvm.load %363 : !llvm.ptr -> i64
    %385 = arith.subi %357, %384 : i64
    %386 = arith.constant 3 : i32
    %388 = arith.extsi %386 : i32 to i64
    %387 = arith.addi %385, %388 : i64
    %389 = arith.constant 2 : i32
    %391 = arith.extsi %389 : i32 to i64
    %390 = arith.divsi %387, %391 : i64
    func.return %390 : i64
  }
  func.func @main() -> i32 {
    %392 = arith.constant 10000000 : i32
    %393 = arith.extsi %392 : i32 to i64
    %395 = arith.constant 1 : i32
    %397 = arith.extsi %395 : i32 to i64
    %396 = arith.addi %393, %397 : i64
    %398 = arith.constant 1 : i32
    %399 = arith.extsi %398 : i32 to i64
    %394 = func.call @calloc(%396, %399) : (i64, i64) -> !llvm.ptr
    %400 = llvm.mlir.zero : !llvm.ptr
    %401 = llvm.icmp "eq" %394, %400 : !llvm.ptr
    cf.cond_br %401, ^bb75, ^bb76
    ^bb75:
      %402 = arith.constant 1 : i32
      func.return %402 : i32
    ^bb76:
      cf.br ^bb77
    ^bb77:
    %403 = arith.constant 0 : i32
    %404 = arith.extsi %403 : i32 to i64
    %405 = llvm.mlir.constant(1 : i64) : i64
    %406 = llvm.alloca %405 x i64 : (i64) -> !llvm.ptr
    llvm.store %404, %406 : i64, !llvm.ptr
    cf.br ^bb78
    ^bb78:
    %407 = llvm.load %406 : !llvm.ptr -> i64
    %408 = arith.cmpi sle, %407, %393 : i64
    cf.cond_br %408, ^bb79, ^bb80
    ^bb79:
      %409 = arith.constant 1 : i32
      %410 = llvm.load %406 : !llvm.ptr -> i64
      %411 = arith.trunci %409 : i32 to i8
      %412 = llvm.getelementptr %394[%410] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %411, %412 : i8, !llvm.ptr
      %413 = llvm.load %406 : !llvm.ptr -> i64
      %414 = arith.constant 1 : i32
      %416 = arith.extsi %414 : i32 to i64
      %415 = arith.addi %413, %416 : i64
      llvm.store %415, %406 : i64, !llvm.ptr
      cf.br ^bb78
    ^bb80:
    %417 = arith.constant 0 : i32
    %418 = arith.constant 0 : i32
    %419 = arith.trunci %417 : i32 to i8
    %420 = arith.extsi %418 : i32 to i64
    %421 = llvm.getelementptr %394[%420] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %419, %421 : i8, !llvm.ptr
    %422 = arith.constant 0 : i32
    %423 = arith.constant 1 : i32
    %424 = arith.trunci %422 : i32 to i8
    %425 = arith.extsi %423 : i32 to i64
    %426 = llvm.getelementptr %394[%425] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %424, %426 : i8, !llvm.ptr
    %427 = arith.constant 2 : i32
    %428 = arith.extsi %427 : i32 to i64
    llvm.store %428, %406 : i64, !llvm.ptr
    cf.br ^bb81
    ^bb81:
    %429 = llvm.load %406 : !llvm.ptr -> i64
    %430 = llvm.load %406 : !llvm.ptr -> i64
    %431 = arith.muli %429, %430 : i64
    %432 = arith.cmpi sle, %431, %393 : i64
    cf.cond_br %432, ^bb82, ^bb83
    ^bb82:
      %434 = llvm.load %406 : !llvm.ptr -> i64
      %435 = llvm.getelementptr %394[%434] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %433 = llvm.load %435 : !llvm.ptr -> i8
      %436 = arith.constant 1 : i32
      %438 = arith.extsi %433 : i8 to i32
      %437 = arith.cmpi eq, %438, %436 : i32
      cf.cond_br %437, ^bb84, ^bb85
      ^bb84:
        %439 = llvm.load %406 : !llvm.ptr -> i64
        %440 = llvm.load %406 : !llvm.ptr -> i64
        %441 = arith.muli %439, %440 : i64
        %442 = llvm.mlir.constant(1 : i64) : i64
        %443 = llvm.alloca %442 x i64 : (i64) -> !llvm.ptr
        llvm.store %441, %443 : i64, !llvm.ptr
        cf.br ^bb87
        ^bb87:
        %444 = llvm.load %443 : !llvm.ptr -> i64
        %445 = arith.cmpi sle, %444, %393 : i64
        cf.cond_br %445, ^bb88, ^bb89
        ^bb88:
          %446 = arith.constant 0 : i32
          %447 = llvm.load %443 : !llvm.ptr -> i64
          %448 = arith.trunci %446 : i32 to i8
          %449 = llvm.getelementptr %394[%447] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %448, %449 : i8, !llvm.ptr
          %450 = llvm.load %443 : !llvm.ptr -> i64
          %451 = llvm.load %406 : !llvm.ptr -> i64
          %452 = arith.addi %450, %451 : i64
          llvm.store %452, %443 : i64, !llvm.ptr
          cf.br ^bb87
        ^bb89:
        cf.br ^bb86
      ^bb85:
        cf.br ^bb86
      ^bb86:
      %453 = llvm.load %406 : !llvm.ptr -> i64
      %454 = arith.constant 1 : i32
      %456 = arith.extsi %454 : i32 to i64
      %455 = arith.addi %453, %456 : i64
      llvm.store %455, %406 : i64, !llvm.ptr
      cf.br ^bb81
    ^bb83:
    %457 = arith.constant 0 : i32
    %458 = arith.extsi %457 : i32 to i64
    %459 = llvm.mlir.constant(1 : i64) : i64
    %460 = llvm.alloca %459 x i64 : (i64) -> !llvm.ptr
    llvm.store %458, %460 : i64, !llvm.ptr
    %461 = arith.constant 3 : i32
    %462 = arith.extsi %461 : i32 to i64
    %463 = llvm.mlir.constant(1 : i64) : i64
    %464 = llvm.alloca %463 x i64 : (i64) -> !llvm.ptr
    llvm.store %462, %464 : i64, !llvm.ptr
    cf.br ^bb90
    ^bb90:
    %465 = llvm.load %464 : !llvm.ptr -> i64
    %466 = arith.cmpi sle, %465, %393 : i64
    cf.cond_br %466, ^bb91, ^bb92
    ^bb91:
      %468 = llvm.load %464 : !llvm.ptr -> i64
      %469 = llvm.getelementptr %394[%468] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %467 = llvm.load %469 : !llvm.ptr -> i8
      %470 = arith.constant 1 : i32
      %472 = arith.extsi %467 : i8 to i32
      %471 = arith.cmpi eq, %472, %470 : i32
      cf.cond_br %471, ^bb93, ^bb94
      ^bb93:
        %474 = arith.constant 13 : i32
        %475 = llvm.load %464 : !llvm.ptr -> i64
        %476 = arith.extsi %474 : i32 to i64
        %473 = func.call @legendre(%476, %475) : (i64, i64) -> i64
        %477 = arith.constant 1 : i32
        %479 = arith.extsi %477 : i32 to i64
        %478 = arith.cmpi eq, %473, %479 : i64
        cf.cond_br %478, ^bb96, ^bb97
        ^bb96:
          %480 = llvm.load %460 : !llvm.ptr -> i64
          %482 = llvm.load %464 : !llvm.ptr -> i64
          %481 = func.call @R_p(%482) : (i64) -> i64
          %483 = arith.addi %480, %481 : i64
          llvm.store %483, %460 : i64, !llvm.ptr
          cf.br ^bb98
        ^bb97:
          cf.br ^bb98
        ^bb98:
        cf.br ^bb95
      ^bb94:
        cf.br ^bb95
      ^bb95:
      %484 = llvm.load %464 : !llvm.ptr -> i64
      %485 = arith.constant 2 : i32
      %487 = arith.extsi %485 : i32 to i64
      %486 = arith.addi %484, %487 : i64
      llvm.store %486, %464 : i64, !llvm.ptr
      cf.br ^bb90
    ^bb92:
    func.call @free(%394) : (!llvm.ptr) -> ()
    %489 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %490 = llvm.load %460 : !llvm.ptr -> i64
    %491 = llvm.call @printf(%489, %490) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %492 = arith.constant 0 : i32
    func.return %492 : i32
  }
}