Problem 519
Tricoloured Coin Fountains — T(20000) mod 1e9.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^2) | O(n * m) |
| Space complexity | O(n) | O(n) |
| Approach | Flow solution | Dynamic programming or generating function |
| Verdict | Unknown |
Flow source
# Project Euler 519
# Tricoloured Coin Fountains — T(20000) mod 1e9.
const MOD: i64 = 1000000000
const N: i64 = 20000
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function isqrt(n: i64) -> i64 {
if n < 2 { return n }
let mut x: i64 = n
let mut y: i64 = (x + 1) / 2
while y < x {
x = y
y = (x + n / x) / 2
}
return x
}
function main() -> i32 {
let max_h: i64 = isqrt(2 * N) + 2
let buf: i64 = max_h + 3
let stride: i64 = max_h + 2
let dp: ptr<i64> = calloc(buf * stride, 8)
if dp == null { return 1 }
dp[1 % buf * stride + 1] = 3
let mut s: i64 = 1
while s < N {
let mut maxj: i64 = isqrt(2 * s) + 1
if maxj > max_h { maxj = max_h }
let row: i64 = (s % buf) * stride
let mut running: i64 = 0
let mut h: i64 = maxj
while h >= 1 {
running = (running + dp[row + h]) % MOD
h = h - 1
}
let remaining: i64 = N - s
if remaining > 0 && running != 0 {
dp[((s + 1) % buf) * stride + 1] = (dp[((s + 1) % buf) * stride + 1] + 2 * running) % MOD
if remaining >= 2 {
dp[((s + 2) % buf) * stride + 2] = (dp[((s + 2) % buf) * stride + 2] + running + dp[row + 1]) % MOD
}
let mut maxk: i64 = maxj + 1
if maxk > max_h { maxk = max_h }
if maxk > remaining { maxk = remaining }
let mut k: i64 = 3
while k <= maxk {
let mut suff: i64 = 0
h = maxj
while h >= k - 1 {
suff = (suff + dp[row + h]) % MOD
h = h - 1
}
dp[((s + k) % buf) * stride + k] = (dp[((s + k) % buf) * stride + k] + suff) % MOD
k = k + 1
}
}
h = 1
while h <= maxj {
dp[row + h] = 0
h = h + 1
}
s = s + 1
}
let limit: i64 = isqrt(2 * N)
let mut ans: i64 = 0
let mut hh: i64 = 1
while hh <= limit {
ans = (ans + dp[(N % buf) * stride + hh]) % MOD
hh = hh + 1
}
printf("%lld\n", ans)
free(dp)
return 0
}
Generated C
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Flow runtime helpers */
typedef struct flow_temp_node { struct flow_temp_node* next; } flow_temp_node;
static flow_temp_node* flow_temp_head = NULL;
static int flow_temp_atexit_set = 0;
__attribute__((unused)) static void flow_temp_free_all(void) {
while (flow_temp_head) {
flow_temp_node* n = flow_temp_head;
flow_temp_head = n->next;
free(n);
}
}
__attribute__((unused)) static void* flow_temp_alloc(size_t nbytes) {
flow_temp_node* node = (flow_temp_node*)malloc(sizeof(flow_temp_node) + nbytes);
if (!node) return NULL;
node->next = flow_temp_head;
flow_temp_head = node;
if (!flow_temp_atexit_set) {
flow_temp_atexit_set = 1;
atexit(flow_temp_free_all);
}
return (void*)(node + 1);
}
#ifndef FLOW_DIAG
#define FLOW_DIAG(msg) fprintf(stderr, "%s", (msg))
#endif
#ifndef FLOW_LOG
#define FLOW_LOG(fmt, ...) printf(fmt, __VA_ARGS__)
#endif
#ifndef FLOW_LOG_EMPTY
#define FLOW_LOG_EMPTY(fmt) printf(fmt)
#endif
static char* flow_strcat(const char* a, const char* b) {
size_t la = strlen(a ? a : ""), lb = strlen(b ? b : "");
char* r = (char*)flow_temp_alloc(la + lb + 1);
if (!r) return NULL;
if (la) memcpy(r, a, la);
if (lb) memcpy(r + la, b, lb);
r[la + lb] = '\0';
return r;
}
#define __flow_in_arr(arr, val) __extension__ ({ \
int _found = 0; \
size_t _n = sizeof(arr)/sizeof((arr)[0]); \
for (size_t _i = 0; _i < _n; _i++) { \
if ((arr)[_i] == (val)) { _found = 1; break; } \
} _found; })
/* Unified fault handler (MISRA #279) — override with -DFLOW_FAULT_HANDLER=fn */
#ifndef FLOW_FAULT_HANDLER
__attribute__((unused)) static inline void flow_fault_handler(const char* msg) {
fprintf(stderr, "flow: %s\n", msg ? msg : "fault");
abort();
#if defined(__GNUC__) || defined(__clang__)
__builtin_unreachable();
#endif
}
#else
#define flow_fault_handler FLOW_FAULT_HANDLER
#endif
#define flow_div_by_zero_handler() flow_fault_handler("division by zero")
#define flow_shift_ub_handler() flow_fault_handler("invalid shift (amount out of range or left-shift of negative)")
#ifndef FLOW_CHECKED_DIV
#define FLOW_CHECKED_DIV(L, R) (((R) != 0) ? ((L) / (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_MOD
#define FLOW_CHECKED_MOD(L, R) (((R) != 0) ? ((L) % (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHL
#define FLOW_CHECKED_SHL(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull)) && ((L) >= 0)) ? ((L) << (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHR
#define FLOW_CHECKED_SHR(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull))) ? ((L) >> (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#include <math.h>
void* _ui_state = NULL;
static inline float i32_to_f32(int32_t v) { return (float)v; }
/* Host stub for @gpu kernels (device codegen replaces this). */
static inline int32_t gpu_thread_id(void) { return 0; }
int64_t isqrt_i64(int64_t n);
int32_t main(void);
static const int64_t MOD = 1000000000;
static const int64_t N = 20000;
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;
}
int32_t main(void) {
int64_t max_h = (isqrt_i64((2 * N)) + 2);
int64_t buf = (max_h + 3);
int64_t stride = (max_h + 2);
int64_t* dp = (int64_t*)(calloc((buf * stride), 8));
if (dp == NULL) {
return 1;
}
dp[((FLOW_CHECKED_MOD((1), (buf)) * stride) + 1)] = 3;
int64_t s = 1;
while (s < N) {
int64_t maxj = (isqrt_i64((2 * s)) + 1);
if (maxj > max_h) {
maxj = max_h;
}
int64_t row = (FLOW_CHECKED_MOD((s), (buf)) * stride);
int64_t running = 0;
int64_t h = maxj;
while (h >= 1) {
running = FLOW_CHECKED_MOD(((running + dp[(row + h)])), (MOD));
h = (h - 1);
}
int64_t remaining = (N - s);
if ((remaining > 0 && running != 0)) {
dp[((FLOW_CHECKED_MOD(((s + 1)), (buf)) * stride) + 1)] = FLOW_CHECKED_MOD(((dp[((FLOW_CHECKED_MOD(((s + 1)), (buf)) * stride) + 1)] + (2 * running))), (MOD));
if (remaining >= 2) {
dp[((FLOW_CHECKED_MOD(((s + 2)), (buf)) * stride) + 2)] = FLOW_CHECKED_MOD((((dp[((FLOW_CHECKED_MOD(((s + 2)), (buf)) * stride) + 2)] + running) + dp[(row + 1)])), (MOD));
}
int64_t maxk = (maxj + 1);
if (maxk > max_h) {
maxk = max_h;
}
if (maxk > remaining) {
maxk = remaining;
}
int64_t k = 3;
while (k <= maxk) {
int64_t suff = 0;
h = maxj;
while (h >= (k - 1)) {
suff = FLOW_CHECKED_MOD(((suff + dp[(row + h)])), (MOD));
h = (h - 1);
}
dp[((FLOW_CHECKED_MOD(((s + k)), (buf)) * stride) + k)] = FLOW_CHECKED_MOD(((dp[((FLOW_CHECKED_MOD(((s + k)), (buf)) * stride) + k)] + suff)), (MOD));
k = (k + 1);
}
}
h = 1;
while (h <= maxj) {
dp[(row + h)] = 0;
h = (h + 1);
}
s = (s + 1);
}
int64_t limit = isqrt_i64((2 * N));
int64_t ans = 0;
int64_t hh = 1;
while (hh <= limit) {
ans = FLOW_CHECKED_MOD(((ans + dp[((FLOW_CHECKED_MOD((N), (buf)) * stride) + hh)])), (MOD));
hh = (hh + 1);
}
printf("%lld\n", ans);
free(dp);
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>
// Constant: MOD
llvm.mlir.global internal constant @MOD(1000000000 : i64) : i64
// Constant: N
llvm.mlir.global internal constant @N(20000 : i64) : i64
func.func private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
func.func @isqrt(%arg0: i64) -> i64 {
%0 = arith.constant 2 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.cmpi slt, %arg0, %2 : i64
cf.cond_br %1, ^bb0, ^bb1
^bb0:
func.return %arg0 : i64
^bb1:
cf.br ^bb2
^bb2:
%3 = llvm.mlir.constant(1 : i64) : i64
%4 = llvm.alloca %3 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %4 : i64, !llvm.ptr
%5 = llvm.load %4 : !llvm.ptr -> i64
%6 = arith.constant 1 : i32
%8 = arith.extsi %6 : i32 to i64
%7 = arith.addi %5, %8 : i64
%9 = arith.constant 2 : i32
%11 = arith.extsi %9 : i32 to i64
%10 = arith.divsi %7, %11 : i64
%12 = llvm.mlir.constant(1 : i64) : i64
%13 = llvm.alloca %12 x i64 : (i64) -> !llvm.ptr
llvm.store %10, %13 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%14 = llvm.load %13 : !llvm.ptr -> i64
%15 = llvm.load %4 : !llvm.ptr -> i64
%16 = arith.cmpi slt, %14, %15 : i64
cf.cond_br %16, ^bb4, ^bb5
^bb4:
%17 = llvm.load %13 : !llvm.ptr -> i64
llvm.store %17, %4 : i64, !llvm.ptr
%18 = llvm.load %4 : !llvm.ptr -> i64
%19 = llvm.load %4 : !llvm.ptr -> i64
%20 = arith.divsi %arg0, %19 : i64
%21 = arith.addi %18, %20 : i64
%22 = arith.constant 2 : i32
%24 = arith.extsi %22 : i32 to i64
%23 = arith.divsi %21, %24 : i64
llvm.store %23, %13 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%25 = llvm.load %4 : !llvm.ptr -> i64
func.return %25 : i64
}
func.func @main() -> i32 {
%27 = arith.constant 2 : i32
%28 = llvm.mlir.addressof @N : !llvm.ptr
%29 = llvm.load %28 : !llvm.ptr -> i64
%31 = arith.extsi %27 : i32 to i64
%30 = arith.muli %31, %29 : i64
%26 = func.call @isqrt(%30) : (i64) -> i64
%32 = arith.constant 2 : i32
%34 = arith.extsi %32 : i32 to i64
%33 = arith.addi %26, %34 : i64
%35 = arith.constant 3 : i32
%37 = arith.extsi %35 : i32 to i64
%36 = arith.addi %33, %37 : i64
%38 = arith.constant 2 : i32
%40 = arith.extsi %38 : i32 to i64
%39 = arith.addi %33, %40 : i64
%42 = arith.muli %36, %39 : i64
%43 = arith.constant 8 : i32
%44 = arith.extsi %43 : i32 to i64
%41 = func.call @calloc(%42, %44) : (i64, i64) -> !llvm.ptr
%45 = llvm.mlir.zero : !llvm.ptr
%46 = llvm.icmp "eq" %41, %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 3 : i32
%49 = arith.constant 1 : i32
%51 = arith.extsi %49 : i32 to i64
%50 = arith.remsi %51, %36 : i64
%52 = arith.muli %50, %39 : i64
%53 = arith.constant 1 : i32
%55 = arith.extsi %53 : i32 to i64
%54 = arith.addi %52, %55 : i64
%56 = arith.extsi %48 : i32 to i64
%57 = llvm.getelementptr %41[%54] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %56, %57 : i64, !llvm.ptr
%58 = arith.constant 1 : 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 ^bb9
^bb9:
%62 = llvm.load %61 : !llvm.ptr -> i64
%63 = llvm.mlir.addressof @N : !llvm.ptr
%64 = llvm.load %63 : !llvm.ptr -> i64
%65 = arith.cmpi slt, %62, %64 : i64
cf.cond_br %65, ^bb10, ^bb11
^bb10:
%67 = arith.constant 2 : i32
%68 = llvm.load %61 : !llvm.ptr -> i64
%70 = arith.extsi %67 : i32 to i64
%69 = arith.muli %70, %68 : i64
%66 = func.call @isqrt(%69) : (i64) -> i64
%71 = arith.constant 1 : i32
%73 = arith.extsi %71 : i32 to i64
%72 = arith.addi %66, %73 : i64
%74 = llvm.mlir.constant(1 : i64) : i64
%75 = llvm.alloca %74 x i64 : (i64) -> !llvm.ptr
llvm.store %72, %75 : i64, !llvm.ptr
%76 = llvm.load %75 : !llvm.ptr -> i64
%77 = arith.cmpi sgt, %76, %33 : i64
cf.cond_br %77, ^bb12, ^bb13
^bb12:
llvm.store %33, %75 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%78 = llvm.load %61 : !llvm.ptr -> i64
%79 = arith.remsi %78, %36 : i64
%80 = arith.muli %79, %39 : i64
%81 = arith.constant 0 : i32
%82 = arith.extsi %81 : i32 to i64
%83 = llvm.mlir.constant(1 : i64) : i64
%84 = llvm.alloca %83 x i64 : (i64) -> !llvm.ptr
llvm.store %82, %84 : i64, !llvm.ptr
%85 = llvm.load %75 : !llvm.ptr -> i64
%86 = llvm.mlir.constant(1 : i64) : i64
%87 = llvm.alloca %86 x i64 : (i64) -> !llvm.ptr
llvm.store %85, %87 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%88 = llvm.load %87 : !llvm.ptr -> i64
%89 = arith.constant 1 : i32
%91 = arith.extsi %89 : i32 to i64
%90 = arith.cmpi sge, %88, %91 : i64
cf.cond_br %90, ^bb16, ^bb17
^bb16:
%92 = llvm.load %84 : !llvm.ptr -> i64
%94 = llvm.load %87 : !llvm.ptr -> i64
%95 = arith.addi %80, %94 : i64
%96 = llvm.getelementptr %41[%95] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%93 = llvm.load %96 : !llvm.ptr -> i64
%97 = arith.addi %92, %93 : i64
%98 = llvm.mlir.addressof @MOD : !llvm.ptr
%99 = llvm.load %98 : !llvm.ptr -> i64
%100 = arith.remsi %97, %99 : i64
llvm.store %100, %84 : i64, !llvm.ptr
%101 = llvm.load %87 : !llvm.ptr -> i64
%102 = arith.constant 1 : i32
%104 = arith.extsi %102 : i32 to i64
%103 = arith.subi %101, %104 : i64
llvm.store %103, %87 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%105 = llvm.mlir.addressof @N : !llvm.ptr
%106 = llvm.load %105 : !llvm.ptr -> i64
%107 = llvm.load %61 : !llvm.ptr -> i64
%108 = arith.subi %106, %107 : i64
%109 = arith.constant 0 : i32
%111 = arith.extsi %109 : i32 to i64
%110 = arith.cmpi sgt, %108, %111 : i64
%112 = scf.if %110 -> (i1) {
%113 = llvm.load %84 : !llvm.ptr -> i64
%114 = arith.constant 0 : i32
%116 = arith.extsi %114 : i32 to i64
%115 = arith.cmpi ne, %113, %116 : i64
scf.yield %115 : i1
} else {
%117 = arith.constant false
scf.yield %117 : i1
}
cf.cond_br %112, ^bb18, ^bb19
^bb18:
%119 = llvm.load %61 : !llvm.ptr -> i64
%120 = arith.constant 1 : i32
%122 = arith.extsi %120 : i32 to i64
%121 = arith.addi %119, %122 : i64
%123 = arith.remsi %121, %36 : i64
%124 = arith.muli %123, %39 : i64
%125 = arith.constant 1 : i32
%127 = arith.extsi %125 : i32 to i64
%126 = arith.addi %124, %127 : i64
%128 = llvm.getelementptr %41[%126] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%118 = llvm.load %128 : !llvm.ptr -> i64
%129 = arith.constant 2 : i32
%130 = llvm.load %84 : !llvm.ptr -> i64
%132 = arith.extsi %129 : i32 to i64
%131 = arith.muli %132, %130 : i64
%133 = arith.addi %118, %131 : i64
%134 = llvm.mlir.addressof @MOD : !llvm.ptr
%135 = llvm.load %134 : !llvm.ptr -> i64
%136 = arith.remsi %133, %135 : i64
%137 = llvm.load %61 : !llvm.ptr -> i64
%138 = arith.constant 1 : i32
%140 = arith.extsi %138 : i32 to i64
%139 = arith.addi %137, %140 : i64
%141 = arith.remsi %139, %36 : i64
%142 = arith.muli %141, %39 : i64
%143 = arith.constant 1 : i32
%145 = arith.extsi %143 : i32 to i64
%144 = arith.addi %142, %145 : i64
%146 = llvm.getelementptr %41[%144] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %136, %146 : i64, !llvm.ptr
%147 = arith.constant 2 : i32
%149 = arith.extsi %147 : i32 to i64
%148 = arith.cmpi sge, %108, %149 : i64
cf.cond_br %148, ^bb21, ^bb22
^bb21:
%151 = llvm.load %61 : !llvm.ptr -> i64
%152 = arith.constant 2 : i32
%154 = arith.extsi %152 : i32 to i64
%153 = arith.addi %151, %154 : i64
%155 = arith.remsi %153, %36 : i64
%156 = arith.muli %155, %39 : i64
%157 = arith.constant 2 : i32
%159 = arith.extsi %157 : i32 to i64
%158 = arith.addi %156, %159 : i64
%160 = llvm.getelementptr %41[%158] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%150 = llvm.load %160 : !llvm.ptr -> i64
%161 = llvm.load %84 : !llvm.ptr -> i64
%162 = arith.addi %150, %161 : i64
%164 = arith.constant 1 : i32
%166 = arith.extsi %164 : i32 to i64
%165 = arith.addi %80, %166 : i64
%167 = llvm.getelementptr %41[%165] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%163 = llvm.load %167 : !llvm.ptr -> i64
%168 = arith.addi %162, %163 : i64
%169 = llvm.mlir.addressof @MOD : !llvm.ptr
%170 = llvm.load %169 : !llvm.ptr -> i64
%171 = arith.remsi %168, %170 : i64
%172 = llvm.load %61 : !llvm.ptr -> i64
%173 = arith.constant 2 : i32
%175 = arith.extsi %173 : i32 to i64
%174 = arith.addi %172, %175 : i64
%176 = arith.remsi %174, %36 : i64
%177 = arith.muli %176, %39 : i64
%178 = arith.constant 2 : i32
%180 = arith.extsi %178 : i32 to i64
%179 = arith.addi %177, %180 : i64
%181 = llvm.getelementptr %41[%179] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %171, %181 : i64, !llvm.ptr
cf.br ^bb23
^bb22:
cf.br ^bb23
^bb23:
%182 = llvm.load %75 : !llvm.ptr -> i64
%183 = arith.constant 1 : i32
%185 = arith.extsi %183 : i32 to i64
%184 = arith.addi %182, %185 : i64
%186 = llvm.mlir.constant(1 : i64) : i64
%187 = llvm.alloca %186 x i64 : (i64) -> !llvm.ptr
llvm.store %184, %187 : i64, !llvm.ptr
%188 = llvm.load %187 : !llvm.ptr -> i64
%189 = arith.cmpi sgt, %188, %33 : i64
cf.cond_br %189, ^bb24, ^bb25
^bb24:
llvm.store %33, %187 : i64, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%190 = llvm.load %187 : !llvm.ptr -> i64
%191 = arith.cmpi sgt, %190, %108 : i64
cf.cond_br %191, ^bb27, ^bb28
^bb27:
llvm.store %108, %187 : i64, !llvm.ptr
cf.br ^bb29
^bb28:
cf.br ^bb29
^bb29:
%192 = arith.constant 3 : i32
%193 = arith.extsi %192 : i32 to i64
%194 = llvm.mlir.constant(1 : i64) : i64
%195 = llvm.alloca %194 x i64 : (i64) -> !llvm.ptr
llvm.store %193, %195 : i64, !llvm.ptr
cf.br ^bb30
^bb30:
%196 = llvm.load %195 : !llvm.ptr -> i64
%197 = llvm.load %187 : !llvm.ptr -> i64
%198 = arith.cmpi sle, %196, %197 : i64
cf.cond_br %198, ^bb31, ^bb32
^bb31:
%199 = arith.constant 0 : i32
%200 = arith.extsi %199 : i32 to i64
%201 = llvm.mlir.constant(1 : i64) : i64
%202 = llvm.alloca %201 x i64 : (i64) -> !llvm.ptr
llvm.store %200, %202 : i64, !llvm.ptr
%203 = llvm.load %75 : !llvm.ptr -> i64
llvm.store %203, %87 : i64, !llvm.ptr
cf.br ^bb33
^bb33:
%204 = llvm.load %87 : !llvm.ptr -> i64
%205 = llvm.load %195 : !llvm.ptr -> i64
%206 = arith.constant 1 : i32
%208 = arith.extsi %206 : i32 to i64
%207 = arith.subi %205, %208 : i64
%209 = arith.cmpi sge, %204, %207 : i64
cf.cond_br %209, ^bb34, ^bb35
^bb34:
%210 = llvm.load %202 : !llvm.ptr -> i64
%212 = llvm.load %87 : !llvm.ptr -> i64
%213 = arith.addi %80, %212 : i64
%214 = llvm.getelementptr %41[%213] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%211 = llvm.load %214 : !llvm.ptr -> i64
%215 = arith.addi %210, %211 : i64
%216 = llvm.mlir.addressof @MOD : !llvm.ptr
%217 = llvm.load %216 : !llvm.ptr -> i64
%218 = arith.remsi %215, %217 : i64
llvm.store %218, %202 : i64, !llvm.ptr
%219 = llvm.load %87 : !llvm.ptr -> i64
%220 = arith.constant 1 : i32
%222 = arith.extsi %220 : i32 to i64
%221 = arith.subi %219, %222 : i64
llvm.store %221, %87 : i64, !llvm.ptr
cf.br ^bb33
^bb35:
%224 = llvm.load %61 : !llvm.ptr -> i64
%225 = llvm.load %195 : !llvm.ptr -> i64
%226 = arith.addi %224, %225 : i64
%227 = arith.remsi %226, %36 : i64
%228 = arith.muli %227, %39 : i64
%229 = llvm.load %195 : !llvm.ptr -> i64
%230 = arith.addi %228, %229 : i64
%231 = llvm.getelementptr %41[%230] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%223 = llvm.load %231 : !llvm.ptr -> i64
%232 = llvm.load %202 : !llvm.ptr -> i64
%233 = arith.addi %223, %232 : i64
%234 = llvm.mlir.addressof @MOD : !llvm.ptr
%235 = llvm.load %234 : !llvm.ptr -> i64
%236 = arith.remsi %233, %235 : i64
%237 = llvm.load %61 : !llvm.ptr -> i64
%238 = llvm.load %195 : !llvm.ptr -> i64
%239 = arith.addi %237, %238 : i64
%240 = arith.remsi %239, %36 : i64
%241 = arith.muli %240, %39 : i64
%242 = llvm.load %195 : !llvm.ptr -> i64
%243 = arith.addi %241, %242 : i64
%244 = llvm.getelementptr %41[%243] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %236, %244 : i64, !llvm.ptr
%245 = llvm.load %195 : !llvm.ptr -> i64
%246 = arith.constant 1 : i32
%248 = arith.extsi %246 : i32 to i64
%247 = arith.addi %245, %248 : i64
llvm.store %247, %195 : i64, !llvm.ptr
cf.br ^bb30
^bb32:
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
%249 = arith.constant 1 : i32
%250 = arith.extsi %249 : i32 to i64
llvm.store %250, %87 : i64, !llvm.ptr
cf.br ^bb36
^bb36:
%251 = llvm.load %87 : !llvm.ptr -> i64
%252 = llvm.load %75 : !llvm.ptr -> i64
%253 = arith.cmpi sle, %251, %252 : i64
cf.cond_br %253, ^bb37, ^bb38
^bb37:
%254 = arith.constant 0 : i32
%255 = llvm.load %87 : !llvm.ptr -> i64
%256 = arith.addi %80, %255 : i64
%257 = arith.extsi %254 : i32 to i64
%258 = llvm.getelementptr %41[%256] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %257, %258 : i64, !llvm.ptr
%259 = llvm.load %87 : !llvm.ptr -> i64
%260 = arith.constant 1 : i32
%262 = arith.extsi %260 : i32 to i64
%261 = arith.addi %259, %262 : i64
llvm.store %261, %87 : i64, !llvm.ptr
cf.br ^bb36
^bb38:
%263 = llvm.load %61 : !llvm.ptr -> i64
%264 = arith.constant 1 : i32
%266 = arith.extsi %264 : i32 to i64
%265 = arith.addi %263, %266 : i64
llvm.store %265, %61 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%268 = arith.constant 2 : i32
%269 = llvm.mlir.addressof @N : !llvm.ptr
%270 = llvm.load %269 : !llvm.ptr -> i64
%272 = arith.extsi %268 : i32 to i64
%271 = arith.muli %272, %270 : i64
%267 = func.call @isqrt(%271) : (i64) -> i64
%273 = arith.constant 0 : i32
%274 = arith.extsi %273 : i32 to i64
%275 = llvm.mlir.constant(1 : i64) : i64
%276 = llvm.alloca %275 x i64 : (i64) -> !llvm.ptr
llvm.store %274, %276 : i64, !llvm.ptr
%277 = arith.constant 1 : i32
%278 = arith.extsi %277 : i32 to i64
%279 = llvm.mlir.constant(1 : i64) : i64
%280 = llvm.alloca %279 x i64 : (i64) -> !llvm.ptr
llvm.store %278, %280 : i64, !llvm.ptr
cf.br ^bb39
^bb39:
%281 = llvm.load %280 : !llvm.ptr -> i64
%282 = arith.cmpi sle, %281, %267 : i64
cf.cond_br %282, ^bb40, ^bb41
^bb40:
%283 = llvm.load %276 : !llvm.ptr -> i64
%285 = llvm.mlir.addressof @N : !llvm.ptr
%286 = llvm.load %285 : !llvm.ptr -> i64
%287 = arith.remsi %286, %36 : i64
%288 = arith.muli %287, %39 : i64
%289 = llvm.load %280 : !llvm.ptr -> i64
%290 = arith.addi %288, %289 : i64
%291 = llvm.getelementptr %41[%290] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%284 = llvm.load %291 : !llvm.ptr -> i64
%292 = arith.addi %283, %284 : i64
%293 = llvm.mlir.addressof @MOD : !llvm.ptr
%294 = llvm.load %293 : !llvm.ptr -> i64
%295 = arith.remsi %292, %294 : i64
llvm.store %295, %276 : i64, !llvm.ptr
%296 = llvm.load %280 : !llvm.ptr -> i64
%297 = arith.constant 1 : i32
%299 = arith.extsi %297 : i32 to i64
%298 = arith.addi %296, %299 : i64
llvm.store %298, %280 : i64, !llvm.ptr
cf.br ^bb39
^bb41:
%300 = llvm.mlir.addressof @str_0 : !llvm.ptr
%301 = llvm.load %276 : !llvm.ptr -> i64
%302 = llvm.call @printf(%300, %301) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%41) : (!llvm.ptr) -> ()
%304 = arith.constant 0 : i32
func.return %304 : i32
}
}