Problem 643

f(n) = sum_{t>=1} (S(floor(n/2^t)) - 1) mod 1e9+7 where S(m) = sum_{k=1..m} phi(k). Uses Du Jiao sieve with pre-enumerated floor(N/i) values.

Answer968274154
Output968274154
StatusPASS
Native helperno
Runtime2100 ms
Peak memory182304 KB
Time complexityO(n) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n log log n)
Space complexityO(n^2)O(n)
ApproachFlow solutionSieve-based totient computation
VerdictOptimal

Flow source

# Project Euler 643: 2-Friendly
# f(n) = sum_{t>=1} (S(floor(n/2^t)) - 1) mod 1e9+7
# where S(m) = sum_{k=1..m} phi(k).
# Uses Du Jiao sieve with pre-enumerated floor(N/i) values.

import euler.nt { isqrt }

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

const MOD: i64 = 1000000007
const LIMIT: i64 = 10000000
const INV2: i64 = 500000004

# Binary search for key in descending-sorted array, return index or -1
function bsearch_desc(keys: ptr<i64>, n: i64, key: i64) -> i64 {
    let mut lo: i64 = 0
    let mut hi: i64 = n - 1
    while lo <= hi {
        let mid: i64 = (lo + hi) / 2
        if keys[mid] == key { return mid }
        if keys[mid] > key { lo = mid + 1 }
        else { hi = mid - 1 }
    }
    return -1
}

function main() -> i32 {
    let N: i64 = 100000000000  # 10^11

    # Step 1: Precompute phi prefix sums up to LIMIT
    let phi: ptr<i64> = calloc(LIMIT + 1, 8)
    if phi == null { return 1 }
    phi[1] = 1

    let is_comp: ptr<i8> = calloc(LIMIT + 1, 1)
    let primes_arr: ptr<i64> = calloc(LIMIT / 10 + 100, 8)
    let mut npc: i64 = 0

    for i in 2..(LIMIT + 1) {
        if is_comp[i] == 0 {
            primes_arr[npc] = i
            npc = npc + 1
            phi[i] = i - 1
        }
        for j in 0..npc {
            let p: i64 = primes_arr[j]
            let ip: i64 = i * p
            if ip > LIMIT { break }
            is_comp[ip] = 1
            if i % p == 0 {
                phi[ip] = phi[i] * p
                break
            } else {
                phi[ip] = phi[i] * (p - 1)
            }
        }
    }

    # Prefix sum of phi mod MOD
    let pref: ptr<i64> = calloc(LIMIT + 1, 8)
    let mut s: i64 = 0
    for i in 1..(LIMIT + 1) {
        s = (s + phi[i]) % MOD
        pref[i] = s
    }

    free(is_comp)
    free(primes_arr)
    free(phi)

    # Step 2: Enumerate all distinct floor(N/i) values for i >= 1
    # Large values: N/i for i=1..root_n (already decreasing)
    # Small values: 1..small_max (increasing, reverse to get decreasing)
    # Merge them (large are all >= root_n, small are all <= root_n, possible overlap at root_n)
    let root_n: i64 = isqrt(N)
    let max_vals: i64 = 2 * root_n + 10

    let keys: ptr<i64> = calloc(max_vals, 8)
    let vals: ptr<i64> = calloc(max_vals, 8)
    let mut nkeys: i64 = 0

    # Large values: N/1, N/2, ..., N/root_n (decreasing)
    for i in 1..(root_n + 1) {
        keys[nkeys] = N / i
        nkeys = nkeys + 1
    }

    # Small values: small_max down to 1 (decreasing)
    let small_max: i64 = N / (root_n + 1)
    for v in 0..small_max {
        keys[nkeys] = small_max - v
        nkeys = nkeys + 1
    }

    # Remove duplicates (consecutive equal values)
    let mut unique: i64 = 0
    for i in 0..nkeys {
        if i == 0 || keys[i] != keys[i - 1] {
            keys[unique] = keys[i]
            unique = unique + 1
        }
    }
    nkeys = unique

    # Step 3: Compute S(v) for each v in INCREASING order
    # (S(v) depends on S(w) for w < v, so process smallest first)
    for idx in 0..nkeys {
        let rev_idx: i64 = nkeys - 1 - idx
        let v: i64 = keys[rev_idx]

        if v <= LIMIT {
            vals[rev_idx] = pref[v]
            continue
        }
        if v == 0 {
            vals[rev_idx] = 0
            continue
        }

        # S(v) = v*(v+1)/2 - sum_{m=2..v} S(floor(v/m))
        # Work mod MOD throughout
        let mut result: i64 = 0
        let v_mod: i64 = v % MOD
        let vp1_mod: i64 = (v + 1) % MOD
        # v*(v+1)/2 mod MOD = v_mod * vp1_mod / 2 mod MOD
        # Since MOD is odd, /2 = * inverse(2) = * (MOD+1)/2
        let mut tri_mod: i128 = (v_mod as i128) * (vp1_mod as i128) % (MOD as i128)
        tri_mod = tri_mod * (INV2 as i128) % (MOD as i128)
        result = tri_mod as i64

        # Group by floor(v/m) values
        let mut m: i64 = 2
        while m <= v {
            let w: i64 = v / m
            if w == 0 { break }
            let m_end: i64 = v / w  # largest m with floor(v/m) = w

            # Look up S(w)
            let sw: i64
            if w <= LIMIT {
                sw = pref[w]
            } else {
                let wi: i64 = bsearch_desc(keys, nkeys, w)
                if wi < 0 { return 1 }
                sw = vals[wi]
            }

            let count: i64 = m_end - m + 1
            let count_mod: i64 = count % MOD
            let term: i128 = (count_mod as i128) * (sw as i128) % (MOD as i128)
            let mut r128: i128 = (result as i128) - term
            r128 = r128 % (MOD as i128)
            if r128 < 0 { r128 = r128 + (MOD as i128) }
            result = r128 as i64

            m = m_end + 1
        }

        vals[rev_idx] = result
    }

    # Step 4: Compute f(N) = sum_{t>=1} (S(floor(N/2^t)) - 1)
    let mut ans: i64 = 0
    let mut t: i64 = 1
    while (N >> t) > 0 {
        let m_val: i64 = N >> t
        let sm: i64
        if m_val <= LIMIT {
            sm = pref[m_val]
        } else {
            let mi: i64 = bsearch_desc(keys, nkeys, m_val)
            if mi < 0 { return 1 }
            sm = vals[mi]
        }
        ans = (ans + sm - 1 + MOD) % MOD
        t = t + 1
    }

    printf("%lld\n", ans)

    free(vals)
    free(keys)
    free(pref)
    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 gcd_i64_i64(int64_t a0, int64_t b0);
int64_t lcm_i64_i64(int64_t a, int64_t b);
int64_t isqrt_i64(int64_t n);
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
bool is_prime_i64(int64_t n);
int64_t bsearch_desc_ptr_i64_i64_i64(int64_t* keys, int64_t n, int64_t key);
int32_t main(void);

static const int64_t MOD = 1000000007;
static const int64_t LIMIT = 10000000;
static const int64_t INV2 = 500000004;

int64_t gcd_i64_i64(int64_t a0, int64_t b0) {
    int64_t a = a0;
    int64_t b = b0;
    while (b != 0) {
        int64_t t = FLOW_CHECKED_MOD((a), (b));
        a = b;
        b = t;
    }
    return a;
}

int64_t lcm_i64_i64(int64_t a, int64_t b) {
    if ((a == 0 || b == 0)) {
        return 0;
    }
    return (FLOW_CHECKED_DIV((a), (gcd_i64_i64(a, b))) * b);
}

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 mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod) {
    int64_t a = FLOW_CHECKED_MOD((a0), (mod));
    int64_t b = FLOW_CHECKED_MOD((b0), (mod));
    int64_t result = 0;
    while (b > 0) {
        if (FLOW_CHECKED_MOD((b), (2)) == 1) {
            result = FLOW_CHECKED_MOD(((result + a)), (mod));
        }
        a = FLOW_CHECKED_MOD(((a * 2)), (mod));
        b = FLOW_CHECKED_DIV((b), (2));
    }
    return result;
}

