Problem 517

A Real Recursion — sum G(p) for primes in (10^7, 10^7+10^4) mod 1e9+7.

Answer581468882
Output581468882
StatusPASS
Native helperno
Runtime490 ms
Peak memory157568 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 517
# A Real Recursion — sum G(p) for primes in (10^7, 10^7+10^4) mod 1e9+7.

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

const MOD: i64 = 1000000007
const LOW: i64 = 10000000
const HIGH: i64 = 10010000

function isqrt(n: i64) -> i64 {
    if n < 2 { return n }
    let mut x: i64 = n
    let mut y: i64 = (x + 1) / 2
    while y < x {
        x = y
        y = (x + n / x) / 2
    }
    return x
}

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

function comb(n: i64, k: i64, fact: ptr<i64>, invfact: ptr<i64>) -> i64 {
    if k < 0 || k > n { return 0 }
    let a: i128 = (fact[n] as i128) * (invfact[k] as i128) % (MOD as i128)
    return (a * (invfact[n - k] as i128) % (MOD as i128)) as i64
}

function G(n: i64, fact: ptr<i64>, invfact: ptr<i64>, u: ptr<i64>) -> i64 {
    let a_floor: i64 = isqrt(n)
    let mut m: i64 = 1
    while m <= a_floor + 1 {
        u[m] = n - isqrt(m * m * n) - 1
        m = m + 1
    }
    let mut ans: i64 = 0
    m = 1
    while m <= a_floor {
        let c: i64 = u[m]
        let i: i64 = m - 1
        ans = (ans + comb(c + i, i, fact, invfact)) % MOD
        m = m + 1
    }
    let mut c0: i64 = 0
    while c0 <= a_floor - 1 {
        let upper: i64 = u[c0 + 1]
        if upper >= 0 {
            let mut lower: i64 = u[c0 + 2] + 1
            if lower < 0 { lower = 0 }
            if lower <= upper {
                ans = (ans + comb(c0 + upper + 1, c0 + 1, fact, invfact)) % MOD
                if lower > 0 {
                    ans = (ans - comb(c0 + lower, c0 + 1, fact, invfact) + MOD) % MOD
                }
            }
        }
        c0 = c0 + 1
    }
    return ans % MOD
}

function is_prime(x: i64) -> i32 {
    if x < 2 { return 0 }
    let mut i: i64 = 2
    while i * i <= x {
        if x % i == 0 { return 0 }
        i = i + 1
    }
    return 1
}

function main() -> i32 {
    let maxn: i64 = HIGH
    let fact: ptr<i64> = calloc(maxn + 1, 8)
    let invfact: ptr<i64> = calloc(maxn + 1, 8)
    let u: ptr<i64> = calloc(maxn + 4, 8)
    if fact == null || invfact == null || u == null { return 1 }

    fact[0] = 1
    let mut i: i64 = 1
    while i <= maxn {
        fact[i] = (fact[i - 1] * i) % MOD
        i = i + 1
    }
    invfact[maxn] = modpow(fact[maxn], MOD - 2, MOD)
    i = maxn
    while i > 0 {
        invfact[i - 1] = (invfact[i] * i) % MOD
        i = i - 1
    }

    let mut total: i64 = 0
    let mut pr: i64 = LOW + 1
    while pr < HIGH {
        if is_prime(pr) == 1 {
            total = (total + G(pr, fact, invfact, u)) % MOD
        }
        pr = pr + 1
    }

    printf("%lld\n", total)
    free(fact)
    free(invfact)
    free(u)
    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 isqrt_i64(int64_t n);
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int64_t comb_i64_i64_ptr_i64_ptr_i64(int64_t n, int64_t k, int64_t* fact, int64_t* invfact);
int64_t G_i64_ptr_i64_ptr_i64_ptr_i64(int64_t n, int64_t* fact, int64_t* invfact, int64_t* u);
int32_t is_prime_i64(int64_t x);
int32_t main(void);

static const int64_t MOD = 1000000007;
static const int64_t LOW = 10000000;
static const int64_t HIGH = 10010000;



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

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

int64_t comb_i64_i64_ptr_i64_ptr_i64(int64_t n, int64_t k, int64_t* fact, int64_t* invfact) {
    if ((k < 0 || k > n)) {
        return 0;
    }
    __int128 a = FLOW_CHECKED_MOD(((((__int128)(fact[n])) * ((__int128)(invfact[k])))), (((__int128)(MOD))));
    return ((int64_t)(FLOW_CHECKED_MOD(((a * ((__int128)(invfact[(n - k)])))), (((__int128)(MOD))))));
}

int64_t G_i64_ptr_i64_ptr_i64_ptr_i64(int64_t n, int64_t* fact, int64_t* invfact, int64_t* u) {
    int64_t a_floor = isqrt_i64(n);
    int64_t m = 1;
    while (m <= (a_floor + 1)) {
        u[m] = ((n - isqrt_i64(((m * m) * n))) - 1);
        m = (m + 1);
    }
    int64_t ans = 0;
    m = 1;
    while (m <= a_floor) {
        int64_t c = u[m];
        int64_t i = (m - 1);
        ans = FLOW_CHECKED_MOD(((ans + comb_i64_i64_ptr_i64_ptr_i64((c + i), i, fact, invfact))), (MOD));
        m = (m + 1);
    }
    int64_t c0 = 0;
    while (c0 <= (a_floor - 1)) {
        int64_t upper = u[(c0 + 1)];
        if (upper >= 0) {
            int64_t lower = (u[(c0 + 2)] + 1);
            if (lower < 0) {
                lower = 0;
            }
            if (lower <= upper) {
                ans = FLOW_CHECKED_MOD(((ans + comb_i64_i64_ptr_i64_ptr_i64(((c0 + upper) + 1), (c0 + 1), fact, invfact))), (MOD));
                if (lower > 0) {
                    ans = FLOW_CHECKED_MOD((((ans - comb_i64_i64_ptr_i64_ptr_i64((c0 + lower), (c0 + 1), fact, invfact)) + MOD)), (MOD));
                }
            }
        }
        c0 = (c0 + 1);
    }
    return FLOW_CHECKED_MOD((ans), (MOD));
}

int32_t is_prime_i64(int64_t x) {
    if (x < 2) {
        return 0;
    }
    int64_t i = 2;
    while ((i * i) <= x) {
        if (FLOW_CHECKED_MOD((x), (i)) == 0) {
            return 0;
        }
        i = (i + 1);
    }
    return 1;
}

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