Problem 329
Prime frog croak probability as reduced fraction.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(2^n) | O(n * s^2) |
| Space complexity | O(n) | O(s^2) |
| Approach | Flow solution | Markov chain or DP over states |
| Verdict | Unknown |
Flow source
# Project Euler 329
# Prime frog croak probability as reduced fraction.
import euler.nt { gcd }
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function probability(square: i64, depth: i64, max_depth: i64, limit: i64, is_prime: ptr<i8>, seq: ptr<i8>, cache: ptr<i64>) -> i64 {
let mut chance: i64 = 1
let want_n: bool = seq[depth] == 78 # 'N'
let prime: bool = is_prime[square] == 1
if (prime && !want_n) || (!prime && want_n) {
chance = 2
}
if depth == max_depth {
return chance
}
let idx: i64 = square * max_depth + depth
if cache[idx] != 0 {
return cache[idx]
}
let mut left: i64 = square - 1
if left < 1 { left = 2 }
let mut right: i64 = square + 1
if right > limit { right = limit - 1 }
let result: i64 = chance * (
probability(left, depth + 1, max_depth, limit, is_prime, seq, cache) +
probability(right, depth + 1, max_depth, limit, is_prime, seq, cache)
)
cache[idx] = result
return result
}
function main() -> i32 {
let limit: i64 = 500
let max_depth: i64 = 15
let is_prime: ptr<i8> = calloc(limit + 1, 1)
let seq: ptr<i8> = calloc(16, 1)
let cache: ptr<i64> = calloc((max_depth + 1) * (limit + 1), 8)
if is_prime == null || seq == null || cache == null { return 1 }
# sequence " PPPPNNPPPNPPNPN" with leading space so index from 1
seq[0] = 32
let s: string = "PPPPNNPPPNPPNPN"
# manually
seq[1]=80; seq[2]=80; seq[3]=80; seq[4]=80; seq[5]=78; seq[6]=78; seq[7]=80
seq[8]=80; seq[9]=80; seq[10]=78; seq[11]=80; seq[12]=80; seq[13]=78; seq[14]=80; seq[15]=78
let mut i: i64 = 0
while i <= limit {
is_prime[i] = 1
i = i + 1
}
is_prime[0] = 0; is_prime[1] = 0
i = 2
while i * i <= limit {
if is_prime[i] == 1 {
let mut j: i64 = i * i
while j <= limit {
is_prime[j] = 0
j = j + i
}
}
i = i + 1
}
let mut total: i64 = 0
i = 1
while i <= limit {
total = total + probability(i, 1, max_depth, limit, is_prime, seq, cache)
i = i + 1
}
let mut denominator: i64 = limit
let mut d: i64 = 1
while d < max_depth {
denominator = denominator * 6
d = d + 1
}
denominator = denominator * 3
let g: i64 = gcd(total, denominator)
printf("%lld/%lld\n", total / g, denominator / g)
free(is_prime); free(seq); free(cache)
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);
int64_t probability_i64_i64_i64_i64_ptr_i8_ptr_i8_ptr_i64(int64_t square, int64_t depth, int64_t max_depth, int64_t limit, int8_t* is_prime, int8_t* seq, int64_t* cache);
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;
}
int64_t probability_i64_i64_i64_i64_ptr_i8_ptr_i8_ptr_i64(int64_t square, int64_t depth, int64_t max_depth, int64_t limit, int8_t* is_prime, int8_t* seq, int64_t* cache) {
int64_t chance = 1;
bool want_n = seq[depth] == 78;
bool prime = is_prime[square] == 1;
if (((prime && (!(want_n))) || ((!(prime)) && want_n))) {
chance = 2;
}
if (depth == max_depth) {
return chance;
}
int64_t idx = ((square * max_depth) + depth);
if (cache[idx] != 0) {
return cache[idx];
}
int64_t left = (square - 1);
if (left < 1) {
left = 2;
}
int64_t right = (square + 1);
if (right > limit) {
right = (limit - 1);
}
int64_t result = (chance * (probability_i64_i64_i64_i64_ptr_i8_ptr_i8_ptr_i64(left, (depth + 1), max_depth, limit, is_prime, seq, cache) + probability_i64_i64_i64_i64_ptr_i8_ptr_i8_ptr_i64(right, (depth + 1), max_depth, limit, is_prime, seq, cache)));
cache[idx] = result;
return result;
}
int32_t main(void) {
int64_t limit = 500;
int64_t max_depth = 15;
int8_t* is_prime = (int8_t*)(calloc((limit + 1), 1));
int8_t* seq = (int8_t*)(calloc(16, 1));
int64_t* cache = (int64_t*)(calloc(((max_depth + 1) * (limit + 1)), 8));
if (((is_prime == NULL || seq == NULL) || cache == NULL)) {
return 1;
}
seq[0] = 32;
char* s = "PPPPNNPPPNPPNPN";
seq[1] = 80;
seq[2] = 80;
seq[3] = 80;
seq[4] = 80;
seq[5] = 78;
seq[6] = 78;
seq[7] = 80;
seq[8] = 80;
seq[9] = 80;
seq[10] = 78;
seq[11] = 80;
seq[12] = 80;
seq[13] = 78;
seq[14] = 80;
seq[15] = 78;
int64_t i = 0;
while (i <= limit) {
is_prime[i] = 1;
i = (i + 1);
}
is_prime[0] = 0;
is_prime[1] = 0;
i = 2;
while ((i * i) <= limit) {
if (is_prime[i] == 1) {
int64_t j = (i * i);
while (j <= limit) {
is_prime[j] = 0;
j = (j + i);
}
}
i = (i + 1);
}
int64_t total = 0;
i = 1;
while (i <= limit) {
total = (total + probability_i64_i64_i64_i64_ptr_i8_ptr_i8_ptr_i64(i, 1, max_depth, limit, is_prime, seq, cache));
i = (i + 1);
}
int64_t denominator = limit;
int64_t d = 1;
while (d < max_depth) {
denominator = (denominator * 6);
d = (d + 1);
}
denominator = (denominator * 3);
int64_t g = gcd_i64_i64(total, denominator);
printf("%lld/%lld\n", FLOW_CHECKED_DIV((total), (g)), FLOW_CHECKED_DIV((denominator), (g)));
free(is_prime);
free(seq);
free(cache);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("PPPPNNPPPNPPNPN\00") {addr_space = 0 : i32} : !llvm.array<16 x i8>
llvm.mlir.global internal constant @str_1("%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 private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
func.func @probability(%arg0: i64, %arg1: i64, %arg2: i64, %arg3: i64, %arg4: !llvm.ptr, %arg5: !llvm.ptr, %arg6: !llvm.ptr) -> i64 {
%175 = arith.constant 1 : i32
%176 = arith.extsi %175 : i32 to i64
%177 = llvm.mlir.constant(1 : i64) : i64
%178 = llvm.alloca %177 x i64 : (i64) -> !llvm.ptr
llvm.store %176, %178 : i64, !llvm.ptr
%180 = llvm.getelementptr %arg5[%arg1] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%179 = llvm.load %180 : !llvm.ptr -> i8
%181 = arith.constant 78 : i32
%183 = arith.extsi %179 : i8 to i32
%182 = arith.cmpi eq, %183, %181 : i32
%185 = llvm.getelementptr %arg4[%arg0] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%184 = llvm.load %185 : !llvm.ptr -> i8
%186 = arith.constant 1 : i32
%188 = arith.extsi %184 : i8 to i32
%187 = arith.cmpi eq, %188, %186 : i32
%189 = scf.if %187 -> (i1) {
%191 = arith.constant 1 : i1
%190 = arith.xori %182, %191 : i1
scf.yield %190 : i1
} else {
%193 = arith.constant false
scf.yield %193 : i1
}
%194 = scf.if %189 -> (i1) {
%195 = arith.constant true
scf.yield %195 : i1
} else {
%197 = arith.constant 1 : i1
%196 = arith.xori %187, %197 : i1
%199 = scf.if %196 -> (i1) {
scf.yield %182 : i1
} else {
%200 = arith.constant false
scf.yield %200 : i1
}
scf.yield %199 : i1
}
cf.cond_br %194, ^bb42, ^bb43
^bb42:
%201 = arith.constant 2 : i32
%202 = arith.extsi %201 : i32 to i64
llvm.store %202, %178 : i64, !llvm.ptr
cf.br ^bb44
^bb43:
cf.br ^bb44
^bb44:
%203 = arith.cmpi eq, %arg1, %arg2 : i64
cf.cond_br %203, ^bb45, ^bb46
^bb45:
%204 = llvm.load %178 : !llvm.ptr -> i64
func.return %204 : i64
^bb46:
cf.br ^bb47
^bb47:
%205 = arith.muli %arg0, %arg2 : i64
%206 = arith.addi %205, %arg1 : i64
%208 = llvm.getelementptr %arg6[%206] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%207 = llvm.load %208 : !llvm.ptr -> i64
%209 = arith.constant 0 : i32
%211 = arith.extsi %209 : i32 to i64
%210 = arith.cmpi ne, %207, %211 : i64
cf.cond_br %210, ^bb48, ^bb49
^bb48:
%213 = llvm.getelementptr %arg6[%206] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%212 = llvm.load %213 : !llvm.ptr -> i64
func.return %212 : i64
^bb49:
cf.br ^bb50
^bb50:
%214 = arith.constant 1 : i32
%216 = arith.extsi %214 : i32 to i64
%215 = arith.subi %arg0, %216 : i64
%217 = llvm.mlir.constant(1 : i64) : i64
%218 = llvm.alloca %217 x i64 : (i64) -> !llvm.ptr
llvm.store %215, %218 : i64, !llvm.ptr
%219 = llvm.load %218 : !llvm.ptr -> i64
%220 = arith.constant 1 : i32
%222 = arith.extsi %220 : i32 to i64
%221 = arith.cmpi slt, %219, %222 : i64
cf.cond_br %221, ^bb51, ^bb52
^bb51:
%223 = arith.constant 2 : i32
%224 = arith.extsi %223 : i32 to i64
llvm.store %224, %218 : i64, !llvm.ptr
cf.br ^bb53
^bb52:
cf.br ^bb53
^bb53:
%225 = arith.constant 1 : i32
%227 = arith.extsi %225 : i32 to i64
%226 = arith.addi %arg0, %227 : i64
%228 = llvm.mlir.constant(1 : i64) : i64
%229 = llvm.alloca %228 x i64 : (i64) -> !llvm.ptr
llvm.store %226, %229 : i64, !llvm.ptr
%230 = llvm.load %229 : !llvm.ptr -> i64
%231 = arith.cmpi sgt, %230, %arg3 : i64
cf.cond_br %231, ^bb54, ^bb55
^bb54:
%232 = arith.constant 1 : i32
%234 = arith.extsi %232 : i32 to i64
%233 = arith.subi %arg3, %234 : i64
llvm.store %233, %229 : i64, !llvm.ptr
cf.br ^bb56
^bb55:
cf.br ^bb56
^bb56:
%235 = llvm.load %178 : !llvm.ptr -> i64
%237 = llvm.load %218 : !llvm.ptr -> i64
%238 = arith.constant 1 : i32
%240 = arith.extsi %238 : i32 to i64
%239 = arith.addi %arg1, %240 : i64
%236 = func.call @probability(%237, %239, %arg2, %arg3, %arg4, %arg5, %arg6) : (i64, i64, i64, i64, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> i64
%242 = llvm.load %229 : !llvm.ptr -> i64
%243 = arith.constant 1 : i32
%245 = arith.extsi %243 : i32 to i64
%244 = arith.addi %arg1, %245 : i64
%241 = func.call @probability(%242, %244, %arg2, %arg3, %arg4, %arg5, %arg6) : (i64, i64, i64, i64, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> i64
%246 = arith.addi %236, %241 : i64
%247 = arith.muli %235, %246 : i64
%248 = llvm.getelementptr %arg6[%206] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %247, %248 : i64, !llvm.ptr
func.return %247 : i64
}
func.func @main() -> i32 {
%249 = arith.constant 500 : i32
%250 = arith.extsi %249 : i32 to i64
%251 = arith.constant 15 : i32
%252 = arith.extsi %251 : i32 to i64
%254 = arith.constant 1 : i32
%256 = arith.extsi %254 : i32 to i64
%255 = arith.addi %250, %256 : i64
%257 = arith.constant 1 : i32
%258 = arith.extsi %257 : i32 to i64
%253 = func.call @calloc(%255, %258) : (i64, i64) -> !llvm.ptr
%260 = arith.constant 16 : i32
%261 = arith.constant 1 : i32
%262 = arith.extsi %260 : i32 to i64
%263 = arith.extsi %261 : i32 to i64
%259 = func.call @calloc(%262, %263) : (i64, i64) -> !llvm.ptr
%265 = arith.constant 1 : i32
%267 = arith.extsi %265 : i32 to i64
%266 = arith.addi %252, %267 : i64
%268 = arith.constant 1 : i32
%270 = arith.extsi %268 : i32 to i64
%269 = arith.addi %250, %270 : i64
%271 = arith.muli %266, %269 : i64
%272 = arith.constant 8 : i32
%273 = arith.extsi %272 : i32 to i64
%264 = func.call @calloc(%271, %273) : (i64, i64) -> !llvm.ptr
%274 = llvm.mlir.zero : !llvm.ptr
%275 = llvm.icmp "eq" %253, %274 : !llvm.ptr
%276 = scf.if %275 -> (i1) {
%277 = arith.constant true
scf.yield %277 : i1
} else {
%278 = llvm.mlir.zero : !llvm.ptr
%279 = llvm.icmp "eq" %259, %278 : !llvm.ptr
scf.yield %279 : i1
}
%280 = scf.if %276 -> (i1) {
%281 = arith.constant true
scf.yield %281 : i1
} else {
%282 = llvm.mlir.zero : !llvm.ptr
%283 = llvm.icmp "eq" %264, %282 : !llvm.ptr
scf.yield %283 : i1
}
cf.cond_br %280, ^bb57, ^bb58
^bb57:
%284 = arith.constant 1 : i32
func.return %284 : i32
^bb58:
cf.br ^bb59
^bb59:
%285 = arith.constant 32 : i32
%286 = arith.constant 0 : i32
%287 = arith.trunci %285 : i32 to i8
%288 = arith.extsi %286 : i32 to i64
%289 = llvm.getelementptr %259[%288] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %287, %289 : i8, !llvm.ptr
%290 = llvm.mlir.addressof @str_0 : !llvm.ptr
%291 = arith.constant 80 : i32
%292 = arith.constant 1 : i32
%293 = arith.trunci %291 : i32 to i8
%294 = arith.extsi %292 : i32 to i64
%295 = llvm.getelementptr %259[%294] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %293, %295 : i8, !llvm.ptr
%296 = arith.constant 80 : i32
%297 = arith.constant 2 : i32
%298 = arith.trunci %296 : i32 to i8
%299 = arith.extsi %297 : i32 to i64
%300 = llvm.getelementptr %259[%299] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %298, %300 : i8, !llvm.ptr
%301 = arith.constant 80 : i32
%302 = arith.constant 3 : i32
%303 = arith.trunci %301 : i32 to i8
%304 = arith.extsi %302 : i32 to i64
%305 = llvm.getelementptr %259[%304] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %303, %305 : i8, !llvm.ptr
%306 = arith.constant 80 : i32
%307 = arith.constant 4 : i32
%308 = arith.trunci %306 : i32 to i8
%309 = arith.extsi %307 : i32 to i64
%310 = llvm.getelementptr %259[%309] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %308, %310 : i8, !llvm.ptr
%311 = arith.constant 78 : i32
%312 = arith.constant 5 : i32
%313 = arith.trunci %311 : i32 to i8
%314 = arith.extsi %312 : i32 to i64
%315 = llvm.getelementptr %259[%314] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %313, %315 : i8, !llvm.ptr
%316 = arith.constant 78 : i32
%317 = arith.constant 6 : i32
%318 = arith.trunci %316 : i32 to i8
%319 = arith.extsi %317 : i32 to i64
%320 = llvm.getelementptr %259[%319] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %318, %320 : i8, !llvm.ptr
%321 = arith.constant 80 : i32
%322 = arith.constant 7 : i32
%323 = arith.trunci %321 : i32 to i8
%324 = arith.extsi %322 : i32 to i64
%325 = llvm.getelementptr %259[%324] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %323, %325 : i8, !llvm.ptr
%326 = arith.constant 80 : i32
%327 = arith.constant 8 : i32
%328 = arith.trunci %326 : i32 to i8
%329 = arith.extsi %327 : i32 to i64
%330 = llvm.getelementptr %259[%329] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %328, %330 : i8, !llvm.ptr
%331 = arith.constant 80 : i32
%332 = arith.constant 9 : i32
%333 = arith.trunci %331 : i32 to i8
%334 = arith.extsi %332 : i32 to i64
%335 = llvm.getelementptr %259[%334] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %333, %335 : i8, !llvm.ptr
%336 = arith.constant 78 : i32
%337 = arith.constant 10 : i32
%338 = arith.trunci %336 : i32 to i8
%339 = arith.extsi %337 : i32 to i64
%340 = llvm.getelementptr %259[%339] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %338, %340 : i8, !llvm.ptr
%341 = arith.constant 80 : i32
%342 = arith.constant 11 : i32
%343 = arith.trunci %341 : i32 to i8
%344 = arith.extsi %342 : i32 to i64
%345 = llvm.getelementptr %259[%344] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %343, %345 : i8, !llvm.ptr
%346 = arith.constant 80 : i32
%347 = arith.constant 12 : i32
%348 = arith.trunci %346 : i32 to i8
%349 = arith.extsi %347 : i32 to i64
%350 = llvm.getelementptr %259[%349] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %348, %350 : i8, !llvm.ptr
%351 = arith.constant 78 : i32
%352 = arith.constant 13 : i32
%353 = arith.trunci %351 : i32 to i8
%354 = arith.extsi %352 : i32 to i64
%355 = llvm.getelementptr %259[%354] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %353, %355 : i8, !llvm.ptr
%356 = arith.constant 80 : i32
%357 = arith.constant 14 : i32
%358 = arith.trunci %356 : i32 to i8
%359 = arith.extsi %357 : i32 to i64
%360 = llvm.getelementptr %259[%359] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %358, %360 : i8, !llvm.ptr
%361 = arith.constant 78 : i32
%362 = arith.constant 15 : i32
%363 = arith.trunci %361 : i32 to i8
%364 = arith.extsi %362 : i32 to i64
%365 = llvm.getelementptr %259[%364] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %363, %365 : i8, !llvm.ptr
%366 = arith.constant 0 : i32
%367 = arith.extsi %366 : i32 to i64
%368 = llvm.mlir.constant(1 : i64) : i64
%369 = llvm.alloca %368 x i64 : (i64) -> !llvm.ptr
llvm.store %367, %369 : i64, !llvm.ptr
cf.br ^bb60
^bb60:
%370 = llvm.load %369 : !llvm.ptr -> i64
%371 = arith.cmpi sle, %370, %250 : i64
cf.cond_br %371, ^bb61, ^bb62
^bb61:
%372 = arith.constant 1 : i32
%373 = llvm.load %369 : !llvm.ptr -> i64
%374 = arith.trunci %372 : i32 to i8
%375 = llvm.getelementptr %253[%373] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %374, %375 : i8, !llvm.ptr
%376 = llvm.load %369 : !llvm.ptr -> i64
%377 = arith.constant 1 : i32
%379 = arith.extsi %377 : i32 to i64
%378 = arith.addi %376, %379 : i64
llvm.store %378, %369 : i64, !llvm.ptr
cf.br ^bb60
^bb62:
%380 = arith.constant 0 : i32
%381 = arith.constant 0 : i32
%382 = arith.trunci %380 : i32 to i8
%383 = arith.extsi %381 : i32 to i64
%384 = llvm.getelementptr %253[%383] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %382, %384 : i8, !llvm.ptr
%385 = arith.constant 0 : i32
%386 = arith.constant 1 : i32
%387 = arith.trunci %385 : i32 to i8
%388 = arith.extsi %386 : i32 to i64
%389 = llvm.getelementptr %253[%388] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %387, %389 : i8, !llvm.ptr
%390 = arith.constant 2 : i32
%391 = arith.extsi %390 : i32 to i64
llvm.store %391, %369 : i64, !llvm.ptr
cf.br ^bb63
^bb63:
%392 = llvm.load %369 : !llvm.ptr -> i64
%393 = llvm.load %369 : !llvm.ptr -> i64
%394 = arith.muli %392, %393 : i64
%395 = arith.cmpi sle, %394, %250 : i64
cf.cond_br %395, ^bb64, ^bb65
^bb64:
%397 = llvm.load %369 : !llvm.ptr -> i64
%398 = llvm.getelementptr %253[%397] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%396 = llvm.load %398 : !llvm.ptr -> i8
%399 = arith.constant 1 : i32
%401 = arith.extsi %396 : i8 to i32
%400 = arith.cmpi eq, %401, %399 : i32
cf.cond_br %400, ^bb66, ^bb67
^bb66:
%402 = llvm.load %369 : !llvm.ptr -> i64
%403 = llvm.load %369 : !llvm.ptr -> i64
%404 = arith.muli %402, %403 : i64
%405 = llvm.mlir.constant(1 : i64) : i64
%406 = llvm.alloca %405 x i64 : (i64) -> !llvm.ptr
llvm.store %404, %406 : i64, !llvm.ptr
cf.br ^bb69
^bb69:
%407 = llvm.load %406 : !llvm.ptr -> i64
%408 = arith.cmpi sle, %407, %250 : i64
cf.cond_br %408, ^bb70, ^bb71
^bb70:
%409 = arith.constant 0 : i32
%410 = llvm.load %406 : !llvm.ptr -> i64
%411 = arith.trunci %409 : i32 to i8
%412 = llvm.getelementptr %253[%410] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %411, %412 : i8, !llvm.ptr
%413 = llvm.load %406 : !llvm.ptr -> i64
%414 = llvm.load %369 : !llvm.ptr -> i64
%415 = arith.addi %413, %414 : i64
llvm.store %415, %406 : i64, !llvm.ptr
cf.br ^bb69
^bb71:
cf.br ^bb68
^bb67:
cf.br ^bb68
^bb68:
%416 = llvm.load %369 : !llvm.ptr -> i64
%417 = arith.constant 1 : i32
%419 = arith.extsi %417 : i32 to i64
%418 = arith.addi %416, %419 : i64
llvm.store %418, %369 : i64, !llvm.ptr
cf.br ^bb63
^bb65:
%420 = arith.constant 0 : i32
%421 = arith.extsi %420 : i32 to i64
%422 = llvm.mlir.constant(1 : i64) : i64
%423 = llvm.alloca %422 x i64 : (i64) -> !llvm.ptr
llvm.store %421, %423 : i64, !llvm.ptr
%424 = arith.constant 1 : i32
%425 = arith.extsi %424 : i32 to i64
llvm.store %425, %369 : i64, !llvm.ptr
cf.br ^bb72
^bb72:
%426 = llvm.load %369 : !llvm.ptr -> i64
%427 = arith.cmpi sle, %426, %250 : i64
cf.cond_br %427, ^bb73, ^bb74
^bb73:
%428 = llvm.load %423 : !llvm.ptr -> i64
%430 = llvm.load %369 : !llvm.ptr -> i64
%431 = arith.constant 1 : i32
%432 = arith.extsi %431 : i32 to i64
%429 = func.call @probability(%430, %432, %252, %250, %253, %259, %264) : (i64, i64, i64, i64, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> i64
%433 = arith.addi %428, %429 : i64
llvm.store %433, %423 : i64, !llvm.ptr
%434 = llvm.load %369 : !llvm.ptr -> i64
%435 = arith.constant 1 : i32
%437 = arith.extsi %435 : i32 to i64
%436 = arith.addi %434, %437 : i64
llvm.store %436, %369 : i64, !llvm.ptr
cf.br ^bb72
^bb74:
%438 = llvm.mlir.constant(1 : i64) : i64
%439 = llvm.alloca %438 x i64 : (i64) -> !llvm.ptr
llvm.store %250, %439 : i64, !llvm.ptr
%440 = arith.constant 1 : i32
%441 = arith.extsi %440 : i32 to i64
%442 = llvm.mlir.constant(1 : i64) : i64
%443 = llvm.alloca %442 x i64 : (i64) -> !llvm.ptr
llvm.store %441, %443 : i64, !llvm.ptr
cf.br ^bb75
^bb75:
%444 = llvm.load %443 : !llvm.ptr -> i64
%445 = arith.cmpi slt, %444, %252 : i64
cf.cond_br %445, ^bb76, ^bb77
^bb76:
%446 = llvm.load %439 : !llvm.ptr -> i64
%447 = arith.constant 6 : i32
%449 = arith.extsi %447 : i32 to i64
%448 = arith.muli %446, %449 : i64
llvm.store %448, %439 : i64, !llvm.ptr
%450 = llvm.load %443 : !llvm.ptr -> i64
%451 = arith.constant 1 : i32
%453 = arith.extsi %451 : i32 to i64
%452 = arith.addi %450, %453 : i64
llvm.store %452, %443 : i64, !llvm.ptr
cf.br ^bb75
^bb77:
%454 = llvm.load %439 : !llvm.ptr -> i64
%455 = arith.constant 3 : i32
%457 = arith.extsi %455 : i32 to i64
%456 = arith.muli %454, %457 : i64
llvm.store %456, %439 : i64, !llvm.ptr
%459 = llvm.load %423 : !llvm.ptr -> i64
%460 = llvm.load %439 : !llvm.ptr -> i64
%458 = func.call @gcd(%459, %460) : (i64, i64) -> i64
%461 = llvm.mlir.addressof @str_1 : !llvm.ptr
%462 = llvm.load %423 : !llvm.ptr -> i64
%463 = arith.divsi %462, %458 : i64
%464 = llvm.load %439 : !llvm.ptr -> i64
%465 = arith.divsi %464, %458 : i64
%466 = llvm.call @printf(%461, %463, %465) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64, i64) -> i32
func.call @free(%253) : (!llvm.ptr) -> ()
func.call @free(%259) : (!llvm.ptr) -> ()
func.call @free(%264) : (!llvm.ptr) -> ()
%470 = arith.constant 0 : i32
func.return %470 : i32
}
}