int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
    if (mod == 1) {
        return 0;
    }
    int64_t result = 1;
    int64_t b = FLOW_CHECKED_MOD((base), (mod));
    int64_t e = exp;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            result = mulmod_i64_i64_i64(result, b, mod);
        }
        b = mulmod_i64_i64_i64(b, b, mod);
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return result;
}

bool is_prime_i64(int64_t n) {
    if (n < 2) {
        return 0;
    }
    if (n < 4) {
        return 1;
    }
    if ((FLOW_CHECKED_MOD((n), (2)) == 0 || FLOW_CHECKED_MOD((n), (3)) == 0)) {
        return 0;
    }
    int64_t i = 5;
    while ((i * i) <= n) {
        if ((FLOW_CHECKED_MOD((n), (i)) == 0 || FLOW_CHECKED_MOD((n), ((i + 2))) == 0)) {
            return 0;
        }
        i = (i + 6);
    }
    return 1;
}



int64_t bsearch_desc_ptr_i64_i64_i64(int64_t* keys, int64_t n, int64_t key) {
    int64_t lo = 0;
    int64_t hi = (n - 1);
    while (lo <= hi) {
        int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
        if (keys[mid] == key) {
            return mid;
        }
        if (keys[mid] > key) {
            lo = (mid + 1);
        } else {
            hi = (mid - 1);
        }
    }
    return (-1);
}

