← All problems
Problem 869
E(10^8) via binary trie on reversed prime bits. Build a binary trie keyed by the reversed binary representation of each prime (LSB first). At every internal node the score contribution is max(count_left, count_right). The answer is total_score / pi(N).
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)?
Space complexity O(n)?
Approach Flow solution Not curated
Verdict Unknown
Flow source
# Project Euler 869: Prime Guessing
# E(10^8) via binary trie on reversed prime bits.
# Build a binary trie keyed by the reversed binary representation of each
# prime (LSB first). At every internal node the score contribution is
# max(count_left, count_right). The answer is total_score / pi(N).
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function sqrt(x: f64) -> f64
function memset(dst: ptr<void>, val: i32, n: i64) -> void
}
const N: i64 = 100000000
# Global state for calc_points
let mut total_points: i64 = 0
let mut tmpbuf: ptr<i32> = null as ptr<i32>
function calc_points(arr: ptr<i32>, lo: i32, hi: i32, bit: i32) -> void {
if hi <= lo { return }
if bit >= 31 { return }
let mut count0: i32 = 0
let mut count1: i32 = 0
let mut i: i32 = lo
while i < hi {
let v: i32 = arr[i] >> bit
if v != 0 {
if (v & 1) != 0 {
count1 = count1 + 1
} else {
count0 = count0 + 1
}
}
i = i + 1
}
if count0 + count1 == 0 { return }
if count0 > count1 {
total_points = total_points + (count0 as i64)
} else {
total_points = total_points + (count1 as i64)
}
let p0: i32 = 0
let p1: i32 = count0
let p2: i32 = count0 + count1
i = lo
while i < hi {
let v: i32 = arr[i] >> bit
if v == 0 {
tmpbuf[p2] = arr[i]
p2 = p2 + 1
} else {
if (v & 1) != 0 {
tmpbuf[p1] = arr[i]
p1 = p1 + 1
} else {
tmpbuf[p0] = arr[i]
p0 = p0 + 1
}
}
i = i + 1
}
let count: i64 = (hi - lo) as i64
# Copy from tmpbuf back to arr[lo..hi]
let mut k: i32 = 0
while k < hi - lo {
arr[lo + k] = tmpbuf[k]
k = k + 1
}
let mid0: i32 = lo + count0
let mid1: i32 = mid0 + count1
calc_points(arr, lo, mid0, bit + 1)
calc_points(arr, mid0, mid1, bit + 1)
}
function main() -> i32 {
let is_prime: ptr<i8> = calloc(N + 1, 1)
memset(is_prime as ptr<void>, 1, N + 1)
is_prime[0] = 0
is_prime[1] = 0
let limit: i32 = sqrt(N as f64) as i32
let mut i: i32 = 2
while i <= limit {
if is_prime[i] != 0 {
let mut j: i32 = i * i
while j <= N as i32 {
is_prime[j] = 0
j = j + i
}
}
i = i + 1
}
let mut pc: i32 = 0
i = 2
while i <= N as i32 {
if is_prime[i] != 0 { pc = pc + 1 }
i = i + 1
}
let primes: ptr<i32> = calloc(pc as i64, 4)
let mut idx: i32 = 0
i = 2
while i <= N as i32 {
if is_prime[i] != 0 {
primes[idx] = i
idx = idx + 1
}
i = i + 1
}
free(is_prime as ptr<void>)
tmpbuf = calloc(pc as i64, 4)
total_points = 0
calc_points(primes, 0, pc, 0)
let result: f64 = (total_points as f64) / (pc as f64)
printf("%.8f\n", result)
free(primes as ptr<void>)
free(tmpbuf as ptr<void>)
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; }
void calc_points_ptr_i32_i32_i32_i32(int32_t* arr, int32_t lo, int32_t hi, int32_t bit);
int32_t main(void);
static const int64_t N = 100000000;
/* Module statics */
static int64_t total_points = 0;
static int32_t* tmpbuf = ((int32_t*)(NULL));
void calc_points_ptr_i32_i32_i32_i32(int32_t* arr, int32_t lo, int32_t hi, int32_t bit) {
if (hi <= lo) {
return;
}
if (bit >= 31) {
return;
}
int32_t count0 = 0;
int32_t count1 = 0;
int32_t i = lo;
while (i < hi) {
int32_t v = FLOW_CHECKED_SHR((arr[i]), (bit));
if (v != 0) {
if ((v & 1) != 0) {
count1 = (count1 + 1);
} else {
count0 = (count0 + 1);
}
}
i = (i + 1);
}
if ((count0 + count1) == 0) {
return;
}
if (count0 > count1) {
total_points = (total_points + ((int64_t)(count0)));
} else {
total_points = (total_points + ((int64_t)(count1)));
}
int32_t p0 = 0;
int32_t p1 = count0;
int32_t p2 = (count0 + count1);
i = lo;
while (i < hi) {
int32_t v = FLOW_CHECKED_SHR((arr[i]), (bit));
if (v == 0) {
tmpbuf[p2] = arr[i];
p2 = (p2 + 1);
} else {
if ((v & 1) != 0) {
tmpbuf[p1] = arr[i];
p1 = (p1 + 1);
} else {
tmpbuf[p0] = arr[i];
p0 = (p0 + 1);
}
}
i = (i + 1);
}
int64_t count = ((int64_t)((hi - lo)));
int32_t k = 0;
while (k < (hi - lo)) {
arr[(lo + k)] = tmpbuf[k];
k = (k + 1);
}
int32_t mid0 = (lo + count0);
int32_t mid1 = (mid0 + count1);
calc_points_ptr_i32_i32_i32_i32(arr, lo, mid0, (bit + 1));
calc_points_ptr_i32_i32_i32_i32(arr, mid0, mid1, (bit + 1));
}
int32_t main(void) {
int8_t* is_prime = (int8_t*)(calloc((N + 1), 1));
memset(((void*)(is_prime)), 1, (N + 1));
is_prime[0] = 0;
is_prime[1] = 0;
int32_t limit = ((int32_t)(sqrt(((double)(N)))));
int32_t i = 2;
while (i <= limit) {
if (is_prime[i] != 0) {
int32_t j = (i * i);
while (j <= ((int32_t)(N))) {
is_prime[j] = 0;
j = (j + i);
}
}
i = (i + 1);
}
int32_t pc = 0;
i = 2;
while (i <= ((int32_t)(N))) {
if (is_prime[i] != 0) {
pc = (pc + 1);
}
i = (i + 1);
}
int32_t* primes = (int32_t*)(calloc(((int64_t)(pc)), 4));
int32_t idx = 0;
i = 2;
while (i <= ((int32_t)(N))) {
if (is_prime[i] != 0) {
primes[idx] = i;
idx = (idx + 1);
}
i = (i + 1);
}
free(((void*)(is_prime)));
tmpbuf = calloc(((int64_t)(pc)), 4);
total_points = 0;
calc_points_ptr_i32_i32_i32_i32(primes, 0, pc, 0);
double result = (((double)(total_points)) / ((double)(pc)));
printf("%.8f\n", result);
free(((void*)(primes)));
free(((void*)(tmpbuf)));
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.8f\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 @sqrt(f64) -> f64
func.func private @memset(!llvm.ptr, i32, i64) -> ()
// Constant: N
llvm.mlir.global internal constant @N(100000000 : i64) : i64
// Module static: total_points
llvm.mlir.global internal @total_points(0 : i64) : i64
// Module static: tmpbuf
llvm.mlir.global internal @tmpbuf() {addr_space = 0 : i32} : !llvm.ptr {
%0 = llvm.mlir.zero : !llvm.ptr
llvm.return %0 : !llvm.ptr
}
func.func @calc_points(%arg0: !llvm.ptr, %arg1: i32, %arg2: i32, %arg3: i32) -> () {
%1 = arith.cmpi sle, %arg2, %arg1 : i32
cf.cond_br %1, ^bb0, ^bb1
^bb0:
func.return
^bb1:
cf.br ^bb2
^bb2:
%2 = arith.constant 31 : i32
%3 = arith.cmpi sge, %arg3, %2 : i32
cf.cond_br %3, ^bb3, ^bb4
^bb3:
func.return
^bb4:
cf.br ^bb5
^bb5:
%4 = arith.constant 0 : i32
%5 = llvm.mlir.constant(1 : i64) : i64
%6 = llvm.alloca %5 x i32 : (i64) -> !llvm.ptr
llvm.store %4, %6 : i32, !llvm.ptr
%7 = arith.constant 0 : i32
%8 = llvm.mlir.constant(1 : i64) : i64
%9 = llvm.alloca %8 x i32 : (i64) -> !llvm.ptr
llvm.store %7, %9 : i32, !llvm.ptr
%10 = llvm.mlir.constant(1 : i64) : i64
%11 = llvm.alloca %10 x i32 : (i64) -> !llvm.ptr
llvm.store %arg1, %11 : i32, !llvm.ptr
cf.br ^bb6
^bb6:
%12 = llvm.load %11 : !llvm.ptr -> i32
%13 = arith.cmpi slt, %12, %arg2 : i32
cf.cond_br %13, ^bb7, ^bb8
^bb7:
%15 = llvm.load %11 : !llvm.ptr -> i32
%16 = arith.extsi %15 : i32 to i64
%17 = llvm.getelementptr %arg0[%16] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%14 = llvm.load %17 : !llvm.ptr -> i32
%18 = arith.shrsi %14, %arg3 : i32
%19 = arith.constant 0 : i32
%20 = arith.cmpi ne, %18, %19 : i32
cf.cond_br %20, ^bb9, ^bb10
^bb9:
%21 = arith.constant 1 : i32
%22 = arith.andi %18, %21 : i32
%23 = arith.constant 0 : i32
%24 = arith.cmpi ne, %22, %23 : i32
cf.cond_br %24, ^bb12, ^bb13
^bb12:
%25 = llvm.load %9 : !llvm.ptr -> i32
%26 = arith.constant 1 : i32
%27 = arith.addi %25, %26 : i32
llvm.store %27, %9 : i32, !llvm.ptr
cf.br ^bb14
^bb13:
%28 = llvm.load %6 : !llvm.ptr -> i32
%29 = arith.constant 1 : i32
%30 = arith.addi %28, %29 : i32
llvm.store %30, %6 : i32, !llvm.ptr
cf.br ^bb14
^bb14:
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%31 = llvm.load %11 : !llvm.ptr -> i32
%32 = arith.constant 1 : i32
%33 = arith.addi %31, %32 : i32
llvm.store %33, %11 : i32, !llvm.ptr
cf.br ^bb6
^bb8:
%34 = llvm.load %6 : !llvm.ptr -> i32
%35 = llvm.load %9 : !llvm.ptr -> i32
%36 = arith.addi %34, %35 : i32
%37 = arith.constant 0 : i32
%38 = arith.cmpi eq, %36, %37 : i32
cf.cond_br %38, ^bb15, ^bb16
^bb15:
func.return
^bb16:
cf.br ^bb17
^bb17:
%39 = llvm.load %6 : !llvm.ptr -> i32
%40 = llvm.load %9 : !llvm.ptr -> i32
%41 = arith.cmpi sgt, %39, %40 : i32
cf.cond_br %41, ^bb18, ^bb19
^bb18:
%42 = llvm.mlir.addressof @total_points : !llvm.ptr
%43 = llvm.load %42 : !llvm.ptr -> i64
%44 = llvm.load %6 : !llvm.ptr -> i32
%45 = arith.extsi %44 : i32 to i64
%46 = arith.addi %43, %45 : i64
%47 = llvm.mlir.addressof @total_points : !llvm.ptr
llvm.store %46, %47 : i64, !llvm.ptr
cf.br ^bb20
^bb19:
%48 = llvm.mlir.addressof @total_points : !llvm.ptr
%49 = llvm.load %48 : !llvm.ptr -> i64
%50 = llvm.load %9 : !llvm.ptr -> i32
%51 = arith.extsi %50 : i32 to i64
%52 = arith.addi %49, %51 : i64
%53 = llvm.mlir.addressof @total_points : !llvm.ptr
llvm.store %52, %53 : i64, !llvm.ptr
cf.br ^bb20
^bb20:
%54 = arith.constant 0 : i32
%55 = llvm.load %6 : !llvm.ptr -> i32
%56 = llvm.load %6 : !llvm.ptr -> i32
%57 = llvm.load %9 : !llvm.ptr -> i32
%58 = arith.addi %56, %57 : i32
llvm.store %arg1, %11 : i32, !llvm.ptr
cf.br ^bb21(%58, %55, %54 : i32, i32, i32)
^bb21(%59: i32, %60: i32, %61: i32):
%62 = llvm.load %11 : !llvm.ptr -> i32
%63 = arith.cmpi slt, %62, %arg2 : i32
cf.cond_br %63, ^bb22(%59, %60, %61 : i32, i32, i32), ^bb23(%59, %60, %61 : i32, i32, i32)
^bb22(%64: i32, %65: i32, %66: i32):
%68 = llvm.load %11 : !llvm.ptr -> i32
%69 = arith.extsi %68 : i32 to i64
%70 = llvm.getelementptr %arg0[%69] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%67 = llvm.load %70 : !llvm.ptr -> i32
%71 = arith.shrsi %67, %arg3 : i32
%72 = arith.constant 0 : i32
%73 = arith.cmpi eq, %71, %72 : i32
cf.cond_br %73, ^bb24, ^bb25
^bb24:
%75 = llvm.load %11 : !llvm.ptr -> i32
%76 = arith.extsi %75 : i32 to i64
%77 = llvm.getelementptr %arg0[%76] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%74 = llvm.load %77 : !llvm.ptr -> i32
%78 = llvm.mlir.addressof @tmpbuf : !llvm.ptr
%79 = llvm.load %78 : !llvm.ptr -> !llvm.ptr
%80 = arith.extsi %64 : i32 to i64
%81 = llvm.getelementptr %79[%80] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %74, %81 : i32, !llvm.ptr
%82 = arith.constant 1 : i32
%83 = arith.addi %64, %82 : i32
cf.br ^bb26(%83, %65, %66 : i32, i32, i32)
^bb25:
%84 = arith.constant 1 : i32
%85 = arith.andi %71, %84 : i32
%86 = arith.constant 0 : i32
%87 = arith.cmpi ne, %85, %86 : i32
%88, %89 = scf.if %87 -> (i32, i32) {
%91 = llvm.load %11 : !llvm.ptr -> i32
%92 = arith.extsi %91 : i32 to i64
%93 = llvm.getelementptr %arg0[%92] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%90 = llvm.load %93 : !llvm.ptr -> i32
%94 = llvm.mlir.addressof @tmpbuf : !llvm.ptr
%95 = llvm.load %94 : !llvm.ptr -> !llvm.ptr
%96 = arith.extsi %65 : i32 to i64
%97 = llvm.getelementptr %95[%96] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %90, %97 : i32, !llvm.ptr
%98 = arith.constant 1 : i32
%99 = arith.addi %65, %98 : i32
scf.yield %99, %66 : i32, i32
} else {
%101 = llvm.load %11 : !llvm.ptr -> i32
%102 = arith.extsi %101 : i32 to i64
%103 = llvm.getelementptr %arg0[%102] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%100 = llvm.load %103 : !llvm.ptr -> i32
%104 = llvm.mlir.addressof @tmpbuf : !llvm.ptr
%105 = llvm.load %104 : !llvm.ptr -> !llvm.ptr
%106 = arith.extsi %66 : i32 to i64
%107 = llvm.getelementptr %105[%106] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %100, %107 : i32, !llvm.ptr
%108 = arith.constant 1 : i32
%109 = arith.addi %66, %108 : i32
scf.yield %65, %109 : i32, i32
}
cf.br ^bb26(%64, %88, %89 : i32, i32, i32)
^bb26(%110: i32, %111: i32, %112: i32):
%113 = llvm.load %11 : !llvm.ptr -> i32
%114 = arith.constant 1 : i32
%115 = arith.addi %113, %114 : i32
llvm.store %115, %11 : i32, !llvm.ptr
cf.br ^bb21(%110, %111, %112 : i32, i32, i32)
^bb23(%116: i32, %117: i32, %118: i32):
%119 = arith.subi %arg2, %arg1 : i32
%120 = arith.extsi %119 : i32 to i64
%121 = arith.constant 0 : i32
%122 = llvm.mlir.constant(1 : i64) : i64
%123 = llvm.alloca %122 x i32 : (i64) -> !llvm.ptr
llvm.store %121, %123 : i32, !llvm.ptr
cf.br ^bb27
^bb27:
%124 = llvm.load %123 : !llvm.ptr -> i32
%125 = arith.subi %arg2, %arg1 : i32
%126 = arith.cmpi slt, %124, %125 : i32
cf.cond_br %126, ^bb28, ^bb29
^bb28:
%128 = llvm.mlir.addressof @tmpbuf : !llvm.ptr
%129 = llvm.load %128 : !llvm.ptr -> !llvm.ptr
%130 = llvm.load %123 : !llvm.ptr -> i32
%131 = arith.extsi %130 : i32 to i64
%132 = llvm.getelementptr %129[%131] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%127 = llvm.load %132 : !llvm.ptr -> i32
%133 = llvm.load %123 : !llvm.ptr -> i32
%134 = arith.addi %arg1, %133 : i32
%135 = arith.extsi %134 : i32 to i64
%136 = llvm.getelementptr %arg0[%135] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %127, %136 : i32, !llvm.ptr
%137 = llvm.load %123 : !llvm.ptr -> i32
%138 = arith.constant 1 : i32
%139 = arith.addi %137, %138 : i32
llvm.store %139, %123 : i32, !llvm.ptr
cf.br ^bb27
^bb29:
%140 = llvm.load %6 : !llvm.ptr -> i32
%141 = arith.addi %arg1, %140 : i32
%142 = llvm.load %9 : !llvm.ptr -> i32
%143 = arith.addi %141, %142 : i32
%145 = arith.constant 1 : i32
%146 = arith.addi %arg3, %145 : i32
func.call @calc_points(%arg0, %arg1, %141, %146) : (!llvm.ptr, i32, i32, i32) -> ()
%148 = arith.constant 1 : i32
%149 = arith.addi %arg3, %148 : i32
func.call @calc_points(%arg0, %141, %143, %149) : (!llvm.ptr, i32, i32, i32) -> ()
func.return
}
func.func @main() -> i32 {
%151 = llvm.mlir.addressof @N : !llvm.ptr
%152 = llvm.load %151 : !llvm.ptr -> i64
%153 = arith.constant 1 : i32
%155 = arith.extsi %153 : i32 to i64
%154 = arith.addi %152, %155 : i64
%156 = arith.constant 1 : i32
%157 = arith.extsi %156 : i32 to i64
%150 = func.call @calloc(%154, %157) : (i64, i64) -> !llvm.ptr
%159 = arith.constant 1 : i32
%160 = llvm.mlir.addressof @N : !llvm.ptr
%161 = llvm.load %160 : !llvm.ptr -> i64
%162 = arith.constant 1 : i32
%164 = arith.extsi %162 : i32 to i64
%163 = arith.addi %161, %164 : i64
func.call @memset(%150, %159, %163) : (!llvm.ptr, i32, i64) -> ()
%165 = arith.constant 0 : i32
%166 = arith.constant 0 : i32
%167 = arith.trunci %165 : i32 to i8
%168 = arith.extsi %166 : i32 to i64
%169 = llvm.getelementptr %150[%168] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %167, %169 : i8, !llvm.ptr
%170 = arith.constant 0 : i32
%171 = arith.constant 1 : i32
%172 = arith.trunci %170 : i32 to i8
%173 = arith.extsi %171 : i32 to i64
%174 = llvm.getelementptr %150[%173] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %172, %174 : i8, !llvm.ptr
%175 = llvm.mlir.addressof @N : !llvm.ptr
%176 = llvm.load %175 : !llvm.ptr -> i64
%177 = arith.sitofp %176 : i64 to f64
%178 = math.sqrt %177 : f64
%179 = arith.fptosi %178 : f64 to i32
%180 = arith.constant 2 : i32
%181 = llvm.mlir.constant(1 : i64) : i64
%182 = llvm.alloca %181 x i32 : (i64) -> !llvm.ptr
llvm.store %180, %182 : i32, !llvm.ptr
cf.br ^bb30
^bb30:
%183 = llvm.load %182 : !llvm.ptr -> i32
%184 = arith.cmpi sle, %183, %179 : i32
cf.cond_br %184, ^bb31, ^bb32
^bb31:
%186 = llvm.load %182 : !llvm.ptr -> i32
%187 = arith.extsi %186 : i32 to i64
%188 = llvm.getelementptr %150[%187] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%185 = llvm.load %188 : !llvm.ptr -> i8
%189 = arith.constant 0 : i32
%191 = arith.extsi %185 : i8 to i32
%190 = arith.cmpi ne, %191, %189 : i32
cf.cond_br %190, ^bb33, ^bb34
^bb33:
%192 = llvm.load %182 : !llvm.ptr -> i32
%193 = llvm.load %182 : !llvm.ptr -> i32
%194 = arith.muli %192, %193 : i32
%195 = llvm.mlir.constant(1 : i64) : i64
%196 = llvm.alloca %195 x i32 : (i64) -> !llvm.ptr
llvm.store %194, %196 : i32, !llvm.ptr
cf.br ^bb36
^bb36:
%197 = llvm.load %196 : !llvm.ptr -> i32
%198 = llvm.mlir.addressof @N : !llvm.ptr
%199 = llvm.load %198 : !llvm.ptr -> i64
%200 = arith.trunci %199 : i64 to i32
%201 = arith.cmpi sle, %197, %200 : i32
cf.cond_br %201, ^bb37, ^bb38
^bb37:
%202 = arith.constant 0 : i32
%203 = llvm.load %196 : !llvm.ptr -> i32
%204 = arith.trunci %202 : i32 to i8
%205 = arith.extsi %203 : i32 to i64
%206 = llvm.getelementptr %150[%205] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %204, %206 : i8, !llvm.ptr
%207 = llvm.load %196 : !llvm.ptr -> i32
%208 = llvm.load %182 : !llvm.ptr -> i32
%209 = arith.addi %207, %208 : i32
llvm.store %209, %196 : i32, !llvm.ptr
cf.br ^bb36
^bb38:
cf.br ^bb35
^bb34:
cf.br ^bb35
^bb35:
%210 = llvm.load %182 : !llvm.ptr -> i32
%211 = arith.constant 1 : i32
%212 = arith.addi %210, %211 : i32
llvm.store %212, %182 : i32, !llvm.ptr
cf.br ^bb30
^bb32:
%213 = arith.constant 0 : i32
%214 = llvm.mlir.constant(1 : i64) : i64
%215 = llvm.alloca %214 x i32 : (i64) -> !llvm.ptr
llvm.store %213, %215 : i32, !llvm.ptr
%216 = arith.constant 2 : i32
llvm.store %216, %182 : i32, !llvm.ptr
cf.br ^bb39
^bb39:
%217 = llvm.load %182 : !llvm.ptr -> i32
%218 = llvm.mlir.addressof @N : !llvm.ptr
%219 = llvm.load %218 : !llvm.ptr -> i64
%220 = arith.trunci %219 : i64 to i32
%221 = arith.cmpi sle, %217, %220 : i32
cf.cond_br %221, ^bb40, ^bb41
^bb40:
%223 = llvm.load %182 : !llvm.ptr -> i32
%224 = arith.extsi %223 : i32 to i64
%225 = llvm.getelementptr %150[%224] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%222 = llvm.load %225 : !llvm.ptr -> i8
%226 = arith.constant 0 : i32
%228 = arith.extsi %222 : i8 to i32
%227 = arith.cmpi ne, %228, %226 : i32
cf.cond_br %227, ^bb42, ^bb43
^bb42:
%229 = llvm.load %215 : !llvm.ptr -> i32
%230 = arith.constant 1 : i32
%231 = arith.addi %229, %230 : i32
llvm.store %231, %215 : i32, !llvm.ptr
cf.br ^bb44
^bb43:
cf.br ^bb44
^bb44:
%232 = llvm.load %182 : !llvm.ptr -> i32
%233 = arith.constant 1 : i32
%234 = arith.addi %232, %233 : i32
llvm.store %234, %182 : i32, !llvm.ptr
cf.br ^bb39
^bb41:
%236 = llvm.load %215 : !llvm.ptr -> i32
%237 = arith.extsi %236 : i32 to i64
%238 = arith.constant 4 : i32
%239 = arith.extsi %238 : i32 to i64
%235 = func.call @calloc(%237, %239) : (i64, i64) -> !llvm.ptr
%240 = arith.constant 0 : i32
%241 = llvm.mlir.constant(1 : i64) : i64
%242 = llvm.alloca %241 x i32 : (i64) -> !llvm.ptr
llvm.store %240, %242 : i32, !llvm.ptr
%243 = arith.constant 2 : i32
llvm.store %243, %182 : i32, !llvm.ptr
cf.br ^bb45
^bb45:
%244 = llvm.load %182 : !llvm.ptr -> i32
%245 = llvm.mlir.addressof @N : !llvm.ptr
%246 = llvm.load %245 : !llvm.ptr -> i64
%247 = arith.trunci %246 : i64 to i32
%248 = arith.cmpi sle, %244, %247 : i32
cf.cond_br %248, ^bb46, ^bb47
^bb46:
%250 = llvm.load %182 : !llvm.ptr -> i32
%251 = arith.extsi %250 : i32 to i64
%252 = llvm.getelementptr %150[%251] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%249 = llvm.load %252 : !llvm.ptr -> i8
%253 = arith.constant 0 : i32
%255 = arith.extsi %249 : i8 to i32
%254 = arith.cmpi ne, %255, %253 : i32
cf.cond_br %254, ^bb48, ^bb49
^bb48:
%256 = llvm.load %182 : !llvm.ptr -> i32
%257 = llvm.load %242 : !llvm.ptr -> i32
%258 = arith.extsi %257 : i32 to i64
%259 = llvm.getelementptr %235[%258] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %256, %259 : i32, !llvm.ptr
%260 = llvm.load %242 : !llvm.ptr -> i32
%261 = arith.constant 1 : i32
%262 = arith.addi %260, %261 : i32
llvm.store %262, %242 : i32, !llvm.ptr
cf.br ^bb50
^bb49:
cf.br ^bb50
^bb50:
%263 = llvm.load %182 : !llvm.ptr -> i32
%264 = arith.constant 1 : i32
%265 = arith.addi %263, %264 : i32
llvm.store %265, %182 : i32, !llvm.ptr
cf.br ^bb45
^bb47:
func.call @free(%150) : (!llvm.ptr) -> ()
%268 = llvm.load %215 : !llvm.ptr -> i32
%269 = arith.extsi %268 : i32 to i64
%270 = arith.constant 4 : i32
%271 = arith.extsi %270 : i32 to i64
%267 = func.call @calloc(%269, %271) : (i64, i64) -> !llvm.ptr
%272 = llvm.mlir.addressof @tmpbuf : !llvm.ptr
llvm.store %267, %272 : !llvm.ptr, !llvm.ptr
%273 = arith.constant 0 : i32
%274 = arith.extsi %273 : i32 to i64
%275 = llvm.mlir.addressof @total_points : !llvm.ptr
llvm.store %274, %275 : i64, !llvm.ptr
%277 = arith.constant 0 : i32
%278 = llvm.load %215 : !llvm.ptr -> i32
%279 = arith.constant 0 : i32
func.call @calc_points(%235, %277, %278, %279) : (!llvm.ptr, i32, i32, i32) -> ()
%280 = llvm.mlir.addressof @total_points : !llvm.ptr
%281 = llvm.load %280 : !llvm.ptr -> i64
%282 = arith.sitofp %281 : i64 to f64
%283 = llvm.load %215 : !llvm.ptr -> i32
%284 = arith.sitofp %283 : i32 to f64
%285 = arith.divf %282, %284 : f64
%286 = llvm.mlir.addressof @str_0 : !llvm.ptr
%287 = llvm.call @printf(%286, %285) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
func.call @free(%235) : (!llvm.ptr) -> ()
%290 = llvm.mlir.addressof @tmpbuf : !llvm.ptr
%291 = llvm.load %290 : !llvm.ptr -> !llvm.ptr
func.call @free(%291) : (!llvm.ptr) -> ()
%292 = arith.constant 0 : i32
func.return %292 : i32
}
}