Problem 952

Order Modulo Factorial. Multiplicative order of prime p modulo n!. Compute R(10^9+7, 10^7) mod 10^9+7.

Answer794394453
Output794394453
StatusPASS
Native helperno
Runtime1220 ms
Peak memory121024 KB
Time complexityO(n^2) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n)
Space complexityO(n^2)O(n)
ApproachFlow solutionBig-integer factorial
VerdictSuboptimal

Flow source

# Project Euler 952
# Order Modulo Factorial. Multiplicative order of prime p modulo n!.
# Compute R(10^9+7, 10^7) mod 10^9+7.

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

const P: i64 = 1000000007
const N: i64 = 10000000

function mod_pow_u64(a0: i64, e0: i64, mod: i64) -> i64 {
    let mut r: i64 = 1 % mod
    let mut a: i64 = a0 % mod
    let mut e: i64 = e0
    while e > 0 {
        if (e & 1) == 1 {
            r = ((r as i128 * (a as i128)) % (mod as i128)) as i64
        }
        a = ((a as i128 * (a as i128)) % (mod as i128)) as i64
        e = e >> 1
    }
    return r
}

function v_p_factorial(n0: i64, p: i64) -> i64 {
    let mut s: i64 = 0
    let mut n: i64 = n0
    while n > 0 {
        n = n / p
        s = s + n
    }
    return s
}

function v2_val(x0: i64) -> i64 {
    let mut c: i64 = 0
    let mut x: i64 = x0
    while (x & 1) == 0 {
        x = x >> 1
        c = c + 1
    }
    return c
}

function order_mod_2_power_exponent(a: i64, k: i64) -> i64 {
    if k <= 1 { return 0 }
    if k == 2 {
        if (a & 3) == 1 { return 0 }
        return 1
    }

    if (a & 3) == 1 {
        let t: i64 = k - v2_val(a - 1)
        if t > 0 { return t }
        return 0
    } else {
        let t: i64 = k - v2_val(a + 1)
        if t > 1 { return t }
        return 1
    }
}

# Global arrays
let mut spf: ptr<i32> = null
let mut primes: ptr<i32> = null
let mut num_primes: i64 = 0
let mut max_exp: ptr<i64> = null

function linear_sieve_spf(limit: i64) -> void {
    spf = calloc(limit + 1, 4)
    primes = calloc(700000, 4)

    num_primes = 0
    let mut i: i64 = 2
    while i <= limit {
        if spf[i] == 0 {
            spf[i] = i as i32
            primes[num_primes] = i as i32
            num_primes = num_primes + 1
        }
        let mut j: i64 = 0
        while j < num_primes {
            let p: i64 = primes[j] as i64
            let ip: i64 = i * p
            if ip > limit { break }
            spf[ip] = p as i32
            if p == (spf[i] as i64) { break }
            j = j + 1
        }
        i = i + 1
    }
}

# Factorize x using SPF. Returns number of (prime, exponent) pairs.
function factorize(x0: i64, fac_p: ptr<i64>, fac_e: ptr<i64>) -> i64 {
    let mut cnt: i64 = 0
    let mut x: i64 = x0
    while x > 1 {
        let p: i64 = spf[x] as i64
        let mut e: i64 = 0
        while x % p == 0 {
            x = x / p
            e = e + 1
        }
        fac_p[cnt] = p
        fac_e[cnt] = e
        cnt = cnt + 1
    }
    return cnt
}

function multiplicative_order_mod_prime(p: i64, q: i64) -> i64 {
    let m: i64 = q - 1
    let fac_p: ptr<i64> = calloc(32, 8)
    let fac_e: ptr<i64> = calloc(32, 8)
    let nfac: i64 = factorize(m, fac_p, fac_e)

    let mut r: i64 = m
    let base: i64 = p % q

    let mut i: i64 = 0
    while i < nfac {
        let f: i64 = fac_p[i]
        let mut e_rem: i64 = fac_e[i]
        while e_rem > 0 {
            let cand: i64 = r / f
            if mod_pow_u64(base, cand, q) == 1 {
                r = cand
                e_rem = e_rem - 1
            } else {
                break
            }
        }
        if e_rem > max_exp[f] {
            max_exp[f] = e_rem
        }
        i = i + 1
    }
    free(fac_p)
    free(fac_e)
    return r
}

