Problem 458

Permutations of Project — T(10^12) mod 10^9 via transfer matrix.

Answer423341841
Output423341841
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^3) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n!)
Space complexityO(n^2)O(n)
ApproachFlow solutionPermutation enumeration or constraint search
VerdictOptimal

Flow source

# Project Euler 458
# Permutations of Project — T(10^12) mod 10^9 via transfer matrix.

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

function mat_mul(a: ptr<i64>, b: ptr<i64>, out: ptr<i64>, n: i32, mod: i64) -> void {
    let mut i: i32 = 0
    while i < n {
        let mut j: i32 = 0
        while j < n {
            let mut s: i64 = 0
            let mut k: i32 = 0
            while k < n {
                let t: i128 = (a[i * n + k] as i128) * (b[k * n + j] as i128)
                s = ((s as i128 + t) % (mod as i128)) as i64
                if s < 0 { s = s + mod }
                k = k + 1
            }
            out[i * n + j] = s
            j = j + 1
        }
        i = i + 1
    }
}

function mat_pow(base: ptr<i64>, exp0: i64, n: i32, mod: i64, out: ptr<i64>) -> void {
    let tmp: ptr<i64> = calloc((n * n) as i64, 8)
    let cur: ptr<i64> = calloc((n * n) as i64, 8)
    if tmp == null || cur == null { return }
    let mut i: i32 = 0
    while i < n * n {
        out[i] = 0
        cur[i] = base[i]
        i = i + 1
    }
    i = 0
    while i < n {
        out[i * n + i] = 1
        i = i + 1
    }
    let mut exp: i64 = exp0
    while exp > 0 {
        if exp % 2 == 1 {
            mat_mul(out, cur, tmp, n, mod)
            i = 0
            while i < n * n {
                out[i] = tmp[i]
                i = i + 1
            }
        }
        mat_mul(cur, cur, tmp, n, mod)
        i = 0
        while i < n * n {
            cur[i] = tmp[i]
            i = i + 1
        }
        exp = exp / 2
    }
    free(tmp)
    free(cur)
}

function main() -> i32 {
    let N: i32 = 8
    let MOD: i64 = 1000000000
    let mat: ptr<i64> = calloc((N * N) as i64, 8)
    let res: ptr<i64> = calloc((N * N) as i64, 8)
    if mat == null || res == null { return 1 }
    mat[1 * N + 0] = 7
    let mut c: i32 = 1
    while c <= 6 {
        mat[1 * N + c] = 1
        c = c + 1
    }
    mat[2 * N + 1] = 6
    c = 2
    while c <= 6 {
        mat[2 * N + c] = 1
        c = c + 1
    }
    mat[3 * N + 2] = 5
    c = 3
    while c <= 6 {
        mat[3 * N + c] = 1
        c = c + 1
    }
    mat[4 * N + 3] = 4
    c = 4
    while c <= 6 {
        mat[4 * N + c] = 1
        c = c + 1
    }
    mat[5 * N + 4] = 3
    c = 5
    while c <= 6 {
        mat[5 * N + c] = 1
        c = c + 1
    }
    mat[6 * N + 5] = 2
    mat[6 * N + 6] = 1
    mat[7 * N + 6] = 1
    mat[7 * N + 7] = 7

    mat_pow(mat, 1000000000000, N, MOD, res)
    let with_project: i64 = res[7 * N + 0]
    let mut all_strings: i64 = res[7 * N + 7]
    if all_strings < with_project {
        all_strings = all_strings + MOD
    }
    printf("%lld\n", all_strings - with_project)
    free(res)
    free(mat)
    return 0
}

Generated C

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Flow runtime helpers */
typedef struct flow_temp_node { struct flow_temp_node* next; } flow_temp_node;
static flow_temp_node* flow_temp_head = NULL;
static int flow_temp_atexit_set = 0;
__attribute__((unused)) static void flow_temp_free_all(void) {
    while (flow_temp_head) {
        flow_temp_node* n = flow_temp_head;
        flow_temp_head = n->next;
        free(n);
    }
}
__attribute__((unused)) static void* flow_temp_alloc(size_t nbytes) {
    flow_temp_node* node = (flow_temp_node*)malloc(sizeof(flow_temp_node) + nbytes);
    if (!node) return NULL;
    node->next = flow_temp_head;
    flow_temp_head = node;
    if (!flow_temp_atexit_set) {
        flow_temp_atexit_set = 1;
        atexit(flow_temp_free_all);
    }
    return (void*)(node + 1);
}
#ifndef FLOW_DIAG
#define FLOW_DIAG(msg) fprintf(stderr, "%s", (msg))
#endif
#ifndef FLOW_LOG
#define FLOW_LOG(fmt, ...) printf(fmt, __VA_ARGS__)
#endif
#ifndef FLOW_LOG_EMPTY
#define FLOW_LOG_EMPTY(fmt) printf(fmt)
#endif
static char* flow_strcat(const char* a, const char* b) {
    size_t la = strlen(a ? a : ""), lb = strlen(b ? b : "");
    char* r = (char*)flow_temp_alloc(la + lb + 1);
    if (!r) return NULL;
    if (la) memcpy(r, a, la);
    if (lb) memcpy(r + la, b, lb);
    r[la + lb] = '\0';
    return r;
}

