Problem 070

Find n, 1 < n < 10^7, minimizing n/φ(n) among n where φ(n) is a permutation of n. Optimum is a product of two primes near sqrt(limit).

Answer8319823
Output8319823
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n!)
Space complexityO(n)O(n)
ApproachFlow solutionPermutation enumeration or constraint search
VerdictOptimal

Flow source

# Project Euler 070
# Find n, 1 < n < 10^7, minimizing n/φ(n) among n where φ(n) is a permutation of n.
# Optimum is a product of two primes near sqrt(limit).

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

function is_perm(a: i64, b: i64, ca: ptr<i32>) -> bool {
    let mut i: i32 = 0
    while i < 10 {
        ca[i] = 0
        i = i + 1
    }
    let mut x: i64 = a
    let mut y: i64 = b
    while x > 0 {
        ca[(x % 10) as i32] = ca[(x % 10) as i32] + 1
        x = x / 10
    }
    while y > 0 {
        ca[(y % 10) as i32] = ca[(y % 10) as i32] - 1
        y = y / 10
    }
    i = 0
    while i < 10 {
        if ca[i] != 0 { return false }
        i = i + 1
    }
    return true
}

function main() -> i32 {
    # Generate primes up to ~5000 (sqrt(1e7)≈3162, search a bit wider)
    let plim: i64 = 5000
    let sieve: ptr<i8> = calloc(plim, 1)
    let primes: ptr<i64> = calloc(1000, 8)
    if sieve == null || primes == null { return 1 }
    sieve[0] = 1
    sieve[1] = 1
    let mut p: i64 = 2
    while p * p < plim {
        if sieve[p] == 0 {
            let mut m: i64 = p * p
            while m < plim {
                sieve[m] = 1
                m = m + p
            }
        }
        p = p + 1
    }
    let mut pc: i32 = 0
    p = 2
    while p < plim {
        if sieve[p] == 0 {
            primes[pc] = p
            pc = pc + 1
        }
        p = p + 1
    }

    let ca: ptr<i32> = calloc(10, 4)
    if ca == null { return 1 }

    let mut best_n: i64 = 0
    let mut best_phi: i64 = 1
    let limit: i64 = 10000000
    let mut i: i32 = 0
    while i < pc {
        let mut j: i32 = i
        while j < pc {
            let n: i64 = primes[i] * primes[j]
            if n >= limit { break }
            # φ(pq) = (p-1)(q-1) for p!=q; φ(p^2)=(p^2-p)
            let mut ph: i64 = 0
            if i == j {
                ph = primes[i] * (primes[i] - 1)
            } else {
                ph = (primes[i] - 1) * (primes[j] - 1)
            }
            if is_perm(n, ph, ca) {
                if best_n == 0 || n * best_phi < best_n * ph {
                    best_n = n
                    best_phi = ph
                }
            }
            j = j + 1
        }
        i = i + 1
    }
    printf("%lld\n", best_n)
    free(ca)
    free(primes)
    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; }

bool is_perm_i64_i64_ptr_i32(int64_t a, int64_t b, int32_t* ca);
int32_t main(void);



bool is_perm_i64_i64_ptr_i32(int64_t a, int64_t b, int32_t* ca) {
    int32_t i = 0;
    while (i < 10) {
        ca[i] = 0;
        i = (i + 1);
    }
    int64_t x = a;
    int64_t y = b;
    while (x > 0) {
        ca[((int32_t)(FLOW_CHECKED_MOD((x), (10))))] = (ca[((int32_t)(FLOW_CHECKED_MOD((x), (10))))] + 1);
        x = FLOW_CHECKED_DIV((x), (10));
    }
    while (y > 0) {
        ca[((int32_t)(FLOW_CHECKED_MOD((y), (10))))] = (ca[((int32_t)(FLOW_CHECKED_MOD((y), (10))))] - 1);
        y = FLOW_CHECKED_DIV((y), (10));
    }
    i = 0;
    while (i < 10) {
        if (ca[i] != 0) {
            return 0;
        }
        i = (i + 1);
    }
    return 1;
}

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