function q_adic_valuation(p: i64, r: i64, q: i64, limit: i64) -> i64 {
    if limit <= 1 { return 1 }
    let mut s: i64 = 1
    let mut q_pow: i64 = q
    while s < limit {
        let q_pow_next: i64 = q_pow * q
        if mod_pow_u64(p, r, q_pow_next) != 1 { break }
        q_pow = q_pow_next
        s = s + 1
    }
    return s
}

function main() -> i32 {
    linear_sieve_spf(N)
    max_exp = calloc(N + 1, 8)

    # Handle q=2 separately
    let a2: i64 = v_p_factorial(N, 2)
    let t2: i64 = order_mod_2_power_exponent(P, a2)
    if t2 > max_exp[2] { max_exp[2] = t2 }

    # Odd primes
    let mut qi: i64 = 0
    while qi < num_primes {
        let q: i64 = primes[qi] as i64
        if q == 2 {
            qi = qi + 1
            continue
        }

        let a: i64 = v_p_factorial(N, q)
        let r0: i64 = multiplicative_order_mod_prime(P, q)
        let s: i64 = q_adic_valuation(P, r0, q, a)
        let extra: i64 = a - s
        if extra > 0 && extra > max_exp[q] {
            max_exp[q] = extra
        }
        qi = qi + 1
    }

    # Reconstruct order modulo P
    let mut res: i64 = 1 % P
    let mut qi2: i64 = 0
    while qi2 < num_primes {
        let q: i64 = primes[qi2] as i64
        let e: i64 = max_exp[q]
        if e != 0 {
            res = ((res as i128 * (mod_pow_u64(q, e, P) as i128)) % (P as i128)) as i64
        }
        qi2 = qi2 + 1
    }

    printf("%lld\n", res)
    free(spf)
    free(primes)
    free(max_exp)
    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 mod_pow_u64_i64_i64_i64(int64_t a0, int64_t e0, int64_t mod);
int64_t v_p_factorial_i64_i64(int64_t n0, int64_t p);
int64_t v2_val_i64(int64_t x0);
int64_t order_mod_2_power_exponent_i64_i64(int64_t a, int64_t k);
void linear_sieve_spf_i64(int64_t limit);
int64_t factorize_i64_ptr_i64_ptr_i64(int64_t x0, int64_t* fac_p, int64_t* fac_e);
int64_t multiplicative_order_mod_prime_i64_i64(int64_t p, int64_t q);
int64_t q_adic_valuation_i64_i64_i64_i64(int64_t p, int64_t r, int64_t q, int64_t limit);
int32_t main(void);

static const int64_t P = 1000000007;
static const int64_t N = 10000000;

/* Module statics */
static int32_t* spf = NULL;
static int32_t* primes = NULL;
static int64_t num_primes = 0;
static int64_t* max_exp = NULL;



int64_t mod_pow_u64_i64_i64_i64(int64_t a0, int64_t e0, int64_t mod) {
    int64_t r = FLOW_CHECKED_MOD((1), (mod));
    int64_t a = FLOW_CHECKED_MOD((a0), (mod));
    int64_t e = e0;
    while (e > 0) {
        if ((e & 1) == 1) {
            r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(a)))), (((__int128)(mod))))));
        }
        a = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(a)) * ((__int128)(a)))), (((__int128)(mod))))));
        e = FLOW_CHECKED_SHR((e), (1));
    }
    return r;
}

int64_t v_p_factorial_i64_i64(int64_t n0, int64_t p) {
    int64_t s = 0;
    int64_t n = n0;
    while (n > 0) {
        n = FLOW_CHECKED_DIV((n), (p));
        s = (s + n);
    }
    return s;
}