#define __flow_in_arr(arr, val) __extension__ ({ \
    int _found = 0; \
    size_t _n = sizeof(arr)/sizeof((arr)[0]); \
    for (size_t _i = 0; _i < _n; _i++) { \
        if ((arr)[_i] == (val)) { _found = 1; break; } \
    } _found; })

/* Unified fault handler (MISRA #279) — override with -DFLOW_FAULT_HANDLER=fn */
#ifndef FLOW_FAULT_HANDLER
__attribute__((unused)) static inline void flow_fault_handler(const char* msg) {
    fprintf(stderr, "flow: %s\n", msg ? msg : "fault");
    abort();
#if defined(__GNUC__) || defined(__clang__)
    __builtin_unreachable();
#endif
}
#else
#define flow_fault_handler FLOW_FAULT_HANDLER
#endif
#define flow_div_by_zero_handler() flow_fault_handler("division by zero")
#define flow_shift_ub_handler() flow_fault_handler("invalid shift (amount out of range or left-shift of negative)")

#ifndef FLOW_CHECKED_DIV
#define FLOW_CHECKED_DIV(L, R) (((R) != 0) ? ((L) / (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_MOD
#define FLOW_CHECKED_MOD(L, R) (((R) != 0) ? ((L) % (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHL
#define FLOW_CHECKED_SHL(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull)) && ((L) >= 0)) ? ((L) << (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHR
#define FLOW_CHECKED_SHR(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull))) ? ((L) >> (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif

#include <math.h>

void* _ui_state = NULL;

static inline float i32_to_f32(int32_t v) { return (float)v; }

/* Host stub for @gpu kernels (device codegen replaces this). */
static inline int32_t gpu_thread_id(void) { return 0; }

void mat_mul_ptr_i64_ptr_i64_ptr_i64_i32_i64(int64_t* a, int64_t* b, int64_t* out, int32_t n, int64_t mod);
void mat_pow_ptr_i64_i64_i32_i64_ptr_i64(int64_t* base, int64_t exp0, int32_t n, int64_t mod, int64_t* out);
int32_t main(void);



void mat_mul_ptr_i64_ptr_i64_ptr_i64_i32_i64(int64_t* a, int64_t* b, int64_t* out, int32_t n, int64_t mod) {
    int32_t i = 0;
    while (i < n) {
        int32_t j = 0;
        while (j < n) {
            int64_t s = 0;
            int32_t k = 0;
            while (k < n) {
                __int128 t = (((__int128)(a[((i * n) + k)])) * ((__int128)(b[((k * n) + j)])));
                s = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(s)) + t)), (((__int128)(mod))))));
                if (s < 0) {
                    s = (s + mod);
                }
                k = (k + 1);
            }
            out[((i * n) + j)] = s;
            j = (j + 1);
        }
        i = (i + 1);
    }
}

void mat_pow_ptr_i64_i64_i32_i64_ptr_i64(int64_t* base, int64_t exp0, int32_t n, int64_t mod, int64_t* out) {
    int64_t* tmp = (int64_t*)(calloc(((int64_t)((n * n))), 8));
    int64_t* cur = (int64_t*)(calloc(((int64_t)((n * n))), 8));
    if ((tmp == NULL || cur == NULL)) {
        return;
    }
    int32_t i = 0;
    while (i < (n * n)) {
        out[i] = 0;
        cur[i] = base[i];
        i = (i + 1);
    }
    i = 0;
    while (i < n) {
        out[((i * n) + i)] = 1;
        i = (i + 1);
    }
    int64_t exp = exp0;
    while (exp > 0) {
        if (FLOW_CHECKED_MOD((exp), (2)) == 1) {
            mat_mul_ptr_i64_ptr_i64_ptr_i64_i32_i64(out, cur, tmp, n, mod);
            i = 0;
            while (i < (n * n)) {
                out[i] = tmp[i];
                i = (i + 1);
            }
        }
        mat_mul_ptr_i64_ptr_i64_ptr_i64_i32_i64(cur, cur, tmp, n, mod);
        i = 0;
        while (i < (n * n)) {
            cur[i] = tmp[i];
            i = (i + 1);
        }
        exp = FLOW_CHECKED_DIV((exp), (2));
    }
    free(tmp);
    free(cur);
}

