Problem 080

Digital sum of the first one hundred decimal digits of irrational sqrts.

Answer40886
Output40886
StatusPASS
Native helperno
Runtime50 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

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

Flow source

# Project Euler 080
# Digital sum of the first one hundred decimal digits of irrational sqrts.

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

function bi_zero(a: ptr<i32>, cap: i32) -> void {
    let mut i: i32 = 0
    while i < cap {
        a[i] = 0
        i = i + 1
    }
}

function bi_from_int(a: ptr<i32>, cap: i32, v: i64) -> void {
    bi_zero(a, cap)
    let mut x: i64 = v
    let mut i: i32 = 0
    while x > 0 && i < cap {
        a[i] = (x % 10) as i32
        x = x / 10
        i = i + 1
    }
}

function bi_cmp(a: ptr<i32>, b: ptr<i32>, cap: i32) -> i32 {
    let mut i: i32 = cap - 1
    while i >= 0 {
        if a[i] != b[i] {
            if a[i] > b[i] { return 1 }
            return 0 - 1
        }
        i = i - 1
    }
    return 0
}

function bi_add(a: ptr<i32>, b: ptr<i32>, cap: i32) -> void {
    let mut carry: i32 = 0
    let mut i: i32 = 0
    while i < cap {
        let v: i32 = a[i] + b[i] + carry
        a[i] = v % 10
        carry = v / 10
        i = i + 1
    }
}

function bi_sub(a: ptr<i32>, b: ptr<i32>, cap: i32) -> void {
    let mut borrow: i32 = 0
    let mut i: i32 = 0
    while i < cap {
        let v: i32 = a[i] - b[i] - borrow
        if v < 0 {
            a[i] = v + 10
            borrow = 1
        } else {
            a[i] = v
            borrow = 0
        }
        i = i + 1
    }
}

function bi_mul_small(a: ptr<i32>, cap: i32, m: i32) -> void {
    let mut carry: i32 = 0
    let mut i: i32 = 0
    while i < cap {
        let v: i32 = a[i] * m + carry
        a[i] = v % 10
        carry = v / 10
        i = i + 1
    }
}

function bi_mul10(a: ptr<i32>, cap: i32) -> void {
    let mut i: i32 = cap - 1
    while i > 0 {
        a[i] = a[i - 1]
        i = i - 1
    }
    a[0] = 0
}

function is_square(n: i32) -> bool {
    let mut r: i32 = 1
    while r * r < n {
        r = r + 1
    }
    return r * r == n
}

function digit_step(rem: ptr<i32>, root: ptr<i32>, tmp: ptr<i32>, trial: ptr<i32>, cap: i32) -> i32 {
    # find largest x in 0..9: (20*root+x)*x <= rem; update rem/root; return x
    let mut x: i32 = 9
    while x >= 0 {
        memcpy(tmp, root, (cap as i64) * 4)
        bi_mul_small(tmp, cap, 20)
        bi_from_int(trial, cap, x as i64)
        bi_add(tmp, trial, cap)
        bi_mul_small(tmp, cap, x)
        if bi_cmp(tmp, rem, cap) <= 0 {
            break
        }
        x = x - 1
    }
    memcpy(tmp, root, (cap as i64) * 4)
    bi_mul_small(tmp, cap, 20)
    bi_from_int(trial, cap, x as i64)
    bi_add(tmp, trial, cap)
    bi_mul_small(tmp, cap, x)
    bi_sub(rem, tmp, cap)
    bi_mul10(root, cap)
    bi_from_int(trial, cap, x as i64)
    bi_add(root, trial, cap)
    return x
}

