Problem 068

Magic 5-gon ring: maximum 16-digit magic string.

Answer6531031914842725
Output6531031914842725
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(1)
ApproachFlow solutionDirect hand evaluation or enumeration
VerdictSuboptimal

Flow source

# Project Euler 068
# Magic 5-gon ring: maximum 16-digit magic string.

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 append_num(v: i64, x: i32) -> i64 {
    if x >= 10 {
        return v * 100 + (x as i64)
    }
    return v * 10 + (x as i64)
}

function main() -> i32 {
    let nums: ptr<i32> = calloc(10, 4)
    let outer: ptr<i32> = calloc(5, 4)
    let inner: ptr<i32> = calloc(5, 4)
    if nums == null || outer == null || inner == null { return 1 }
    let mut i: i32 = 0
    for i in 0..10 {
        nums[i] = i + 1
    }

    let mut best: i64 = 0
    while true {
        for i in 0..5 {
            outer[i] = nums[i]
            inner[i] = nums[i + 5]
        }
        let mut has10: bool = false
        for i in 0..5 {
            if outer[i] == 10 { has10 = true }
        }
        if has10 {
            let s0: i32 = outer[0] + inner[0] + inner[1]
            let mut magic: bool = true
            i = 1
            while i < 5 {
                let nidx: i32 = i + 1
                if nidx == 5 { nidx = 0 }
                if outer[i] + inner[i] + inner[nidx] != s0 {
                    magic = false
                    break
                }
                i = i + 1
            }
            if magic {
                let mut start: i32 = 0
                for i in 1..5 {
                    if outer[i] < outer[start] { start = i }
                }
                let mut v: i64 = 0
                let mut digs: i32 = 0
                for i in 0..5 {
                    let mut idx: i32 = start + i
                    if idx >= 5 { idx = idx - 5 }
                    let a: i32 = outer[idx]
                    let b: i32 = inner[idx]
                    let mut cidx: i32 = idx + 1
                    if cidx >= 5 { cidx = 0 }
                    let c: i32 = inner[cidx]
                    if a >= 10 { digs = digs + 2 } else { digs = digs + 1 }
                    digs = digs + 2
                    v = append_num(v, a)
                    v = append_num(v, b)
                    v = append_num(v, c)
                }
                if digs == 16 && v > best {
                    best = v
                }
            }
        }
        if !next_perm(nums, 10) { break }
    }
    printf("%lld\n", best)
    free(inner)
    free(outer)
    free(nums)
    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);
int64_t append_num_i64_i32(int64_t v, int32_t x);
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;
}

int64_t append_num_i64_i32(int64_t v, int32_t x) {
    if (x >= 10) {
        return ((v * 100) + ((int64_t)(x)));
    }
    return ((v * 10) + ((int64_t)(x)));
}

