Problem 407

Sum M(n) for 1<=n<=10^7.

Answer39782849136421
Output39782849136421
StatusPASS
Native helperno
Runtime1750 ms
Peak memory40224 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log n)
Space complexityO(n)O(n)
ApproachFlow solutionIdempotents via divisor analysis
VerdictSuboptimal

Flow source

# Project Euler 407
# Sum M(n) for 1<=n<=10^7.

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

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

function main() -> i32 {
    let LIMIT: i64 = 10000000
    let spf: ptr<i32> = calloc(LIMIT + 1, 4)
    if spf == null { return 1 }
    let mut i: i64 = 0
    while i <= LIMIT {
        spf[i] = i as i32
        i = i + 1
    }
    i = 2
    while i * i <= LIMIT {
        if (spf[i] as i64) == i {
            let mut j: i64 = i * i
            while j <= LIMIT {
                if (spf[j] as i64) == j {
                    spf[j] = i as i32
                }
                j = j + i
            }
        }
        i = i + 1
    }

    let mut total: i64 = 0
    let mut n: i64 = 2
    while n <= LIMIT {
        let mut t: i64 = n
        let p0: i64 = spf[t] as i64
        let mut pk: i64 = 1
        while t % p0 == 0 {
            t = t / p0
            pk = pk * p0
        }
        if t == 1 {
            total = total + 1
            n = n + 1
            continue
        }
        # CRT subsets of sum of e_i where e_i ≡ 1 mod q_i, 0 mod n/q_i
        let mut sums: ptr<i64> = calloc(512, 8)
        if sums == null { return 1 }
        let mut sc: i64 = 1
        sums[0] = 0
        let mut min_gt: i64 = n

        # first factor pk
        let mut q: i64 = pk
        let n_div: i64 = n / q
        let e: i64 = n_div * modinv(n_div, q)
        let mut v: i64 = e % n
        if v > 1 && v < min_gt { min_gt = v }
        sums[sc] = v
        sc = sc + 1

        while t > 1 {
            let p: i64 = spf[t] as i64
            pk = 1
            while t % p == 0 {
                t = t / p
                pk = pk * p
            }
            q = pk
            let nd: i64 = n / q
            let ee: i64 = nd * modinv(nd, q)
            let old: i64 = sc
            let mut s: i64 = 0
            while s < old {
                v = sums[s] + ee
                if v >= n { v = v - n }
                if v > 1 && v < min_gt { min_gt = v }
                sums[sc] = v
                sc = sc + 1
                s = s + 1
            }
        }
        total = total + n + 1 - min_gt
        free(sums)
        n = n + 1
    }
    printf("%lld\n", total)
    free(spf)
    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 modinv_i64_i64(int64_t a0, int64_t m);
int32_t main(void);



int64_t modinv_i64_i64(int64_t a0, int64_t m) {
    int64_t a = FLOW_CHECKED_MOD((a0), (m));
    int64_t b = m;
    int64_t x0 = 1;
    int64_t x1 = 0;
    while (b != 0) {
        int64_t q = FLOW_CHECKED_DIV((a), (b));
        int64_t t = (a - (q * b));
        a = b;
        b = t;
        int64_t tx = (x0 - (q * x1));
        x0 = x1;
        x1 = tx;
    }
    if (x0 < 0) {
        x0 = (x0 + m);
    }
    return x0;
}

int32_t main(void) {
    int64_t LIMIT = 10000000;
    int32_t* spf = (int32_t*)(calloc((LIMIT + 1), 4));
    if (spf == NULL) {
        return 1;
    }
    int64_t i = 0;
    while (i <= LIMIT) {
        spf[i] = ((int32_t)(i));
        i = (i + 1);
    }
    i = 2;
    while ((i * i) <= LIMIT) {
        if (((int64_t)(spf[i])) == i) {
            int64_t j = (i * i);
            while (j <= LIMIT) {
                if (((int64_t)(spf[j])) == j) {
                    spf[j] = ((int32_t)(i));
                }
                j = (j + i);
            }
        }
        i = (i + 1);
    }
    int64_t total = 0;
    int64_t n = 2;
    while (n <= LIMIT) {
        int64_t t = n;
        int64_t p0 = ((int64_t)(spf[t]));
        int64_t pk = 1;
        while (FLOW_CHECKED_MOD((t), (p0)) == 0) {
            t = FLOW_CHECKED_DIV((t), (p0));
            pk = (pk * p0);
        }
        if (t == 1) {
            total = (total + 1);
            n = (n + 1);
            continue;
        }
        int64_t* sums = (int64_t*)(calloc(512, 8));
        if (sums == NULL) {
            return 1;
        }
        int64_t sc = 1;
        sums[0] = 0;
        int64_t min_gt = n;
        int64_t q = pk;
        int64_t n_div = FLOW_CHECKED_DIV((n), (q));
        int64_t e = (n_div * modinv_i64_i64(n_div, q));
        int64_t v = FLOW_CHECKED_MOD((e), (n));
        if ((v > 1 && v < min_gt)) {
            min_gt = v;
        }
        sums[sc] = v;
        sc = (sc + 1);
        while (t > 1) {
            int64_t p = ((int64_t)(spf[t]));
            pk = 1;
            while (FLOW_CHECKED_MOD((t), (p)) == 0) {
                t = FLOW_CHECKED_DIV((t), (p));
                pk = (pk * p);
            }
            q = pk;
            int64_t nd = FLOW_CHECKED_DIV((n), (q));
            int64_t ee = (nd * modinv_i64_i64(nd, q));
            int64_t old = sc;
            int64_t s = 0;
            while (s < old) {
                v = (sums[s] + ee);
                if (v >= n) {
                    v = (v - n);
                }
                if ((v > 1 && v < min_gt)) {
                    min_gt = v;
                }
                sums[sc] = v;
                sc = (sc + 1);
                s = (s + 1);
            }
        }
        total = (((total + n) + 1) - min_gt);
        free(sums);
        n = (n + 1);
    }
    printf("%lld\n", total);
    free(spf);
    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 @modinv(%arg0: i64, %arg1: i64) -> i64 {
    %0 = arith.remsi %arg0, %arg1 : i64
    %1 = llvm.mlir.constant(1 : i64) : i64
    %2 = llvm.alloca %1 x i64 : (i64) -> !llvm.ptr
    llvm.store %0, %2 : i64, !llvm.ptr
    %3 = llvm.mlir.constant(1 : i64) : i64
    %4 = llvm.alloca %3 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %4 : i64, !llvm.ptr
    %5 = arith.constant 1 : i32
    %6 = arith.extsi %5 : i32 to i64
    %7 = llvm.mlir.constant(1 : i64) : i64
    %8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
    llvm.store %6, %8 : i64, !llvm.ptr
    %9 = arith.constant 0 : i32
    %10 = arith.extsi %9 : i32 to i64
    %11 = llvm.mlir.constant(1 : i64) : i64
    %12 = llvm.alloca %11 x i64 : (i64) -> !llvm.ptr
    llvm.store %10, %12 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %13 = llvm.load %4 : !llvm.ptr -> i64
    %14 = arith.constant 0 : i32
    %16 = arith.extsi %14 : i32 to i64
    %15 = arith.cmpi ne, %13, %16 : i64
    cf.cond_br %15, ^bb1, ^bb2
    ^bb1:
      %17 = llvm.load %2 : !llvm.ptr -> i64
      %18 = llvm.load %4 : !llvm.ptr -> i64
      %19 = arith.divsi %17, %18 : i64
      %20 = llvm.load %2 : !llvm.ptr -> i64
      %21 = llvm.load %4 : !llvm.ptr -> i64
      %22 = arith.muli %19, %21 : i64
      %23 = arith.subi %20, %22 : i64
      %24 = llvm.load %4 : !llvm.ptr -> i64
      llvm.store %24, %2 : i64, !llvm.ptr
      llvm.store %23, %4 : i64, !llvm.ptr
      %25 = llvm.load %8 : !llvm.ptr -> i64
      %26 = llvm.load %12 : !llvm.ptr -> i64
      %27 = arith.muli %19, %26 : i64
      %28 = arith.subi %25, %27 : i64
      %29 = llvm.load %12 : !llvm.ptr -> i64
      llvm.store %29, %8 : i64, !llvm.ptr
      llvm.store %28, %12 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %30 = llvm.load %8 : !llvm.ptr -> i64
    %31 = arith.constant 0 : i32
    %33 = arith.extsi %31 : i32 to i64
    %32 = arith.cmpi slt, %30, %33 : i64
    cf.cond_br %32, ^bb3, ^bb4
    ^bb3:
      %34 = llvm.load %8 : !llvm.ptr -> i64
      %35 = arith.addi %34, %arg1 : i64
      llvm.store %35, %8 : i64, !llvm.ptr
      cf.br ^bb5
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %36 = llvm.load %8 : !llvm.ptr -> i64
    func.return %36 : i64
  }
  func.func @main() -> i32 {
    %37 = arith.constant 10000000 : i32
    %38 = arith.extsi %37 : i32 to i64
    %40 = arith.constant 1 : i32
    %42 = arith.extsi %40 : i32 to i64
    %41 = arith.addi %38, %42 : i64
    %43 = arith.constant 4 : i32
    %44 = arith.extsi %43 : i32 to i64
    %39 = func.call @calloc(%41, %44) : (i64, i64) -> !llvm.ptr
    %45 = llvm.mlir.zero : !llvm.ptr
    %46 = llvm.icmp "eq" %39, %45 : !llvm.ptr
    cf.cond_br %46, ^bb6, ^bb7
    ^bb6:
      %47 = arith.constant 1 : i32
      func.return %47 : i32
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %48 = arith.constant 0 : i32
    %49 = arith.extsi %48 : i32 to i64
    %50 = llvm.mlir.constant(1 : i64) : i64
    %51 = llvm.alloca %50 x i64 : (i64) -> !llvm.ptr
    llvm.store %49, %51 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %52 = llvm.load %51 : !llvm.ptr -> i64
    %53 = arith.cmpi sle, %52, %38 : i64
    cf.cond_br %53, ^bb10, ^bb11
    ^bb10:
      %54 = llvm.load %51 : !llvm.ptr -> i64
      %55 = arith.trunci %54 : i64 to i32
      %56 = llvm.load %51 : !llvm.ptr -> i64
      %57 = llvm.getelementptr %39[%56] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %55, %57 : i32, !llvm.ptr
      %58 = llvm.load %51 : !llvm.ptr -> i64
      %59 = arith.constant 1 : i32
      %61 = arith.extsi %59 : i32 to i64
      %60 = arith.addi %58, %61 : i64
      llvm.store %60, %51 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %62 = arith.constant 2 : i32
    %63 = arith.extsi %62 : i32 to i64
    llvm.store %63, %51 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %64 = llvm.load %51 : !llvm.ptr -> i64
    %65 = llvm.load %51 : !llvm.ptr -> i64
    %66 = arith.muli %64, %65 : i64
    %67 = arith.cmpi sle, %66, %38 : i64
    cf.cond_br %67, ^bb13, ^bb14
    ^bb13:
      %69 = llvm.load %51 : !llvm.ptr -> i64
      %70 = llvm.getelementptr %39[%69] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %68 = llvm.load %70 : !llvm.ptr -> i32
      %71 = arith.extsi %68 : i32 to i64
      %72 = llvm.load %51 : !llvm.ptr -> i64
      %73 = arith.cmpi eq, %71, %72 : i64
      cf.cond_br %73, ^bb15, ^bb16
      ^bb15:
        %74 = llvm.load %51 : !llvm.ptr -> i64
        %75 = llvm.load %51 : !llvm.ptr -> i64
        %76 = arith.muli %74, %75 : i64
        %77 = llvm.mlir.constant(1 : i64) : i64
        %78 = llvm.alloca %77 x i64 : (i64) -> !llvm.ptr
        llvm.store %76, %78 : i64, !llvm.ptr
        cf.br ^bb18
        ^bb18:
        %79 = llvm.load %78 : !llvm.ptr -> i64
        %80 = arith.cmpi sle, %79, %38 : i64
        cf.cond_br %80, ^bb19, ^bb20
        ^bb19:
          %82 = llvm.load %78 : !llvm.ptr -> i64
          %83 = llvm.getelementptr %39[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %81 = llvm.load %83 : !llvm.ptr -> i32
          %84 = arith.extsi %81 : i32 to i64
          %85 = llvm.load %78 : !llvm.ptr -> i64
          %86 = arith.cmpi eq, %84, %85 : i64
          cf.cond_br %86, ^bb21, ^bb22
          ^bb21:
            %87 = llvm.load %51 : !llvm.ptr -> i64
            %88 = arith.trunci %87 : i64 to i32
            %89 = llvm.load %78 : !llvm.ptr -> i64
            %90 = llvm.getelementptr %39[%89] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            llvm.store %88, %90 : i32, !llvm.ptr
            cf.br ^bb23
          ^bb22:
            cf.br ^bb23
          ^bb23:
          %91 = llvm.load %78 : !llvm.ptr -> i64
          %92 = llvm.load %51 : !llvm.ptr -> i64
          %93 = arith.addi %91, %92 : i64
          llvm.store %93, %78 : i64, !llvm.ptr
          cf.br ^bb18
        ^bb20:
        cf.br ^bb17
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %94 = llvm.load %51 : !llvm.ptr -> i64
      %95 = arith.constant 1 : i32
      %97 = arith.extsi %95 : i32 to i64
      %96 = arith.addi %94, %97 : i64
      llvm.store %96, %51 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %98 = arith.constant 0 : i32
    %99 = arith.extsi %98 : i32 to i64
    %100 = llvm.mlir.constant(1 : i64) : i64
    %101 = llvm.alloca %100 x i64 : (i64) -> !llvm.ptr
    llvm.store %99, %101 : i64, !llvm.ptr
    %102 = arith.constant 2 : i32
    %103 = arith.extsi %102 : i32 to i64
    %104 = llvm.mlir.constant(1 : i64) : i64
    %105 = llvm.alloca %104 x i64 : (i64) -> !llvm.ptr
    llvm.store %103, %105 : i64, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %106 = llvm.load %105 : !llvm.ptr -> i64
    %107 = arith.cmpi sle, %106, %38 : i64
    cf.cond_br %107, ^bb25, ^bb26
    ^bb25:
      %108 = llvm.load %105 : !llvm.ptr -> i64
      %109 = llvm.mlir.constant(1 : i64) : i64
      %110 = llvm.alloca %109 x i64 : (i64) -> !llvm.ptr
      llvm.store %108, %110 : i64, !llvm.ptr
      %112 = llvm.load %110 : !llvm.ptr -> i64
      %113 = llvm.getelementptr %39[%112] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %111 = llvm.load %113 : !llvm.ptr -> i32
      %114 = arith.extsi %111 : i32 to i64
      %115 = arith.constant 1 : i32
      %116 = arith.extsi %115 : i32 to i64
      %117 = llvm.mlir.constant(1 : i64) : i64
      %118 = llvm.alloca %117 x i64 : (i64) -> !llvm.ptr
      llvm.store %116, %118 : i64, !llvm.ptr
      cf.br ^bb27
      ^bb27:
      %119 = llvm.load %110 : !llvm.ptr -> i64
      %120 = arith.remsi %119, %114 : i64
      %121 = arith.constant 0 : i32
      %123 = arith.extsi %121 : i32 to i64
      %122 = arith.cmpi eq, %120, %123 : i64
      cf.cond_br %122, ^bb28, ^bb29
      ^bb28:
        %124 = llvm.load %110 : !llvm.ptr -> i64
        %125 = arith.divsi %124, %114 : i64
        llvm.store %125, %110 : i64, !llvm.ptr
        %126 = llvm.load %118 : !llvm.ptr -> i64
        %127 = arith.muli %126, %114 : i64
        llvm.store %127, %118 : i64, !llvm.ptr
        cf.br ^bb27
      ^bb29:
      %128 = llvm.load %110 : !llvm.ptr -> i64
      %129 = arith.constant 1 : i32
      %131 = arith.extsi %129 : i32 to i64
      %130 = arith.cmpi eq, %128, %131 : i64
      cf.cond_br %130, ^bb30, ^bb31
      ^bb30:
        %132 = llvm.load %101 : !llvm.ptr -> i64
        %133 = arith.constant 1 : i32
        %135 = arith.extsi %133 : i32 to i64
        %134 = arith.addi %132, %135 : i64
        llvm.store %134, %101 : i64, !llvm.ptr
        %136 = llvm.load %105 : !llvm.ptr -> i64
        %137 = arith.constant 1 : i32
        %139 = arith.extsi %137 : i32 to i64
        %138 = arith.addi %136, %139 : i64
        llvm.store %138, %105 : i64, !llvm.ptr
        cf.br ^bb24
      ^bb31:
        cf.br ^bb32
      ^bb32:
      %141 = arith.constant 512 : i32
      %142 = arith.constant 8 : i32
      %143 = arith.extsi %141 : i32 to i64
      %144 = arith.extsi %142 : i32 to i64
      %140 = func.call @calloc(%143, %144) : (i64, i64) -> !llvm.ptr
      %145 = llvm.mlir.constant(1 : i64) : i64
      %146 = llvm.alloca %145 x !llvm.ptr : (i64) -> !llvm.ptr
      llvm.store %140, %146 : !llvm.ptr, !llvm.ptr
      %147 = llvm.load %146 : !llvm.ptr -> !llvm.ptr
      %148 = llvm.mlir.zero : !llvm.ptr
      %149 = llvm.icmp "eq" %147, %148 : !llvm.ptr
      cf.cond_br %149, ^bb33, ^bb34
      ^bb33:
        %150 = arith.constant 1 : i32
        func.return %150 : i32
      ^bb34:
        cf.br ^bb35
      ^bb35:
      %151 = arith.constant 1 : i32
      %152 = arith.extsi %151 : i32 to i64
      %153 = llvm.mlir.constant(1 : i64) : i64
      %154 = llvm.alloca %153 x i64 : (i64) -> !llvm.ptr
      llvm.store %152, %154 : i64, !llvm.ptr
      %155 = arith.constant 0 : i32
      %156 = llvm.load %146 : !llvm.ptr -> !llvm.ptr
      %157 = arith.constant 0 : i32
      %158 = arith.extsi %155 : i32 to i64
      %159 = arith.extsi %157 : i32 to i64
      %160 = llvm.getelementptr %156[%159] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %158, %160 : i64, !llvm.ptr
      %161 = llvm.load %105 : !llvm.ptr -> i64
      %162 = llvm.mlir.constant(1 : i64) : i64
      %163 = llvm.alloca %162 x i64 : (i64) -> !llvm.ptr
      llvm.store %161, %163 : i64, !llvm.ptr
      %164 = llvm.load %118 : !llvm.ptr -> i64
      %165 = llvm.mlir.constant(1 : i64) : i64
      %166 = llvm.alloca %165 x i64 : (i64) -> !llvm.ptr
      llvm.store %164, %166 : i64, !llvm.ptr
      %167 = llvm.load %105 : !llvm.ptr -> i64
      %168 = llvm.load %166 : !llvm.ptr -> i64
      %169 = arith.divsi %167, %168 : i64
      %171 = llvm.load %166 : !llvm.ptr -> i64
      %170 = func.call @modinv(%169, %171) : (i64, i64) -> i64
      %172 = arith.muli %169, %170 : i64
      %173 = llvm.load %105 : !llvm.ptr -> i64
      %174 = arith.remsi %172, %173 : i64
      %175 = llvm.mlir.constant(1 : i64) : i64
      %176 = llvm.alloca %175 x i64 : (i64) -> !llvm.ptr
      llvm.store %174, %176 : i64, !llvm.ptr
      %177 = llvm.load %176 : !llvm.ptr -> i64
      %178 = arith.constant 1 : i32
      %180 = arith.extsi %178 : i32 to i64
      %179 = arith.cmpi sgt, %177, %180 : i64
      %181 = scf.if %179 -> (i1) {
        %182 = llvm.load %176 : !llvm.ptr -> i64
        %183 = llvm.load %163 : !llvm.ptr -> i64
        %184 = arith.cmpi slt, %182, %183 : i64
        scf.yield %184 : i1
      } else {
        %185 = arith.constant false
        scf.yield %185 : i1
      }
      cf.cond_br %181, ^bb36, ^bb37
      ^bb36:
        %186 = llvm.load %176 : !llvm.ptr -> i64
        llvm.store %186, %163 : i64, !llvm.ptr
        cf.br ^bb38
      ^bb37:
        cf.br ^bb38
      ^bb38:
      %187 = llvm.load %176 : !llvm.ptr -> i64
      %188 = llvm.load %146 : !llvm.ptr -> !llvm.ptr
      %189 = llvm.load %154 : !llvm.ptr -> i64
      %190 = llvm.getelementptr %188[%189] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %187, %190 : i64, !llvm.ptr
      %191 = llvm.load %154 : !llvm.ptr -> i64
      %192 = arith.constant 1 : i32
      %194 = arith.extsi %192 : i32 to i64
      %193 = arith.addi %191, %194 : i64
      llvm.store %193, %154 : i64, !llvm.ptr
      cf.br ^bb39
      ^bb39:
      %195 = llvm.load %110 : !llvm.ptr -> i64
      %196 = arith.constant 1 : i32
      %198 = arith.extsi %196 : i32 to i64
      %197 = arith.cmpi sgt, %195, %198 : i64
      cf.cond_br %197, ^bb40, ^bb41
      ^bb40:
        %200 = llvm.load %110 : !llvm.ptr -> i64
        %201 = llvm.getelementptr %39[%200] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %199 = llvm.load %201 : !llvm.ptr -> i32
        %202 = arith.extsi %199 : i32 to i64
        %203 = arith.constant 1 : i32
        %204 = arith.extsi %203 : i32 to i64
        llvm.store %204, %118 : i64, !llvm.ptr
        cf.br ^bb42
        ^bb42:
        %205 = llvm.load %110 : !llvm.ptr -> i64
        %206 = arith.remsi %205, %202 : i64
        %207 = arith.constant 0 : i32
        %209 = arith.extsi %207 : i32 to i64
        %208 = arith.cmpi eq, %206, %209 : i64
        cf.cond_br %208, ^bb43, ^bb44
        ^bb43:
          %210 = llvm.load %110 : !llvm.ptr -> i64
          %211 = arith.divsi %210, %202 : i64
          llvm.store %211, %110 : i64, !llvm.ptr
          %212 = llvm.load %118 : !llvm.ptr -> i64
          %213 = arith.muli %212, %202 : i64
          llvm.store %213, %118 : i64, !llvm.ptr
          cf.br ^bb42
        ^bb44:
        %214 = llvm.load %118 : !llvm.ptr -> i64
        llvm.store %214, %166 : i64, !llvm.ptr
        %215 = llvm.load %105 : !llvm.ptr -> i64
        %216 = llvm.load %166 : !llvm.ptr -> i64
        %217 = arith.divsi %215, %216 : i64
        %219 = llvm.load %166 : !llvm.ptr -> i64
        %218 = func.call @modinv(%217, %219) : (i64, i64) -> i64
        %220 = arith.muli %217, %218 : i64
        %221 = llvm.load %154 : !llvm.ptr -> i64
        %222 = arith.constant 0 : i32
        %223 = arith.extsi %222 : i32 to i64
        %224 = llvm.mlir.constant(1 : i64) : i64
        %225 = llvm.alloca %224 x i64 : (i64) -> !llvm.ptr
        llvm.store %223, %225 : i64, !llvm.ptr
        cf.br ^bb45
        ^bb45:
        %226 = llvm.load %225 : !llvm.ptr -> i64
        %227 = arith.cmpi slt, %226, %221 : i64
        cf.cond_br %227, ^bb46, ^bb47
        ^bb46:
          %229 = llvm.load %146 : !llvm.ptr -> !llvm.ptr
          %230 = llvm.load %225 : !llvm.ptr -> i64
          %231 = llvm.getelementptr %229[%230] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %228 = llvm.load %231 : !llvm.ptr -> i64
          %232 = arith.addi %228, %220 : i64
          llvm.store %232, %176 : i64, !llvm.ptr
          %233 = llvm.load %176 : !llvm.ptr -> i64
          %234 = llvm.load %105 : !llvm.ptr -> i64
          %235 = arith.cmpi sge, %233, %234 : i64
          cf.cond_br %235, ^bb48, ^bb49
          ^bb48:
            %236 = llvm.load %176 : !llvm.ptr -> i64
            %237 = llvm.load %105 : !llvm.ptr -> i64
            %238 = arith.subi %236, %237 : i64
            llvm.store %238, %176 : i64, !llvm.ptr
            cf.br ^bb50
          ^bb49:
            cf.br ^bb50
          ^bb50:
          %239 = llvm.load %176 : !llvm.ptr -> i64
          %240 = arith.constant 1 : i32
          %242 = arith.extsi %240 : i32 to i64
          %241 = arith.cmpi sgt, %239, %242 : i64
          %243 = scf.if %241 -> (i1) {
            %244 = llvm.load %176 : !llvm.ptr -> i64
            %245 = llvm.load %163 : !llvm.ptr -> i64
            %246 = arith.cmpi slt, %244, %245 : i64
            scf.yield %246 : i1
          } else {
            %247 = arith.constant false
            scf.yield %247 : i1
          }
          cf.cond_br %243, ^bb51, ^bb52
          ^bb51:
            %248 = llvm.load %176 : !llvm.ptr -> i64
            llvm.store %248, %163 : i64, !llvm.ptr
            cf.br ^bb53
          ^bb52:
            cf.br ^bb53
          ^bb53:
          %249 = llvm.load %176 : !llvm.ptr -> i64
          %250 = llvm.load %146 : !llvm.ptr -> !llvm.ptr
          %251 = llvm.load %154 : !llvm.ptr -> i64
          %252 = llvm.getelementptr %250[%251] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %249, %252 : i64, !llvm.ptr
          %253 = llvm.load %154 : !llvm.ptr -> i64
          %254 = arith.constant 1 : i32
          %256 = arith.extsi %254 : i32 to i64
          %255 = arith.addi %253, %256 : i64
          llvm.store %255, %154 : i64, !llvm.ptr
          %257 = llvm.load %225 : !llvm.ptr -> i64
          %258 = arith.constant 1 : i32
          %260 = arith.extsi %258 : i32 to i64
          %259 = arith.addi %257, %260 : i64
          llvm.store %259, %225 : i64, !llvm.ptr
          cf.br ^bb45
        ^bb47:
        cf.br ^bb39
      ^bb41:
      %261 = llvm.load %101 : !llvm.ptr -> i64
      %262 = llvm.load %105 : !llvm.ptr -> i64
      %263 = arith.addi %261, %262 : i64
      %264 = arith.constant 1 : i32
      %266 = arith.extsi %264 : i32 to i64
      %265 = arith.addi %263, %266 : i64
      %267 = llvm.load %163 : !llvm.ptr -> i64
      %268 = arith.subi %265, %267 : i64
      llvm.store %268, %101 : i64, !llvm.ptr
      %270 = llvm.load %146 : !llvm.ptr -> !llvm.ptr
      func.call @free(%270) : (!llvm.ptr) -> ()
      %271 = llvm.load %105 : !llvm.ptr -> i64
      %272 = arith.constant 1 : i32
      %274 = arith.extsi %272 : i32 to i64
      %273 = arith.addi %271, %274 : i64
      llvm.store %273, %105 : i64, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %275 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %276 = llvm.load %101 : !llvm.ptr -> i64
    %277 = llvm.call @printf(%275, %276) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%39) : (!llvm.ptr) -> ()
    %279 = arith.constant 0 : i32
    func.return %279 : i32
  }
}