Problem 808

Reversible Prime Squares — sum of first 50 reversible prime squares.

Answer3807504276997394
Output3807504276997394
StatusPASS
Native helperno
Runtime730 ms
Peak memory49920 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log log n)
Space complexityO(n)O(n)
ApproachFlow solutionSieve or enumeration
VerdictSuboptimal

Flow source

# Project Euler 808
# Reversible Prime Squares — sum of first 50 reversible prime squares.

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

function isqrt(n: i64) -> i64 {
    if n < 2 { return n }
    let mut x: i64 = n
    let mut y: i64 = (x + 1) / 2
    while y < x {
        x = y
        y = (x + n / x) / 2
    }
    return x
}

function reverse_num(x: i64) -> i64 {
    let mut n: i64 = x
    let mut r: i64 = 0
    while n > 0 {
        r = r * 10 + (n % 10)
        n = n / 10
    }
    return r
}

function is_palindrome(x: i64) -> i32 {
    if reverse_num(x) == x { return 1 }
    return 0
}

function solve() -> i64 {
    let limit: i64 = 50000000
    let is_prime: ptr<i8> = calloc(limit + 1, 1)
    # 0 = prime after sieve setup; mark composites as 1
    is_prime[0] = 1
    is_prime[1] = 1
    let mut i: i64 = 2
    while i * i <= limit {
        if is_prime[i] == 0 {
            let mut j: i64 = i * i
            while j <= limit {
                is_prime[j] = 1
                j = j + i
            }
        }
        i = i + 1
    }

    let values: ptr<i64> = calloc(200, 8)
    let mut vcount: i64 = 0
    let mut p: i64 = 11  # skip tiny primes; start past 2,3,5,7
    while p <= limit {
        if is_prime[p] == 0 {
            let sq: i64 = p * p
            if is_palindrome(sq) == 0 {
                let rev: i64 = reverse_num(sq)
                let r: i64 = isqrt(rev)
                if r * r == rev && r <= limit && is_prime[r] == 0 {
                    # add both if unique
                    let mut found: i32 = 0
                    let mut k: i64 = 0
                    while k < vcount {
                        if values[k] == sq { found = 1 }
                        k = k + 1
                    }
                    if found == 0 {
                        values[vcount] = sq
                        vcount = vcount + 1
                    }
                    found = 0
                    k = 0
                    while k < vcount {
                        if values[k] == rev { found = 1 }
                        k = k + 1
                    }
                    if found == 0 {
                        values[vcount] = rev
                        vcount = vcount + 1
                    }
                }
            }
        }
        p = p + 1
    }

    # sort values
    let mut a: i64 = 0
    while a < vcount {
        let mut b: i64 = a + 1
        while b < vcount {
            if values[b] < values[a] {
                let t: i64 = values[a]
                values[a] = values[b]
                values[b] = t
            }
            b = b + 1
        }
        a = a + 1
    }

    let mut total: i64 = 0
    let mut ntake: i64 = 50
    if vcount < ntake { ntake = vcount }
    a = 0
    while a < ntake {
        total = total + values[a]
        a = a + 1
    }
    free(is_prime)
    free(values)
    return total
}

function main() -> i32 {
    printf("%lld\n", solve())
    return 0
}

Generated C

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

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

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

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

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

#include <math.h>

void* _ui_state = NULL;

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

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

int64_t isqrt_i64(int64_t n);
int64_t reverse_num_i64(int64_t x);
int32_t is_palindrome_i64(int64_t x);
int64_t solve(void);
int32_t main(void);



int64_t isqrt_i64(int64_t n) {
    if (n < 2) {
        return n;
    }
    int64_t x = n;
    int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
    while (y < x) {
        x = y;
        y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
    }
    return x;
}

int64_t reverse_num_i64(int64_t x) {
    int64_t n = x;
    int64_t r = 0;
    while (n > 0) {
        r = ((r * 10) + FLOW_CHECKED_MOD((n), (10)));
        n = FLOW_CHECKED_DIV((n), (10));
    }
    return r;
}

int32_t is_palindrome_i64(int64_t x) {
    if (reverse_num_i64(x) == x) {
        return 1;
    }
    return 0;
}

