Problem 464
Möbius intervals C(2·10^7): linear sieve μ + Fenwick on prefix walks.
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 464
# Möbius intervals C(2·10^7): linear sieve μ + Fenwick on prefix walks.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function fenwick_sum(bit: ptr<i32>, idx0: i64) -> i64 {
let mut idx: i64 = idx0
let mut s: i64 = 0
while idx > 0 {
s = s + (bit[idx] as i64)
idx = idx & (idx - 1)
}
return s
}
function fenwick_add(bit: ptr<i32>, size: i64, idx0: i64, delta: i32) -> void {
let mut idx: i64 = idx0
while idx <= size {
bit[idx] = bit[idx] + delta
idx = idx + (idx & (0 - idx))
}
}
function compute_C(n: i64) -> i64 {
if n <= 0 { return 0 }
let mu: ptr<i8> = calloc(n + 1, 1)
let lp: ptr<i32> = calloc(n + 1, 4)
let primes: ptr<i32> = calloc(n / 5 + 16, 4)
if mu == null || lp == null || primes == null { return 0 }
mu[1] = 1
let mut pc: i64 = 0
let mut i: i64 = 2
while i <= n {
if lp[i] == 0 {
lp[i] = i as i32
primes[pc] = i as i32
pc = pc + 1
mu[i] = 0 - 1
}
let li: i64 = lp[i] as i64
let mi: i64 = mu[i] as i64
let mut j: i64 = 0
while j < pc {
let p: i64 = primes[j] as i64
if p > li { break }
let ip: i64 = i * p
if ip > n { break }
lp[ip] = p as i32
if p == li {
mu[ip] = 0
break
}
mu[ip] = (0 - mi) as i8
j = j + 1
}
i = i + 1
}
let mut prefix_a: i64 = 0
let mut prefix_b: i64 = 0
let mut min_a: i64 = 0
let mut max_a: i64 = 0
let mut min_b: i64 = 0
let mut max_b: i64 = 0
let mut pos: i64 = 1
while pos <= n {
let m: i64 = mu[pos] as i64
match m {
1 => {
prefix_a = prefix_a + 99
prefix_b = prefix_b - 100
}
-1 => {
prefix_a = prefix_a - 100
prefix_b = prefix_b + 99
}
_ => { }
}
if prefix_a < min_a { min_a = prefix_a }
elif prefix_a > max_a { max_a = prefix_a }
if prefix_b < min_b { min_b = prefix_b }
elif prefix_b > max_b { max_b = prefix_b }
pos = pos + 1
}
let range_a: i64 = max_a - min_a + 1
let range_b: i64 = max_b - min_b + 1
let limit_a: i64 = range_a + 1
let limit_b: i64 = range_b + 1
let bit_a: ptr<i32> = calloc(limit_a + 2, 4)
let bit_b: ptr<i32> = calloc(limit_b + 2, 4)
if bit_a == null || bit_b == null { return 0 }
let offset_a: i64 = 1 - min_a
let offset_b: i64 = 1 - min_b
fenwick_add(bit_a, limit_a, offset_a, 1)
fenwick_add(bit_b, limit_b, offset_b, 1)
prefix_a = 0
prefix_b = 0
let mut bad_a: i64 = 0
let mut bad_b: i64 = 0
pos = 1
while pos <= n {
let m: i64 = mu[pos] as i64
match m {
1 => {
prefix_a = prefix_a + 99
prefix_b = prefix_b - 100
}
-1 => {
prefix_a = prefix_a - 100
prefix_b = prefix_b + 99
}
_ => { }
}
let idx_a: i64 = prefix_a + offset_a
bad_a = bad_a + fenwick_sum(bit_a, idx_a - 1)
fenwick_add(bit_a, limit_a, idx_a, 1)
let idx_b: i64 = prefix_b + offset_b
bad_b = bad_b + fenwick_sum(bit_b, idx_b - 1)
fenwick_add(bit_b, limit_b, idx_b, 1)
pos = pos + 1
}
free(bit_a)
free(bit_b)
free(mu)
free(lp)
free(primes)
return n * (n + 1) / 2 - bad_a - bad_b
}
function main() -> i32 {
if compute_C(10) != 13 {
printf("%lld\n", compute_C(10))
return 1
}
if compute_C(500) != 16676 {
printf("%lld\n", compute_C(500))
return 1
}
printf("%lld\n", compute_C(20000000))
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 fenwick_sum_ptr_i32_i64(int32_t* bit, int64_t idx0);
void fenwick_add_ptr_i32_i64_i64_i32(int32_t* bit, int64_t size, int64_t idx0, int32_t delta);
int64_t compute_C_i64(int64_t n);
int32_t main(void);
int64_t fenwick_sum_ptr_i32_i64(int32_t* bit, int64_t idx0) {
int64_t idx = idx0;
int64_t s = 0;
while (idx > 0) {
s = (s + ((int64_t)(bit[idx])));
idx = (idx & (idx - 1));
}
return s;
}
void fenwick_add_ptr_i32_i64_i64_i32(int32_t* bit, int64_t size, int64_t idx0, int32_t delta) {
int64_t idx = idx0;
while (idx <= size) {
bit[idx] = (bit[idx] + delta);
idx = (idx + (idx & (0 - idx)));
}
}
int64_t compute_C_i64(int64_t n) {
if (n <= 0) {
return 0;
}
int8_t* mu = (int8_t*)(calloc((n + 1), 1));
int32_t* lp = (int32_t*)(calloc((n + 1), 4));
int32_t* primes = (int32_t*)(calloc((FLOW_CHECKED_DIV((n), (5)) + 16), 4));
if (((mu == NULL || lp == NULL) || primes == NULL)) {
return 0;
}
mu[1] = 1;
int64_t pc = 0;
int64_t i = 2;
while (i <= n) {
if (lp[i] == 0) {
lp[i] = ((int32_t)(i));
primes[pc] = ((int32_t)(i));
pc = (pc + 1);
mu[i] = (0 - 1);
}
int64_t li = ((int64_t)(lp[i]));
int64_t mi = ((int64_t)(mu[i]));
int64_t j = 0;
while (j < pc) {
int64_t p = ((int64_t)(primes[j]));
if (p > li) {
break;
}
int64_t ip = (i * p);
if (ip > n) {
break;
}
lp[ip] = ((int32_t)(p));
if (p == li) {
mu[ip] = 0;
break;
}
mu[ip] = ((int8_t)((0 - mi)));
j = (j + 1);
}
i = (i + 1);
}
int64_t prefix_a = 0;
int64_t prefix_b = 0;
int64_t min_a = 0;
int64_t max_a = 0;
int64_t min_b = 0;
int64_t max_b = 0;
int64_t pos = 1;
while (pos <= n) {
int64_t m = ((int64_t)(mu[pos]));
{ // match block
if ((m) == 1) {
prefix_a = (prefix_a + 99);
prefix_b = (prefix_b - 100);
} else if ((m) == (-1)) {
prefix_a = (prefix_a - 100);
prefix_b = (prefix_b + 99);
} else { // exhaustive
}
} // end match
if (prefix_a < min_a) {
min_a = prefix_a;
} else if (prefix_a > max_a) {
max_a = prefix_a;
}
if (prefix_b < min_b) {
min_b = prefix_b;
} else if (prefix_b > max_b) {
max_b = prefix_b;
}
pos = (pos + 1);
}
int64_t range_a = ((max_a - min_a) + 1);
int64_t range_b = ((max_b - min_b) + 1);
int64_t limit_a = (range_a + 1);
int64_t limit_b = (range_b + 1);
int32_t* bit_a = (int32_t*)(calloc((limit_a + 2), 4));
int32_t* bit_b = (int32_t*)(calloc((limit_b + 2), 4));
if ((bit_a == NULL || bit_b == NULL)) {
return 0;
}
int64_t offset_a = (1 - min_a);
int64_t offset_b = (1 - min_b);
fenwick_add_ptr_i32_i64_i64_i32(bit_a, limit_a, offset_a, 1);
fenwick_add_ptr_i32_i64_i64_i32(bit_b, limit_b, offset_b, 1);
prefix_a = 0;
prefix_b = 0;
int64_t bad_a = 0;
int64_t bad_b = 0;
pos = 1;
while (pos <= n) {
int64_t m = ((int64_t)(mu[pos]));
{ // match block
if ((m) == 1) {
prefix_a = (prefix_a + 99);
prefix_b = (prefix_b - 100);
} else if ((m) == (-1)) {
prefix_a = (prefix_a - 100);
prefix_b = (prefix_b + 99);
} else { // exhaustive
}
} // end match
int64_t idx_a = (prefix_a + offset_a);
bad_a = (bad_a + fenwick_sum_ptr_i32_i64(bit_a, (idx_a - 1)));
fenwick_add_ptr_i32_i64_i64_i32(bit_a, limit_a, idx_a, 1);
int64_t idx_b = (prefix_b + offset_b);
bad_b = (bad_b + fenwick_sum_ptr_i32_i64(bit_b, (idx_b - 1)));
fenwick_add_ptr_i32_i64_i64_i32(bit_b, limit_b, idx_b, 1);
pos = (pos + 1);
}
free(bit_a);
free(bit_b);
free(mu);
free(lp);
free(primes);
return ((FLOW_CHECKED_DIV(((n * (n + 1))), (2)) - bad_a) - bad_b);
}
int32_t main(void) {
if (compute_C_i64(10) != 13) {
printf("%lld\n", compute_C_i64(10));
return 1;
}
if (compute_C_i64(500) != 16676) {
printf("%lld\n", compute_C_i64(500));
return 1;
}
printf("%lld\n", compute_C_i64(20000000));
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 @fenwick_sum(%arg0: !llvm.ptr, %arg1: i64) -> i64 {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %1 : i64, !llvm.ptr
%2 = arith.constant 0 : i32
%3 = arith.extsi %2 : i32 to i64
%4 = llvm.mlir.constant(1 : i64) : i64
%5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
llvm.store %3, %5 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%6 = llvm.load %1 : !llvm.ptr -> i64
%7 = arith.constant 0 : i32
%9 = arith.extsi %7 : i32 to i64
%8 = arith.cmpi sgt, %6, %9 : i64
cf.cond_br %8, ^bb1, ^bb2
^bb1:
%10 = llvm.load %5 : !llvm.ptr -> i64
%12 = llvm.load %1 : !llvm.ptr -> i64
%13 = llvm.getelementptr %arg0[%12] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%11 = llvm.load %13 : !llvm.ptr -> i32
%14 = arith.extsi %11 : i32 to i64
%15 = arith.addi %10, %14 : i64
llvm.store %15, %5 : i64, !llvm.ptr
%16 = llvm.load %1 : !llvm.ptr -> i64
%17 = llvm.load %1 : !llvm.ptr -> i64
%18 = arith.constant 1 : i32
%20 = arith.extsi %18 : i32 to i64
%19 = arith.subi %17, %20 : i64
%21 = arith.andi %16, %19 : i64
llvm.store %21, %1 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%22 = llvm.load %5 : !llvm.ptr -> i64
func.return %22 : i64
}
func.func @fenwick_add(%arg0: !llvm.ptr, %arg1: i64, %arg2: i64, %arg3: i32) -> () {
%23 = llvm.mlir.constant(1 : i64) : i64
%24 = llvm.alloca %23 x i64 : (i64) -> !llvm.ptr
llvm.store %arg2, %24 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%25 = llvm.load %24 : !llvm.ptr -> i64
%26 = arith.cmpi sle, %25, %arg1 : i64
cf.cond_br %26, ^bb4, ^bb5
^bb4:
%28 = llvm.load %24 : !llvm.ptr -> i64
%29 = llvm.getelementptr %arg0[%28] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%27 = llvm.load %29 : !llvm.ptr -> i32
%30 = arith.addi %27, %arg3 : i32
%31 = llvm.load %24 : !llvm.ptr -> i64
%32 = llvm.getelementptr %arg0[%31] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %30, %32 : i32, !llvm.ptr
%33 = llvm.load %24 : !llvm.ptr -> i64
%34 = llvm.load %24 : !llvm.ptr -> i64
%35 = arith.constant 0 : i32
%36 = llvm.load %24 : !llvm.ptr -> i64
%38 = arith.extsi %35 : i32 to i64
%37 = arith.subi %38, %36 : i64
%39 = arith.andi %34, %37 : i64
%40 = arith.addi %33, %39 : i64
llvm.store %40, %24 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
func.return
}
func.func @compute_C(%arg0: i64) -> i64 {
%41 = arith.constant 0 : i32
%43 = arith.extsi %41 : i32 to i64
%42 = arith.cmpi sle, %arg0, %43 : i64
cf.cond_br %42, ^bb6, ^bb7
^bb6:
%44 = arith.constant 0 : i32
%45 = arith.extsi %44 : i32 to i64
func.return %45 : i64
^bb7:
cf.br ^bb8
^bb8:
%47 = arith.constant 1 : i32
%49 = arith.extsi %47 : i32 to i64
%48 = arith.addi %arg0, %49 : i64
%50 = arith.constant 1 : i32
%51 = arith.extsi %50 : i32 to i64
%46 = func.call @calloc(%48, %51) : (i64, i64) -> !llvm.ptr
%53 = arith.constant 1 : i32
%55 = arith.extsi %53 : i32 to i64
%54 = arith.addi %arg0, %55 : i64
%56 = arith.constant 4 : i32
%57 = arith.extsi %56 : i32 to i64
%52 = func.call @calloc(%54, %57) : (i64, i64) -> !llvm.ptr
%59 = arith.constant 5 : i32
%61 = arith.extsi %59 : i32 to i64
%60 = arith.divsi %arg0, %61 : i64
%62 = arith.constant 16 : i32
%64 = arith.extsi %62 : i32 to i64
%63 = arith.addi %60, %64 : i64
%65 = arith.constant 4 : i32
%66 = arith.extsi %65 : i32 to i64
%58 = func.call @calloc(%63, %66) : (i64, i64) -> !llvm.ptr
%67 = llvm.mlir.zero : !llvm.ptr
%68 = llvm.icmp "eq" %46, %67 : !llvm.ptr
%69 = scf.if %68 -> (i1) {
%70 = arith.constant true
scf.yield %70 : i1
} else {
%71 = llvm.mlir.zero : !llvm.ptr
%72 = llvm.icmp "eq" %52, %71 : !llvm.ptr
scf.yield %72 : i1
}
%73 = scf.if %69 -> (i1) {
%74 = arith.constant true
scf.yield %74 : i1
} else {
%75 = llvm.mlir.zero : !llvm.ptr
%76 = llvm.icmp "eq" %58, %75 : !llvm.ptr
scf.yield %76 : i1
}
cf.cond_br %73, ^bb9, ^bb10
^bb9:
%77 = arith.constant 0 : i32
%78 = arith.extsi %77 : i32 to i64
func.return %78 : i64
^bb10:
cf.br ^bb11
^bb11:
%79 = arith.constant 1 : i32
%80 = arith.constant 1 : i32
%81 = arith.trunci %79 : i32 to i8
%82 = arith.extsi %80 : i32 to i64
%83 = llvm.getelementptr %46[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %81, %83 : i8, !llvm.ptr
%84 = arith.constant 0 : i32
%85 = arith.extsi %84 : i32 to i64
%86 = llvm.mlir.constant(1 : i64) : i64
%87 = llvm.alloca %86 x i64 : (i64) -> !llvm.ptr
llvm.store %85, %87 : i64, !llvm.ptr
%88 = arith.constant 2 : i32
%89 = arith.extsi %88 : i32 to i64
%90 = llvm.mlir.constant(1 : i64) : i64
%91 = llvm.alloca %90 x i64 : (i64) -> !llvm.ptr
llvm.store %89, %91 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%92 = llvm.load %91 : !llvm.ptr -> i64
%93 = arith.cmpi sle, %92, %arg0 : i64
cf.cond_br %93, ^bb13, ^bb14
^bb13:
%95 = llvm.load %91 : !llvm.ptr -> i64
%96 = llvm.getelementptr %52[%95] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%94 = llvm.load %96 : !llvm.ptr -> i32
%97 = arith.constant 0 : i32
%98 = arith.cmpi eq, %94, %97 : i32
cf.cond_br %98, ^bb15, ^bb16
^bb15:
%99 = llvm.load %91 : !llvm.ptr -> i64
%100 = arith.trunci %99 : i64 to i32
%101 = llvm.load %91 : !llvm.ptr -> i64
%102 = llvm.getelementptr %52[%101] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %100, %102 : i32, !llvm.ptr
%103 = llvm.load %91 : !llvm.ptr -> i64
%104 = arith.trunci %103 : i64 to i32
%105 = llvm.load %87 : !llvm.ptr -> i64
%106 = llvm.getelementptr %58[%105] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %104, %106 : i32, !llvm.ptr
%107 = llvm.load %87 : !llvm.ptr -> i64
%108 = arith.constant 1 : i32
%110 = arith.extsi %108 : i32 to i64
%109 = arith.addi %107, %110 : i64
llvm.store %109, %87 : i64, !llvm.ptr
%111 = arith.constant 0 : i32
%112 = arith.constant 1 : i32
%113 = arith.subi %111, %112 : i32
%114 = llvm.load %91 : !llvm.ptr -> i64
%115 = arith.trunci %113 : i32 to i8
%116 = llvm.getelementptr %46[%114] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %115, %116 : i8, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%118 = llvm.load %91 : !llvm.ptr -> i64
%119 = llvm.getelementptr %52[%118] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%117 = llvm.load %119 : !llvm.ptr -> i32
%120 = arith.extsi %117 : i32 to i64
%122 = llvm.load %91 : !llvm.ptr -> i64
%123 = llvm.getelementptr %46[%122] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%121 = llvm.load %123 : !llvm.ptr -> i8
%124 = arith.extsi %121 : i8 to i64
%125 = arith.constant 0 : i32
%126 = arith.extsi %125 : i32 to i64
%127 = llvm.mlir.constant(1 : i64) : i64
%128 = llvm.alloca %127 x i64 : (i64) -> !llvm.ptr
llvm.store %126, %128 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%129 = llvm.load %128 : !llvm.ptr -> i64
%130 = llvm.load %87 : !llvm.ptr -> i64
%131 = arith.cmpi slt, %129, %130 : i64
cf.cond_br %131, ^bb19, ^bb20
^bb19:
%133 = llvm.load %128 : !llvm.ptr -> i64
%134 = llvm.getelementptr %58[%133] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%132 = llvm.load %134 : !llvm.ptr -> i32
%135 = arith.extsi %132 : i32 to i64
%136 = arith.cmpi sgt, %135, %120 : i64
cf.cond_br %136, ^bb21, ^bb22
^bb21:
cf.br ^bb20
^bb22:
cf.br ^bb23
^bb23:
%137 = llvm.load %91 : !llvm.ptr -> i64
%138 = arith.muli %137, %135 : i64
%139 = arith.cmpi sgt, %138, %arg0 : i64
cf.cond_br %139, ^bb24, ^bb25
^bb24:
cf.br ^bb20
^bb25:
cf.br ^bb26
^bb26:
%140 = arith.trunci %135 : i64 to i32
%141 = llvm.getelementptr %52[%138] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %140, %141 : i32, !llvm.ptr
%142 = arith.cmpi eq, %135, %120 : i64
cf.cond_br %142, ^bb27, ^bb28
^bb27:
%143 = arith.constant 0 : i32
%144 = arith.trunci %143 : i32 to i8
%145 = llvm.getelementptr %46[%138] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %144, %145 : i8, !llvm.ptr
cf.br ^bb20
^bb28:
cf.br ^bb29
^bb29:
%146 = arith.constant 0 : i32
%148 = arith.extsi %146 : i32 to i64
%147 = arith.subi %148, %124 : i64
%149 = arith.trunci %147 : i64 to i8
%150 = llvm.getelementptr %46[%138] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %149, %150 : i8, !llvm.ptr
%151 = llvm.load %128 : !llvm.ptr -> i64
%152 = arith.constant 1 : i32
%154 = arith.extsi %152 : i32 to i64
%153 = arith.addi %151, %154 : i64
llvm.store %153, %128 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%155 = llvm.load %91 : !llvm.ptr -> i64
%156 = arith.constant 1 : i32
%158 = arith.extsi %156 : i32 to i64
%157 = arith.addi %155, %158 : i64
llvm.store %157, %91 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%159 = arith.constant 0 : i32
%160 = arith.extsi %159 : i32 to i64
%161 = llvm.mlir.constant(1 : i64) : i64
%162 = llvm.alloca %161 x i64 : (i64) -> !llvm.ptr
llvm.store %160, %162 : i64, !llvm.ptr
%163 = arith.constant 0 : i32
%164 = arith.extsi %163 : i32 to i64
%165 = llvm.mlir.constant(1 : i64) : i64
%166 = llvm.alloca %165 x i64 : (i64) -> !llvm.ptr
llvm.store %164, %166 : i64, !llvm.ptr
%167 = arith.constant 0 : i32
%168 = arith.extsi %167 : i32 to i64
%169 = llvm.mlir.constant(1 : i64) : i64
%170 = llvm.alloca %169 x i64 : (i64) -> !llvm.ptr
llvm.store %168, %170 : i64, !llvm.ptr
%171 = arith.constant 0 : i32
%172 = arith.extsi %171 : i32 to i64
%173 = llvm.mlir.constant(1 : i64) : i64
%174 = llvm.alloca %173 x i64 : (i64) -> !llvm.ptr
llvm.store %172, %174 : i64, !llvm.ptr
%175 = arith.constant 0 : i32
%176 = arith.extsi %175 : i32 to i64
%177 = llvm.mlir.constant(1 : i64) : i64
%178 = llvm.alloca %177 x i64 : (i64) -> !llvm.ptr
llvm.store %176, %178 : i64, !llvm.ptr
%179 = arith.constant 0 : i32
%180 = arith.extsi %179 : i32 to i64
%181 = llvm.mlir.constant(1 : i64) : i64
%182 = llvm.alloca %181 x i64 : (i64) -> !llvm.ptr
llvm.store %180, %182 : i64, !llvm.ptr
%183 = arith.constant 1 : 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 ^bb30
^bb30:
%187 = llvm.load %186 : !llvm.ptr -> i64
%188 = arith.cmpi sle, %187, %arg0 : i64
cf.cond_br %188, ^bb31, ^bb32
^bb31:
%190 = llvm.load %186 : !llvm.ptr -> i64
%191 = llvm.getelementptr %46[%190] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%189 = llvm.load %191 : !llvm.ptr -> i8
%192 = arith.extsi %189 : i8 to i64
%193 = arith.constant 1 : i32
%194 = arith.cmpi eq, %192, %193 : i64
cf.cond_br %194, ^bb33, ^bb34
^bb33:
%195 = llvm.load %162 : !llvm.ptr -> i64
%196 = arith.constant 99 : i32
%198 = arith.extsi %196 : i32 to i64
%197 = arith.addi %195, %198 : i64
llvm.store %197, %162 : i64, !llvm.ptr
%199 = llvm.load %166 : !llvm.ptr -> i64
%200 = arith.constant 100 : i32
%202 = arith.extsi %200 : i32 to i64
%201 = arith.subi %199, %202 : i64
llvm.store %201, %166 : i64, !llvm.ptr
cf.br ^bb39
^bb34:
%203 = arith.constant 1 : i32
%205 = arith.constant 0 : i32
%204 = arith.subi %205, %203 : i32
%206 = arith.cmpi eq, %192, %204 : i64
cf.cond_br %206, ^bb35, ^bb36
^bb35:
%207 = llvm.load %162 : !llvm.ptr -> i64
%208 = arith.constant 100 : i32
%210 = arith.extsi %208 : i32 to i64
%209 = arith.subi %207, %210 : i64
llvm.store %209, %162 : i64, !llvm.ptr
%211 = llvm.load %166 : !llvm.ptr -> i64
%212 = arith.constant 99 : i32
%214 = arith.extsi %212 : i32 to i64
%213 = arith.addi %211, %214 : i64
llvm.store %213, %166 : i64, !llvm.ptr
cf.br ^bb39
^bb36:
%215 = arith.constant 1 : i1
cf.cond_br %215, ^bb37, ^bb38
^bb37:
cf.br ^bb39
^bb38:
cf.br ^bb39
^bb39:
%216 = llvm.load %162 : !llvm.ptr -> i64
%217 = llvm.load %170 : !llvm.ptr -> i64
%218 = arith.cmpi slt, %216, %217 : i64
cf.cond_br %218, ^bb40, ^bb41
^bb40:
%219 = llvm.load %162 : !llvm.ptr -> i64
llvm.store %219, %170 : i64, !llvm.ptr
cf.br ^bb42
^bb41:
%220 = llvm.load %162 : !llvm.ptr -> i64
%221 = llvm.load %174 : !llvm.ptr -> i64
%222 = arith.cmpi sgt, %220, %221 : i64
cf.cond_br %222, ^bb43, ^bb42
^bb43:
%223 = llvm.load %162 : !llvm.ptr -> i64
llvm.store %223, %174 : i64, !llvm.ptr
cf.br ^bb42
^bb42:
%224 = llvm.load %166 : !llvm.ptr -> i64
%225 = llvm.load %178 : !llvm.ptr -> i64
%226 = arith.cmpi slt, %224, %225 : i64
cf.cond_br %226, ^bb44, ^bb45
^bb44:
%227 = llvm.load %166 : !llvm.ptr -> i64
llvm.store %227, %178 : i64, !llvm.ptr
cf.br ^bb46
^bb45:
%228 = llvm.load %166 : !llvm.ptr -> i64
%229 = llvm.load %182 : !llvm.ptr -> i64
%230 = arith.cmpi sgt, %228, %229 : i64
cf.cond_br %230, ^bb47, ^bb46
^bb47:
%231 = llvm.load %166 : !llvm.ptr -> i64
llvm.store %231, %182 : i64, !llvm.ptr
cf.br ^bb46
^bb46:
%232 = llvm.load %186 : !llvm.ptr -> i64
%233 = arith.constant 1 : i32
%235 = arith.extsi %233 : i32 to i64
%234 = arith.addi %232, %235 : i64
llvm.store %234, %186 : i64, !llvm.ptr
cf.br ^bb30
^bb32:
%236 = llvm.load %174 : !llvm.ptr -> i64
%237 = llvm.load %170 : !llvm.ptr -> i64
%238 = arith.subi %236, %237 : i64
%239 = arith.constant 1 : i32
%241 = arith.extsi %239 : i32 to i64
%240 = arith.addi %238, %241 : i64
%242 = llvm.load %182 : !llvm.ptr -> i64
%243 = llvm.load %178 : !llvm.ptr -> i64
%244 = arith.subi %242, %243 : i64
%245 = arith.constant 1 : i32
%247 = arith.extsi %245 : i32 to i64
%246 = arith.addi %244, %247 : i64
%248 = arith.constant 1 : i32
%250 = arith.extsi %248 : i32 to i64
%249 = arith.addi %240, %250 : i64
%251 = arith.constant 1 : i32
%253 = arith.extsi %251 : i32 to i64
%252 = arith.addi %246, %253 : i64
%255 = arith.constant 2 : i32
%257 = arith.extsi %255 : i32 to i64
%256 = arith.addi %249, %257 : i64
%258 = arith.constant 4 : i32
%259 = arith.extsi %258 : i32 to i64
%254 = func.call @calloc(%256, %259) : (i64, i64) -> !llvm.ptr
%261 = arith.constant 2 : i32
%263 = arith.extsi %261 : i32 to i64
%262 = arith.addi %252, %263 : i64
%264 = arith.constant 4 : i32
%265 = arith.extsi %264 : i32 to i64
%260 = func.call @calloc(%262, %265) : (i64, i64) -> !llvm.ptr
%266 = llvm.mlir.zero : !llvm.ptr
%267 = llvm.icmp "eq" %254, %266 : !llvm.ptr
%268 = scf.if %267 -> (i1) {
%269 = arith.constant true
scf.yield %269 : i1
} else {
%270 = llvm.mlir.zero : !llvm.ptr
%271 = llvm.icmp "eq" %260, %270 : !llvm.ptr
scf.yield %271 : i1
}
cf.cond_br %268, ^bb48, ^bb49
^bb48:
%272 = arith.constant 0 : i32
%273 = arith.extsi %272 : i32 to i64
func.return %273 : i64
^bb49:
cf.br ^bb50
^bb50:
%274 = arith.constant 1 : i32
%275 = llvm.load %170 : !llvm.ptr -> i64
%277 = arith.extsi %274 : i32 to i64
%276 = arith.subi %277, %275 : i64
%278 = arith.constant 1 : i32
%279 = llvm.load %178 : !llvm.ptr -> i64
%281 = arith.extsi %278 : i32 to i64
%280 = arith.subi %281, %279 : i64
%283 = arith.constant 1 : i32
func.call @fenwick_add(%254, %249, %276, %283) : (!llvm.ptr, i64, i64, i32) -> ()
%285 = arith.constant 1 : i32
func.call @fenwick_add(%260, %252, %280, %285) : (!llvm.ptr, i64, i64, i32) -> ()
%286 = arith.constant 0 : i32
%287 = arith.extsi %286 : i32 to i64
llvm.store %287, %162 : i64, !llvm.ptr
%288 = arith.constant 0 : i32
%289 = arith.extsi %288 : i32 to i64
llvm.store %289, %166 : i64, !llvm.ptr
%290 = arith.constant 0 : i32
%291 = arith.extsi %290 : i32 to i64
%292 = llvm.mlir.constant(1 : i64) : i64
%293 = llvm.alloca %292 x i64 : (i64) -> !llvm.ptr
llvm.store %291, %293 : i64, !llvm.ptr
%294 = arith.constant 0 : i32
%295 = arith.extsi %294 : i32 to i64
%296 = llvm.mlir.constant(1 : i64) : i64
%297 = llvm.alloca %296 x i64 : (i64) -> !llvm.ptr
llvm.store %295, %297 : i64, !llvm.ptr
%298 = arith.constant 1 : i32
%299 = arith.extsi %298 : i32 to i64
llvm.store %299, %186 : i64, !llvm.ptr
cf.br ^bb51
^bb51:
%300 = llvm.load %186 : !llvm.ptr -> i64
%301 = arith.cmpi sle, %300, %arg0 : i64
cf.cond_br %301, ^bb52, ^bb53
^bb52:
%303 = llvm.load %186 : !llvm.ptr -> i64
%304 = llvm.getelementptr %46[%303] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%302 = llvm.load %304 : !llvm.ptr -> i8
%305 = arith.extsi %302 : i8 to i64
%306 = arith.constant 1 : i32
%307 = arith.cmpi eq, %305, %306 : i64
cf.cond_br %307, ^bb54, ^bb55
^bb54:
%308 = llvm.load %162 : !llvm.ptr -> i64
%309 = arith.constant 99 : i32
%311 = arith.extsi %309 : i32 to i64
%310 = arith.addi %308, %311 : i64
llvm.store %310, %162 : i64, !llvm.ptr
%312 = llvm.load %166 : !llvm.ptr -> i64
%313 = arith.constant 100 : i32
%315 = arith.extsi %313 : i32 to i64
%314 = arith.subi %312, %315 : i64
llvm.store %314, %166 : i64, !llvm.ptr
cf.br ^bb60
^bb55:
%316 = arith.constant 1 : i32
%318 = arith.constant 0 : i32
%317 = arith.subi %318, %316 : i32
%319 = arith.cmpi eq, %305, %317 : i64
cf.cond_br %319, ^bb56, ^bb57
^bb56:
%320 = llvm.load %162 : !llvm.ptr -> i64
%321 = arith.constant 100 : i32
%323 = arith.extsi %321 : i32 to i64
%322 = arith.subi %320, %323 : i64
llvm.store %322, %162 : i64, !llvm.ptr
%324 = llvm.load %166 : !llvm.ptr -> i64
%325 = arith.constant 99 : i32
%327 = arith.extsi %325 : i32 to i64
%326 = arith.addi %324, %327 : i64
llvm.store %326, %166 : i64, !llvm.ptr
cf.br ^bb60
^bb57:
%328 = arith.constant 1 : i1
cf.cond_br %328, ^bb58, ^bb59
^bb58:
cf.br ^bb60
^bb59:
cf.br ^bb60
^bb60:
%329 = llvm.load %162 : !llvm.ptr -> i64
%330 = arith.addi %329, %276 : i64
%331 = llvm.load %293 : !llvm.ptr -> i64
%333 = arith.constant 1 : i32
%335 = arith.extsi %333 : i32 to i64
%334 = arith.subi %330, %335 : i64
%332 = func.call @fenwick_sum(%254, %334) : (!llvm.ptr, i64) -> i64
%336 = arith.addi %331, %332 : i64
llvm.store %336, %293 : i64, !llvm.ptr
%338 = arith.constant 1 : i32
func.call @fenwick_add(%254, %249, %330, %338) : (!llvm.ptr, i64, i64, i32) -> ()
%339 = llvm.load %166 : !llvm.ptr -> i64
%340 = arith.addi %339, %280 : i64
%341 = llvm.load %297 : !llvm.ptr -> i64
%343 = arith.constant 1 : i32
%345 = arith.extsi %343 : i32 to i64
%344 = arith.subi %340, %345 : i64
%342 = func.call @fenwick_sum(%260, %344) : (!llvm.ptr, i64) -> i64
%346 = arith.addi %341, %342 : i64
llvm.store %346, %297 : i64, !llvm.ptr
%348 = arith.constant 1 : i32
func.call @fenwick_add(%260, %252, %340, %348) : (!llvm.ptr, i64, i64, i32) -> ()
%349 = llvm.load %186 : !llvm.ptr -> i64
%350 = arith.constant 1 : i32
%352 = arith.extsi %350 : i32 to i64
%351 = arith.addi %349, %352 : i64
llvm.store %351, %186 : i64, !llvm.ptr
cf.br ^bb51
^bb53:
func.call @free(%254) : (!llvm.ptr) -> ()
func.call @free(%260) : (!llvm.ptr) -> ()
func.call @free(%46) : (!llvm.ptr) -> ()
func.call @free(%52) : (!llvm.ptr) -> ()
func.call @free(%58) : (!llvm.ptr) -> ()
%358 = arith.constant 1 : i32
%360 = arith.extsi %358 : i32 to i64
%359 = arith.addi %arg0, %360 : i64
%361 = arith.muli %arg0, %359 : i64
%362 = arith.constant 2 : i32
%364 = arith.extsi %362 : i32 to i64
%363 = arith.divsi %361, %364 : i64
%365 = llvm.load %293 : !llvm.ptr -> i64
%366 = arith.subi %363, %365 : i64
%367 = llvm.load %297 : !llvm.ptr -> i64
%368 = arith.subi %366, %367 : i64
func.return %368 : i64
}
func.func @main() -> i32 {
%370 = arith.constant 10 : i32
%371 = arith.extsi %370 : i32 to i64
%369 = func.call @compute_C(%371) : (i64) -> i64
%372 = arith.constant 13 : i32
%374 = arith.extsi %372 : i32 to i64
%373 = arith.cmpi ne, %369, %374 : i64
cf.cond_br %373, ^bb61, ^bb62
^bb61:
%375 = llvm.mlir.addressof @str_0 : !llvm.ptr
%377 = arith.constant 10 : i32
%378 = arith.extsi %377 : i32 to i64
%376 = func.call @compute_C(%378) : (i64) -> i64
%379 = llvm.call @printf(%375, %376) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%380 = arith.constant 1 : i32
func.return %380 : i32
^bb62:
cf.br ^bb63
^bb63:
%382 = arith.constant 500 : i32
%383 = arith.extsi %382 : i32 to i64
%381 = func.call @compute_C(%383) : (i64) -> i64
%384 = arith.constant 16676 : i32
%386 = arith.extsi %384 : i32 to i64
%385 = arith.cmpi ne, %381, %386 : i64
cf.cond_br %385, ^bb64, ^bb65
^bb64:
%387 = llvm.mlir.addressof @str_0 : !llvm.ptr
%389 = arith.constant 500 : i32
%390 = arith.extsi %389 : i32 to i64
%388 = func.call @compute_C(%390) : (i64) -> i64
%391 = llvm.call @printf(%387, %388) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%392 = arith.constant 1 : i32
func.return %392 : i32
^bb65:
cf.br ^bb66
^bb66:
%393 = llvm.mlir.addressof @str_0 : !llvm.ptr
%395 = arith.constant 20000000 : i32
%396 = arith.extsi %395 : i32 to i64
%394 = func.call @compute_C(%396) : (i64) -> i64
%397 = llvm.call @printf(%393, %394) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%398 = arith.constant 0 : i32
func.return %398 : i32
}
}