Problem 830

Binomials and Powers - S(10^18) mod 83^3 * 89^3 * 97^3. Pure Flow port of the native C solver.

Answer254179446930484376
Output254179446930484376
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n^2)
Space complexityO(1)O(n)
ApproachFlow solutionPascal triangle computation
VerdictOptimal

Flow source

# Project Euler 830
# Binomials and Powers - S(10^18) mod 83^3 * 89^3 * 97^3.
# Pure Flow port of the native C solver.

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

function mmul(a: i64, b: i64, mod: i64) -> i64 {
    return ((a as i128) * (b as i128) % (mod as i128)) as i64
}

function mpow(base: i64, exp: i64, mod: i64) -> i64 {
    let mut result: i64 = 1 % mod
    let mut b: i64 = base % mod
    if b < 0 { b = b + mod }
    let mut e: i64 = exp
    while e > 0 {
        if e % 2 == 1 { result = mmul(result, b, mod) }
        b = mmul(b, b, mod)
        e = e / 2
    }
    return result
}

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

function modinv(a: i64, mod: i64) -> i64 {
    return egcd(a, mod, mod)
}

function v_p(x: i64, p: i32) -> i32 {
    if x == 0 { return 100 }
    let mut c: i32 = 0
    let mut v: i64 = x
    while v % (p as i64) == 0 {
        v = v / (p as i64)
        c = c + 1
    }
    return c
}

# Largest J such that v_p(n*(n-1)*...*(n-J+1)) < a
function max_j(n: i64, p: i32, a: i32) -> i32 {
    if n <= 0 { return 0 }
    let mut vp: i32 = 0
    let mut j: i32 = 0
    while true {
        if vp >= a { return j - 1 }
        j = j + 1
        if (j as i64) > n { return j - 1 }
        vp = vp + v_p(n - (j as i64) + 1, p)
    }
    return 0
}

# Forward differences of powers: F[j] = Delta^j f(0) for f(i) = i^n, mod p^a
function forward_diff(n: i64, J: i32, mod: i64, p: i32, F: ptr<i64>) -> void {
    let arr: ptr<i64> = malloc(((J as i64) + 1) * 8)
    arr[0] = 0
    let mut i: i32 = 1
    while i <= J {
        if i % p == 0 {
            arr[i] = 0
        } else {
            arr[i] = mpow(i as i64, n, mod)
        }
        i = i + 1
    }
    let mut j: i32 = 0
    while j <= J {
        F[j] = arr[0] % mod
        let mut i2: i32 = 0
        while i2 < J - j {
            arr[i2] = (arr[i2 + 1] - arr[i2]) % mod
            if arr[i2] < 0 { arr[i2] = arr[i2] + mod }
            i2 = i2 + 1
        }
        j = j + 1
    }
    free(arr)
}

# C(n, 0..J) mod p^a using p-adic valuation tracking
function binom_prefix(n: i64, J: i32, mod: i64, p: i32, a: i32, out: ptr<i64>) -> void {
    out[0] = 1 % mod
    let mut u_c: i64 = 1 % mod
    let mut v_c: i32 = 0

    let mut j: i32 = 1
    while j <= J {
        let term: i64 = n - (j as i64) + 1
        let v_term: i32 = v_p(term, p)
        let mut u_term: i64 = term
        let mut k: i32 = 0
        while k < v_term {
            u_term = u_term / (p as i64)
            k = k + 1
        }
        u_term = u_term % mod

        let v_jd: i32 = v_p(j as i64, p)
        let mut u_jd: i64 = j as i64
        let mut k2: i32 = 0
        while k2 < v_jd {
            u_jd = u_jd / (p as i64)
            k2 = k2 + 1
        }
        u_jd = u_jd % mod
        let inv_ujd: i64 = modinv(u_jd, mod)

        u_c = mmul(mmul(u_c, u_term, mod), inv_ujd, mod)
        v_c = v_c + v_term - v_jd

        if v_c >= a {
            out[j] = 0
        } else {
            let mut pp: i64 = 1
            let mut k3: i32 = 0
            while k3 < v_c {
                pp = pp * (p as i64)
                k3 = k3 + 1
            }
            out[j] = mmul(pp % mod, u_c, mod)
        }
        j = j + 1
    }
}

