Problem 822

Square the Smallest — S(10^4, 10^16) mod 1234567891.

Answer950591530
Output950591530
StatusPASS
Native helperno
Runtime30 ms
Peak memory1216 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

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

Flow source

# Project Euler 822
# Square the Smallest — S(10^4, 10^16) mod 1234567891.

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

const MOD: i64 = 1234567891

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 S(n: i64, m: i64) -> i64 {
    let A: ptr<i64> = calloc(n - 1, 8)
    let mut i: i64 = 0
    while i < n - 1 {
        A[i] = i + 2
        i = i + 1
    }
    let mut k: i64 = 0
    while 1 == 1 {
        let mut mn: i64 = A[0]
        let mut mx: i64 = A[0]
        let mut mi: i64 = 0
        i = 1
        while i < n - 1 {
            if A[i] < mn {
                mn = A[i]
                mi = i
            }
            if A[i] > mx { mx = A[i] }
            i = i + 1
        }
        if mn * mn >= mx { break }
        A[mi] = mn * mn
        k = k + 1
    }
    # sort A
    i = 0
    while i < n - 1 {
        let mut j: i64 = i + 1
        while j < n - 1 {
            if A[j] < A[i] {
                let t: i64 = A[i]
                A[i] = A[j]
                A[j] = t
            }
            j = j + 1
        }
        i = i + 1
    }
    let q: i64 = (m - k) / (n - 1)
    let r: i64 = (m - k) % (n - 1)
    let mut total: i64 = 0
    i = 0
    while i < n - 1 {
        let mut exp2: i64 = q
        if i < r { exp2 = q + 1 }
        # A[i]^(2^exp2) mod MOD using phi(MOD)=MOD-1
        let e: i64 = modpow(2, exp2, MOD - 1)
        total = (total + modpow(A[i], e, MOD)) % MOD
        i = i + 1
    }
    free(A)
    return total
}

