Problem 236
Luxury Hysteresis: maximal m as reduced fraction.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^2) | O(n log n) |
| Space complexity | O(1) | O(n) |
| Approach | Flow solution | Scan with comparison |
| Verdict | Suboptimal |
Flow source
# Project Euler 236
# Luxury Hysteresis: maximal m as reduced fraction.
import euler.nt { gcd }
function main() -> i32 {
let A1: i64 = 5248
let A2: i64 = 1312
let A3: i64 = 2624
let A4: i64 = 5760
let A5: i64 = 3936
let B1: i64 = 640
let B2: i64 = 1888
let B3: i64 = 3776
let B4: i64 = 3776
let B5: i64 = 5664
let SA: i64 = A1 + A2 + A3 + A4 + A5
let SB: i64 = B1 + B2 + B3 + B4 + B5
let mut max_num: i64 = 0
let mut max_den: i64 = 1
let mut s: i64 = 1
while s <= A2 {
let r_start: i64 = (59 * s) / 41 + 1
if r_start <= B2 {
let mut r: i64 = r_start
while r <= B2 {
if gcd(r, s) == 1 {
let mut t2: i64 = A2 / s
if B2 / r < t2 { t2 = B2 / r }
let mut t3: i64 = A3 / s
if B3 / r < t3 { t3 = B3 / r }
let mut t5: i64 = A5 / s
if B5 / r < t5 { t5 = B5 / r }
if t2 >= 1 && t3 >= 1 && t5 >= 1 {
let T_max: i64 = t2 + t3 + t5
let d1: i64 = gcd(59 * s, 5 * r)
let a1_base: i64 = (59 * s) / d1
let b1_base: i64 = (5 * r) / d1
let mut t1_max: i64 = A1 / a1_base
if B1 / b1_base < t1_max { t1_max = B1 / b1_base }
if t1_max >= 1 {
let d4: i64 = gcd(90 * s, 41 * r)
let a4_base: i64 = (90 * s) / d4
let b4_base: i64 = (41 * r) / d4
let mut t4_max: i64 = A4 / a4_base
if B4 / b4_base < t4_max { t4_max = B4 / b4_base }
if t4_max >= 1 {
let L: i64 = SB * 59 * s
let R: i64 = SA * 41 * r
let denom: i64 = s * L - r * R
if denom != 0 {
let c1: i64 = b1_base * R - a1_base * L
let c4: i64 = b4_base * R - a4_base * L
let mut found: bool = false
let mut t1: i64 = 1
while t1 <= t1_max && !found {
let base: i64 = c1 * t1
let mut t4: i64 = 1
while t4 <= t4_max {
let num: i64 = base + c4 * t4
if num % denom == 0 {
let T: i64 = num / denom
if T >= 3 && T <= T_max {
let g: i64 = gcd(41 * r, 59 * s)
let mn: i64 = (41 * r) / g
let md: i64 = (59 * s) / g
if mn * max_den > max_num * md {
max_num = mn
max_den = md
}
found = true
break
}
}
t4 = t4 + 1
}
t1 = t1 + 1
}
}
}
}
}
}
r = r + 1
}
}
s = s + 1
}
printf("%lld/%lld\n", max_num, max_den)
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_i64_i64(int64_t a0, int64_t b0);
int64_t lcm_i64_i64(int64_t a, int64_t b);
int64_t isqrt_i64(int64_t n);
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
bool is_prime_i64(int64_t n);
int32_t main(void);
int64_t gcd_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 lcm_i64_i64(int64_t a, int64_t b) {
if ((a == 0 || b == 0)) {
return 0;
}
return (FLOW_CHECKED_DIV((a), (gcd_i64_i64(a, b))) * b);
}
int64_t isqrt_i64(int64_t n) {
if (n < 2) {
return n;
}
int64_t x = n;
int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
while (y < x) {
x = y;
y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
}
return x;
}
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod) {
int64_t a = FLOW_CHECKED_MOD((a0), (mod));
int64_t b = FLOW_CHECKED_MOD((b0), (mod));
int64_t result = 0;
while (b > 0) {
if (FLOW_CHECKED_MOD((b), (2)) == 1) {
result = FLOW_CHECKED_MOD(((result + a)), (mod));
}
a = FLOW_CHECKED_MOD(((a * 2)), (mod));
b = FLOW_CHECKED_DIV((b), (2));
}
return result;
}
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
if (mod == 1) {
return 0;
}
int64_t result = 1;
int64_t b = FLOW_CHECKED_MOD((base), (mod));
int64_t e = exp;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
result = mulmod_i64_i64_i64(result, b, mod);
}
b = mulmod_i64_i64_i64(b, b, mod);
e = FLOW_CHECKED_DIV((e), (2));
}
return result;
}
bool is_prime_i64(int64_t n) {
if (n < 2) {
return 0;
}
if (n < 4) {
return 1;
}
if ((FLOW_CHECKED_MOD((n), (2)) == 0 || FLOW_CHECKED_MOD((n), (3)) == 0)) {
return 0;
}
int64_t i = 5;
while ((i * i) <= n) {
if ((FLOW_CHECKED_MOD((n), (i)) == 0 || FLOW_CHECKED_MOD((n), ((i + 2))) == 0)) {
return 0;
}
i = (i + 6);
}
return 1;
}
int32_t main(void) {
int64_t A1 = 5248;
int64_t A2 = 1312;
int64_t A3 = 2624;
int64_t A4 = 5760;
int64_t A5 = 3936;
int64_t B1 = 640;
int64_t B2 = 1888;
int64_t B3 = 3776;
int64_t B4 = 3776;
int64_t B5 = 5664;
int64_t SA = ((((A1 + A2) + A3) + A4) + A5);
int64_t SB = ((((B1 + B2) + B3) + B4) + B5);
int64_t max_num = 0;
int64_t max_den = 1;
int64_t s = 1;
while (s <= A2) {
int64_t r_start = (FLOW_CHECKED_DIV(((59 * s)), (41)) + 1);
if (r_start <= B2) {
int64_t r = r_start;
while (r <= B2) {
if (gcd_i64_i64(r, s) == 1) {
int64_t t2 = FLOW_CHECKED_DIV((A2), (s));
if (FLOW_CHECKED_DIV((B2), (r)) < t2) {
t2 = FLOW_CHECKED_DIV((B2), (r));
}
int64_t t3 = FLOW_CHECKED_DIV((A3), (s));
if (FLOW_CHECKED_DIV((B3), (r)) < t3) {
t3 = FLOW_CHECKED_DIV((B3), (r));
}
int64_t t5 = FLOW_CHECKED_DIV((A5), (s));
if (FLOW_CHECKED_DIV((B5), (r)) < t5) {
t5 = FLOW_CHECKED_DIV((B5), (r));
}
if (((t2 >= 1 && t3 >= 1) && t5 >= 1)) {
int64_t T_max = ((t2 + t3) + t5);
int64_t d1 = gcd_i64_i64((59 * s), (5 * r));
int64_t a1_base = FLOW_CHECKED_DIV(((59 * s)), (d1));
int64_t b1_base = FLOW_CHECKED_DIV(((5 * r)), (d1));
int64_t t1_max = FLOW_CHECKED_DIV((A1), (a1_base));
if (FLOW_CHECKED_DIV((B1), (b1_base)) < t1_max) {
t1_max = FLOW_CHECKED_DIV((B1), (b1_base));
}
if (t1_max >= 1) {
int64_t d4 = gcd_i64_i64((90 * s), (41 * r));
int64_t a4_base = FLOW_CHECKED_DIV(((90 * s)), (d4));
int64_t b4_base = FLOW_CHECKED_DIV(((41 * r)), (d4));
int64_t t4_max = FLOW_CHECKED_DIV((A4), (a4_base));
if (FLOW_CHECKED_DIV((B4), (b4_base)) < t4_max) {
t4_max = FLOW_CHECKED_DIV((B4), (b4_base));
}
if (t4_max >= 1) {
int64_t L = ((SB * 59) * s);
int64_t R = ((SA * 41) * r);
int64_t denom = ((s * L) - (r * R));
if (denom != 0) {
int64_t c1 = ((b1_base * R) - (a1_base * L));
int64_t c4 = ((b4_base * R) - (a4_base * L));
bool found = 0;
int64_t t1 = 1;
while ((t1 <= t1_max && (!(found)))) {
int64_t base = (c1 * t1);
int64_t t4 = 1;
while (t4 <= t4_max) {
int64_t num = (base + (c4 * t4));
if (FLOW_CHECKED_MOD((num), (denom)) == 0) {
int64_t T = FLOW_CHECKED_DIV((num), (denom));
if ((T >= 3 && T <= T_max)) {
int64_t g = gcd_i64_i64((41 * r), (59 * s));
int64_t mn = FLOW_CHECKED_DIV(((41 * r)), (g));
int64_t md = FLOW_CHECKED_DIV(((59 * s)), (g));
if ((mn * max_den) > (max_num * md)) {
max_num = mn;
max_den = md;
}
found = 1;
break;
}
}
t4 = (t4 + 1);
}
t1 = (t1 + 1);
}
}
}
}
}
}
r = (r + 1);
}
}
s = (s + 1);
}
printf("%lld/%lld\n", max_num, max_den);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%lld/%lld\n\00") {addr_space = 0 : i32} : !llvm.array<11 x i8>
func.func @gcd(%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.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %3 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%4 = llvm.load %3 : !llvm.ptr -> i64
%5 = arith.constant 0 : i32
%7 = arith.extsi %5 : i32 to i64
%6 = arith.cmpi ne, %4, %7 : i64
cf.cond_br %6, ^bb1, ^bb2
^bb1:
%8 = llvm.load %1 : !llvm.ptr -> i64
%9 = llvm.load %3 : !llvm.ptr -> i64
%10 = arith.remsi %8, %9 : i64
%11 = llvm.load %3 : !llvm.ptr -> i64
llvm.store %11, %1 : i64, !llvm.ptr
llvm.store %10, %3 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%12 = llvm.load %1 : !llvm.ptr -> i64
func.return %12 : i64
}
func.func @lcm(%arg0: i64, %arg1: i64) -> i64 {
%13 = arith.constant 0 : i32
%15 = arith.extsi %13 : i32 to i64
%14 = arith.cmpi eq, %arg0, %15 : i64
%16 = scf.if %14 -> (i1) {
%17 = arith.constant true
scf.yield %17 : i1
} else {
%18 = arith.constant 0 : i32
%20 = arith.extsi %18 : i32 to i64
%19 = arith.cmpi eq, %arg1, %20 : i64
scf.yield %19 : i1
}
cf.cond_br %16, ^bb3, ^bb4
^bb3:
%21 = arith.constant 0 : i32
%22 = arith.extsi %21 : i32 to i64
func.return %22 : i64
^bb4:
cf.br ^bb5
^bb5:
%23 = func.call @gcd(%arg0, %arg1) : (i64, i64) -> i64
%24 = arith.divsi %arg0, %23 : i64
%25 = arith.muli %24, %arg1 : i64
func.return %25 : i64
}
func.func @isqrt(%arg0: i64) -> i64 {
%26 = arith.constant 2 : i32
%28 = arith.extsi %26 : i32 to i64
%27 = arith.cmpi slt, %arg0, %28 : i64
cf.cond_br %27, ^bb6, ^bb7
^bb6:
func.return %arg0 : i64
^bb7:
cf.br ^bb8
^bb8:
%29 = llvm.mlir.constant(1 : i64) : i64
%30 = llvm.alloca %29 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %30 : i64, !llvm.ptr
%31 = llvm.load %30 : !llvm.ptr -> i64
%32 = arith.constant 1 : i32
%34 = arith.extsi %32 : i32 to i64
%33 = arith.addi %31, %34 : i64
%35 = arith.constant 2 : i32
%37 = arith.extsi %35 : i32 to i64
%36 = arith.divsi %33, %37 : i64
%38 = llvm.mlir.constant(1 : i64) : i64
%39 = llvm.alloca %38 x i64 : (i64) -> !llvm.ptr
llvm.store %36, %39 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%40 = llvm.load %39 : !llvm.ptr -> i64
%41 = llvm.load %30 : !llvm.ptr -> i64
%42 = arith.cmpi slt, %40, %41 : i64
cf.cond_br %42, ^bb10, ^bb11
^bb10:
%43 = llvm.load %39 : !llvm.ptr -> i64
llvm.store %43, %30 : i64, !llvm.ptr
%44 = llvm.load %30 : !llvm.ptr -> i64
%45 = llvm.load %30 : !llvm.ptr -> i64
%46 = arith.divsi %arg0, %45 : i64
%47 = arith.addi %44, %46 : i64
%48 = arith.constant 2 : i32
%50 = arith.extsi %48 : i32 to i64
%49 = arith.divsi %47, %50 : i64
llvm.store %49, %39 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%51 = llvm.load %30 : !llvm.ptr -> i64
func.return %51 : i64
}
func.func @mulmod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%52 = arith.remsi %arg0, %arg2 : 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 = arith.remsi %arg1, %arg2 : i64
%56 = llvm.mlir.constant(1 : i64) : i64
%57 = llvm.alloca %56 x i64 : (i64) -> !llvm.ptr
llvm.store %55, %57 : i64, !llvm.ptr
%58 = arith.constant 0 : i32
%59 = arith.extsi %58 : i32 to i64
%60 = llvm.mlir.constant(1 : i64) : i64
%61 = llvm.alloca %60 x i64 : (i64) -> !llvm.ptr
llvm.store %59, %61 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%62 = llvm.load %57 : !llvm.ptr -> i64
%63 = arith.constant 0 : i32
%65 = arith.extsi %63 : i32 to i64
%64 = arith.cmpi sgt, %62, %65 : i64
cf.cond_br %64, ^bb13, ^bb14
^bb13:
%66 = llvm.load %57 : !llvm.ptr -> i64
%67 = arith.constant 2 : i32
%69 = arith.extsi %67 : i32 to i64
%68 = arith.remsi %66, %69 : i64
%70 = arith.constant 1 : i32
%72 = arith.extsi %70 : i32 to i64
%71 = arith.cmpi eq, %68, %72 : i64
cf.cond_br %71, ^bb15, ^bb16
^bb15:
%73 = llvm.load %61 : !llvm.ptr -> i64
%74 = llvm.load %54 : !llvm.ptr -> i64
%75 = arith.addi %73, %74 : i64
%76 = arith.remsi %75, %arg2 : i64
llvm.store %76, %61 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%77 = llvm.load %54 : !llvm.ptr -> i64
%78 = arith.constant 2 : i32
%80 = arith.extsi %78 : i32 to i64
%79 = arith.muli %77, %80 : i64
%81 = arith.remsi %79, %arg2 : i64
llvm.store %81, %54 : i64, !llvm.ptr
%82 = llvm.load %57 : !llvm.ptr -> i64
%83 = arith.constant 2 : i32
%85 = arith.extsi %83 : i32 to i64
%84 = arith.divsi %82, %85 : i64
llvm.store %84, %57 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%86 = llvm.load %61 : !llvm.ptr -> i64
func.return %86 : i64
}
func.func @mod_pow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%87 = arith.constant 1 : i32
%89 = arith.extsi %87 : i32 to i64
%88 = arith.cmpi eq, %arg2, %89 : i64
cf.cond_br %88, ^bb18, ^bb19
^bb18:
%90 = arith.constant 0 : i32
%91 = arith.extsi %90 : i32 to i64
func.return %91 : i64
^bb19:
cf.br ^bb20
^bb20:
%92 = arith.constant 1 : i32
%93 = arith.extsi %92 : i32 to i64
%94 = llvm.mlir.constant(1 : i64) : i64
%95 = llvm.alloca %94 x i64 : (i64) -> !llvm.ptr
llvm.store %93, %95 : i64, !llvm.ptr
%96 = arith.remsi %arg0, %arg2 : i64
%97 = llvm.mlir.constant(1 : i64) : i64
%98 = llvm.alloca %97 x i64 : (i64) -> !llvm.ptr
llvm.store %96, %98 : i64, !llvm.ptr
%99 = llvm.mlir.constant(1 : i64) : i64
%100 = llvm.alloca %99 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %100 : i64, !llvm.ptr
cf.br ^bb21
^bb21:
%101 = llvm.load %100 : !llvm.ptr -> i64
%102 = arith.constant 0 : i32
%104 = arith.extsi %102 : i32 to i64
%103 = arith.cmpi sgt, %101, %104 : i64
cf.cond_br %103, ^bb22, ^bb23
^bb22:
%105 = llvm.load %100 : !llvm.ptr -> i64
%106 = arith.constant 2 : i32
%108 = arith.extsi %106 : i32 to i64
%107 = arith.remsi %105, %108 : i64
%109 = arith.constant 1 : i32
%111 = arith.extsi %109 : i32 to i64
%110 = arith.cmpi eq, %107, %111 : i64
cf.cond_br %110, ^bb24, ^bb25
^bb24:
%113 = llvm.load %95 : !llvm.ptr -> i64
%114 = llvm.load %98 : !llvm.ptr -> i64
%112 = func.call @mulmod(%113, %114, %arg2) : (i64, i64, i64) -> i64
llvm.store %112, %95 : i64, !llvm.ptr
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%116 = llvm.load %98 : !llvm.ptr -> i64
%117 = llvm.load %98 : !llvm.ptr -> i64
%115 = func.call @mulmod(%116, %117, %arg2) : (i64, i64, i64) -> i64
llvm.store %115, %98 : i64, !llvm.ptr
%118 = llvm.load %100 : !llvm.ptr -> i64
%119 = arith.constant 2 : i32
%121 = arith.extsi %119 : i32 to i64
%120 = arith.divsi %118, %121 : i64
llvm.store %120, %100 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%122 = llvm.load %95 : !llvm.ptr -> i64
func.return %122 : i64
}
func.func @is_prime(%arg0: i64) -> i1 {
%123 = arith.constant 2 : i32
%125 = arith.extsi %123 : i32 to i64
%124 = arith.cmpi slt, %arg0, %125 : i64
cf.cond_br %124, ^bb27, ^bb28
^bb27:
%126 = arith.constant 0 : i1
func.return %126 : i1
^bb28:
cf.br ^bb29
^bb29:
%127 = arith.constant 4 : i32
%129 = arith.extsi %127 : i32 to i64
%128 = arith.cmpi slt, %arg0, %129 : i64
cf.cond_br %128, ^bb30, ^bb31
^bb30:
%130 = arith.constant 1 : i1
func.return %130 : i1
^bb31:
cf.br ^bb32
^bb32:
%131 = arith.constant 2 : i32
%133 = arith.extsi %131 : i32 to i64
%132 = arith.remsi %arg0, %133 : i64
%134 = arith.constant 0 : i32
%136 = arith.extsi %134 : i32 to i64
%135 = arith.cmpi eq, %132, %136 : i64
%137 = scf.if %135 -> (i1) {
%138 = arith.constant true
scf.yield %138 : i1
} else {
%139 = arith.constant 3 : i32
%141 = arith.extsi %139 : i32 to i64
%140 = arith.remsi %arg0, %141 : i64
%142 = arith.constant 0 : i32
%144 = arith.extsi %142 : i32 to i64
%143 = arith.cmpi eq, %140, %144 : i64
scf.yield %143 : i1
}
cf.cond_br %137, ^bb33, ^bb34
^bb33:
%145 = arith.constant 0 : i1
func.return %145 : i1
^bb34:
cf.br ^bb35
^bb35:
%146 = arith.constant 5 : i32
%147 = arith.extsi %146 : i32 to i64
%148 = llvm.mlir.constant(1 : i64) : i64
%149 = llvm.alloca %148 x i64 : (i64) -> !llvm.ptr
llvm.store %147, %149 : i64, !llvm.ptr
cf.br ^bb36
^bb36:
%150 = llvm.load %149 : !llvm.ptr -> i64
%151 = llvm.load %149 : !llvm.ptr -> i64
%152 = arith.muli %150, %151 : i64
%153 = arith.cmpi sle, %152, %arg0 : i64
cf.cond_br %153, ^bb37, ^bb38
^bb37:
%154 = llvm.load %149 : !llvm.ptr -> i64
%155 = arith.remsi %arg0, %154 : i64
%156 = arith.constant 0 : i32
%158 = arith.extsi %156 : i32 to i64
%157 = arith.cmpi eq, %155, %158 : i64
%159 = scf.if %157 -> (i1) {
%160 = arith.constant true
scf.yield %160 : i1
} else {
%161 = llvm.load %149 : !llvm.ptr -> i64
%162 = arith.constant 2 : i32
%164 = arith.extsi %162 : i32 to i64
%163 = arith.addi %161, %164 : i64
%165 = arith.remsi %arg0, %163 : i64
%166 = arith.constant 0 : i32
%168 = arith.extsi %166 : i32 to i64
%167 = arith.cmpi eq, %165, %168 : i64
scf.yield %167 : i1
}
cf.cond_br %159, ^bb39, ^bb40
^bb39:
%169 = arith.constant 0 : i1
func.return %169 : i1
^bb40:
cf.br ^bb41
^bb41:
%170 = llvm.load %149 : !llvm.ptr -> i64
%171 = arith.constant 6 : i32
%173 = arith.extsi %171 : i32 to i64
%172 = arith.addi %170, %173 : i64
llvm.store %172, %149 : i64, !llvm.ptr
cf.br ^bb36
^bb38:
%174 = arith.constant 1 : i1
func.return %174 : i1
}
func.func @main() -> i32 {
%175 = arith.constant 5248 : i32
%176 = arith.extsi %175 : i32 to i64
%177 = arith.constant 1312 : i32
%178 = arith.extsi %177 : i32 to i64
%179 = arith.constant 2624 : i32
%180 = arith.extsi %179 : i32 to i64
%181 = arith.constant 5760 : i32
%182 = arith.extsi %181 : i32 to i64
%183 = arith.constant 3936 : i32
%184 = arith.extsi %183 : i32 to i64
%185 = arith.constant 640 : i32
%186 = arith.extsi %185 : i32 to i64
%187 = arith.constant 1888 : i32
%188 = arith.extsi %187 : i32 to i64
%189 = arith.constant 3776 : i32
%190 = arith.extsi %189 : i32 to i64
%191 = arith.constant 3776 : i32
%192 = arith.extsi %191 : i32 to i64
%193 = arith.constant 5664 : i32
%194 = arith.extsi %193 : i32 to i64
%195 = arith.addi %176, %178 : i64
%196 = arith.addi %195, %180 : i64
%197 = arith.addi %196, %182 : i64
%198 = arith.addi %197, %184 : i64
%199 = arith.addi %186, %188 : i64
%200 = arith.addi %199, %190 : i64
%201 = arith.addi %200, %192 : i64
%202 = arith.addi %201, %194 : i64
%203 = arith.constant 0 : i32
%204 = arith.extsi %203 : i32 to i64
%205 = llvm.mlir.constant(1 : i64) : i64
%206 = llvm.alloca %205 x i64 : (i64) -> !llvm.ptr
llvm.store %204, %206 : i64, !llvm.ptr
%207 = arith.constant 1 : i32
%208 = arith.extsi %207 : i32 to i64
%209 = llvm.mlir.constant(1 : i64) : i64
%210 = llvm.alloca %209 x i64 : (i64) -> !llvm.ptr
llvm.store %208, %210 : i64, !llvm.ptr
%211 = arith.constant 1 : i32
%212 = arith.extsi %211 : i32 to i64
%213 = llvm.mlir.constant(1 : i64) : i64
%214 = llvm.alloca %213 x i64 : (i64) -> !llvm.ptr
llvm.store %212, %214 : i64, !llvm.ptr
cf.br ^bb42
^bb42:
%215 = llvm.load %214 : !llvm.ptr -> i64
%216 = arith.cmpi sle, %215, %178 : i64
cf.cond_br %216, ^bb43, ^bb44
^bb43:
%217 = arith.constant 59 : i32
%218 = llvm.load %214 : !llvm.ptr -> i64
%220 = arith.extsi %217 : i32 to i64
%219 = arith.muli %220, %218 : i64
%221 = arith.constant 41 : i32
%223 = arith.extsi %221 : i32 to i64
%222 = arith.divsi %219, %223 : i64
%224 = arith.constant 1 : i32
%226 = arith.extsi %224 : i32 to i64
%225 = arith.addi %222, %226 : i64
%227 = arith.cmpi sle, %225, %188 : i64
cf.cond_br %227, ^bb45, ^bb46
^bb45:
%228 = llvm.mlir.constant(1 : i64) : i64
%229 = llvm.alloca %228 x i64 : (i64) -> !llvm.ptr
llvm.store %225, %229 : i64, !llvm.ptr
cf.br ^bb48
^bb48:
%230 = llvm.load %229 : !llvm.ptr -> i64
%231 = arith.cmpi sle, %230, %188 : i64
cf.cond_br %231, ^bb49, ^bb50
^bb49:
%233 = llvm.load %229 : !llvm.ptr -> i64
%234 = llvm.load %214 : !llvm.ptr -> i64
%232 = func.call @gcd(%233, %234) : (i64, i64) -> i64
%235 = arith.constant 1 : i32
%237 = arith.extsi %235 : i32 to i64
%236 = arith.cmpi eq, %232, %237 : i64
cf.cond_br %236, ^bb51, ^bb52
^bb51:
%238 = llvm.load %214 : !llvm.ptr -> i64
%239 = arith.divsi %178, %238 : i64
%240 = llvm.mlir.constant(1 : i64) : i64
%241 = llvm.alloca %240 x i64 : (i64) -> !llvm.ptr
llvm.store %239, %241 : i64, !llvm.ptr
%242 = llvm.load %229 : !llvm.ptr -> i64
%243 = arith.divsi %188, %242 : i64
%244 = llvm.load %241 : !llvm.ptr -> i64
%245 = arith.cmpi slt, %243, %244 : i64
cf.cond_br %245, ^bb54, ^bb55
^bb54:
%246 = llvm.load %229 : !llvm.ptr -> i64
%247 = arith.divsi %188, %246 : i64
llvm.store %247, %241 : i64, !llvm.ptr
cf.br ^bb56
^bb55:
cf.br ^bb56
^bb56:
%248 = llvm.load %214 : !llvm.ptr -> i64
%249 = arith.divsi %180, %248 : i64
%250 = llvm.mlir.constant(1 : i64) : i64
%251 = llvm.alloca %250 x i64 : (i64) -> !llvm.ptr
llvm.store %249, %251 : i64, !llvm.ptr
%252 = llvm.load %229 : !llvm.ptr -> i64
%253 = arith.divsi %190, %252 : i64
%254 = llvm.load %251 : !llvm.ptr -> i64
%255 = arith.cmpi slt, %253, %254 : i64
cf.cond_br %255, ^bb57, ^bb58
^bb57:
%256 = llvm.load %229 : !llvm.ptr -> i64
%257 = arith.divsi %190, %256 : i64
llvm.store %257, %251 : i64, !llvm.ptr
cf.br ^bb59
^bb58:
cf.br ^bb59
^bb59:
%258 = llvm.load %214 : !llvm.ptr -> i64
%259 = arith.divsi %184, %258 : i64
%260 = llvm.mlir.constant(1 : i64) : i64
%261 = llvm.alloca %260 x i64 : (i64) -> !llvm.ptr
llvm.store %259, %261 : i64, !llvm.ptr
%262 = llvm.load %229 : !llvm.ptr -> i64
%263 = arith.divsi %194, %262 : i64
%264 = llvm.load %261 : !llvm.ptr -> i64
%265 = arith.cmpi slt, %263, %264 : i64
cf.cond_br %265, ^bb60, ^bb61
^bb60:
%266 = llvm.load %229 : !llvm.ptr -> i64
%267 = arith.divsi %194, %266 : i64
llvm.store %267, %261 : i64, !llvm.ptr
cf.br ^bb62
^bb61:
cf.br ^bb62
^bb62:
%268 = llvm.load %241 : !llvm.ptr -> i64
%269 = arith.constant 1 : i32
%271 = arith.extsi %269 : i32 to i64
%270 = arith.cmpi sge, %268, %271 : i64
%272 = scf.if %270 -> (i1) {
%273 = llvm.load %251 : !llvm.ptr -> i64
%274 = arith.constant 1 : i32
%276 = arith.extsi %274 : i32 to i64
%275 = arith.cmpi sge, %273, %276 : i64
scf.yield %275 : i1
} else {
%277 = arith.constant false
scf.yield %277 : i1
}
%278 = scf.if %272 -> (i1) {
%279 = llvm.load %261 : !llvm.ptr -> i64
%280 = arith.constant 1 : i32
%282 = arith.extsi %280 : i32 to i64
%281 = arith.cmpi sge, %279, %282 : i64
scf.yield %281 : i1
} else {
%283 = arith.constant false
scf.yield %283 : i1
}
cf.cond_br %278, ^bb63, ^bb64
^bb63:
%284 = llvm.load %241 : !llvm.ptr -> i64
%285 = llvm.load %251 : !llvm.ptr -> i64
%286 = arith.addi %284, %285 : i64
%287 = llvm.load %261 : !llvm.ptr -> i64
%288 = arith.addi %286, %287 : i64
%290 = arith.constant 59 : i32
%291 = llvm.load %214 : !llvm.ptr -> i64
%293 = arith.extsi %290 : i32 to i64
%292 = arith.muli %293, %291 : i64
%294 = arith.constant 5 : i32
%295 = llvm.load %229 : !llvm.ptr -> i64
%297 = arith.extsi %294 : i32 to i64
%296 = arith.muli %297, %295 : i64
%289 = func.call @gcd(%292, %296) : (i64, i64) -> i64
%298 = arith.constant 59 : i32
%299 = llvm.load %214 : !llvm.ptr -> i64
%301 = arith.extsi %298 : i32 to i64
%300 = arith.muli %301, %299 : i64
%302 = arith.divsi %300, %289 : i64
%303 = arith.constant 5 : i32
%304 = llvm.load %229 : !llvm.ptr -> i64
%306 = arith.extsi %303 : i32 to i64
%305 = arith.muli %306, %304 : i64
%307 = arith.divsi %305, %289 : i64
%308 = arith.divsi %176, %302 : i64
%309 = llvm.mlir.constant(1 : i64) : i64
%310 = llvm.alloca %309 x i64 : (i64) -> !llvm.ptr
llvm.store %308, %310 : i64, !llvm.ptr
%311 = arith.divsi %186, %307 : i64
%312 = llvm.load %310 : !llvm.ptr -> i64
%313 = arith.cmpi slt, %311, %312 : i64
cf.cond_br %313, ^bb66, ^bb67
^bb66:
%314 = arith.divsi %186, %307 : i64
llvm.store %314, %310 : i64, !llvm.ptr
cf.br ^bb68
^bb67:
cf.br ^bb68
^bb68:
%315 = llvm.load %310 : !llvm.ptr -> i64
%316 = arith.constant 1 : i32
%318 = arith.extsi %316 : i32 to i64
%317 = arith.cmpi sge, %315, %318 : i64
cf.cond_br %317, ^bb69, ^bb70
^bb69:
%320 = arith.constant 90 : i32
%321 = llvm.load %214 : !llvm.ptr -> i64
%323 = arith.extsi %320 : i32 to i64
%322 = arith.muli %323, %321 : i64
%324 = arith.constant 41 : i32
%325 = llvm.load %229 : !llvm.ptr -> i64
%327 = arith.extsi %324 : i32 to i64
%326 = arith.muli %327, %325 : i64
%319 = func.call @gcd(%322, %326) : (i64, i64) -> i64
%328 = arith.constant 90 : i32
%329 = llvm.load %214 : !llvm.ptr -> i64
%331 = arith.extsi %328 : i32 to i64
%330 = arith.muli %331, %329 : i64
%332 = arith.divsi %330, %319 : i64
%333 = arith.constant 41 : i32
%334 = llvm.load %229 : !llvm.ptr -> i64
%336 = arith.extsi %333 : i32 to i64
%335 = arith.muli %336, %334 : i64
%337 = arith.divsi %335, %319 : i64
%338 = arith.divsi %182, %332 : i64
%339 = llvm.mlir.constant(1 : i64) : i64
%340 = llvm.alloca %339 x i64 : (i64) -> !llvm.ptr
llvm.store %338, %340 : i64, !llvm.ptr
%341 = arith.divsi %192, %337 : i64
%342 = llvm.load %340 : !llvm.ptr -> i64
%343 = arith.cmpi slt, %341, %342 : i64
cf.cond_br %343, ^bb72, ^bb73
^bb72:
%344 = arith.divsi %192, %337 : i64
llvm.store %344, %340 : i64, !llvm.ptr
cf.br ^bb74
^bb73:
cf.br ^bb74
^bb74:
%345 = llvm.load %340 : !llvm.ptr -> i64
%346 = arith.constant 1 : i32
%348 = arith.extsi %346 : i32 to i64
%347 = arith.cmpi sge, %345, %348 : i64
cf.cond_br %347, ^bb75, ^bb76
^bb75:
%349 = arith.constant 59 : i32
%351 = arith.extsi %349 : i32 to i64
%350 = arith.muli %202, %351 : i64
%352 = llvm.load %214 : !llvm.ptr -> i64
%353 = arith.muli %350, %352 : i64
%354 = arith.constant 41 : i32
%356 = arith.extsi %354 : i32 to i64
%355 = arith.muli %198, %356 : i64
%357 = llvm.load %229 : !llvm.ptr -> i64
%358 = arith.muli %355, %357 : i64
%359 = llvm.load %214 : !llvm.ptr -> i64
%360 = arith.muli %359, %353 : i64
%361 = llvm.load %229 : !llvm.ptr -> i64
%362 = arith.muli %361, %358 : i64
%363 = arith.subi %360, %362 : i64
%364 = arith.constant 0 : i32
%366 = arith.extsi %364 : i32 to i64
%365 = arith.cmpi ne, %363, %366 : i64
cf.cond_br %365, ^bb78, ^bb79
^bb78:
%367 = arith.muli %307, %358 : i64
%368 = arith.muli %302, %353 : i64
%369 = arith.subi %367, %368 : i64
%370 = arith.muli %337, %358 : i64
%371 = arith.muli %332, %353 : i64
%372 = arith.subi %370, %371 : i64
%373 = arith.constant 0 : i1
%374 = llvm.mlir.constant(1 : i64) : i64
%375 = llvm.alloca %374 x i1 : (i64) -> !llvm.ptr
llvm.store %373, %375 : i1, !llvm.ptr
%376 = arith.constant 1 : i32
%377 = arith.extsi %376 : i32 to i64
%378 = llvm.mlir.constant(1 : i64) : i64
%379 = llvm.alloca %378 x i64 : (i64) -> !llvm.ptr
llvm.store %377, %379 : i64, !llvm.ptr
cf.br ^bb81
^bb81:
%380 = llvm.load %379 : !llvm.ptr -> i64
%381 = llvm.load %310 : !llvm.ptr -> i64
%382 = arith.cmpi sle, %380, %381 : i64
%383 = scf.if %382 -> (i1) {
%384 = llvm.load %375 : !llvm.ptr -> i1
%386 = arith.constant 1 : i1
%385 = arith.xori %384, %386 : i1
scf.yield %385 : i1
} else {
%388 = arith.constant false
scf.yield %388 : i1
}
cf.cond_br %383, ^bb82, ^bb83
^bb82:
%389 = llvm.load %379 : !llvm.ptr -> i64
%390 = arith.muli %369, %389 : i64
%391 = arith.constant 1 : i32
%392 = arith.extsi %391 : i32 to i64
%393 = llvm.mlir.constant(1 : i64) : i64
%394 = llvm.alloca %393 x i64 : (i64) -> !llvm.ptr
llvm.store %392, %394 : i64, !llvm.ptr
cf.br ^bb84
^bb84:
%395 = llvm.load %394 : !llvm.ptr -> i64
%396 = llvm.load %340 : !llvm.ptr -> i64
%397 = arith.cmpi sle, %395, %396 : i64
cf.cond_br %397, ^bb85, ^bb86
^bb85:
%398 = llvm.load %394 : !llvm.ptr -> i64
%399 = arith.muli %372, %398 : i64
%400 = arith.addi %390, %399 : i64
%401 = arith.remsi %400, %363 : i64
%402 = arith.constant 0 : i32
%404 = arith.extsi %402 : i32 to i64
%403 = arith.cmpi eq, %401, %404 : i64
cf.cond_br %403, ^bb87, ^bb88
^bb87:
%405 = arith.divsi %400, %363 : i64
%406 = arith.constant 3 : i32
%408 = arith.extsi %406 : i32 to i64
%407 = arith.cmpi sge, %405, %408 : i64
%409 = scf.if %407 -> (i1) {
%410 = arith.cmpi sle, %405, %288 : i64
scf.yield %410 : i1
} else {
%411 = arith.constant false
scf.yield %411 : i1
}
cf.cond_br %409, ^bb90, ^bb91
^bb90:
%413 = arith.constant 41 : i32
%414 = llvm.load %229 : !llvm.ptr -> i64
%416 = arith.extsi %413 : i32 to i64
%415 = arith.muli %416, %414 : i64
%417 = arith.constant 59 : i32
%418 = llvm.load %214 : !llvm.ptr -> i64
%420 = arith.extsi %417 : i32 to i64
%419 = arith.muli %420, %418 : i64
%412 = func.call @gcd(%415, %419) : (i64, i64) -> i64
%421 = arith.constant 41 : i32
%422 = llvm.load %229 : !llvm.ptr -> i64
%424 = arith.extsi %421 : i32 to i64
%423 = arith.muli %424, %422 : i64
%425 = arith.divsi %423, %412 : i64
%426 = arith.constant 59 : i32
%427 = llvm.load %214 : !llvm.ptr -> i64
%429 = arith.extsi %426 : i32 to i64
%428 = arith.muli %429, %427 : i64
%430 = arith.divsi %428, %412 : i64
%431 = llvm.load %210 : !llvm.ptr -> i64
%432 = arith.muli %425, %431 : i64
%433 = llvm.load %206 : !llvm.ptr -> i64
%434 = arith.muli %433, %430 : i64
%435 = arith.cmpi sgt, %432, %434 : i64
cf.cond_br %435, ^bb93, ^bb94
^bb93:
llvm.store %425, %206 : i64, !llvm.ptr
llvm.store %430, %210 : i64, !llvm.ptr
cf.br ^bb95
^bb94:
cf.br ^bb95
^bb95:
%436 = arith.constant 1 : i1
llvm.store %436, %375 : i1, !llvm.ptr
cf.br ^bb86
^bb91:
cf.br ^bb92
^bb92:
cf.br ^bb89
^bb88:
cf.br ^bb89
^bb89:
%437 = llvm.load %394 : !llvm.ptr -> i64
%438 = arith.constant 1 : i32
%440 = arith.extsi %438 : i32 to i64
%439 = arith.addi %437, %440 : i64
llvm.store %439, %394 : i64, !llvm.ptr
cf.br ^bb84
^bb86:
%441 = llvm.load %379 : !llvm.ptr -> i64
%442 = arith.constant 1 : i32
%444 = arith.extsi %442 : i32 to i64
%443 = arith.addi %441, %444 : i64
llvm.store %443, %379 : i64, !llvm.ptr
cf.br ^bb81
^bb83:
cf.br ^bb80
^bb79:
cf.br ^bb80
^bb80:
cf.br ^bb77
^bb76:
cf.br ^bb77
^bb77:
cf.br ^bb71
^bb70:
cf.br ^bb71
^bb71:
cf.br ^bb65
^bb64:
cf.br ^bb65
^bb65:
cf.br ^bb53
^bb52:
cf.br ^bb53
^bb53:
%445 = llvm.load %229 : !llvm.ptr -> i64
%446 = arith.constant 1 : i32
%448 = arith.extsi %446 : i32 to i64
%447 = arith.addi %445, %448 : i64
llvm.store %447, %229 : i64, !llvm.ptr
cf.br ^bb48
^bb50:
cf.br ^bb47
^bb46:
cf.br ^bb47
^bb47:
%449 = llvm.load %214 : !llvm.ptr -> i64
%450 = arith.constant 1 : i32
%452 = arith.extsi %450 : i32 to i64
%451 = arith.addi %449, %452 : i64
llvm.store %451, %214 : i64, !llvm.ptr
cf.br ^bb42
^bb44:
%453 = llvm.mlir.addressof @str_0 : !llvm.ptr
%454 = llvm.load %206 : !llvm.ptr -> i64
%455 = llvm.load %210 : !llvm.ptr -> i64
%456 = llvm.call @printf(%453, %454, %455) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64, i64) -> i32
%457 = arith.constant 0 : i32
func.return %457 : i32
}
}