Problem 051

Smallest prime in an eight prime family by digit replacement.

Answer121313
Output121313
StatusPASS
Native helperno
Runtime0 ms
Peak memory2096 KB
Time complexityO(n^3) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n log log n)
Space complexityO(n)O(n)
ApproachFlow solutionSieve of Eratosthenes
VerdictSuboptimal

Flow source

# Project Euler 051
# Smallest prime in an eight prime family by digit replacement.

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

struct DigitSlot {
    dig: i32,
    rep: i32
}

function pow10(n: i32) -> i64 {
    let mut r: i64 = 1
    let mut i: i32 = 0
    while i < n {
        r = r * 10
        i = i + 1
    }
    return r
}

function main() -> i32 {
    let limit: i64 = 1000000
    let sieve: ptr<i8> = calloc(limit, 1)
    if sieve == null { return 1 }
    sieve[0] = 1
    sieve[1] = 1
    let mut p: i64 = 2
    while p * p < limit {
        if sieve[p] == 0 {
            let mut m: i64 = p * p
            while m < limit {
                sieve[m] = 1
                m = m + p
            }
        }
        p = p + 1
    }

    let slots: ptr<DigitSlot> = calloc(10, 8) as ptr<DigitSlot>
    if slots == null { return 1 }

    let mut ans: i64 = 0
    let mut n: i64 = 10
    while n < limit {
        if sieve[n] == 0 {
            let mut x: i64 = n
            let mut nd: i32 = 0
            while x > 0 {
                slots[nd].dig = (x % 10) as i32
                x = x / 10
                nd = nd + 1
            }
            # slots[0].dig = LSD; slots[nd-1].dig = MSD
            let masks: i32 = (1 << nd) - 1
            let mut mask: i32 = 1
            while mask <= masks {
                # require replaced digits equal
                let mut same: i32 = -1
                let mut ok_mask: bool = true
                let mut i: i32 = 0
                while i < nd {
                    if (mask & (1 << i)) != 0 {
                        if same < 0 {
                            same = slots[i].dig
                        } elif slots[i].dig != same {
                            ok_mask = false
                            break
                        }
                    }
                    i = i + 1
                }
                if ok_mask {
                    let mut count: i32 = 0
                    let mut first: i64 = 0
                    let mut d: i32 = 0
                    while d <= 9 {
                        # build number with replaced digits = d
                        let mut v: i64 = 0
                        let mut place: i64 = 1
                        let mut lead_zero: bool = false
                        i = 0
                        while i < nd {
                            let mut digit: i32 = slots[i].dig
                            if (mask & (1 << i)) != 0 {
                                digit = d
                            }
                            if i == nd - 1 && digit == 0 {
                                lead_zero = true
                            }
                            v = v + (digit as i64) * place
                            place = place * 10
                            i = i + 1
                        }
                        if !lead_zero && v < limit && sieve[v] == 0 {
                            count = count + 1
                            if first == 0 {
                                first = v
                            }
                        }
                        d = d + 1
                    }
                    if count >= 8 {
                        if ans == 0 || first < ans {
                            ans = first
                        }
                    }
                }
                mask = mask + 1
            }
            if ans != 0 && n > ans {
                break
            }
        }
        n = n + 1
    }

    printf("%lld\n", ans)
    free(slots)
    free(sieve)
    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; }

typedef struct DigitSlot DigitSlot;

struct DigitSlot {
    int32_t dig;
    int32_t rep;
};

int64_t pow10_i32(int32_t n);
int32_t main(void);



int64_t pow10_i32(int32_t n) {
    int64_t r = 1;
    int32_t i = 0;
    while (i < n) {
        r = (r * 10);
        i = (i + 1);
    }
    return r;
}

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