function main() -> i32 {
    printf("%lld\n", S(10000, 10000000000000000))
    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 modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int64_t S_i64_i64(int64_t n, int64_t m);
int32_t main(void);

static const int64_t MOD = 1234567891;



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 S_i64_i64(int64_t n, int64_t m) {
    int64_t* A = (int64_t*)(calloc((n - 1), 8));
    int64_t i = 0;
    while (i < (n - 1)) {
        A[i] = (i + 2);
        i = (i + 1);
    }
    int64_t k = 0;
    while (1 == 1) {
        int64_t mn = A[0];
        int64_t mx = A[0];
        int64_t mi = 0;
        i = 1;
        while (i < (n - 1)) {
            if (A[i] < mn) {
                mn = A[i];
                mi = i;
            }
            if (A[i] > mx) {
                mx = A[i];
            }
            i = (i + 1);
        }
        if ((mn * mn) >= mx) {
            break;
        }
        A[mi] = (mn * mn);
        k = (k + 1);
    }
    i = 0;
    while (i < (n - 1)) {
        int64_t j = (i + 1);
        while (j < (n - 1)) {
            if (A[j] < A[i]) {
                int64_t t = A[i];
                A[i] = A[j];
                A[j] = t;
            }
            j = (j + 1);
        }
        i = (i + 1);
    }
    int64_t q = FLOW_CHECKED_DIV(((m - k)), ((n - 1)));
    int64_t r = FLOW_CHECKED_MOD(((m - k)), ((n - 1)));
    int64_t total = 0;
    i = 0;
    while (i < (n - 1)) {
        int64_t exp2 = q;
        if (i < r) {
            exp2 = (q + 1);
        }
        int64_t e = modpow_i64_i64_i64(2, exp2, (MOD - 1));
        total = FLOW_CHECKED_MOD(((total + modpow_i64_i64_i64(A[i], e, MOD))), (MOD));
        i = (i + 1);
    }
    free(A);
    return total;
}

int32_t main(void) {
    printf("%lld\n", S_i64_i64(10000, 10000000000000000));
    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(1234567891 : i64) : i64
  func.func @modpow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %0 = arith.constant 1 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
    llvm.store %1, %3 : i64, !llvm.ptr
    %4 = arith.remsi %arg0, %arg2 : i64
    %5 = llvm.mlir.constant(1 : i64) : i64
    %6 = llvm.alloca %5 x i64 : (i64) -> !llvm.ptr
    llvm.store %4, %6 : i64, !llvm.ptr
    %7 = llvm.mlir.constant(1 : i64) : i64
    %8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %8 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %9 = llvm.load %8 : !llvm.ptr -> i64
    %10 = arith.constant 0 : i32
    %12 = arith.extsi %10 : i32 to i64
    %11 = arith.cmpi sgt, %9, %12 : i64
    cf.cond_br %11, ^bb1, ^bb2
    ^bb1:
      %13 = llvm.load %8 : !llvm.ptr -> i64
      %14 = arith.constant 1 : i32
      %16 = arith.extsi %14 : i32 to i64
      %15 = arith.andi %13, %16 : i64
      %17 = arith.constant 0 : i32
      %19 = arith.extsi %17 : i32 to i64
      %18 = arith.cmpi ne, %15, %19 : i64
      cf.cond_br %18, ^bb3, ^bb4
      ^bb3:
        %20 = llvm.load %3 : !llvm.ptr -> i64
        %21 = arith.extsi %20 : i64 to i128
        %22 = llvm.load %6 : !llvm.ptr -> i64
        %23 = arith.extsi %22 : i64 to i128
        %25 = arith.trunci %21 : i128 to i64
        %26 = arith.trunci %23 : i128 to i64
        %24 = arith.muli %25, %26 : i64
        %27 = arith.extsi %arg2 : i64 to i128
        %29 = arith.trunci %27 : i128 to i64
        %28 = arith.remsi %24, %29 : i64
        llvm.store %28, %3 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %30 = llvm.load %6 : !llvm.ptr -> i64
      %31 = arith.extsi %30 : i64 to i128
      %32 = llvm.load %6 : !llvm.ptr -> i64
      %33 = arith.extsi %32 : i64 to i128
      %35 = arith.trunci %31 : i128 to i64
      %36 = arith.trunci %33 : i128 to i64
      %34 = arith.muli %35, %36 : i64
      %37 = arith.extsi %arg2 : i64 to i128
      %39 = arith.trunci %37 : i128 to i64
      %38 = arith.remsi %34, %39 : i64
      llvm.store %38, %6 : i64, !llvm.ptr
      %40 = llvm.load %8 : !llvm.ptr -> i64
      %41 = arith.constant 1 : i32
      %43 = arith.extsi %41 : i32 to i64
      %42 = arith.shrsi %40, %43 : i64
      llvm.store %42, %8 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %44 = llvm.load %3 : !llvm.ptr -> i64
    func.return %44 : i64
  }
  func.func @S(%arg0: i64, %arg1: i64) -> i64 {
    %46 = arith.constant 1 : i32
    %48 = arith.extsi %46 : i32 to i64
    %47 = arith.subi %arg0, %48 : i64
    %49 = arith.constant 8 : i32
    %50 = arith.extsi %49 : i32 to i64
    %45 = func.call @calloc(%47, %50) : (i64, i64) -> !llvm.ptr
    %51 = arith.constant 0 : i32
    %52 = arith.extsi %51 : i32 to i64
    %53 = llvm.mlir.constant(1 : i64) : i64
    %54 = llvm.alloca %53 x i64 : (i64) -> !llvm.ptr
    llvm.store %52, %54 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %55 = llvm.load %54 : !llvm.ptr -> i64
    %56 = arith.constant 1 : i32
    %58 = arith.extsi %56 : i32 to i64
    %57 = arith.subi %arg0, %58 : i64
    %59 = arith.cmpi slt, %55, %57 : i64
    cf.cond_br %59, ^bb7, ^bb8
    ^bb7:
      %60 = llvm.load %54 : !llvm.ptr -> i64
      %61 = arith.constant 2 : i32
      %63 = arith.extsi %61 : i32 to i64
      %62 = arith.addi %60, %63 : i64
      %64 = llvm.load %54 : !llvm.ptr -> i64
      %65 = llvm.getelementptr %45[%64] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %62, %65 : i64, !llvm.ptr
      %66 = llvm.load %54 : !llvm.ptr -> i64
      %67 = arith.constant 1 : i32
      %69 = arith.extsi %67 : i32 to i64
      %68 = arith.addi %66, %69 : i64
      llvm.store %68, %54 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %70 = arith.constant 0 : i32
    %71 = arith.extsi %70 : i32 to i64
    %72 = llvm.mlir.constant(1 : i64) : i64
    %73 = llvm.alloca %72 x i64 : (i64) -> !llvm.ptr
    llvm.store %71, %73 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %74 = arith.constant 1 : i32
    %75 = arith.constant 1 : i32
    %76 = arith.cmpi eq, %74, %75 : i32
    cf.cond_br %76, ^bb10, ^bb11
    ^bb10:
      %78 = arith.constant 0 : i32
      %79 = arith.extsi %78 : i32 to i64
      %80 = llvm.getelementptr %45[%79] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %77 = llvm.load %80 : !llvm.ptr -> i64
      %81 = llvm.mlir.constant(1 : i64) : i64
      %82 = llvm.alloca %81 x i64 : (i64) -> !llvm.ptr
      llvm.store %77, %82 : i64, !llvm.ptr
      %84 = arith.constant 0 : i32
      %85 = arith.extsi %84 : i32 to i64
      %86 = llvm.getelementptr %45[%85] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %83 = llvm.load %86 : !llvm.ptr -> i64
      %87 = llvm.mlir.constant(1 : i64) : i64
      %88 = llvm.alloca %87 x i64 : (i64) -> !llvm.ptr
      llvm.store %83, %88 : i64, !llvm.ptr
      %89 = arith.constant 0 : i32
      %90 = arith.extsi %89 : i32 to i64
      %91 = llvm.mlir.constant(1 : i64) : i64
      %92 = llvm.alloca %91 x i64 : (i64) -> !llvm.ptr
      llvm.store %90, %92 : i64, !llvm.ptr
      %93 = arith.constant 1 : i32
      %94 = arith.extsi %93 : i32 to i64
      llvm.store %94, %54 : i64, !llvm.ptr
      cf.br ^bb12
      ^bb12:
      %95 = llvm.load %54 : !llvm.ptr -> i64
      %96 = arith.constant 1 : i32
      %98 = arith.extsi %96 : i32 to i64
      %97 = arith.subi %arg0, %98 : i64
      %99 = arith.cmpi slt, %95, %97 : i64
      cf.cond_br %99, ^bb13, ^bb14
      ^bb13:
        %101 = llvm.load %54 : !llvm.ptr -> i64
        %102 = llvm.getelementptr %45[%101] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %100 = llvm.load %102 : !llvm.ptr -> i64
        %103 = llvm.load %82 : !llvm.ptr -> i64
        %104 = arith.cmpi slt, %100, %103 : i64
        cf.cond_br %104, ^bb15, ^bb16
        ^bb15:
          %106 = llvm.load %54 : !llvm.ptr -> i64
          %107 = llvm.getelementptr %45[%106] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %105 = llvm.load %107 : !llvm.ptr -> i64
          llvm.store %105, %82 : i64, !llvm.ptr
          %108 = llvm.load %54 : !llvm.ptr -> i64
          llvm.store %108, %92 : i64, !llvm.ptr
          cf.br ^bb17
        ^bb16:
          cf.br ^bb17
        ^bb17:
        %110 = llvm.load %54 : !llvm.ptr -> i64
        %111 = llvm.getelementptr %45[%110] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %109 = llvm.load %111 : !llvm.ptr -> i64
        %112 = llvm.load %88 : !llvm.ptr -> i64
        %113 = arith.cmpi sgt, %109, %112 : i64
        cf.cond_br %113, ^bb18, ^bb19
        ^bb18:
          %115 = llvm.load %54 : !llvm.ptr -> i64
          %116 = llvm.getelementptr %45[%115] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %114 = llvm.load %116 : !llvm.ptr -> i64
          llvm.store %114, %88 : i64, !llvm.ptr
          cf.br ^bb20
        ^bb19:
          cf.br ^bb20
        ^bb20:
        %117 = llvm.load %54 : !llvm.ptr -> i64
        %118 = arith.constant 1 : i32
        %120 = arith.extsi %118 : i32 to i64
        %119 = arith.addi %117, %120 : i64
        llvm.store %119, %54 : i64, !llvm.ptr
        cf.br ^bb12
      ^bb14:
      %121 = llvm.load %82 : !llvm.ptr -> i64
      %122 = llvm.load %82 : !llvm.ptr -> i64
      %123 = arith.muli %121, %122 : i64
      %124 = llvm.load %88 : !llvm.ptr -> i64
      %125 = arith.cmpi sge, %123, %124 : i64
      cf.cond_br %125, ^bb21, ^bb22
      ^bb21:
        cf.br ^bb11
      ^bb22:
        cf.br ^bb23
      ^bb23:
      %126 = llvm.load %82 : !llvm.ptr -> i64
      %127 = llvm.load %82 : !llvm.ptr -> i64
      %128 = arith.muli %126, %127 : i64
      %129 = llvm.load %92 : !llvm.ptr -> i64
      %130 = llvm.getelementptr %45[%129] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %128, %130 : i64, !llvm.ptr
      %131 = llvm.load %73 : !llvm.ptr -> i64
      %132 = arith.constant 1 : i32
      %134 = arith.extsi %132 : i32 to i64
      %133 = arith.addi %131, %134 : i64
      llvm.store %133, %73 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %135 = arith.constant 0 : i32
    %136 = arith.extsi %135 : i32 to i64
    llvm.store %136, %54 : i64, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %137 = llvm.load %54 : !llvm.ptr -> i64
    %138 = arith.constant 1 : i32
    %140 = arith.extsi %138 : i32 to i64
    %139 = arith.subi %arg0, %140 : i64
    %141 = arith.cmpi slt, %137, %139 : i64
    cf.cond_br %141, ^bb25, ^bb26
    ^bb25:
      %142 = llvm.load %54 : !llvm.ptr -> i64
      %143 = arith.constant 1 : i32
      %145 = arith.extsi %143 : i32 to i64
      %144 = arith.addi %142, %145 : i64
      %146 = llvm.mlir.constant(1 : i64) : i64
      %147 = llvm.alloca %146 x i64 : (i64) -> !llvm.ptr
      llvm.store %144, %147 : i64, !llvm.ptr
      cf.br ^bb27
      ^bb27:
      %148 = llvm.load %147 : !llvm.ptr -> i64
      %149 = arith.constant 1 : i32
      %151 = arith.extsi %149 : i32 to i64
      %150 = arith.subi %arg0, %151 : i64
      %152 = arith.cmpi slt, %148, %150 : i64
      cf.cond_br %152, ^bb28, ^bb29
      ^bb28:
        %154 = llvm.load %147 : !llvm.ptr -> i64
        %155 = llvm.getelementptr %45[%154] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %153 = llvm.load %155 : !llvm.ptr -> i64
        %157 = llvm.load %54 : !llvm.ptr -> i64
        %158 = llvm.getelementptr %45[%157] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %156 = llvm.load %158 : !llvm.ptr -> i64
        %159 = arith.cmpi slt, %153, %156 : i64
        cf.cond_br %159, ^bb30, ^bb31
        ^bb30:
          %161 = llvm.load %54 : !llvm.ptr -> i64
          %162 = llvm.getelementptr %45[%161] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %160 = llvm.load %162 : !llvm.ptr -> i64
          %164 = llvm.load %147 : !llvm.ptr -> i64
          %165 = llvm.getelementptr %45[%164] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %163 = llvm.load %165 : !llvm.ptr -> i64
          %166 = llvm.load %54 : !llvm.ptr -> i64
          %167 = llvm.getelementptr %45[%166] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %163, %167 : i64, !llvm.ptr
          %168 = llvm.load %147 : !llvm.ptr -> i64
          %169 = llvm.getelementptr %45[%168] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %160, %169 : i64, !llvm.ptr
          cf.br ^bb32
        ^bb31:
          cf.br ^bb32
        ^bb32:
        %170 = llvm.load %147 : !llvm.ptr -> i64
        %171 = arith.constant 1 : i32
        %173 = arith.extsi %171 : i32 to i64
        %172 = arith.addi %170, %173 : i64
        llvm.store %172, %147 : i64, !llvm.ptr
        cf.br ^bb27
      ^bb29:
      %174 = llvm.load %54 : !llvm.ptr -> i64
      %175 = arith.constant 1 : i32
      %177 = arith.extsi %175 : i32 to i64
      %176 = arith.addi %174, %177 : i64
      llvm.store %176, %54 : i64, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %178 = llvm.load %73 : !llvm.ptr -> i64
    %179 = arith.subi %arg1, %178 : i64
    %180 = arith.constant 1 : i32
    %182 = arith.extsi %180 : i32 to i64
    %181 = arith.subi %arg0, %182 : i64
    %183 = arith.divsi %179, %181 : i64
    %184 = llvm.load %73 : !llvm.ptr -> i64
    %185 = arith.subi %arg1, %184 : i64
    %186 = arith.constant 1 : i32
    %188 = arith.extsi %186 : i32 to i64
    %187 = arith.subi %arg0, %188 : i64
    %189 = arith.remsi %185, %187 : i64
    %190 = arith.constant 0 : i32
    %191 = arith.extsi %190 : i32 to i64
    %192 = llvm.mlir.constant(1 : i64) : i64
    %193 = llvm.alloca %192 x i64 : (i64) -> !llvm.ptr
    llvm.store %191, %193 : i64, !llvm.ptr
    %194 = arith.constant 0 : i32
    %195 = arith.extsi %194 : i32 to i64
    llvm.store %195, %54 : i64, !llvm.ptr
    cf.br ^bb33
    ^bb33:
    %196 = llvm.load %54 : !llvm.ptr -> i64
    %197 = arith.constant 1 : i32
    %199 = arith.extsi %197 : i32 to i64
    %198 = arith.subi %arg0, %199 : i64
    %200 = arith.cmpi slt, %196, %198 : i64
    cf.cond_br %200, ^bb34, ^bb35
    ^bb34:
      %201 = llvm.mlir.constant(1 : i64) : i64
      %202 = llvm.alloca %201 x i64 : (i64) -> !llvm.ptr
      llvm.store %183, %202 : i64, !llvm.ptr
      %203 = llvm.load %54 : !llvm.ptr -> i64
      %204 = arith.cmpi slt, %203, %189 : i64
      cf.cond_br %204, ^bb36, ^bb37
      ^bb36:
        %205 = arith.constant 1 : i32
        %207 = arith.extsi %205 : i32 to i64
        %206 = arith.addi %183, %207 : i64
        llvm.store %206, %202 : i64, !llvm.ptr
        cf.br ^bb38
      ^bb37:
        cf.br ^bb38
      ^bb38:
      %209 = arith.constant 2 : i32
      %210 = llvm.load %202 : !llvm.ptr -> i64
      %211 = llvm.mlir.addressof @MOD : !llvm.ptr
      %212 = llvm.load %211 : !llvm.ptr -> i64
      %213 = arith.constant 1 : i32
      %215 = arith.extsi %213 : i32 to i64
      %214 = arith.subi %212, %215 : i64
      %216 = arith.extsi %209 : i32 to i64
      %208 = func.call @modpow(%216, %210, %214) : (i64, i64, i64) -> i64
      %217 = llvm.load %193 : !llvm.ptr -> i64
      %220 = llvm.load %54 : !llvm.ptr -> i64
      %221 = llvm.getelementptr %45[%220] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %219 = llvm.load %221 : !llvm.ptr -> i64
      %222 = llvm.mlir.addressof @MOD : !llvm.ptr
      %223 = llvm.load %222 : !llvm.ptr -> i64
      %218 = func.call @modpow(%219, %208, %223) : (i64, i64, i64) -> i64
      %224 = arith.addi %217, %218 : i64
      %225 = llvm.mlir.addressof @MOD : !llvm.ptr
      %226 = llvm.load %225 : !llvm.ptr -> i64
      %227 = arith.remsi %224, %226 : i64
      llvm.store %227, %193 : i64, !llvm.ptr
      %228 = llvm.load %54 : !llvm.ptr -> i64
      %229 = arith.constant 1 : i32
      %231 = arith.extsi %229 : i32 to i64
      %230 = arith.addi %228, %231 : i64
      llvm.store %230, %54 : i64, !llvm.ptr
      cf.br ^bb33
    ^bb35:
    func.call @free(%45) : (!llvm.ptr) -> ()
    %233 = llvm.load %193 : !llvm.ptr -> i64
    func.return %233 : i64
  }
  func.func @main() -> i32 {
    %234 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %236 = arith.constant 10000 : i32
    %237 = arith.constant 9999995705032704 : i32
    %238 = arith.extsi %236 : i32 to i64
    %239 = arith.extsi %237 : i32 to i64
    %235 = func.call @S(%238, %239) : (i64, i64) -> i64
    %240 = llvm.call @printf(%234, %235) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %241 = arith.constant 0 : i32
    func.return %241 : i32
  }
}