function solve_mod_prime_power(n: i64, p: i32, a: i32) -> i64 {
    let mut mod: i64 = 1
    let mut i: i32 = 0
    while i < a {
        mod = mod * (p as i64)
        i = i + 1
    }

    if n == 0 { return 1 % mod }

    let J: i32 = max_j(n, p, a)

    let fact_stirling: ptr<i64> = malloc(((J as i64) + 1) * 8)
    let choose: ptr<i64> = malloc(((J as i64) + 1) * 8)

    forward_diff(n, J, mod, p, fact_stirling)
    binom_prefix(n, J, mod, p, a, choose)

    let pow2: i64 = mpow(2, n, mod)
    let inv2: i64 = modinv(2, mod)

    let mut res: i64 = 0
    let mut cur_pow2: i64 = pow2
    let mut j: i32 = 0
    while j <= J {
        let term: i64 = mmul(mmul(choose[j], fact_stirling[j], mod), cur_pow2, mod)
        res = (res + term) % mod
        cur_pow2 = mmul(cur_pow2, inv2, mod)
        j = j + 1
    }

    free(fact_stirling)
    free(choose)
    return res
}

function crt(residues: ptr<i64>, moduli: ptr<i64>, count: i32) -> i64 {
    let mut M: i64 = 1
    let mut i: i32 = 0
    while i < count {
        M = M * moduli[i]
        i = i + 1
    }
    let mut x: i128 = 0
    let mut i2: i32 = 0
    while i2 < count {
        let Mi: i64 = M / moduli[i2]
        let inv: i64 = modinv(Mi % moduli[i2], moduli[i2])
        x = x + (residues[i2] as i128) * (Mi as i128) * (inv as i128)
        i2 = i2 + 1
    }
    return (x % (M as i128)) as i64
}

function main() -> i32 {
    let primes: ptr<i32> = malloc(12)
    primes[0] = 83; primes[1] = 89; primes[2] = 97
    let power: i32 = 3
    let mut n: i64 = 1
    let mut i: i32 = 0
    while i < 18 {
        n = n * 10
        i = i + 1
    }

    let mods: ptr<i64> = malloc(24)
    let residues: ptr<i64> = malloc(24)
    let mut i2: i32 = 0
    while i2 < 3 {
        mods[i2] = 1
        let mut k: i32 = 0
        while k < power {
            mods[i2] = mods[i2] * (primes[i2] as i64)
            k = k + 1
        }
        residues[i2] = solve_mod_prime_power(n, primes[i2], power)
        i2 = i2 + 1
    }

    printf("%lld\n", crt(residues, mods, 3))
    free(primes)
    free(mods)
    free(residues)
    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 mmul_i64_i64_i64(int64_t a, int64_t b, int64_t mod);
int64_t mpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
int64_t egcd_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t modinv_i64_i64(int64_t a, int64_t mod);
int32_t v_p_i64_i32(int64_t x, int32_t p);
int32_t max_j_i64_i32_i32(int64_t n, int32_t p, int32_t a);
void forward_diff_i64_i32_i64_i32_ptr_i64(int64_t n, int32_t J, int64_t mod, int32_t p, int64_t* F);
void binom_prefix_i64_i32_i64_i32_i32_ptr_i64(int64_t n, int32_t J, int64_t mod, int32_t p, int32_t a, int64_t* out);
int64_t solve_mod_prime_power_i64_i32_i32(int64_t n, int32_t p, int32_t a);
int64_t crt_ptr_i64_ptr_i64_i32(int64_t* residues, int64_t* moduli, int32_t count);
int32_t main(void);




int64_t mmul_i64_i64_i64(int64_t a, int64_t b, int64_t mod) {
    return ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(a)) * ((__int128)(b)))), (((__int128)(mod))))));
}

