Problem 875

Quadruple Congruence: Q(12345678) mod 1001961001. Pure Flow port of the native C solver.

Answer79645946
Output79645946
StatusPASS
Native helperno
Runtime220 ms
Peak memory100784 KB
Time complexityO(n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

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

Flow source

# Project Euler 875
# Quadruple Congruence: Q(12345678) mod 1001961001.
# Pure Flow port of the native C solver.

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

const MOD: i64 = 1001961001
const TARGET_N: i64 = 12345678

let mut spf: ptr<i32> = null
let mut primes: ptr<i32> = null
let mut nprimes: i32 = 0
let mut f_arr: ptr<i32> = null

function mulmod(a: i64, b: i64, m: i64) -> i64 {
    let r: i128 = ((a as i128) * (b as i128)) % (m as i128)
    return r as i64
}

function powmod(a0: i64, e0: i64, m: i64) -> i64 {
    let mut r: i64 = 1 % m
    let mut a: i64 = a0 % m
    let mut e: i64 = e0
    while e > 0 {
        if e % 2 == 1 { r = mulmod(r, a, m) }
        a = mulmod(a, a, m)
        e = e / 2
    }
    return r
}

function q_prime_power_mod(p: i64, k: i32, mod: i64) -> i64 {
    if p == 2 {
        if k == 1 { return 128 % mod }
        let r: i64 = 8 % mod
        let mut g: i64 = 0
        let mut cur: i64 = 1
        let mut i: i32 = 0
        while i < k - 1 {
            g = g + cur
            if g >= mod { g = g - mod }
            cur = mulmod(cur, r, mod)
            i = i + 1
        }
        let term1: i64 = powmod(2, 7 * (k as i64), mod)
        let term2: i64 = mulmod(powmod(2, 4 * (k as i64) + 3, mod), g, mod)
        return (term1 + term2) % mod
    }
    # Odd prime
    if k == 1 {
        let p2: i64 = mulmod(p, p, mod)
        let p3: i64 = mulmod(p2, p, mod)
        let p4: i64 = mulmod(p2, p2, mod)
        let p7: i64 = mulmod(p4, p3, mod)
        return (p7 + p4 + mod - p3) % mod
    }
    let r: i64 = powmod(p, 3, mod)
    let mut g: i64 = 0
    let mut cur: i64 = 1
    let mut i: i32 = 0
    while i < k {
        g = g + cur
        if g >= mod { g = g - mod }
        cur = mulmod(cur, r, mod)
        i = i + 1
    }
    let term1: i64 = powmod(p, 7 * (k as i64), mod)
    let termp: i64 = powmod(p, 4 * (k as i64) - 1, mod)
    let term2: i64 = mulmod(mulmod((p - 1) % mod, termp, mod), g, mod)
    return (term1 + term2) % mod
}

function smallest_prime_factors(n: i64) -> void {
    spf = calloc(n + 1, 4) as ptr<i32>
    primes = malloc((n + 1) * 4) as ptr<i32>
    nprimes = 0
    spf[1] = 1

    let mut i: i64 = 2
    while i <= n {
        if spf[i] == 0 {
            spf[i] = i as i32
            primes[nprimes] = i as i32
            nprimes = nprimes + 1
        }
        let si: i64 = spf[i] as i64
        let mut j: i32 = 0
        while j < nprimes {
            let p: i64 = primes[j] as i64
            let ip: i64 = i * p
            if ip > n { break }
            spf[ip] = p as i32
            if p == si { break }
            j = j + 1
        }
        i = i + 1
    }
}

function compute_Q(N: i64, mod: i64) -> i64 {
    smallest_prime_factors(N)

    f_arr = calloc(N + 1, 4) as ptr<i32>
    f_arr[1] = 1
    let mut total: i64 = 1 % mod

    let mut n: i64 = 2
    while n <= N {
        let p: i64 = spf[n] as i64
        let m: i64 = n / p
        let val: i64 = 0

        if (spf[m] as i64) != p {
            let qp: i64 = 0
            if m == 1 {
                qp = q_prime_power_mod(p, 1, mod)
            } else {
                qp = f_arr[p] as i64
            }
            val = mulmod(f_arr[m] as i64, qp, mod)
        } else {
            let mut ppow: i64 = p * p
            let mut k: i32 = 2
            let mut mm: i64 = m / p
            while mm > 1 && (spf[mm] as i64) == p {
                ppow = ppow * p
                k = k + 1
                mm = mm / p
            }
            let rest: i64 = mm
            let qp: i64 = 0
            if rest == 1 {
                qp = q_prime_power_mod(p, k, mod)
            } else {
                qp = f_arr[ppow] as i64
            }
            val = mulmod(f_arr[rest] as i64, qp, mod)
        }

        f_arr[n] = val as i32
        total = total + val
        if total >= mod { total = total - mod }
        n = n + 1
    }

    free(spf as ptr<void>)
    free(primes as ptr<void>)
    free(f_arr as ptr<void>)
    return total
}

function main() -> i32 {
    printf("%lld\n", compute_Q(TARGET_N, MOD))
    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 mulmod_i64_i64_i64(int64_t a, int64_t b, int64_t m);
int64_t powmod_i64_i64_i64(int64_t a0, int64_t e0, int64_t m);
int64_t q_prime_power_mod_i64_i32_i64(int64_t p, int32_t k, int64_t mod);
void smallest_prime_factors_i64(int64_t n);
int64_t compute_Q_i64_i64(int64_t N, int64_t mod);
int32_t main(void);

static const int64_t MOD = 1001961001;
static const int64_t TARGET_N = 12345678;

/* Module statics */
static int32_t* spf = NULL;
static int32_t* primes = NULL;
static int32_t nprimes = 0;
static int32_t* f_arr = NULL;





int64_t mulmod_i64_i64_i64(int64_t a, int64_t b, int64_t m) {
    __int128 r = FLOW_CHECKED_MOD(((((__int128)(a)) * ((__int128)(b)))), (((__int128)(m))));
    return ((int64_t)(r));
}

int64_t powmod_i64_i64_i64(int64_t a0, int64_t e0, int64_t m) {
    int64_t r = FLOW_CHECKED_MOD((1), (m));
    int64_t a = FLOW_CHECKED_MOD((a0), (m));
    int64_t e = e0;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            r = mulmod_i64_i64_i64(r, a, m);
        }
        a = mulmod_i64_i64_i64(a, a, m);
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return r;
}

int64_t q_prime_power_mod_i64_i32_i64(int64_t p, int32_t k, int64_t mod) {
    if (p == 2) {
        if (k == 1) {
            return FLOW_CHECKED_MOD((128), (mod));
        }
        int64_t r = FLOW_CHECKED_MOD((8), (mod));
        int64_t g = 0;
        int64_t cur = 1;
        int32_t i = 0;
        while (i < (k - 1)) {
            g = (g + cur);
            if (g >= mod) {
                g = (g - mod);
            }
            cur = mulmod_i64_i64_i64(cur, r, mod);
            i = (i + 1);
        }
        int64_t term1 = powmod_i64_i64_i64(2, (7 * ((int64_t)(k))), mod);
        int64_t term2 = mulmod_i64_i64_i64(powmod_i64_i64_i64(2, ((4 * ((int64_t)(k))) + 3), mod), g, mod);
        return FLOW_CHECKED_MOD(((term1 + term2)), (mod));
    }
    if (k == 1) {
        int64_t p2 = mulmod_i64_i64_i64(p, p, mod);
        int64_t p3 = mulmod_i64_i64_i64(p2, p, mod);
        int64_t p4 = mulmod_i64_i64_i64(p2, p2, mod);
        int64_t p7 = mulmod_i64_i64_i64(p4, p3, mod);
        return FLOW_CHECKED_MOD(((((p7 + p4) + mod) - p3)), (mod));
    }
    int64_t r = powmod_i64_i64_i64(p, 3, mod);
    int64_t g = 0;
    int64_t cur = 1;
    int32_t i = 0;
    while (i < k) {
        g = (g + cur);
        if (g >= mod) {
            g = (g - mod);
        }
        cur = mulmod_i64_i64_i64(cur, r, mod);
        i = (i + 1);
    }
    int64_t term1 = powmod_i64_i64_i64(p, (7 * ((int64_t)(k))), mod);
    int64_t termp = powmod_i64_i64_i64(p, ((4 * ((int64_t)(k))) - 1), mod);
    int64_t term2 = mulmod_i64_i64_i64(mulmod_i64_i64_i64(FLOW_CHECKED_MOD(((p - 1)), (mod)), termp, mod), g, mod);
    return FLOW_CHECKED_MOD(((term1 + term2)), (mod));
}

void smallest_prime_factors_i64(int64_t n) {
    spf = ((int32_t*)(calloc((n + 1), 4)));
    primes = ((int32_t*)(malloc(((n + 1) * 4))));
    nprimes = 0;
    spf[1] = 1;
    int64_t i = 2;
    while (i <= n) {
        if (spf[i] == 0) {
            spf[i] = ((int32_t)(i));
            primes[nprimes] = ((int32_t)(i));
            nprimes = (nprimes + 1);
        }
        int64_t si = ((int64_t)(spf[i]));
        int32_t j = 0;
        while (j < nprimes) {
            int64_t p = ((int64_t)(primes[j]));
            int64_t ip = (i * p);
            if (ip > n) {
                break;
            }
            spf[ip] = ((int32_t)(p));
            if (p == si) {
                break;
            }
            j = (j + 1);
        }
        i = (i + 1);
    }
}

int64_t compute_Q_i64_i64(int64_t N, int64_t mod) {
    smallest_prime_factors_i64(N);
    f_arr = ((int32_t*)(calloc((N + 1), 4)));
    f_arr[1] = 1;
    int64_t total = FLOW_CHECKED_MOD((1), (mod));
    int64_t n = 2;
    while (n <= N) {
        int64_t p = ((int64_t)(spf[n]));
        int64_t m = FLOW_CHECKED_DIV((n), (p));
        int64_t val = 0;
        if (((int64_t)(spf[m])) != p) {
            int64_t qp = 0;
            if (m == 1) {
                qp = q_prime_power_mod_i64_i32_i64(p, 1, mod);
            } else {
                qp = ((int64_t)(f_arr[p]));
            }
            val = mulmod_i64_i64_i64(((int64_t)(f_arr[m])), qp, mod);
        } else {
            int64_t ppow = (p * p);
            int32_t k = 2;
            int64_t mm = FLOW_CHECKED_DIV((m), (p));
            while ((mm > 1 && ((int64_t)(spf[mm])) == p)) {
                ppow = (ppow * p);
                k = (k + 1);
                mm = FLOW_CHECKED_DIV((mm), (p));
            }
            int64_t rest = mm;
            int64_t qp = 0;
            if (rest == 1) {
                qp = q_prime_power_mod_i64_i32_i64(p, k, mod);
            } else {
                qp = ((int64_t)(f_arr[ppow]));
            }
            val = mulmod_i64_i64_i64(((int64_t)(f_arr[rest])), qp, mod);
        }
        f_arr[n] = ((int32_t)(val));
        total = (total + val);
        if (total >= mod) {
            total = (total - mod);
        }
        n = (n + 1);
    }
    free(((void*)(spf)));
    free(((void*)(primes)));
    free(((void*)(f_arr)));
    return total;
}

int32_t main(void) {
    printf("%lld\n", compute_Q_i64_i64(TARGET_N, MOD));
    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 @malloc(i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func private @memset(!llvm.ptr, i64, i64) -> !llvm.ptr
  // Constant: MOD
  llvm.mlir.global internal constant @MOD(1001961001 : i64) : i64
  // Constant: TARGET_N
  llvm.mlir.global internal constant @TARGET_N(12345678 : i64) : i64
  // Module static: spf
  llvm.mlir.global internal @spf() {addr_space = 0 : i32} : !llvm.ptr {
    %0 = llvm.mlir.zero : !llvm.ptr
    llvm.return %0 : !llvm.ptr
  }
  // Module static: primes
  llvm.mlir.global internal @primes() {addr_space = 0 : i32} : !llvm.ptr {
    %1 = llvm.mlir.zero : !llvm.ptr
    llvm.return %1 : !llvm.ptr
  }
  // Module static: nprimes
  llvm.mlir.global internal @nprimes(0 : i32) : i32
  // Module static: f_arr
  llvm.mlir.global internal @f_arr() {addr_space = 0 : i32} : !llvm.ptr {
    %2 = llvm.mlir.zero : !llvm.ptr
    llvm.return %2 : !llvm.ptr
  }
  func.func @mulmod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %3 = arith.extsi %arg0 : i64 to i128
    %4 = arith.extsi %arg1 : i64 to i128
    %6 = arith.trunci %3 : i128 to i64
    %7 = arith.trunci %4 : i128 to i64
    %5 = arith.muli %6, %7 : i64
    %8 = arith.extsi %arg2 : i64 to i128
    %10 = arith.trunci %8 : i128 to i64
    %9 = arith.remsi %5, %10 : i64
    %11 = arith.extsi %9 : i64 to i128
    %12 = arith.trunci %11 : i128 to i64
    func.return %12 : i64
  }
  func.func @powmod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %13 = arith.constant 1 : i32
    %15 = arith.extsi %13 : i32 to i64
    %14 = arith.remsi %15, %arg2 : i64
    %16 = llvm.mlir.constant(1 : i64) : i64
    %17 = llvm.alloca %16 x i64 : (i64) -> !llvm.ptr
    llvm.store %14, %17 : i64, !llvm.ptr
    %18 = arith.remsi %arg0, %arg2 : i64
    %19 = llvm.mlir.constant(1 : i64) : i64
    %20 = llvm.alloca %19 x i64 : (i64) -> !llvm.ptr
    llvm.store %18, %20 : i64, !llvm.ptr
    %21 = llvm.mlir.constant(1 : i64) : i64
    %22 = llvm.alloca %21 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %22 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %23 = llvm.load %22 : !llvm.ptr -> i64
    %24 = arith.constant 0 : i32
    %26 = arith.extsi %24 : i32 to i64
    %25 = arith.cmpi sgt, %23, %26 : i64
    cf.cond_br %25, ^bb1, ^bb2
    ^bb1:
      %27 = llvm.load %22 : !llvm.ptr -> i64
      %28 = arith.constant 2 : i32
      %30 = arith.extsi %28 : i32 to i64
      %29 = arith.remsi %27, %30 : i64
      %31 = arith.constant 1 : i32
      %33 = arith.extsi %31 : i32 to i64
      %32 = arith.cmpi eq, %29, %33 : i64
      cf.cond_br %32, ^bb3, ^bb4
      ^bb3:
        %35 = llvm.load %17 : !llvm.ptr -> i64
        %36 = llvm.load %20 : !llvm.ptr -> i64
        %34 = func.call @mulmod(%35, %36, %arg2) : (i64, i64, i64) -> i64
        llvm.store %34, %17 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %38 = llvm.load %20 : !llvm.ptr -> i64
      %39 = llvm.load %20 : !llvm.ptr -> i64
      %37 = func.call @mulmod(%38, %39, %arg2) : (i64, i64, i64) -> i64
      llvm.store %37, %20 : i64, !llvm.ptr
      %40 = llvm.load %22 : !llvm.ptr -> i64
      %41 = arith.constant 2 : i32
      %43 = arith.extsi %41 : i32 to i64
      %42 = arith.divsi %40, %43 : i64
      llvm.store %42, %22 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %44 = llvm.load %17 : !llvm.ptr -> i64
    func.return %44 : i64
  }
  func.func @q_prime_power_mod(%arg0: i64, %arg1: i32, %arg2: i64) -> i64 {
    %45 = arith.constant 2 : i32
    %47 = arith.extsi %45 : i32 to i64
    %46 = arith.cmpi eq, %arg0, %47 : i64
    cf.cond_br %46, ^bb6, ^bb7
    ^bb6:
      %48 = arith.constant 1 : i32
      %49 = arith.cmpi eq, %arg1, %48 : i32
      cf.cond_br %49, ^bb9, ^bb10
      ^bb9:
        %50 = arith.constant 128 : i32
        %52 = arith.extsi %50 : i32 to i64
        %51 = arith.remsi %52, %arg2 : i64
        func.return %51 : i64
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %53 = arith.constant 8 : i32
      %55 = arith.extsi %53 : i32 to i64
      %54 = arith.remsi %55, %arg2 : i64
      %56 = arith.constant 0 : i32
      %57 = arith.extsi %56 : i32 to i64
      %58 = llvm.mlir.constant(1 : i64) : i64
      %59 = llvm.alloca %58 x i64 : (i64) -> !llvm.ptr
      llvm.store %57, %59 : i64, !llvm.ptr
      %60 = arith.constant 1 : i32
      %61 = arith.extsi %60 : i32 to i64
      %62 = llvm.mlir.constant(1 : i64) : i64
      %63 = llvm.alloca %62 x i64 : (i64) -> !llvm.ptr
      llvm.store %61, %63 : i64, !llvm.ptr
      %64 = arith.constant 0 : i32
      %65 = llvm.mlir.constant(1 : i64) : i64
      %66 = llvm.alloca %65 x i32 : (i64) -> !llvm.ptr
      llvm.store %64, %66 : i32, !llvm.ptr
      cf.br ^bb12
      ^bb12:
      %67 = llvm.load %66 : !llvm.ptr -> i32
      %68 = arith.constant 1 : i32
      %69 = arith.subi %arg1, %68 : i32
      %70 = arith.cmpi slt, %67, %69 : i32
      cf.cond_br %70, ^bb13, ^bb14
      ^bb13:
        %71 = llvm.load %59 : !llvm.ptr -> i64
        %72 = llvm.load %63 : !llvm.ptr -> i64
        %73 = arith.addi %71, %72 : i64
        llvm.store %73, %59 : i64, !llvm.ptr
        %74 = llvm.load %59 : !llvm.ptr -> i64
        %75 = arith.cmpi sge, %74, %arg2 : i64
        cf.cond_br %75, ^bb15, ^bb16
        ^bb15:
          %76 = llvm.load %59 : !llvm.ptr -> i64
          %77 = arith.subi %76, %arg2 : i64
          llvm.store %77, %59 : i64, !llvm.ptr
          cf.br ^bb17
        ^bb16:
          cf.br ^bb17
        ^bb17:
        %79 = llvm.load %63 : !llvm.ptr -> i64
        %78 = func.call @mulmod(%79, %54, %arg2) : (i64, i64, i64) -> i64
        llvm.store %78, %63 : i64, !llvm.ptr
        %80 = llvm.load %66 : !llvm.ptr -> i32
        %81 = arith.constant 1 : i32
        %82 = arith.addi %80, %81 : i32
        llvm.store %82, %66 : i32, !llvm.ptr
        cf.br ^bb12
      ^bb14:
      %84 = arith.constant 2 : i32
      %85 = arith.constant 7 : i32
      %86 = arith.extsi %arg1 : i32 to i64
      %88 = arith.extsi %85 : i32 to i64
      %87 = arith.muli %88, %86 : i64
      %89 = arith.extsi %84 : i32 to i64
      %83 = func.call @powmod(%89, %87, %arg2) : (i64, i64, i64) -> i64
      %92 = arith.constant 2 : i32
      %93 = arith.constant 4 : i32
      %94 = arith.extsi %arg1 : i32 to i64
      %96 = arith.extsi %93 : i32 to i64
      %95 = arith.muli %96, %94 : i64
      %97 = arith.constant 3 : i32
      %99 = arith.extsi %97 : i32 to i64
      %98 = arith.addi %95, %99 : i64
      %100 = arith.extsi %92 : i32 to i64
      %91 = func.call @powmod(%100, %98, %arg2) : (i64, i64, i64) -> i64
      %101 = llvm.load %59 : !llvm.ptr -> i64
      %90 = func.call @mulmod(%91, %101, %arg2) : (i64, i64, i64) -> i64
      %102 = arith.addi %83, %90 : i64
      %103 = arith.remsi %102, %arg2 : i64
      func.return %103 : i64
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %104 = arith.constant 1 : i32
    %105 = arith.cmpi eq, %arg1, %104 : i32
    cf.cond_br %105, ^bb18, ^bb19
    ^bb18:
      %106 = func.call @mulmod(%arg0, %arg0, %arg2) : (i64, i64, i64) -> i64
      %107 = func.call @mulmod(%106, %arg0, %arg2) : (i64, i64, i64) -> i64
      %108 = func.call @mulmod(%106, %106, %arg2) : (i64, i64, i64) -> i64
      %109 = func.call @mulmod(%108, %107, %arg2) : (i64, i64, i64) -> i64
      %110 = arith.addi %109, %108 : i64
      %111 = arith.addi %110, %arg2 : i64
      %112 = arith.subi %111, %107 : i64
      %113 = arith.remsi %112, %arg2 : i64
      func.return %113 : i64
    ^bb19:
      cf.br ^bb20
    ^bb20:
    %115 = arith.constant 3 : i32
    %116 = arith.extsi %115 : i32 to i64
    %114 = func.call @powmod(%arg0, %116, %arg2) : (i64, i64, i64) -> i64
    %117 = arith.constant 0 : i32
    %118 = arith.extsi %117 : i32 to i64
    %119 = llvm.mlir.constant(1 : i64) : i64
    %120 = llvm.alloca %119 x i64 : (i64) -> !llvm.ptr
    llvm.store %118, %120 : i64, !llvm.ptr
    %121 = arith.constant 1 : i32
    %122 = arith.extsi %121 : i32 to i64
    %123 = llvm.mlir.constant(1 : i64) : i64
    %124 = llvm.alloca %123 x i64 : (i64) -> !llvm.ptr
    llvm.store %122, %124 : i64, !llvm.ptr
    %125 = arith.constant 0 : i32
    %126 = llvm.mlir.constant(1 : i64) : i64
    %127 = llvm.alloca %126 x i32 : (i64) -> !llvm.ptr
    llvm.store %125, %127 : i32, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %128 = llvm.load %127 : !llvm.ptr -> i32
    %129 = arith.cmpi slt, %128, %arg1 : i32
    cf.cond_br %129, ^bb22, ^bb23
    ^bb22:
      %130 = llvm.load %120 : !llvm.ptr -> i64
      %131 = llvm.load %124 : !llvm.ptr -> i64
      %132 = arith.addi %130, %131 : i64
      llvm.store %132, %120 : i64, !llvm.ptr
      %133 = llvm.load %120 : !llvm.ptr -> i64
      %134 = arith.cmpi sge, %133, %arg2 : i64
      cf.cond_br %134, ^bb24, ^bb25
      ^bb24:
        %135 = llvm.load %120 : !llvm.ptr -> i64
        %136 = arith.subi %135, %arg2 : i64
        llvm.store %136, %120 : i64, !llvm.ptr
        cf.br ^bb26
      ^bb25:
        cf.br ^bb26
      ^bb26:
      %138 = llvm.load %124 : !llvm.ptr -> i64
      %137 = func.call @mulmod(%138, %114, %arg2) : (i64, i64, i64) -> i64
      llvm.store %137, %124 : i64, !llvm.ptr
      %139 = llvm.load %127 : !llvm.ptr -> i32
      %140 = arith.constant 1 : i32
      %141 = arith.addi %139, %140 : i32
      llvm.store %141, %127 : i32, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %143 = arith.constant 7 : i32
    %144 = arith.extsi %arg1 : i32 to i64
    %146 = arith.extsi %143 : i32 to i64
    %145 = arith.muli %146, %144 : i64
    %142 = func.call @powmod(%arg0, %145, %arg2) : (i64, i64, i64) -> i64
    %148 = arith.constant 4 : i32
    %149 = arith.extsi %arg1 : i32 to i64
    %151 = arith.extsi %148 : i32 to i64
    %150 = arith.muli %151, %149 : i64
    %152 = arith.constant 1 : i32
    %154 = arith.extsi %152 : i32 to i64
    %153 = arith.subi %150, %154 : i64
    %147 = func.call @powmod(%arg0, %153, %arg2) : (i64, i64, i64) -> i64
    %157 = arith.constant 1 : i32
    %159 = arith.extsi %157 : i32 to i64
    %158 = arith.subi %arg0, %159 : i64
    %160 = arith.remsi %158, %arg2 : i64
    %156 = func.call @mulmod(%160, %147, %arg2) : (i64, i64, i64) -> i64
    %161 = llvm.load %120 : !llvm.ptr -> i64
    %155 = func.call @mulmod(%156, %161, %arg2) : (i64, i64, i64) -> i64
    %162 = arith.addi %142, %155 : i64
    %163 = arith.remsi %162, %arg2 : i64
    func.return %163 : i64
  }
  func.func @smallest_prime_factors(%arg0: i64) -> () {
    %165 = arith.constant 1 : i32
    %167 = arith.extsi %165 : i32 to i64
    %166 = arith.addi %arg0, %167 : i64
    %168 = arith.constant 4 : i32
    %169 = arith.extsi %168 : i32 to i64
    %164 = func.call @calloc(%166, %169) : (i64, i64) -> !llvm.ptr
    %170 = llvm.mlir.addressof @spf : !llvm.ptr
    llvm.store %164, %170 : !llvm.ptr, !llvm.ptr
    %172 = arith.constant 1 : i32
    %174 = arith.extsi %172 : i32 to i64
    %173 = arith.addi %arg0, %174 : i64
    %175 = arith.constant 4 : i32
    %177 = arith.extsi %175 : i32 to i64
    %176 = arith.muli %173, %177 : i64
    %171 = func.call @malloc(%176) : (i64) -> !llvm.ptr
    %178 = llvm.mlir.addressof @primes : !llvm.ptr
    llvm.store %171, %178 : !llvm.ptr, !llvm.ptr
    %179 = arith.constant 0 : i32
    %180 = llvm.mlir.addressof @nprimes : !llvm.ptr
    llvm.store %179, %180 : i32, !llvm.ptr
    %181 = arith.constant 1 : i32
    %182 = llvm.mlir.addressof @spf : !llvm.ptr
    %183 = llvm.load %182 : !llvm.ptr -> !llvm.ptr
    %184 = arith.constant 1 : i32
    %185 = arith.extsi %184 : i32 to i64
    %186 = llvm.getelementptr %183[%185] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    llvm.store %181, %186 : i32, !llvm.ptr
    %187 = arith.constant 2 : i32
    %188 = arith.extsi %187 : i32 to i64
    %189 = llvm.mlir.constant(1 : i64) : i64
    %190 = llvm.alloca %189 x i64 : (i64) -> !llvm.ptr
    llvm.store %188, %190 : i64, !llvm.ptr
    cf.br ^bb27
    ^bb27:
    %191 = llvm.load %190 : !llvm.ptr -> i64
    %192 = arith.cmpi sle, %191, %arg0 : i64
    cf.cond_br %192, ^bb28, ^bb29
    ^bb28:
      %194 = llvm.mlir.addressof @spf : !llvm.ptr
      %195 = llvm.load %194 : !llvm.ptr -> !llvm.ptr
      %196 = llvm.load %190 : !llvm.ptr -> i64
      %197 = llvm.getelementptr %195[%196] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %193 = llvm.load %197 : !llvm.ptr -> i32
      %198 = arith.constant 0 : i32
      %199 = arith.cmpi eq, %193, %198 : i32
      cf.cond_br %199, ^bb30, ^bb31
      ^bb30:
        %200 = llvm.load %190 : !llvm.ptr -> i64
        %201 = arith.trunci %200 : i64 to i32
        %202 = llvm.mlir.addressof @spf : !llvm.ptr
        %203 = llvm.load %202 : !llvm.ptr -> !llvm.ptr
        %204 = llvm.load %190 : !llvm.ptr -> i64
        %205 = llvm.getelementptr %203[%204] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        llvm.store %201, %205 : i32, !llvm.ptr
        %206 = llvm.load %190 : !llvm.ptr -> i64
        %207 = arith.trunci %206 : i64 to i32
        %208 = llvm.mlir.addressof @primes : !llvm.ptr
        %209 = llvm.load %208 : !llvm.ptr -> !llvm.ptr
        %210 = llvm.mlir.addressof @nprimes : !llvm.ptr
        %211 = llvm.load %210 : !llvm.ptr -> i32
        %212 = arith.extsi %211 : i32 to i64
        %213 = llvm.getelementptr %209[%212] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        llvm.store %207, %213 : i32, !llvm.ptr
        %214 = llvm.mlir.addressof @nprimes : !llvm.ptr
        %215 = llvm.load %214 : !llvm.ptr -> i32
        %216 = arith.constant 1 : i32
        %217 = arith.addi %215, %216 : i32
        %218 = llvm.mlir.addressof @nprimes : !llvm.ptr
        llvm.store %217, %218 : i32, !llvm.ptr
        cf.br ^bb32
      ^bb31:
        cf.br ^bb32
      ^bb32:
      %220 = llvm.mlir.addressof @spf : !llvm.ptr
      %221 = llvm.load %220 : !llvm.ptr -> !llvm.ptr
      %222 = llvm.load %190 : !llvm.ptr -> i64
      %223 = llvm.getelementptr %221[%222] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %219 = llvm.load %223 : !llvm.ptr -> i32
      %224 = arith.extsi %219 : i32 to i64
      %225 = arith.constant 0 : i32
      %226 = llvm.mlir.constant(1 : i64) : i64
      %227 = llvm.alloca %226 x i32 : (i64) -> !llvm.ptr
      llvm.store %225, %227 : i32, !llvm.ptr
      cf.br ^bb33
      ^bb33:
      %228 = llvm.load %227 : !llvm.ptr -> i32
      %229 = llvm.mlir.addressof @nprimes : !llvm.ptr
      %230 = llvm.load %229 : !llvm.ptr -> i32
      %231 = arith.cmpi slt, %228, %230 : i32
      cf.cond_br %231, ^bb34, ^bb35
      ^bb34:
        %233 = llvm.mlir.addressof @primes : !llvm.ptr
        %234 = llvm.load %233 : !llvm.ptr -> !llvm.ptr
        %235 = llvm.load %227 : !llvm.ptr -> i32
        %236 = arith.extsi %235 : i32 to i64
        %237 = llvm.getelementptr %234[%236] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %232 = llvm.load %237 : !llvm.ptr -> i32
        %238 = arith.extsi %232 : i32 to i64
        %239 = llvm.load %190 : !llvm.ptr -> i64
        %240 = arith.muli %239, %238 : i64
        %241 = arith.cmpi sgt, %240, %arg0 : i64
        cf.cond_br %241, ^bb36, ^bb37
        ^bb36:
          cf.br ^bb35
        ^bb37:
          cf.br ^bb38
        ^bb38:
        %242 = arith.trunci %238 : i64 to i32
        %243 = llvm.mlir.addressof @spf : !llvm.ptr
        %244 = llvm.load %243 : !llvm.ptr -> !llvm.ptr
        %245 = llvm.getelementptr %244[%240] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        llvm.store %242, %245 : i32, !llvm.ptr
        %246 = arith.cmpi eq, %238, %224 : i64
        cf.cond_br %246, ^bb39, ^bb40
        ^bb39:
          cf.br ^bb35
        ^bb40:
          cf.br ^bb41
        ^bb41:
        %247 = llvm.load %227 : !llvm.ptr -> i32
        %248 = arith.constant 1 : i32
        %249 = arith.addi %247, %248 : i32
        llvm.store %249, %227 : i32, !llvm.ptr
        cf.br ^bb33
      ^bb35:
      %250 = llvm.load %190 : !llvm.ptr -> i64
      %251 = arith.constant 1 : i32
      %253 = arith.extsi %251 : i32 to i64
      %252 = arith.addi %250, %253 : i64
      llvm.store %252, %190 : i64, !llvm.ptr
      cf.br ^bb27
    ^bb29:
    func.return
  }
  func.func @compute_Q(%arg0: i64, %arg1: i64) -> i64 {
    func.call @smallest_prime_factors(%arg0) : (i64) -> ()
    %256 = arith.constant 1 : i32
    %258 = arith.extsi %256 : i32 to i64
    %257 = arith.addi %arg0, %258 : i64
    %259 = arith.constant 4 : i32
    %260 = arith.extsi %259 : i32 to i64
    %255 = func.call @calloc(%257, %260) : (i64, i64) -> !llvm.ptr
    %261 = llvm.mlir.addressof @f_arr : !llvm.ptr
    llvm.store %255, %261 : !llvm.ptr, !llvm.ptr
    %262 = arith.constant 1 : i32
    %263 = llvm.mlir.addressof @f_arr : !llvm.ptr
    %264 = llvm.load %263 : !llvm.ptr -> !llvm.ptr
    %265 = arith.constant 1 : i32
    %266 = arith.extsi %265 : i32 to i64
    %267 = llvm.getelementptr %264[%266] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    llvm.store %262, %267 : i32, !llvm.ptr
    %268 = arith.constant 1 : i32
    %270 = arith.extsi %268 : i32 to i64
    %269 = arith.remsi %270, %arg1 : i64
    %271 = llvm.mlir.constant(1 : i64) : i64
    %272 = llvm.alloca %271 x i64 : (i64) -> !llvm.ptr
    llvm.store %269, %272 : i64, !llvm.ptr
    %273 = arith.constant 2 : 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 ^bb42
    ^bb42:
    %277 = llvm.load %276 : !llvm.ptr -> i64
    %278 = arith.cmpi sle, %277, %arg0 : i64
    cf.cond_br %278, ^bb43, ^bb44
    ^bb43:
      %280 = llvm.mlir.addressof @spf : !llvm.ptr
      %281 = llvm.load %280 : !llvm.ptr -> !llvm.ptr
      %282 = llvm.load %276 : !llvm.ptr -> i64
      %283 = llvm.getelementptr %281[%282] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %279 = llvm.load %283 : !llvm.ptr -> i32
      %284 = arith.extsi %279 : i32 to i64
      %285 = llvm.load %276 : !llvm.ptr -> i64
      %286 = arith.divsi %285, %284 : i64
      %287 = arith.constant 0 : i32
      %288 = arith.extsi %287 : i32 to i64
      %290 = llvm.mlir.addressof @spf : !llvm.ptr
      %291 = llvm.load %290 : !llvm.ptr -> !llvm.ptr
      %292 = llvm.getelementptr %291[%286] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %289 = llvm.load %292 : !llvm.ptr -> i32
      %293 = arith.extsi %289 : i32 to i64
      %294 = arith.cmpi ne, %293, %284 : i64
      cf.cond_br %294, ^bb45, ^bb46
      ^bb45:
        %295 = arith.constant 0 : i32
        %296 = arith.extsi %295 : i32 to i64
        %297 = arith.constant 1 : i32
        %299 = arith.extsi %297 : i32 to i64
        %298 = arith.cmpi eq, %286, %299 : i64
        %300 = scf.if %298 -> (i64) {
          %302 = arith.constant 1 : i32
          %301 = func.call @q_prime_power_mod(%284, %302, %arg1) : (i64, i32, i64) -> i64
          scf.yield %301 : i64
        } else {
          %304 = llvm.mlir.addressof @f_arr : !llvm.ptr
          %305 = llvm.load %304 : !llvm.ptr -> !llvm.ptr
          %306 = llvm.getelementptr %305[%284] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %303 = llvm.load %306 : !llvm.ptr -> i32
          %307 = arith.extsi %303 : i32 to i64
          scf.yield %307 : i64
        }
        %310 = llvm.mlir.addressof @f_arr : !llvm.ptr
        %311 = llvm.load %310 : !llvm.ptr -> !llvm.ptr
        %312 = llvm.getelementptr %311[%286] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %309 = llvm.load %312 : !llvm.ptr -> i32
        %313 = arith.extsi %309 : i32 to i64
        %308 = func.call @mulmod(%313, %300, %arg1) : (i64, i64, i64) -> i64
        cf.br ^bb47(%308 : i64)
      ^bb46:
        %314 = arith.muli %284, %284 : i64
        %315 = llvm.mlir.constant(1 : i64) : i64
        %316 = llvm.alloca %315 x i64 : (i64) -> !llvm.ptr
        llvm.store %314, %316 : i64, !llvm.ptr
        %317 = arith.constant 2 : i32
        %318 = llvm.mlir.constant(1 : i64) : i64
        %319 = llvm.alloca %318 x i32 : (i64) -> !llvm.ptr
        llvm.store %317, %319 : i32, !llvm.ptr
        %320 = arith.divsi %286, %284 : i64
        %321 = llvm.mlir.constant(1 : i64) : i64
        %322 = llvm.alloca %321 x i64 : (i64) -> !llvm.ptr
        llvm.store %320, %322 : i64, !llvm.ptr
        cf.br ^bb48
        ^bb48:
        %323 = llvm.load %322 : !llvm.ptr -> i64
        %324 = arith.constant 1 : i32
        %326 = arith.extsi %324 : i32 to i64
        %325 = arith.cmpi sgt, %323, %326 : i64
        %327 = scf.if %325 -> (i1) {
          %329 = llvm.mlir.addressof @spf : !llvm.ptr
          %330 = llvm.load %329 : !llvm.ptr -> !llvm.ptr
          %331 = llvm.load %322 : !llvm.ptr -> i64
          %332 = llvm.getelementptr %330[%331] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %328 = llvm.load %332 : !llvm.ptr -> i32
          %333 = arith.extsi %328 : i32 to i64
          %334 = arith.cmpi eq, %333, %284 : i64
          scf.yield %334 : i1
        } else {
          %335 = arith.constant false
          scf.yield %335 : i1
        }
        cf.cond_br %327, ^bb49, ^bb50
        ^bb49:
          %336 = llvm.load %316 : !llvm.ptr -> i64
          %337 = arith.muli %336, %284 : i64
          llvm.store %337, %316 : i64, !llvm.ptr
          %338 = llvm.load %319 : !llvm.ptr -> i32
          %339 = arith.constant 1 : i32
          %340 = arith.addi %338, %339 : i32
          llvm.store %340, %319 : i32, !llvm.ptr
          %341 = llvm.load %322 : !llvm.ptr -> i64
          %342 = arith.divsi %341, %284 : i64
          llvm.store %342, %322 : i64, !llvm.ptr
          cf.br ^bb48
        ^bb50:
        %343 = llvm.load %322 : !llvm.ptr -> i64
        %344 = arith.constant 0 : i32
        %345 = arith.extsi %344 : i32 to i64
        %346 = arith.constant 1 : i32
        %348 = arith.extsi %346 : i32 to i64
        %347 = arith.cmpi eq, %343, %348 : i64
        %349 = scf.if %347 -> (i64) {
          %351 = llvm.load %319 : !llvm.ptr -> i32
          %350 = func.call @q_prime_power_mod(%284, %351, %arg1) : (i64, i32, i64) -> i64
          scf.yield %350 : i64
        } else {
          %353 = llvm.mlir.addressof @f_arr : !llvm.ptr
          %354 = llvm.load %353 : !llvm.ptr -> !llvm.ptr
          %355 = llvm.load %316 : !llvm.ptr -> i64
          %356 = llvm.getelementptr %354[%355] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %352 = llvm.load %356 : !llvm.ptr -> i32
          %357 = arith.extsi %352 : i32 to i64
          scf.yield %357 : i64
        }
        %360 = llvm.mlir.addressof @f_arr : !llvm.ptr
        %361 = llvm.load %360 : !llvm.ptr -> !llvm.ptr
        %362 = llvm.getelementptr %361[%343] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %359 = llvm.load %362 : !llvm.ptr -> i32
        %363 = arith.extsi %359 : i32 to i64
        %358 = func.call @mulmod(%363, %349, %arg1) : (i64, i64, i64) -> i64
        cf.br ^bb47(%358 : i64)
      ^bb47(%364: i64):
      %365 = arith.trunci %364 : i64 to i32
      %366 = llvm.mlir.addressof @f_arr : !llvm.ptr
      %367 = llvm.load %366 : !llvm.ptr -> !llvm.ptr
      %368 = llvm.load %276 : !llvm.ptr -> i64
      %369 = llvm.getelementptr %367[%368] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %365, %369 : i32, !llvm.ptr
      %370 = llvm.load %272 : !llvm.ptr -> i64
      %371 = arith.addi %370, %364 : i64
      llvm.store %371, %272 : i64, !llvm.ptr
      %372 = llvm.load %272 : !llvm.ptr -> i64
      %373 = arith.cmpi sge, %372, %arg1 : i64
      cf.cond_br %373, ^bb51, ^bb52
      ^bb51:
        %374 = llvm.load %272 : !llvm.ptr -> i64
        %375 = arith.subi %374, %arg1 : i64
        llvm.store %375, %272 : i64, !llvm.ptr
        cf.br ^bb53
      ^bb52:
        cf.br ^bb53
      ^bb53:
      %376 = llvm.load %276 : !llvm.ptr -> i64
      %377 = arith.constant 1 : i32
      %379 = arith.extsi %377 : i32 to i64
      %378 = arith.addi %376, %379 : i64
      llvm.store %378, %276 : i64, !llvm.ptr
      cf.br ^bb42
    ^bb44:
    %381 = llvm.mlir.addressof @spf : !llvm.ptr
    %382 = llvm.load %381 : !llvm.ptr -> !llvm.ptr
    func.call @free(%382) : (!llvm.ptr) -> ()
    %384 = llvm.mlir.addressof @primes : !llvm.ptr
    %385 = llvm.load %384 : !llvm.ptr -> !llvm.ptr
    func.call @free(%385) : (!llvm.ptr) -> ()
    %387 = llvm.mlir.addressof @f_arr : !llvm.ptr
    %388 = llvm.load %387 : !llvm.ptr -> !llvm.ptr
    func.call @free(%388) : (!llvm.ptr) -> ()
    %389 = llvm.load %272 : !llvm.ptr -> i64
    func.return %389 : i64
  }
  func.func @main() -> i32 {
    %390 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %392 = llvm.mlir.addressof @TARGET_N : !llvm.ptr
    %393 = llvm.load %392 : !llvm.ptr -> i64
    %394 = llvm.mlir.addressof @MOD : !llvm.ptr
    %395 = llvm.load %394 : !llvm.ptr -> i64
    %391 = func.call @compute_Q(%393, %395) : (i64, i64) -> i64
    %396 = llvm.call @printf(%390, %391) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %397 = arith.constant 0 : i32
    func.return %397 : i32
  }
}