Problem 377

Last 9 digits of sum_{i=1..17} f(13^i) for digit-sum numbers without 0.

Answer732385277
Output732385277
StatusPASS
Native helperno
Runtime0 ms
Peak memory1296 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 solutionBig-integer arithmetic
VerdictSuboptimal

Flow source

# Project Euler 377
# Last 9 digits of sum_{i=1..17} f(13^i) for digit-sum numbers without 0.

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>, mod: i64) -> void {
    let mut i: i32 = 0
    while i < 18 {
        let mut j: i32 = 0
        while j < 18 {
            let mut s: i64 = 0
            let mut t: i32 = 0
            while t < 18 {
                s = (s + A[i * 18 + t] * B[t * 18 + j]) % mod
                t = t + 1
            }
            out[i * 18 + j] = s
            j = j + 1
        }
        i = i + 1
    }
}

function mat_vec(A: ptr<i64>, v: ptr<i64>, out: ptr<i64>, mod: i64) -> void {
    let mut i: i32 = 0
    while i < 18 {
        let mut s: i64 = 0
        let mut j: i32 = 0
        while j < 18 {
            s = (s + A[i * 18 + j] * v[j]) % mod
            j = j + 1
        }
        out[i] = s
        i = i + 1
    }
}

function mat_copy(src: ptr<i64>, dst: ptr<i64>) -> void {
    let mut i: i32 = 0
    while i < 324 {
        dst[i] = src[i]
        i = i + 1
    }
}

function vec_copy(src: ptr<i64>, dst: ptr<i64>) -> void {
    let mut i: i32 = 0
    while i < 18 {
        dst[i] = src[i]
        i = i + 1
    }
}

function f_of(n0: i64, powers: ptr<i64>, v0: ptr<i64>, mod: i64) -> i64 {
    let v: ptr<i64> = calloc(18, 8)
    let tmp: ptr<i64> = calloc(18, 8)
    if v == null || tmp == null { return 0 }
    vec_copy(v0, v)
    let mut n: i64 = n0
    let mut bit: i32 = 0
    while n > 0 {
        if n % 2 == 1 {
            mat_vec(powers + (bit as i64) * 324, v, tmp, mod)
            vec_copy(tmp, v)
        }
        n = n / 2
        bit = bit + 1
    }
    let ans: i64 = v[9]
    free(tmp)
    free(v)
    return ans
}

function main() -> i32 {
    let MOD: i64 = 1000000000
    let powers: ptr<i64> = calloc(64 * 324, 8)
    let tmp: ptr<i64> = calloc(324, 8)
    let v0: ptr<i64> = calloc(18, 8)
    if powers == null || tmp == null || v0 == null { return 1 }

    # Build M into powers[0]
    let M: ptr<i64> = powers
    let mut j: i32 = 0
    while j < 9 {
        M[0 * 18 + j] = 10 % MOD
        j = j + 1
    }
    let mut i: i32 = 1
    while i < 9 {
        M[i * 18 + (i - 1)] = 1
        i = i + 1
    }
    j = 0
    while j < 9 {
        M[9 * 18 + j] = (j + 1) as i64
        M[9 * 18 + 9 + j] = 1
        j = j + 1
    }
    i = 10
    while i < 18 {
        M[i * 18 + (i - 1)] = 1
        i = i + 1
    }

    let mut b: i32 = 1
    while b < 64 {
        mat_mul(powers + ((b - 1) as i64) * 324, powers + ((b - 1) as i64) * 324, tmp, MOD)
        mat_copy(tmp, powers + (b as i64) * 324)
        b = b + 1
    }

    v0[0] = 1

    let mut total: i64 = 0
    let mut pow13: i64 = 13
    let mut ii: i32 = 1
    while ii <= 17 {
        total = (total + f_of(pow13, powers, v0, MOD)) % MOD
        if ii < 17 { pow13 = pow13 * 13 }
        ii = ii + 1
    }

    printf("%lld\n", total)
    free(v0)
    free(tmp)
    free(powers)
    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_i64(int64_t* A, int64_t* B, int64_t* out, int64_t mod);
void mat_vec_ptr_i64_ptr_i64_ptr_i64_i64(int64_t* A, int64_t* v, int64_t* out, int64_t mod);
void mat_copy_ptr_i64_ptr_i64(int64_t* src, int64_t* dst);
void vec_copy_ptr_i64_ptr_i64(int64_t* src, int64_t* dst);
int64_t f_of_i64_ptr_i64_ptr_i64_i64(int64_t n0, int64_t* powers, int64_t* v0, int64_t mod);
int32_t main(void);



void mat_mul_ptr_i64_ptr_i64_ptr_i64_i64(int64_t* A, int64_t* B, int64_t* out, int64_t mod) {
    int32_t i = 0;
    while (i < 18) {
        int32_t j = 0;
        while (j < 18) {
            int64_t s = 0;
            int32_t t = 0;
            while (t < 18) {
                s = FLOW_CHECKED_MOD(((s + (A[((i * 18) + t)] * B[((t * 18) + j)]))), (mod));
                t = (t + 1);
            }
            out[((i * 18) + j)] = s;
            j = (j + 1);
        }
        i = (i + 1);
    }
}

void mat_vec_ptr_i64_ptr_i64_ptr_i64_i64(int64_t* A, int64_t* v, int64_t* out, int64_t mod) {
    int32_t i = 0;
    while (i < 18) {
        int64_t s = 0;
        int32_t j = 0;
        while (j < 18) {
            s = FLOW_CHECKED_MOD(((s + (A[((i * 18) + j)] * v[j]))), (mod));
            j = (j + 1);
        }
        out[i] = s;
        i = (i + 1);
    }
}

void mat_copy_ptr_i64_ptr_i64(int64_t* src, int64_t* dst) {
    int32_t i = 0;
    while (i < 324) {
        dst[i] = src[i];
        i = (i + 1);
    }
}

void vec_copy_ptr_i64_ptr_i64(int64_t* src, int64_t* dst) {
    int32_t i = 0;
    while (i < 18) {
        dst[i] = src[i];
        i = (i + 1);
    }
}

int64_t f_of_i64_ptr_i64_ptr_i64_i64(int64_t n0, int64_t* powers, int64_t* v0, int64_t mod) {
    int64_t* v = (int64_t*)(calloc(18, 8));
    int64_t* tmp = (int64_t*)(calloc(18, 8));
    if ((v == NULL || tmp == NULL)) {
        return 0;
    }
    vec_copy_ptr_i64_ptr_i64(v0, v);
    int64_t n = n0;
    int32_t bit = 0;
    while (n > 0) {
        if (FLOW_CHECKED_MOD((n), (2)) == 1) {
            mat_vec_ptr_i64_ptr_i64_ptr_i64_i64((powers + (((int64_t)(bit)) * 324)), v, tmp, mod);
            vec_copy_ptr_i64_ptr_i64(tmp, v);
        }
        n = FLOW_CHECKED_DIV((n), (2));
        bit = (bit + 1);
    }
    int64_t ans = v[9];
    free(tmp);
    free(v);
    return ans;
}

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