Problem 720
Unpredictable permutations: rank of the doubling construction mod 1e9+7. Ported from native C to pure Flow. Builds doubling arrays for 2^24.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^2) | O(n!) |
| Space complexity | O(1) | O(n) |
| Approach | Flow solution | Permutation enumeration or constraint search |
| Verdict | Optimal |
Flow source
# Project Euler 720
# Unpredictable permutations: rank of the doubling construction mod 1e9+7.
# Ported from native C to pure Flow. Builds doubling arrays for 2^24.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function malloc(n: i64) -> ptr<void>
}
const MOD: i64 = 1000000007
function mulmod(a: i64, b: i64) -> i64 {
return (a % MOD) * (b % MOD) % MOD
}
function main() -> i32 {
let k: i32 = 25
if k == 0 {
printf("%lld\n", 1 as i64)
return 0
}
if k == 1 {
printf("%lld\n", 1 as i64)
return 0
}
if k == 2 {
printf("%lld\n", 3 as i64)
return 0
}
let target: i64 = (1 as i64) << (k - 1)
# build_arrays: use mutable pointers so we can reassign
let mut vals: ptr<i32> = malloc(target * 4) as ptr<i32>
let mut codes: ptr<i32> = malloc(target * 4) as ptr<i32>
vals[0] = 1
vals[1] = 3
vals[2] = 2
vals[3] = 4
codes[0] = 0
codes[1] = 1
codes[2] = 0
codes[3] = 0
let mut size: i64 = 4
while size < target {
let m: i64 = size
let n: i64 = m << 1
let nv: ptr<i32> = malloc(n * 4) as ptr<i32>
let nc: ptr<i32> = malloc(n * 4) as ptr<i32>
let mut i: i64 = 0
while i < m - 1 {
let v: i32 = vals[i]
nv[i] = (v << 1) - 1
nc[i] = (v - 1) + codes[i]
i = i + 1
}
nv[m - 1] = 2
nc[m - 1] = 0
let v_last: i32 = vals[m - 1]
nv[m] = (v_last << 1) - 1
nc[m] = (m - 2) as i32
let mut j: i64 = 1
while j < m {
let v: i32 = vals[j]
nv[m + j] = v << 1
nc[m + j] = codes[j]
j = j + 1
}
free(vals)
free(codes)
vals = nv
codes = nc
size = n
}
# rank_from_prev(vals, codes, target)
let m: i64 = target
let mut rank: i64 = 0
let mut fact: i64 = 1
let mut step: i64 = 1
let mut j: i64 = m - 1
while j > 0 {
let l: i64 = codes[j] as i64
rank = (rank + mulmod(l, fact)) % MOD
fact = mulmod(fact, step)
step = step + 1
j = j - 1
}
let l_mid: i64 = m - 2
rank = (rank + mulmod(l_mid, fact)) % MOD
fact = mulmod(fact, step)
step = step + 1
fact = mulmod(fact, step)
step = step + 1
let mut i: i64 = m - 2
while i >= 0 {
let v: i32 = vals[i]
let l: i64 = (v - 1) as i64 + (codes[i] as i64)
rank = (rank + mulmod(l, fact)) % MOD
fact = mulmod(fact, step)
step = step + 1
i = i - 1
}
let rank0: i64 = rank
let result: i64 = (rank0 + 1) % MOD
free(vals)
free(codes)
printf("%lld\n", result)
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 mulmod_i64_i64(int64_t a, int64_t b);
int32_t main(void);
static const int64_t MOD = 1000000007;
int64_t mulmod_i64_i64(int64_t a, int64_t b) {
return FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((a), (MOD)) * FLOW_CHECKED_MOD((b), (MOD)))), (MOD));
}
int32_t main(void) {
int32_t k = 25;
if (k == 0) {
printf("%lld\n", ((int64_t)(1)));
return 0;
}
if (k == 1) {
printf("%lld\n", ((int64_t)(1)));
return 0;
}
if (k == 2) {
printf("%lld\n", ((int64_t)(3)));
return 0;
}
int64_t target = FLOW_CHECKED_SHL((((int64_t)(1))), ((k - 1)));
int32_t* vals = (int32_t*)(((int32_t*)(malloc((target * 4)))));
int32_t* codes = (int32_t*)(((int32_t*)(malloc((target * 4)))));
vals[0] = 1;
vals[1] = 3;
vals[2] = 2;
vals[3] = 4;
codes[0] = 0;
codes[1] = 1;
codes[2] = 0;
codes[3] = 0;
int64_t size = 4;
while (size < target) {
int64_t m = size;
int64_t n = FLOW_CHECKED_SHL((m), (1));
int32_t* nv = (int32_t*)(((int32_t*)(malloc((n * 4)))));
int32_t* nc = (int32_t*)(((int32_t*)(malloc((n * 4)))));
int64_t i = 0;
while (i < (m - 1)) {
int32_t v = vals[i];
nv[i] = (FLOW_CHECKED_SHL((v), (1)) - 1);
nc[i] = ((v - 1) + codes[i]);
i = (i + 1);
}
nv[(m - 1)] = 2;
nc[(m - 1)] = 0;
int32_t v_last = vals[(m - 1)];
nv[m] = (FLOW_CHECKED_SHL((v_last), (1)) - 1);
nc[m] = ((int32_t)((m - 2)));
int64_t j = 1;
while (j < m) {
int32_t v = vals[j];
nv[(m + j)] = FLOW_CHECKED_SHL((v), (1));
nc[(m + j)] = codes[j];
j = (j + 1);
}
free(vals);
free(codes);
vals = nv;
codes = nc;
size = n;
}
int64_t m = target;
int64_t rank = 0;
int64_t fact = 1;
int64_t step = 1;
int64_t j = (m - 1);
while (j > 0) {
int64_t l = ((int64_t)(codes[j]));
rank = FLOW_CHECKED_MOD(((rank + mulmod_i64_i64(l, fact))), (MOD));
fact = mulmod_i64_i64(fact, step);
step = (step + 1);
j = (j - 1);
}
int64_t l_mid = (m - 2);
rank = FLOW_CHECKED_MOD(((rank + mulmod_i64_i64(l_mid, fact))), (MOD));
fact = mulmod_i64_i64(fact, step);
step = (step + 1);
fact = mulmod_i64_i64(fact, step);
step = (step + 1);
int64_t i = (m - 2);
while (i >= 0) {
int32_t v = vals[i];
int64_t l = (((int64_t)((v - 1))) + ((int64_t)(codes[i])));
rank = FLOW_CHECKED_MOD(((rank + mulmod_i64_i64(l, fact))), (MOD));
fact = mulmod_i64_i64(fact, step);
step = (step + 1);
i = (i - 1);
}
int64_t rank0 = rank;
int64_t result = FLOW_CHECKED_MOD(((rank0 + 1)), (MOD));
free(vals);
free(codes);
printf("%lld\n", result);
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 private @malloc(i64) -> !llvm.ptr
// Constant: MOD
llvm.mlir.global internal constant @MOD(1000000007 : i64) : i64
func.func @mulmod(%arg0: i64, %arg1: i64) -> i64 {
%0 = llvm.mlir.addressof @MOD : !llvm.ptr
%1 = llvm.load %0 : !llvm.ptr -> i64
%2 = arith.remsi %arg0, %1 : i64
%3 = llvm.mlir.addressof @MOD : !llvm.ptr
%4 = llvm.load %3 : !llvm.ptr -> i64
%5 = arith.remsi %arg1, %4 : i64
%6 = arith.muli %2, %5 : i64
%7 = llvm.mlir.addressof @MOD : !llvm.ptr
%8 = llvm.load %7 : !llvm.ptr -> i64
%9 = arith.remsi %6, %8 : i64
func.return %9 : i64
}
func.func @main() -> i32 {
%10 = arith.constant 25 : i32
%11 = arith.constant 0 : i32
%12 = arith.cmpi eq, %10, %11 : i32
cf.cond_br %12, ^bb0, ^bb1
^bb0:
%13 = llvm.mlir.addressof @str_0 : !llvm.ptr
%14 = arith.constant 1 : i32
%15 = arith.extsi %14 : i32 to i64
%16 = llvm.call @printf(%13, %15) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%17 = arith.constant 0 : i32
func.return %17 : i32
^bb1:
cf.br ^bb2
^bb2:
%18 = arith.constant 1 : i32
%19 = arith.cmpi eq, %10, %18 : i32
cf.cond_br %19, ^bb3, ^bb4
^bb3:
%20 = llvm.mlir.addressof @str_0 : !llvm.ptr
%21 = arith.constant 1 : i32
%22 = arith.extsi %21 : i32 to i64
%23 = llvm.call @printf(%20, %22) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%24 = arith.constant 0 : i32
func.return %24 : i32
^bb4:
cf.br ^bb5
^bb5:
%25 = arith.constant 2 : i32
%26 = arith.cmpi eq, %10, %25 : i32
cf.cond_br %26, ^bb6, ^bb7
^bb6:
%27 = llvm.mlir.addressof @str_0 : !llvm.ptr
%28 = arith.constant 3 : i32
%29 = arith.extsi %28 : i32 to i64
%30 = llvm.call @printf(%27, %29) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%31 = arith.constant 0 : i32
func.return %31 : i32
^bb7:
cf.br ^bb8
^bb8:
%32 = arith.constant 1 : i32
%33 = arith.extsi %32 : i32 to i64
%34 = arith.constant 1 : i32
%35 = arith.subi %10, %34 : i32
%37 = arith.extsi %35 : i32 to i64
%36 = arith.shli %33, %37 : i64
%39 = arith.constant 4 : i32
%41 = arith.extsi %39 : i32 to i64
%40 = arith.muli %36, %41 : i64
%38 = func.call @malloc(%40) : (i64) -> !llvm.ptr
%42 = llvm.mlir.constant(1 : i64) : i64
%43 = llvm.alloca %42 x !llvm.ptr : (i64) -> !llvm.ptr
llvm.store %38, %43 : !llvm.ptr, !llvm.ptr
%45 = arith.constant 4 : i32
%47 = arith.extsi %45 : i32 to i64
%46 = arith.muli %36, %47 : i64
%44 = func.call @malloc(%46) : (i64) -> !llvm.ptr
%48 = llvm.mlir.constant(1 : i64) : i64
%49 = llvm.alloca %48 x !llvm.ptr : (i64) -> !llvm.ptr
llvm.store %44, %49 : !llvm.ptr, !llvm.ptr
%50 = arith.constant 1 : i32
%51 = llvm.load %43 : !llvm.ptr -> !llvm.ptr
%52 = arith.constant 0 : i32
%53 = arith.extsi %52 : i32 to i64
%54 = llvm.getelementptr %51[%53] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %50, %54 : i32, !llvm.ptr
%55 = arith.constant 3 : i32
%56 = llvm.load %43 : !llvm.ptr -> !llvm.ptr
%57 = arith.constant 1 : i32
%58 = arith.extsi %57 : i32 to i64
%59 = llvm.getelementptr %56[%58] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %55, %59 : i32, !llvm.ptr
%60 = arith.constant 2 : i32
%61 = llvm.load %43 : !llvm.ptr -> !llvm.ptr
%62 = arith.constant 2 : i32
%63 = arith.extsi %62 : i32 to i64
%64 = llvm.getelementptr %61[%63] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %60, %64 : i32, !llvm.ptr
%65 = arith.constant 4 : i32
%66 = llvm.load %43 : !llvm.ptr -> !llvm.ptr
%67 = arith.constant 3 : i32
%68 = arith.extsi %67 : i32 to i64
%69 = llvm.getelementptr %66[%68] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %65, %69 : i32, !llvm.ptr
%70 = arith.constant 0 : i32
%71 = llvm.load %49 : !llvm.ptr -> !llvm.ptr
%72 = arith.constant 0 : i32
%73 = arith.extsi %72 : i32 to i64
%74 = llvm.getelementptr %71[%73] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %70, %74 : i32, !llvm.ptr
%75 = arith.constant 1 : i32
%76 = llvm.load %49 : !llvm.ptr -> !llvm.ptr
%77 = arith.constant 1 : i32
%78 = arith.extsi %77 : i32 to i64
%79 = llvm.getelementptr %76[%78] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %75, %79 : i32, !llvm.ptr
%80 = arith.constant 0 : i32
%81 = llvm.load %49 : !llvm.ptr -> !llvm.ptr
%82 = arith.constant 2 : i32
%83 = arith.extsi %82 : i32 to i64
%84 = llvm.getelementptr %81[%83] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %80, %84 : i32, !llvm.ptr
%85 = arith.constant 0 : i32
%86 = llvm.load %49 : !llvm.ptr -> !llvm.ptr
%87 = arith.constant 3 : i32
%88 = arith.extsi %87 : i32 to i64
%89 = llvm.getelementptr %86[%88] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %85, %89 : i32, !llvm.ptr
%90 = arith.constant 4 : i32
%91 = arith.extsi %90 : i32 to i64
%92 = llvm.mlir.constant(1 : i64) : i64
%93 = llvm.alloca %92 x i64 : (i64) -> !llvm.ptr
llvm.store %91, %93 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%94 = llvm.load %93 : !llvm.ptr -> i64
%95 = arith.cmpi slt, %94, %36 : i64
cf.cond_br %95, ^bb10, ^bb11
^bb10:
%96 = llvm.load %93 : !llvm.ptr -> i64
%97 = arith.constant 1 : i32
%99 = arith.extsi %97 : i32 to i64
%98 = arith.shli %96, %99 : i64
%101 = arith.constant 4 : i32
%103 = arith.extsi %101 : i32 to i64
%102 = arith.muli %98, %103 : i64
%100 = func.call @malloc(%102) : (i64) -> !llvm.ptr
%105 = arith.constant 4 : i32
%107 = arith.extsi %105 : i32 to i64
%106 = arith.muli %98, %107 : i64
%104 = func.call @malloc(%106) : (i64) -> !llvm.ptr
%108 = arith.constant 0 : i32
%109 = arith.extsi %108 : i32 to i64
%110 = llvm.mlir.constant(1 : i64) : i64
%111 = llvm.alloca %110 x i64 : (i64) -> !llvm.ptr
llvm.store %109, %111 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%112 = llvm.load %111 : !llvm.ptr -> i64
%113 = arith.constant 1 : i32
%115 = arith.extsi %113 : i32 to i64
%114 = arith.subi %96, %115 : i64
%116 = arith.cmpi slt, %112, %114 : i64
cf.cond_br %116, ^bb13, ^bb14
^bb13:
%118 = llvm.load %43 : !llvm.ptr -> !llvm.ptr
%119 = llvm.load %111 : !llvm.ptr -> i64
%120 = llvm.getelementptr %118[%119] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%117 = llvm.load %120 : !llvm.ptr -> i32
%121 = arith.constant 1 : i32
%122 = arith.shli %117, %121 : i32
%123 = arith.constant 1 : i32
%124 = arith.subi %122, %123 : i32
%125 = llvm.load %111 : !llvm.ptr -> i64
%126 = llvm.getelementptr %100[%125] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %124, %126 : i32, !llvm.ptr
%127 = arith.constant 1 : i32
%128 = arith.subi %117, %127 : i32
%130 = llvm.load %49 : !llvm.ptr -> !llvm.ptr
%131 = llvm.load %111 : !llvm.ptr -> i64
%132 = llvm.getelementptr %130[%131] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%129 = llvm.load %132 : !llvm.ptr -> i32
%133 = arith.addi %128, %129 : i32
%134 = llvm.load %111 : !llvm.ptr -> i64
%135 = llvm.getelementptr %104[%134] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %133, %135 : i32, !llvm.ptr
%136 = llvm.load %111 : !llvm.ptr -> i64
%137 = arith.constant 1 : i32
%139 = arith.extsi %137 : i32 to i64
%138 = arith.addi %136, %139 : i64
llvm.store %138, %111 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%140 = arith.constant 2 : i32
%141 = arith.constant 1 : i32
%143 = arith.extsi %141 : i32 to i64
%142 = arith.subi %96, %143 : i64
%144 = llvm.getelementptr %100[%142] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %140, %144 : i32, !llvm.ptr
%145 = arith.constant 0 : i32
%146 = arith.constant 1 : i32
%148 = arith.extsi %146 : i32 to i64
%147 = arith.subi %96, %148 : i64
%149 = llvm.getelementptr %104[%147] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %145, %149 : i32, !llvm.ptr
%151 = llvm.load %43 : !llvm.ptr -> !llvm.ptr
%152 = arith.constant 1 : i32
%154 = arith.extsi %152 : i32 to i64
%153 = arith.subi %96, %154 : i64
%155 = llvm.getelementptr %151[%153] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%150 = llvm.load %155 : !llvm.ptr -> i32
%156 = arith.constant 1 : i32
%157 = arith.shli %150, %156 : i32
%158 = arith.constant 1 : i32
%159 = arith.subi %157, %158 : i32
%160 = llvm.getelementptr %100[%96] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %159, %160 : i32, !llvm.ptr
%161 = arith.constant 2 : i32
%163 = arith.extsi %161 : i32 to i64
%162 = arith.subi %96, %163 : i64
%164 = arith.trunci %162 : i64 to i32
%165 = llvm.getelementptr %104[%96] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %164, %165 : i32, !llvm.ptr
%166 = arith.constant 1 : i32
%167 = arith.extsi %166 : i32 to i64
%168 = llvm.mlir.constant(1 : i64) : i64
%169 = llvm.alloca %168 x i64 : (i64) -> !llvm.ptr
llvm.store %167, %169 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%170 = llvm.load %169 : !llvm.ptr -> i64
%171 = arith.cmpi slt, %170, %96 : i64
cf.cond_br %171, ^bb16, ^bb17
^bb16:
%173 = llvm.load %43 : !llvm.ptr -> !llvm.ptr
%174 = llvm.load %169 : !llvm.ptr -> i64
%175 = llvm.getelementptr %173[%174] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%172 = llvm.load %175 : !llvm.ptr -> i32
%176 = arith.constant 1 : i32
%177 = arith.shli %172, %176 : i32
%178 = llvm.load %169 : !llvm.ptr -> i64
%179 = arith.addi %96, %178 : i64
%180 = llvm.getelementptr %100[%179] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %177, %180 : i32, !llvm.ptr
%182 = llvm.load %49 : !llvm.ptr -> !llvm.ptr
%183 = llvm.load %169 : !llvm.ptr -> i64
%184 = llvm.getelementptr %182[%183] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%181 = llvm.load %184 : !llvm.ptr -> i32
%185 = llvm.load %169 : !llvm.ptr -> i64
%186 = arith.addi %96, %185 : i64
%187 = llvm.getelementptr %104[%186] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %181, %187 : i32, !llvm.ptr
%188 = llvm.load %169 : !llvm.ptr -> i64
%189 = arith.constant 1 : i32
%191 = arith.extsi %189 : i32 to i64
%190 = arith.addi %188, %191 : i64
llvm.store %190, %169 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%193 = llvm.load %43 : !llvm.ptr -> !llvm.ptr
func.call @free(%193) : (!llvm.ptr) -> ()
%195 = llvm.load %49 : !llvm.ptr -> !llvm.ptr
func.call @free(%195) : (!llvm.ptr) -> ()
llvm.store %100, %43 : !llvm.ptr, !llvm.ptr
llvm.store %104, %49 : !llvm.ptr, !llvm.ptr
llvm.store %98, %93 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%196 = arith.constant 0 : i32
%197 = arith.extsi %196 : i32 to i64
%198 = llvm.mlir.constant(1 : i64) : i64
%199 = llvm.alloca %198 x i64 : (i64) -> !llvm.ptr
llvm.store %197, %199 : i64, !llvm.ptr
%200 = arith.constant 1 : i32
%201 = arith.extsi %200 : i32 to i64
%202 = llvm.mlir.constant(1 : i64) : i64
%203 = llvm.alloca %202 x i64 : (i64) -> !llvm.ptr
llvm.store %201, %203 : i64, !llvm.ptr
%204 = arith.constant 1 : i32
%205 = arith.extsi %204 : i32 to i64
%206 = llvm.mlir.constant(1 : i64) : i64
%207 = llvm.alloca %206 x i64 : (i64) -> !llvm.ptr
llvm.store %205, %207 : i64, !llvm.ptr
%208 = arith.constant 1 : i32
%210 = arith.extsi %208 : i32 to i64
%209 = arith.subi %36, %210 : i64
%211 = llvm.mlir.constant(1 : i64) : i64
%212 = llvm.alloca %211 x i64 : (i64) -> !llvm.ptr
llvm.store %209, %212 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%213 = llvm.load %212 : !llvm.ptr -> i64
%214 = arith.constant 0 : i32
%216 = arith.extsi %214 : i32 to i64
%215 = arith.cmpi sgt, %213, %216 : i64
cf.cond_br %215, ^bb19, ^bb20
^bb19:
%218 = llvm.load %49 : !llvm.ptr -> !llvm.ptr
%219 = llvm.load %212 : !llvm.ptr -> i64
%220 = llvm.getelementptr %218[%219] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%217 = llvm.load %220 : !llvm.ptr -> i32
%221 = arith.extsi %217 : i32 to i64
%222 = llvm.load %199 : !llvm.ptr -> i64
%224 = llvm.load %203 : !llvm.ptr -> i64
%223 = func.call @mulmod(%221, %224) : (i64, i64) -> i64
%225 = arith.addi %222, %223 : i64
%226 = llvm.mlir.addressof @MOD : !llvm.ptr
%227 = llvm.load %226 : !llvm.ptr -> i64
%228 = arith.remsi %225, %227 : i64
llvm.store %228, %199 : i64, !llvm.ptr
%230 = llvm.load %203 : !llvm.ptr -> i64
%231 = llvm.load %207 : !llvm.ptr -> i64
%229 = func.call @mulmod(%230, %231) : (i64, i64) -> i64
llvm.store %229, %203 : i64, !llvm.ptr
%232 = llvm.load %207 : !llvm.ptr -> i64
%233 = arith.constant 1 : i32
%235 = arith.extsi %233 : i32 to i64
%234 = arith.addi %232, %235 : i64
llvm.store %234, %207 : i64, !llvm.ptr
%236 = llvm.load %212 : !llvm.ptr -> i64
%237 = arith.constant 1 : i32
%239 = arith.extsi %237 : i32 to i64
%238 = arith.subi %236, %239 : i64
llvm.store %238, %212 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%240 = arith.constant 2 : i32
%242 = arith.extsi %240 : i32 to i64
%241 = arith.subi %36, %242 : i64
%243 = llvm.load %199 : !llvm.ptr -> i64
%245 = llvm.load %203 : !llvm.ptr -> i64
%244 = func.call @mulmod(%241, %245) : (i64, i64) -> i64
%246 = arith.addi %243, %244 : i64
%247 = llvm.mlir.addressof @MOD : !llvm.ptr
%248 = llvm.load %247 : !llvm.ptr -> i64
%249 = arith.remsi %246, %248 : i64
llvm.store %249, %199 : i64, !llvm.ptr
%251 = llvm.load %203 : !llvm.ptr -> i64
%252 = llvm.load %207 : !llvm.ptr -> i64
%250 = func.call @mulmod(%251, %252) : (i64, i64) -> i64
llvm.store %250, %203 : i64, !llvm.ptr
%253 = llvm.load %207 : !llvm.ptr -> i64
%254 = arith.constant 1 : i32
%256 = arith.extsi %254 : i32 to i64
%255 = arith.addi %253, %256 : i64
llvm.store %255, %207 : i64, !llvm.ptr
%258 = llvm.load %203 : !llvm.ptr -> i64
%259 = llvm.load %207 : !llvm.ptr -> i64
%257 = func.call @mulmod(%258, %259) : (i64, i64) -> i64
llvm.store %257, %203 : i64, !llvm.ptr
%260 = llvm.load %207 : !llvm.ptr -> i64
%261 = arith.constant 1 : i32
%263 = arith.extsi %261 : i32 to i64
%262 = arith.addi %260, %263 : i64
llvm.store %262, %207 : i64, !llvm.ptr
%264 = arith.constant 2 : i32
%266 = arith.extsi %264 : i32 to i64
%265 = arith.subi %36, %266 : i64
%267 = llvm.mlir.constant(1 : i64) : i64
%268 = llvm.alloca %267 x i64 : (i64) -> !llvm.ptr
llvm.store %265, %268 : i64, !llvm.ptr
cf.br ^bb21
^bb21:
%269 = llvm.load %268 : !llvm.ptr -> i64
%270 = arith.constant 0 : i32
%272 = arith.extsi %270 : i32 to i64
%271 = arith.cmpi sge, %269, %272 : i64
cf.cond_br %271, ^bb22, ^bb23
^bb22:
%274 = llvm.load %43 : !llvm.ptr -> !llvm.ptr
%275 = llvm.load %268 : !llvm.ptr -> i64
%276 = llvm.getelementptr %274[%275] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%273 = llvm.load %276 : !llvm.ptr -> i32
%277 = arith.constant 1 : i32
%278 = arith.subi %273, %277 : i32
%279 = arith.extsi %278 : i32 to i64
%281 = llvm.load %49 : !llvm.ptr -> !llvm.ptr
%282 = llvm.load %268 : !llvm.ptr -> i64
%283 = llvm.getelementptr %281[%282] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%280 = llvm.load %283 : !llvm.ptr -> i32
%284 = arith.extsi %280 : i32 to i64
%285 = arith.addi %279, %284 : i64
%286 = llvm.load %199 : !llvm.ptr -> i64
%288 = llvm.load %203 : !llvm.ptr -> i64
%287 = func.call @mulmod(%285, %288) : (i64, i64) -> i64
%289 = arith.addi %286, %287 : i64
%290 = llvm.mlir.addressof @MOD : !llvm.ptr
%291 = llvm.load %290 : !llvm.ptr -> i64
%292 = arith.remsi %289, %291 : i64
llvm.store %292, %199 : i64, !llvm.ptr
%294 = llvm.load %203 : !llvm.ptr -> i64
%295 = llvm.load %207 : !llvm.ptr -> i64
%293 = func.call @mulmod(%294, %295) : (i64, i64) -> i64
llvm.store %293, %203 : i64, !llvm.ptr
%296 = llvm.load %207 : !llvm.ptr -> i64
%297 = arith.constant 1 : i32
%299 = arith.extsi %297 : i32 to i64
%298 = arith.addi %296, %299 : i64
llvm.store %298, %207 : i64, !llvm.ptr
%300 = llvm.load %268 : !llvm.ptr -> i64
%301 = arith.constant 1 : i32
%303 = arith.extsi %301 : i32 to i64
%302 = arith.subi %300, %303 : i64
llvm.store %302, %268 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%304 = llvm.load %199 : !llvm.ptr -> i64
%305 = arith.constant 1 : i32
%307 = arith.extsi %305 : i32 to i64
%306 = arith.addi %304, %307 : i64
%308 = llvm.mlir.addressof @MOD : !llvm.ptr
%309 = llvm.load %308 : !llvm.ptr -> i64
%310 = arith.remsi %306, %309 : i64
%312 = llvm.load %43 : !llvm.ptr -> !llvm.ptr
func.call @free(%312) : (!llvm.ptr) -> ()
%314 = llvm.load %49 : !llvm.ptr -> !llvm.ptr
func.call @free(%314) : (!llvm.ptr) -> ()
%315 = llvm.mlir.addressof @str_0 : !llvm.ptr
%316 = llvm.call @printf(%315, %310) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%317 = arith.constant 0 : i32
func.return %317 : i32
}
}