← All problems
Problem 391
sum_{n=1..1000} M(n)^3 via popcount block mapping composition.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)?
Space complexity O(n^2)?
Approach Flow solution Not curated
Verdict Unknown
Flow source
# Project Euler 391
# sum_{n=1..1000} M(n)^3 via popcount block mapping composition.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
# Mapping kinds: 0=identity, 1=constant, 2=table
function apply_map(kind: i32, cval: i32, tab: ptr<i32>, base: i64, s: i32) -> i32 {
if kind == 0 { return s }
if kind == 1 { return cval }
return tab[base + (s as i64)]
}
function is_const_table(tab: ptr<i32>, base: i64, n: i32) -> i32 {
let first: i32 = tab[base]
let mut s: i32 = 1
while s <= n {
if tab[base + (s as i64)] != first { return 0 - 1 }
s = s + 1
}
return first
}
function compute_M(n: i32) -> i32 {
let MAX_K: i32 = 40
let WIDTH: i32 = MAX_K + 2
let np1: i64 = (n + 1) as i64
let cells: i64 = (WIDTH as i64) * np1
let kind_a: ptr<i32> = calloc(WIDTH as i64, 4)
let kind_b: ptr<i32> = calloc(WIDTH as i64, 4)
let cval_a: ptr<i32> = calloc(WIDTH as i64, 4)
let cval_b: ptr<i32> = calloc(WIDTH as i64, 4)
let tab_a: ptr<i32> = calloc(cells, 4)
let tab_b: ptr<i32> = calloc(cells, 4)
if kind_a == null || kind_b == null || cval_a == null || cval_b == null { return 0 }
if tab_a == null || tab_b == null { return 0 }
kind_a[0] = 0
let mut off: i32 = 1
while off < WIDTH {
if off > n {
kind_a[off] = 1
cval_a[off] = 0
} else {
kind_a[off] = 2
let base: i64 = (off as i64) * np1
let lim: i32 = n - off
let mut s: i32 = 0
while s <= n {
if s <= lim {
tab_a[base + (s as i64)] = s + off
} else {
tab_a[base + (s as i64)] = 0
}
s = s + 1
}
}
off = off + 1
}
let mut k: i32 = 1
let mut result: i32 = 0
let mut done: i32 = 0
while k <= MAX_K {
if done == 1 { break }
let mut o: i32 = 0
while o < WIDTH - 1 {
let ak: i32 = kind_a[o]
let bk: i32 = kind_a[o + 1]
let ac: i32 = cval_a[o]
let bc: i32 = cval_a[o + 1]
let abase: i64 = (o as i64) * np1
let bbase: i64 = ((o + 1) as i64) * np1
let obase: i64 = abase
if ak == 1 {
kind_b[o] = 1
cval_b[o] = ac
} else {
if bk == 1 {
kind_b[o] = 1
cval_b[o] = apply_map(ak, ac, tab_a, abase, bc)
} else {
if ak == 0 {
kind_b[o] = bk
cval_b[o] = bc
if bk == 2 {
let mut s: i32 = 0
while s <= n {
tab_b[obase + (s as i64)] = tab_a[bbase + (s as i64)]
s = s + 1
}
}
} else {
if bk == 0 {
kind_b[o] = ak
cval_b[o] = ac
if ak == 2 {
let mut s: i32 = 0
while s <= n {
tab_b[obase + (s as i64)] = tab_a[abase + (s as i64)]
s = s + 1
}
}
} else {
kind_b[o] = 2
let mut s: i32 = 0
while s <= n {
let mid: i32 = tab_a[bbase + (s as i64)]
tab_b[obase + (s as i64)] = tab_a[abase + (mid as i64)]
s = s + 1
}
}
}
}
}
o = o + 1
}
kind_b[WIDTH - 1] = 1
cval_b[WIDTH - 1] = 0
let mut root_k: i32 = kind_b[0]
let mut root_c: i32 = cval_b[0]
if root_k == 2 {
let c: i32 = is_const_table(tab_b, 0, n)
if c >= 0 {
kind_b[0] = 1
cval_b[0] = c
root_c = c
root_k = 1
}
}
if root_k == 1 {
result = root_c
done = 1
} else {
let mut i: i32 = 0
while i < WIDTH {
kind_a[i] = kind_b[i]
cval_a[i] = cval_b[i]
i = i + 1
}
let mut j: i64 = 0
while j < cells {
tab_a[j] = tab_b[j]
j = j + 1
}
}
k = k + 1
}
free(tab_b)
free(tab_a)
free(cval_b)
free(cval_a)
free(kind_b)
free(kind_a)
return result
}
function main() -> i32 {
let mut total: i64 = 0
let mut n: i32 = 1
while n <= 1000 {
let x: i64 = compute_M(n) as i64
total = total + x * x * x
n = n + 1
}
printf("%lld\n", total)
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; }
int32_t apply_map_i32_i32_ptr_i32_i64_i32(int32_t kind, int32_t cval, int32_t* tab, int64_t base, int32_t s);
int32_t is_const_table_ptr_i32_i64_i32(int32_t* tab, int64_t base, int32_t n);
int32_t compute_M_i32(int32_t n);
int32_t main(void);
int32_t apply_map_i32_i32_ptr_i32_i64_i32(int32_t kind, int32_t cval, int32_t* tab, int64_t base, int32_t s) {
if (kind == 0) {
return s;
}
if (kind == 1) {
return cval;
}
return tab[(base + ((int64_t)(s)))];
}
int32_t is_const_table_ptr_i32_i64_i32(int32_t* tab, int64_t base, int32_t n) {
int32_t first = tab[base];
int32_t s = 1;
while (s <= n) {
if (tab[(base + ((int64_t)(s)))] != first) {
return (0 - 1);
}
s = (s + 1);
}
return first;
}
int32_t compute_M_i32(int32_t n) {
int32_t MAX_K = 40;
int32_t WIDTH = (MAX_K + 2);
int64_t np1 = ((int64_t)((n + 1)));
int64_t cells = (((int64_t)(WIDTH)) * np1);
int32_t* kind_a = (int32_t*)(calloc(((int64_t)(WIDTH)), 4));
int32_t* kind_b = (int32_t*)(calloc(((int64_t)(WIDTH)), 4));
int32_t* cval_a = (int32_t*)(calloc(((int64_t)(WIDTH)), 4));
int32_t* cval_b = (int32_t*)(calloc(((int64_t)(WIDTH)), 4));
int32_t* tab_a = (int32_t*)(calloc(cells, 4));
int32_t* tab_b = (int32_t*)(calloc(cells, 4));
if ((((kind_a == NULL || kind_b == NULL) || cval_a == NULL) || cval_b == NULL)) {
return 0;
}
if ((tab_a == NULL || tab_b == NULL)) {
return 0;
}
kind_a[0] = 0;
int32_t off = 1;
while (off < WIDTH) {
if (off > n) {
kind_a[off] = 1;
cval_a[off] = 0;
} else {
kind_a[off] = 2;
int64_t base = (((int64_t)(off)) * np1);
int32_t lim = (n - off);
int32_t s = 0;
while (s <= n) {
if (s <= lim) {
tab_a[(base + ((int64_t)(s)))] = (s + off);
} else {
tab_a[(base + ((int64_t)(s)))] = 0;
}
s = (s + 1);
}
}
off = (off + 1);
}
int32_t k = 1;
int32_t result = 0;
int32_t done = 0;
while (k <= MAX_K) {
if (done == 1) {
break;
}
int32_t o = 0;
while (o < (WIDTH - 1)) {
int32_t ak = kind_a[o];
int32_t bk = kind_a[(o + 1)];
int32_t ac = cval_a[o];
int32_t bc = cval_a[(o + 1)];
int64_t abase = (((int64_t)(o)) * np1);
int64_t bbase = (((int64_t)((o + 1))) * np1);
int64_t obase = abase;
if (ak == 1) {
kind_b[o] = 1;
cval_b[o] = ac;
} else {
if (bk == 1) {
kind_b[o] = 1;
cval_b[o] = apply_map_i32_i32_ptr_i32_i64_i32(ak, ac, tab_a, abase, bc);
} else {
if (ak == 0) {
kind_b[o] = bk;
cval_b[o] = bc;
if (bk == 2) {
int32_t s = 0;
while (s <= n) {
tab_b[(obase + ((int64_t)(s)))] = tab_a[(bbase + ((int64_t)(s)))];
s = (s + 1);
}
}
} else {
if (bk == 0) {
kind_b[o] = ak;
cval_b[o] = ac;
if (ak == 2) {
int32_t s = 0;
while (s <= n) {
tab_b[(obase + ((int64_t)(s)))] = tab_a[(abase + ((int64_t)(s)))];
s = (s + 1);
}
}
} else {
kind_b[o] = 2;
int32_t s = 0;
while (s <= n) {
int32_t mid = tab_a[(bbase + ((int64_t)(s)))];
tab_b[(obase + ((int64_t)(s)))] = tab_a[(abase + ((int64_t)(mid)))];
s = (s + 1);
}
}
}
}
}
o = (o + 1);
}
kind_b[(WIDTH - 1)] = 1;
cval_b[(WIDTH - 1)] = 0;
int32_t root_k = kind_b[0];
int32_t root_c = cval_b[0];
if (root_k == 2) {
int32_t c = is_const_table_ptr_i32_i64_i32(tab_b, 0, n);
if (c >= 0) {
kind_b[0] = 1;
cval_b[0] = c;
root_c = c;
root_k = 1;
}
}
if (root_k == 1) {
result = root_c;
done = 1;
} else {
int32_t i = 0;
while (i < WIDTH) {
kind_a[i] = kind_b[i];
cval_a[i] = cval_b[i];
i = (i + 1);
}
int64_t j = 0;
while (j < cells) {
tab_a[j] = tab_b[j];
j = (j + 1);
}
}
k = (k + 1);
}
free(tab_b);
free(tab_a);
free(cval_b);
free(cval_a);
free(kind_b);
free(kind_a);
return result;
}
int32_t main(void) {
int64_t total = 0;
int32_t n = 1;
while (n <= 1000) {
int64_t x = ((int64_t)(compute_M_i32(n)));
total = (total + ((x * x) * x));
n = (n + 1);
}
printf("%lld\n", total);
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 @apply_map(%arg0: i32, %arg1: i32, %arg2: !llvm.ptr, %arg3: i64, %arg4: i32) -> i32 {
%0 = arith.constant 0 : i32
%1 = arith.cmpi eq, %arg0, %0 : i32
cf.cond_br %1, ^bb0, ^bb1
^bb0:
func.return %arg4 : i32
^bb1:
cf.br ^bb2
^bb2:
%2 = arith.constant 1 : i32
%3 = arith.cmpi eq, %arg0, %2 : i32
cf.cond_br %3, ^bb3, ^bb4
^bb3:
func.return %arg1 : i32
^bb4:
cf.br ^bb5
^bb5:
%5 = arith.extsi %arg4 : i32 to i64
%6 = arith.addi %arg3, %5 : i64
%7 = llvm.getelementptr %arg2[%6] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%4 = llvm.load %7 : !llvm.ptr -> i32
func.return %4 : i32
}
func.func @is_const_table(%arg0: !llvm.ptr, %arg1: i64, %arg2: i32) -> i32 {
%9 = llvm.getelementptr %arg0[%arg1] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%8 = llvm.load %9 : !llvm.ptr -> i32
%10 = arith.constant 1 : i32
%11 = llvm.mlir.constant(1 : i64) : i64
%12 = llvm.alloca %11 x i32 : (i64) -> !llvm.ptr
llvm.store %10, %12 : i32, !llvm.ptr
cf.br ^bb6
^bb6:
%13 = llvm.load %12 : !llvm.ptr -> i32
%14 = arith.cmpi sle, %13, %arg2 : i32
cf.cond_br %14, ^bb7, ^bb8
^bb7:
%16 = llvm.load %12 : !llvm.ptr -> i32
%17 = arith.extsi %16 : i32 to i64
%18 = arith.addi %arg1, %17 : i64
%19 = llvm.getelementptr %arg0[%18] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%15 = llvm.load %19 : !llvm.ptr -> i32
%20 = arith.cmpi ne, %15, %8 : i32
cf.cond_br %20, ^bb9, ^bb10
^bb9:
%21 = arith.constant 0 : i32
%22 = arith.constant 1 : i32
%23 = arith.subi %21, %22 : i32
func.return %23 : i32
^bb10:
cf.br ^bb11
^bb11:
%24 = llvm.load %12 : !llvm.ptr -> i32
%25 = arith.constant 1 : i32
%26 = arith.addi %24, %25 : i32
llvm.store %26, %12 : i32, !llvm.ptr
cf.br ^bb6
^bb8:
func.return %8 : i32
}
func.func @compute_M(%arg0: i32) -> i32 {
%27 = arith.constant 40 : i32
%28 = arith.constant 2 : i32
%29 = arith.addi %27, %28 : i32
%30 = arith.constant 1 : i32
%31 = arith.addi %arg0, %30 : i32
%32 = arith.extsi %31 : i32 to i64
%33 = arith.extsi %29 : i32 to i64
%34 = arith.muli %33, %32 : i64
%36 = arith.extsi %29 : i32 to i64
%37 = arith.constant 4 : i32
%38 = arith.extsi %37 : i32 to i64
%35 = func.call @calloc(%36, %38) : (i64, i64) -> !llvm.ptr
%40 = arith.extsi %29 : i32 to i64
%41 = arith.constant 4 : i32
%42 = arith.extsi %41 : i32 to i64
%39 = func.call @calloc(%40, %42) : (i64, i64) -> !llvm.ptr
%44 = arith.extsi %29 : i32 to i64
%45 = arith.constant 4 : i32
%46 = arith.extsi %45 : i32 to i64
%43 = func.call @calloc(%44, %46) : (i64, i64) -> !llvm.ptr
%48 = arith.extsi %29 : i32 to i64
%49 = arith.constant 4 : i32
%50 = arith.extsi %49 : i32 to i64
%47 = func.call @calloc(%48, %50) : (i64, i64) -> !llvm.ptr
%52 = arith.constant 4 : i32
%53 = arith.extsi %52 : i32 to i64
%51 = func.call @calloc(%34, %53) : (i64, i64) -> !llvm.ptr
%55 = arith.constant 4 : i32
%56 = arith.extsi %55 : i32 to i64
%54 = func.call @calloc(%34, %56) : (i64, i64) -> !llvm.ptr
%57 = llvm.mlir.zero : !llvm.ptr
%58 = llvm.icmp "eq" %35, %57 : !llvm.ptr
%59 = scf.if %58 -> (i1) {
%60 = arith.constant true
scf.yield %60 : i1
} else {
%61 = llvm.mlir.zero : !llvm.ptr
%62 = llvm.icmp "eq" %39, %61 : !llvm.ptr
scf.yield %62 : i1
}
%63 = scf.if %59 -> (i1) {
%64 = arith.constant true
scf.yield %64 : i1
} else {
%65 = llvm.mlir.zero : !llvm.ptr
%66 = llvm.icmp "eq" %43, %65 : !llvm.ptr
scf.yield %66 : i1
}
%67 = scf.if %63 -> (i1) {
%68 = arith.constant true
scf.yield %68 : i1
} else {
%69 = llvm.mlir.zero : !llvm.ptr
%70 = llvm.icmp "eq" %47, %69 : !llvm.ptr
scf.yield %70 : i1
}
cf.cond_br %67, ^bb12, ^bb13
^bb12:
%71 = arith.constant 0 : i32
func.return %71 : i32
^bb13:
cf.br ^bb14
^bb14:
%72 = llvm.mlir.zero : !llvm.ptr
%73 = llvm.icmp "eq" %51, %72 : !llvm.ptr
%74 = scf.if %73 -> (i1) {
%75 = arith.constant true
scf.yield %75 : i1
} else {
%76 = llvm.mlir.zero : !llvm.ptr
%77 = llvm.icmp "eq" %54, %76 : !llvm.ptr
scf.yield %77 : i1
}
cf.cond_br %74, ^bb15, ^bb16
^bb15:
%78 = arith.constant 0 : i32
func.return %78 : i32
^bb16:
cf.br ^bb17
^bb17:
%79 = arith.constant 0 : i32
%80 = arith.constant 0 : i32
%81 = arith.extsi %80 : i32 to i64
%82 = llvm.getelementptr %35[%81] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %79, %82 : i32, !llvm.ptr
%83 = arith.constant 1 : i32
%84 = llvm.mlir.constant(1 : i64) : i64
%85 = llvm.alloca %84 x i32 : (i64) -> !llvm.ptr
llvm.store %83, %85 : i32, !llvm.ptr
cf.br ^bb18
^bb18:
%86 = llvm.load %85 : !llvm.ptr -> i32
%87 = arith.cmpi slt, %86, %29 : i32
cf.cond_br %87, ^bb19, ^bb20
^bb19:
%88 = llvm.load %85 : !llvm.ptr -> i32
%89 = arith.cmpi sgt, %88, %arg0 : i32
cf.cond_br %89, ^bb21, ^bb22
^bb21:
%90 = arith.constant 1 : i32
%91 = llvm.load %85 : !llvm.ptr -> i32
%92 = arith.extsi %91 : i32 to i64
%93 = llvm.getelementptr %35[%92] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %90, %93 : i32, !llvm.ptr
%94 = arith.constant 0 : i32
%95 = llvm.load %85 : !llvm.ptr -> i32
%96 = arith.extsi %95 : i32 to i64
%97 = llvm.getelementptr %43[%96] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %94, %97 : i32, !llvm.ptr
cf.br ^bb23
^bb22:
%98 = arith.constant 2 : i32
%99 = llvm.load %85 : !llvm.ptr -> i32
%100 = arith.extsi %99 : i32 to i64
%101 = llvm.getelementptr %35[%100] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %98, %101 : i32, !llvm.ptr
%102 = llvm.load %85 : !llvm.ptr -> i32
%103 = arith.extsi %102 : i32 to i64
%104 = arith.muli %103, %32 : i64
%105 = llvm.load %85 : !llvm.ptr -> i32
%106 = arith.subi %arg0, %105 : i32
%107 = arith.constant 0 : i32
%108 = llvm.mlir.constant(1 : i64) : i64
%109 = llvm.alloca %108 x i32 : (i64) -> !llvm.ptr
llvm.store %107, %109 : i32, !llvm.ptr
cf.br ^bb24
^bb24:
%110 = llvm.load %109 : !llvm.ptr -> i32
%111 = arith.cmpi sle, %110, %arg0 : i32
cf.cond_br %111, ^bb25, ^bb26
^bb25:
%112 = llvm.load %109 : !llvm.ptr -> i32
%113 = arith.cmpi sle, %112, %106 : i32
cf.cond_br %113, ^bb27, ^bb28
^bb27:
%114 = llvm.load %109 : !llvm.ptr -> i32
%115 = llvm.load %85 : !llvm.ptr -> i32
%116 = arith.addi %114, %115 : i32
%117 = llvm.load %109 : !llvm.ptr -> i32
%118 = arith.extsi %117 : i32 to i64
%119 = arith.addi %104, %118 : i64
%120 = llvm.getelementptr %51[%119] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %116, %120 : i32, !llvm.ptr
cf.br ^bb29
^bb28:
%121 = arith.constant 0 : i32
%122 = llvm.load %109 : !llvm.ptr -> i32
%123 = arith.extsi %122 : i32 to i64
%124 = arith.addi %104, %123 : i64
%125 = llvm.getelementptr %51[%124] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %121, %125 : i32, !llvm.ptr
cf.br ^bb29
^bb29:
%126 = llvm.load %109 : !llvm.ptr -> i32
%127 = arith.constant 1 : i32
%128 = arith.addi %126, %127 : i32
llvm.store %128, %109 : i32, !llvm.ptr
cf.br ^bb24
^bb26:
cf.br ^bb23
^bb23:
%129 = llvm.load %85 : !llvm.ptr -> i32
%130 = arith.constant 1 : i32
%131 = arith.addi %129, %130 : i32
llvm.store %131, %85 : i32, !llvm.ptr
cf.br ^bb18
^bb20:
%132 = arith.constant 1 : i32
%133 = llvm.mlir.constant(1 : i64) : i64
%134 = llvm.alloca %133 x i32 : (i64) -> !llvm.ptr
llvm.store %132, %134 : i32, !llvm.ptr
%135 = arith.constant 0 : i32
%136 = llvm.mlir.constant(1 : i64) : i64
%137 = llvm.alloca %136 x i32 : (i64) -> !llvm.ptr
llvm.store %135, %137 : i32, !llvm.ptr
%138 = arith.constant 0 : i32
%139 = llvm.mlir.constant(1 : i64) : i64
%140 = llvm.alloca %139 x i32 : (i64) -> !llvm.ptr
llvm.store %138, %140 : i32, !llvm.ptr
cf.br ^bb30
^bb30:
%141 = llvm.load %134 : !llvm.ptr -> i32
%142 = arith.cmpi sle, %141, %27 : i32
cf.cond_br %142, ^bb31, ^bb32
^bb31:
%143 = llvm.load %140 : !llvm.ptr -> i32
%144 = arith.constant 1 : i32
%145 = arith.cmpi eq, %143, %144 : i32
cf.cond_br %145, ^bb33, ^bb34
^bb33:
cf.br ^bb32
^bb34:
cf.br ^bb35
^bb35:
%146 = arith.constant 0 : i32
%147 = llvm.mlir.constant(1 : i64) : i64
%148 = llvm.alloca %147 x i32 : (i64) -> !llvm.ptr
llvm.store %146, %148 : i32, !llvm.ptr
cf.br ^bb36
^bb36:
%149 = llvm.load %148 : !llvm.ptr -> i32
%150 = arith.constant 1 : i32
%151 = arith.subi %29, %150 : i32
%152 = arith.cmpi slt, %149, %151 : i32
cf.cond_br %152, ^bb37, ^bb38
^bb37:
%154 = llvm.load %148 : !llvm.ptr -> i32
%155 = arith.extsi %154 : i32 to i64
%156 = llvm.getelementptr %35[%155] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%153 = llvm.load %156 : !llvm.ptr -> i32
%158 = llvm.load %148 : !llvm.ptr -> i32
%159 = arith.constant 1 : i32
%160 = arith.addi %158, %159 : i32
%161 = arith.extsi %160 : i32 to i64
%162 = llvm.getelementptr %35[%161] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%157 = llvm.load %162 : !llvm.ptr -> i32
%164 = llvm.load %148 : !llvm.ptr -> i32
%165 = arith.extsi %164 : i32 to i64
%166 = llvm.getelementptr %43[%165] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%163 = llvm.load %166 : !llvm.ptr -> i32
%168 = llvm.load %148 : !llvm.ptr -> i32
%169 = arith.constant 1 : i32
%170 = arith.addi %168, %169 : i32
%171 = arith.extsi %170 : i32 to i64
%172 = llvm.getelementptr %43[%171] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%167 = llvm.load %172 : !llvm.ptr -> i32
%173 = llvm.load %148 : !llvm.ptr -> i32
%174 = arith.extsi %173 : i32 to i64
%175 = arith.muli %174, %32 : i64
%176 = llvm.load %148 : !llvm.ptr -> i32
%177 = arith.constant 1 : i32
%178 = arith.addi %176, %177 : i32
%179 = arith.extsi %178 : i32 to i64
%180 = arith.muli %179, %32 : i64
%181 = arith.constant 1 : i32
%182 = arith.cmpi eq, %153, %181 : i32
cf.cond_br %182, ^bb39, ^bb40
^bb39:
%183 = arith.constant 1 : i32
%184 = llvm.load %148 : !llvm.ptr -> i32
%185 = arith.extsi %184 : i32 to i64
%186 = llvm.getelementptr %39[%185] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %183, %186 : i32, !llvm.ptr
%187 = llvm.load %148 : !llvm.ptr -> i32
%188 = arith.extsi %187 : i32 to i64
%189 = llvm.getelementptr %47[%188] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %163, %189 : i32, !llvm.ptr
cf.br ^bb41
^bb40:
%190 = arith.constant 1 : i32
%191 = arith.cmpi eq, %157, %190 : i32
cf.cond_br %191, ^bb42, ^bb43
^bb42:
%192 = arith.constant 1 : i32
%193 = llvm.load %148 : !llvm.ptr -> i32
%194 = arith.extsi %193 : i32 to i64
%195 = llvm.getelementptr %39[%194] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %192, %195 : i32, !llvm.ptr
%196 = func.call @apply_map(%153, %163, %51, %175, %167) : (i32, i32, !llvm.ptr, i64, i32) -> i32
%197 = llvm.load %148 : !llvm.ptr -> i32
%198 = arith.extsi %197 : i32 to i64
%199 = llvm.getelementptr %47[%198] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %196, %199 : i32, !llvm.ptr
cf.br ^bb44
^bb43:
%200 = arith.constant 0 : i32
%201 = arith.cmpi eq, %153, %200 : i32
cf.cond_br %201, ^bb45, ^bb46
^bb45:
%202 = llvm.load %148 : !llvm.ptr -> i32
%203 = arith.extsi %202 : i32 to i64
%204 = llvm.getelementptr %39[%203] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %157, %204 : i32, !llvm.ptr
%205 = llvm.load %148 : !llvm.ptr -> i32
%206 = arith.extsi %205 : i32 to i64
%207 = llvm.getelementptr %47[%206] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %167, %207 : i32, !llvm.ptr
%208 = arith.constant 2 : i32
%209 = arith.cmpi eq, %157, %208 : i32
cf.cond_br %209, ^bb48, ^bb49
^bb48:
%210 = arith.constant 0 : i32
%211 = llvm.mlir.constant(1 : i64) : i64
%212 = llvm.alloca %211 x i32 : (i64) -> !llvm.ptr
llvm.store %210, %212 : i32, !llvm.ptr
cf.br ^bb51
^bb51:
%213 = llvm.load %212 : !llvm.ptr -> i32
%214 = arith.cmpi sle, %213, %arg0 : i32
cf.cond_br %214, ^bb52, ^bb53
^bb52:
%216 = llvm.load %212 : !llvm.ptr -> i32
%217 = arith.extsi %216 : i32 to i64
%218 = arith.addi %180, %217 : i64
%219 = llvm.getelementptr %51[%218] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%215 = llvm.load %219 : !llvm.ptr -> i32
%220 = llvm.load %212 : !llvm.ptr -> i32
%221 = arith.extsi %220 : i32 to i64
%222 = arith.addi %175, %221 : i64
%223 = llvm.getelementptr %54[%222] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %215, %223 : i32, !llvm.ptr
%224 = llvm.load %212 : !llvm.ptr -> i32
%225 = arith.constant 1 : i32
%226 = arith.addi %224, %225 : i32
llvm.store %226, %212 : i32, !llvm.ptr
cf.br ^bb51
^bb53:
cf.br ^bb50
^bb49:
cf.br ^bb50
^bb50:
cf.br ^bb47
^bb46:
%227 = arith.constant 0 : i32
%228 = arith.cmpi eq, %157, %227 : i32
cf.cond_br %228, ^bb54, ^bb55
^bb54:
%229 = llvm.load %148 : !llvm.ptr -> i32
%230 = arith.extsi %229 : i32 to i64
%231 = llvm.getelementptr %39[%230] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %153, %231 : i32, !llvm.ptr
%232 = llvm.load %148 : !llvm.ptr -> i32
%233 = arith.extsi %232 : i32 to i64
%234 = llvm.getelementptr %47[%233] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %163, %234 : i32, !llvm.ptr
%235 = arith.constant 2 : i32
%236 = arith.cmpi eq, %153, %235 : i32
cf.cond_br %236, ^bb57, ^bb58
^bb57:
%237 = arith.constant 0 : i32
%238 = llvm.mlir.constant(1 : i64) : i64
%239 = llvm.alloca %238 x i32 : (i64) -> !llvm.ptr
llvm.store %237, %239 : i32, !llvm.ptr
cf.br ^bb60
^bb60:
%240 = llvm.load %239 : !llvm.ptr -> i32
%241 = arith.cmpi sle, %240, %arg0 : i32
cf.cond_br %241, ^bb61, ^bb62
^bb61:
%243 = llvm.load %239 : !llvm.ptr -> i32
%244 = arith.extsi %243 : i32 to i64
%245 = arith.addi %175, %244 : i64
%246 = llvm.getelementptr %51[%245] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%242 = llvm.load %246 : !llvm.ptr -> i32
%247 = llvm.load %239 : !llvm.ptr -> i32
%248 = arith.extsi %247 : i32 to i64
%249 = arith.addi %175, %248 : i64
%250 = llvm.getelementptr %54[%249] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %242, %250 : i32, !llvm.ptr
%251 = llvm.load %239 : !llvm.ptr -> i32
%252 = arith.constant 1 : i32
%253 = arith.addi %251, %252 : i32
llvm.store %253, %239 : i32, !llvm.ptr
cf.br ^bb60
^bb62:
cf.br ^bb59
^bb58:
cf.br ^bb59
^bb59:
cf.br ^bb56
^bb55:
%254 = arith.constant 2 : i32
%255 = llvm.load %148 : !llvm.ptr -> i32
%256 = arith.extsi %255 : i32 to i64
%257 = llvm.getelementptr %39[%256] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %254, %257 : i32, !llvm.ptr
%258 = arith.constant 0 : i32
%259 = llvm.mlir.constant(1 : i64) : i64
%260 = llvm.alloca %259 x i32 : (i64) -> !llvm.ptr
llvm.store %258, %260 : i32, !llvm.ptr
cf.br ^bb63
^bb63:
%261 = llvm.load %260 : !llvm.ptr -> i32
%262 = arith.cmpi sle, %261, %arg0 : i32
cf.cond_br %262, ^bb64, ^bb65
^bb64:
%264 = llvm.load %260 : !llvm.ptr -> i32
%265 = arith.extsi %264 : i32 to i64
%266 = arith.addi %180, %265 : i64
%267 = llvm.getelementptr %51[%266] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%263 = llvm.load %267 : !llvm.ptr -> i32
%269 = arith.extsi %263 : i32 to i64
%270 = arith.addi %175, %269 : i64
%271 = llvm.getelementptr %51[%270] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%268 = llvm.load %271 : !llvm.ptr -> i32
%272 = llvm.load %260 : !llvm.ptr -> i32
%273 = arith.extsi %272 : i32 to i64
%274 = arith.addi %175, %273 : i64
%275 = llvm.getelementptr %54[%274] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %268, %275 : i32, !llvm.ptr
%276 = llvm.load %260 : !llvm.ptr -> i32
%277 = arith.constant 1 : i32
%278 = arith.addi %276, %277 : i32
llvm.store %278, %260 : i32, !llvm.ptr
cf.br ^bb63
^bb65:
cf.br ^bb56
^bb56:
cf.br ^bb47
^bb47:
cf.br ^bb44
^bb44:
cf.br ^bb41
^bb41:
%279 = llvm.load %148 : !llvm.ptr -> i32
%280 = arith.constant 1 : i32
%281 = arith.addi %279, %280 : i32
llvm.store %281, %148 : i32, !llvm.ptr
cf.br ^bb36
^bb38:
%282 = arith.constant 1 : i32
%283 = arith.constant 1 : i32
%284 = arith.subi %29, %283 : i32
%285 = arith.extsi %284 : i32 to i64
%286 = llvm.getelementptr %39[%285] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %282, %286 : i32, !llvm.ptr
%287 = arith.constant 0 : i32
%288 = arith.constant 1 : i32
%289 = arith.subi %29, %288 : i32
%290 = arith.extsi %289 : i32 to i64
%291 = llvm.getelementptr %47[%290] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %287, %291 : i32, !llvm.ptr
%293 = arith.constant 0 : i32
%294 = arith.extsi %293 : i32 to i64
%295 = llvm.getelementptr %39[%294] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%292 = llvm.load %295 : !llvm.ptr -> i32
%296 = llvm.mlir.constant(1 : i64) : i64
%297 = llvm.alloca %296 x i32 : (i64) -> !llvm.ptr
llvm.store %292, %297 : i32, !llvm.ptr
%299 = arith.constant 0 : i32
%300 = arith.extsi %299 : i32 to i64
%301 = llvm.getelementptr %47[%300] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%298 = llvm.load %301 : !llvm.ptr -> i32
%302 = llvm.mlir.constant(1 : i64) : i64
%303 = llvm.alloca %302 x i32 : (i64) -> !llvm.ptr
llvm.store %298, %303 : i32, !llvm.ptr
%304 = llvm.load %297 : !llvm.ptr -> i32
%305 = arith.constant 2 : i32
%306 = arith.cmpi eq, %304, %305 : i32
cf.cond_br %306, ^bb66, ^bb67
^bb66:
%308 = arith.constant 0 : i32
%309 = arith.extsi %308 : i32 to i64
%307 = func.call @is_const_table(%54, %309, %arg0) : (!llvm.ptr, i64, i32) -> i32
%310 = arith.constant 0 : i32
%311 = arith.cmpi sge, %307, %310 : i32
cf.cond_br %311, ^bb69, ^bb70
^bb69:
%312 = arith.constant 1 : i32
%313 = arith.constant 0 : i32
%314 = arith.extsi %313 : i32 to i64
%315 = llvm.getelementptr %39[%314] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %312, %315 : i32, !llvm.ptr
%316 = arith.constant 0 : i32
%317 = arith.extsi %316 : i32 to i64
%318 = llvm.getelementptr %47[%317] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %307, %318 : i32, !llvm.ptr
llvm.store %307, %303 : i32, !llvm.ptr
%319 = arith.constant 1 : i32
llvm.store %319, %297 : i32, !llvm.ptr
cf.br ^bb71
^bb70:
cf.br ^bb71
^bb71:
cf.br ^bb68
^bb67:
cf.br ^bb68
^bb68:
%320 = llvm.load %297 : !llvm.ptr -> i32
%321 = arith.constant 1 : i32
%322 = arith.cmpi eq, %320, %321 : i32
cf.cond_br %322, ^bb72, ^bb73
^bb72:
%323 = llvm.load %303 : !llvm.ptr -> i32
llvm.store %323, %137 : i32, !llvm.ptr
%324 = arith.constant 1 : i32
llvm.store %324, %140 : i32, !llvm.ptr
cf.br ^bb74
^bb73:
%325 = arith.constant 0 : i32
%326 = llvm.mlir.constant(1 : i64) : i64
%327 = llvm.alloca %326 x i32 : (i64) -> !llvm.ptr
llvm.store %325, %327 : i32, !llvm.ptr
cf.br ^bb75
^bb75:
%328 = llvm.load %327 : !llvm.ptr -> i32
%329 = arith.cmpi slt, %328, %29 : i32
cf.cond_br %329, ^bb76, ^bb77
^bb76:
%331 = llvm.load %327 : !llvm.ptr -> i32
%332 = arith.extsi %331 : i32 to i64
%333 = llvm.getelementptr %39[%332] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%330 = llvm.load %333 : !llvm.ptr -> i32
%334 = llvm.load %327 : !llvm.ptr -> i32
%335 = arith.extsi %334 : i32 to i64
%336 = llvm.getelementptr %35[%335] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %330, %336 : i32, !llvm.ptr
%338 = llvm.load %327 : !llvm.ptr -> i32
%339 = arith.extsi %338 : i32 to i64
%340 = llvm.getelementptr %47[%339] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%337 = llvm.load %340 : !llvm.ptr -> i32
%341 = llvm.load %327 : !llvm.ptr -> i32
%342 = arith.extsi %341 : i32 to i64
%343 = llvm.getelementptr %43[%342] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %337, %343 : i32, !llvm.ptr
%344 = llvm.load %327 : !llvm.ptr -> i32
%345 = arith.constant 1 : i32
%346 = arith.addi %344, %345 : i32
llvm.store %346, %327 : i32, !llvm.ptr
cf.br ^bb75
^bb77:
%347 = arith.constant 0 : i32
%348 = arith.extsi %347 : i32 to i64
%349 = llvm.mlir.constant(1 : i64) : i64
%350 = llvm.alloca %349 x i64 : (i64) -> !llvm.ptr
llvm.store %348, %350 : i64, !llvm.ptr
cf.br ^bb78
^bb78:
%351 = llvm.load %350 : !llvm.ptr -> i64
%352 = arith.cmpi slt, %351, %34 : i64
cf.cond_br %352, ^bb79, ^bb80
^bb79:
%354 = llvm.load %350 : !llvm.ptr -> i64
%355 = llvm.getelementptr %54[%354] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%353 = llvm.load %355 : !llvm.ptr -> i32
%356 = llvm.load %350 : !llvm.ptr -> i64
%357 = llvm.getelementptr %51[%356] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %353, %357 : i32, !llvm.ptr
%358 = llvm.load %350 : !llvm.ptr -> i64
%359 = arith.constant 1 : i32
%361 = arith.extsi %359 : i32 to i64
%360 = arith.addi %358, %361 : i64
llvm.store %360, %350 : i64, !llvm.ptr
cf.br ^bb78
^bb80:
cf.br ^bb74
^bb74:
%362 = llvm.load %134 : !llvm.ptr -> i32
%363 = arith.constant 1 : i32
%364 = arith.addi %362, %363 : i32
llvm.store %364, %134 : i32, !llvm.ptr
cf.br ^bb30
^bb32:
func.call @free(%54) : (!llvm.ptr) -> ()
func.call @free(%51) : (!llvm.ptr) -> ()
func.call @free(%47) : (!llvm.ptr) -> ()
func.call @free(%43) : (!llvm.ptr) -> ()
func.call @free(%39) : (!llvm.ptr) -> ()
func.call @free(%35) : (!llvm.ptr) -> ()
%371 = llvm.load %137 : !llvm.ptr -> i32
func.return %371 : i32
}
func.func @main() -> i32 {
%372 = arith.constant 0 : i32
%373 = arith.extsi %372 : i32 to i64
%374 = llvm.mlir.constant(1 : i64) : i64
%375 = llvm.alloca %374 x i64 : (i64) -> !llvm.ptr
llvm.store %373, %375 : i64, !llvm.ptr
%376 = arith.constant 1 : i32
%377 = llvm.mlir.constant(1 : i64) : i64
%378 = llvm.alloca %377 x i32 : (i64) -> !llvm.ptr
llvm.store %376, %378 : i32, !llvm.ptr
cf.br ^bb81
^bb81:
%379 = llvm.load %378 : !llvm.ptr -> i32
%380 = arith.constant 1000 : i32
%381 = arith.cmpi sle, %379, %380 : i32
cf.cond_br %381, ^bb82, ^bb83
^bb82:
%383 = llvm.load %378 : !llvm.ptr -> i32
%382 = func.call @compute_M(%383) : (i32) -> i32
%384 = arith.extsi %382 : i32 to i64
%385 = llvm.load %375 : !llvm.ptr -> i64
%386 = arith.muli %384, %384 : i64
%387 = arith.muli %386, %384 : i64
%388 = arith.addi %385, %387 : i64
llvm.store %388, %375 : i64, !llvm.ptr
%389 = llvm.load %378 : !llvm.ptr -> i32
%390 = arith.constant 1 : i32
%391 = arith.addi %389, %390 : i32
llvm.store %391, %378 : i32, !llvm.ptr
cf.br ^bb81
^bb83:
%392 = llvm.mlir.addressof @str_0 : !llvm.ptr
%393 = llvm.load %375 : !llvm.ptr -> i64
%394 = llvm.call @printf(%392, %393) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%395 = arith.constant 0 : i32
func.return %395 : i32
}
}