int64_t solve(void) {
    int64_t limit = 50000000;
    int8_t* is_prime = (int8_t*)(calloc((limit + 1), 1));
    is_prime[0] = 1;
    is_prime[1] = 1;
    int64_t i = 2;
    while ((i * i) <= limit) {
        if (is_prime[i] == 0) {
            int64_t j = (i * i);
            while (j <= limit) {
                is_prime[j] = 1;
                j = (j + i);
            }
        }
        i = (i + 1);
    }
    int64_t* values = (int64_t*)(calloc(200, 8));
    int64_t vcount = 0;
    int64_t p = 11;
    while (p <= limit) {
        if (is_prime[p] == 0) {
            int64_t sq = (p * p);
            if (is_palindrome_i64(sq) == 0) {
                int64_t rev = reverse_num_i64(sq);
                int64_t r = isqrt_i64(rev);
                if ((((r * r) == rev && r <= limit) && is_prime[r] == 0)) {
                    int32_t found = 0;
                    int64_t k = 0;
                    while (k < vcount) {
                        if (values[k] == sq) {
                            found = 1;
                        }
                        k = (k + 1);
                    }
                    if (found == 0) {
                        values[vcount] = sq;
                        vcount = (vcount + 1);
                    }
                    found = 0;
                    k = 0;
                    while (k < vcount) {
                        if (values[k] == rev) {
                            found = 1;
                        }
                        k = (k + 1);
                    }
                    if (found == 0) {
                        values[vcount] = rev;
                        vcount = (vcount + 1);
                    }
                }
            }
        }
        p = (p + 1);
    }
    int64_t a = 0;
    while (a < vcount) {
        int64_t b = (a + 1);
        while (b < vcount) {
            if (values[b] < values[a]) {
                int64_t t = values[a];
                values[a] = values[b];
                values[b] = t;
            }
            b = (b + 1);
        }
        a = (a + 1);
    }
    int64_t total = 0;
    int64_t ntake = 50;
    if (vcount < ntake) {
        ntake = vcount;
    }
    a = 0;
    while (a < ntake) {
        total = (total + values[a]);
        a = (a + 1);
    }
    free(is_prime);
    free(values);
    return total;
}