int32_t main(void) {
    int64_t N = 100000000000;
    int64_t* phi = (int64_t*)(calloc((LIMIT + 1), 8));
    if (phi == NULL) {
        return 1;
    }
    phi[1] = 1;
    int8_t* is_comp = (int8_t*)(calloc((LIMIT + 1), 1));
    int64_t* primes_arr = (int64_t*)(calloc((FLOW_CHECKED_DIV((LIMIT), (10)) + 100), 8));
    int64_t npc = 0;
    int32_t __flow_step_1 = 1;
    for (int32_t i = 2; (2 <= (LIMIT + 1)) ? i < (LIMIT + 1) : i > (LIMIT + 1); i += (2 <= (LIMIT + 1)) ? 1 : -1) {
        if (is_comp[i] == 0) {
            primes_arr[npc] = i;
            npc = (npc + 1);
            phi[i] = (i - 1);
        }
        int32_t __flow_step_2 = 1;
        for (int32_t j = 0; (0 <= npc) ? j < npc : j > npc; j += (0 <= npc) ? 1 : -1) {
            int64_t p = primes_arr[j];
            int64_t ip = (i * p);
            if (ip > LIMIT) {
                break;
            }
            is_comp[ip] = 1;
            if (FLOW_CHECKED_MOD((i), (p)) == 0) {
                phi[ip] = (phi[i] * p);
                break;
            } else {
                phi[ip] = (phi[i] * (p - 1));
            }
        }
    }
    int64_t* pref = (int64_t*)(calloc((LIMIT + 1), 8));
    int64_t s = 0;
    int32_t __flow_step_3 = 1;
    for (int32_t i = 1; (1 <= (LIMIT + 1)) ? i < (LIMIT + 1) : i > (LIMIT + 1); i += (1 <= (LIMIT + 1)) ? 1 : -1) {
        s = FLOW_CHECKED_MOD(((s + phi[i])), (MOD));
        pref[i] = s;
    }
    free(is_comp);
    free(primes_arr);
    free(phi);
    int64_t root_n = isqrt_i64(N);
    int64_t max_vals = ((2 * root_n) + 10);
    int64_t* keys = (int64_t*)(calloc(max_vals, 8));
    int64_t* vals = (int64_t*)(calloc(max_vals, 8));
    int64_t nkeys = 0;
    int32_t __flow_step_4 = 1;
    for (int32_t i = 1; (1 <= (root_n + 1)) ? i < (root_n + 1) : i > (root_n + 1); i += (1 <= (root_n + 1)) ? 1 : -1) {
        keys[nkeys] = FLOW_CHECKED_DIV((N), (i));
        nkeys = (nkeys + 1);
    }
    int64_t small_max = FLOW_CHECKED_DIV((N), ((root_n + 1)));
    int32_t __flow_step_5 = 1;
    for (int32_t v = 0; (0 <= small_max) ? v < small_max : v > small_max; v += (0 <= small_max) ? 1 : -1) {
        keys[nkeys] = (small_max - v);
        nkeys = (nkeys + 1);
    }
    int64_t unique = 0;
    int32_t __flow_step_6 = 1;
    for (int32_t i = 0; (0 <= nkeys) ? i < nkeys : i > nkeys; i += (0 <= nkeys) ? 1 : -1) {
        if ((i == 0 || keys[i] != keys[(i - 1)])) {
            keys[unique] = keys[i];
            unique = (unique + 1);
        }
    }
    nkeys = unique;
    int32_t __flow_step_7 = 1;
    for (int32_t idx = 0; (0 <= nkeys) ? idx < nkeys : idx > nkeys; idx += (0 <= nkeys) ? 1 : -1) {
        int64_t rev_idx = ((nkeys - 1) - idx);
        int64_t v = keys[rev_idx];
        if (v <= LIMIT) {
            vals[rev_idx] = pref[v];
            continue;
        }
        if (v == 0) {
            vals[rev_idx] = 0;
            continue;
        }
        int64_t result = 0;
        int64_t v_mod = FLOW_CHECKED_MOD((v), (MOD));
        int64_t vp1_mod = FLOW_CHECKED_MOD(((v + 1)), (MOD));
        __int128 tri_mod = FLOW_CHECKED_MOD(((((__int128)(v_mod)) * ((__int128)(vp1_mod)))), (((__int128)(MOD))));
        tri_mod = FLOW_CHECKED_MOD(((tri_mod * ((__int128)(INV2)))), (((__int128)(MOD))));
        result = ((int64_t)(tri_mod));
        int64_t m = 2;
        while (m <= v) {
            int64_t w = FLOW_CHECKED_DIV((v), (m));
            if (w == 0) {
                break;
            }
            int64_t m_end = FLOW_CHECKED_DIV((v), (w));
            int64_t sw;
            if (w <= LIMIT) {
                sw = pref[w];
            } else {
                int64_t wi = bsearch_desc_ptr_i64_i64_i64(keys, nkeys, w);
                if (wi < 0) {
                    return 1;
                }
                sw = vals[wi];
            }
            int64_t count = ((m_end - m) + 1);
            int64_t count_mod = FLOW_CHECKED_MOD((count), (MOD));
            __int128 term = FLOW_CHECKED_MOD(((((__int128)(count_mod)) * ((__int128)(sw)))), (((__int128)(MOD))));
            __int128 r128 = (((__int128)(result)) - term);
            r128 = FLOW_CHECKED_MOD((r128), (((__int128)(MOD))));
            if (r128 < 0) {
                r128 = (r128 + ((__int128)(MOD)));
            }
            result = ((int64_t)(r128));
            m = (m_end + 1);
        }
        vals[rev_idx] = result;
    }
    int64_t ans = 0;
    int64_t t = 1;
    while (FLOW_CHECKED_SHR((N), (t)) > 0) {
        int64_t m_val = FLOW_CHECKED_SHR((N), (t));
        int64_t sm;
        if (m_val <= LIMIT) {
            sm = pref[m_val];
        } else {
            int64_t mi = bsearch_desc_ptr_i64_i64_i64(keys, nkeys, m_val);
            if (mi < 0) {
                return 1;
            }
            sm = vals[mi];
        }
        ans = FLOW_CHECKED_MOD(((((ans + sm) - 1) + MOD)), (MOD));
        t = (t + 1);
    }
    printf("%lld\n", ans);
    free(vals);
    free(keys);
    free(pref);
    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 @gcd(%arg0: i64, %arg1: i64) -> i64 {
    %0 = llvm.mlir.constant(1 : i64) : i64
    %1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %1 : i64, !llvm.ptr
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %3 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %4 = llvm.load %3 : !llvm.ptr -> i64
    %5 = arith.constant 0 : i32
    %7 = arith.extsi %5 : i32 to i64
    %6 = arith.cmpi ne, %4, %7 : i64
    cf.cond_br %6, ^bb1, ^bb2
    ^bb1:
      %8 = llvm.load %1 : !llvm.ptr -> i64
      %9 = llvm.load %3 : !llvm.ptr -> i64
      %10 = arith.remsi %8, %9 : i64
      %11 = llvm.load %3 : !llvm.ptr -> i64
      llvm.store %11, %1 : i64, !llvm.ptr
      llvm.store %10, %3 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %12 = llvm.load %1 : !llvm.ptr -> i64
    func.return %12 : i64
  }
  func.func @lcm(%arg0: i64, %arg1: i64) -> i64 {
    %13 = arith.constant 0 : i32
    %15 = arith.extsi %13 : i32 to i64
    %14 = arith.cmpi eq, %arg0, %15 : i64
    %16 = scf.if %14 -> (i1) {
      %17 = arith.constant true
      scf.yield %17 : i1
    } else {
      %18 = arith.constant 0 : i32
      %20 = arith.extsi %18 : i32 to i64
      %19 = arith.cmpi eq, %arg1, %20 : i64
      scf.yield %19 : i1
    }
    cf.cond_br %16, ^bb3, ^bb4
    ^bb3:
      %21 = arith.constant 0 : i32
      %22 = arith.extsi %21 : i32 to i64
      func.return %22 : i64
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %23 = func.call @gcd(%arg0, %arg1) : (i64, i64) -> i64
    %24 = arith.divsi %arg0, %23 : i64
    %25 = arith.muli %24, %arg1 : i64
    func.return %25 : i64
  }
  func.func @isqrt(%arg0: i64) -> i64 {
    %26 = arith.constant 2 : i32
    %28 = arith.extsi %26 : i32 to i64
    %27 = arith.cmpi slt, %arg0, %28 : i64
    cf.cond_br %27, ^bb6, ^bb7
    ^bb6:
      func.return %arg0 : i64
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %29 = llvm.mlir.constant(1 : i64) : i64
    %30 = llvm.alloca %29 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %30 : i64, !llvm.ptr
    %31 = llvm.load %30 : !llvm.ptr -> i64
    %32 = arith.constant 1 : i32
    %34 = arith.extsi %32 : i32 to i64
    %33 = arith.addi %31, %34 : i64
    %35 = arith.constant 2 : i32
    %37 = arith.extsi %35 : i32 to i64
    %36 = arith.divsi %33, %37 : i64
    %38 = llvm.mlir.constant(1 : i64) : i64
    %39 = llvm.alloca %38 x i64 : (i64) -> !llvm.ptr
    llvm.store %36, %39 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %40 = llvm.load %39 : !llvm.ptr -> i64
    %41 = llvm.load %30 : !llvm.ptr -> i64
    %42 = arith.cmpi slt, %40, %41 : i64
    cf.cond_br %42, ^bb10, ^bb11
    ^bb10:
      %43 = llvm.load %39 : !llvm.ptr -> i64
      llvm.store %43, %30 : i64, !llvm.ptr
      %44 = llvm.load %30 : !llvm.ptr -> i64
      %45 = llvm.load %30 : !llvm.ptr -> i64
      %46 = arith.divsi %arg0, %45 : i64
      %47 = arith.addi %44, %46 : i64
      %48 = arith.constant 2 : i32
      %50 = arith.extsi %48 : i32 to i64
      %49 = arith.divsi %47, %50 : i64
      llvm.store %49, %39 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %51 = llvm.load %30 : !llvm.ptr -> i64
    func.return %51 : i64
  }
  func.func @mulmod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %52 = arith.remsi %arg0, %arg2 : i64
    %53 = llvm.mlir.constant(1 : i64) : i64
    %54 = llvm.alloca %53 x i64 : (i64) -> !llvm.ptr
    llvm.store %52, %54 : i64, !llvm.ptr
    %55 = arith.remsi %arg1, %arg2 : i64
    %56 = llvm.mlir.constant(1 : i64) : i64
    %57 = llvm.alloca %56 x i64 : (i64) -> !llvm.ptr
    llvm.store %55, %57 : i64, !llvm.ptr
    %58 = arith.constant 0 : i32
    %59 = arith.extsi %58 : i32 to i64
    %60 = llvm.mlir.constant(1 : i64) : i64
    %61 = llvm.alloca %60 x i64 : (i64) -> !llvm.ptr
    llvm.store %59, %61 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %62 = llvm.load %57 : !llvm.ptr -> i64
    %63 = arith.constant 0 : i32
    %65 = arith.extsi %63 : i32 to i64
    %64 = arith.cmpi sgt, %62, %65 : i64
    cf.cond_br %64, ^bb13, ^bb14
    ^bb13:
      %66 = llvm.load %57 : !llvm.ptr -> i64
      %67 = arith.constant 2 : i32
      %69 = arith.extsi %67 : i32 to i64
      %68 = arith.remsi %66, %69 : i64
      %70 = arith.constant 1 : i32
      %72 = arith.extsi %70 : i32 to i64
      %71 = arith.cmpi eq, %68, %72 : i64
      cf.cond_br %71, ^bb15, ^bb16
      ^bb15:
        %73 = llvm.load %61 : !llvm.ptr -> i64
        %74 = llvm.load %54 : !llvm.ptr -> i64
        %75 = arith.addi %73, %74 : i64
        %76 = arith.remsi %75, %arg2 : i64
        llvm.store %76, %61 : i64, !llvm.ptr
        cf.br ^bb17
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %77 = llvm.load %54 : !llvm.ptr -> i64
      %78 = arith.constant 2 : i32
      %80 = arith.extsi %78 : i32 to i64
      %79 = arith.muli %77, %80 : i64
      %81 = arith.remsi %79, %arg2 : i64
      llvm.store %81, %54 : i64, !llvm.ptr
      %82 = llvm.load %57 : !llvm.ptr -> i64
      %83 = arith.constant 2 : i32
      %85 = arith.extsi %83 : i32 to i64
      %84 = arith.divsi %82, %85 : i64
      llvm.store %84, %57 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %86 = llvm.load %61 : !llvm.ptr -> i64
    func.return %86 : i64
  }
  func.func @mod_pow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %87 = arith.constant 1 : i32
    %89 = arith.extsi %87 : i32 to i64
    %88 = arith.cmpi eq, %arg2, %89 : i64
    cf.cond_br %88, ^bb18, ^bb19
    ^bb18:
      %90 = arith.constant 0 : i32
      %91 = arith.extsi %90 : i32 to i64
      func.return %91 : i64
    ^bb19:
      cf.br ^bb20
    ^bb20:
    %92 = arith.constant 1 : i32
    %93 = arith.extsi %92 : i32 to i64
    %94 = llvm.mlir.constant(1 : i64) : i64
    %95 = llvm.alloca %94 x i64 : (i64) -> !llvm.ptr
    llvm.store %93, %95 : i64, !llvm.ptr
    %96 = arith.remsi %arg0, %arg2 : i64
    %97 = llvm.mlir.constant(1 : i64) : i64
    %98 = llvm.alloca %97 x i64 : (i64) -> !llvm.ptr
    llvm.store %96, %98 : i64, !llvm.ptr
    %99 = llvm.mlir.constant(1 : i64) : i64
    %100 = llvm.alloca %99 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %100 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %101 = llvm.load %100 : !llvm.ptr -> i64
    %102 = arith.constant 0 : i32
    %104 = arith.extsi %102 : i32 to i64
    %103 = arith.cmpi sgt, %101, %104 : i64
    cf.cond_br %103, ^bb22, ^bb23
    ^bb22:
      %105 = llvm.load %100 : !llvm.ptr -> i64
      %106 = arith.constant 2 : i32
      %108 = arith.extsi %106 : i32 to i64
      %107 = arith.remsi %105, %108 : i64
      %109 = arith.constant 1 : i32
      %111 = arith.extsi %109 : i32 to i64
      %110 = arith.cmpi eq, %107, %111 : i64
      cf.cond_br %110, ^bb24, ^bb25
      ^bb24:
        %113 = llvm.load %95 : !llvm.ptr -> i64
        %114 = llvm.load %98 : !llvm.ptr -> i64
        %112 = func.call @mulmod(%113, %114, %arg2) : (i64, i64, i64) -> i64
        llvm.store %112, %95 : i64, !llvm.ptr
        cf.br ^bb26
      ^bb25:
        cf.br ^bb26
      ^bb26:
      %116 = llvm.load %98 : !llvm.ptr -> i64
      %117 = llvm.load %98 : !llvm.ptr -> i64
      %115 = func.call @mulmod(%116, %117, %arg2) : (i64, i64, i64) -> i64
      llvm.store %115, %98 : i64, !llvm.ptr
      %118 = llvm.load %100 : !llvm.ptr -> i64
      %119 = arith.constant 2 : i32
      %121 = arith.extsi %119 : i32 to i64
      %120 = arith.divsi %118, %121 : i64
      llvm.store %120, %100 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %122 = llvm.load %95 : !llvm.ptr -> i64
    func.return %122 : i64
  }
  func.func @is_prime(%arg0: i64) -> i1 {
    %123 = arith.constant 2 : i32
    %125 = arith.extsi %123 : i32 to i64
    %124 = arith.cmpi slt, %arg0, %125 : i64
    cf.cond_br %124, ^bb27, ^bb28
    ^bb27:
      %126 = arith.constant 0 : i1
      func.return %126 : i1
    ^bb28:
      cf.br ^bb29
    ^bb29:
    %127 = arith.constant 4 : i32
    %129 = arith.extsi %127 : i32 to i64
    %128 = arith.cmpi slt, %arg0, %129 : i64
    cf.cond_br %128, ^bb30, ^bb31
    ^bb30:
      %130 = arith.constant 1 : i1
      func.return %130 : i1
    ^bb31:
      cf.br ^bb32
    ^bb32:
    %131 = arith.constant 2 : i32
    %133 = arith.extsi %131 : i32 to i64
    %132 = arith.remsi %arg0, %133 : i64
    %134 = arith.constant 0 : i32
    %136 = arith.extsi %134 : i32 to i64
    %135 = arith.cmpi eq, %132, %136 : i64
    %137 = scf.if %135 -> (i1) {
      %138 = arith.constant true
      scf.yield %138 : i1
    } else {
      %139 = arith.constant 3 : i32
      %141 = arith.extsi %139 : i32 to i64
      %140 = arith.remsi %arg0, %141 : i64
      %142 = arith.constant 0 : i32
      %144 = arith.extsi %142 : i32 to i64
      %143 = arith.cmpi eq, %140, %144 : i64
      scf.yield %143 : i1
    }
    cf.cond_br %137, ^bb33, ^bb34
    ^bb33:
      %145 = arith.constant 0 : i1
      func.return %145 : i1
    ^bb34:
      cf.br ^bb35
    ^bb35:
    %146 = arith.constant 5 : i32
    %147 = arith.extsi %146 : i32 to i64
    %148 = llvm.mlir.constant(1 : i64) : i64
    %149 = llvm.alloca %148 x i64 : (i64) -> !llvm.ptr
    llvm.store %147, %149 : i64, !llvm.ptr
    cf.br ^bb36
    ^bb36:
    %150 = llvm.load %149 : !llvm.ptr -> i64
    %151 = llvm.load %149 : !llvm.ptr -> i64
    %152 = arith.muli %150, %151 : i64
    %153 = arith.cmpi sle, %152, %arg0 : i64
    cf.cond_br %153, ^bb37, ^bb38
    ^bb37:
      %154 = llvm.load %149 : !llvm.ptr -> i64
      %155 = arith.remsi %arg0, %154 : i64
      %156 = arith.constant 0 : i32
      %158 = arith.extsi %156 : i32 to i64
      %157 = arith.cmpi eq, %155, %158 : i64
      %159 = scf.if %157 -> (i1) {
        %160 = arith.constant true
        scf.yield %160 : i1
      } else {
        %161 = llvm.load %149 : !llvm.ptr -> i64
        %162 = arith.constant 2 : i32
        %164 = arith.extsi %162 : i32 to i64
        %163 = arith.addi %161, %164 : i64
        %165 = arith.remsi %arg0, %163 : i64
        %166 = arith.constant 0 : i32
        %168 = arith.extsi %166 : i32 to i64
        %167 = arith.cmpi eq, %165, %168 : i64
        scf.yield %167 : i1
      }
      cf.cond_br %159, ^bb39, ^bb40
      ^bb39:
        %169 = arith.constant 0 : i1
        func.return %169 : i1
      ^bb40:
        cf.br ^bb41
      ^bb41:
      %170 = llvm.load %149 : !llvm.ptr -> i64
      %171 = arith.constant 6 : i32
      %173 = arith.extsi %171 : i32 to i64
      %172 = arith.addi %170, %173 : i64
      llvm.store %172, %149 : i64, !llvm.ptr
      cf.br ^bb36
    ^bb38:
    %174 = arith.constant 1 : i1
    func.return %174 : i1
  }
  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: LIMIT
  llvm.mlir.global internal constant @LIMIT(10000000 : i64) : i64
  // Constant: INV2
  llvm.mlir.global internal constant @INV2(500000004 : i64) : i64
  func.func @bsearch_desc(%arg0: !llvm.ptr, %arg1: i64, %arg2: i64) -> i64 {
    %175 = arith.constant 0 : i32
    %176 = arith.extsi %175 : i32 to i64
    %177 = llvm.mlir.constant(1 : i64) : i64
    %178 = llvm.alloca %177 x i64 : (i64) -> !llvm.ptr
    llvm.store %176, %178 : i64, !llvm.ptr
    %179 = arith.constant 1 : i32
    %181 = arith.extsi %179 : i32 to i64
    %180 = arith.subi %arg1, %181 : i64
    %182 = llvm.mlir.constant(1 : i64) : i64
    %183 = llvm.alloca %182 x i64 : (i64) -> !llvm.ptr
    llvm.store %180, %183 : i64, !llvm.ptr
    cf.br ^bb42
    ^bb42:
    %184 = llvm.load %178 : !llvm.ptr -> i64
    %185 = llvm.load %183 : !llvm.ptr -> i64
    %186 = arith.cmpi sle, %184, %185 : i64
    cf.cond_br %186, ^bb43, ^bb44
    ^bb43:
      %187 = llvm.load %178 : !llvm.ptr -> i64
      %188 = llvm.load %183 : !llvm.ptr -> i64
      %189 = arith.addi %187, %188 : i64
      %190 = arith.constant 2 : i32
      %192 = arith.extsi %190 : i32 to i64
      %191 = arith.divsi %189, %192 : i64
      %194 = llvm.getelementptr %arg0[%191] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %193 = llvm.load %194 : !llvm.ptr -> i64
      %195 = arith.cmpi eq, %193, %arg2 : i64
      cf.cond_br %195, ^bb45, ^bb46
      ^bb45:
        func.return %191 : i64
      ^bb46:
        cf.br ^bb47
      ^bb47:
      %197 = llvm.getelementptr %arg0[%191] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %196 = llvm.load %197 : !llvm.ptr -> i64
      %198 = arith.cmpi sgt, %196, %arg2 : i64
      cf.cond_br %198, ^bb48, ^bb49
      ^bb48:
        %199 = arith.constant 1 : i32
        %201 = arith.extsi %199 : i32 to i64
        %200 = arith.addi %191, %201 : i64
        llvm.store %200, %178 : i64, !llvm.ptr
        cf.br ^bb50
      ^bb49:
        %202 = arith.constant 1 : i32
        %204 = arith.extsi %202 : i32 to i64
        %203 = arith.subi %191, %204 : i64
        llvm.store %203, %183 : i64, !llvm.ptr
        cf.br ^bb50
      ^bb50:
      cf.br ^bb42
    ^bb44:
    %205 = arith.constant 1 : i32
    %207 = arith.constant 0 : i32
    %206 = arith.subi %207, %205 : i32
    %208 = arith.extsi %206 : i32 to i64
    func.return %208 : i64
  }
  func.func @main() -> i32 {
    %209 = arith.constant 95705032704 : i32
    %210 = arith.extsi %209 : i32 to i64
    %212 = llvm.mlir.addressof @LIMIT : !llvm.ptr
    %213 = llvm.load %212 : !llvm.ptr -> i64
    %214 = arith.constant 1 : i32
    %216 = arith.extsi %214 : i32 to i64
    %215 = arith.addi %213, %216 : i64
    %217 = arith.constant 8 : i32
    %218 = arith.extsi %217 : i32 to i64
    %211 = func.call @calloc(%215, %218) : (i64, i64) -> !llvm.ptr
    %219 = llvm.mlir.zero : !llvm.ptr
    %220 = llvm.icmp "eq" %211, %219 : !llvm.ptr
    cf.cond_br %220, ^bb51, ^bb52
    ^bb51:
      %221 = arith.constant 1 : i32
      func.return %221 : i32
    ^bb52:
      cf.br ^bb53
    ^bb53:
    %222 = arith.constant 1 : i32
    %223 = arith.constant 1 : i32
    %224 = arith.extsi %222 : i32 to i64
    %225 = arith.extsi %223 : i32 to i64
    %226 = llvm.getelementptr %211[%225] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %224, %226 : i64, !llvm.ptr
    %228 = llvm.mlir.addressof @LIMIT : !llvm.ptr
    %229 = llvm.load %228 : !llvm.ptr -> i64
    %230 = arith.constant 1 : i32
    %232 = arith.extsi %230 : i32 to i64
    %231 = arith.addi %229, %232 : i64
    %233 = arith.constant 1 : i32
    %234 = arith.extsi %233 : i32 to i64
    %227 = func.call @calloc(%231, %234) : (i64, i64) -> !llvm.ptr
    %236 = llvm.mlir.addressof @LIMIT : !llvm.ptr
    %237 = llvm.load %236 : !llvm.ptr -> i64
    %238 = arith.constant 10 : i32
    %240 = arith.extsi %238 : i32 to i64
    %239 = arith.divsi %237, %240 : i64
    %241 = arith.constant 100 : i32
    %243 = arith.extsi %241 : i32 to i64
    %242 = arith.addi %239, %243 : i64
    %244 = arith.constant 8 : i32
    %245 = arith.extsi %244 : i32 to i64
    %235 = func.call @calloc(%242, %245) : (i64, i64) -> !llvm.ptr
    %246 = arith.constant 0 : i32
    %247 = arith.extsi %246 : i32 to i64
    %248 = llvm.mlir.constant(1 : i64) : i64
    %249 = llvm.alloca %248 x i64 : (i64) -> !llvm.ptr
    llvm.store %247, %249 : i64, !llvm.ptr
    %250 = arith.constant 2 : i32
    %251 = llvm.mlir.addressof @LIMIT : !llvm.ptr
    %252 = llvm.load %251 : !llvm.ptr -> i64
    %253 = arith.constant 1 : i32
    %255 = arith.extsi %253 : i32 to i64
    %254 = arith.addi %252, %255 : i64
    %256 = arith.index_cast %250 : i32 to index
    %257 = arith.index_cast %254 : i32 to index
    %259 = arith.constant 1 : index
    %260 = arith.constant -1 : index
    %261 = arith.cmpi sle, %256, %257 : index
    %258 = arith.select %261, %259, %260 : index
    cf.br ^bb54(%256 : index)
    ^bb54(%262: index):
    %263 = arith.cmpi slt, %262, %257 : index
    %264 = arith.cmpi sgt, %262, %257 : index
    %265 = arith.select %261, %263, %264 : i1
    cf.cond_br %265, ^bb55(%262 : index), ^bb56(%262 : index)
    ^bb55(%266: index):
      %268 = arith.index_cast %266 : index to i64
      %269 = llvm.getelementptr %227[%268] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %267 = llvm.load %269 : !llvm.ptr -> i8
      %270 = arith.constant 0 : i32
      %272 = arith.extsi %267 : i8 to i32
      %271 = arith.cmpi eq, %272, %270 : i32
      cf.cond_br %271, ^bb57, ^bb58
      ^bb57:
        %273 = llvm.load %249 : !llvm.ptr -> i64
        %274 = arith.index_cast %266 : index to i64
        %275 = llvm.getelementptr %235[%273] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %274, %275 : i64, !llvm.ptr
        %276 = llvm.load %249 : !llvm.ptr -> i64
        %277 = arith.constant 1 : i32
        %279 = arith.extsi %277 : i32 to i64
        %278 = arith.addi %276, %279 : i64
        llvm.store %278, %249 : i64, !llvm.ptr
        %280 = arith.constant 1 : i32
        %282 = arith.index_cast %266 : index to i32
        %281 = arith.subi %282, %280 : i32
        %283 = arith.extsi %281 : i32 to i64
        %284 = arith.index_cast %266 : index to i64
        %285 = llvm.getelementptr %211[%284] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %283, %285 : i64, !llvm.ptr
        cf.br ^bb59
      ^bb58:
        cf.br ^bb59
      ^bb59:
      %286 = arith.constant 0 : i32
      %287 = llvm.load %249 : !llvm.ptr -> i64
      %288 = arith.index_cast %286 : i32 to index
      %289 = arith.index_cast %287 : i32 to index
      %291 = arith.constant 1 : index
      %292 = arith.constant -1 : index
      %293 = arith.cmpi sle, %288, %289 : index
      %290 = arith.select %293, %291, %292 : index
      cf.br ^bb60(%288 : index)
      ^bb60(%294: index):
      %295 = arith.cmpi slt, %294, %289 : index
      %296 = arith.cmpi sgt, %294, %289 : index
      %297 = arith.select %293, %295, %296 : i1
      cf.cond_br %297, ^bb61(%294 : index), ^bb62(%294 : index)
      ^bb61(%298: index):
        %300 = arith.index_cast %298 : index to i64
        %301 = llvm.getelementptr %235[%300] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %299 = llvm.load %301 : !llvm.ptr -> i64
        %303 = arith.index_cast %266 : index to i32
        %304 = arith.trunci %299 : i64 to i32
        %302 = arith.muli %303, %304 : i32
        %305 = arith.extsi %302 : i32 to i64
        %306 = llvm.mlir.addressof @LIMIT : !llvm.ptr
        %307 = llvm.load %306 : !llvm.ptr -> i64
        %308 = arith.cmpi sgt, %305, %307 : i64
        cf.cond_br %308, ^bb63, ^bb64
        ^bb63:
          cf.br ^bb62(%298 : index)
        ^bb64:
          cf.br ^bb65
        ^bb65:
        %309 = arith.constant 1 : i32
        %310 = arith.trunci %309 : i32 to i8
        %311 = llvm.getelementptr %227[%305] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        llvm.store %310, %311 : i8, !llvm.ptr
        %313 = arith.index_cast %266 : index to i32
        %314 = arith.trunci %299 : i64 to i32
        %312 = arith.remsi %313, %314 : i32
        %315 = arith.constant 0 : i32
        %316 = arith.cmpi eq, %312, %315 : i32
        cf.cond_br %316, ^bb66, ^bb67
        ^bb66:
          %318 = arith.index_cast %266 : index to i64
          %319 = llvm.getelementptr %211[%318] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %317 = llvm.load %319 : !llvm.ptr -> i64
          %320 = arith.muli %317, %299 : i64
          %321 = llvm.getelementptr %211[%305] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %320, %321 : i64, !llvm.ptr
          cf.br ^bb62(%298 : index)
        ^bb67:
          %323 = arith.index_cast %266 : index to i64
          %324 = llvm.getelementptr %211[%323] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %322 = llvm.load %324 : !llvm.ptr -> i64
          %325 = arith.constant 1 : i32
          %327 = arith.extsi %325 : i32 to i64
          %326 = arith.subi %299, %327 : i64
          %328 = arith.muli %322, %326 : i64
          %329 = llvm.getelementptr %211[%305] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %328, %329 : i64, !llvm.ptr
          cf.br ^bb68
        ^bb68:
        %330 = arith.addi %298, %290 : index
        cf.br ^bb60(%330 : index)
      ^bb62(%331: index):
      %332 = arith.addi %266, %258 : index
      cf.br ^bb54(%332 : index)
    ^bb56(%333: index):
    %335 = llvm.mlir.addressof @LIMIT : !llvm.ptr
    %336 = llvm.load %335 : !llvm.ptr -> i64
    %337 = arith.constant 1 : i32
    %339 = arith.extsi %337 : i32 to i64
    %338 = arith.addi %336, %339 : i64
    %340 = arith.constant 8 : i32
    %341 = arith.extsi %340 : i32 to i64
    %334 = func.call @calloc(%338, %341) : (i64, i64) -> !llvm.ptr
    %342 = arith.constant 0 : i32
    %343 = arith.extsi %342 : i32 to i64
    %344 = llvm.mlir.constant(1 : i64) : i64
    %345 = llvm.alloca %344 x i64 : (i64) -> !llvm.ptr
    llvm.store %343, %345 : i64, !llvm.ptr
    %346 = arith.constant 1 : i32
    %347 = llvm.mlir.addressof @LIMIT : !llvm.ptr
    %348 = llvm.load %347 : !llvm.ptr -> i64
    %349 = arith.constant 1 : i32
    %351 = arith.extsi %349 : i32 to i64
    %350 = arith.addi %348, %351 : i64
    %352 = arith.index_cast %346 : i32 to index
    %353 = arith.index_cast %350 : i32 to index
    %355 = arith.constant 1 : index
    %356 = arith.constant -1 : index
    %357 = arith.cmpi sle, %352, %353 : index
    %354 = arith.select %357, %355, %356 : index
    cf.br ^bb69(%352 : index)
    ^bb69(%358: index):
    %359 = arith.cmpi slt, %358, %353 : index
    %360 = arith.cmpi sgt, %358, %353 : index
    %361 = arith.select %357, %359, %360 : i1
    cf.cond_br %361, ^bb70(%358 : index), ^bb71(%358 : index)
    ^bb70(%362: index):
      %363 = llvm.load %345 : !llvm.ptr -> i64
      %365 = arith.index_cast %362 : index to i64
      %366 = llvm.getelementptr %211[%365] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %364 = llvm.load %366 : !llvm.ptr -> i64
      %367 = arith.addi %363, %364 : i64
      %368 = llvm.mlir.addressof @MOD : !llvm.ptr
      %369 = llvm.load %368 : !llvm.ptr -> i64
      %370 = arith.remsi %367, %369 : i64
      llvm.store %370, %345 : i64, !llvm.ptr
      %371 = llvm.load %345 : !llvm.ptr -> i64
      %372 = arith.index_cast %362 : index to i64
      %373 = llvm.getelementptr %334[%372] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %371, %373 : i64, !llvm.ptr
      %374 = arith.addi %362, %354 : index
      cf.br ^bb69(%374 : index)
    ^bb71(%375: index):
    func.call @free(%227) : (!llvm.ptr) -> ()
    func.call @free(%235) : (!llvm.ptr) -> ()
    func.call @free(%211) : (!llvm.ptr) -> ()
    %379 = func.call @isqrt(%210) : (i64) -> i64
    %380 = arith.constant 2 : i32
    %382 = arith.extsi %380 : i32 to i64
    %381 = arith.muli %382, %379 : i64
    %383 = arith.constant 10 : i32
    %385 = arith.extsi %383 : i32 to i64
    %384 = arith.addi %381, %385 : i64
    %387 = arith.constant 8 : i32
    %388 = arith.extsi %387 : i32 to i64
    %386 = func.call @calloc(%384, %388) : (i64, i64) -> !llvm.ptr
    %390 = arith.constant 8 : i32
    %391 = arith.extsi %390 : i32 to i64
    %389 = func.call @calloc(%384, %391) : (i64, i64) -> !llvm.ptr
    %392 = arith.constant 0 : i32
    %393 = arith.extsi %392 : i32 to i64
    %394 = llvm.mlir.constant(1 : i64) : i64
    %395 = llvm.alloca %394 x i64 : (i64) -> !llvm.ptr
    llvm.store %393, %395 : i64, !llvm.ptr
    %396 = arith.constant 1 : i32
    %397 = arith.constant 1 : i32
    %399 = arith.extsi %397 : i32 to i64
    %398 = arith.addi %379, %399 : i64
    %400 = arith.index_cast %396 : i32 to index
    %401 = arith.index_cast %398 : i32 to index
    %403 = arith.constant 1 : index
    %404 = arith.constant -1 : index
    %405 = arith.cmpi sle, %400, %401 : index
    %402 = arith.select %405, %403, %404 : index
    cf.br ^bb72(%400 : index)
    ^bb72(%406: index):
    %407 = arith.cmpi slt, %406, %401 : index
    %408 = arith.cmpi sgt, %406, %401 : index
    %409 = arith.select %405, %407, %408 : i1
    cf.cond_br %409, ^bb73(%406 : index), ^bb74(%406 : index)
    ^bb73(%410: index):
      %412 = arith.trunci %210 : i64 to i32
      %413 = arith.index_cast %410 : index to i32
      %411 = arith.divsi %412, %413 : i32
      %414 = llvm.load %395 : !llvm.ptr -> i64
      %415 = arith.extsi %411 : i32 to i64
      %416 = llvm.getelementptr %386[%414] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %415, %416 : i64, !llvm.ptr
      %417 = llvm.load %395 : !llvm.ptr -> i64
      %418 = arith.constant 1 : i32
      %420 = arith.extsi %418 : i32 to i64
      %419 = arith.addi %417, %420 : i64
      llvm.store %419, %395 : i64, !llvm.ptr
      %421 = arith.addi %410, %402 : index
      cf.br ^bb72(%421 : index)
    ^bb74(%422: index):
    %423 = arith.constant 1 : i32
    %425 = arith.extsi %423 : i32 to i64
    %424 = arith.addi %379, %425 : i64
    %426 = arith.divsi %210, %424 : i64
    %427 = arith.constant 0 : i32
    %428 = arith.index_cast %427 : i32 to index
    %429 = arith.index_cast %426 : i32 to index
    %431 = arith.constant 1 : index
    %432 = arith.constant -1 : index
    %433 = arith.cmpi sle, %428, %429 : index
    %430 = arith.select %433, %431, %432 : index
    cf.br ^bb75(%428 : index)
    ^bb75(%434: index):
    %435 = arith.cmpi slt, %434, %429 : index
    %436 = arith.cmpi sgt, %434, %429 : index
    %437 = arith.select %433, %435, %436 : i1
    cf.cond_br %437, ^bb76(%434 : index), ^bb77(%434 : index)
    ^bb76(%438: index):
      %440 = arith.trunci %426 : i64 to i32
      %441 = arith.index_cast %438 : index to i32
      %439 = arith.subi %440, %441 : i32
      %442 = llvm.load %395 : !llvm.ptr -> i64
      %443 = arith.extsi %439 : i32 to i64
      %444 = llvm.getelementptr %386[%442] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %443, %444 : i64, !llvm.ptr
      %445 = llvm.load %395 : !llvm.ptr -> i64
      %446 = arith.constant 1 : i32
      %448 = arith.extsi %446 : i32 to i64
      %447 = arith.addi %445, %448 : i64
      llvm.store %447, %395 : i64, !llvm.ptr
      %449 = arith.addi %438, %430 : index
      cf.br ^bb75(%449 : index)
    ^bb77(%450: index):
    %451 = arith.constant 0 : i32
    %452 = arith.extsi %451 : i32 to i64
    %453 = llvm.mlir.constant(1 : i64) : i64
    %454 = llvm.alloca %453 x i64 : (i64) -> !llvm.ptr
    llvm.store %452, %454 : i64, !llvm.ptr
    %455 = arith.constant 0 : i32
    %456 = llvm.load %395 : !llvm.ptr -> i64
    %457 = arith.index_cast %455 : i32 to index
    %458 = arith.index_cast %456 : i32 to index
    %460 = arith.constant 1 : index
    %461 = arith.constant -1 : index
    %462 = arith.cmpi sle, %457, %458 : index
    %459 = arith.select %462, %460, %461 : index
    cf.br ^bb78(%457 : index)
    ^bb78(%463: index):
    %464 = arith.cmpi slt, %463, %458 : index
    %465 = arith.cmpi sgt, %463, %458 : index
    %466 = arith.select %462, %464, %465 : i1
    cf.cond_br %466, ^bb79(%463 : index), ^bb80(%463 : index)
    ^bb79(%467: index):
      %468 = arith.constant 0 : i32
      %470 = arith.index_cast %467 : index to i32
      %469 = arith.cmpi eq, %470, %468 : i32
      %471 = scf.if %469 -> (i1) {
        %472 = arith.constant true
        scf.yield %472 : i1
      } else {
        %474 = arith.index_cast %467 : index to i64
        %475 = llvm.getelementptr %386[%474] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %473 = llvm.load %475 : !llvm.ptr -> i64
        %477 = arith.constant 1 : i32
        %479 = arith.index_cast %467 : index to i32
        %478 = arith.subi %479, %477 : i32
        %480 = arith.extsi %478 : i32 to i64
        %481 = llvm.getelementptr %386[%480] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %476 = llvm.load %481 : !llvm.ptr -> i64
        %482 = arith.cmpi ne, %473, %476 : i64
        scf.yield %482 : i1
      }
      cf.cond_br %471, ^bb81, ^bb82
      ^bb81:
        %484 = arith.index_cast %467 : index to i64
        %485 = llvm.getelementptr %386[%484] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %483 = llvm.load %485 : !llvm.ptr -> i64
        %486 = llvm.load %454 : !llvm.ptr -> i64
        %487 = llvm.getelementptr %386[%486] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %483, %487 : i64, !llvm.ptr
        %488 = llvm.load %454 : !llvm.ptr -> i64
        %489 = arith.constant 1 : i32
        %491 = arith.extsi %489 : i32 to i64
        %490 = arith.addi %488, %491 : i64
        llvm.store %490, %454 : i64, !llvm.ptr
        cf.br ^bb83
      ^bb82:
        cf.br ^bb83
      ^bb83:
      %492 = arith.addi %467, %459 : index
      cf.br ^bb78(%492 : index)
    ^bb80(%493: index):
    %494 = llvm.load %454 : !llvm.ptr -> i64
    llvm.store %494, %395 : i64, !llvm.ptr
    %495 = arith.constant 0 : i32
    %496 = llvm.load %395 : !llvm.ptr -> i64
    %497 = arith.index_cast %495 : i32 to index
    %498 = arith.index_cast %496 : i32 to index
    %500 = arith.constant 1 : index
    %501 = arith.constant -1 : index
    %502 = arith.cmpi sle, %497, %498 : index
    %499 = arith.select %502, %500, %501 : index
    cf.br ^bb84(%497 : index)
    ^bb84(%503: index):
    %504 = arith.cmpi slt, %503, %498 : index
    %505 = arith.cmpi sgt, %503, %498 : index
    %506 = arith.select %502, %504, %505 : i1
    cf.cond_br %506, ^bb85(%503 : index), ^bb86(%503 : index)
    ^bb85(%507: index):
      %508 = llvm.load %395 : !llvm.ptr -> i64
      %509 = arith.constant 1 : i32
      %511 = arith.extsi %509 : i32 to i64
      %510 = arith.subi %508, %511 : i64
      %513 = arith.trunci %510 : i64 to i32
      %514 = arith.index_cast %507 : index to i32
      %512 = arith.subi %513, %514 : i32
      %515 = arith.extsi %512 : i32 to i64
      %517 = llvm.getelementptr %386[%515] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %516 = llvm.load %517 : !llvm.ptr -> i64
      %518 = llvm.mlir.addressof @LIMIT : !llvm.ptr
      %519 = llvm.load %518 : !llvm.ptr -> i64
      %520 = arith.cmpi sle, %516, %519 : i64
      cf.cond_br %520, ^bb87, ^bb88
      ^bb87:
        %522 = llvm.getelementptr %334[%516] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %521 = llvm.load %522 : !llvm.ptr -> i64
        %523 = llvm.getelementptr %389[%515] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %521, %523 : i64, !llvm.ptr
        %524 = arith.addi %507, %499 : index
        cf.br ^bb84(%524 : index)
      ^bb88:
        cf.br ^bb89
      ^bb89:
      %525 = arith.constant 0 : i32
      %527 = arith.extsi %525 : i32 to i64
      %526 = arith.cmpi eq, %516, %527 : i64
      cf.cond_br %526, ^bb90, ^bb91
      ^bb90:
        %528 = arith.constant 0 : i32
        %529 = arith.extsi %528 : i32 to i64
        %530 = llvm.getelementptr %389[%515] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %529, %530 : i64, !llvm.ptr
        %531 = arith.addi %507, %499 : index
        cf.br ^bb84(%531 : index)
      ^bb91:
        cf.br ^bb92
      ^bb92:
      %532 = arith.constant 0 : i32
      %533 = arith.extsi %532 : i32 to i64
      %534 = llvm.mlir.constant(1 : i64) : i64
      %535 = llvm.alloca %534 x i64 : (i64) -> !llvm.ptr
      llvm.store %533, %535 : i64, !llvm.ptr
      %536 = llvm.mlir.addressof @MOD : !llvm.ptr
      %537 = llvm.load %536 : !llvm.ptr -> i64
      %538 = arith.remsi %516, %537 : i64
      %539 = arith.constant 1 : i32
      %541 = arith.extsi %539 : i32 to i64
      %540 = arith.addi %516, %541 : i64
      %542 = llvm.mlir.addressof @MOD : !llvm.ptr
      %543 = llvm.load %542 : !llvm.ptr -> i64
      %544 = arith.remsi %540, %543 : i64
      %545 = arith.extsi %538 : i64 to i128
      %546 = arith.extsi %544 : i64 to i128
      %548 = arith.trunci %545 : i128 to i64
      %549 = arith.trunci %546 : i128 to i64
      %547 = arith.muli %548, %549 : i64
      %550 = llvm.mlir.addressof @MOD : !llvm.ptr
      %551 = llvm.load %550 : !llvm.ptr -> i64
      %552 = arith.extsi %551 : i64 to i128
      %554 = arith.trunci %552 : i128 to i64
      %553 = arith.remsi %547, %554 : i64
      %555 = arith.extsi %553 : i64 to i128
      %556 = llvm.mlir.constant(1 : i64) : i64
      %557 = llvm.alloca %556 x i128 : (i64) -> !llvm.ptr
      llvm.store %555, %557 : i128, !llvm.ptr
      %558 = llvm.load %557 : !llvm.ptr -> i128
      %559 = llvm.mlir.addressof @INV2 : !llvm.ptr
      %560 = llvm.load %559 : !llvm.ptr -> i64
      %561 = arith.extsi %560 : i64 to i128
      %563 = arith.trunci %558 : i128 to i64
      %564 = arith.trunci %561 : i128 to i64
      %562 = arith.muli %563, %564 : i64
      %565 = llvm.mlir.addressof @MOD : !llvm.ptr
      %566 = llvm.load %565 : !llvm.ptr -> i64
      %567 = arith.extsi %566 : i64 to i128
      %569 = arith.trunci %567 : i128 to i64
      %568 = arith.remsi %562, %569 : i64
      %570 = arith.extsi %568 : i64 to i128
      llvm.store %570, %557 : i128, !llvm.ptr
      %571 = llvm.load %557 : !llvm.ptr -> i128
      %572 = arith.trunci %571 : i128 to i64
      llvm.store %572, %535 : i64, !llvm.ptr
      %573 = arith.constant 2 : i32
      %574 = arith.extsi %573 : i32 to i64
      %575 = llvm.mlir.constant(1 : i64) : i64
      %576 = llvm.alloca %575 x i64 : (i64) -> !llvm.ptr
      llvm.store %574, %576 : i64, !llvm.ptr
      cf.br ^bb93
      ^bb93:
      %577 = llvm.load %576 : !llvm.ptr -> i64
      %578 = arith.cmpi sle, %577, %516 : i64
      cf.cond_br %578, ^bb94, ^bb95
      ^bb94:
        %579 = llvm.load %576 : !llvm.ptr -> i64
        %580 = arith.divsi %516, %579 : i64
        %581 = arith.constant 0 : i32
        %583 = arith.extsi %581 : i32 to i64
        %582 = arith.cmpi eq, %580, %583 : i64
        cf.cond_br %582, ^bb96, ^bb97
        ^bb96:
          cf.br ^bb95
        ^bb97:
          cf.br ^bb98
        ^bb98:
        %584 = arith.divsi %516, %580 : i64
        %585 = llvm.mlir.undef : i64
        %586 = llvm.mlir.addressof @LIMIT : !llvm.ptr
        %587 = llvm.load %586 : !llvm.ptr -> i64
        %588 = arith.cmpi sle, %580, %587 : i64
        cf.cond_br %588, ^bb99, ^bb100
        ^bb99:
          %590 = llvm.getelementptr %334[%580] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %589 = llvm.load %590 : !llvm.ptr -> i64
          cf.br ^bb101(%589 : i64)
        ^bb100:
          %592 = llvm.load %395 : !llvm.ptr -> i64
          %591 = func.call @bsearch_desc(%386, %592, %580) : (!llvm.ptr, i64, i64) -> i64
          %593 = arith.constant 0 : i32
          %595 = arith.extsi %593 : i32 to i64
          %594 = arith.cmpi slt, %591, %595 : i64
          cf.cond_br %594, ^bb102, ^bb103
          ^bb102:
            %596 = arith.constant 1 : i32
            func.return %596 : i32
          ^bb103:
            cf.br ^bb104
          ^bb104:
          %598 = llvm.getelementptr %389[%591] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %597 = llvm.load %598 : !llvm.ptr -> i64
          cf.br ^bb101(%597 : i64)
        ^bb101(%599: i64):
        %600 = llvm.load %576 : !llvm.ptr -> i64
        %601 = arith.subi %584, %600 : i64
        %602 = arith.constant 1 : i32
        %604 = arith.extsi %602 : i32 to i64
        %603 = arith.addi %601, %604 : i64
        %605 = llvm.mlir.addressof @MOD : !llvm.ptr
        %606 = llvm.load %605 : !llvm.ptr -> i64
        %607 = arith.remsi %603, %606 : i64
        %608 = arith.extsi %607 : i64 to i128
        %609 = arith.extsi %599 : i64 to i128
        %611 = arith.trunci %608 : i128 to i64
        %612 = arith.trunci %609 : i128 to i64
        %610 = arith.muli %611, %612 : i64
        %613 = llvm.mlir.addressof @MOD : !llvm.ptr
        %614 = llvm.load %613 : !llvm.ptr -> i64
        %615 = arith.extsi %614 : i64 to i128
        %617 = arith.trunci %615 : i128 to i64
        %616 = arith.remsi %610, %617 : i64
        %618 = arith.extsi %616 : i64 to i128
        %619 = llvm.load %535 : !llvm.ptr -> i64
        %620 = arith.extsi %619 : i64 to i128
        %622 = arith.trunci %620 : i128 to i64
        %623 = arith.trunci %618 : i128 to i64
        %621 = arith.subi %622, %623 : i64
        %624 = arith.extsi %621 : i64 to i128
        %625 = llvm.mlir.constant(1 : i64) : i64
        %626 = llvm.alloca %625 x i128 : (i64) -> !llvm.ptr
        llvm.store %624, %626 : i128, !llvm.ptr
        %627 = llvm.load %626 : !llvm.ptr -> i128
        %628 = llvm.mlir.addressof @MOD : !llvm.ptr
        %629 = llvm.load %628 : !llvm.ptr -> i64
        %630 = arith.extsi %629 : i64 to i128
        %632 = arith.trunci %627 : i128 to i64
        %633 = arith.trunci %630 : i128 to i64
        %631 = arith.remsi %632, %633 : i64
        %634 = arith.extsi %631 : i64 to i128
        llvm.store %634, %626 : i128, !llvm.ptr
        %635 = llvm.load %626 : !llvm.ptr -> i128
        %636 = arith.constant 0 : i32
        %638 = arith.trunci %635 : i128 to i64
        %639 = arith.extsi %636 : i32 to i64
        %637 = arith.cmpi slt, %638, %639 : i64
        cf.cond_br %637, ^bb105, ^bb106
        ^bb105:
          %640 = llvm.load %626 : !llvm.ptr -> i128
          %641 = llvm.mlir.addressof @MOD : !llvm.ptr
          %642 = llvm.load %641 : !llvm.ptr -> i64
          %643 = arith.extsi %642 : i64 to i128
          %645 = arith.trunci %640 : i128 to i64
          %646 = arith.trunci %643 : i128 to i64
          %644 = arith.addi %645, %646 : i64
          %647 = arith.extsi %644 : i64 to i128
          llvm.store %647, %626 : i128, !llvm.ptr
          cf.br ^bb107
        ^bb106:
          cf.br ^bb107
        ^bb107:
        %648 = llvm.load %626 : !llvm.ptr -> i128
        %649 = arith.trunci %648 : i128 to i64
        llvm.store %649, %535 : i64, !llvm.ptr
        %650 = arith.constant 1 : i32
        %652 = arith.extsi %650 : i32 to i64
        %651 = arith.addi %584, %652 : i64
        llvm.store %651, %576 : i64, !llvm.ptr
        cf.br ^bb93
      ^bb95:
      %653 = llvm.load %535 : !llvm.ptr -> i64
      %654 = llvm.getelementptr %389[%515] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %653, %654 : i64, !llvm.ptr
      %655 = arith.addi %507, %499 : index
      cf.br ^bb84(%655 : index)
    ^bb86(%656: index):
    %657 = arith.constant 0 : i32
    %658 = arith.extsi %657 : i32 to i64
    %659 = llvm.mlir.constant(1 : i64) : i64
    %660 = llvm.alloca %659 x i64 : (i64) -> !llvm.ptr
    llvm.store %658, %660 : i64, !llvm.ptr
    %661 = arith.constant 1 : i32
    %662 = arith.extsi %661 : i32 to i64
    %663 = llvm.mlir.constant(1 : i64) : i64
    %664 = llvm.alloca %663 x i64 : (i64) -> !llvm.ptr
    llvm.store %662, %664 : i64, !llvm.ptr
    cf.br ^bb108
    ^bb108:
    %665 = llvm.load %664 : !llvm.ptr -> i64
    %666 = arith.shrsi %210, %665 : i64
    %667 = arith.constant 0 : i32
    %669 = arith.extsi %667 : i32 to i64
    %668 = arith.cmpi sgt, %666, %669 : i64
    cf.cond_br %668, ^bb109, ^bb110
    ^bb109:
      %670 = llvm.load %664 : !llvm.ptr -> i64
      %671 = arith.shrsi %210, %670 : i64
      %672 = llvm.mlir.undef : i64
      %673 = llvm.mlir.addressof @LIMIT : !llvm.ptr
      %674 = llvm.load %673 : !llvm.ptr -> i64
      %675 = arith.cmpi sle, %671, %674 : i64
      cf.cond_br %675, ^bb111, ^bb112
      ^bb111:
        %677 = llvm.getelementptr %334[%671] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %676 = llvm.load %677 : !llvm.ptr -> i64
        cf.br ^bb113(%676 : i64)
      ^bb112:
        %679 = llvm.load %395 : !llvm.ptr -> i64
        %678 = func.call @bsearch_desc(%386, %679, %671) : (!llvm.ptr, i64, i64) -> i64
        %680 = arith.constant 0 : i32
        %682 = arith.extsi %680 : i32 to i64
        %681 = arith.cmpi slt, %678, %682 : i64
        cf.cond_br %681, ^bb114, ^bb115
        ^bb114:
          %683 = arith.constant 1 : i32
          func.return %683 : i32
        ^bb115:
          cf.br ^bb116
        ^bb116:
        %685 = llvm.getelementptr %389[%678] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %684 = llvm.load %685 : !llvm.ptr -> i64
        cf.br ^bb113(%684 : i64)
      ^bb113(%686: i64):
      %687 = llvm.load %660 : !llvm.ptr -> i64
      %688 = arith.addi %687, %686 : i64
      %689 = arith.constant 1 : i32
      %691 = arith.extsi %689 : i32 to i64
      %690 = arith.subi %688, %691 : i64
      %692 = llvm.mlir.addressof @MOD : !llvm.ptr
      %693 = llvm.load %692 : !llvm.ptr -> i64
      %694 = arith.addi %690, %693 : i64
      %695 = llvm.mlir.addressof @MOD : !llvm.ptr
      %696 = llvm.load %695 : !llvm.ptr -> i64
      %697 = arith.remsi %694, %696 : i64
      llvm.store %697, %660 : i64, !llvm.ptr
      %698 = llvm.load %664 : !llvm.ptr -> i64
      %699 = arith.constant 1 : i32
      %701 = arith.extsi %699 : i32 to i64
      %700 = arith.addi %698, %701 : i64
      llvm.store %700, %664 : i64, !llvm.ptr
      cf.br ^bb108
    ^bb110:
    %702 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %703 = llvm.load %660 : !llvm.ptr -> i64
    %704 = llvm.call @printf(%702, %703) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%389) : (!llvm.ptr) -> ()
    func.call @free(%386) : (!llvm.ptr) -> ()
    func.call @free(%334) : (!llvm.ptr) -> ()
    %708 = arith.constant 0 : i32
    func.return %708 : i32
  }
}