Problem 757
Stealthy numbers: count distinct N = x(x+1)y(y+1) <= 10^14. Ported from native C to pure Flow. Sorting implemented in Flow.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^2) | ? |
| Space complexity | O(1) | ? |
| Approach | Flow solution | Not curated |
| Verdict | Unknown |
Flow source
# Project Euler 757
# Stealthy numbers: count distinct N = x(x+1)y(y+1) <= 10^14.
# Ported from native C to pure Flow. Sorting implemented in Flow.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function malloc(n: i64) -> ptr<void>
function realloc(p: ptr<void>, n: i64) -> ptr<void>
function sqrt(x: f64) -> f64
}
# In-place heapsort of arr[0..cnt).
function heapsort(arr: ptr<i64>, cnt: i64) -> void {
# Build max heap.
let mut start: i64 = cnt / 2
while start > 0 {
start = start - 1
sift_down(arr, start, cnt)
}
# Sort down.
let mut end: i64 = cnt
while end > 1 {
end = end - 1
let tmp: i64 = arr[0]
arr[0] = arr[end]
arr[end] = tmp
sift_down(arr, 0, end)
}
}
function sift_down(arr: ptr<i64>, root0: i64, end: i64) -> void {
let mut root: i64 = root0
while true {
let child: i64 = root * 2 + 1
if child >= end {
break
}
let mut c: i64 = child
if child + 1 < end && arr[child + 1] > arr[child] {
c = child + 1
}
if arr[c] <= arr[root] {
break
}
let tmp: i64 = arr[root]
arr[root] = arr[c]
arr[c] = tmp
root = c
}
}
function main() -> i32 {
let limit: i64 = 100000000000000
let mut cap: i64 = 1 << 24
let mut arr: ptr<i64> = malloc(cap * 8) as ptr<i64>
let mut cnt: i64 = 0
let mut x: i64 = 1
while true {
let xx: i64 = x * (x + 1)
if xx * xx > limit {
break
}
let rem: i64 = limit / xx
let mut ymax: i64 = ((sqrt((4 * rem + 1) as f64) - 1.0) / 2.0) as i64
while ymax * (ymax + 1) > rem {
ymax = ymax - 1
}
let mut y: i64 = x
while y <= ymax {
let n: i64 = xx * y * (y + 1)
if cnt >= cap {
cap = cap << 1
arr = realloc(arr as ptr<void>, cap * 8) as ptr<i64>
}
arr[cnt] = n
cnt = cnt + 1
y = y + 1
}
x = x + 1
}
heapsort(arr, cnt)
let mut nd: i64 = 0
let mut i: i64 = 0
while i < cnt {
nd = nd + 1
let mut j: i64 = i + 1
while j < cnt && arr[j] == arr[i] {
j = j + 1
}
i = j
}
printf("%lld\n", nd)
free(arr 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 heapsort_ptr_i64_i64(int64_t* arr, int64_t cnt);
void sift_down_ptr_i64_i64_i64(int64_t* arr, int64_t root0, int64_t end);
int32_t main(void);
void heapsort_ptr_i64_i64(int64_t* arr, int64_t cnt) {
int64_t start = FLOW_CHECKED_DIV((cnt), (2));
while (start > 0) {
start = (start - 1);
sift_down_ptr_i64_i64_i64(arr, start, cnt);
}
int64_t end = cnt;
while (end > 1) {
end = (end - 1);
int64_t tmp = arr[0];
arr[0] = arr[end];
arr[end] = tmp;
sift_down_ptr_i64_i64_i64(arr, 0, end);
}
}
void sift_down_ptr_i64_i64_i64(int64_t* arr, int64_t root0, int64_t end) {
int64_t root = root0;
while (1) {
int64_t child = ((root * 2) + 1);
if (child >= end) {
break;
}
int64_t c = child;
if (((child + 1) < end && arr[(child + 1)] > arr[child])) {
c = (child + 1);
}
if (arr[c] <= arr[root]) {
break;
}
int64_t tmp = arr[root];
arr[root] = arr[c];
arr[c] = tmp;
root = c;
}
}
int32_t main(void) {
int64_t limit = 100000000000000;
int64_t cap = FLOW_CHECKED_SHL((1), (24));
int64_t* arr = (int64_t*)(((int64_t*)(malloc((cap * 8)))));
int64_t cnt = 0;
int64_t x = 1;
while (1) {
int64_t xx = (x * (x + 1));
if ((xx * xx) > limit) {
break;
}
int64_t rem = FLOW_CHECKED_DIV((limit), (xx));
int64_t ymax = ((int64_t)(((sqrt(((double)(((4 * rem) + 1)))) - 1.0) / 2.0)));
while ((ymax * (ymax + 1)) > rem) {
ymax = (ymax - 1);
}
int64_t y = x;
while (y <= ymax) {
int64_t n = ((xx * y) * (y + 1));
if (cnt >= cap) {
cap = FLOW_CHECKED_SHL((cap), (1));
arr = ((int64_t*)(realloc(((void*)(arr)), (cap * 8))));
}
arr[cnt] = n;
cnt = (cnt + 1);
y = (y + 1);
}
x = (x + 1);
}
heapsort_ptr_i64_i64(arr, cnt);
int64_t nd = 0;
int64_t i = 0;
while (i < cnt) {
nd = (nd + 1);
int64_t j = (i + 1);
while ((j < cnt && arr[j] == arr[i])) {
j = (j + 1);
}
i = j;
}
printf("%lld\n", nd);
free(((void*)(arr)));
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 @realloc(!llvm.ptr, i64) -> !llvm.ptr
func.func private @sqrt(f64) -> f64
func.func @heapsort(%arg0: !llvm.ptr, %arg1: i64) -> () {
%0 = arith.constant 2 : i32
%2 = arith.extsi %0 : i32 to i64
%1 = arith.divsi %arg1, %2 : i64
%3 = llvm.mlir.constant(1 : i64) : i64
%4 = llvm.alloca %3 x i64 : (i64) -> !llvm.ptr
llvm.store %1, %4 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%5 = llvm.load %4 : !llvm.ptr -> i64
%6 = arith.constant 0 : i32
%8 = arith.extsi %6 : i32 to i64
%7 = arith.cmpi sgt, %5, %8 : i64
cf.cond_br %7, ^bb1, ^bb2
^bb1:
%9 = llvm.load %4 : !llvm.ptr -> i64
%10 = arith.constant 1 : i32
%12 = arith.extsi %10 : i32 to i64
%11 = arith.subi %9, %12 : i64
llvm.store %11, %4 : i64, !llvm.ptr
%14 = llvm.load %4 : !llvm.ptr -> i64
func.call @sift_down(%arg0, %14, %arg1) : (!llvm.ptr, i64, i64) -> ()
cf.br ^bb0
^bb2:
%15 = llvm.mlir.constant(1 : i64) : i64
%16 = llvm.alloca %15 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %16 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%17 = llvm.load %16 : !llvm.ptr -> i64
%18 = arith.constant 1 : i32
%20 = arith.extsi %18 : i32 to i64
%19 = arith.cmpi sgt, %17, %20 : i64
cf.cond_br %19, ^bb4, ^bb5
^bb4:
%21 = llvm.load %16 : !llvm.ptr -> i64
%22 = arith.constant 1 : i32
%24 = arith.extsi %22 : i32 to i64
%23 = arith.subi %21, %24 : i64
llvm.store %23, %16 : i64, !llvm.ptr
%26 = arith.constant 0 : i32
%27 = arith.extsi %26 : i32 to i64
%28 = llvm.getelementptr %arg0[%27] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%25 = llvm.load %28 : !llvm.ptr -> i64
%30 = llvm.load %16 : !llvm.ptr -> i64
%31 = llvm.getelementptr %arg0[%30] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%29 = llvm.load %31 : !llvm.ptr -> i64
%32 = arith.constant 0 : i32
%33 = arith.extsi %32 : i32 to i64
%34 = llvm.getelementptr %arg0[%33] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %29, %34 : i64, !llvm.ptr
%35 = llvm.load %16 : !llvm.ptr -> i64
%36 = llvm.getelementptr %arg0[%35] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %25, %36 : i64, !llvm.ptr
%38 = arith.constant 0 : i32
%39 = llvm.load %16 : !llvm.ptr -> i64
%40 = arith.extsi %38 : i32 to i64
func.call @sift_down(%arg0, %40, %39) : (!llvm.ptr, i64, i64) -> ()
cf.br ^bb3
^bb5:
func.return
}
func.func @sift_down(%arg0: !llvm.ptr, %arg1: i64, %arg2: i64) -> () {
%41 = llvm.mlir.constant(1 : i64) : i64
%42 = llvm.alloca %41 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %42 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%43 = arith.constant 1 : i1
cf.cond_br %43, ^bb7, ^bb8
^bb7:
%44 = llvm.load %42 : !llvm.ptr -> i64
%45 = arith.constant 2 : i32
%47 = arith.extsi %45 : i32 to i64
%46 = arith.muli %44, %47 : i64
%48 = arith.constant 1 : i32
%50 = arith.extsi %48 : i32 to i64
%49 = arith.addi %46, %50 : i64
%51 = arith.cmpi sge, %49, %arg2 : i64
cf.cond_br %51, ^bb9, ^bb10
^bb9:
cf.br ^bb8
^bb10:
cf.br ^bb11
^bb11:
%52 = llvm.mlir.constant(1 : i64) : i64
%53 = llvm.alloca %52 x i64 : (i64) -> !llvm.ptr
llvm.store %49, %53 : i64, !llvm.ptr
%54 = arith.constant 1 : i32
%56 = arith.extsi %54 : i32 to i64
%55 = arith.addi %49, %56 : i64
%57 = arith.cmpi slt, %55, %arg2 : i64
%58 = scf.if %57 -> (i1) {
%60 = arith.constant 1 : i32
%62 = arith.extsi %60 : i32 to i64
%61 = arith.addi %49, %62 : i64
%63 = llvm.getelementptr %arg0[%61] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%59 = llvm.load %63 : !llvm.ptr -> i64
%65 = llvm.getelementptr %arg0[%49] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%64 = llvm.load %65 : !llvm.ptr -> i64
%66 = arith.cmpi sgt, %59, %64 : i64
scf.yield %66 : i1
} else {
%67 = arith.constant false
scf.yield %67 : i1
}
cf.cond_br %58, ^bb12, ^bb13
^bb12:
%68 = arith.constant 1 : i32
%70 = arith.extsi %68 : i32 to i64
%69 = arith.addi %49, %70 : i64
llvm.store %69, %53 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%72 = llvm.load %53 : !llvm.ptr -> i64
%73 = llvm.getelementptr %arg0[%72] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%71 = llvm.load %73 : !llvm.ptr -> i64
%75 = llvm.load %42 : !llvm.ptr -> i64
%76 = llvm.getelementptr %arg0[%75] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%74 = llvm.load %76 : !llvm.ptr -> i64
%77 = arith.cmpi sle, %71, %74 : i64
cf.cond_br %77, ^bb15, ^bb16
^bb15:
cf.br ^bb8
^bb16:
cf.br ^bb17
^bb17:
%79 = llvm.load %42 : !llvm.ptr -> i64
%80 = llvm.getelementptr %arg0[%79] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%78 = llvm.load %80 : !llvm.ptr -> i64
%82 = llvm.load %53 : !llvm.ptr -> i64
%83 = llvm.getelementptr %arg0[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%81 = llvm.load %83 : !llvm.ptr -> i64
%84 = llvm.load %42 : !llvm.ptr -> i64
%85 = llvm.getelementptr %arg0[%84] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %81, %85 : i64, !llvm.ptr
%86 = llvm.load %53 : !llvm.ptr -> i64
%87 = llvm.getelementptr %arg0[%86] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %78, %87 : i64, !llvm.ptr
%88 = llvm.load %53 : !llvm.ptr -> i64
llvm.store %88, %42 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
func.return
}
func.func @main() -> i32 {
%89 = arith.constant 99995705032704 : i32
%90 = arith.extsi %89 : i32 to i64
%91 = arith.constant 1 : i32
%92 = arith.constant 24 : i32
%93 = arith.shli %91, %92 : i32
%94 = arith.extsi %93 : i32 to i64
%95 = llvm.mlir.constant(1 : i64) : i64
%96 = llvm.alloca %95 x i64 : (i64) -> !llvm.ptr
llvm.store %94, %96 : i64, !llvm.ptr
%98 = llvm.load %96 : !llvm.ptr -> i64
%99 = arith.constant 8 : i32
%101 = arith.extsi %99 : i32 to i64
%100 = arith.muli %98, %101 : i64
%97 = func.call @malloc(%100) : (i64) -> !llvm.ptr
%102 = llvm.mlir.constant(1 : i64) : i64
%103 = llvm.alloca %102 x !llvm.ptr : (i64) -> !llvm.ptr
llvm.store %97, %103 : !llvm.ptr, !llvm.ptr
%104 = arith.constant 0 : i32
%105 = arith.extsi %104 : i32 to i64
%106 = llvm.mlir.constant(1 : i64) : i64
%107 = llvm.alloca %106 x i64 : (i64) -> !llvm.ptr
llvm.store %105, %107 : i64, !llvm.ptr
%108 = arith.constant 1 : i32
%109 = arith.extsi %108 : i32 to i64
%110 = llvm.mlir.constant(1 : i64) : i64
%111 = llvm.alloca %110 x i64 : (i64) -> !llvm.ptr
llvm.store %109, %111 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%112 = arith.constant 1 : i1
cf.cond_br %112, ^bb19, ^bb20
^bb19:
%113 = llvm.load %111 : !llvm.ptr -> i64
%114 = llvm.load %111 : !llvm.ptr -> i64
%115 = arith.constant 1 : i32
%117 = arith.extsi %115 : i32 to i64
%116 = arith.addi %114, %117 : i64
%118 = arith.muli %113, %116 : i64
%119 = arith.muli %118, %118 : i64
%120 = arith.cmpi sgt, %119, %90 : i64
cf.cond_br %120, ^bb21, ^bb22
^bb21:
cf.br ^bb20
^bb22:
cf.br ^bb23
^bb23:
%121 = arith.divsi %90, %118 : i64
%122 = arith.constant 4 : i32
%124 = arith.extsi %122 : i32 to i64
%123 = arith.muli %124, %121 : i64
%125 = arith.constant 1 : i32
%127 = arith.extsi %125 : i32 to i64
%126 = arith.addi %123, %127 : i64
%128 = arith.sitofp %126 : i64 to f64
%129 = math.sqrt %128 : f64
%130 = arith.constant 1.0 : f32
%132 = arith.extf %130 : f32 to f64
%131 = arith.subf %129, %132 : f64
%133 = arith.constant 2.0 : f32
%135 = arith.extf %133 : f32 to f64
%134 = arith.divf %131, %135 : f64
%136 = arith.fptosi %134 : f64 to i64
%137 = llvm.mlir.constant(1 : i64) : i64
%138 = llvm.alloca %137 x i64 : (i64) -> !llvm.ptr
llvm.store %136, %138 : i64, !llvm.ptr
cf.br ^bb24
^bb24:
%139 = llvm.load %138 : !llvm.ptr -> i64
%140 = llvm.load %138 : !llvm.ptr -> i64
%141 = arith.constant 1 : i32
%143 = arith.extsi %141 : i32 to i64
%142 = arith.addi %140, %143 : i64
%144 = arith.muli %139, %142 : i64
%145 = arith.cmpi sgt, %144, %121 : i64
cf.cond_br %145, ^bb25, ^bb26
^bb25:
%146 = llvm.load %138 : !llvm.ptr -> i64
%147 = arith.constant 1 : i32
%149 = arith.extsi %147 : i32 to i64
%148 = arith.subi %146, %149 : i64
llvm.store %148, %138 : i64, !llvm.ptr
cf.br ^bb24
^bb26:
%150 = llvm.load %111 : !llvm.ptr -> i64
%151 = llvm.mlir.constant(1 : i64) : i64
%152 = llvm.alloca %151 x i64 : (i64) -> !llvm.ptr
llvm.store %150, %152 : i64, !llvm.ptr
cf.br ^bb27
^bb27:
%153 = llvm.load %152 : !llvm.ptr -> i64
%154 = llvm.load %138 : !llvm.ptr -> i64
%155 = arith.cmpi sle, %153, %154 : i64
cf.cond_br %155, ^bb28, ^bb29
^bb28:
%156 = llvm.load %152 : !llvm.ptr -> i64
%157 = arith.muli %118, %156 : i64
%158 = llvm.load %152 : !llvm.ptr -> i64
%159 = arith.constant 1 : i32
%161 = arith.extsi %159 : i32 to i64
%160 = arith.addi %158, %161 : i64
%162 = arith.muli %157, %160 : i64
%163 = llvm.load %107 : !llvm.ptr -> i64
%164 = llvm.load %96 : !llvm.ptr -> i64
%165 = arith.cmpi sge, %163, %164 : i64
cf.cond_br %165, ^bb30, ^bb31
^bb30:
%166 = llvm.load %96 : !llvm.ptr -> i64
%167 = arith.constant 1 : i32
%169 = arith.extsi %167 : i32 to i64
%168 = arith.shli %166, %169 : i64
llvm.store %168, %96 : i64, !llvm.ptr
%171 = llvm.load %103 : !llvm.ptr -> !llvm.ptr
%172 = llvm.load %96 : !llvm.ptr -> i64
%173 = arith.constant 8 : i32
%175 = arith.extsi %173 : i32 to i64
%174 = arith.muli %172, %175 : i64
%170 = func.call @realloc(%171, %174) : (!llvm.ptr, i64) -> !llvm.ptr
llvm.store %170, %103 : !llvm.ptr, !llvm.ptr
cf.br ^bb32
^bb31:
cf.br ^bb32
^bb32:
%176 = llvm.load %103 : !llvm.ptr -> !llvm.ptr
%177 = llvm.load %107 : !llvm.ptr -> i64
%178 = llvm.getelementptr %176[%177] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %162, %178 : i64, !llvm.ptr
%179 = llvm.load %107 : !llvm.ptr -> i64
%180 = arith.constant 1 : i32
%182 = arith.extsi %180 : i32 to i64
%181 = arith.addi %179, %182 : i64
llvm.store %181, %107 : i64, !llvm.ptr
%183 = llvm.load %152 : !llvm.ptr -> i64
%184 = arith.constant 1 : i32
%186 = arith.extsi %184 : i32 to i64
%185 = arith.addi %183, %186 : i64
llvm.store %185, %152 : i64, !llvm.ptr
cf.br ^bb27
^bb29:
%187 = llvm.load %111 : !llvm.ptr -> i64
%188 = arith.constant 1 : i32
%190 = arith.extsi %188 : i32 to i64
%189 = arith.addi %187, %190 : i64
llvm.store %189, %111 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
%192 = llvm.load %103 : !llvm.ptr -> !llvm.ptr
%193 = llvm.load %107 : !llvm.ptr -> i64
func.call @heapsort(%192, %193) : (!llvm.ptr, i64) -> ()
%194 = arith.constant 0 : i32
%195 = arith.extsi %194 : i32 to i64
%196 = llvm.mlir.constant(1 : i64) : i64
%197 = llvm.alloca %196 x i64 : (i64) -> !llvm.ptr
llvm.store %195, %197 : i64, !llvm.ptr
%198 = arith.constant 0 : i32
%199 = arith.extsi %198 : i32 to i64
%200 = llvm.mlir.constant(1 : i64) : i64
%201 = llvm.alloca %200 x i64 : (i64) -> !llvm.ptr
llvm.store %199, %201 : i64, !llvm.ptr
cf.br ^bb33
^bb33:
%202 = llvm.load %201 : !llvm.ptr -> i64
%203 = llvm.load %107 : !llvm.ptr -> i64
%204 = arith.cmpi slt, %202, %203 : i64
cf.cond_br %204, ^bb34, ^bb35
^bb34:
%205 = llvm.load %197 : !llvm.ptr -> i64
%206 = arith.constant 1 : i32
%208 = arith.extsi %206 : i32 to i64
%207 = arith.addi %205, %208 : i64
llvm.store %207, %197 : i64, !llvm.ptr
%209 = llvm.load %201 : !llvm.ptr -> i64
%210 = arith.constant 1 : i32
%212 = arith.extsi %210 : i32 to i64
%211 = arith.addi %209, %212 : i64
%213 = llvm.mlir.constant(1 : i64) : i64
%214 = llvm.alloca %213 x i64 : (i64) -> !llvm.ptr
llvm.store %211, %214 : i64, !llvm.ptr
cf.br ^bb36
^bb36:
%215 = llvm.load %214 : !llvm.ptr -> i64
%216 = llvm.load %107 : !llvm.ptr -> i64
%217 = arith.cmpi slt, %215, %216 : i64
%218 = scf.if %217 -> (i1) {
%220 = llvm.load %103 : !llvm.ptr -> !llvm.ptr
%221 = llvm.load %214 : !llvm.ptr -> i64
%222 = llvm.getelementptr %220[%221] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%219 = llvm.load %222 : !llvm.ptr -> i64
%224 = llvm.load %103 : !llvm.ptr -> !llvm.ptr
%225 = llvm.load %201 : !llvm.ptr -> i64
%226 = llvm.getelementptr %224[%225] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%223 = llvm.load %226 : !llvm.ptr -> i64
%227 = arith.cmpi eq, %219, %223 : i64
scf.yield %227 : i1
} else {
%228 = arith.constant false
scf.yield %228 : i1
}
cf.cond_br %218, ^bb37, ^bb38
^bb37:
%229 = llvm.load %214 : !llvm.ptr -> i64
%230 = arith.constant 1 : i32
%232 = arith.extsi %230 : i32 to i64
%231 = arith.addi %229, %232 : i64
llvm.store %231, %214 : i64, !llvm.ptr
cf.br ^bb36
^bb38:
%233 = llvm.load %214 : !llvm.ptr -> i64
llvm.store %233, %201 : i64, !llvm.ptr
cf.br ^bb33
^bb35:
%234 = llvm.mlir.addressof @str_0 : !llvm.ptr
%235 = llvm.load %197 : !llvm.ptr -> i64
%236 = llvm.call @printf(%234, %235) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%238 = llvm.load %103 : !llvm.ptr -> !llvm.ptr
func.call @free(%238) : (!llvm.ptr) -> ()
%239 = arith.constant 0 : i32
func.return %239 : i32
}
}