Problem 065

Find the sum of digits in the numerator of the 100th convergent of e. e = [2; 1,2,1, 1,4,1, 1,6,1, ..., 1,2k,1, ...]

Answer272
Output272
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n)
Space complexityO(n^2)O(n)
ApproachFlow solutionBig-integer arithmetic
VerdictOptimal

Flow source

# Project Euler 065
# Find the sum of digits in the numerator of the 100th convergent of e.
# e = [2; 1,2,1, 1,4,1, 1,6,1, ..., 1,2k,1, ...]

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

function max2i(a: i32, b: i32) -> i32 {
    if a > b { return a }
    return b
}

function add(a: ptr<i32>, alen: i32, b: ptr<i32>, blen: i32, out: ptr<i32>) -> i32 {
    let mut carry: i32 = 0
    let mut i: i32 = 0
    let maxlen: i32 = max2i(alen, blen)
    while i < maxlen || carry > 0 {
        let mut v: i32 = carry
        if i < alen { v = v + a[i] }
        if i < blen { v = v + b[i] }
        out[i] = v % 10
        carry = v / 10
        i = i + 1
    }
    return i
}

function mul_small(digits: ptr<i32>, len: i32, m: i64, out: ptr<i32>) -> i32 {
    let mut carry: i64 = 0
    let mut i: i32 = 0
    while i < len {
        let v: i64 = (digits[i] as i64) * m + carry
        out[i] = (v % 10) as i32
        carry = v / 10
        i = i + 1
    }
    while carry > 0 {
        out[i] = (carry % 10) as i32
        carry = carry / 10
        i = i + 1
    }
    return i
}

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

function e_term(k: i32) -> i64 {
    # 0-indexed terms: a0=2, then 1,2,1, 1,4,1, ...
    if k == 0 { return 2 }
    if k % 3 == 2 {
        return 2 * ((k / 3) as i64 + 1)
    }
    return 1
}

function main() -> i32 {
    let width: i32 = 100
    let n0: ptr<i32> = calloc(width as i64, 4)
    let n1: ptr<i32> = calloc(width as i64, 4)
    let n2: ptr<i32> = calloc(width as i64, 4)
    let tmp: ptr<i32> = calloc(width as i64, 4)
    if n0 == null || n1 == null || n2 == null || tmp == null { return 1 }

    # convergents: h_{-1}=1,h_0=a0;  h_n = a_n*h_{n-1} + h_{n-2}
    # Start with h-2=0,h-1=1 then iterate — for numerator only:
    # n_{-2}=0, n_{-1}=1
    # Actually: n-1=1, n0=a0
    n0[0] = 1  # n_{-1}
    let mut len0: i32 = 1
    n1[0] = 2  # n_0 = a0
    let mut len1: i32 = 1

    let mut k: i32 = 1
    while k < 100 {
        let a: i64 = e_term(k)
        let mlen: i32 = mul_small(n1, len1, a, tmp)
        let len2: i32 = add(tmp, mlen, n0, len0, n2)
        copy_digits(n1, n0, len1)
        len0 = len1
        copy_digits(n2, n1, len2)
        len1 = len2
        k = k + 1
    }

    let mut total: i64 = 0
    let mut i: i32 = 0
    while i < len1 {
        total = total + (n1[i] as i64)
        i = i + 1
    }
    printf("%lld\n", total)
    free(tmp)
    free(n2)
    free(n1)
    free(n0)
    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; }

int32_t max2i_i32_i32(int32_t a, int32_t b);
int32_t add_ptr_i32_i32_ptr_i32_i32_ptr_i32(int32_t* a, int32_t alen, int32_t* b, int32_t blen, int32_t* out);
int32_t mul_small_ptr_i32_i32_i64_ptr_i32(int32_t* digits, int32_t len, int64_t m, int32_t* out);
void copy_digits_ptr_i32_ptr_i32_i32(int32_t* src, int32_t* dst, int32_t len);
int64_t e_term_i32(int32_t k);
int32_t main(void);



int32_t max2i_i32_i32(int32_t a, int32_t b) {
    if (a > b) {
        return a;
    }
    return b;
}