int64_t mpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
    int64_t result = FLOW_CHECKED_MOD((1), (mod));
    int64_t b = FLOW_CHECKED_MOD((base), (mod));
    if (b < 0) {
        b = (b + mod);
    }
    int64_t e = exp;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            result = mmul_i64_i64_i64(result, b, mod);
        }
        b = mmul_i64_i64_i64(b, b, mod);
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return result;
}

int64_t egcd_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod) {
    int64_t a = FLOW_CHECKED_MOD((a0), (mod));
    if (a < 0) {
        a = (a + mod);
    }
    int64_t b = mod;
    int64_t x0 = 1;
    int64_t x1 = 0;
    while (b != 0) {
        int64_t q = FLOW_CHECKED_DIV((a), (b));
        int64_t t = b;
        b = FLOW_CHECKED_MOD((a), (b));
        a = t;
        int64_t t2 = x1;
        x1 = (x0 - (q * x1));
        x0 = t2;
    }
    return FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((x0), (mod)) + mod)), (mod));
}

int64_t modinv_i64_i64(int64_t a, int64_t mod) {
    return egcd_i64_i64_i64(a, mod, mod);
}

int32_t v_p_i64_i32(int64_t x, int32_t p) {
    if (x == 0) {
        return 100;
    }
    int32_t c = 0;
    int64_t v = x;
    while (FLOW_CHECKED_MOD((v), (((int64_t)(p)))) == 0) {
        v = FLOW_CHECKED_DIV((v), (((int64_t)(p))));
        c = (c + 1);
    }
    return c;
}

int32_t max_j_i64_i32_i32(int64_t n, int32_t p, int32_t a) {
    if (n <= 0) {
        return 0;
    }
    int32_t vp = 0;
    int32_t j = 0;
    while (1) {
        if (vp >= a) {
            return (j - 1);
        }
        j = (j + 1);
        if (((int64_t)(j)) > n) {
            return (j - 1);
        }
        vp = (vp + v_p_i64_i32(((n - ((int64_t)(j))) + 1), p));
    }
    return 0;
}

void forward_diff_i64_i32_i64_i32_ptr_i64(int64_t n, int32_t J, int64_t mod, int32_t p, int64_t* F) {
    int64_t* arr = (int64_t*)(malloc(((((int64_t)(J)) + 1) * 8)));
    arr[0] = 0;
    int32_t i = 1;
    while (i <= J) {
        if (FLOW_CHECKED_MOD((i), (p)) == 0) {
            arr[i] = 0;
        } else {
            arr[i] = mpow_i64_i64_i64(((int64_t)(i)), n, mod);
        }
        i = (i + 1);
    }
    int32_t j = 0;
    while (j <= J) {
        F[j] = FLOW_CHECKED_MOD((arr[0]), (mod));
        int32_t i2 = 0;
        while (i2 < (J - j)) {
            arr[i2] = FLOW_CHECKED_MOD(((arr[(i2 + 1)] - arr[i2])), (mod));
            if (arr[i2] < 0) {
                arr[i2] = (arr[i2] + mod);
            }
            i2 = (i2 + 1);
        }
        j = (j + 1);
    }
    free(arr);
}

