Problem 155
Distinct capacitances from series/parallel networks of <= 18 unit capacitors.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^2) | O(e log v) |
| Space complexity | O(n^2) | O(v) |
| Approach | Flow solution | Kruskal or Prim algorithm |
| Verdict | Unknown |
Flow source
# Project Euler 155
# Distinct capacitances from series/parallel networks of <= 18 unit capacitors.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function gcd_abs(a0: i64, b0: i64) -> i64 {
let mut a: i64 = a0
if a < 0 { a = -a }
let mut b: i64 = b0
if b < 0 { b = -b }
while b != 0 {
let t: i64 = a % b
a = b
b = t
}
return a
}
function set_insert(nums: ptr<i64>, dens: ptr<i64>, used: ptr<i8>, cap: i64, num0: i64, den0: i64) -> bool {
let g: i64 = gcd_abs(num0, den0)
let mut n: i64 = num0 / g
let mut d: i64 = den0 / g
if d < 0 { n = -n; d = -d }
let mut slot: i64 = n * 11400714819323198485 + d * 14029467366897019727
if slot < 0 { slot = -slot }
slot = slot & (cap - 1)
let start: i64 = slot
while used[slot] != 0 {
if nums[slot] == n && dens[slot] == d { return false }
slot = (slot + 1) & (cap - 1)
if slot == start { return false }
}
used[slot] = 1
nums[slot] = n
dens[slot] = d
return true
}
function main() -> i32 {
let limit: i32 = 18
let pool: i64 = 7000000
let nums: ptr<i64> = calloc(pool, 8)
let dens: ptr<i64> = calloc(pool, 8)
let offsets: ptr<i64> = calloc((limit + 2) as i64, 8)
let counts: ptr<i32> = calloc((limit + 1) as i64, 4)
if nums == null || dens == null || offsets == null || counts == null { return 1 }
let scap: i64 = 16777216
let snum: ptr<i64> = calloc(scap, 8)
let sden: ptr<i64> = calloc(scap, 8)
let sused: ptr<i8> = calloc(scap, 1)
if snum == null || sden == null || sused == null { return 1 }
nums[0] = 1
dens[0] = 1
counts[1] = 1
offsets[1] = 0
offsets[2] = 1
set_insert(snum, sden, sused, scap, 1, 1)
let mut total: i64 = 1
let mut next_free: i64 = 1
let mut size: i32 = 2
while size <= limit {
let tcap: i64 = 16777216
let tnum: ptr<i64> = calloc(tcap, 8)
let tden: ptr<i64> = calloc(tcap, 8)
let tused: ptr<i8> = calloc(tcap, 1)
if tnum == null || tden == null || tused == null { return 1 }
offsets[size] = next_free
let mut tcount: i32 = 0
let mut left: i32 = 1
while left <= size / 2 {
let right: i32 = size - left
let lb: i64 = offsets[left]
let rb: i64 = offsets[right]
let lc: i32 = counts[left]
let rc: i32 = counts[right]
let mut i: i32 = 0
while i < lc {
let mut j: i32 = 0
if left == right { j = i } else { j = 0 }
while j < rc {
let a0: i64 = nums[lb + (i as i64)]
let a1: i64 = dens[lb + (i as i64)]
let b0: i64 = nums[rb + (j as i64)]
let b1: i64 = dens[rb + (j as i64)]
let num: i64 = a0 * b1 + b0 * a1
let den: i64 = a1 * b1
if set_insert(tnum, tden, tused, tcap, num, den) {
let g: i64 = gcd_abs(num, den)
let mut nn2: i64 = num / g
let mut dd2: i64 = den / g
if dd2 < 0 {
nn2 = -nn2
dd2 = -dd2
}
nums[next_free] = nn2
dens[next_free] = dd2
next_free = next_free + 1
tcount = tcount + 1
if set_insert(snum, sden, sused, scap, nn2, dd2) {
total = total + 1
}
}
if set_insert(tnum, tden, tused, tcap, den, num) {
let g2: i64 = gcd_abs(den, num)
let mut nn3: i64 = den / g2
let mut dd3: i64 = num / g2
if dd3 < 0 {
nn3 = -nn3
dd3 = -dd3
}
nums[next_free] = nn3
dens[next_free] = dd3
next_free = next_free + 1
tcount = tcount + 1
if set_insert(snum, sden, sused, scap, nn3, dd3) {
total = total + 1
}
}
j = j + 1
}
i = i + 1
}
left = left + 1
}
counts[size] = tcount
offsets[size + 1] = next_free
free(tnum)
free(tden)
free(tused)
size = size + 1
}
printf("%lld\n", total)
free(nums)
free(dens)
free(offsets)
free(counts)
free(snum)
free(sden)
free(sused)
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 gcd_abs_i64_i64(int64_t a0, int64_t b0);
bool set_insert_ptr_i64_ptr_i64_ptr_i8_i64_i64_i64(int64_t* nums, int64_t* dens, int8_t* used, int64_t cap, int64_t num0, int64_t den0);
int32_t main(void);
int64_t gcd_abs_i64_i64(int64_t a0, int64_t b0) {
int64_t a = a0;
if (a < 0) {
a = (-a);
}
int64_t b = b0;
if (b < 0) {
b = (-b);
}
while (b != 0) {
int64_t t = FLOW_CHECKED_MOD((a), (b));
a = b;
b = t;
}
return a;
}
bool set_insert_ptr_i64_ptr_i64_ptr_i8_i64_i64_i64(int64_t* nums, int64_t* dens, int8_t* used, int64_t cap, int64_t num0, int64_t den0) {
int64_t g = gcd_abs_i64_i64(num0, den0);
int64_t n = FLOW_CHECKED_DIV((num0), (g));
int64_t d = FLOW_CHECKED_DIV((den0), (g));
if (d < 0) {
n = (-n);
d = (-d);
}
int64_t slot = ((n * ((__int128)0x9E3779B97F4A7C15ULL)) + (d * ((__int128)0xC2B2AE3D27D4EB4FULL)));
if (slot < 0) {
slot = (-slot);
}
slot = (slot & (cap - 1));
int64_t start = slot;
while (used[slot] != 0) {
if ((nums[slot] == n && dens[slot] == d)) {
return 0;
}
slot = ((slot + 1) & (cap - 1));
if (slot == start) {
return 0;
}
}
used[slot] = 1;
nums[slot] = n;
dens[slot] = d;
return 1;
}
int32_t main(void) {
int32_t limit = 18;
int64_t pool = 7000000;
int64_t* nums = (int64_t*)(calloc(pool, 8));
int64_t* dens = (int64_t*)(calloc(pool, 8));
int64_t* offsets = (int64_t*)(calloc(((int64_t)((limit + 2))), 8));
int32_t* counts = (int32_t*)(calloc(((int64_t)((limit + 1))), 4));
if ((((nums == NULL || dens == NULL) || offsets == NULL) || counts == NULL)) {
return 1;
}
int64_t scap = 16777216;
int64_t* snum = (int64_t*)(calloc(scap, 8));
int64_t* sden = (int64_t*)(calloc(scap, 8));
int8_t* sused = (int8_t*)(calloc(scap, 1));
if (((snum == NULL || sden == NULL) || sused == NULL)) {
return 1;
}
nums[0] = 1;
dens[0] = 1;
counts[1] = 1;
offsets[1] = 0;
offsets[2] = 1;
set_insert_ptr_i64_ptr_i64_ptr_i8_i64_i64_i64(snum, sden, sused, scap, 1, 1);
int64_t total = 1;
int64_t next_free = 1;
int32_t size = 2;
while (size <= limit) {
int64_t tcap = 16777216;
int64_t* tnum = (int64_t*)(calloc(tcap, 8));
int64_t* tden = (int64_t*)(calloc(tcap, 8));
int8_t* tused = (int8_t*)(calloc(tcap, 1));
if (((tnum == NULL || tden == NULL) || tused == NULL)) {
return 1;
}
offsets[size] = next_free;
int32_t tcount = 0;
int32_t left = 1;
while (left <= FLOW_CHECKED_DIV((size), (2))) {
int32_t right = (size - left);
int64_t lb = offsets[left];
int64_t rb = offsets[right];
int32_t lc = counts[left];
int32_t rc = counts[right];
int32_t i = 0;
while (i < lc) {
int32_t j = 0;
if (left == right) {
j = i;
} else {
j = 0;
}
while (j < rc) {
int64_t a0 = nums[(lb + ((int64_t)(i)))];
int64_t a1 = dens[(lb + ((int64_t)(i)))];
int64_t b0 = nums[(rb + ((int64_t)(j)))];
int64_t b1 = dens[(rb + ((int64_t)(j)))];
int64_t num = ((a0 * b1) + (b0 * a1));
int64_t den = (a1 * b1);
if (set_insert_ptr_i64_ptr_i64_ptr_i8_i64_i64_i64(tnum, tden, tused, tcap, num, den)) {
int64_t g = gcd_abs_i64_i64(num, den);
int64_t nn2 = FLOW_CHECKED_DIV((num), (g));
int64_t dd2 = FLOW_CHECKED_DIV((den), (g));
if (dd2 < 0) {
nn2 = (-nn2);
dd2 = (-dd2);
}
nums[next_free] = nn2;
dens[next_free] = dd2;
next_free = (next_free + 1);
tcount = (tcount + 1);
if (set_insert_ptr_i64_ptr_i64_ptr_i8_i64_i64_i64(snum, sden, sused, scap, nn2, dd2)) {
total = (total + 1);
}
}
if (set_insert_ptr_i64_ptr_i64_ptr_i8_i64_i64_i64(tnum, tden, tused, tcap, den, num)) {
int64_t g2 = gcd_abs_i64_i64(den, num);
int64_t nn3 = FLOW_CHECKED_DIV((den), (g2));
int64_t dd3 = FLOW_CHECKED_DIV((num), (g2));
if (dd3 < 0) {
nn3 = (-nn3);
dd3 = (-dd3);
}
nums[next_free] = nn3;
dens[next_free] = dd3;
next_free = (next_free + 1);
tcount = (tcount + 1);
if (set_insert_ptr_i64_ptr_i64_ptr_i8_i64_i64_i64(snum, sden, sused, scap, nn3, dd3)) {
total = (total + 1);
}
}
j = (j + 1);
}
i = (i + 1);
}
left = (left + 1);
}
counts[size] = tcount;
offsets[(size + 1)] = next_free;
free(tnum);
free(tden);
free(tused);
size = (size + 1);
}
printf("%lld\n", total);
free(nums);
free(dens);
free(offsets);
free(counts);
free(snum);
free(sden);
free(sused);
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 @gcd_abs(%arg0: i64, %arg1: i64) -> i64 {
%0 = llvm.mlir.constant(1 : i64) : i64
%1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %1 : i64, !llvm.ptr
%2 = llvm.load %1 : !llvm.ptr -> i64
%3 = arith.constant 0 : i32
%5 = arith.extsi %3 : i32 to i64
%4 = arith.cmpi slt, %2, %5 : i64
cf.cond_br %4, ^bb0, ^bb1
^bb0:
%6 = llvm.load %1 : !llvm.ptr -> i64
%8 = arith.constant 0 : i64
%7 = arith.subi %8, %6 : i64
llvm.store %7, %1 : i64, !llvm.ptr
cf.br ^bb2
^bb1:
cf.br ^bb2
^bb2:
%9 = llvm.mlir.constant(1 : i64) : i64
%10 = llvm.alloca %9 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %10 : i64, !llvm.ptr
%11 = llvm.load %10 : !llvm.ptr -> i64
%12 = arith.constant 0 : i32
%14 = arith.extsi %12 : i32 to i64
%13 = arith.cmpi slt, %11, %14 : i64
cf.cond_br %13, ^bb3, ^bb4
^bb3:
%15 = llvm.load %10 : !llvm.ptr -> i64
%17 = arith.constant 0 : i64
%16 = arith.subi %17, %15 : i64
llvm.store %16, %10 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
cf.br ^bb6
^bb6:
%18 = llvm.load %10 : !llvm.ptr -> i64
%19 = arith.constant 0 : i32
%21 = arith.extsi %19 : i32 to i64
%20 = arith.cmpi ne, %18, %21 : i64
cf.cond_br %20, ^bb7, ^bb8
^bb7:
%22 = llvm.load %1 : !llvm.ptr -> i64
%23 = llvm.load %10 : !llvm.ptr -> i64
%24 = arith.remsi %22, %23 : i64
%25 = llvm.load %10 : !llvm.ptr -> i64
llvm.store %25, %1 : i64, !llvm.ptr
llvm.store %24, %10 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%26 = llvm.load %1 : !llvm.ptr -> i64
func.return %26 : i64
}
func.func @set_insert(%arg0: !llvm.ptr, %arg1: !llvm.ptr, %arg2: !llvm.ptr, %arg3: i64, %arg4: i64, %arg5: i64) -> i1 {
%27 = func.call @gcd_abs(%arg4, %arg5) : (i64, i64) -> i64
%28 = arith.divsi %arg4, %27 : i64
%29 = llvm.mlir.constant(1 : i64) : i64
%30 = llvm.alloca %29 x i64 : (i64) -> !llvm.ptr
llvm.store %28, %30 : i64, !llvm.ptr
%31 = arith.divsi %arg5, %27 : i64
%32 = llvm.mlir.constant(1 : i64) : i64
%33 = llvm.alloca %32 x i64 : (i64) -> !llvm.ptr
llvm.store %31, %33 : i64, !llvm.ptr
%34 = llvm.load %33 : !llvm.ptr -> i64
%35 = arith.constant 0 : i32
%37 = arith.extsi %35 : i32 to i64
%36 = arith.cmpi slt, %34, %37 : i64
cf.cond_br %36, ^bb9, ^bb10
^bb9:
%38 = llvm.load %30 : !llvm.ptr -> i64
%40 = arith.constant 0 : i64
%39 = arith.subi %40, %38 : i64
llvm.store %39, %30 : i64, !llvm.ptr
%41 = llvm.load %33 : !llvm.ptr -> i64
%43 = arith.constant 0 : i64
%42 = arith.subi %43, %41 : i64
llvm.store %42, %33 : i64, !llvm.ptr
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%44 = llvm.load %30 : !llvm.ptr -> i64
%45 = arith.constant 11400714815028231189 : i32
%47 = arith.extsi %45 : i32 to i64
%46 = arith.muli %44, %47 : i64
%48 = llvm.load %33 : !llvm.ptr -> i64
%49 = arith.constant 14029467362602052431 : i32
%51 = arith.extsi %49 : i32 to i64
%50 = arith.muli %48, %51 : i64
%52 = arith.addi %46, %50 : i64
%53 = llvm.mlir.constant(1 : i64) : i64
%54 = llvm.alloca %53 x i64 : (i64) -> !llvm.ptr
llvm.store %52, %54 : i64, !llvm.ptr
%55 = llvm.load %54 : !llvm.ptr -> i64
%56 = arith.constant 0 : i32
%58 = arith.extsi %56 : i32 to i64
%57 = arith.cmpi slt, %55, %58 : i64
cf.cond_br %57, ^bb12, ^bb13
^bb12:
%59 = llvm.load %54 : !llvm.ptr -> i64
%61 = arith.constant 0 : i64
%60 = arith.subi %61, %59 : i64
llvm.store %60, %54 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%62 = llvm.load %54 : !llvm.ptr -> i64
%63 = arith.constant 1 : i32
%65 = arith.extsi %63 : i32 to i64
%64 = arith.subi %arg3, %65 : i64
%66 = arith.andi %62, %64 : i64
llvm.store %66, %54 : i64, !llvm.ptr
%67 = llvm.load %54 : !llvm.ptr -> i64
cf.br ^bb15
^bb15:
%69 = llvm.load %54 : !llvm.ptr -> i64
%70 = llvm.getelementptr %arg2[%69] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%68 = llvm.load %70 : !llvm.ptr -> i8
%71 = arith.constant 0 : i32
%73 = arith.extsi %68 : i8 to i32
%72 = arith.cmpi ne, %73, %71 : i32
cf.cond_br %72, ^bb16, ^bb17
^bb16:
%75 = llvm.load %54 : !llvm.ptr -> i64
%76 = llvm.getelementptr %arg0[%75] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%74 = llvm.load %76 : !llvm.ptr -> i64
%77 = llvm.load %30 : !llvm.ptr -> i64
%78 = arith.cmpi eq, %74, %77 : i64
%79 = scf.if %78 -> (i1) {
%81 = llvm.load %54 : !llvm.ptr -> i64
%82 = llvm.getelementptr %arg1[%81] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%80 = llvm.load %82 : !llvm.ptr -> i64
%83 = llvm.load %33 : !llvm.ptr -> i64
%84 = arith.cmpi eq, %80, %83 : i64
scf.yield %84 : i1
} else {
%85 = arith.constant false
scf.yield %85 : i1
}
cf.cond_br %79, ^bb18, ^bb19
^bb18:
%86 = arith.constant 0 : i1
func.return %86 : i1
^bb19:
cf.br ^bb20
^bb20:
%87 = llvm.load %54 : !llvm.ptr -> i64
%88 = arith.constant 1 : i32
%90 = arith.extsi %88 : i32 to i64
%89 = arith.addi %87, %90 : i64
%91 = arith.constant 1 : i32
%93 = arith.extsi %91 : i32 to i64
%92 = arith.subi %arg3, %93 : i64
%94 = arith.andi %89, %92 : i64
llvm.store %94, %54 : i64, !llvm.ptr
%95 = llvm.load %54 : !llvm.ptr -> i64
%96 = arith.cmpi eq, %95, %67 : i64
cf.cond_br %96, ^bb21, ^bb22
^bb21:
%97 = arith.constant 0 : i1
func.return %97 : i1
^bb22:
cf.br ^bb23
^bb23:
cf.br ^bb15
^bb17:
%98 = arith.constant 1 : i32
%99 = llvm.load %54 : !llvm.ptr -> i64
%100 = arith.trunci %98 : i32 to i8
%101 = llvm.getelementptr %arg2[%99] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %100, %101 : i8, !llvm.ptr
%102 = llvm.load %30 : !llvm.ptr -> i64
%103 = llvm.load %54 : !llvm.ptr -> i64
%104 = llvm.getelementptr %arg0[%103] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %102, %104 : i64, !llvm.ptr
%105 = llvm.load %33 : !llvm.ptr -> i64
%106 = llvm.load %54 : !llvm.ptr -> i64
%107 = llvm.getelementptr %arg1[%106] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %105, %107 : i64, !llvm.ptr
%108 = arith.constant 1 : i1
func.return %108 : i1
}
func.func @main() -> i32 {
%109 = arith.constant 18 : i32
%110 = arith.constant 7000000 : i32
%111 = arith.extsi %110 : i32 to i64
%113 = arith.constant 8 : i32
%114 = arith.extsi %113 : i32 to i64
%112 = func.call @calloc(%111, %114) : (i64, i64) -> !llvm.ptr
%116 = arith.constant 8 : i32
%117 = arith.extsi %116 : i32 to i64
%115 = func.call @calloc(%111, %117) : (i64, i64) -> !llvm.ptr
%119 = arith.constant 2 : i32
%120 = arith.addi %109, %119 : i32
%121 = arith.extsi %120 : i32 to i64
%122 = arith.constant 8 : i32
%123 = arith.extsi %122 : i32 to i64
%118 = func.call @calloc(%121, %123) : (i64, i64) -> !llvm.ptr
%125 = arith.constant 1 : i32
%126 = arith.addi %109, %125 : i32
%127 = arith.extsi %126 : i32 to i64
%128 = arith.constant 4 : i32
%129 = arith.extsi %128 : i32 to i64
%124 = func.call @calloc(%127, %129) : (i64, i64) -> !llvm.ptr
%130 = llvm.mlir.zero : !llvm.ptr
%131 = llvm.icmp "eq" %112, %130 : !llvm.ptr
%132 = scf.if %131 -> (i1) {
%133 = arith.constant true
scf.yield %133 : i1
} else {
%134 = llvm.mlir.zero : !llvm.ptr
%135 = llvm.icmp "eq" %115, %134 : !llvm.ptr
scf.yield %135 : i1
}
%136 = scf.if %132 -> (i1) {
%137 = arith.constant true
scf.yield %137 : i1
} else {
%138 = llvm.mlir.zero : !llvm.ptr
%139 = llvm.icmp "eq" %118, %138 : !llvm.ptr
scf.yield %139 : i1
}
%140 = scf.if %136 -> (i1) {
%141 = arith.constant true
scf.yield %141 : i1
} else {
%142 = llvm.mlir.zero : !llvm.ptr
%143 = llvm.icmp "eq" %124, %142 : !llvm.ptr
scf.yield %143 : i1
}
cf.cond_br %140, ^bb24, ^bb25
^bb24:
%144 = arith.constant 1 : i32
func.return %144 : i32
^bb25:
cf.br ^bb26
^bb26:
%145 = arith.constant 16777216 : i32
%146 = arith.extsi %145 : i32 to i64
%148 = arith.constant 8 : i32
%149 = arith.extsi %148 : i32 to i64
%147 = func.call @calloc(%146, %149) : (i64, i64) -> !llvm.ptr
%151 = arith.constant 8 : i32
%152 = arith.extsi %151 : i32 to i64
%150 = func.call @calloc(%146, %152) : (i64, i64) -> !llvm.ptr
%154 = arith.constant 1 : i32
%155 = arith.extsi %154 : i32 to i64
%153 = func.call @calloc(%146, %155) : (i64, i64) -> !llvm.ptr
%156 = llvm.mlir.zero : !llvm.ptr
%157 = llvm.icmp "eq" %147, %156 : !llvm.ptr
%158 = scf.if %157 -> (i1) {
%159 = arith.constant true
scf.yield %159 : i1
} else {
%160 = llvm.mlir.zero : !llvm.ptr
%161 = llvm.icmp "eq" %150, %160 : !llvm.ptr
scf.yield %161 : i1
}
%162 = scf.if %158 -> (i1) {
%163 = arith.constant true
scf.yield %163 : i1
} else {
%164 = llvm.mlir.zero : !llvm.ptr
%165 = llvm.icmp "eq" %153, %164 : !llvm.ptr
scf.yield %165 : i1
}
cf.cond_br %162, ^bb27, ^bb28
^bb27:
%166 = arith.constant 1 : i32
func.return %166 : i32
^bb28:
cf.br ^bb29
^bb29:
%167 = arith.constant 1 : i32
%168 = arith.constant 0 : i32
%169 = arith.extsi %167 : i32 to i64
%170 = arith.extsi %168 : i32 to i64
%171 = llvm.getelementptr %112[%170] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %169, %171 : i64, !llvm.ptr
%172 = arith.constant 1 : i32
%173 = arith.constant 0 : i32
%174 = arith.extsi %172 : i32 to i64
%175 = arith.extsi %173 : i32 to i64
%176 = llvm.getelementptr %115[%175] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %174, %176 : i64, !llvm.ptr
%177 = arith.constant 1 : i32
%178 = arith.constant 1 : i32
%179 = arith.extsi %178 : i32 to i64
%180 = llvm.getelementptr %124[%179] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %177, %180 : i32, !llvm.ptr
%181 = arith.constant 0 : i32
%182 = arith.constant 1 : i32
%183 = arith.extsi %181 : i32 to i64
%184 = arith.extsi %182 : i32 to i64
%185 = llvm.getelementptr %118[%184] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %183, %185 : i64, !llvm.ptr
%186 = arith.constant 1 : i32
%187 = arith.constant 2 : i32
%188 = arith.extsi %186 : i32 to i64
%189 = arith.extsi %187 : i32 to i64
%190 = llvm.getelementptr %118[%189] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %188, %190 : i64, !llvm.ptr
%192 = arith.constant 1 : i32
%193 = arith.constant 1 : i32
%194 = arith.extsi %192 : i32 to i64
%195 = arith.extsi %193 : i32 to i64
%191 = func.call @set_insert(%147, %150, %153, %146, %194, %195) : (!llvm.ptr, !llvm.ptr, !llvm.ptr, i64, i64, i64) -> i1
%196 = arith.constant 1 : i32
%197 = arith.extsi %196 : i32 to i64
%198 = llvm.mlir.constant(1 : i64) : i64
%199 = llvm.alloca %198 x i64 : (i64) -> !llvm.ptr
llvm.store %197, %199 : i64, !llvm.ptr
%200 = arith.constant 1 : i32
%201 = arith.extsi %200 : i32 to i64
%202 = llvm.mlir.constant(1 : i64) : i64
%203 = llvm.alloca %202 x i64 : (i64) -> !llvm.ptr
llvm.store %201, %203 : i64, !llvm.ptr
%204 = arith.constant 2 : i32
%205 = llvm.mlir.constant(1 : i64) : i64
%206 = llvm.alloca %205 x i32 : (i64) -> !llvm.ptr
llvm.store %204, %206 : i32, !llvm.ptr
cf.br ^bb30
^bb30:
%207 = llvm.load %206 : !llvm.ptr -> i32
%208 = arith.cmpi sle, %207, %109 : i32
cf.cond_br %208, ^bb31, ^bb32
^bb31:
%209 = arith.constant 16777216 : i32
%210 = arith.extsi %209 : i32 to i64
%212 = arith.constant 8 : i32
%213 = arith.extsi %212 : i32 to i64
%211 = func.call @calloc(%210, %213) : (i64, i64) -> !llvm.ptr
%215 = arith.constant 8 : i32
%216 = arith.extsi %215 : i32 to i64
%214 = func.call @calloc(%210, %216) : (i64, i64) -> !llvm.ptr
%218 = arith.constant 1 : i32
%219 = arith.extsi %218 : i32 to i64
%217 = func.call @calloc(%210, %219) : (i64, i64) -> !llvm.ptr
%220 = llvm.mlir.zero : !llvm.ptr
%221 = llvm.icmp "eq" %211, %220 : !llvm.ptr
%222 = scf.if %221 -> (i1) {
%223 = arith.constant true
scf.yield %223 : i1
} else {
%224 = llvm.mlir.zero : !llvm.ptr
%225 = llvm.icmp "eq" %214, %224 : !llvm.ptr
scf.yield %225 : i1
}
%226 = scf.if %222 -> (i1) {
%227 = arith.constant true
scf.yield %227 : i1
} else {
%228 = llvm.mlir.zero : !llvm.ptr
%229 = llvm.icmp "eq" %217, %228 : !llvm.ptr
scf.yield %229 : i1
}
cf.cond_br %226, ^bb33, ^bb34
^bb33:
%230 = arith.constant 1 : i32
func.return %230 : i32
^bb34:
cf.br ^bb35
^bb35:
%231 = llvm.load %203 : !llvm.ptr -> i64
%232 = llvm.load %206 : !llvm.ptr -> i32
%233 = arith.extsi %232 : i32 to i64
%234 = llvm.getelementptr %118[%233] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %231, %234 : i64, !llvm.ptr
%235 = arith.constant 0 : i32
%236 = llvm.mlir.constant(1 : i64) : i64
%237 = llvm.alloca %236 x i32 : (i64) -> !llvm.ptr
llvm.store %235, %237 : i32, !llvm.ptr
%238 = arith.constant 1 : i32
%239 = llvm.mlir.constant(1 : i64) : i64
%240 = llvm.alloca %239 x i32 : (i64) -> !llvm.ptr
llvm.store %238, %240 : i32, !llvm.ptr
cf.br ^bb36
^bb36:
%241 = llvm.load %240 : !llvm.ptr -> i32
%242 = llvm.load %206 : !llvm.ptr -> i32
%243 = arith.constant 2 : i32
%244 = arith.divsi %242, %243 : i32
%245 = arith.cmpi sle, %241, %244 : i32
cf.cond_br %245, ^bb37, ^bb38
^bb37:
%246 = llvm.load %206 : !llvm.ptr -> i32
%247 = llvm.load %240 : !llvm.ptr -> i32
%248 = arith.subi %246, %247 : i32
%250 = llvm.load %240 : !llvm.ptr -> i32
%251 = arith.extsi %250 : i32 to i64
%252 = llvm.getelementptr %118[%251] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%249 = llvm.load %252 : !llvm.ptr -> i64
%254 = arith.extsi %248 : i32 to i64
%255 = llvm.getelementptr %118[%254] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%253 = llvm.load %255 : !llvm.ptr -> i64
%257 = llvm.load %240 : !llvm.ptr -> i32
%258 = arith.extsi %257 : i32 to i64
%259 = llvm.getelementptr %124[%258] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%256 = llvm.load %259 : !llvm.ptr -> i32
%261 = arith.extsi %248 : i32 to i64
%262 = llvm.getelementptr %124[%261] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%260 = llvm.load %262 : !llvm.ptr -> i32
%263 = arith.constant 0 : i32
%264 = llvm.mlir.constant(1 : i64) : i64
%265 = llvm.alloca %264 x i32 : (i64) -> !llvm.ptr
llvm.store %263, %265 : i32, !llvm.ptr
cf.br ^bb39
^bb39:
%266 = llvm.load %265 : !llvm.ptr -> i32
%267 = arith.cmpi slt, %266, %256 : i32
cf.cond_br %267, ^bb40, ^bb41
^bb40:
%268 = arith.constant 0 : i32
%269 = llvm.mlir.constant(1 : i64) : i64
%270 = llvm.alloca %269 x i32 : (i64) -> !llvm.ptr
llvm.store %268, %270 : i32, !llvm.ptr
%271 = llvm.load %240 : !llvm.ptr -> i32
%272 = arith.cmpi eq, %271, %248 : i32
cf.cond_br %272, ^bb42, ^bb43
^bb42:
%273 = llvm.load %265 : !llvm.ptr -> i32
llvm.store %273, %270 : i32, !llvm.ptr
cf.br ^bb44
^bb43:
%274 = arith.constant 0 : i32
llvm.store %274, %270 : i32, !llvm.ptr
cf.br ^bb44
^bb44:
cf.br ^bb45
^bb45:
%275 = llvm.load %270 : !llvm.ptr -> i32
%276 = arith.cmpi slt, %275, %260 : i32
cf.cond_br %276, ^bb46, ^bb47
^bb46:
%278 = llvm.load %265 : !llvm.ptr -> i32
%279 = arith.extsi %278 : i32 to i64
%280 = arith.addi %249, %279 : i64
%281 = llvm.getelementptr %112[%280] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%277 = llvm.load %281 : !llvm.ptr -> i64
%283 = llvm.load %265 : !llvm.ptr -> i32
%284 = arith.extsi %283 : i32 to i64
%285 = arith.addi %249, %284 : i64
%286 = llvm.getelementptr %115[%285] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%282 = llvm.load %286 : !llvm.ptr -> i64
%288 = llvm.load %270 : !llvm.ptr -> i32
%289 = arith.extsi %288 : i32 to i64
%290 = arith.addi %253, %289 : i64
%291 = llvm.getelementptr %112[%290] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%287 = llvm.load %291 : !llvm.ptr -> i64
%293 = llvm.load %270 : !llvm.ptr -> i32
%294 = arith.extsi %293 : i32 to i64
%295 = arith.addi %253, %294 : i64
%296 = llvm.getelementptr %115[%295] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%292 = llvm.load %296 : !llvm.ptr -> i64
%297 = arith.muli %277, %292 : i64
%298 = arith.muli %287, %282 : i64
%299 = arith.addi %297, %298 : i64
%300 = arith.muli %282, %292 : i64
%301 = func.call @set_insert(%211, %214, %217, %210, %299, %300) : (!llvm.ptr, !llvm.ptr, !llvm.ptr, i64, i64, i64) -> i1
cf.cond_br %301, ^bb48, ^bb49
^bb48:
%302 = func.call @gcd_abs(%299, %300) : (i64, i64) -> i64
%303 = arith.divsi %299, %302 : i64
%304 = llvm.mlir.constant(1 : i64) : i64
%305 = llvm.alloca %304 x i64 : (i64) -> !llvm.ptr
llvm.store %303, %305 : i64, !llvm.ptr
%306 = arith.divsi %300, %302 : i64
%307 = llvm.mlir.constant(1 : i64) : i64
%308 = llvm.alloca %307 x i64 : (i64) -> !llvm.ptr
llvm.store %306, %308 : i64, !llvm.ptr
%309 = llvm.load %308 : !llvm.ptr -> i64
%310 = arith.constant 0 : i32
%312 = arith.extsi %310 : i32 to i64
%311 = arith.cmpi slt, %309, %312 : i64
cf.cond_br %311, ^bb51, ^bb52
^bb51:
%313 = llvm.load %305 : !llvm.ptr -> i64
%315 = arith.constant 0 : i64
%314 = arith.subi %315, %313 : i64
llvm.store %314, %305 : i64, !llvm.ptr
%316 = llvm.load %308 : !llvm.ptr -> i64
%318 = arith.constant 0 : i64
%317 = arith.subi %318, %316 : i64
llvm.store %317, %308 : i64, !llvm.ptr
cf.br ^bb53
^bb52:
cf.br ^bb53
^bb53:
%319 = llvm.load %305 : !llvm.ptr -> i64
%320 = llvm.load %203 : !llvm.ptr -> i64
%321 = llvm.getelementptr %112[%320] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %319, %321 : i64, !llvm.ptr
%322 = llvm.load %308 : !llvm.ptr -> i64
%323 = llvm.load %203 : !llvm.ptr -> i64
%324 = llvm.getelementptr %115[%323] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %322, %324 : i64, !llvm.ptr
%325 = llvm.load %203 : !llvm.ptr -> i64
%326 = arith.constant 1 : i32
%328 = arith.extsi %326 : i32 to i64
%327 = arith.addi %325, %328 : i64
llvm.store %327, %203 : i64, !llvm.ptr
%329 = llvm.load %237 : !llvm.ptr -> i32
%330 = arith.constant 1 : i32
%331 = arith.addi %329, %330 : i32
llvm.store %331, %237 : i32, !llvm.ptr
%333 = llvm.load %305 : !llvm.ptr -> i64
%334 = llvm.load %308 : !llvm.ptr -> i64
%332 = func.call @set_insert(%147, %150, %153, %146, %333, %334) : (!llvm.ptr, !llvm.ptr, !llvm.ptr, i64, i64, i64) -> i1
cf.cond_br %332, ^bb54, ^bb55
^bb54:
%335 = llvm.load %199 : !llvm.ptr -> i64
%336 = arith.constant 1 : i32
%338 = arith.extsi %336 : i32 to i64
%337 = arith.addi %335, %338 : i64
llvm.store %337, %199 : i64, !llvm.ptr
cf.br ^bb56
^bb55:
cf.br ^bb56
^bb56:
cf.br ^bb50
^bb49:
cf.br ^bb50
^bb50:
%339 = func.call @set_insert(%211, %214, %217, %210, %300, %299) : (!llvm.ptr, !llvm.ptr, !llvm.ptr, i64, i64, i64) -> i1
cf.cond_br %339, ^bb57, ^bb58
^bb57:
%340 = func.call @gcd_abs(%300, %299) : (i64, i64) -> i64
%341 = arith.divsi %300, %340 : i64
%342 = llvm.mlir.constant(1 : i64) : i64
%343 = llvm.alloca %342 x i64 : (i64) -> !llvm.ptr
llvm.store %341, %343 : i64, !llvm.ptr
%344 = arith.divsi %299, %340 : i64
%345 = llvm.mlir.constant(1 : i64) : i64
%346 = llvm.alloca %345 x i64 : (i64) -> !llvm.ptr
llvm.store %344, %346 : i64, !llvm.ptr
%347 = llvm.load %346 : !llvm.ptr -> i64
%348 = arith.constant 0 : i32
%350 = arith.extsi %348 : i32 to i64
%349 = arith.cmpi slt, %347, %350 : i64
cf.cond_br %349, ^bb60, ^bb61
^bb60:
%351 = llvm.load %343 : !llvm.ptr -> i64
%353 = arith.constant 0 : i64
%352 = arith.subi %353, %351 : i64
llvm.store %352, %343 : i64, !llvm.ptr
%354 = llvm.load %346 : !llvm.ptr -> i64
%356 = arith.constant 0 : i64
%355 = arith.subi %356, %354 : i64
llvm.store %355, %346 : i64, !llvm.ptr
cf.br ^bb62
^bb61:
cf.br ^bb62
^bb62:
%357 = llvm.load %343 : !llvm.ptr -> i64
%358 = llvm.load %203 : !llvm.ptr -> i64
%359 = llvm.getelementptr %112[%358] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %357, %359 : i64, !llvm.ptr
%360 = llvm.load %346 : !llvm.ptr -> i64
%361 = llvm.load %203 : !llvm.ptr -> i64
%362 = llvm.getelementptr %115[%361] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %360, %362 : i64, !llvm.ptr
%363 = llvm.load %203 : !llvm.ptr -> i64
%364 = arith.constant 1 : i32
%366 = arith.extsi %364 : i32 to i64
%365 = arith.addi %363, %366 : i64
llvm.store %365, %203 : i64, !llvm.ptr
%367 = llvm.load %237 : !llvm.ptr -> i32
%368 = arith.constant 1 : i32
%369 = arith.addi %367, %368 : i32
llvm.store %369, %237 : i32, !llvm.ptr
%371 = llvm.load %343 : !llvm.ptr -> i64
%372 = llvm.load %346 : !llvm.ptr -> i64
%370 = func.call @set_insert(%147, %150, %153, %146, %371, %372) : (!llvm.ptr, !llvm.ptr, !llvm.ptr, i64, i64, i64) -> i1
cf.cond_br %370, ^bb63, ^bb64
^bb63:
%373 = llvm.load %199 : !llvm.ptr -> i64
%374 = arith.constant 1 : i32
%376 = arith.extsi %374 : i32 to i64
%375 = arith.addi %373, %376 : i64
llvm.store %375, %199 : i64, !llvm.ptr
cf.br ^bb65
^bb64:
cf.br ^bb65
^bb65:
cf.br ^bb59
^bb58:
cf.br ^bb59
^bb59:
%377 = llvm.load %270 : !llvm.ptr -> i32
%378 = arith.constant 1 : i32
%379 = arith.addi %377, %378 : i32
llvm.store %379, %270 : i32, !llvm.ptr
cf.br ^bb45
^bb47:
%380 = llvm.load %265 : !llvm.ptr -> i32
%381 = arith.constant 1 : i32
%382 = arith.addi %380, %381 : i32
llvm.store %382, %265 : i32, !llvm.ptr
cf.br ^bb39
^bb41:
%383 = llvm.load %240 : !llvm.ptr -> i32
%384 = arith.constant 1 : i32
%385 = arith.addi %383, %384 : i32
llvm.store %385, %240 : i32, !llvm.ptr
cf.br ^bb36
^bb38:
%386 = llvm.load %237 : !llvm.ptr -> i32
%387 = llvm.load %206 : !llvm.ptr -> i32
%388 = arith.extsi %387 : i32 to i64
%389 = llvm.getelementptr %124[%388] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %386, %389 : i32, !llvm.ptr
%390 = llvm.load %203 : !llvm.ptr -> i64
%391 = llvm.load %206 : !llvm.ptr -> i32
%392 = arith.constant 1 : i32
%393 = arith.addi %391, %392 : i32
%394 = arith.extsi %393 : i32 to i64
%395 = llvm.getelementptr %118[%394] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %390, %395 : i64, !llvm.ptr
func.call @free(%211) : (!llvm.ptr) -> ()
func.call @free(%214) : (!llvm.ptr) -> ()
func.call @free(%217) : (!llvm.ptr) -> ()
%399 = llvm.load %206 : !llvm.ptr -> i32
%400 = arith.constant 1 : i32
%401 = arith.addi %399, %400 : i32
llvm.store %401, %206 : i32, !llvm.ptr
cf.br ^bb30
^bb32:
%402 = llvm.mlir.addressof @str_0 : !llvm.ptr
%403 = llvm.load %199 : !llvm.ptr -> i64
%404 = llvm.call @printf(%402, %403) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%112) : (!llvm.ptr) -> ()
func.call @free(%115) : (!llvm.ptr) -> ()
func.call @free(%118) : (!llvm.ptr) -> ()
func.call @free(%124) : (!llvm.ptr) -> ()
func.call @free(%147) : (!llvm.ptr) -> ()
func.call @free(%150) : (!llvm.ptr) -> ()
func.call @free(%153) : (!llvm.ptr) -> ()
%412 = arith.constant 0 : i32
func.return %412 : i32
}
}