int32_t add_ptr_i32_i32_ptr_i32_i32_ptr_i32(int32_t* a, int32_t alen, int32_t* b, int32_t blen, int32_t* out) {
    int32_t carry = 0;
    int32_t i = 0;
    int32_t maxlen = max2i_i32_i32(alen, blen);
    while ((i < maxlen || carry > 0)) {
        int32_t v = carry;
        if (i < alen) {
            v = (v + a[i]);
        }
        if (i < blen) {
            v = (v + b[i]);
        }
        out[i] = FLOW_CHECKED_MOD((v), (10));
        carry = FLOW_CHECKED_DIV((v), (10));
        i = (i + 1);
    }
    return i;
}

int32_t mul_small_ptr_i32_i32_i64_ptr_i32(int32_t* digits, int32_t len, int64_t m, int32_t* out) {
    int64_t carry = 0;
    int32_t i = 0;
    while (i < len) {
        int64_t v = ((((int64_t)(digits[i])) * m) + carry);
        out[i] = ((int32_t)(FLOW_CHECKED_MOD((v), (10))));
        carry = FLOW_CHECKED_DIV((v), (10));
        i = (i + 1);
    }
    while (carry > 0) {
        out[i] = ((int32_t)(FLOW_CHECKED_MOD((carry), (10))));
        carry = FLOW_CHECKED_DIV((carry), (10));
        i = (i + 1);
    }
    return i;
}

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

int64_t e_term_i32(int32_t k) {
    if (k == 0) {
        return 2;
    }
    if (FLOW_CHECKED_MOD((k), (3)) == 2) {
        return (2 * (((int64_t)(FLOW_CHECKED_DIV((k), (3)))) + 1));
    }
    return 1;
}