int64_t v2_val_i64(int64_t x0) {
    int64_t c = 0;
    int64_t x = x0;
    while ((x & 1) == 0) {
        x = FLOW_CHECKED_SHR((x), (1));
        c = (c + 1);
    }
    return c;
}

int64_t order_mod_2_power_exponent_i64_i64(int64_t a, int64_t k) {
    if (k <= 1) {
        return 0;
    }
    if (k == 2) {
        if ((a & 3) == 1) {
            return 0;
        }
        return 1;
    }
    if ((a & 3) == 1) {
        int64_t t = (k - v2_val_i64((a - 1)));
        if (t > 0) {
            return t;
        }
        return 0;
    } else {
        int64_t t = (k - v2_val_i64((a + 1)));
        if (t > 1) {
            return t;
        }
        return 1;
    }
}

void linear_sieve_spf_i64(int64_t limit) {
    spf = calloc((limit + 1), 4);
    primes = calloc(700000, 4);
    num_primes = 0;
    int64_t i = 2;
    while (i <= limit) {
        if (spf[i] == 0) {
            spf[i] = ((int32_t)(i));
            primes[num_primes] = ((int32_t)(i));
            num_primes = (num_primes + 1);
        }
        int64_t j = 0;
        while (j < num_primes) {
            int64_t p = ((int64_t)(primes[j]));
            int64_t ip = (i * p);
            if (ip > limit) {
                break;
            }
            spf[ip] = ((int32_t)(p));
            if (p == ((int64_t)(spf[i]))) {
                break;
            }
            j = (j + 1);
        }
        i = (i + 1);
    }
}

int64_t factorize_i64_ptr_i64_ptr_i64(int64_t x0, int64_t* fac_p, int64_t* fac_e) {
    int64_t cnt = 0;
    int64_t x = x0;
    while (x > 1) {
        int64_t p = ((int64_t)(spf[x]));
        int64_t e = 0;
        while (FLOW_CHECKED_MOD((x), (p)) == 0) {
            x = FLOW_CHECKED_DIV((x), (p));
            e = (e + 1);
        }
        fac_p[cnt] = p;
        fac_e[cnt] = e;
        cnt = (cnt + 1);
    }
    return cnt;
}

int64_t multiplicative_order_mod_prime_i64_i64(int64_t p, int64_t q) {
    int64_t m = (q - 1);
    int64_t* fac_p = (int64_t*)(calloc(32, 8));
    int64_t* fac_e = (int64_t*)(calloc(32, 8));
    int64_t nfac = factorize_i64_ptr_i64_ptr_i64(m, fac_p, fac_e);
    int64_t r = m;
    int64_t base = FLOW_CHECKED_MOD((p), (q));
    int64_t i = 0;
    while (i < nfac) {
        int64_t f = fac_p[i];
        int64_t e_rem = fac_e[i];
        while (e_rem > 0) {
            int64_t cand = FLOW_CHECKED_DIV((r), (f));
            if (mod_pow_u64_i64_i64_i64(base, cand, q) == 1) {
                r = cand;
                e_rem = (e_rem - 1);
            } else {
                break;
            }
        }
        if (e_rem > max_exp[f]) {
            max_exp[f] = e_rem;
        }
        i = (i + 1);
    }
    free(fac_p);
    free(fac_e);
    return r;
}

int64_t q_adic_valuation_i64_i64_i64_i64(int64_t p, int64_t r, int64_t q, int64_t limit) {
    if (limit <= 1) {
        return 1;
    }
    int64_t s = 1;
    int64_t q_pow = q;
    while (s < limit) {
        int64_t q_pow_next = (q_pow * q);
        if (mod_pow_u64_i64_i64_i64(p, r, q_pow_next) != 1) {
            break;
        }
        q_pow = q_pow_next;
        s = (s + 1);
    }
    return s;
}

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