Problem 350

f(10^6, 10^12, 10^18) mod 101^4.

Answer84664213
Output84664213
StatusPASS
Native helperno
Runtime10 ms
Peak memory18656 KB
Time complexityO(n) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

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

Flow source

# Project Euler 350
# f(10^6, 10^12, 10^18) mod 101^4.

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

function modpow(base: i64, exp: i64, mod: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = base % mod
    let mut e: i64 = exp
    while e > 0 {
        if e % 2 == 1 { r = (r * b) % mod }
        b = (b * b) % mod
        e = e / 2
    }
    return r
}

function main() -> i32 {
    let G: i64 = 1000000
    let L: i64 = 1000000000000
    let N: i64 = 1000000000000000000
    let mod: i64 = 104060401
    let n: i64 = L / G
    let mut max_e: i64 = 0
    let mut t: i64 = n
    while t > 1 {
        t = t / 2
        max_e = max_e + 1
    }
    let pow_cache: ptr<i64> = calloc(max_e + 3, 8)
    let a: ptr<i64> = calloc(max_e + 1, 8)
    for e in 0..max_e + 2 {
        pow_cache[e] = modpow(e, N, mod)
    }
    for e in 1..max_e + 1 {
        a[e] = (pow_cache[e + 1] - 2 * pow_cache[e] + pow_cache[e - 1] + 2 * mod) % mod
    }
    let spf: ptr<i32> = calloc(n + 1, 4)
    let primes: ptr<i32> = calloc(n / 5 + 10, 4)
    let mut pc: i64 = 0
    let mut i: i64 = 2
    while i <= n {
        if spf[i] == 0 {
            spf[i] = i as i32
            primes[pc] = i as i32
            pc = pc + 1
        }
        let mut j: i64 = 0
        while j < pc {
            let p: i64 = primes[j] as i64
            if p > (spf[i] as i64) || i * p > n { break }
            spf[i * p] = p as i32
            j = j + 1
        }
        i = i + 1
    }
    let expv: ptr<i8> = calloc(n + 1, 1)
    let rest: ptr<i32> = calloc(n + 1, 4)
    let c: ptr<i32> = calloc(n + 1, 4)
    let pref: ptr<i32> = calloc(n + 1, 4)
    if expv == null || rest == null || c == null || pref == null { return 1 }
    rest[1] = 1
    c[1] = 1
    for i in 2..n + 1 {
        let p: i64 = spf[i] as i64
        let m: i64 = i / p
        if (spf[m] as i64) == p {
            expv[i] = expv[m] + 1
            rest[i] = rest[m]
        } else {
            expv[i] = 1
            rest[i] = m as i32
        }
        c[i] = ((c[rest[i]] as i64) * a[expv[i]] % mod) as i32
    }
    let mut s: i64 = 0
    for i in 1..n + 1 {
        s = (s + (c[i] as i64)) % mod
        pref[i] = s as i32
    }
    let mut ans: i64 = 0
    let mut d: i64 = G
    while d <= L {
        let q: i64 = L / d
        let r: i64 = L / q
        let cnt: i64 = r - d + 1
        ans = (ans + (cnt % mod) * (pref[q] as i64)) % mod
        d = r + 1
    }
    printf("%lld\n", ans)
    free(pow_cache); free(a); free(spf); free(primes)
    free(expv); free(rest); free(c); 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 modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
int32_t main(void);



int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
    int64_t r = 1;
    int64_t b = FLOW_CHECKED_MOD((base), (mod));
    int64_t e = exp;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            r = FLOW_CHECKED_MOD(((r * b)), (mod));
        }
        b = FLOW_CHECKED_MOD(((b * b)), (mod));
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return r;
}

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