void binom_prefix_i64_i32_i64_i32_i32_ptr_i64(int64_t n, int32_t J, int64_t mod, int32_t p, int32_t a, int64_t* out) {
    out[0] = FLOW_CHECKED_MOD((1), (mod));
    int64_t u_c = FLOW_CHECKED_MOD((1), (mod));
    int32_t v_c = 0;
    int32_t j = 1;
    while (j <= J) {
        int64_t term = ((n - ((int64_t)(j))) + 1);
        int32_t v_term = v_p_i64_i32(term, p);
        int64_t u_term = term;
        int32_t k = 0;
        while (k < v_term) {
            u_term = FLOW_CHECKED_DIV((u_term), (((int64_t)(p))));
            k = (k + 1);
        }
        u_term = FLOW_CHECKED_MOD((u_term), (mod));
        int32_t v_jd = v_p_i64_i32(((int64_t)(j)), p);
        int64_t u_jd = ((int64_t)(j));
        int32_t k2 = 0;
        while (k2 < v_jd) {
            u_jd = FLOW_CHECKED_DIV((u_jd), (((int64_t)(p))));
            k2 = (k2 + 1);
        }
        u_jd = FLOW_CHECKED_MOD((u_jd), (mod));
        int64_t inv_ujd = modinv_i64_i64(u_jd, mod);
        u_c = mmul_i64_i64_i64(mmul_i64_i64_i64(u_c, u_term, mod), inv_ujd, mod);
        v_c = ((v_c + v_term) - v_jd);
        if (v_c >= a) {
            out[j] = 0;
        } else {
            int64_t pp = 1;
            int32_t k3 = 0;
            while (k3 < v_c) {
                pp = (pp * ((int64_t)(p)));
                k3 = (k3 + 1);
            }
            out[j] = mmul_i64_i64_i64(FLOW_CHECKED_MOD((pp), (mod)), u_c, mod);
        }
        j = (j + 1);
    }
}

int64_t solve_mod_prime_power_i64_i32_i32(int64_t n, int32_t p, int32_t a) {
    int64_t mod = 1;
    int32_t i = 0;
    while (i < a) {
        mod = (mod * ((int64_t)(p)));
        i = (i + 1);
    }
    if (n == 0) {
        return FLOW_CHECKED_MOD((1), (mod));
    }
    int32_t J = max_j_i64_i32_i32(n, p, a);
    int64_t* fact_stirling = (int64_t*)(malloc(((((int64_t)(J)) + 1) * 8)));
    int64_t* choose = (int64_t*)(malloc(((((int64_t)(J)) + 1) * 8)));
    forward_diff_i64_i32_i64_i32_ptr_i64(n, J, mod, p, fact_stirling);
    binom_prefix_i64_i32_i64_i32_i32_ptr_i64(n, J, mod, p, a, choose);
    int64_t pow2 = mpow_i64_i64_i64(2, n, mod);
    int64_t inv2 = modinv_i64_i64(2, mod);
    int64_t res = 0;
    int64_t cur_pow2 = pow2;
    int32_t j = 0;
    while (j <= J) {
        int64_t term = mmul_i64_i64_i64(mmul_i64_i64_i64(choose[j], fact_stirling[j], mod), cur_pow2, mod);
        res = FLOW_CHECKED_MOD(((res + term)), (mod));
        cur_pow2 = mmul_i64_i64_i64(cur_pow2, inv2, mod);
        j = (j + 1);
    }
    free(fact_stirling);
    free(choose);
    return res;
}

int64_t crt_ptr_i64_ptr_i64_i32(int64_t* residues, int64_t* moduli, int32_t count) {
    int64_t M = 1;
    int32_t i = 0;
    while (i < count) {
        M = (M * moduli[i]);
        i = (i + 1);
    }
    __int128 x = 0;
    int32_t i2 = 0;
    while (i2 < count) {
        int64_t Mi = FLOW_CHECKED_DIV((M), (moduli[i2]));
        int64_t inv = modinv_i64_i64(FLOW_CHECKED_MOD((Mi), (moduli[i2])), moduli[i2]);
        x = (x + ((((__int128)(residues[i2])) * ((__int128)(Mi))) * ((__int128)(inv))));
        i2 = (i2 + 1);
    }
    return ((int64_t)(FLOW_CHECKED_MOD((x), (((__int128)(M))))));
}

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