Problem 281

Sum f(m,n) for f(m,n) <= 10^15.

Answer1485776387445623
Output1485776387445623
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^5) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^5)O(n^2)
Space complexityO(1)O(n)
ApproachFlow solutionDouble enumeration
VerdictUnknown

Flow source

# Project Euler 281
# Sum f(m,n) for f(m,n) <= 10^15.

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
}

function phi(n0: i64) -> i64 {
    if n0 == 1 { return 1 }
    let mut n: i64 = n0
    let mut result: i64 = 1
    let mut d: i64 = 2
    while n > 1 {
        let mut count: i64 = 0
        while n % d == 0 {
            count = count + 1
            n = n / d
        }
        if count > 0 {
            let mut p: i64 = 1
            let mut i: i64 = 0
            while i < count - 1 {
                p = p * d
                i = i + 1
            }
            result = result * p * (d - 1)
        }
        d = d + 1
        if d * d > n {
            if n > 1 {
                result = result * (n - 1)
            }
            break
        }
    }
    return result
}

function factorial(n: i64) -> i64 {
    let mut r: i64 = 1
    let mut i: i64 = 2
    while i <= n {
        r = r * i
        i = i + 1
    }
    return r
}

function f_mn(m: i64, n: i64) -> i64 {
    let mut total: i64 = 0
    let mut d: i64 = 1
    while d <= n {
        if n % d == 0 {
            # phi(n/d) * (m*d)! / (d!)^m
            let ph: i64 = phi(n / d)
            let mut num: i64 = factorial(m * d)
            let den_one: i64 = factorial(d)
            let mut den: i64 = 1
            let mut i: i64 = 0
            while i < m {
                den = den * den_one
                i = i + 1
            }
            total = total + ph * (num / den)
        }
        d = d + 1
    }
    return total / (m * n)
}