function main() -> i32 {
    let digits: i32 = 100
    let cap: i32 = digits + 5
    let rem: ptr<i32> = calloc(cap as i64, 4)
    let root: ptr<i32> = calloc(cap as i64, 4)
    let tmp: ptr<i32> = calloc(cap as i64, 4)
    let trial: ptr<i32> = calloc(cap as i64, 4)
    if rem == null || root == null || tmp == null || trial == null { return 1 }

    let mut total: i64 = 0
    let mut n: i32 = 2
    while n <= 100 {
        if !is_square(n) {
            bi_zero(rem, cap)
            bi_zero(root, cap)
            # first pair is n itself (n < 100 ⇒ one integer pair)
            bi_from_int(rem, cap, n as i64)
            let mut sum: i64 = 0
            let mut d: i32 = 0
            while d < digits {
                let x: i32 = digit_step(rem, root, tmp, trial, cap)
                sum = sum + (x as i64)
                d = d + 1
                if d < digits {
                    bi_mul_small(rem, cap, 100)
                }
            }
            total = total + sum
        }
        n = n + 1
    }
    printf("%lld\n", total)
    free(rem)
    free(root)
    free(tmp)
    free(trial)
    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 bi_zero_ptr_i32_i32(int32_t* a, int32_t cap);
void bi_from_int_ptr_i32_i32_i64(int32_t* a, int32_t cap, int64_t v);
int32_t bi_cmp_ptr_i32_ptr_i32_i32(int32_t* a, int32_t* b, int32_t cap);
void bi_add_ptr_i32_ptr_i32_i32(int32_t* a, int32_t* b, int32_t cap);
void bi_sub_ptr_i32_ptr_i32_i32(int32_t* a, int32_t* b, int32_t cap);
void bi_mul_small_ptr_i32_i32_i32(int32_t* a, int32_t cap, int32_t m);
void bi_mul10_ptr_i32_i32(int32_t* a, int32_t cap);
bool is_square_i32(int32_t n);
int32_t digit_step_ptr_i32_ptr_i32_ptr_i32_ptr_i32_i32(int32_t* rem, int32_t* root, int32_t* tmp, int32_t* trial, int32_t cap);
int32_t main(void);




void bi_zero_ptr_i32_i32(int32_t* a, int32_t cap) {
    int32_t i = 0;
    while (i < cap) {
        a[i] = 0;
        i = (i + 1);
    }
}

void bi_from_int_ptr_i32_i32_i64(int32_t* a, int32_t cap, int64_t v) {
    bi_zero_ptr_i32_i32(a, cap);
    int64_t x = v;
    int32_t i = 0;
    while ((x > 0 && i < cap)) {
        a[i] = ((int32_t)(FLOW_CHECKED_MOD((x), (10))));
        x = FLOW_CHECKED_DIV((x), (10));
        i = (i + 1);
    }
}

int32_t bi_cmp_ptr_i32_ptr_i32_i32(int32_t* a, int32_t* b, int32_t cap) {
    int32_t i = (cap - 1);
    while (i >= 0) {
        if (a[i] != b[i]) {
            if (a[i] > b[i]) {
                return 1;
            }
            return (0 - 1);
        }
        i = (i - 1);
    }
    return 0;
}

void bi_add_ptr_i32_ptr_i32_i32(int32_t* a, int32_t* b, int32_t cap) {
    int32_t carry = 0;
    int32_t i = 0;
    while (i < cap) {
        int32_t v = ((a[i] + b[i]) + carry);
        a[i] = FLOW_CHECKED_MOD((v), (10));
        carry = FLOW_CHECKED_DIV((v), (10));
        i = (i + 1);
    }
}

void bi_sub_ptr_i32_ptr_i32_i32(int32_t* a, int32_t* b, int32_t cap) {
    int32_t borrow = 0;
    int32_t i = 0;
    while (i < cap) {
        int32_t v = ((a[i] - b[i]) - borrow);
        if (v < 0) {
            a[i] = (v + 10);
            borrow = 1;
        } else {
            a[i] = v;
            borrow = 0;
        }
        i = (i + 1);
    }
}

void bi_mul_small_ptr_i32_i32_i32(int32_t* a, int32_t cap, int32_t m) {
    int32_t carry = 0;
    int32_t i = 0;
    while (i < cap) {
        int32_t v = ((a[i] * m) + carry);
        a[i] = FLOW_CHECKED_MOD((v), (10));
        carry = FLOW_CHECKED_DIV((v), (10));
        i = (i + 1);
    }
}

void bi_mul10_ptr_i32_i32(int32_t* a, int32_t cap) {
    int32_t i = (cap - 1);
    while (i > 0) {
        a[i] = a[(i - 1)];
        i = (i - 1);
    }
    a[0] = 0;
}

bool is_square_i32(int32_t n) {
    int32_t r = 1;
    while ((r * r) < n) {
        r = (r + 1);
    }
    return (r * r) == n;
}

int32_t digit_step_ptr_i32_ptr_i32_ptr_i32_ptr_i32_i32(int32_t* rem, int32_t* root, int32_t* tmp, int32_t* trial, int32_t cap) {
    int32_t x = 9;
    while (x >= 0) {
        memcpy(tmp, root, (((int64_t)(cap)) * 4));
        bi_mul_small_ptr_i32_i32_i32(tmp, cap, 20);
        bi_from_int_ptr_i32_i32_i64(trial, cap, ((int64_t)(x)));
        bi_add_ptr_i32_ptr_i32_i32(tmp, trial, cap);
        bi_mul_small_ptr_i32_i32_i32(tmp, cap, x);
        if (bi_cmp_ptr_i32_ptr_i32_i32(tmp, rem, cap) <= 0) {
            break;
        }
        x = (x - 1);
    }
    memcpy(tmp, root, (((int64_t)(cap)) * 4));
    bi_mul_small_ptr_i32_i32_i32(tmp, cap, 20);
    bi_from_int_ptr_i32_i32_i64(trial, cap, ((int64_t)(x)));
    bi_add_ptr_i32_ptr_i32_i32(tmp, trial, cap);
    bi_mul_small_ptr_i32_i32_i32(tmp, cap, x);
    bi_sub_ptr_i32_ptr_i32_i32(rem, tmp, cap);
    bi_mul10_ptr_i32_i32(root, cap);
    bi_from_int_ptr_i32_i32_i64(trial, cap, ((int64_t)(x)));
    bi_add_ptr_i32_ptr_i32_i32(root, trial, cap);
    return x;
}

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