Problem 043

Sum of 0–9 pandigital numbers with substring divisibility properties.

Answer16695334890
Output16695334890
StatusPASS
Native helperno
Runtime10 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n!)
Space complexityO(1)O(n)
ApproachFlow solutionPandigital substring divisibility
VerdictOptimal

Flow source

# Project Euler 043
# Sum of 0–9 pandigital numbers with substring divisibility properties.

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

function swap(a: ptr<i32>, i: i32, j: i32) -> void {
    let t: i32 = a[i]
    a[i] = a[j]
    a[j] = t
}

function next_perm(a: ptr<i32>, n: i32) -> bool {
    let mut i: i32 = n - 2
    while i >= 0 && a[i] >= a[i + 1] { i = i - 1 }
    if i < 0 { return false }
    let mut j: i32 = n - 1
    while a[j] <= a[i] { j = j - 1 }
    swap(a, i, j)
    let mut l: i32 = i + 1
    let mut r: i32 = n - 1
    while l < r {
        swap(a, l, r)
        l = l + 1
        r = r - 1
    }
    return true
}

function sub3(a: ptr<i32>, i: i32) -> i32 {
    return a[i] * 100 + a[i + 1] * 10 + a[i + 2]
}

function to_num(a: ptr<i32>) -> i64 {
    let mut v: i64 = 0
    for i in 0..10 {
        v = v * 10 + (a[i] as i64)
    }
    return v
}

function main() -> i32 {
    let d: ptr<i32> = calloc(10, 4)
    if d == null { return 1 }
    for i in 0..10 {
        d[i] = i
    }

    let primes: array<i32, 7> = [2, 3, 5, 7, 11, 13, 17]
    let mut total: i64 = 0
    while true {
        let mut ok: bool = true
        let mut k: i32 = 0
        while k < 7 {
            if sub3(d, k + 1) % primes[k] != 0 {
                ok = false
                break
            }
            k = k + 1
        }
        if ok {
            total = total + to_num(d)
        }
        if !next_perm(d, 10) { break }
    }
    printf("%lld\n", total)
    free(d)
    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 swap_ptr_i32_i32_i32(int32_t* a, int32_t i, int32_t j);
bool next_perm_ptr_i32_i32(int32_t* a, int32_t n);
int32_t sub3_ptr_i32_i32(int32_t* a, int32_t i);
int64_t to_num_ptr_i32(int32_t* a);
int32_t main(void);



void swap_ptr_i32_i32_i32(int32_t* a, int32_t i, int32_t j) {
    int32_t t = a[i];
    a[i] = a[j];
    a[j] = t;
}

bool next_perm_ptr_i32_i32(int32_t* a, int32_t n) {
    int32_t i = (n - 2);
    while ((i >= 0 && a[i] >= a[(i + 1)])) {
        i = (i - 1);
    }
    if (i < 0) {
        return 0;
    }
    int32_t j = (n - 1);
    while (a[j] <= a[i]) {
        j = (j - 1);
    }
    swap_ptr_i32_i32_i32(a, i, j);
    int32_t l = (i + 1);
    int32_t r = (n - 1);
    while (l < r) {
        swap_ptr_i32_i32_i32(a, l, r);
        l = (l + 1);
        r = (r - 1);
    }
    return 1;
}

int32_t sub3_ptr_i32_i32(int32_t* a, int32_t i) {
    return (((a[i] * 100) + (a[(i + 1)] * 10)) + a[(i + 2)]);
}

int64_t to_num_ptr_i32(int32_t* a) {
    int64_t v = 0;
    int32_t __flow_step_1 = 1;
    for (int32_t i = 0; (0 <= 10) ? i < 10 : i > 10; i += (0 <= 10) ? 1 : -1) {
        v = ((v * 10) + ((int64_t)(a[i])));
    }
    return v;
}

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