int32_t main(void) {
    int32_t N = 8;
    int64_t MOD = 1000000000;
    int64_t* mat = (int64_t*)(calloc(((int64_t)((N * N))), 8));
    int64_t* res = (int64_t*)(calloc(((int64_t)((N * N))), 8));
    if ((mat == NULL || res == NULL)) {
        return 1;
    }
    mat[((1 * N) + 0)] = 7;
    int32_t c = 1;
    while (c <= 6) {
        mat[((1 * N) + c)] = 1;
        c = (c + 1);
    }
    mat[((2 * N) + 1)] = 6;
    c = 2;
    while (c <= 6) {
        mat[((2 * N) + c)] = 1;
        c = (c + 1);
    }
    mat[((3 * N) + 2)] = 5;
    c = 3;
    while (c <= 6) {
        mat[((3 * N) + c)] = 1;
        c = (c + 1);
    }
    mat[((4 * N) + 3)] = 4;
    c = 4;
    while (c <= 6) {
        mat[((4 * N) + c)] = 1;
        c = (c + 1);
    }
    mat[((5 * N) + 4)] = 3;
    c = 5;
    while (c <= 6) {
        mat[((5 * N) + c)] = 1;
        c = (c + 1);
    }
    mat[((6 * N) + 5)] = 2;
    mat[((6 * N) + 6)] = 1;
    mat[((7 * N) + 6)] = 1;
    mat[((7 * N) + 7)] = 7;
    mat_pow_ptr_i64_i64_i32_i64_ptr_i64(mat, 1000000000000, N, MOD, res);
    int64_t with_project = res[((7 * N) + 0)];
    int64_t all_strings = res[((7 * N) + 7)];
    if (all_strings < with_project) {
        all_strings = (all_strings + MOD);
    }
    printf("%lld\n", (all_strings - with_project));
    free(res);
    free(mat);
    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 @mat_mul(%arg0: !llvm.ptr, %arg1: !llvm.ptr, %arg2: !llvm.ptr, %arg3: i32, %arg4: i64) -> () {
    %0 = arith.constant 0 : i32
    %1 = llvm.mlir.constant(1 : i64) : i64
    %2 = llvm.alloca %1 x i32 : (i64) -> !llvm.ptr
    llvm.store %0, %2 : i32, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %3 = llvm.load %2 : !llvm.ptr -> i32
    %4 = arith.cmpi slt, %3, %arg3 : i32
    cf.cond_br %4, ^bb1, ^bb2
    ^bb1:
      %5 = arith.constant 0 : i32
      %6 = llvm.mlir.constant(1 : i64) : i64
      %7 = llvm.alloca %6 x i32 : (i64) -> !llvm.ptr
      llvm.store %5, %7 : i32, !llvm.ptr
      cf.br ^bb3
      ^bb3:
      %8 = llvm.load %7 : !llvm.ptr -> i32
      %9 = arith.cmpi slt, %8, %arg3 : i32
      cf.cond_br %9, ^bb4, ^bb5
      ^bb4:
        %10 = arith.constant 0 : i32
        %11 = arith.extsi %10 : i32 to i64
        %12 = llvm.mlir.constant(1 : i64) : i64
        %13 = llvm.alloca %12 x i64 : (i64) -> !llvm.ptr
        llvm.store %11, %13 : i64, !llvm.ptr
        %14 = arith.constant 0 : i32
        %15 = llvm.mlir.constant(1 : i64) : i64
        %16 = llvm.alloca %15 x i32 : (i64) -> !llvm.ptr
        llvm.store %14, %16 : i32, !llvm.ptr
        cf.br ^bb6
        ^bb6:
        %17 = llvm.load %16 : !llvm.ptr -> i32
        %18 = arith.cmpi slt, %17, %arg3 : i32
        cf.cond_br %18, ^bb7, ^bb8
        ^bb7:
          %20 = llvm.load %2 : !llvm.ptr -> i32
          %21 = arith.muli %20, %arg3 : i32
          %22 = llvm.load %16 : !llvm.ptr -> i32
          %23 = arith.addi %21, %22 : i32
          %24 = arith.extsi %23 : i32 to i64
          %25 = llvm.getelementptr %arg0[%24] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %19 = llvm.load %25 : !llvm.ptr -> i64
          %26 = arith.extsi %19 : i64 to i128
          %28 = llvm.load %16 : !llvm.ptr -> i32
          %29 = arith.muli %28, %arg3 : i32
          %30 = llvm.load %7 : !llvm.ptr -> i32
          %31 = arith.addi %29, %30 : i32
          %32 = arith.extsi %31 : i32 to i64
          %33 = llvm.getelementptr %arg1[%32] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %27 = llvm.load %33 : !llvm.ptr -> i64
          %34 = arith.extsi %27 : i64 to i128
          %36 = arith.trunci %26 : i128 to i64
          %37 = arith.trunci %34 : i128 to i64
          %35 = arith.muli %36, %37 : i64
          %38 = arith.extsi %35 : i64 to i128
          %39 = llvm.load %13 : !llvm.ptr -> i64
          %40 = arith.extsi %39 : i64 to i128
          %42 = arith.trunci %40 : i128 to i64
          %43 = arith.trunci %38 : i128 to i64
          %41 = arith.addi %42, %43 : i64
          %44 = arith.extsi %arg4 : i64 to i128
          %46 = arith.trunci %44 : i128 to i64
          %45 = arith.remsi %41, %46 : i64
          llvm.store %45, %13 : i64, !llvm.ptr
          %47 = llvm.load %13 : !llvm.ptr -> i64
          %48 = arith.constant 0 : i32
          %50 = arith.extsi %48 : i32 to i64
          %49 = arith.cmpi slt, %47, %50 : i64
          cf.cond_br %49, ^bb9, ^bb10
          ^bb9:
            %51 = llvm.load %13 : !llvm.ptr -> i64
            %52 = arith.addi %51, %arg4 : i64
            llvm.store %52, %13 : i64, !llvm.ptr
            cf.br ^bb11
          ^bb10:
            cf.br ^bb11
          ^bb11:
          %53 = llvm.load %16 : !llvm.ptr -> i32
          %54 = arith.constant 1 : i32
          %55 = arith.addi %53, %54 : i32
          llvm.store %55, %16 : i32, !llvm.ptr
          cf.br ^bb6
        ^bb8:
        %56 = llvm.load %13 : !llvm.ptr -> i64
        %57 = llvm.load %2 : !llvm.ptr -> i32
        %58 = arith.muli %57, %arg3 : i32
        %59 = llvm.load %7 : !llvm.ptr -> i32
        %60 = arith.addi %58, %59 : i32
        %61 = arith.extsi %60 : i32 to i64
        %62 = llvm.getelementptr %arg2[%61] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %56, %62 : i64, !llvm.ptr
        %63 = llvm.load %7 : !llvm.ptr -> i32
        %64 = arith.constant 1 : i32
        %65 = arith.addi %63, %64 : i32
        llvm.store %65, %7 : i32, !llvm.ptr
        cf.br ^bb3
      ^bb5:
      %66 = llvm.load %2 : !llvm.ptr -> i32
      %67 = arith.constant 1 : i32
      %68 = arith.addi %66, %67 : i32
      llvm.store %68, %2 : i32, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    func.return
  }
  func.func @mat_pow(%arg0: !llvm.ptr, %arg1: i64, %arg2: i32, %arg3: i64, %arg4: !llvm.ptr) -> () {
    %70 = arith.muli %arg2, %arg2 : i32
    %71 = arith.extsi %70 : i32 to i64
    %72 = arith.constant 8 : i32
    %73 = arith.extsi %72 : i32 to i64
    %69 = func.call @calloc(%71, %73) : (i64, i64) -> !llvm.ptr
    %75 = arith.muli %arg2, %arg2 : i32
    %76 = arith.extsi %75 : i32 to i64
    %77 = arith.constant 8 : i32
    %78 = arith.extsi %77 : i32 to i64
    %74 = func.call @calloc(%76, %78) : (i64, i64) -> !llvm.ptr
    %79 = llvm.mlir.zero : !llvm.ptr
    %80 = llvm.icmp "eq" %69, %79 : !llvm.ptr
    %81 = scf.if %80 -> (i1) {
      %82 = arith.constant true
      scf.yield %82 : i1
    } else {
      %83 = llvm.mlir.zero : !llvm.ptr
      %84 = llvm.icmp "eq" %74, %83 : !llvm.ptr
      scf.yield %84 : i1
    }
    cf.cond_br %81, ^bb12, ^bb13
    ^bb12:
      func.return
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %85 = arith.constant 0 : i32
    %86 = llvm.mlir.constant(1 : i64) : i64
    %87 = llvm.alloca %86 x i32 : (i64) -> !llvm.ptr
    llvm.store %85, %87 : i32, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %88 = llvm.load %87 : !llvm.ptr -> i32
    %89 = arith.muli %arg2, %arg2 : i32
    %90 = arith.cmpi slt, %88, %89 : i32
    cf.cond_br %90, ^bb16, ^bb17
    ^bb16:
      %91 = arith.constant 0 : i32
      %92 = llvm.load %87 : !llvm.ptr -> i32
      %93 = arith.extsi %91 : i32 to i64
      %94 = arith.extsi %92 : i32 to i64
      %95 = llvm.getelementptr %arg4[%94] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %93, %95 : i64, !llvm.ptr
      %97 = llvm.load %87 : !llvm.ptr -> i32
      %98 = arith.extsi %97 : i32 to i64
      %99 = llvm.getelementptr %arg0[%98] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %96 = llvm.load %99 : !llvm.ptr -> i64
      %100 = llvm.load %87 : !llvm.ptr -> i32
      %101 = arith.extsi %100 : i32 to i64
      %102 = llvm.getelementptr %74[%101] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %96, %102 : i64, !llvm.ptr
      %103 = llvm.load %87 : !llvm.ptr -> i32
      %104 = arith.constant 1 : i32
      %105 = arith.addi %103, %104 : i32
      llvm.store %105, %87 : i32, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %106 = arith.constant 0 : i32
    llvm.store %106, %87 : i32, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %107 = llvm.load %87 : !llvm.ptr -> i32
    %108 = arith.cmpi slt, %107, %arg2 : i32
    cf.cond_br %108, ^bb19, ^bb20
    ^bb19:
      %109 = arith.constant 1 : i32
      %110 = llvm.load %87 : !llvm.ptr -> i32
      %111 = arith.muli %110, %arg2 : i32
      %112 = llvm.load %87 : !llvm.ptr -> i32
      %113 = arith.addi %111, %112 : i32
      %114 = arith.extsi %109 : i32 to i64
      %115 = arith.extsi %113 : i32 to i64
      %116 = llvm.getelementptr %arg4[%115] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %114, %116 : i64, !llvm.ptr
      %117 = llvm.load %87 : !llvm.ptr -> i32
      %118 = arith.constant 1 : i32
      %119 = arith.addi %117, %118 : i32
      llvm.store %119, %87 : i32, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %120 = llvm.mlir.constant(1 : i64) : i64
    %121 = llvm.alloca %120 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %121 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %122 = llvm.load %121 : !llvm.ptr -> i64
    %123 = arith.constant 0 : i32
    %125 = arith.extsi %123 : i32 to i64
    %124 = arith.cmpi sgt, %122, %125 : i64
    cf.cond_br %124, ^bb22, ^bb23
    ^bb22:
      %126 = llvm.load %121 : !llvm.ptr -> i64
      %127 = arith.constant 2 : i32
      %129 = arith.extsi %127 : i32 to i64
      %128 = arith.remsi %126, %129 : i64
      %130 = arith.constant 1 : i32
      %132 = arith.extsi %130 : i32 to i64
      %131 = arith.cmpi eq, %128, %132 : i64
      cf.cond_br %131, ^bb24, ^bb25
      ^bb24:
        func.call @mat_mul(%arg4, %74, %69, %arg2, %arg3) : (!llvm.ptr, !llvm.ptr, !llvm.ptr, i32, i64) -> ()
        %134 = arith.constant 0 : i32
        llvm.store %134, %87 : i32, !llvm.ptr
        cf.br ^bb27
        ^bb27:
        %135 = llvm.load %87 : !llvm.ptr -> i32
        %136 = arith.muli %arg2, %arg2 : i32
        %137 = arith.cmpi slt, %135, %136 : i32
        cf.cond_br %137, ^bb28, ^bb29
        ^bb28:
          %139 = llvm.load %87 : !llvm.ptr -> i32
          %140 = arith.extsi %139 : i32 to i64
          %141 = llvm.getelementptr %69[%140] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %138 = llvm.load %141 : !llvm.ptr -> i64
          %142 = llvm.load %87 : !llvm.ptr -> i32
          %143 = arith.extsi %142 : i32 to i64
          %144 = llvm.getelementptr %arg4[%143] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %138, %144 : i64, !llvm.ptr
          %145 = llvm.load %87 : !llvm.ptr -> i32
          %146 = arith.constant 1 : i32
          %147 = arith.addi %145, %146 : i32
          llvm.store %147, %87 : i32, !llvm.ptr
          cf.br ^bb27
        ^bb29:
        cf.br ^bb26
      ^bb25:
        cf.br ^bb26
      ^bb26:
      func.call @mat_mul(%74, %74, %69, %arg2, %arg3) : (!llvm.ptr, !llvm.ptr, !llvm.ptr, i32, i64) -> ()
      %149 = arith.constant 0 : i32
      llvm.store %149, %87 : i32, !llvm.ptr
      cf.br ^bb30
      ^bb30:
      %150 = llvm.load %87 : !llvm.ptr -> i32
      %151 = arith.muli %arg2, %arg2 : i32
      %152 = arith.cmpi slt, %150, %151 : i32
      cf.cond_br %152, ^bb31, ^bb32
      ^bb31:
        %154 = llvm.load %87 : !llvm.ptr -> i32
        %155 = arith.extsi %154 : i32 to i64
        %156 = llvm.getelementptr %69[%155] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %153 = llvm.load %156 : !llvm.ptr -> i64
        %157 = llvm.load %87 : !llvm.ptr -> i32
        %158 = arith.extsi %157 : i32 to i64
        %159 = llvm.getelementptr %74[%158] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %153, %159 : i64, !llvm.ptr
        %160 = llvm.load %87 : !llvm.ptr -> i32
        %161 = arith.constant 1 : i32
        %162 = arith.addi %160, %161 : i32
        llvm.store %162, %87 : i32, !llvm.ptr
        cf.br ^bb30
      ^bb32:
      %163 = llvm.load %121 : !llvm.ptr -> i64
      %164 = arith.constant 2 : i32
      %166 = arith.extsi %164 : i32 to i64
      %165 = arith.divsi %163, %166 : i64
      llvm.store %165, %121 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    func.call @free(%69) : (!llvm.ptr) -> ()
    func.call @free(%74) : (!llvm.ptr) -> ()
    func.return
  }
  func.func @main() -> i32 {
    %169 = arith.constant 8 : i32
    %170 = arith.constant 1000000000 : i32
    %171 = arith.extsi %170 : i32 to i64
    %173 = arith.muli %169, %169 : i32
    %174 = arith.extsi %173 : i32 to i64
    %175 = arith.constant 8 : i32
    %176 = arith.extsi %175 : i32 to i64
    %172 = func.call @calloc(%174, %176) : (i64, i64) -> !llvm.ptr
    %178 = arith.muli %169, %169 : i32
    %179 = arith.extsi %178 : i32 to i64
    %180 = arith.constant 8 : i32
    %181 = arith.extsi %180 : i32 to i64
    %177 = func.call @calloc(%179, %181) : (i64, i64) -> !llvm.ptr
    %182 = llvm.mlir.zero : !llvm.ptr
    %183 = llvm.icmp "eq" %172, %182 : !llvm.ptr
    %184 = scf.if %183 -> (i1) {
      %185 = arith.constant true
      scf.yield %185 : i1
    } else {
      %186 = llvm.mlir.zero : !llvm.ptr
      %187 = llvm.icmp "eq" %177, %186 : !llvm.ptr
      scf.yield %187 : i1
    }
    cf.cond_br %184, ^bb33, ^bb34
    ^bb33:
      %188 = arith.constant 1 : i32
      func.return %188 : i32
    ^bb34:
      cf.br ^bb35
    ^bb35:
    %189 = arith.constant 7 : i32
    %190 = arith.constant 1 : i32
    %191 = arith.muli %190, %169 : i32
    %192 = arith.constant 0 : i32
    %193 = arith.addi %191, %192 : i32
    %194 = arith.extsi %189 : i32 to i64
    %195 = arith.extsi %193 : i32 to i64
    %196 = llvm.getelementptr %172[%195] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %194, %196 : i64, !llvm.ptr
    %197 = arith.constant 1 : i32
    %198 = llvm.mlir.constant(1 : i64) : i64
    %199 = llvm.alloca %198 x i32 : (i64) -> !llvm.ptr
    llvm.store %197, %199 : i32, !llvm.ptr
    cf.br ^bb36
    ^bb36:
    %200 = llvm.load %199 : !llvm.ptr -> i32
    %201 = arith.constant 6 : i32
    %202 = arith.cmpi sle, %200, %201 : i32
    cf.cond_br %202, ^bb37, ^bb38
    ^bb37:
      %203 = arith.constant 1 : i32
      %204 = arith.constant 1 : i32
      %205 = arith.muli %204, %169 : i32
      %206 = llvm.load %199 : !llvm.ptr -> i32
      %207 = arith.addi %205, %206 : i32
      %208 = arith.extsi %203 : i32 to i64
      %209 = arith.extsi %207 : i32 to i64
      %210 = llvm.getelementptr %172[%209] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %208, %210 : i64, !llvm.ptr
      %211 = llvm.load %199 : !llvm.ptr -> i32
      %212 = arith.constant 1 : i32
      %213 = arith.addi %211, %212 : i32
      llvm.store %213, %199 : i32, !llvm.ptr
      cf.br ^bb36
    ^bb38:
    %214 = arith.constant 6 : i32
    %215 = arith.constant 2 : i32
    %216 = arith.muli %215, %169 : i32
    %217 = arith.constant 1 : i32
    %218 = arith.addi %216, %217 : i32
    %219 = arith.extsi %214 : i32 to i64
    %220 = arith.extsi %218 : i32 to i64
    %221 = llvm.getelementptr %172[%220] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %219, %221 : i64, !llvm.ptr
    %222 = arith.constant 2 : i32
    llvm.store %222, %199 : i32, !llvm.ptr
    cf.br ^bb39
    ^bb39:
    %223 = llvm.load %199 : !llvm.ptr -> i32
    %224 = arith.constant 6 : i32
    %225 = arith.cmpi sle, %223, %224 : i32
    cf.cond_br %225, ^bb40, ^bb41
    ^bb40:
      %226 = arith.constant 1 : i32
      %227 = arith.constant 2 : i32
      %228 = arith.muli %227, %169 : i32
      %229 = llvm.load %199 : !llvm.ptr -> i32
      %230 = arith.addi %228, %229 : i32
      %231 = arith.extsi %226 : i32 to i64
      %232 = arith.extsi %230 : i32 to i64
      %233 = llvm.getelementptr %172[%232] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %231, %233 : i64, !llvm.ptr
      %234 = llvm.load %199 : !llvm.ptr -> i32
      %235 = arith.constant 1 : i32
      %236 = arith.addi %234, %235 : i32
      llvm.store %236, %199 : i32, !llvm.ptr
      cf.br ^bb39
    ^bb41:
    %237 = arith.constant 5 : i32
    %238 = arith.constant 3 : i32
    %239 = arith.muli %238, %169 : i32
    %240 = arith.constant 2 : i32
    %241 = arith.addi %239, %240 : i32
    %242 = arith.extsi %237 : i32 to i64
    %243 = arith.extsi %241 : i32 to i64
    %244 = llvm.getelementptr %172[%243] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %242, %244 : i64, !llvm.ptr
    %245 = arith.constant 3 : i32
    llvm.store %245, %199 : i32, !llvm.ptr
    cf.br ^bb42
    ^bb42:
    %246 = llvm.load %199 : !llvm.ptr -> i32
    %247 = arith.constant 6 : i32
    %248 = arith.cmpi sle, %246, %247 : i32
    cf.cond_br %248, ^bb43, ^bb44
    ^bb43:
      %249 = arith.constant 1 : i32
      %250 = arith.constant 3 : i32
      %251 = arith.muli %250, %169 : i32
      %252 = llvm.load %199 : !llvm.ptr -> i32
      %253 = arith.addi %251, %252 : i32
      %254 = arith.extsi %249 : i32 to i64
      %255 = arith.extsi %253 : i32 to i64
      %256 = llvm.getelementptr %172[%255] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %254, %256 : i64, !llvm.ptr
      %257 = llvm.load %199 : !llvm.ptr -> i32
      %258 = arith.constant 1 : i32
      %259 = arith.addi %257, %258 : i32
      llvm.store %259, %199 : i32, !llvm.ptr
      cf.br ^bb42
    ^bb44:
    %260 = arith.constant 4 : i32
    %261 = arith.constant 4 : i32
    %262 = arith.muli %261, %169 : i32
    %263 = arith.constant 3 : i32
    %264 = arith.addi %262, %263 : i32
    %265 = arith.extsi %260 : i32 to i64
    %266 = arith.extsi %264 : i32 to i64
    %267 = llvm.getelementptr %172[%266] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %265, %267 : i64, !llvm.ptr
    %268 = arith.constant 4 : i32
    llvm.store %268, %199 : i32, !llvm.ptr
    cf.br ^bb45
    ^bb45:
    %269 = llvm.load %199 : !llvm.ptr -> i32
    %270 = arith.constant 6 : i32
    %271 = arith.cmpi sle, %269, %270 : i32
    cf.cond_br %271, ^bb46, ^bb47
    ^bb46:
      %272 = arith.constant 1 : i32
      %273 = arith.constant 4 : i32
      %274 = arith.muli %273, %169 : i32
      %275 = llvm.load %199 : !llvm.ptr -> i32
      %276 = arith.addi %274, %275 : i32
      %277 = arith.extsi %272 : i32 to i64
      %278 = arith.extsi %276 : i32 to i64
      %279 = llvm.getelementptr %172[%278] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %277, %279 : i64, !llvm.ptr
      %280 = llvm.load %199 : !llvm.ptr -> i32
      %281 = arith.constant 1 : i32
      %282 = arith.addi %280, %281 : i32
      llvm.store %282, %199 : i32, !llvm.ptr
      cf.br ^bb45
    ^bb47:
    %283 = arith.constant 3 : i32
    %284 = arith.constant 5 : i32
    %285 = arith.muli %284, %169 : i32
    %286 = arith.constant 4 : i32
    %287 = arith.addi %285, %286 : i32
    %288 = arith.extsi %283 : i32 to i64
    %289 = arith.extsi %287 : i32 to i64
    %290 = llvm.getelementptr %172[%289] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %288, %290 : i64, !llvm.ptr
    %291 = arith.constant 5 : i32
    llvm.store %291, %199 : i32, !llvm.ptr
    cf.br ^bb48
    ^bb48:
    %292 = llvm.load %199 : !llvm.ptr -> i32
    %293 = arith.constant 6 : i32
    %294 = arith.cmpi sle, %292, %293 : i32
    cf.cond_br %294, ^bb49, ^bb50
    ^bb49:
      %295 = arith.constant 1 : i32
      %296 = arith.constant 5 : i32
      %297 = arith.muli %296, %169 : i32
      %298 = llvm.load %199 : !llvm.ptr -> i32
      %299 = arith.addi %297, %298 : i32
      %300 = arith.extsi %295 : i32 to i64
      %301 = arith.extsi %299 : i32 to i64
      %302 = llvm.getelementptr %172[%301] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %300, %302 : i64, !llvm.ptr
      %303 = llvm.load %199 : !llvm.ptr -> i32
      %304 = arith.constant 1 : i32
      %305 = arith.addi %303, %304 : i32
      llvm.store %305, %199 : i32, !llvm.ptr
      cf.br ^bb48
    ^bb50:
    %306 = arith.constant 2 : i32
    %307 = arith.constant 6 : i32
    %308 = arith.muli %307, %169 : i32
    %309 = arith.constant 5 : i32
    %310 = arith.addi %308, %309 : i32
    %311 = arith.extsi %306 : i32 to i64
    %312 = arith.extsi %310 : i32 to i64
    %313 = llvm.getelementptr %172[%312] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %311, %313 : i64, !llvm.ptr
    %314 = arith.constant 1 : i32
    %315 = arith.constant 6 : i32
    %316 = arith.muli %315, %169 : i32
    %317 = arith.constant 6 : i32
    %318 = arith.addi %316, %317 : i32
    %319 = arith.extsi %314 : i32 to i64
    %320 = arith.extsi %318 : i32 to i64
    %321 = llvm.getelementptr %172[%320] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %319, %321 : i64, !llvm.ptr
    %322 = arith.constant 1 : i32
    %323 = arith.constant 7 : i32
    %324 = arith.muli %323, %169 : i32
    %325 = arith.constant 6 : i32
    %326 = arith.addi %324, %325 : i32
    %327 = arith.extsi %322 : i32 to i64
    %328 = arith.extsi %326 : i32 to i64
    %329 = llvm.getelementptr %172[%328] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %327, %329 : i64, !llvm.ptr
    %330 = arith.constant 7 : i32
    %331 = arith.constant 7 : i32
    %332 = arith.muli %331, %169 : i32
    %333 = arith.constant 7 : i32
    %334 = arith.addi %332, %333 : i32
    %335 = arith.extsi %330 : i32 to i64
    %336 = arith.extsi %334 : i32 to i64
    %337 = llvm.getelementptr %172[%336] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %335, %337 : i64, !llvm.ptr
    %339 = arith.constant 995705032704 : i32
    %340 = arith.extsi %339 : i32 to i64
    func.call @mat_pow(%172, %340, %169, %171, %177) : (!llvm.ptr, i64, i32, i64, !llvm.ptr) -> ()
    %342 = arith.constant 7 : i32
    %343 = arith.muli %342, %169 : i32
    %344 = arith.constant 0 : i32
    %345 = arith.addi %343, %344 : i32
    %346 = arith.extsi %345 : i32 to i64
    %347 = llvm.getelementptr %177[%346] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %341 = llvm.load %347 : !llvm.ptr -> i64
    %349 = arith.constant 7 : i32
    %350 = arith.muli %349, %169 : i32
    %351 = arith.constant 7 : i32
    %352 = arith.addi %350, %351 : i32
    %353 = arith.extsi %352 : i32 to i64
    %354 = llvm.getelementptr %177[%353] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %348 = llvm.load %354 : !llvm.ptr -> i64
    %355 = llvm.mlir.constant(1 : i64) : i64
    %356 = llvm.alloca %355 x i64 : (i64) -> !llvm.ptr
    llvm.store %348, %356 : i64, !llvm.ptr
    %357 = llvm.load %356 : !llvm.ptr -> i64
    %358 = arith.cmpi slt, %357, %341 : i64
    cf.cond_br %358, ^bb51, ^bb52
    ^bb51:
      %359 = llvm.load %356 : !llvm.ptr -> i64
      %360 = arith.addi %359, %171 : i64
      llvm.store %360, %356 : i64, !llvm.ptr
      cf.br ^bb53
    ^bb52:
      cf.br ^bb53
    ^bb53:
    %361 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %362 = llvm.load %356 : !llvm.ptr -> i64
    %363 = arith.subi %362, %341 : i64
    %364 = llvm.call @printf(%361, %363) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%177) : (!llvm.ptr) -> ()
    func.call @free(%172) : (!llvm.ptr) -> ()
    %367 = arith.constant 0 : i32
    func.return %367 : i32
  }
}