function main() -> i32 {
    let limit: i64 = 1000000000000000
    let mut total: i64 = 0
    let mut m: i64 = 2
    while m < 100 {
        let mut n: i64 = 1
        while n < 100 {
            # avoid factorial overflow: m*d with d|n, m*n small enough
            if m * n > 20 {
                # factorials get huge; use careful multiplicative formula
                break
            }
            let t: i64 = f_mn(m, n)
            if t <= limit {
                total = total + t
            } else {
                break
            }
            n = n + 1
        }
        m = m + 1
    }
    # Recompute properly with big-safe multiplicative binomial-style
    # Restart with safer f
    total = 0
    m = 2
    while m < 40 {
        let mut n: i64 = 1
        while n < 40 {
            let mut s: i64 = 0
            let mut d: i64 = 1
            let mut overflow: bool = false
            while d <= n {
                if n % d == 0 {
                    let ph: i64 = phi(n / d)
                    # compute (m*d)! / (d!)^m multiplicatively
                    let mut val: i64 = 1
                    let md: i64 = m * d
                    # val = C(md, d) * C(md-d, d) * ... * C(d, d) = (md)!/(d!)^m
                    let mut take: i64 = 0
                    while take < m {
                        let start: i64 = take * d + 1
                        let end: i64 = (take + 1) * d
                        # multiply by C(end, d) wait: sequential
                        # Better: multiply (take*d+1)..(take*d+d) / 1..d
                        let mut num_i: i64 = 1
                        while num_i <= d {
                            # multiply (take*d + num_i) / num_i
                            # check overflow
                            let mul: i64 = take * d + num_i
                            if val > 2000000000000000000 / mul {
                                overflow = true
                                break
                            }
                            val = val * mul / num_i
                            num_i = num_i + 1
                        }
                        if overflow { break }
                        take = take + 1
                    }
                    if overflow { break }
                    if ph > 0 && val > 2000000000000000000 / ph {
                        overflow = true
                        break
                    }
                    s = s + ph * val
                }
                d = d + 1
            }
            if overflow { break }
            let t: i64 = s / (m * n)
            if t <= 0 { break }
            if t <= limit {
                total = total + t
            } else {
                break
            }
            n = n + 1
        }
        m = m + 1
    }
    printf("%lld\n", total)
    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 phi_i64(int64_t n0);
int64_t factorial_i64(int64_t n);
int64_t f_mn_i64_i64(int64_t m, int64_t n);
int32_t main(void);



int64_t phi_i64(int64_t n0) {
    if (n0 == 1) {
        return 1;
    }
    int64_t n = n0;
    int64_t result = 1;
    int64_t d = 2;
    while (n > 1) {
        int64_t count = 0;
        while (FLOW_CHECKED_MOD((n), (d)) == 0) {
            count = (count + 1);
            n = FLOW_CHECKED_DIV((n), (d));
        }
        if (count > 0) {
            int64_t p = 1;
            int64_t i = 0;
            while (i < (count - 1)) {
                p = (p * d);
                i = (i + 1);
            }
            result = ((result * p) * (d - 1));
        }
        d = (d + 1);
        if ((d * d) > n) {
            if (n > 1) {
                result = (result * (n - 1));
            }
            break;
        }
    }
    return result;
}

int64_t factorial_i64(int64_t n) {
    int64_t r = 1;
    int64_t i = 2;
    while (i <= n) {
        r = (r * i);
        i = (i + 1);
    }
    return r;
}

int64_t f_mn_i64_i64(int64_t m, int64_t n) {
    int64_t total = 0;
    int64_t d = 1;
    while (d <= n) {
        if (FLOW_CHECKED_MOD((n), (d)) == 0) {
            int64_t ph = phi_i64(FLOW_CHECKED_DIV((n), (d)));
            int64_t num = factorial_i64((m * d));
            int64_t den_one = factorial_i64(d);
            int64_t den = 1;
            int64_t i = 0;
            while (i < m) {
                den = (den * den_one);
                i = (i + 1);
            }
            total = (total + (ph * FLOW_CHECKED_DIV((num), (den))));
        }
        d = (d + 1);
    }
    return FLOW_CHECKED_DIV((total), ((m * n)));
}

int32_t main(void) {
    int64_t limit = 1000000000000000;
    int64_t total = 0;
    int64_t m = 2;
    while (m < 100) {
        int64_t n = 1;
        while (n < 100) {
            if ((m * n) > 20) {
                break;
            }
            int64_t t = f_mn_i64_i64(m, n);
            if (t <= limit) {
                total = (total + t);
            } else {
                break;
            }
            n = (n + 1);
        }
        m = (m + 1);
    }
    total = 0;
    m = 2;
    while (m < 40) {
        int64_t n = 1;
        while (n < 40) {
            int64_t s = 0;
            int64_t d = 1;
            bool overflow = 0;
            while (d <= n) {
                if (FLOW_CHECKED_MOD((n), (d)) == 0) {
                    int64_t ph = phi_i64(FLOW_CHECKED_DIV((n), (d)));
                    int64_t val = 1;
                    int64_t md = (m * d);
                    int64_t take = 0;
                    while (take < m) {
                        int64_t start = ((take * d) + 1);
                        int64_t end = ((take + 1) * d);
                        int64_t num_i = 1;
                        while (num_i <= d) {
                            int64_t mul = ((take * d) + num_i);
                            if (val > FLOW_CHECKED_DIV((2000000000000000000), (mul))) {
                                overflow = 1;
                                break;
                            }
                            val = FLOW_CHECKED_DIV(((val * mul)), (num_i));
                            num_i = (num_i + 1);
                        }
                        if (overflow) {
                            break;
                        }
                        take = (take + 1);
                    }
                    if (overflow) {
                        break;
                    }
                    if ((ph > 0 && val > FLOW_CHECKED_DIV((2000000000000000000), (ph)))) {
                        overflow = 1;
                        break;
                    }
                    s = (s + (ph * val));
                }
                d = (d + 1);
            }
            if (overflow) {
                break;
            }
            int64_t t = FLOW_CHECKED_DIV((s), ((m * n)));
            if (t <= 0) {
                break;
            }
            if (t <= limit) {
                total = (total + t);
            } else {
                break;
            }
            n = (n + 1);
        }
        m = (m + 1);
    }
    printf("%lld\n", total);
    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 @phi(%arg0: i64) -> i64 {
    %0 = arith.constant 1 : i32
    %2 = arith.extsi %0 : i32 to i64
    %1 = arith.cmpi eq, %arg0, %2 : i64
    cf.cond_br %1, ^bb0, ^bb1
    ^bb0:
      %3 = arith.constant 1 : i32
      %4 = arith.extsi %3 : i32 to i64
      func.return %4 : i64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %5 = llvm.mlir.constant(1 : i64) : i64
    %6 = llvm.alloca %5 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %6 : i64, !llvm.ptr
    %7 = arith.constant 1 : i32
    %8 = arith.extsi %7 : i32 to i64
    %9 = llvm.mlir.constant(1 : i64) : i64
    %10 = llvm.alloca %9 x i64 : (i64) -> !llvm.ptr
    llvm.store %8, %10 : i64, !llvm.ptr
    %11 = arith.constant 2 : i32
    %12 = arith.extsi %11 : i32 to i64
    %13 = llvm.mlir.constant(1 : i64) : i64
    %14 = llvm.alloca %13 x i64 : (i64) -> !llvm.ptr
    llvm.store %12, %14 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %15 = llvm.load %6 : !llvm.ptr -> i64
    %16 = arith.constant 1 : i32
    %18 = arith.extsi %16 : i32 to i64
    %17 = arith.cmpi sgt, %15, %18 : i64
    cf.cond_br %17, ^bb4, ^bb5
    ^bb4:
      %19 = arith.constant 0 : i32
      %20 = arith.extsi %19 : i32 to i64
      %21 = llvm.mlir.constant(1 : i64) : i64
      %22 = llvm.alloca %21 x i64 : (i64) -> !llvm.ptr
      llvm.store %20, %22 : i64, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %23 = llvm.load %6 : !llvm.ptr -> i64
      %24 = llvm.load %14 : !llvm.ptr -> i64
      %25 = arith.remsi %23, %24 : i64
      %26 = arith.constant 0 : i32
      %28 = arith.extsi %26 : i32 to i64
      %27 = arith.cmpi eq, %25, %28 : i64
      cf.cond_br %27, ^bb7, ^bb8
      ^bb7:
        %29 = llvm.load %22 : !llvm.ptr -> i64
        %30 = arith.constant 1 : i32
        %32 = arith.extsi %30 : i32 to i64
        %31 = arith.addi %29, %32 : i64
        llvm.store %31, %22 : i64, !llvm.ptr
        %33 = llvm.load %6 : !llvm.ptr -> i64
        %34 = llvm.load %14 : !llvm.ptr -> i64
        %35 = arith.divsi %33, %34 : i64
        llvm.store %35, %6 : i64, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %36 = llvm.load %22 : !llvm.ptr -> i64
      %37 = arith.constant 0 : i32
      %39 = arith.extsi %37 : i32 to i64
      %38 = arith.cmpi sgt, %36, %39 : i64
      cf.cond_br %38, ^bb9, ^bb10
      ^bb9:
        %40 = arith.constant 1 : i32
        %41 = arith.extsi %40 : i32 to i64
        %42 = llvm.mlir.constant(1 : i64) : i64
        %43 = llvm.alloca %42 x i64 : (i64) -> !llvm.ptr
        llvm.store %41, %43 : i64, !llvm.ptr
        %44 = arith.constant 0 : i32
        %45 = arith.extsi %44 : i32 to i64
        %46 = llvm.mlir.constant(1 : i64) : i64
        %47 = llvm.alloca %46 x i64 : (i64) -> !llvm.ptr
        llvm.store %45, %47 : i64, !llvm.ptr
        cf.br ^bb12
        ^bb12:
        %48 = llvm.load %47 : !llvm.ptr -> i64
        %49 = llvm.load %22 : !llvm.ptr -> i64
        %50 = arith.constant 1 : i32
        %52 = arith.extsi %50 : i32 to i64
        %51 = arith.subi %49, %52 : i64
        %53 = arith.cmpi slt, %48, %51 : i64
        cf.cond_br %53, ^bb13, ^bb14
        ^bb13:
          %54 = llvm.load %43 : !llvm.ptr -> i64
          %55 = llvm.load %14 : !llvm.ptr -> i64
          %56 = arith.muli %54, %55 : i64
          llvm.store %56, %43 : i64, !llvm.ptr
          %57 = llvm.load %47 : !llvm.ptr -> i64
          %58 = arith.constant 1 : i32
          %60 = arith.extsi %58 : i32 to i64
          %59 = arith.addi %57, %60 : i64
          llvm.store %59, %47 : i64, !llvm.ptr
          cf.br ^bb12
        ^bb14:
        %61 = llvm.load %10 : !llvm.ptr -> i64
        %62 = llvm.load %43 : !llvm.ptr -> i64
        %63 = arith.muli %61, %62 : i64
        %64 = llvm.load %14 : !llvm.ptr -> i64
        %65 = arith.constant 1 : i32
        %67 = arith.extsi %65 : i32 to i64
        %66 = arith.subi %64, %67 : i64
        %68 = arith.muli %63, %66 : i64
        llvm.store %68, %10 : i64, !llvm.ptr
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %69 = llvm.load %14 : !llvm.ptr -> i64
      %70 = arith.constant 1 : i32
      %72 = arith.extsi %70 : i32 to i64
      %71 = arith.addi %69, %72 : i64
      llvm.store %71, %14 : i64, !llvm.ptr
      %73 = llvm.load %14 : !llvm.ptr -> i64
      %74 = llvm.load %14 : !llvm.ptr -> i64
      %75 = arith.muli %73, %74 : i64
      %76 = llvm.load %6 : !llvm.ptr -> i64
      %77 = arith.cmpi sgt, %75, %76 : i64
      cf.cond_br %77, ^bb15, ^bb16
      ^bb15:
        %78 = llvm.load %6 : !llvm.ptr -> i64
        %79 = arith.constant 1 : i32
        %81 = arith.extsi %79 : i32 to i64
        %80 = arith.cmpi sgt, %78, %81 : i64
        cf.cond_br %80, ^bb18, ^bb19
        ^bb18:
          %82 = llvm.load %10 : !llvm.ptr -> i64
          %83 = llvm.load %6 : !llvm.ptr -> i64
          %84 = arith.constant 1 : i32
          %86 = arith.extsi %84 : i32 to i64
          %85 = arith.subi %83, %86 : i64
          %87 = arith.muli %82, %85 : i64
          llvm.store %87, %10 : i64, !llvm.ptr
          cf.br ^bb20
        ^bb19:
          cf.br ^bb20
        ^bb20:
        cf.br ^bb5
      ^bb16:
        cf.br ^bb17
      ^bb17:
      cf.br ^bb3
    ^bb5:
    %88 = llvm.load %10 : !llvm.ptr -> i64
    func.return %88 : i64
  }
  func.func @factorial(%arg0: i64) -> i64 {
    %89 = arith.constant 1 : i32
    %90 = arith.extsi %89 : i32 to i64
    %91 = llvm.mlir.constant(1 : i64) : i64
    %92 = llvm.alloca %91 x i64 : (i64) -> !llvm.ptr
    llvm.store %90, %92 : i64, !llvm.ptr
    %93 = arith.constant 2 : i32
    %94 = arith.extsi %93 : i32 to i64
    %95 = llvm.mlir.constant(1 : i64) : i64
    %96 = llvm.alloca %95 x i64 : (i64) -> !llvm.ptr
    llvm.store %94, %96 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %97 = llvm.load %96 : !llvm.ptr -> i64
    %98 = arith.cmpi sle, %97, %arg0 : i64
    cf.cond_br %98, ^bb22, ^bb23
    ^bb22:
      %99 = llvm.load %92 : !llvm.ptr -> i64
      %100 = llvm.load %96 : !llvm.ptr -> i64
      %101 = arith.muli %99, %100 : i64
      llvm.store %101, %92 : i64, !llvm.ptr
      %102 = llvm.load %96 : !llvm.ptr -> i64
      %103 = arith.constant 1 : i32
      %105 = arith.extsi %103 : i32 to i64
      %104 = arith.addi %102, %105 : i64
      llvm.store %104, %96 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %106 = llvm.load %92 : !llvm.ptr -> i64
    func.return %106 : i64
  }
  func.func @f_mn(%arg0: i64, %arg1: i64) -> i64 {
    %107 = arith.constant 0 : i32
    %108 = arith.extsi %107 : i32 to i64
    %109 = llvm.mlir.constant(1 : i64) : i64
    %110 = llvm.alloca %109 x i64 : (i64) -> !llvm.ptr
    llvm.store %108, %110 : i64, !llvm.ptr
    %111 = arith.constant 1 : i32
    %112 = arith.extsi %111 : i32 to i64
    %113 = llvm.mlir.constant(1 : i64) : i64
    %114 = llvm.alloca %113 x i64 : (i64) -> !llvm.ptr
    llvm.store %112, %114 : i64, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %115 = llvm.load %114 : !llvm.ptr -> i64
    %116 = arith.cmpi sle, %115, %arg1 : i64
    cf.cond_br %116, ^bb25, ^bb26
    ^bb25:
      %117 = llvm.load %114 : !llvm.ptr -> i64
      %118 = arith.remsi %arg1, %117 : i64
      %119 = arith.constant 0 : i32
      %121 = arith.extsi %119 : i32 to i64
      %120 = arith.cmpi eq, %118, %121 : i64
      cf.cond_br %120, ^bb27, ^bb28
      ^bb27:
        %123 = llvm.load %114 : !llvm.ptr -> i64
        %124 = arith.divsi %arg1, %123 : i64
        %122 = func.call @phi(%124) : (i64) -> i64
        %126 = llvm.load %114 : !llvm.ptr -> i64
        %127 = arith.muli %arg0, %126 : i64
        %125 = func.call @factorial(%127) : (i64) -> i64
        %128 = llvm.mlir.constant(1 : i64) : i64
        %129 = llvm.alloca %128 x i64 : (i64) -> !llvm.ptr
        llvm.store %125, %129 : i64, !llvm.ptr
        %131 = llvm.load %114 : !llvm.ptr -> i64
        %130 = func.call @factorial(%131) : (i64) -> i64
        %132 = arith.constant 1 : i32
        %133 = arith.extsi %132 : i32 to i64
        %134 = llvm.mlir.constant(1 : i64) : i64
        %135 = llvm.alloca %134 x i64 : (i64) -> !llvm.ptr
        llvm.store %133, %135 : i64, !llvm.ptr
        %136 = arith.constant 0 : i32
        %137 = arith.extsi %136 : i32 to i64
        %138 = llvm.mlir.constant(1 : i64) : i64
        %139 = llvm.alloca %138 x i64 : (i64) -> !llvm.ptr
        llvm.store %137, %139 : i64, !llvm.ptr
        cf.br ^bb30
        ^bb30:
        %140 = llvm.load %139 : !llvm.ptr -> i64
        %141 = arith.cmpi slt, %140, %arg0 : i64
        cf.cond_br %141, ^bb31, ^bb32
        ^bb31:
          %142 = llvm.load %135 : !llvm.ptr -> i64
          %143 = arith.muli %142, %130 : i64
          llvm.store %143, %135 : i64, !llvm.ptr
          %144 = llvm.load %139 : !llvm.ptr -> i64
          %145 = arith.constant 1 : i32
          %147 = arith.extsi %145 : i32 to i64
          %146 = arith.addi %144, %147 : i64
          llvm.store %146, %139 : i64, !llvm.ptr
          cf.br ^bb30
        ^bb32:
        %148 = llvm.load %110 : !llvm.ptr -> i64
        %149 = llvm.load %129 : !llvm.ptr -> i64
        %150 = llvm.load %135 : !llvm.ptr -> i64
        %151 = arith.divsi %149, %150 : i64
        %152 = arith.muli %122, %151 : i64
        %153 = arith.addi %148, %152 : i64
        llvm.store %153, %110 : i64, !llvm.ptr
        cf.br ^bb29
      ^bb28:
        cf.br ^bb29
      ^bb29:
      %154 = llvm.load %114 : !llvm.ptr -> i64
      %155 = arith.constant 1 : i32
      %157 = arith.extsi %155 : i32 to i64
      %156 = arith.addi %154, %157 : i64
      llvm.store %156, %114 : i64, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %158 = llvm.load %110 : !llvm.ptr -> i64
    %159 = arith.muli %arg0, %arg1 : i64
    %160 = arith.divsi %158, %159 : i64
    func.return %160 : i64
  }
  func.func @main() -> i32 {
    %161 = arith.constant 999995705032704 : i32
    %162 = arith.extsi %161 : i32 to i64
    %163 = arith.constant 0 : i32
    %164 = arith.extsi %163 : i32 to i64
    %165 = llvm.mlir.constant(1 : i64) : i64
    %166 = llvm.alloca %165 x i64 : (i64) -> !llvm.ptr
    llvm.store %164, %166 : i64, !llvm.ptr
    %167 = arith.constant 2 : i32
    %168 = arith.extsi %167 : i32 to i64
    %169 = llvm.mlir.constant(1 : i64) : i64
    %170 = llvm.alloca %169 x i64 : (i64) -> !llvm.ptr
    llvm.store %168, %170 : i64, !llvm.ptr
    cf.br ^bb33
    ^bb33:
    %171 = llvm.load %170 : !llvm.ptr -> i64
    %172 = arith.constant 100 : i32
    %174 = arith.extsi %172 : i32 to i64
    %173 = arith.cmpi slt, %171, %174 : i64
    cf.cond_br %173, ^bb34, ^bb35
    ^bb34:
      %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
      cf.br ^bb36
      ^bb36:
      %179 = llvm.load %178 : !llvm.ptr -> i64
      %180 = arith.constant 100 : i32
      %182 = arith.extsi %180 : i32 to i64
      %181 = arith.cmpi slt, %179, %182 : i64
      cf.cond_br %181, ^bb37, ^bb38
      ^bb37:
        %183 = llvm.load %170 : !llvm.ptr -> i64
        %184 = llvm.load %178 : !llvm.ptr -> i64
        %185 = arith.muli %183, %184 : i64
        %186 = arith.constant 20 : i32
        %188 = arith.extsi %186 : i32 to i64
        %187 = arith.cmpi sgt, %185, %188 : i64
        cf.cond_br %187, ^bb39, ^bb40
        ^bb39:
          cf.br ^bb38
        ^bb40:
          cf.br ^bb41
        ^bb41:
        %190 = llvm.load %170 : !llvm.ptr -> i64
        %191 = llvm.load %178 : !llvm.ptr -> i64
        %189 = func.call @f_mn(%190, %191) : (i64, i64) -> i64
        %192 = arith.cmpi sle, %189, %162 : i64
        cf.cond_br %192, ^bb42, ^bb43
        ^bb42:
          %193 = llvm.load %166 : !llvm.ptr -> i64
          %194 = arith.addi %193, %189 : i64
          llvm.store %194, %166 : i64, !llvm.ptr
          cf.br ^bb44
        ^bb43:
          cf.br ^bb38
        ^bb44:
        %195 = llvm.load %178 : !llvm.ptr -> i64
        %196 = arith.constant 1 : i32
        %198 = arith.extsi %196 : i32 to i64
        %197 = arith.addi %195, %198 : i64
        llvm.store %197, %178 : i64, !llvm.ptr
        cf.br ^bb36
      ^bb38:
      %199 = llvm.load %170 : !llvm.ptr -> i64
      %200 = arith.constant 1 : i32
      %202 = arith.extsi %200 : i32 to i64
      %201 = arith.addi %199, %202 : i64
      llvm.store %201, %170 : i64, !llvm.ptr
      cf.br ^bb33
    ^bb35:
    %203 = arith.constant 0 : i32
    %204 = arith.extsi %203 : i32 to i64
    llvm.store %204, %166 : i64, !llvm.ptr
    %205 = arith.constant 2 : i32
    %206 = arith.extsi %205 : i32 to i64
    llvm.store %206, %170 : i64, !llvm.ptr
    cf.br ^bb45
    ^bb45:
    %207 = llvm.load %170 : !llvm.ptr -> i64
    %208 = arith.constant 40 : i32
    %210 = arith.extsi %208 : i32 to i64
    %209 = arith.cmpi slt, %207, %210 : i64
    cf.cond_br %209, ^bb46, ^bb47
    ^bb46:
      %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 ^bb48
      ^bb48:
      %215 = llvm.load %214 : !llvm.ptr -> i64
      %216 = arith.constant 40 : i32
      %218 = arith.extsi %216 : i32 to i64
      %217 = arith.cmpi slt, %215, %218 : i64
      cf.cond_br %217, ^bb49, ^bb50
      ^bb49:
        %219 = arith.constant 0 : i32
        %220 = arith.extsi %219 : i32 to i64
        %221 = llvm.mlir.constant(1 : i64) : i64
        %222 = llvm.alloca %221 x i64 : (i64) -> !llvm.ptr
        llvm.store %220, %222 : i64, !llvm.ptr
        %223 = arith.constant 1 : i32
        %224 = arith.extsi %223 : i32 to i64
        %225 = llvm.mlir.constant(1 : i64) : i64
        %226 = llvm.alloca %225 x i64 : (i64) -> !llvm.ptr
        llvm.store %224, %226 : i64, !llvm.ptr
        %227 = arith.constant 0 : i1
        %228 = llvm.mlir.constant(1 : i64) : i64
        %229 = llvm.alloca %228 x i1 : (i64) -> !llvm.ptr
        llvm.store %227, %229 : i1, !llvm.ptr
        cf.br ^bb51
        ^bb51:
        %230 = llvm.load %226 : !llvm.ptr -> i64
        %231 = llvm.load %214 : !llvm.ptr -> i64
        %232 = arith.cmpi sle, %230, %231 : i64
        cf.cond_br %232, ^bb52, ^bb53
        ^bb52:
          %233 = llvm.load %214 : !llvm.ptr -> i64
          %234 = llvm.load %226 : !llvm.ptr -> i64
          %235 = arith.remsi %233, %234 : i64
          %236 = arith.constant 0 : i32
          %238 = arith.extsi %236 : i32 to i64
          %237 = arith.cmpi eq, %235, %238 : i64
          cf.cond_br %237, ^bb54, ^bb55
          ^bb54:
            %240 = llvm.load %214 : !llvm.ptr -> i64
            %241 = llvm.load %226 : !llvm.ptr -> i64
            %242 = arith.divsi %240, %241 : i64
            %239 = func.call @phi(%242) : (i64) -> i64
            %243 = arith.constant 1 : i32
            %244 = arith.extsi %243 : i32 to i64
            %245 = llvm.mlir.constant(1 : i64) : i64
            %246 = llvm.alloca %245 x i64 : (i64) -> !llvm.ptr
            llvm.store %244, %246 : i64, !llvm.ptr
            %247 = llvm.load %170 : !llvm.ptr -> i64
            %248 = llvm.load %226 : !llvm.ptr -> i64
            %249 = arith.muli %247, %248 : i64
            %250 = arith.constant 0 : i32
            %251 = arith.extsi %250 : i32 to i64
            %252 = llvm.mlir.constant(1 : i64) : i64
            %253 = llvm.alloca %252 x i64 : (i64) -> !llvm.ptr
            llvm.store %251, %253 : i64, !llvm.ptr
            cf.br ^bb57
            ^bb57:
            %254 = llvm.load %253 : !llvm.ptr -> i64
            %255 = llvm.load %170 : !llvm.ptr -> i64
            %256 = arith.cmpi slt, %254, %255 : i64
            cf.cond_br %256, ^bb58, ^bb59
            ^bb58:
              %257 = llvm.load %253 : !llvm.ptr -> i64
              %258 = llvm.load %226 : !llvm.ptr -> i64
              %259 = arith.muli %257, %258 : i64
              %260 = arith.constant 1 : i32
              %262 = arith.extsi %260 : i32 to i64
              %261 = arith.addi %259, %262 : i64
              %263 = llvm.load %253 : !llvm.ptr -> i64
              %264 = arith.constant 1 : i32
              %266 = arith.extsi %264 : i32 to i64
              %265 = arith.addi %263, %266 : i64
              %267 = llvm.load %226 : !llvm.ptr -> i64
              %268 = arith.muli %265, %267 : i64
              %269 = arith.constant 1 : i32
              %270 = arith.extsi %269 : i32 to i64
              %271 = llvm.mlir.constant(1 : i64) : i64
              %272 = llvm.alloca %271 x i64 : (i64) -> !llvm.ptr
              llvm.store %270, %272 : i64, !llvm.ptr
              cf.br ^bb60
              ^bb60:
              %273 = llvm.load %272 : !llvm.ptr -> i64
              %274 = llvm.load %226 : !llvm.ptr -> i64
              %275 = arith.cmpi sle, %273, %274 : i64
              cf.cond_br %275, ^bb61, ^bb62
              ^bb61:
                %276 = llvm.load %253 : !llvm.ptr -> i64
                %277 = llvm.load %226 : !llvm.ptr -> i64
                %278 = arith.muli %276, %277 : i64
                %279 = llvm.load %272 : !llvm.ptr -> i64
                %280 = arith.addi %278, %279 : i64
                %281 = llvm.load %246 : !llvm.ptr -> i64
                %282 = arith.constant 1999999995705032704 : i32
                %284 = arith.extsi %282 : i32 to i64
                %283 = arith.divsi %284, %280 : i64
                %285 = arith.cmpi sgt, %281, %283 : i64
                cf.cond_br %285, ^bb63, ^bb64
                ^bb63:
                  %286 = arith.constant 1 : i1
                  llvm.store %286, %229 : i1, !llvm.ptr
                  cf.br ^bb62
                ^bb64:
                  cf.br ^bb65
                ^bb65:
                %287 = llvm.load %246 : !llvm.ptr -> i64
                %288 = arith.muli %287, %280 : i64
                %289 = llvm.load %272 : !llvm.ptr -> i64
                %290 = arith.divsi %288, %289 : i64
                llvm.store %290, %246 : i64, !llvm.ptr
                %291 = llvm.load %272 : !llvm.ptr -> i64
                %292 = arith.constant 1 : i32
                %294 = arith.extsi %292 : i32 to i64
                %293 = arith.addi %291, %294 : i64
                llvm.store %293, %272 : i64, !llvm.ptr
                cf.br ^bb60
              ^bb62:
              %295 = llvm.load %229 : !llvm.ptr -> i1
              cf.cond_br %295, ^bb66, ^bb67
              ^bb66:
                cf.br ^bb59
              ^bb67:
                cf.br ^bb68
              ^bb68:
              %296 = llvm.load %253 : !llvm.ptr -> i64
              %297 = arith.constant 1 : i32
              %299 = arith.extsi %297 : i32 to i64
              %298 = arith.addi %296, %299 : i64
              llvm.store %298, %253 : i64, !llvm.ptr
              cf.br ^bb57
            ^bb59:
            %300 = llvm.load %229 : !llvm.ptr -> i1
            cf.cond_br %300, ^bb69, ^bb70
            ^bb69:
              cf.br ^bb53
            ^bb70:
              cf.br ^bb71
            ^bb71:
            %301 = arith.constant 0 : i32
            %303 = arith.extsi %301 : i32 to i64
            %302 = arith.cmpi sgt, %239, %303 : i64
            %304 = scf.if %302 -> (i1) {
              %305 = llvm.load %246 : !llvm.ptr -> i64
              %306 = arith.constant 1999999995705032704 : i32
              %308 = arith.extsi %306 : i32 to i64
              %307 = arith.divsi %308, %239 : i64
              %309 = arith.cmpi sgt, %305, %307 : i64
              scf.yield %309 : i1
            } else {
              %310 = arith.constant false
              scf.yield %310 : i1
            }
            cf.cond_br %304, ^bb72, ^bb73
            ^bb72:
              %311 = arith.constant 1 : i1
              llvm.store %311, %229 : i1, !llvm.ptr
              cf.br ^bb53
            ^bb73:
              cf.br ^bb74
            ^bb74:
            %312 = llvm.load %222 : !llvm.ptr -> i64
            %313 = llvm.load %246 : !llvm.ptr -> i64
            %314 = arith.muli %239, %313 : i64
            %315 = arith.addi %312, %314 : i64
            llvm.store %315, %222 : i64, !llvm.ptr
            cf.br ^bb56
          ^bb55:
            cf.br ^bb56
          ^bb56:
          %316 = llvm.load %226 : !llvm.ptr -> i64
          %317 = arith.constant 1 : i32
          %319 = arith.extsi %317 : i32 to i64
          %318 = arith.addi %316, %319 : i64
          llvm.store %318, %226 : i64, !llvm.ptr
          cf.br ^bb51
        ^bb53:
        %320 = llvm.load %229 : !llvm.ptr -> i1
        cf.cond_br %320, ^bb75, ^bb76
        ^bb75:
          cf.br ^bb50
        ^bb76:
          cf.br ^bb77
        ^bb77:
        %321 = llvm.load %222 : !llvm.ptr -> i64
        %322 = llvm.load %170 : !llvm.ptr -> i64
        %323 = llvm.load %214 : !llvm.ptr -> i64
        %324 = arith.muli %322, %323 : i64
        %325 = arith.divsi %321, %324 : i64
        %326 = arith.constant 0 : i32
        %328 = arith.extsi %326 : i32 to i64
        %327 = arith.cmpi sle, %325, %328 : i64
        cf.cond_br %327, ^bb78, ^bb79
        ^bb78:
          cf.br ^bb50
        ^bb79:
          cf.br ^bb80
        ^bb80:
        %329 = arith.cmpi sle, %325, %162 : i64
        cf.cond_br %329, ^bb81, ^bb82
        ^bb81:
          %330 = llvm.load %166 : !llvm.ptr -> i64
          %331 = arith.addi %330, %325 : i64
          llvm.store %331, %166 : i64, !llvm.ptr
          cf.br ^bb83
        ^bb82:
          cf.br ^bb50
        ^bb83:
        %332 = llvm.load %214 : !llvm.ptr -> i64
        %333 = arith.constant 1 : i32
        %335 = arith.extsi %333 : i32 to i64
        %334 = arith.addi %332, %335 : i64
        llvm.store %334, %214 : i64, !llvm.ptr
        cf.br ^bb48
      ^bb50:
      %336 = llvm.load %170 : !llvm.ptr -> i64
      %337 = arith.constant 1 : i32
      %339 = arith.extsi %337 : i32 to i64
      %338 = arith.addi %336, %339 : i64
      llvm.store %338, %170 : i64, !llvm.ptr
      cf.br ^bb45
    ^bb47:
    %340 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %341 = llvm.load %166 : !llvm.ptr -> i64
    %342 = llvm.call @printf(%340, %341) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %343 = arith.constant 0 : i32
    func.return %343 : i32
  }
}