← All problems
Problem 962
count_triangles(10^6) = 7259046 Counts triangles (a,b,c) with a<=b<=c<a+b and a+b+c<=limit such that a^3*(a+b-c)*(a+b+c) / (b*(a+b)^2) is a perfect square. Pure Flow port of native/p962.c.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n^2)
Space complexity O(1)O(n^2)
Approach Flow solution Bottom-up DP
Verdict Optimal
Flow source
# Project Euler 962: Counting Triangles
# count_triangles(10^6) = 7259046
# Counts triangles (a,b,c) with a<=b<=c<a+b and a+b+c<=limit such that
# a^3*(a+b-c)*(a+b+c) / (b*(a+b)^2) is a perfect square.
# Pure Flow port of native/p962.c.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function malloc(n: i64) -> ptr<void>
function sqrt(x: f64) -> f64
function cbrt(x: f64) -> f64
}
function isqrt_i64(n: i64) -> i64 {
if n <= 0 { return 0 }
let mut r: i64 = sqrt(n as f64) as i64
while r > 0 && r * r > n {
r = r - 1
}
while (r + 1) * (r + 1) <= n {
r = r + 1
}
return r
}
function isqrt_ceil_i64(n: i64) -> i64 {
if n <= 0 { return 0 }
let r: i64 = isqrt_i64(n)
if r * r == n { return r }
return r + 1
}
function icbrt_floor(n: i64) -> i64 {
if n <= 0 { return 0 }
let mut x: i64 = cbrt(n as f64) as i64
while (x + 1) * (x + 1) * (x + 1) <= n {
x = x + 1
}
while x * x * x > n {
x = x - 1
}
return x
}
function igcd(a0: i64, b0: i64) -> i64 {
let mut a: i64 = a0
let mut b: i64 = b0
while b != 0 {
let t: i64 = a % b
a = b
b = t
}
return a
}
function count_with_parity(lo0: i64, hi: i64, parity: i64) -> i64 {
let mut lo: i64 = lo0
if hi < lo { return 0 }
if lo % 2 != parity { lo = lo + 1 }
if lo > hi { return 0 }
return (hi - lo) / 2 + 1
}
# spf sieve: spf[i] = smallest prime factor of i
function spf_sieve(limit: i64, spf: ptr<i64>) -> void {
let mut i: i64 = 0
while i <= limit {
spf[i] = i
i = i + 1
}
let sq: i64 = sqrt(limit as f64) as i64
i = 2
while i <= sq {
if spf[i] == i {
let mut j: i64 = i * i
while j <= limit {
if spf[j] == j {
spf[j] = i
}
j = j + i
}
}
i = i + 1
}
}
# compute_squarefree: sf[n] = squarefree kernel of n
function compute_squarefree(limit: i64, spf: ptr<i64>, sf: ptr<i64>) -> void {
sf[0] = 0
sf[1] = 1
let mut n: i64 = 2
while n <= limit {
let p: i64 = spf[n]
let m: i64 = n / p
if m % p == 0 {
sf[n] = sf[m / p]
} else {
sf[n] = p * sf[m]
}
n = n + 1
}
}
function count_triangles(limit: i64) -> i64 {
let k_limit: i64 = icbrt_floor(2 * limit * limit) + 2
let mut factor_limit: i64 = limit / 3 + 1
if k_limit + 1 > factor_limit {
factor_limit = k_limit + 1
}
let spf: ptr<i64> = malloc((factor_limit + 1) * 8)
spf_sieve(factor_limit, spf)
let squarefree: ptr<i64> = malloc((factor_limit + 1) * 8)
compute_squarefree(factor_limit, spf, squarefree)
let mut total: i64 = 0
let mut k: i64 = 2
while k <= k_limit {
let max_s: i64 = limit / k
if max_s == 0 { break }
let max_product: i64 = max_s * max_s
let mut v: i64 = (k + 1) / 2
while v < k {
if igcd(v, k) != 1 {
v = v + 1
continue
}
let u: i64 = k - v
let d: i64 = squarefree[u]
if v * d > max_product {
v = v + 1
continue
}
let r_limit: i64 = u * max_s / (u + 2 * v)
if r_limit <= 0 {
v = v + 1
continue
}
let common_dv: i64 = igcd(d, v)
let d_reduced: i64 = d / common_dv
let v_reduced: i64 = v / common_dv
let numerator: i64 = u + 2 * v
let mut r: i64 = 1
while r <= r_limit {
let common_rv: i64 = igcd(r, v_reduced)
let remaining_r: i64 = r / common_rv
let numerator_kernel: i64 = squarefree[remaining_r]
let common: i64 = igcd(d_reduced, numerator_kernel)
let base: i64 = (v_reduced / common_rv) * (d_reduced * numerator_kernel / (common * common))
if base > max_s {
r = r + 1
continue
}
let s_min: i64 = (numerator * r + u - 1) / u
let n_min: i64 = isqrt_ceil_i64((s_min + base - 1) / base)
let n_max: i64 = isqrt_i64(max_s / base)
if base % 2 == 1 {
total = total + count_with_parity(n_min, n_max, r % 2)
} else {
if r % 2 == 0 {
let cnt: i64 = n_max - n_min + 1
if cnt > 0 {
total = total + cnt
}
}
}
r = r + 1
}
v = v + 1
}
k = k + 1
}
free(spf)
free(squarefree)
return total
}
function main() -> i32 {
let result: i64 = count_triangles(1000000)
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; }
double cbrt(double x);
int64_t isqrt_i64_i64(int64_t n);
int64_t isqrt_ceil_i64_i64(int64_t n);
int64_t icbrt_floor_i64(int64_t n);
int64_t igcd_i64_i64(int64_t a0, int64_t b0);
int64_t count_with_parity_i64_i64_i64(int64_t lo0, int64_t hi, int64_t parity);
void spf_sieve_i64_ptr_i64(int64_t limit, int64_t* spf);
void compute_squarefree_i64_ptr_i64_ptr_i64(int64_t limit, int64_t* spf, int64_t* sf);
int64_t count_triangles_i64(int64_t limit);
int32_t main(void);
int64_t isqrt_i64_i64(int64_t n) {
if (n <= 0) {
return 0;
}
int64_t r = ((int64_t)(sqrt(((double)(n)))));
while ((r > 0 && (r * r) > n)) {
r = (r - 1);
}
while (((r + 1) * (r + 1)) <= n) {
r = (r + 1);
}
return r;
}
int64_t isqrt_ceil_i64_i64(int64_t n) {
if (n <= 0) {
return 0;
}
int64_t r = isqrt_i64_i64(n);
if ((r * r) == n) {
return r;
}
return (r + 1);
}
int64_t icbrt_floor_i64(int64_t n) {
if (n <= 0) {
return 0;
}
int64_t x = ((int64_t)(cbrt(((double)(n)))));
while ((((x + 1) * (x + 1)) * (x + 1)) <= n) {
x = (x + 1);
}
while (((x * x) * x) > n) {
x = (x - 1);
}
return x;
}
int64_t igcd_i64_i64(int64_t a0, int64_t b0) {
int64_t a = a0;
int64_t b = b0;
while (b != 0) {
int64_t t = FLOW_CHECKED_MOD((a), (b));
a = b;
b = t;
}
return a;
}
int64_t count_with_parity_i64_i64_i64(int64_t lo0, int64_t hi, int64_t parity) {
int64_t lo = lo0;
if (hi < lo) {
return 0;
}
if (FLOW_CHECKED_MOD((lo), (2)) != parity) {
lo = (lo + 1);
}
if (lo > hi) {
return 0;
}
return (FLOW_CHECKED_DIV(((hi - lo)), (2)) + 1);
}
void spf_sieve_i64_ptr_i64(int64_t limit, int64_t* spf) {
int64_t i = 0;
while (i <= limit) {
spf[i] = i;
i = (i + 1);
}
int64_t sq = ((int64_t)(sqrt(((double)(limit)))));
i = 2;
while (i <= sq) {
if (spf[i] == i) {
int64_t j = (i * i);
while (j <= limit) {
if (spf[j] == j) {
spf[j] = i;
}
j = (j + i);
}
}
i = (i + 1);
}
}
void compute_squarefree_i64_ptr_i64_ptr_i64(int64_t limit, int64_t* spf, int64_t* sf) {
sf[0] = 0;
sf[1] = 1;
int64_t n = 2;
while (n <= limit) {
int64_t p = spf[n];
int64_t m = FLOW_CHECKED_DIV((n), (p));
if (FLOW_CHECKED_MOD((m), (p)) == 0) {
sf[n] = sf[FLOW_CHECKED_DIV((m), (p))];
} else {
sf[n] = (p * sf[m]);
}
n = (n + 1);
}
}
int64_t count_triangles_i64(int64_t limit) {
int64_t k_limit = (icbrt_floor_i64(((2 * limit) * limit)) + 2);
int64_t factor_limit = (FLOW_CHECKED_DIV((limit), (3)) + 1);
if ((k_limit + 1) > factor_limit) {
factor_limit = (k_limit + 1);
}
int64_t* spf = (int64_t*)(malloc(((factor_limit + 1) * 8)));
spf_sieve_i64_ptr_i64(factor_limit, spf);
int64_t* squarefree = (int64_t*)(malloc(((factor_limit + 1) * 8)));
compute_squarefree_i64_ptr_i64_ptr_i64(factor_limit, spf, squarefree);
int64_t total = 0;
int64_t k = 2;
while (k <= k_limit) {
int64_t max_s = FLOW_CHECKED_DIV((limit), (k));
if (max_s == 0) {
break;
}
int64_t max_product = (max_s * max_s);
int64_t v = FLOW_CHECKED_DIV(((k + 1)), (2));
while (v < k) {
if (igcd_i64_i64(v, k) != 1) {
v = (v + 1);
continue;
}
int64_t u = (k - v);
int64_t d = squarefree[u];
if ((v * d) > max_product) {
v = (v + 1);
continue;
}
int64_t r_limit = FLOW_CHECKED_DIV(((u * max_s)), ((u + (2 * v))));
if (r_limit <= 0) {
v = (v + 1);
continue;
}
int64_t common_dv = igcd_i64_i64(d, v);
int64_t d_reduced = FLOW_CHECKED_DIV((d), (common_dv));
int64_t v_reduced = FLOW_CHECKED_DIV((v), (common_dv));
int64_t numerator = (u + (2 * v));
int64_t r = 1;
while (r <= r_limit) {
int64_t common_rv = igcd_i64_i64(r, v_reduced);
int64_t remaining_r = FLOW_CHECKED_DIV((r), (common_rv));
int64_t numerator_kernel = squarefree[remaining_r];
int64_t common = igcd_i64_i64(d_reduced, numerator_kernel);
int64_t base = (FLOW_CHECKED_DIV((v_reduced), (common_rv)) * FLOW_CHECKED_DIV(((d_reduced * numerator_kernel)), ((common * common))));
if (base > max_s) {
r = (r + 1);
continue;
}
int64_t s_min = FLOW_CHECKED_DIV(((((numerator * r) + u) - 1)), (u));
int64_t n_min = isqrt_ceil_i64_i64(FLOW_CHECKED_DIV((((s_min + base) - 1)), (base)));
int64_t n_max = isqrt_i64_i64(FLOW_CHECKED_DIV((max_s), (base)));
if (FLOW_CHECKED_MOD((base), (2)) == 1) {
total = (total + count_with_parity_i64_i64_i64(n_min, n_max, FLOW_CHECKED_MOD((r), (2))));
} else {
if (FLOW_CHECKED_MOD((r), (2)) == 0) {
int64_t cnt = ((n_max - n_min) + 1);
if (cnt > 0) {
total = (total + cnt);
}
}
}
r = (r + 1);
}
v = (v + 1);
}
k = (k + 1);
}
free(spf);
free(squarefree);
return total;
}
int32_t main(void) {
int64_t result = count_triangles_i64(1000000);
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
func.func private @sqrt(f64) -> f64
func.func private @cbrt(f64) -> f64
func.func @isqrt_i64(%arg0: i64) -> i64 {
%0 = arith.constant 0 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.cmpi sle, %arg0, %2 : i64
cf.cond_br %1, ^bb0, ^bb1
^bb0:
%3 = arith.constant 0 : i32
%4 = arith.extsi %3 : i32 to i64
func.return %4 : i64
^bb1:
cf.br ^bb2
^bb2:
%5 = arith.sitofp %arg0 : i64 to f64
%6 = math.sqrt %5 : f64
%7 = arith.fptosi %6 : f64 to i64
%8 = llvm.mlir.constant(1 : i64) : i64
%9 = llvm.alloca %8 x i64 : (i64) -> !llvm.ptr
llvm.store %7, %9 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%10 = llvm.load %9 : !llvm.ptr -> i64
%11 = arith.constant 0 : i32
%13 = arith.extsi %11 : i32 to i64
%12 = arith.cmpi sgt, %10, %13 : i64
%14 = scf.if %12 -> (i1) {
%15 = llvm.load %9 : !llvm.ptr -> i64
%16 = llvm.load %9 : !llvm.ptr -> i64
%17 = arith.muli %15, %16 : i64
%18 = arith.cmpi sgt, %17, %arg0 : i64
scf.yield %18 : i1
} else {
%19 = arith.constant false
scf.yield %19 : i1
}
cf.cond_br %14, ^bb4, ^bb5
^bb4:
%20 = llvm.load %9 : !llvm.ptr -> i64
%21 = arith.constant 1 : i32
%23 = arith.extsi %21 : i32 to i64
%22 = arith.subi %20, %23 : i64
llvm.store %22, %9 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
cf.br ^bb6
^bb6:
%24 = llvm.load %9 : !llvm.ptr -> i64
%25 = arith.constant 1 : i32
%27 = arith.extsi %25 : i32 to i64
%26 = arith.addi %24, %27 : i64
%28 = llvm.load %9 : !llvm.ptr -> i64
%29 = arith.constant 1 : i32
%31 = arith.extsi %29 : i32 to i64
%30 = arith.addi %28, %31 : i64
%32 = arith.muli %26, %30 : i64
%33 = arith.cmpi sle, %32, %arg0 : i64
cf.cond_br %33, ^bb7, ^bb8
^bb7:
%34 = llvm.load %9 : !llvm.ptr -> i64
%35 = arith.constant 1 : i32
%37 = arith.extsi %35 : i32 to i64
%36 = arith.addi %34, %37 : i64
llvm.store %36, %9 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%38 = llvm.load %9 : !llvm.ptr -> i64
func.return %38 : i64
}
func.func @isqrt_ceil_i64(%arg0: i64) -> i64 {
%39 = arith.constant 0 : i32
%41 = arith.extsi %39 : i32 to i64
%40 = arith.cmpi sle, %arg0, %41 : i64
cf.cond_br %40, ^bb9, ^bb10
^bb9:
%42 = arith.constant 0 : i32
%43 = arith.extsi %42 : i32 to i64
func.return %43 : i64
^bb10:
cf.br ^bb11
^bb11:
%44 = func.call @isqrt_i64(%arg0) : (i64) -> i64
%45 = arith.muli %44, %44 : i64
%46 = arith.cmpi eq, %45, %arg0 : i64
cf.cond_br %46, ^bb12, ^bb13
^bb12:
func.return %44 : i64
^bb13:
cf.br ^bb14
^bb14:
%47 = arith.constant 1 : i32
%49 = arith.extsi %47 : i32 to i64
%48 = arith.addi %44, %49 : i64
func.return %48 : i64
}
func.func @icbrt_floor(%arg0: i64) -> i64 {
%50 = arith.constant 0 : i32
%52 = arith.extsi %50 : i32 to i64
%51 = arith.cmpi sle, %arg0, %52 : i64
cf.cond_br %51, ^bb15, ^bb16
^bb15:
%53 = arith.constant 0 : i32
%54 = arith.extsi %53 : i32 to i64
func.return %54 : i64
^bb16:
cf.br ^bb17
^bb17:
%56 = arith.sitofp %arg0 : i64 to f64
%55 = func.call @cbrt(%56) : (f64) -> f64
%57 = arith.fptosi %55 : f64 to i64
%58 = llvm.mlir.constant(1 : i64) : i64
%59 = llvm.alloca %58 x i64 : (i64) -> !llvm.ptr
llvm.store %57, %59 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%60 = llvm.load %59 : !llvm.ptr -> i64
%61 = arith.constant 1 : i32
%63 = arith.extsi %61 : i32 to i64
%62 = arith.addi %60, %63 : i64
%64 = llvm.load %59 : !llvm.ptr -> i64
%65 = arith.constant 1 : i32
%67 = arith.extsi %65 : i32 to i64
%66 = arith.addi %64, %67 : i64
%68 = arith.muli %62, %66 : i64
%69 = llvm.load %59 : !llvm.ptr -> i64
%70 = arith.constant 1 : i32
%72 = arith.extsi %70 : i32 to i64
%71 = arith.addi %69, %72 : i64
%73 = arith.muli %68, %71 : i64
%74 = arith.cmpi sle, %73, %arg0 : i64
cf.cond_br %74, ^bb19, ^bb20
^bb19:
%75 = llvm.load %59 : !llvm.ptr -> i64
%76 = arith.constant 1 : i32
%78 = arith.extsi %76 : i32 to i64
%77 = arith.addi %75, %78 : i64
llvm.store %77, %59 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
cf.br ^bb21
^bb21:
%79 = llvm.load %59 : !llvm.ptr -> i64
%80 = llvm.load %59 : !llvm.ptr -> i64
%81 = arith.muli %79, %80 : i64
%82 = llvm.load %59 : !llvm.ptr -> i64
%83 = arith.muli %81, %82 : i64
%84 = arith.cmpi sgt, %83, %arg0 : i64
cf.cond_br %84, ^bb22, ^bb23
^bb22:
%85 = llvm.load %59 : !llvm.ptr -> i64
%86 = arith.constant 1 : i32
%88 = arith.extsi %86 : i32 to i64
%87 = arith.subi %85, %88 : i64
llvm.store %87, %59 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%89 = llvm.load %59 : !llvm.ptr -> i64
func.return %89 : i64
}
func.func @igcd(%arg0: i64, %arg1: i64) -> i64 {
%90 = llvm.mlir.constant(1 : i64) : i64
%91 = llvm.alloca %90 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %91 : i64, !llvm.ptr
%92 = llvm.mlir.constant(1 : i64) : i64
%93 = llvm.alloca %92 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %93 : i64, !llvm.ptr
cf.br ^bb24
^bb24:
%94 = llvm.load %93 : !llvm.ptr -> i64
%95 = arith.constant 0 : i32
%97 = arith.extsi %95 : i32 to i64
%96 = arith.cmpi ne, %94, %97 : i64
cf.cond_br %96, ^bb25, ^bb26
^bb25:
%98 = llvm.load %91 : !llvm.ptr -> i64
%99 = llvm.load %93 : !llvm.ptr -> i64
%100 = arith.remsi %98, %99 : i64
%101 = llvm.load %93 : !llvm.ptr -> i64
llvm.store %101, %91 : i64, !llvm.ptr
llvm.store %100, %93 : i64, !llvm.ptr
cf.br ^bb24
^bb26:
%102 = llvm.load %91 : !llvm.ptr -> i64
func.return %102 : i64
}
func.func @count_with_parity(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%103 = llvm.mlir.constant(1 : i64) : i64
%104 = llvm.alloca %103 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %104 : i64, !llvm.ptr
%105 = llvm.load %104 : !llvm.ptr -> i64
%106 = arith.cmpi slt, %arg1, %105 : i64
cf.cond_br %106, ^bb27, ^bb28
^bb27:
%107 = arith.constant 0 : i32
%108 = arith.extsi %107 : i32 to i64
func.return %108 : i64
^bb28:
cf.br ^bb29
^bb29:
%109 = llvm.load %104 : !llvm.ptr -> i64
%110 = arith.constant 2 : i32
%112 = arith.extsi %110 : i32 to i64
%111 = arith.remsi %109, %112 : i64
%113 = arith.cmpi ne, %111, %arg2 : i64
cf.cond_br %113, ^bb30, ^bb31
^bb30:
%114 = llvm.load %104 : !llvm.ptr -> i64
%115 = arith.constant 1 : i32
%117 = arith.extsi %115 : i32 to i64
%116 = arith.addi %114, %117 : i64
llvm.store %116, %104 : i64, !llvm.ptr
cf.br ^bb32
^bb31:
cf.br ^bb32
^bb32:
%118 = llvm.load %104 : !llvm.ptr -> i64
%119 = arith.cmpi sgt, %118, %arg1 : i64
cf.cond_br %119, ^bb33, ^bb34
^bb33:
%120 = arith.constant 0 : i32
%121 = arith.extsi %120 : i32 to i64
func.return %121 : i64
^bb34:
cf.br ^bb35
^bb35:
%122 = llvm.load %104 : !llvm.ptr -> i64
%123 = arith.subi %arg1, %122 : i64
%124 = arith.constant 2 : i32
%126 = arith.extsi %124 : i32 to i64
%125 = arith.divsi %123, %126 : i64
%127 = arith.constant 1 : i32
%129 = arith.extsi %127 : i32 to i64
%128 = arith.addi %125, %129 : i64
func.return %128 : i64
}
func.func @spf_sieve(%arg0: i64, %arg1: !llvm.ptr) -> () {
%130 = arith.constant 0 : i32
%131 = arith.extsi %130 : i32 to i64
%132 = llvm.mlir.constant(1 : i64) : i64
%133 = llvm.alloca %132 x i64 : (i64) -> !llvm.ptr
llvm.store %131, %133 : i64, !llvm.ptr
cf.br ^bb36
^bb36:
%134 = llvm.load %133 : !llvm.ptr -> i64
%135 = arith.cmpi sle, %134, %arg0 : i64
cf.cond_br %135, ^bb37, ^bb38
^bb37:
%136 = llvm.load %133 : !llvm.ptr -> i64
%137 = llvm.load %133 : !llvm.ptr -> i64
%138 = llvm.getelementptr %arg1[%137] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %136, %138 : i64, !llvm.ptr
%139 = llvm.load %133 : !llvm.ptr -> i64
%140 = arith.constant 1 : i32
%142 = arith.extsi %140 : i32 to i64
%141 = arith.addi %139, %142 : i64
llvm.store %141, %133 : i64, !llvm.ptr
cf.br ^bb36
^bb38:
%143 = arith.sitofp %arg0 : i64 to f64
%144 = math.sqrt %143 : f64
%145 = arith.fptosi %144 : f64 to i64
%146 = arith.constant 2 : i32
%147 = arith.extsi %146 : i32 to i64
llvm.store %147, %133 : i64, !llvm.ptr
cf.br ^bb39
^bb39:
%148 = llvm.load %133 : !llvm.ptr -> i64
%149 = arith.cmpi sle, %148, %145 : i64
cf.cond_br %149, ^bb40, ^bb41
^bb40:
%151 = llvm.load %133 : !llvm.ptr -> i64
%152 = llvm.getelementptr %arg1[%151] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%150 = llvm.load %152 : !llvm.ptr -> i64
%153 = llvm.load %133 : !llvm.ptr -> i64
%154 = arith.cmpi eq, %150, %153 : i64
cf.cond_br %154, ^bb42, ^bb43
^bb42:
%155 = llvm.load %133 : !llvm.ptr -> i64
%156 = llvm.load %133 : !llvm.ptr -> i64
%157 = arith.muli %155, %156 : i64
%158 = llvm.mlir.constant(1 : i64) : i64
%159 = llvm.alloca %158 x i64 : (i64) -> !llvm.ptr
llvm.store %157, %159 : i64, !llvm.ptr
cf.br ^bb45
^bb45:
%160 = llvm.load %159 : !llvm.ptr -> i64
%161 = arith.cmpi sle, %160, %arg0 : i64
cf.cond_br %161, ^bb46, ^bb47
^bb46:
%163 = llvm.load %159 : !llvm.ptr -> i64
%164 = llvm.getelementptr %arg1[%163] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%162 = llvm.load %164 : !llvm.ptr -> i64
%165 = llvm.load %159 : !llvm.ptr -> i64
%166 = arith.cmpi eq, %162, %165 : i64
cf.cond_br %166, ^bb48, ^bb49
^bb48:
%167 = llvm.load %133 : !llvm.ptr -> i64
%168 = llvm.load %159 : !llvm.ptr -> i64
%169 = llvm.getelementptr %arg1[%168] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %167, %169 : i64, !llvm.ptr
cf.br ^bb50
^bb49:
cf.br ^bb50
^bb50:
%170 = llvm.load %159 : !llvm.ptr -> i64
%171 = llvm.load %133 : !llvm.ptr -> i64
%172 = arith.addi %170, %171 : i64
llvm.store %172, %159 : i64, !llvm.ptr
cf.br ^bb45
^bb47:
cf.br ^bb44
^bb43:
cf.br ^bb44
^bb44:
%173 = llvm.load %133 : !llvm.ptr -> i64
%174 = arith.constant 1 : i32
%176 = arith.extsi %174 : i32 to i64
%175 = arith.addi %173, %176 : i64
llvm.store %175, %133 : i64, !llvm.ptr
cf.br ^bb39
^bb41:
func.return
}
func.func @compute_squarefree(%arg0: i64, %arg1: !llvm.ptr, %arg2: !llvm.ptr) -> () {
%177 = arith.constant 0 : i32
%178 = arith.constant 0 : i32
%179 = arith.extsi %177 : i32 to i64
%180 = arith.extsi %178 : i32 to i64
%181 = llvm.getelementptr %arg2[%180] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %179, %181 : i64, !llvm.ptr
%182 = arith.constant 1 : i32
%183 = arith.constant 1 : i32
%184 = arith.extsi %182 : i32 to i64
%185 = arith.extsi %183 : i32 to i64
%186 = llvm.getelementptr %arg2[%185] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %184, %186 : i64, !llvm.ptr
%187 = arith.constant 2 : i32
%188 = arith.extsi %187 : i32 to i64
%189 = llvm.mlir.constant(1 : i64) : i64
%190 = llvm.alloca %189 x i64 : (i64) -> !llvm.ptr
llvm.store %188, %190 : i64, !llvm.ptr
cf.br ^bb51
^bb51:
%191 = llvm.load %190 : !llvm.ptr -> i64
%192 = arith.cmpi sle, %191, %arg0 : i64
cf.cond_br %192, ^bb52, ^bb53
^bb52:
%194 = llvm.load %190 : !llvm.ptr -> i64
%195 = llvm.getelementptr %arg1[%194] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%193 = llvm.load %195 : !llvm.ptr -> i64
%196 = llvm.load %190 : !llvm.ptr -> i64
%197 = arith.divsi %196, %193 : i64
%198 = arith.remsi %197, %193 : i64
%199 = arith.constant 0 : i32
%201 = arith.extsi %199 : i32 to i64
%200 = arith.cmpi eq, %198, %201 : i64
cf.cond_br %200, ^bb54, ^bb55
^bb54:
%203 = arith.divsi %197, %193 : i64
%204 = llvm.getelementptr %arg2[%203] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%202 = llvm.load %204 : !llvm.ptr -> i64
%205 = llvm.load %190 : !llvm.ptr -> i64
%206 = llvm.getelementptr %arg2[%205] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %202, %206 : i64, !llvm.ptr
cf.br ^bb56
^bb55:
%208 = llvm.getelementptr %arg2[%197] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%207 = llvm.load %208 : !llvm.ptr -> i64
%209 = arith.muli %193, %207 : i64
%210 = llvm.load %190 : !llvm.ptr -> i64
%211 = llvm.getelementptr %arg2[%210] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %209, %211 : i64, !llvm.ptr
cf.br ^bb56
^bb56:
%212 = llvm.load %190 : !llvm.ptr -> i64
%213 = arith.constant 1 : i32
%215 = arith.extsi %213 : i32 to i64
%214 = arith.addi %212, %215 : i64
llvm.store %214, %190 : i64, !llvm.ptr
cf.br ^bb51
^bb53:
func.return
}
func.func @count_triangles(%arg0: i64) -> i64 {
%217 = arith.constant 2 : i32
%219 = arith.extsi %217 : i32 to i64
%218 = arith.muli %219, %arg0 : i64
%220 = arith.muli %218, %arg0 : i64
%216 = func.call @icbrt_floor(%220) : (i64) -> i64
%221 = arith.constant 2 : i32
%223 = arith.extsi %221 : i32 to i64
%222 = arith.addi %216, %223 : i64
%224 = arith.constant 3 : i32
%226 = arith.extsi %224 : i32 to i64
%225 = arith.divsi %arg0, %226 : i64
%227 = arith.constant 1 : i32
%229 = arith.extsi %227 : i32 to i64
%228 = arith.addi %225, %229 : i64
%230 = llvm.mlir.constant(1 : i64) : i64
%231 = llvm.alloca %230 x i64 : (i64) -> !llvm.ptr
llvm.store %228, %231 : i64, !llvm.ptr
%232 = arith.constant 1 : i32
%234 = arith.extsi %232 : i32 to i64
%233 = arith.addi %222, %234 : i64
%235 = llvm.load %231 : !llvm.ptr -> i64
%236 = arith.cmpi sgt, %233, %235 : i64
cf.cond_br %236, ^bb57, ^bb58
^bb57:
%237 = arith.constant 1 : i32
%239 = arith.extsi %237 : i32 to i64
%238 = arith.addi %222, %239 : i64
llvm.store %238, %231 : i64, !llvm.ptr
cf.br ^bb59
^bb58:
cf.br ^bb59
^bb59:
%241 = llvm.load %231 : !llvm.ptr -> i64
%242 = arith.constant 1 : i32
%244 = arith.extsi %242 : i32 to i64
%243 = arith.addi %241, %244 : i64
%245 = arith.constant 8 : i32
%247 = arith.extsi %245 : i32 to i64
%246 = arith.muli %243, %247 : i64
%240 = func.call @malloc(%246) : (i64) -> !llvm.ptr
%249 = llvm.load %231 : !llvm.ptr -> i64
func.call @spf_sieve(%249, %240) : (i64, !llvm.ptr) -> ()
%251 = llvm.load %231 : !llvm.ptr -> i64
%252 = arith.constant 1 : i32
%254 = arith.extsi %252 : i32 to i64
%253 = arith.addi %251, %254 : i64
%255 = arith.constant 8 : i32
%257 = arith.extsi %255 : i32 to i64
%256 = arith.muli %253, %257 : i64
%250 = func.call @malloc(%256) : (i64) -> !llvm.ptr
%259 = llvm.load %231 : !llvm.ptr -> i64
func.call @compute_squarefree(%259, %240, %250) : (i64, !llvm.ptr, !llvm.ptr) -> ()
%260 = arith.constant 0 : i32
%261 = arith.extsi %260 : i32 to i64
%262 = llvm.mlir.constant(1 : i64) : i64
%263 = llvm.alloca %262 x i64 : (i64) -> !llvm.ptr
llvm.store %261, %263 : i64, !llvm.ptr
%264 = arith.constant 2 : i32
%265 = arith.extsi %264 : i32 to i64
%266 = llvm.mlir.constant(1 : i64) : i64
%267 = llvm.alloca %266 x i64 : (i64) -> !llvm.ptr
llvm.store %265, %267 : i64, !llvm.ptr
cf.br ^bb60
^bb60:
%268 = llvm.load %267 : !llvm.ptr -> i64
%269 = arith.cmpi sle, %268, %222 : i64
cf.cond_br %269, ^bb61, ^bb62
^bb61:
%270 = llvm.load %267 : !llvm.ptr -> i64
%271 = arith.divsi %arg0, %270 : i64
%272 = arith.constant 0 : i32
%274 = arith.extsi %272 : i32 to i64
%273 = arith.cmpi eq, %271, %274 : i64
cf.cond_br %273, ^bb63, ^bb64
^bb63:
cf.br ^bb62
^bb64:
cf.br ^bb65
^bb65:
%275 = arith.muli %271, %271 : i64
%276 = llvm.load %267 : !llvm.ptr -> i64
%277 = arith.constant 1 : i32
%279 = arith.extsi %277 : i32 to i64
%278 = arith.addi %276, %279 : i64
%280 = arith.constant 2 : i32
%282 = arith.extsi %280 : i32 to i64
%281 = arith.divsi %278, %282 : i64
%283 = llvm.mlir.constant(1 : i64) : i64
%284 = llvm.alloca %283 x i64 : (i64) -> !llvm.ptr
llvm.store %281, %284 : i64, !llvm.ptr
cf.br ^bb66
^bb66:
%285 = llvm.load %284 : !llvm.ptr -> i64
%286 = llvm.load %267 : !llvm.ptr -> i64
%287 = arith.cmpi slt, %285, %286 : i64
cf.cond_br %287, ^bb67, ^bb68
^bb67:
%289 = llvm.load %284 : !llvm.ptr -> i64
%290 = llvm.load %267 : !llvm.ptr -> i64
%288 = func.call @igcd(%289, %290) : (i64, i64) -> i64
%291 = arith.constant 1 : i32
%293 = arith.extsi %291 : i32 to i64
%292 = arith.cmpi ne, %288, %293 : i64
cf.cond_br %292, ^bb69, ^bb70
^bb69:
%294 = llvm.load %284 : !llvm.ptr -> i64
%295 = arith.constant 1 : i32
%297 = arith.extsi %295 : i32 to i64
%296 = arith.addi %294, %297 : i64
llvm.store %296, %284 : i64, !llvm.ptr
cf.br ^bb66
^bb70:
cf.br ^bb71
^bb71:
%298 = llvm.load %267 : !llvm.ptr -> i64
%299 = llvm.load %284 : !llvm.ptr -> i64
%300 = arith.subi %298, %299 : i64
%302 = llvm.getelementptr %250[%300] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%301 = llvm.load %302 : !llvm.ptr -> i64
%303 = llvm.load %284 : !llvm.ptr -> i64
%304 = arith.muli %303, %301 : i64
%305 = arith.cmpi sgt, %304, %275 : i64
cf.cond_br %305, ^bb72, ^bb73
^bb72:
%306 = llvm.load %284 : !llvm.ptr -> i64
%307 = arith.constant 1 : i32
%309 = arith.extsi %307 : i32 to i64
%308 = arith.addi %306, %309 : i64
llvm.store %308, %284 : i64, !llvm.ptr
cf.br ^bb66
^bb73:
cf.br ^bb74
^bb74:
%310 = arith.muli %300, %271 : i64
%311 = arith.constant 2 : i32
%312 = llvm.load %284 : !llvm.ptr -> i64
%314 = arith.extsi %311 : i32 to i64
%313 = arith.muli %314, %312 : i64
%315 = arith.addi %300, %313 : i64
%316 = arith.divsi %310, %315 : i64
%317 = arith.constant 0 : i32
%319 = arith.extsi %317 : i32 to i64
%318 = arith.cmpi sle, %316, %319 : i64
cf.cond_br %318, ^bb75, ^bb76
^bb75:
%320 = llvm.load %284 : !llvm.ptr -> i64
%321 = arith.constant 1 : i32
%323 = arith.extsi %321 : i32 to i64
%322 = arith.addi %320, %323 : i64
llvm.store %322, %284 : i64, !llvm.ptr
cf.br ^bb66
^bb76:
cf.br ^bb77
^bb77:
%325 = llvm.load %284 : !llvm.ptr -> i64
%324 = func.call @igcd(%301, %325) : (i64, i64) -> i64
%326 = arith.divsi %301, %324 : i64
%327 = llvm.load %284 : !llvm.ptr -> i64
%328 = arith.divsi %327, %324 : i64
%329 = arith.constant 2 : i32
%330 = llvm.load %284 : !llvm.ptr -> i64
%332 = arith.extsi %329 : i32 to i64
%331 = arith.muli %332, %330 : i64
%333 = arith.addi %300, %331 : i64
%334 = arith.constant 1 : i32
%335 = arith.extsi %334 : i32 to i64
%336 = llvm.mlir.constant(1 : i64) : i64
%337 = llvm.alloca %336 x i64 : (i64) -> !llvm.ptr
llvm.store %335, %337 : i64, !llvm.ptr
cf.br ^bb78
^bb78:
%338 = llvm.load %337 : !llvm.ptr -> i64
%339 = arith.cmpi sle, %338, %316 : i64
cf.cond_br %339, ^bb79, ^bb80
^bb79:
%341 = llvm.load %337 : !llvm.ptr -> i64
%340 = func.call @igcd(%341, %328) : (i64, i64) -> i64
%342 = llvm.load %337 : !llvm.ptr -> i64
%343 = arith.divsi %342, %340 : i64
%345 = llvm.getelementptr %250[%343] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%344 = llvm.load %345 : !llvm.ptr -> i64
%346 = func.call @igcd(%326, %344) : (i64, i64) -> i64
%347 = arith.divsi %328, %340 : i64
%348 = arith.muli %326, %344 : i64
%349 = arith.muli %346, %346 : i64
%350 = arith.divsi %348, %349 : i64
%351 = arith.muli %347, %350 : i64
%352 = arith.cmpi sgt, %351, %271 : i64
cf.cond_br %352, ^bb81, ^bb82
^bb81:
%353 = llvm.load %337 : !llvm.ptr -> i64
%354 = arith.constant 1 : i32
%356 = arith.extsi %354 : i32 to i64
%355 = arith.addi %353, %356 : i64
llvm.store %355, %337 : i64, !llvm.ptr
cf.br ^bb78
^bb82:
cf.br ^bb83
^bb83:
%357 = llvm.load %337 : !llvm.ptr -> i64
%358 = arith.muli %333, %357 : i64
%359 = arith.addi %358, %300 : i64
%360 = arith.constant 1 : i32
%362 = arith.extsi %360 : i32 to i64
%361 = arith.subi %359, %362 : i64
%363 = arith.divsi %361, %300 : i64
%365 = arith.addi %363, %351 : i64
%366 = arith.constant 1 : i32
%368 = arith.extsi %366 : i32 to i64
%367 = arith.subi %365, %368 : i64
%369 = arith.divsi %367, %351 : i64
%364 = func.call @isqrt_ceil_i64(%369) : (i64) -> i64
%371 = arith.divsi %271, %351 : i64
%370 = func.call @isqrt_i64(%371) : (i64) -> i64
%372 = arith.constant 2 : i32
%374 = arith.extsi %372 : i32 to i64
%373 = arith.remsi %351, %374 : i64
%375 = arith.constant 1 : i32
%377 = arith.extsi %375 : i32 to i64
%376 = arith.cmpi eq, %373, %377 : i64
cf.cond_br %376, ^bb84, ^bb85
^bb84:
%378 = llvm.load %263 : !llvm.ptr -> i64
%380 = llvm.load %337 : !llvm.ptr -> i64
%381 = arith.constant 2 : i32
%383 = arith.extsi %381 : i32 to i64
%382 = arith.remsi %380, %383 : i64
%379 = func.call @count_with_parity(%364, %370, %382) : (i64, i64, i64) -> i64
%384 = arith.addi %378, %379 : i64
llvm.store %384, %263 : i64, !llvm.ptr
cf.br ^bb86
^bb85:
%385 = llvm.load %337 : !llvm.ptr -> i64
%386 = arith.constant 2 : i32
%388 = arith.extsi %386 : i32 to i64
%387 = arith.remsi %385, %388 : i64
%389 = arith.constant 0 : i32
%391 = arith.extsi %389 : i32 to i64
%390 = arith.cmpi eq, %387, %391 : i64
cf.cond_br %390, ^bb87, ^bb88
^bb87:
%392 = arith.subi %370, %364 : i64
%393 = arith.constant 1 : i32
%395 = arith.extsi %393 : i32 to i64
%394 = arith.addi %392, %395 : i64
%396 = arith.constant 0 : i32
%398 = arith.extsi %396 : i32 to i64
%397 = arith.cmpi sgt, %394, %398 : i64
cf.cond_br %397, ^bb90, ^bb91
^bb90:
%399 = llvm.load %263 : !llvm.ptr -> i64
%400 = arith.addi %399, %394 : i64
llvm.store %400, %263 : i64, !llvm.ptr
cf.br ^bb92
^bb91:
cf.br ^bb92
^bb92:
cf.br ^bb89
^bb88:
cf.br ^bb89
^bb89:
cf.br ^bb86
^bb86:
%401 = llvm.load %337 : !llvm.ptr -> i64
%402 = arith.constant 1 : i32
%404 = arith.extsi %402 : i32 to i64
%403 = arith.addi %401, %404 : i64
llvm.store %403, %337 : i64, !llvm.ptr
cf.br ^bb78
^bb80:
%405 = llvm.load %284 : !llvm.ptr -> i64
%406 = arith.constant 1 : i32
%408 = arith.extsi %406 : i32 to i64
%407 = arith.addi %405, %408 : i64
llvm.store %407, %284 : i64, !llvm.ptr
cf.br ^bb66
^bb68:
%409 = llvm.load %267 : !llvm.ptr -> i64
%410 = arith.constant 1 : i32
%412 = arith.extsi %410 : i32 to i64
%411 = arith.addi %409, %412 : i64
llvm.store %411, %267 : i64, !llvm.ptr
cf.br ^bb60
^bb62:
func.call @free(%240) : (!llvm.ptr) -> ()
func.call @free(%250) : (!llvm.ptr) -> ()
%415 = llvm.load %263 : !llvm.ptr -> i64
func.return %415 : i64
}
func.func @main() -> i32 {
%417 = arith.constant 1000000 : i32
%418 = arith.extsi %417 : i32 to i64
%416 = func.call @count_triangles(%418) : (i64) -> i64
%419 = llvm.mlir.addressof @str_0 : !llvm.ptr
%420 = llvm.call @printf(%419, %416) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%421 = arith.constant 0 : i32
func.return %421 : i32
}
}