int32_t main(void) {
    int32_t width = 100;
    int32_t* n0 = (int32_t*)(calloc(((int64_t)(width)), 4));
    int32_t* n1 = (int32_t*)(calloc(((int64_t)(width)), 4));
    int32_t* n2 = (int32_t*)(calloc(((int64_t)(width)), 4));
    int32_t* tmp = (int32_t*)(calloc(((int64_t)(width)), 4));
    if ((((n0 == NULL || n1 == NULL) || n2 == NULL) || tmp == NULL)) {
        return 1;
    }
    n0[0] = 1;
    int32_t len0 = 1;
    n1[0] = 2;
    int32_t len1 = 1;
    int32_t k = 1;
    while (k < 100) {
        int64_t a = e_term_i32(k);
        int32_t mlen = mul_small_ptr_i32_i32_i64_ptr_i32(n1, len1, a, tmp);
        int32_t len2 = add_ptr_i32_i32_ptr_i32_i32_ptr_i32(tmp, mlen, n0, len0, n2);
        copy_digits_ptr_i32_ptr_i32_i32(n1, n0, len1);
        len0 = len1;
        copy_digits_ptr_i32_ptr_i32_i32(n2, n1, len2);
        len1 = len2;
        k = (k + 1);
    }
    int64_t total = 0;
    int32_t i = 0;
    while (i < len1) {
        total = (total + ((int64_t)(n1[i])));
        i = (i + 1);
    }
    printf("%lld\n", total);
    free(tmp);
    free(n2);
    free(n1);
    free(n0);
    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 @max2i(%arg0: i32, %arg1: i32) -> i32 {
    %0 = arith.cmpi sgt, %arg0, %arg1 : i32
    cf.cond_br %0, ^bb0, ^bb1
    ^bb0:
      func.return %arg0 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    func.return %arg1 : i32
  }
  func.func @add(%arg0: !llvm.ptr, %arg1: i32, %arg2: !llvm.ptr, %arg3: i32, %arg4: !llvm.ptr) -> i32 {
    %1 = arith.constant 0 : i32
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i32 : (i64) -> !llvm.ptr
    llvm.store %1, %3 : i32, !llvm.ptr
    %4 = arith.constant 0 : i32
    %5 = llvm.mlir.constant(1 : i64) : i64
    %6 = llvm.alloca %5 x i32 : (i64) -> !llvm.ptr
    llvm.store %4, %6 : i32, !llvm.ptr
    %7 = func.call @max2i(%arg1, %arg3) : (i32, i32) -> i32
    cf.br ^bb3
    ^bb3:
    %8 = llvm.load %6 : !llvm.ptr -> i32
    %9 = arith.cmpi slt, %8, %7 : i32
    %10 = scf.if %9 -> (i1) {
      %11 = arith.constant true
      scf.yield %11 : i1
    } else {
      %12 = llvm.load %3 : !llvm.ptr -> i32
      %13 = arith.constant 0 : i32
      %14 = arith.cmpi sgt, %12, %13 : i32
      scf.yield %14 : i1
    }
    cf.cond_br %10, ^bb4, ^bb5
    ^bb4:
      %15 = llvm.load %3 : !llvm.ptr -> i32
      %16 = llvm.mlir.constant(1 : i64) : i64
      %17 = llvm.alloca %16 x i32 : (i64) -> !llvm.ptr
      llvm.store %15, %17 : i32, !llvm.ptr
      %18 = llvm.load %6 : !llvm.ptr -> i32
      %19 = arith.cmpi slt, %18, %arg1 : i32
      cf.cond_br %19, ^bb6, ^bb7
      ^bb6:
        %20 = llvm.load %17 : !llvm.ptr -> i32
        %22 = llvm.load %6 : !llvm.ptr -> i32
        %23 = arith.extsi %22 : i32 to i64
        %24 = llvm.getelementptr %arg0[%23] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %21 = llvm.load %24 : !llvm.ptr -> i32
        %25 = arith.addi %20, %21 : i32
        llvm.store %25, %17 : i32, !llvm.ptr
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %26 = llvm.load %6 : !llvm.ptr -> i32
      %27 = arith.cmpi slt, %26, %arg3 : i32
      cf.cond_br %27, ^bb9, ^bb10
      ^bb9:
        %28 = llvm.load %17 : !llvm.ptr -> i32
        %30 = llvm.load %6 : !llvm.ptr -> i32
        %31 = arith.extsi %30 : i32 to i64
        %32 = llvm.getelementptr %arg2[%31] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %29 = llvm.load %32 : !llvm.ptr -> i32
        %33 = arith.addi %28, %29 : i32
        llvm.store %33, %17 : i32, !llvm.ptr
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %34 = llvm.load %17 : !llvm.ptr -> i32
      %35 = arith.constant 10 : i32
      %36 = arith.remsi %34, %35 : i32
      %37 = llvm.load %6 : !llvm.ptr -> i32
      %38 = arith.extsi %37 : i32 to i64
      %39 = llvm.getelementptr %arg4[%38] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %36, %39 : i32, !llvm.ptr
      %40 = llvm.load %17 : !llvm.ptr -> i32
      %41 = arith.constant 10 : i32
      %42 = arith.divsi %40, %41 : i32
      llvm.store %42, %3 : i32, !llvm.ptr
      %43 = llvm.load %6 : !llvm.ptr -> i32
      %44 = arith.constant 1 : i32
      %45 = arith.addi %43, %44 : i32
      llvm.store %45, %6 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %46 = llvm.load %6 : !llvm.ptr -> i32
    func.return %46 : i32
  }
  func.func @mul_small(%arg0: !llvm.ptr, %arg1: i32, %arg2: i64, %arg3: !llvm.ptr) -> i32 {
    %47 = arith.constant 0 : i32
    %48 = arith.extsi %47 : i32 to i64
    %49 = llvm.mlir.constant(1 : i64) : i64
    %50 = llvm.alloca %49 x i64 : (i64) -> !llvm.ptr
    llvm.store %48, %50 : i64, !llvm.ptr
    %51 = arith.constant 0 : i32
    %52 = llvm.mlir.constant(1 : i64) : i64
    %53 = llvm.alloca %52 x i32 : (i64) -> !llvm.ptr
    llvm.store %51, %53 : i32, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %54 = llvm.load %53 : !llvm.ptr -> i32
    %55 = arith.cmpi slt, %54, %arg1 : i32
    cf.cond_br %55, ^bb13, ^bb14
    ^bb13:
      %57 = llvm.load %53 : !llvm.ptr -> i32
      %58 = arith.extsi %57 : i32 to i64
      %59 = llvm.getelementptr %arg0[%58] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %56 = llvm.load %59 : !llvm.ptr -> i32
      %60 = arith.extsi %56 : i32 to i64
      %61 = arith.muli %60, %arg2 : i64
      %62 = llvm.load %50 : !llvm.ptr -> i64
      %63 = arith.addi %61, %62 : i64
      %64 = arith.constant 10 : i32
      %66 = arith.extsi %64 : i32 to i64
      %65 = arith.remsi %63, %66 : i64
      %67 = arith.trunci %65 : i64 to i32
      %68 = llvm.load %53 : !llvm.ptr -> i32
      %69 = arith.extsi %68 : i32 to i64
      %70 = llvm.getelementptr %arg3[%69] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %67, %70 : i32, !llvm.ptr
      %71 = arith.constant 10 : i32
      %73 = arith.extsi %71 : i32 to i64
      %72 = arith.divsi %63, %73 : i64
      llvm.store %72, %50 : i64, !llvm.ptr
      %74 = llvm.load %53 : !llvm.ptr -> i32
      %75 = arith.constant 1 : i32
      %76 = arith.addi %74, %75 : i32
      llvm.store %76, %53 : i32, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    cf.br ^bb15
    ^bb15:
    %77 = llvm.load %50 : !llvm.ptr -> i64
    %78 = arith.constant 0 : i32
    %80 = arith.extsi %78 : i32 to i64
    %79 = arith.cmpi sgt, %77, %80 : i64
    cf.cond_br %79, ^bb16, ^bb17
    ^bb16:
      %81 = llvm.load %50 : !llvm.ptr -> i64
      %82 = arith.constant 10 : i32
      %84 = arith.extsi %82 : i32 to i64
      %83 = arith.remsi %81, %84 : i64
      %85 = arith.trunci %83 : i64 to i32
      %86 = llvm.load %53 : !llvm.ptr -> i32
      %87 = arith.extsi %86 : i32 to i64
      %88 = llvm.getelementptr %arg3[%87] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %85, %88 : i32, !llvm.ptr
      %89 = llvm.load %50 : !llvm.ptr -> i64
      %90 = arith.constant 10 : i32
      %92 = arith.extsi %90 : i32 to i64
      %91 = arith.divsi %89, %92 : i64
      llvm.store %91, %50 : i64, !llvm.ptr
      %93 = llvm.load %53 : !llvm.ptr -> i32
      %94 = arith.constant 1 : i32
      %95 = arith.addi %93, %94 : i32
      llvm.store %95, %53 : i32, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %96 = llvm.load %53 : !llvm.ptr -> i32
    func.return %96 : i32
  }
  func.func @copy_digits(%arg0: !llvm.ptr, %arg1: !llvm.ptr, %arg2: i32) -> () {
    %97 = arith.constant 0 : i32
    %98 = llvm.mlir.constant(1 : i64) : i64
    %99 = llvm.alloca %98 x i32 : (i64) -> !llvm.ptr
    llvm.store %97, %99 : i32, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %100 = llvm.load %99 : !llvm.ptr -> i32
    %101 = arith.cmpi slt, %100, %arg2 : i32
    cf.cond_br %101, ^bb19, ^bb20
    ^bb19:
      %103 = llvm.load %99 : !llvm.ptr -> i32
      %104 = arith.extsi %103 : i32 to i64
      %105 = llvm.getelementptr %arg0[%104] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %102 = llvm.load %105 : !llvm.ptr -> i32
      %106 = llvm.load %99 : !llvm.ptr -> i32
      %107 = arith.extsi %106 : i32 to i64
      %108 = llvm.getelementptr %arg1[%107] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %102, %108 : i32, !llvm.ptr
      %109 = llvm.load %99 : !llvm.ptr -> i32
      %110 = arith.constant 1 : i32
      %111 = arith.addi %109, %110 : i32
      llvm.store %111, %99 : i32, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    func.return
  }
  func.func @e_term(%arg0: i32) -> i64 {
    %112 = arith.constant 0 : i32
    %113 = arith.cmpi eq, %arg0, %112 : i32
    cf.cond_br %113, ^bb21, ^bb22
    ^bb21:
      %114 = arith.constant 2 : i32
      %115 = arith.extsi %114 : i32 to i64
      func.return %115 : i64
    ^bb22:
      cf.br ^bb23
    ^bb23:
    %116 = arith.constant 3 : i32
    %117 = arith.remsi %arg0, %116 : i32
    %118 = arith.constant 2 : i32
    %119 = arith.cmpi eq, %117, %118 : i32
    cf.cond_br %119, ^bb24, ^bb25
    ^bb24:
      %120 = arith.constant 2 : i32
      %121 = arith.constant 3 : i32
      %122 = arith.divsi %arg0, %121 : i32
      %123 = arith.extsi %122 : i32 to i64
      %124 = arith.constant 1 : i32
      %126 = arith.extsi %124 : i32 to i64
      %125 = arith.addi %123, %126 : i64
      %128 = arith.extsi %120 : i32 to i64
      %127 = arith.muli %128, %125 : i64
      func.return %127 : i64
    ^bb25:
      cf.br ^bb26
    ^bb26:
    %129 = arith.constant 1 : i32
    %130 = arith.extsi %129 : i32 to i64
    func.return %130 : i64
  }
  func.func @main() -> i32 {
    %131 = arith.constant 100 : i32
    %133 = arith.extsi %131 : i32 to i64
    %134 = arith.constant 4 : i32
    %135 = arith.extsi %134 : i32 to i64
    %132 = func.call @calloc(%133, %135) : (i64, i64) -> !llvm.ptr
    %137 = arith.extsi %131 : i32 to i64
    %138 = arith.constant 4 : i32
    %139 = arith.extsi %138 : i32 to i64
    %136 = func.call @calloc(%137, %139) : (i64, i64) -> !llvm.ptr
    %141 = arith.extsi %131 : i32 to i64
    %142 = arith.constant 4 : i32
    %143 = arith.extsi %142 : i32 to i64
    %140 = func.call @calloc(%141, %143) : (i64, i64) -> !llvm.ptr
    %145 = arith.extsi %131 : i32 to i64
    %146 = arith.constant 4 : i32
    %147 = arith.extsi %146 : i32 to i64
    %144 = func.call @calloc(%145, %147) : (i64, i64) -> !llvm.ptr
    %148 = llvm.mlir.zero : !llvm.ptr
    %149 = llvm.icmp "eq" %132, %148 : !llvm.ptr
    %150 = scf.if %149 -> (i1) {
      %151 = arith.constant true
      scf.yield %151 : i1
    } else {
      %152 = llvm.mlir.zero : !llvm.ptr
      %153 = llvm.icmp "eq" %136, %152 : !llvm.ptr
      scf.yield %153 : i1
    }
    %154 = scf.if %150 -> (i1) {
      %155 = arith.constant true
      scf.yield %155 : i1
    } else {
      %156 = llvm.mlir.zero : !llvm.ptr
      %157 = llvm.icmp "eq" %140, %156 : !llvm.ptr
      scf.yield %157 : i1
    }
    %158 = scf.if %154 -> (i1) {
      %159 = arith.constant true
      scf.yield %159 : i1
    } else {
      %160 = llvm.mlir.zero : !llvm.ptr
      %161 = llvm.icmp "eq" %144, %160 : !llvm.ptr
      scf.yield %161 : i1
    }
    cf.cond_br %158, ^bb27, ^bb28
    ^bb27:
      %162 = arith.constant 1 : i32
      func.return %162 : i32
    ^bb28:
      cf.br ^bb29
    ^bb29:
    %163 = arith.constant 1 : i32
    %164 = arith.constant 0 : i32
    %165 = arith.extsi %164 : i32 to i64
    %166 = llvm.getelementptr %132[%165] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    llvm.store %163, %166 : i32, !llvm.ptr
    %167 = arith.constant 1 : i32
    %168 = llvm.mlir.constant(1 : i64) : i64
    %169 = llvm.alloca %168 x i32 : (i64) -> !llvm.ptr
    llvm.store %167, %169 : i32, !llvm.ptr
    %170 = arith.constant 2 : i32
    %171 = arith.constant 0 : i32
    %172 = arith.extsi %171 : i32 to i64
    %173 = llvm.getelementptr %136[%172] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    llvm.store %170, %173 : i32, !llvm.ptr
    %174 = arith.constant 1 : i32
    %175 = llvm.mlir.constant(1 : i64) : i64
    %176 = llvm.alloca %175 x i32 : (i64) -> !llvm.ptr
    llvm.store %174, %176 : i32, !llvm.ptr
    %177 = arith.constant 1 : i32
    %178 = llvm.mlir.constant(1 : i64) : i64
    %179 = llvm.alloca %178 x i32 : (i64) -> !llvm.ptr
    llvm.store %177, %179 : i32, !llvm.ptr
    cf.br ^bb30
    ^bb30:
    %180 = llvm.load %179 : !llvm.ptr -> i32
    %181 = arith.constant 100 : i32
    %182 = arith.cmpi slt, %180, %181 : i32
    cf.cond_br %182, ^bb31, ^bb32
    ^bb31:
      %184 = llvm.load %179 : !llvm.ptr -> i32
      %183 = func.call @e_term(%184) : (i32) -> i64
      %186 = llvm.load %176 : !llvm.ptr -> i32
      %185 = func.call @mul_small(%136, %186, %183, %144) : (!llvm.ptr, i32, i64, !llvm.ptr) -> i32
      %188 = llvm.load %169 : !llvm.ptr -> i32
      %187 = func.call @add(%144, %185, %132, %188, %140) : (!llvm.ptr, i32, !llvm.ptr, i32, !llvm.ptr) -> i32
      %190 = llvm.load %176 : !llvm.ptr -> i32
      func.call @copy_digits(%136, %132, %190) : (!llvm.ptr, !llvm.ptr, i32) -> ()
      %191 = llvm.load %176 : !llvm.ptr -> i32
      llvm.store %191, %169 : i32, !llvm.ptr
      func.call @copy_digits(%140, %136, %187) : (!llvm.ptr, !llvm.ptr, i32) -> ()
      llvm.store %187, %176 : i32, !llvm.ptr
      %193 = llvm.load %179 : !llvm.ptr -> i32
      %194 = arith.constant 1 : i32
      %195 = arith.addi %193, %194 : i32
      llvm.store %195, %179 : i32, !llvm.ptr
      cf.br ^bb30
    ^bb32:
    %196 = arith.constant 0 : i32
    %197 = arith.extsi %196 : i32 to i64
    %198 = llvm.mlir.constant(1 : i64) : i64
    %199 = llvm.alloca %198 x i64 : (i64) -> !llvm.ptr
    llvm.store %197, %199 : i64, !llvm.ptr
    %200 = arith.constant 0 : i32
    %201 = llvm.mlir.constant(1 : i64) : i64
    %202 = llvm.alloca %201 x i32 : (i64) -> !llvm.ptr
    llvm.store %200, %202 : i32, !llvm.ptr
    cf.br ^bb33
    ^bb33:
    %203 = llvm.load %202 : !llvm.ptr -> i32
    %204 = llvm.load %176 : !llvm.ptr -> i32
    %205 = arith.cmpi slt, %203, %204 : i32
    cf.cond_br %205, ^bb34, ^bb35
    ^bb34:
      %206 = llvm.load %199 : !llvm.ptr -> i64
      %208 = llvm.load %202 : !llvm.ptr -> i32
      %209 = arith.extsi %208 : i32 to i64
      %210 = llvm.getelementptr %136[%209] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %207 = llvm.load %210 : !llvm.ptr -> i32
      %211 = arith.extsi %207 : i32 to i64
      %212 = arith.addi %206, %211 : i64
      llvm.store %212, %199 : i64, !llvm.ptr
      %213 = llvm.load %202 : !llvm.ptr -> i32
      %214 = arith.constant 1 : i32
      %215 = arith.addi %213, %214 : i32
      llvm.store %215, %202 : i32, !llvm.ptr
      cf.br ^bb33
    ^bb35:
    %216 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %217 = llvm.load %199 : !llvm.ptr -> i64
    %218 = llvm.call @printf(%216, %217) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%144) : (!llvm.ptr) -> ()
    func.call @free(%140) : (!llvm.ptr) -> ()
    func.call @free(%136) : (!llvm.ptr) -> ()
    func.call @free(%132) : (!llvm.ptr) -> ()
    %223 = arith.constant 0 : i32
    func.return %223 : i32
  }
}