int32_t main(void) {
    int32_t* nums = (int32_t*)(calloc(10, 4));
    int32_t* outer = (int32_t*)(calloc(5, 4));
    int32_t* inner = (int32_t*)(calloc(5, 4));
    if (((nums == NULL || outer == NULL) || inner == NULL)) {
        return 1;
    }
    int32_t i = 0;
    int32_t __flow_step_1 = 1;
    for (int32_t i = 0; (0 <= 10) ? i < 10 : i > 10; i += (0 <= 10) ? 1 : -1) {
        nums[i] = (i + 1);
    }
    int64_t best = 0;
    while (1) {
        int32_t __flow_step_2 = 1;
        for (int32_t i = 0; (0 <= 5) ? i < 5 : i > 5; i += (0 <= 5) ? 1 : -1) {
            outer[i] = nums[i];
            inner[i] = nums[(i + 5)];
        }
        bool has10 = 0;
        int32_t __flow_step_3 = 1;
        for (int32_t i = 0; (0 <= 5) ? i < 5 : i > 5; i += (0 <= 5) ? 1 : -1) {
            if (outer[i] == 10) {
                has10 = 1;
            }
        }
        if (has10) {
            int32_t s0 = ((outer[0] + inner[0]) + inner[1]);
            bool magic = 1;
            i = 1;
            while (i < 5) {
                int32_t nidx = (i + 1);
                if (nidx == 5) {
                    nidx = 0;
                }
                if (((outer[i] + inner[i]) + inner[nidx]) != s0) {
                    magic = 0;
                    break;
                }
                i = (i + 1);
            }
            if (magic) {
                int32_t start = 0;
                int32_t __flow_step_4 = 1;
                for (int32_t i = 1; (1 <= 5) ? i < 5 : i > 5; i += (1 <= 5) ? 1 : -1) {
                    if (outer[i] < outer[start]) {
                        start = i;
                    }
                }
                int64_t v = 0;
                int32_t digs = 0;
                int32_t __flow_step_5 = 1;
                for (int32_t i = 0; (0 <= 5) ? i < 5 : i > 5; i += (0 <= 5) ? 1 : -1) {
                    int32_t idx = (start + i);
                    if (idx >= 5) {
                        idx = (idx - 5);
                    }
                    int32_t a = outer[idx];
                    int32_t b = inner[idx];
                    int32_t cidx = (idx + 1);
                    if (cidx >= 5) {
                        cidx = 0;
                    }
                    int32_t c = inner[cidx];
                    if (a >= 10) {
                        digs = (digs + 2);
                    } else {
                        digs = (digs + 1);
                    }
                    digs = (digs + 2);
                    v = append_num_i64_i32(v, a);
                    v = append_num_i64_i32(v, b);
                    v = append_num_i64_i32(v, c);
                }
                if ((digs == 16 && v > best)) {
                    best = v;
                }
            }
        }
        if ((!(next_perm_ptr_i32_i32(nums, 10)))) {
            break;
        }
    }
    printf("%lld\n", best);
    free(inner);
    free(outer);
    free(nums);
    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 @append_num(%arg0: i64, %arg1: i32) -> i64 {
    %78 = arith.constant 10 : i32
    %79 = arith.cmpi sge, %arg1, %78 : i32
    cf.cond_br %79, ^bb12, ^bb13
    ^bb12:
      %80 = arith.constant 100 : i32
      %82 = arith.extsi %80 : i32 to i64
      %81 = arith.muli %arg0, %82 : i64
      %83 = arith.extsi %arg1 : i32 to i64
      %84 = arith.addi %81, %83 : i64
      func.return %84 : i64
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %85 = arith.constant 10 : i32
    %87 = arith.extsi %85 : i32 to i64
    %86 = arith.muli %arg0, %87 : i64
    %88 = arith.extsi %arg1 : i32 to i64
    %89 = arith.addi %86, %88 : i64
    func.return %89 : i64
  }
  func.func @main() -> i32 {
    %91 = arith.constant 10 : i32
    %92 = arith.constant 4 : i32
    %93 = arith.extsi %91 : i32 to i64
    %94 = arith.extsi %92 : i32 to i64
    %90 = func.call @calloc(%93, %94) : (i64, i64) -> !llvm.ptr
    %96 = arith.constant 5 : i32
    %97 = arith.constant 4 : i32
    %98 = arith.extsi %96 : i32 to i64
    %99 = arith.extsi %97 : i32 to i64
    %95 = func.call @calloc(%98, %99) : (i64, i64) -> !llvm.ptr
    %101 = arith.constant 5 : i32
    %102 = arith.constant 4 : i32
    %103 = arith.extsi %101 : i32 to i64
    %104 = arith.extsi %102 : i32 to i64
    %100 = func.call @calloc(%103, %104) : (i64, i64) -> !llvm.ptr
    %105 = llvm.mlir.zero : !llvm.ptr
    %106 = llvm.icmp "eq" %90, %105 : !llvm.ptr
    %107 = scf.if %106 -> (i1) {
      %108 = arith.constant true
      scf.yield %108 : i1
    } else {
      %109 = llvm.mlir.zero : !llvm.ptr
      %110 = llvm.icmp "eq" %95, %109 : !llvm.ptr
      scf.yield %110 : i1
    }
    %111 = scf.if %107 -> (i1) {
      %112 = arith.constant true
      scf.yield %112 : i1
    } else {
      %113 = llvm.mlir.zero : !llvm.ptr
      %114 = llvm.icmp "eq" %100, %113 : !llvm.ptr
      scf.yield %114 : i1
    }
    cf.cond_br %111, ^bb15, ^bb16
    ^bb15:
      %115 = arith.constant 1 : i32
      func.return %115 : i32
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %116 = arith.constant 0 : i32
    %117 = llvm.mlir.constant(1 : i64) : i64
    %118 = llvm.alloca %117 x i32 : (i64) -> !llvm.ptr
    llvm.store %116, %118 : i32, !llvm.ptr
    %119 = arith.constant 0 : i32
    %120 = arith.constant 10 : i32
    %121 = arith.index_cast %119 : i32 to index
    %122 = arith.index_cast %120 : i32 to index
    %124 = arith.constant 1 : index
    %125 = arith.constant -1 : index
    %126 = arith.cmpi sle, %121, %122 : index
    %123 = arith.select %126, %124, %125 : index
    cf.br ^bb18(%121 : index)
    ^bb18(%127: index):
    %128 = arith.cmpi slt, %127, %122 : index
    %129 = arith.cmpi sgt, %127, %122 : index
    %130 = arith.select %126, %128, %129 : i1
    cf.cond_br %130, ^bb19(%127 : index), ^bb20(%127 : index)
    ^bb19(%131: index):
      %132 = arith.constant 1 : i32
      %134 = arith.index_cast %131 : index to i32
      %133 = arith.addi %134, %132 : i32
      %135 = arith.index_cast %131 : index to i64
      %136 = llvm.getelementptr %90[%135] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %133, %136 : i32, !llvm.ptr
      %137 = arith.addi %131, %123 : index
      cf.br ^bb18(%137 : index)
    ^bb20(%138: index):
    %139 = arith.constant 0 : i32
    %140 = arith.extsi %139 : i32 to i64
    %141 = llvm.mlir.constant(1 : i64) : i64
    %142 = llvm.alloca %141 x i64 : (i64) -> !llvm.ptr
    llvm.store %140, %142 : i64, !llvm.ptr
    cf.br ^bb21(%131 : index)
    ^bb21(%143: index):
    %144 = arith.constant 1 : i1
    cf.cond_br %144, ^bb22(%143 : index), ^bb23(%143 : index)
    ^bb22(%145: index):
      %146 = arith.constant 0 : i32
      %147 = arith.constant 5 : i32
      %148 = arith.index_cast %146 : i32 to index
      %149 = arith.index_cast %147 : i32 to index
      %151 = arith.constant 1 : index
      %152 = arith.constant -1 : index
      %153 = arith.cmpi sle, %148, %149 : index
      %150 = arith.select %153, %151, %152 : index
      cf.br ^bb24(%148 : index)
      ^bb24(%154: index):
      %155 = arith.cmpi slt, %154, %149 : index
      %156 = arith.cmpi sgt, %154, %149 : index
      %157 = arith.select %153, %155, %156 : i1
      cf.cond_br %157, ^bb25(%154 : index), ^bb26(%154 : index)
      ^bb25(%158: index):
        %160 = arith.index_cast %158 : index to i64
        %161 = llvm.getelementptr %90[%160] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %159 = llvm.load %161 : !llvm.ptr -> i32
        %162 = arith.index_cast %158 : index to i64
        %163 = llvm.getelementptr %95[%162] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        llvm.store %159, %163 : i32, !llvm.ptr
        %165 = arith.constant 5 : i32
        %167 = arith.index_cast %158 : index to i32
        %166 = arith.addi %167, %165 : i32
        %168 = arith.extsi %166 : i32 to i64
        %169 = llvm.getelementptr %90[%168] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %164 = llvm.load %169 : !llvm.ptr -> i32
        %170 = arith.index_cast %158 : index to i64
        %171 = llvm.getelementptr %100[%170] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        llvm.store %164, %171 : i32, !llvm.ptr
        %172 = arith.addi %158, %150 : index
        cf.br ^bb24(%172 : index)
      ^bb26(%173: index):
      %174 = arith.constant 0 : i1
      %175 = llvm.mlir.constant(1 : i64) : i64
      %176 = llvm.alloca %175 x i1 : (i64) -> !llvm.ptr
      llvm.store %174, %176 : i1, !llvm.ptr
      %177 = arith.constant 0 : i32
      %178 = arith.constant 5 : i32
      %179 = arith.index_cast %177 : i32 to index
      %180 = arith.index_cast %178 : i32 to index
      %182 = arith.constant 1 : index
      %183 = arith.constant -1 : index
      %184 = arith.cmpi sle, %179, %180 : index
      %181 = arith.select %184, %182, %183 : index
      cf.br ^bb27(%179 : index)
      ^bb27(%185: index):
      %186 = arith.cmpi slt, %185, %180 : index
      %187 = arith.cmpi sgt, %185, %180 : index
      %188 = arith.select %184, %186, %187 : i1
      cf.cond_br %188, ^bb28(%185 : index), ^bb29(%185 : index)
      ^bb28(%189: index):
        %191 = arith.index_cast %189 : index to i64
        %192 = llvm.getelementptr %95[%191] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %190 = llvm.load %192 : !llvm.ptr -> i32
        %193 = arith.constant 10 : i32
        %194 = arith.cmpi eq, %190, %193 : i32
        cf.cond_br %194, ^bb30, ^bb31
        ^bb30:
          %195 = arith.constant 1 : i1
          llvm.store %195, %176 : i1, !llvm.ptr
          cf.br ^bb32
        ^bb31:
          cf.br ^bb32
        ^bb32:
        %196 = arith.addi %189, %181 : index
        cf.br ^bb27(%196 : index)
      ^bb29(%197: index):
      %198 = llvm.load %176 : !llvm.ptr -> i1
      cf.cond_br %198, ^bb33, ^bb34
      ^bb33:
        %200 = arith.constant 0 : i32
        %201 = arith.extsi %200 : i32 to i64
        %202 = llvm.getelementptr %95[%201] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %199 = llvm.load %202 : !llvm.ptr -> i32
        %204 = arith.constant 0 : i32
        %205 = arith.extsi %204 : i32 to i64
        %206 = llvm.getelementptr %100[%205] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %203 = llvm.load %206 : !llvm.ptr -> i32
        %207 = arith.addi %199, %203 : i32
        %209 = arith.constant 1 : i32
        %210 = arith.extsi %209 : i32 to i64
        %211 = llvm.getelementptr %100[%210] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %208 = llvm.load %211 : !llvm.ptr -> i32
        %212 = arith.addi %207, %208 : i32
        %213 = arith.constant 1 : i1
        %214 = llvm.mlir.constant(1 : i64) : i64
        %215 = llvm.alloca %214 x i1 : (i64) -> !llvm.ptr
        llvm.store %213, %215 : i1, !llvm.ptr
        %216 = arith.constant 1 : i32
        %217 = arith.index_cast %216 : i32 to index
        cf.br ^bb36(%217 : index)
        ^bb36(%218: index):
        %219 = arith.constant 5 : i32
        %221 = arith.index_cast %218 : index to i32
        %220 = arith.cmpi slt, %221, %219 : i32
        cf.cond_br %220, ^bb37(%218 : index), ^bb38(%218 : index)
        ^bb37(%222: index):
          %223 = arith.constant 1 : i32
          %225 = arith.index_cast %222 : index to i32
          %224 = arith.addi %225, %223 : i32
          %226 = arith.constant 5 : i32
          %227 = arith.cmpi eq, %224, %226 : i32
          %228 = scf.if %227 -> (i32) {
            %229 = arith.constant 0 : i32
            scf.yield %229 : i32
          } else {
            scf.yield %224 : i32
          }
          %231 = arith.index_cast %222 : index to i64
          %232 = llvm.getelementptr %95[%231] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %230 = llvm.load %232 : !llvm.ptr -> i32
          %234 = arith.index_cast %222 : index to i64
          %235 = llvm.getelementptr %100[%234] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %233 = llvm.load %235 : !llvm.ptr -> i32
          %236 = arith.addi %230, %233 : i32
          %238 = arith.extsi %228 : i32 to i64
          %239 = llvm.getelementptr %100[%238] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %237 = llvm.load %239 : !llvm.ptr -> i32
          %240 = arith.addi %236, %237 : i32
          %241 = arith.cmpi ne, %240, %212 : i32
          cf.cond_br %241, ^bb39, ^bb40
          ^bb39:
            %242 = arith.constant 0 : i1
            llvm.store %242, %215 : i1, !llvm.ptr
            cf.br ^bb38(%222 : index)
          ^bb40:
            cf.br ^bb41
          ^bb41:
          %243 = arith.constant 1 : i32
          %245 = arith.index_cast %222 : index to i32
          %244 = arith.addi %245, %243 : i32
          %246 = arith.index_cast %244 : i32 to index
          cf.br ^bb36(%246 : index)
        ^bb38(%247: index):
        %248 = llvm.load %215 : !llvm.ptr -> i1
        cf.cond_br %248, ^bb42, ^bb43
        ^bb42:
          %249 = arith.constant 0 : i32
          %250 = llvm.mlir.constant(1 : i64) : i64
          %251 = llvm.alloca %250 x i32 : (i64) -> !llvm.ptr
          llvm.store %249, %251 : i32, !llvm.ptr
          %252 = arith.constant 1 : i32
          %253 = arith.constant 5 : i32
          %254 = arith.index_cast %252 : i32 to index
          %255 = arith.index_cast %253 : i32 to index
          %257 = arith.constant 1 : index
          %258 = arith.constant -1 : index
          %259 = arith.cmpi sle, %254, %255 : index
          %256 = arith.select %259, %257, %258 : index
          cf.br ^bb45(%254 : index)
          ^bb45(%260: index):
          %261 = arith.cmpi slt, %260, %255 : index
          %262 = arith.cmpi sgt, %260, %255 : index
          %263 = arith.select %259, %261, %262 : i1
          cf.cond_br %263, ^bb46(%260 : index), ^bb47(%260 : index)
          ^bb46(%264: index):
            %266 = arith.index_cast %264 : index to i64
            %267 = llvm.getelementptr %95[%266] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            %265 = llvm.load %267 : !llvm.ptr -> i32
            %269 = llvm.load %251 : !llvm.ptr -> i32
            %270 = arith.extsi %269 : i32 to i64
            %271 = llvm.getelementptr %95[%270] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            %268 = llvm.load %271 : !llvm.ptr -> i32
            %272 = arith.cmpi slt, %265, %268 : i32
            cf.cond_br %272, ^bb48, ^bb49
            ^bb48:
              %273 = arith.index_cast %264 : index to i32
              llvm.store %273, %251 : i32, !llvm.ptr
              cf.br ^bb50
            ^bb49:
              cf.br ^bb50
            ^bb50:
            %274 = arith.addi %264, %256 : index
            cf.br ^bb45(%274 : index)
          ^bb47(%275: index):
          %276 = arith.constant 0 : i32
          %277 = arith.extsi %276 : i32 to i64
          %278 = llvm.mlir.constant(1 : i64) : i64
          %279 = llvm.alloca %278 x i64 : (i64) -> !llvm.ptr
          llvm.store %277, %279 : i64, !llvm.ptr
          %280 = arith.constant 0 : i32
          %281 = llvm.mlir.constant(1 : i64) : i64
          %282 = llvm.alloca %281 x i32 : (i64) -> !llvm.ptr
          llvm.store %280, %282 : i32, !llvm.ptr
          %283 = arith.constant 0 : i32
          %284 = arith.constant 5 : i32
          %285 = arith.index_cast %283 : i32 to index
          %286 = arith.index_cast %284 : i32 to index
          %288 = arith.constant 1 : index
          %289 = arith.constant -1 : index
          %290 = arith.cmpi sle, %285, %286 : index
          %287 = arith.select %290, %288, %289 : index
          cf.br ^bb51(%285 : index)
          ^bb51(%291: index):
          %292 = arith.cmpi slt, %291, %286 : index
          %293 = arith.cmpi sgt, %291, %286 : index
          %294 = arith.select %290, %292, %293 : i1
          cf.cond_br %294, ^bb52(%291 : index), ^bb53(%291 : index)
          ^bb52(%295: index):
            %296 = llvm.load %251 : !llvm.ptr -> i32
            %298 = arith.index_cast %295 : index to i32
            %297 = arith.addi %296, %298 : i32
            %299 = llvm.mlir.constant(1 : i64) : i64
            %300 = llvm.alloca %299 x i32 : (i64) -> !llvm.ptr
            llvm.store %297, %300 : i32, !llvm.ptr
            %301 = llvm.load %300 : !llvm.ptr -> i32
            %302 = arith.constant 5 : i32
            %303 = arith.cmpi sge, %301, %302 : i32
            cf.cond_br %303, ^bb54, ^bb55
            ^bb54:
              %304 = llvm.load %300 : !llvm.ptr -> i32
              %305 = arith.constant 5 : i32
              %306 = arith.subi %304, %305 : i32
              llvm.store %306, %300 : i32, !llvm.ptr
              cf.br ^bb56
            ^bb55:
              cf.br ^bb56
            ^bb56:
            %308 = llvm.load %300 : !llvm.ptr -> i32
            %309 = arith.extsi %308 : i32 to i64
            %310 = llvm.getelementptr %95[%309] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            %307 = llvm.load %310 : !llvm.ptr -> i32
            %312 = llvm.load %300 : !llvm.ptr -> i32
            %313 = arith.extsi %312 : i32 to i64
            %314 = llvm.getelementptr %100[%313] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            %311 = llvm.load %314 : !llvm.ptr -> i32
            %315 = llvm.load %300 : !llvm.ptr -> i32
            %316 = arith.constant 1 : i32
            %317 = arith.addi %315, %316 : i32
            %318 = llvm.mlir.constant(1 : i64) : i64
            %319 = llvm.alloca %318 x i32 : (i64) -> !llvm.ptr
            llvm.store %317, %319 : i32, !llvm.ptr
            %320 = llvm.load %319 : !llvm.ptr -> i32
            %321 = arith.constant 5 : i32
            %322 = arith.cmpi sge, %320, %321 : i32
            cf.cond_br %322, ^bb57, ^bb58
            ^bb57:
              %323 = arith.constant 0 : i32
              llvm.store %323, %319 : i32, !llvm.ptr
              cf.br ^bb59
            ^bb58:
              cf.br ^bb59
            ^bb59:
            %325 = llvm.load %319 : !llvm.ptr -> i32
            %326 = arith.extsi %325 : i32 to i64
            %327 = llvm.getelementptr %100[%326] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            %324 = llvm.load %327 : !llvm.ptr -> i32
            %328 = arith.constant 10 : i32
            %329 = arith.cmpi sge, %307, %328 : i32
            cf.cond_br %329, ^bb60, ^bb61
            ^bb60:
              %330 = llvm.load %282 : !llvm.ptr -> i32
              %331 = arith.constant 2 : i32
              %332 = arith.addi %330, %331 : i32
              llvm.store %332, %282 : i32, !llvm.ptr
              cf.br ^bb62
            ^bb61:
              %333 = llvm.load %282 : !llvm.ptr -> i32
              %334 = arith.constant 1 : i32
              %335 = arith.addi %333, %334 : i32
              llvm.store %335, %282 : i32, !llvm.ptr
              cf.br ^bb62
            ^bb62:
            %336 = llvm.load %282 : !llvm.ptr -> i32
            %337 = arith.constant 2 : i32
            %338 = arith.addi %336, %337 : i32
            llvm.store %338, %282 : i32, !llvm.ptr
            %340 = llvm.load %279 : !llvm.ptr -> i64
            %339 = func.call @append_num(%340, %307) : (i64, i32) -> i64
            llvm.store %339, %279 : i64, !llvm.ptr
            %342 = llvm.load %279 : !llvm.ptr -> i64
            %341 = func.call @append_num(%342, %311) : (i64, i32) -> i64
            llvm.store %341, %279 : i64, !llvm.ptr
            %344 = llvm.load %279 : !llvm.ptr -> i64
            %343 = func.call @append_num(%344, %324) : (i64, i32) -> i64
            llvm.store %343, %279 : i64, !llvm.ptr
            %345 = arith.addi %295, %287 : index
            cf.br ^bb51(%345 : index)
          ^bb53(%346: index):
          %347 = llvm.load %282 : !llvm.ptr -> i32
          %348 = arith.constant 16 : i32
          %349 = arith.cmpi eq, %347, %348 : i32
          %350 = scf.if %349 -> (i1) {
            %351 = llvm.load %279 : !llvm.ptr -> i64
            %352 = llvm.load %142 : !llvm.ptr -> i64
            %353 = arith.cmpi sgt, %351, %352 : i64
            scf.yield %353 : i1
          } else {
            %354 = arith.constant false
            scf.yield %354 : i1
          }
          cf.cond_br %350, ^bb63, ^bb64
          ^bb63:
            %355 = llvm.load %279 : !llvm.ptr -> i64
            llvm.store %355, %142 : i64, !llvm.ptr
            cf.br ^bb65
          ^bb64:
            cf.br ^bb65
          ^bb65:
          cf.br ^bb44
        ^bb43:
          cf.br ^bb44
        ^bb44:
        cf.br ^bb35(%295 : index)
      ^bb34:
        cf.br ^bb35(%189 : index)
      ^bb35(%356: index):
      %358 = arith.constant 10 : i32
      %357 = func.call @next_perm(%90, %358) : (!llvm.ptr, i32) -> i1
      %360 = arith.constant 1 : i1
      %359 = arith.xori %357, %360 : i1
      cf.cond_br %359, ^bb66, ^bb67
      ^bb66:
        cf.br ^bb23(%356 : index)
      ^bb67:
        cf.br ^bb68
      ^bb68:
      cf.br ^bb21(%356 : index)
    ^bb23(%362: index):
    %363 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %364 = llvm.load %142 : !llvm.ptr -> i64
    %365 = llvm.call @printf(%363, %364) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%100) : (!llvm.ptr) -> ()
    func.call @free(%95) : (!llvm.ptr) -> ()
    func.call @free(%90) : (!llvm.ptr) -> ()
    %369 = arith.constant 0 : i32
    func.return %369 : i32
  }
}