int32_t main(void) {
    printf("%lld\n", solve());
    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 @isqrt(%arg0: i64) -> i64 {
    %0 = arith.constant 2 : i32
    %2 = arith.extsi %0 : i32 to i64
    %1 = arith.cmpi slt, %arg0, %2 : i64
    cf.cond_br %1, ^bb0, ^bb1
    ^bb0:
      func.return %arg0 : i64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %3 = llvm.mlir.constant(1 : i64) : i64
    %4 = llvm.alloca %3 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %4 : i64, !llvm.ptr
    %5 = llvm.load %4 : !llvm.ptr -> i64
    %6 = arith.constant 1 : i32
    %8 = arith.extsi %6 : i32 to i64
    %7 = arith.addi %5, %8 : i64
    %9 = arith.constant 2 : i32
    %11 = arith.extsi %9 : i32 to i64
    %10 = arith.divsi %7, %11 : i64
    %12 = llvm.mlir.constant(1 : i64) : i64
    %13 = llvm.alloca %12 x i64 : (i64) -> !llvm.ptr
    llvm.store %10, %13 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %14 = llvm.load %13 : !llvm.ptr -> i64
    %15 = llvm.load %4 : !llvm.ptr -> i64
    %16 = arith.cmpi slt, %14, %15 : i64
    cf.cond_br %16, ^bb4, ^bb5
    ^bb4:
      %17 = llvm.load %13 : !llvm.ptr -> i64
      llvm.store %17, %4 : i64, !llvm.ptr
      %18 = llvm.load %4 : !llvm.ptr -> i64
      %19 = llvm.load %4 : !llvm.ptr -> i64
      %20 = arith.divsi %arg0, %19 : i64
      %21 = arith.addi %18, %20 : i64
      %22 = arith.constant 2 : i32
      %24 = arith.extsi %22 : i32 to i64
      %23 = arith.divsi %21, %24 : i64
      llvm.store %23, %13 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %25 = llvm.load %4 : !llvm.ptr -> i64
    func.return %25 : i64
  }
  func.func @reverse_num(%arg0: i64) -> i64 {
    %26 = llvm.mlir.constant(1 : i64) : i64
    %27 = llvm.alloca %26 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %27 : i64, !llvm.ptr
    %28 = arith.constant 0 : i32
    %29 = arith.extsi %28 : i32 to i64
    %30 = llvm.mlir.constant(1 : i64) : i64
    %31 = llvm.alloca %30 x i64 : (i64) -> !llvm.ptr
    llvm.store %29, %31 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %32 = llvm.load %27 : !llvm.ptr -> i64
    %33 = arith.constant 0 : i32
    %35 = arith.extsi %33 : i32 to i64
    %34 = arith.cmpi sgt, %32, %35 : i64
    cf.cond_br %34, ^bb7, ^bb8
    ^bb7:
      %36 = llvm.load %31 : !llvm.ptr -> i64
      %37 = arith.constant 10 : i32
      %39 = arith.extsi %37 : i32 to i64
      %38 = arith.muli %36, %39 : i64
      %40 = llvm.load %27 : !llvm.ptr -> i64
      %41 = arith.constant 10 : i32
      %43 = arith.extsi %41 : i32 to i64
      %42 = arith.remsi %40, %43 : i64
      %44 = arith.addi %38, %42 : i64
      llvm.store %44, %31 : i64, !llvm.ptr
      %45 = llvm.load %27 : !llvm.ptr -> i64
      %46 = arith.constant 10 : i32
      %48 = arith.extsi %46 : i32 to i64
      %47 = arith.divsi %45, %48 : i64
      llvm.store %47, %27 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %49 = llvm.load %31 : !llvm.ptr -> i64
    func.return %49 : i64
  }
  func.func @is_palindrome(%arg0: i64) -> i32 {
    %50 = func.call @reverse_num(%arg0) : (i64) -> i64
    %51 = arith.cmpi eq, %50, %arg0 : i64
    cf.cond_br %51, ^bb9, ^bb10
    ^bb9:
      %52 = arith.constant 1 : i32
      func.return %52 : i32
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %53 = arith.constant 0 : i32
    func.return %53 : i32
  }
  func.func @solve() -> i64 {
    %54 = arith.constant 50000000 : i32
    %55 = arith.extsi %54 : i32 to i64
    %57 = arith.constant 1 : i32
    %59 = arith.extsi %57 : i32 to i64
    %58 = arith.addi %55, %59 : i64
    %60 = arith.constant 1 : i32
    %61 = arith.extsi %60 : i32 to i64
    %56 = func.call @calloc(%58, %61) : (i64, i64) -> !llvm.ptr
    %62 = arith.constant 1 : i32
    %63 = arith.constant 0 : i32
    %64 = arith.trunci %62 : i32 to i8
    %65 = arith.extsi %63 : i32 to i64
    %66 = llvm.getelementptr %56[%65] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %64, %66 : i8, !llvm.ptr
    %67 = arith.constant 1 : i32
    %68 = arith.constant 1 : i32
    %69 = arith.trunci %67 : i32 to i8
    %70 = arith.extsi %68 : i32 to i64
    %71 = llvm.getelementptr %56[%70] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %69, %71 : i8, !llvm.ptr
    %72 = arith.constant 2 : i32
    %73 = arith.extsi %72 : i32 to i64
    %74 = llvm.mlir.constant(1 : i64) : i64
    %75 = llvm.alloca %74 x i64 : (i64) -> !llvm.ptr
    llvm.store %73, %75 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %76 = llvm.load %75 : !llvm.ptr -> i64
    %77 = llvm.load %75 : !llvm.ptr -> i64
    %78 = arith.muli %76, %77 : i64
    %79 = arith.cmpi sle, %78, %55 : i64
    cf.cond_br %79, ^bb13, ^bb14
    ^bb13:
      %81 = llvm.load %75 : !llvm.ptr -> i64
      %82 = llvm.getelementptr %56[%81] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %80 = llvm.load %82 : !llvm.ptr -> i8
      %83 = arith.constant 0 : i32
      %85 = arith.extsi %80 : i8 to i32
      %84 = arith.cmpi eq, %85, %83 : i32
      cf.cond_br %84, ^bb15, ^bb16
      ^bb15:
        %86 = llvm.load %75 : !llvm.ptr -> i64
        %87 = llvm.load %75 : !llvm.ptr -> i64
        %88 = arith.muli %86, %87 : i64
        %89 = llvm.mlir.constant(1 : i64) : i64
        %90 = llvm.alloca %89 x i64 : (i64) -> !llvm.ptr
        llvm.store %88, %90 : i64, !llvm.ptr
        cf.br ^bb18
        ^bb18:
        %91 = llvm.load %90 : !llvm.ptr -> i64
        %92 = arith.cmpi sle, %91, %55 : i64
        cf.cond_br %92, ^bb19, ^bb20
        ^bb19:
          %93 = arith.constant 1 : i32
          %94 = llvm.load %90 : !llvm.ptr -> i64
          %95 = arith.trunci %93 : i32 to i8
          %96 = llvm.getelementptr %56[%94] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %95, %96 : i8, !llvm.ptr
          %97 = llvm.load %90 : !llvm.ptr -> i64
          %98 = llvm.load %75 : !llvm.ptr -> i64
          %99 = arith.addi %97, %98 : i64
          llvm.store %99, %90 : i64, !llvm.ptr
          cf.br ^bb18
        ^bb20:
        cf.br ^bb17
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %100 = llvm.load %75 : !llvm.ptr -> i64
      %101 = arith.constant 1 : i32
      %103 = arith.extsi %101 : i32 to i64
      %102 = arith.addi %100, %103 : i64
      llvm.store %102, %75 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %105 = arith.constant 200 : i32
    %106 = arith.constant 8 : i32
    %107 = arith.extsi %105 : i32 to i64
    %108 = arith.extsi %106 : i32 to i64
    %104 = func.call @calloc(%107, %108) : (i64, i64) -> !llvm.ptr
    %109 = arith.constant 0 : i32
    %110 = arith.extsi %109 : i32 to i64
    %111 = llvm.mlir.constant(1 : i64) : i64
    %112 = llvm.alloca %111 x i64 : (i64) -> !llvm.ptr
    llvm.store %110, %112 : i64, !llvm.ptr
    %113 = arith.constant 11 : i32
    %114 = arith.extsi %113 : i32 to i64
    %115 = llvm.mlir.constant(1 : i64) : i64
    %116 = llvm.alloca %115 x i64 : (i64) -> !llvm.ptr
    llvm.store %114, %116 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %117 = llvm.load %116 : !llvm.ptr -> i64
    %118 = arith.cmpi sle, %117, %55 : i64
    cf.cond_br %118, ^bb22, ^bb23
    ^bb22:
      %120 = llvm.load %116 : !llvm.ptr -> i64
      %121 = llvm.getelementptr %56[%120] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %119 = llvm.load %121 : !llvm.ptr -> i8
      %122 = arith.constant 0 : i32
      %124 = arith.extsi %119 : i8 to i32
      %123 = arith.cmpi eq, %124, %122 : i32
      cf.cond_br %123, ^bb24, ^bb25
      ^bb24:
        %125 = llvm.load %116 : !llvm.ptr -> i64
        %126 = llvm.load %116 : !llvm.ptr -> i64
        %127 = arith.muli %125, %126 : i64
        %128 = func.call @is_palindrome(%127) : (i64) -> i32
        %129 = arith.constant 0 : i32
        %130 = arith.cmpi eq, %128, %129 : i32
        cf.cond_br %130, ^bb27, ^bb28
        ^bb27:
          %131 = func.call @reverse_num(%127) : (i64) -> i64
          %132 = func.call @isqrt(%131) : (i64) -> i64
          %133 = arith.muli %132, %132 : i64
          %134 = arith.cmpi eq, %133, %131 : i64
          %135 = scf.if %134 -> (i1) {
            %136 = arith.cmpi sle, %132, %55 : i64
            scf.yield %136 : i1
          } else {
            %137 = arith.constant false
            scf.yield %137 : i1
          }
          %138 = scf.if %135 -> (i1) {
            %140 = llvm.getelementptr %56[%132] : (!llvm.ptr, i64) -> !llvm.ptr, i8
            %139 = llvm.load %140 : !llvm.ptr -> i8
            %141 = arith.constant 0 : i32
            %143 = arith.extsi %139 : i8 to i32
            %142 = arith.cmpi eq, %143, %141 : i32
            scf.yield %142 : i1
          } else {
            %144 = arith.constant false
            scf.yield %144 : i1
          }
          cf.cond_br %138, ^bb30, ^bb31
          ^bb30:
            %145 = arith.constant 0 : i32
            %146 = llvm.mlir.constant(1 : i64) : i64
            %147 = llvm.alloca %146 x i32 : (i64) -> !llvm.ptr
            llvm.store %145, %147 : i32, !llvm.ptr
            %148 = arith.constant 0 : i32
            %149 = arith.extsi %148 : i32 to i64
            %150 = llvm.mlir.constant(1 : i64) : i64
            %151 = llvm.alloca %150 x i64 : (i64) -> !llvm.ptr
            llvm.store %149, %151 : i64, !llvm.ptr
            cf.br ^bb33
            ^bb33:
            %152 = llvm.load %151 : !llvm.ptr -> i64
            %153 = llvm.load %112 : !llvm.ptr -> i64
            %154 = arith.cmpi slt, %152, %153 : i64
            cf.cond_br %154, ^bb34, ^bb35
            ^bb34:
              %156 = llvm.load %151 : !llvm.ptr -> i64
              %157 = llvm.getelementptr %104[%156] : (!llvm.ptr, i64) -> !llvm.ptr, i64
              %155 = llvm.load %157 : !llvm.ptr -> i64
              %158 = arith.cmpi eq, %155, %127 : i64
              cf.cond_br %158, ^bb36, ^bb37
              ^bb36:
                %159 = arith.constant 1 : i32
                llvm.store %159, %147 : i32, !llvm.ptr
                cf.br ^bb38
              ^bb37:
                cf.br ^bb38
              ^bb38:
              %160 = llvm.load %151 : !llvm.ptr -> i64
              %161 = arith.constant 1 : i32
              %163 = arith.extsi %161 : i32 to i64
              %162 = arith.addi %160, %163 : i64
              llvm.store %162, %151 : i64, !llvm.ptr
              cf.br ^bb33
            ^bb35:
            %164 = llvm.load %147 : !llvm.ptr -> i32
            %165 = arith.constant 0 : i32
            %166 = arith.cmpi eq, %164, %165 : i32
            cf.cond_br %166, ^bb39, ^bb40
            ^bb39:
              %167 = llvm.load %112 : !llvm.ptr -> i64
              %168 = llvm.getelementptr %104[%167] : (!llvm.ptr, i64) -> !llvm.ptr, i64
              llvm.store %127, %168 : i64, !llvm.ptr
              %169 = llvm.load %112 : !llvm.ptr -> i64
              %170 = arith.constant 1 : i32
              %172 = arith.extsi %170 : i32 to i64
              %171 = arith.addi %169, %172 : i64
              llvm.store %171, %112 : i64, !llvm.ptr
              cf.br ^bb41
            ^bb40:
              cf.br ^bb41
            ^bb41:
            %173 = arith.constant 0 : i32
            llvm.store %173, %147 : i32, !llvm.ptr
            %174 = arith.constant 0 : i32
            %175 = arith.extsi %174 : i32 to i64
            llvm.store %175, %151 : i64, !llvm.ptr
            cf.br ^bb42
            ^bb42:
            %176 = llvm.load %151 : !llvm.ptr -> i64
            %177 = llvm.load %112 : !llvm.ptr -> i64
            %178 = arith.cmpi slt, %176, %177 : i64
            cf.cond_br %178, ^bb43, ^bb44
            ^bb43:
              %180 = llvm.load %151 : !llvm.ptr -> i64
              %181 = llvm.getelementptr %104[%180] : (!llvm.ptr, i64) -> !llvm.ptr, i64
              %179 = llvm.load %181 : !llvm.ptr -> i64
              %182 = arith.cmpi eq, %179, %131 : i64
              cf.cond_br %182, ^bb45, ^bb46
              ^bb45:
                %183 = arith.constant 1 : i32
                llvm.store %183, %147 : i32, !llvm.ptr
                cf.br ^bb47
              ^bb46:
                cf.br ^bb47
              ^bb47:
              %184 = llvm.load %151 : !llvm.ptr -> i64
              %185 = arith.constant 1 : i32
              %187 = arith.extsi %185 : i32 to i64
              %186 = arith.addi %184, %187 : i64
              llvm.store %186, %151 : i64, !llvm.ptr
              cf.br ^bb42
            ^bb44:
            %188 = llvm.load %147 : !llvm.ptr -> i32
            %189 = arith.constant 0 : i32
            %190 = arith.cmpi eq, %188, %189 : i32
            cf.cond_br %190, ^bb48, ^bb49
            ^bb48:
              %191 = llvm.load %112 : !llvm.ptr -> i64
              %192 = llvm.getelementptr %104[%191] : (!llvm.ptr, i64) -> !llvm.ptr, i64
              llvm.store %131, %192 : i64, !llvm.ptr
              %193 = llvm.load %112 : !llvm.ptr -> i64
              %194 = arith.constant 1 : i32
              %196 = arith.extsi %194 : i32 to i64
              %195 = arith.addi %193, %196 : i64
              llvm.store %195, %112 : i64, !llvm.ptr
              cf.br ^bb50
            ^bb49:
              cf.br ^bb50
            ^bb50:
            cf.br ^bb32
          ^bb31:
            cf.br ^bb32
          ^bb32:
          cf.br ^bb29
        ^bb28:
          cf.br ^bb29
        ^bb29:
        cf.br ^bb26
      ^bb25:
        cf.br ^bb26
      ^bb26:
      %197 = llvm.load %116 : !llvm.ptr -> i64
      %198 = arith.constant 1 : i32
      %200 = arith.extsi %198 : i32 to i64
      %199 = arith.addi %197, %200 : i64
      llvm.store %199, %116 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %201 = arith.constant 0 : i32
    %202 = arith.extsi %201 : i32 to i64
    %203 = llvm.mlir.constant(1 : i64) : i64
    %204 = llvm.alloca %203 x i64 : (i64) -> !llvm.ptr
    llvm.store %202, %204 : i64, !llvm.ptr
    cf.br ^bb51
    ^bb51:
    %205 = llvm.load %204 : !llvm.ptr -> i64
    %206 = llvm.load %112 : !llvm.ptr -> i64
    %207 = arith.cmpi slt, %205, %206 : i64
    cf.cond_br %207, ^bb52, ^bb53
    ^bb52:
      %208 = llvm.load %204 : !llvm.ptr -> i64
      %209 = arith.constant 1 : i32
      %211 = arith.extsi %209 : i32 to i64
      %210 = arith.addi %208, %211 : i64
      %212 = llvm.mlir.constant(1 : i64) : i64
      %213 = llvm.alloca %212 x i64 : (i64) -> !llvm.ptr
      llvm.store %210, %213 : i64, !llvm.ptr
      cf.br ^bb54
      ^bb54:
      %214 = llvm.load %213 : !llvm.ptr -> i64
      %215 = llvm.load %112 : !llvm.ptr -> i64
      %216 = arith.cmpi slt, %214, %215 : i64
      cf.cond_br %216, ^bb55, ^bb56
      ^bb55:
        %218 = llvm.load %213 : !llvm.ptr -> i64
        %219 = llvm.getelementptr %104[%218] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %217 = llvm.load %219 : !llvm.ptr -> i64
        %221 = llvm.load %204 : !llvm.ptr -> i64
        %222 = llvm.getelementptr %104[%221] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %220 = llvm.load %222 : !llvm.ptr -> i64
        %223 = arith.cmpi slt, %217, %220 : i64
        cf.cond_br %223, ^bb57, ^bb58
        ^bb57:
          %225 = llvm.load %204 : !llvm.ptr -> i64
          %226 = llvm.getelementptr %104[%225] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %224 = llvm.load %226 : !llvm.ptr -> i64
          %228 = llvm.load %213 : !llvm.ptr -> i64
          %229 = llvm.getelementptr %104[%228] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %227 = llvm.load %229 : !llvm.ptr -> i64
          %230 = llvm.load %204 : !llvm.ptr -> i64
          %231 = llvm.getelementptr %104[%230] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %227, %231 : i64, !llvm.ptr
          %232 = llvm.load %213 : !llvm.ptr -> i64
          %233 = llvm.getelementptr %104[%232] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %224, %233 : i64, !llvm.ptr
          cf.br ^bb59
        ^bb58:
          cf.br ^bb59
        ^bb59:
        %234 = llvm.load %213 : !llvm.ptr -> i64
        %235 = arith.constant 1 : i32
        %237 = arith.extsi %235 : i32 to i64
        %236 = arith.addi %234, %237 : i64
        llvm.store %236, %213 : i64, !llvm.ptr
        cf.br ^bb54
      ^bb56:
      %238 = llvm.load %204 : !llvm.ptr -> i64
      %239 = arith.constant 1 : i32
      %241 = arith.extsi %239 : i32 to i64
      %240 = arith.addi %238, %241 : i64
      llvm.store %240, %204 : i64, !llvm.ptr
      cf.br ^bb51
    ^bb53:
    %242 = arith.constant 0 : i32
    %243 = arith.extsi %242 : i32 to i64
    %244 = llvm.mlir.constant(1 : i64) : i64
    %245 = llvm.alloca %244 x i64 : (i64) -> !llvm.ptr
    llvm.store %243, %245 : i64, !llvm.ptr
    %246 = arith.constant 50 : i32
    %247 = arith.extsi %246 : i32 to i64
    %248 = llvm.mlir.constant(1 : i64) : i64
    %249 = llvm.alloca %248 x i64 : (i64) -> !llvm.ptr
    llvm.store %247, %249 : i64, !llvm.ptr
    %250 = llvm.load %112 : !llvm.ptr -> i64
    %251 = llvm.load %249 : !llvm.ptr -> i64
    %252 = arith.cmpi slt, %250, %251 : i64
    cf.cond_br %252, ^bb60, ^bb61
    ^bb60:
      %253 = llvm.load %112 : !llvm.ptr -> i64
      llvm.store %253, %249 : i64, !llvm.ptr
      cf.br ^bb62
    ^bb61:
      cf.br ^bb62
    ^bb62:
    %254 = arith.constant 0 : i32
    %255 = arith.extsi %254 : i32 to i64
    llvm.store %255, %204 : i64, !llvm.ptr
    cf.br ^bb63
    ^bb63:
    %256 = llvm.load %204 : !llvm.ptr -> i64
    %257 = llvm.load %249 : !llvm.ptr -> i64
    %258 = arith.cmpi slt, %256, %257 : i64
    cf.cond_br %258, ^bb64, ^bb65
    ^bb64:
      %259 = llvm.load %245 : !llvm.ptr -> i64
      %261 = llvm.load %204 : !llvm.ptr -> i64
      %262 = llvm.getelementptr %104[%261] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %260 = llvm.load %262 : !llvm.ptr -> i64
      %263 = arith.addi %259, %260 : i64
      llvm.store %263, %245 : i64, !llvm.ptr
      %264 = llvm.load %204 : !llvm.ptr -> i64
      %265 = arith.constant 1 : i32
      %267 = arith.extsi %265 : i32 to i64
      %266 = arith.addi %264, %267 : i64
      llvm.store %266, %204 : i64, !llvm.ptr
      cf.br ^bb63
    ^bb65:
    func.call @free(%56) : (!llvm.ptr) -> ()
    func.call @free(%104) : (!llvm.ptr) -> ()
    %270 = llvm.load %245 : !llvm.ptr -> i64
    func.return %270 : i64
  }
  func.func @main() -> i32 {
    %271 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %272 = func.call @solve() : () -> i64
    %273 = llvm.call @printf(%271, %272) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %274 = arith.constant 0 : i32
    func.return %274 : i32
  }
}