Problem 599

Distinct Colourings of a Rubik's Cube (2x2x2) Burnside on corner wreath product: |G|=8!*3^7, n=10 colours.

Answer12395526079546335
Output12395526079546335
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)?
Space complexityO(1)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 599
# Distinct Colourings of a Rubik's Cube (2x2x2)
# Burnside on corner wreath product: |G|=8!*3^7, n=10 colours.

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

function ipow128(base: i64, exp0: i64) -> i128 {
    let mut r: i128 = 1
    let mut e: i64 = exp0
    let mut b: i128 = base as i128
    while e > 0 {
        if (e & 1) != 0 { r = r * b }
        b = b * b
        e = e >> 1
    }
    return r
}

function comb(n: i64, k0: i64) -> i64 {
    if k0 < 0 || k0 > n { return 0 }
    let mut k: i64 = k0
    if k > n - k { k = n - k }
    let mut r: i64 = 1
    let mut i: i64 = 1
    while i <= k {
        r = r * (n - k + i) / i
        i = i + 1
    }
    return r
}

function distinct_colourings(n: i64) -> i64 {
    let dp: ptr<i64> = calloc(81, 8)
    if dp == null { return -1 }
    dp[0] = 1
    let mut i: i64 = 1
    while i <= 8 {
        let mut k: i64 = 1
        while k <= i {
            dp[i * 9 + k] = dp[(i - 1) * 9 + (k - 1)] + (i - 1) * dp[(i - 1) * 9 + k]
            k = k + 1
        }
        i = i + 1
    }

    let B: ptr<i64> = calloc(9, 8)
    let dpr: ptr<i64> = calloc(27, 8)
    if B == null || dpr == null { free(dp); return -1 }
    dpr[0] = 1
    let mut r: i64 = 0
    while r < 8 {
        let mut s: i64 = 0
        while s < 3 {
            dpr[(r + 1) * 3 + ((s + 1) % 3)] = dpr[(r + 1) * 3 + ((s + 1) % 3)] + dpr[r * 3 + s]
            dpr[(r + 1) * 3 + ((s + 2) % 3)] = dpr[(r + 1) * 3 + ((s + 2) % 3)] + dpr[r * 3 + s]
            s = s + 1
        }
        r = r + 1
    }
    r = 0
    while r <= 8 {
        B[r] = dpr[r * 3]
        r = r + 1
    }

    let mut numerator: i128 = 0
    let mut m: i64 = 1
    while m <= 8 {
        let factor: i128 = ipow128(3, 8 - m)
        let mut term_m: i128 = 0
        let mut z: i64 = 0
        while z <= m {
            let rr: i64 = m - z
            let count_vectors: i128 = (comb(m, z) * B[rr]) as i128
            let cycles: i64 = m + 2 * z
            term_m = term_m + count_vectors * ipow128(n, cycles)
            z = z + 1
        }
        numerator = numerator + (dp[8 * 9 + m] as i128) * factor * term_m
        m = m + 1
    }
    free(dpr)
    free(B)
    free(dp)
    let denom: i128 = (40320 as i128) * ipow128(3, 7)
    return (numerator / denom) as i64
}

function main() -> i32 {
    printf("%lld\n", distinct_colourings(10))
    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; }

__int128 ipow128_i64_i64(int64_t base, int64_t exp0);
int64_t comb_i64_i64(int64_t n, int64_t k0);
int64_t distinct_colourings_i64(int64_t n);
int32_t main(void);



__int128 ipow128_i64_i64(int64_t base, int64_t exp0) {
    __int128 r = 1;
    int64_t e = exp0;
    __int128 b = ((__int128)(base));
    while (e > 0) {
        if ((e & 1) != 0) {
            r = (r * b);
        }
        b = (b * b);
        e = FLOW_CHECKED_SHR((e), (1));
    }
    return r;
}

int64_t comb_i64_i64(int64_t n, int64_t k0) {
    if ((k0 < 0 || k0 > n)) {
        return 0;
    }
    int64_t k = k0;
    if (k > (n - k)) {
        k = (n - k);
    }
    int64_t r = 1;
    int64_t i = 1;
    while (i <= k) {
        r = FLOW_CHECKED_DIV(((r * ((n - k) + i))), (i));
        i = (i + 1);
    }
    return r;
}

int64_t distinct_colourings_i64(int64_t n) {
    int64_t* dp = (int64_t*)(calloc(81, 8));
    if (dp == NULL) {
        return (-1);
    }
    dp[0] = 1;
    int64_t i = 1;
    while (i <= 8) {
        int64_t k = 1;
        while (k <= i) {
            dp[((i * 9) + k)] = (dp[(((i - 1) * 9) + (k - 1))] + ((i - 1) * dp[(((i - 1) * 9) + k)]));
            k = (k + 1);
        }
        i = (i + 1);
    }
    int64_t* B = (int64_t*)(calloc(9, 8));
    int64_t* dpr = (int64_t*)(calloc(27, 8));
    if ((B == NULL || dpr == NULL)) {
        free(dp);
        return (-1);
    }
    dpr[0] = 1;
    int64_t r = 0;
    while (r < 8) {
        int64_t s = 0;
        while (s < 3) {
            dpr[(((r + 1) * 3) + FLOW_CHECKED_MOD(((s + 1)), (3)))] = (dpr[(((r + 1) * 3) + FLOW_CHECKED_MOD(((s + 1)), (3)))] + dpr[((r * 3) + s)]);
            dpr[(((r + 1) * 3) + FLOW_CHECKED_MOD(((s + 2)), (3)))] = (dpr[(((r + 1) * 3) + FLOW_CHECKED_MOD(((s + 2)), (3)))] + dpr[((r * 3) + s)]);
            s = (s + 1);
        }
        r = (r + 1);
    }
    r = 0;
    while (r <= 8) {
        B[r] = dpr[(r * 3)];
        r = (r + 1);
    }
    __int128 numerator = 0;
    int64_t m = 1;
    while (m <= 8) {
        __int128 factor = ipow128_i64_i64(3, (8 - m));
        __int128 term_m = 0;
        int64_t z = 0;
        while (z <= m) {
            int64_t rr = (m - z);
            __int128 count_vectors = ((__int128)((comb_i64_i64(m, z) * B[rr])));
            int64_t cycles = (m + (2 * z));
            term_m = (term_m + (count_vectors * ipow128_i64_i64(n, cycles)));
            z = (z + 1);
        }
        numerator = (numerator + ((((__int128)(dp[((8 * 9) + m)])) * factor) * term_m));
        m = (m + 1);
    }
    free(dpr);
    free(B);
    free(dp);
    __int128 denom = (((__int128)(40320)) * ipow128_i64_i64(3, 7));
    return ((int64_t)(FLOW_CHECKED_DIV((numerator), (denom))));
}

int32_t main(void) {
    printf("%lld\n", distinct_colourings_i64(10));
    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 @ipow128(%arg0: i64, %arg1: i64) -> i128 {
    %0 = arith.constant 1 : i32
    %1 = arith.extsi %0 : i32 to i128
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i128 : (i64) -> !llvm.ptr
    llvm.store %1, %3 : i128, !llvm.ptr
    %4 = llvm.mlir.constant(1 : i64) : i64
    %5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %5 : i64, !llvm.ptr
    %6 = arith.extsi %arg0 : i64 to i128
    %7 = llvm.mlir.constant(1 : i64) : i64
    %8 = llvm.alloca %7 x i128 : (i64) -> !llvm.ptr
    llvm.store %6, %8 : i128, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %9 = llvm.load %5 : !llvm.ptr -> i64
    %10 = arith.constant 0 : i32
    %12 = arith.extsi %10 : i32 to i64
    %11 = arith.cmpi sgt, %9, %12 : i64
    cf.cond_br %11, ^bb1, ^bb2
    ^bb1:
      %13 = llvm.load %5 : !llvm.ptr -> i64
      %14 = arith.constant 1 : i32
      %16 = arith.extsi %14 : i32 to i64
      %15 = arith.andi %13, %16 : i64
      %17 = arith.constant 0 : i32
      %19 = arith.extsi %17 : i32 to i64
      %18 = arith.cmpi ne, %15, %19 : i64
      cf.cond_br %18, ^bb3, ^bb4
      ^bb3:
        %20 = llvm.load %3 : !llvm.ptr -> i128
        %21 = llvm.load %8 : !llvm.ptr -> i128
        %23 = arith.trunci %20 : i128 to i64
        %24 = arith.trunci %21 : i128 to i64
        %22 = arith.muli %23, %24 : i64
        %25 = arith.extsi %22 : i64 to i128
        llvm.store %25, %3 : i128, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %26 = llvm.load %8 : !llvm.ptr -> i128
      %27 = llvm.load %8 : !llvm.ptr -> i128
      %29 = arith.trunci %26 : i128 to i64
      %30 = arith.trunci %27 : i128 to i64
      %28 = arith.muli %29, %30 : i64
      %31 = arith.extsi %28 : i64 to i128
      llvm.store %31, %8 : i128, !llvm.ptr
      %32 = llvm.load %5 : !llvm.ptr -> i64
      %33 = arith.constant 1 : i32
      %35 = arith.extsi %33 : i32 to i64
      %34 = arith.shrsi %32, %35 : i64
      llvm.store %34, %5 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %36 = llvm.load %3 : !llvm.ptr -> i128
    func.return %36 : i128
  }
  func.func @comb(%arg0: i64, %arg1: i64) -> i64 {
    %37 = arith.constant 0 : i32
    %39 = arith.extsi %37 : i32 to i64
    %38 = arith.cmpi slt, %arg1, %39 : i64
    %40 = scf.if %38 -> (i1) {
      %41 = arith.constant true
      scf.yield %41 : i1
    } else {
      %42 = arith.cmpi sgt, %arg1, %arg0 : i64
      scf.yield %42 : i1
    }
    cf.cond_br %40, ^bb6, ^bb7
    ^bb6:
      %43 = arith.constant 0 : i32
      %44 = arith.extsi %43 : i32 to i64
      func.return %44 : i64
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %45 = llvm.mlir.constant(1 : i64) : i64
    %46 = llvm.alloca %45 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %46 : i64, !llvm.ptr
    %47 = llvm.load %46 : !llvm.ptr -> i64
    %48 = llvm.load %46 : !llvm.ptr -> i64
    %49 = arith.subi %arg0, %48 : i64
    %50 = arith.cmpi sgt, %47, %49 : i64
    cf.cond_br %50, ^bb9, ^bb10
    ^bb9:
      %51 = llvm.load %46 : !llvm.ptr -> i64
      %52 = arith.subi %arg0, %51 : i64
      llvm.store %52, %46 : i64, !llvm.ptr
      cf.br ^bb11
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %53 = arith.constant 1 : i32
    %54 = arith.extsi %53 : i32 to i64
    %55 = llvm.mlir.constant(1 : i64) : i64
    %56 = llvm.alloca %55 x i64 : (i64) -> !llvm.ptr
    llvm.store %54, %56 : i64, !llvm.ptr
    %57 = arith.constant 1 : i32
    %58 = arith.extsi %57 : i32 to i64
    %59 = llvm.mlir.constant(1 : i64) : i64
    %60 = llvm.alloca %59 x i64 : (i64) -> !llvm.ptr
    llvm.store %58, %60 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %61 = llvm.load %60 : !llvm.ptr -> i64
    %62 = llvm.load %46 : !llvm.ptr -> i64
    %63 = arith.cmpi sle, %61, %62 : i64
    cf.cond_br %63, ^bb13, ^bb14
    ^bb13:
      %64 = llvm.load %56 : !llvm.ptr -> i64
      %65 = llvm.load %46 : !llvm.ptr -> i64
      %66 = arith.subi %arg0, %65 : i64
      %67 = llvm.load %60 : !llvm.ptr -> i64
      %68 = arith.addi %66, %67 : i64
      %69 = arith.muli %64, %68 : i64
      %70 = llvm.load %60 : !llvm.ptr -> i64
      %71 = arith.divsi %69, %70 : i64
      llvm.store %71, %56 : i64, !llvm.ptr
      %72 = llvm.load %60 : !llvm.ptr -> i64
      %73 = arith.constant 1 : i32
      %75 = arith.extsi %73 : i32 to i64
      %74 = arith.addi %72, %75 : i64
      llvm.store %74, %60 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %76 = llvm.load %56 : !llvm.ptr -> i64
    func.return %76 : i64
  }
  func.func @distinct_colourings(%arg0: i64) -> i64 {
    %78 = arith.constant 81 : i32
    %79 = arith.constant 8 : i32
    %80 = arith.extsi %78 : i32 to i64
    %81 = arith.extsi %79 : i32 to i64
    %77 = func.call @calloc(%80, %81) : (i64, i64) -> !llvm.ptr
    %82 = llvm.mlir.zero : !llvm.ptr
    %83 = llvm.icmp "eq" %77, %82 : !llvm.ptr
    cf.cond_br %83, ^bb15, ^bb16
    ^bb15:
      %84 = arith.constant 1 : i32
      %86 = arith.constant 0 : i32
      %85 = arith.subi %86, %84 : i32
      %87 = arith.extsi %85 : i32 to i64
      func.return %87 : i64
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %88 = arith.constant 1 : i32
    %89 = arith.constant 0 : i32
    %90 = arith.extsi %88 : i32 to i64
    %91 = arith.extsi %89 : i32 to i64
    %92 = llvm.getelementptr %77[%91] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %90, %92 : i64, !llvm.ptr
    %93 = arith.constant 1 : i32
    %94 = arith.extsi %93 : i32 to i64
    %95 = llvm.mlir.constant(1 : i64) : i64
    %96 = llvm.alloca %95 x i64 : (i64) -> !llvm.ptr
    llvm.store %94, %96 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %97 = llvm.load %96 : !llvm.ptr -> i64
    %98 = arith.constant 8 : i32
    %100 = arith.extsi %98 : i32 to i64
    %99 = arith.cmpi sle, %97, %100 : i64
    cf.cond_br %99, ^bb19, ^bb20
    ^bb19:
      %101 = arith.constant 1 : i32
      %102 = arith.extsi %101 : i32 to i64
      %103 = llvm.mlir.constant(1 : i64) : i64
      %104 = llvm.alloca %103 x i64 : (i64) -> !llvm.ptr
      llvm.store %102, %104 : i64, !llvm.ptr
      cf.br ^bb21
      ^bb21:
      %105 = llvm.load %104 : !llvm.ptr -> i64
      %106 = llvm.load %96 : !llvm.ptr -> i64
      %107 = arith.cmpi sle, %105, %106 : i64
      cf.cond_br %107, ^bb22, ^bb23
      ^bb22:
        %109 = llvm.load %96 : !llvm.ptr -> i64
        %110 = arith.constant 1 : i32
        %112 = arith.extsi %110 : i32 to i64
        %111 = arith.subi %109, %112 : i64
        %113 = arith.constant 9 : i32
        %115 = arith.extsi %113 : i32 to i64
        %114 = arith.muli %111, %115 : i64
        %116 = llvm.load %104 : !llvm.ptr -> i64
        %117 = arith.constant 1 : i32
        %119 = arith.extsi %117 : i32 to i64
        %118 = arith.subi %116, %119 : i64
        %120 = arith.addi %114, %118 : i64
        %121 = llvm.getelementptr %77[%120] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %108 = llvm.load %121 : !llvm.ptr -> i64
        %122 = llvm.load %96 : !llvm.ptr -> i64
        %123 = arith.constant 1 : i32
        %125 = arith.extsi %123 : i32 to i64
        %124 = arith.subi %122, %125 : i64
        %127 = llvm.load %96 : !llvm.ptr -> i64
        %128 = arith.constant 1 : i32
        %130 = arith.extsi %128 : i32 to i64
        %129 = arith.subi %127, %130 : i64
        %131 = arith.constant 9 : i32
        %133 = arith.extsi %131 : i32 to i64
        %132 = arith.muli %129, %133 : i64
        %134 = llvm.load %104 : !llvm.ptr -> i64
        %135 = arith.addi %132, %134 : i64
        %136 = llvm.getelementptr %77[%135] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %126 = llvm.load %136 : !llvm.ptr -> i64
        %137 = arith.muli %124, %126 : i64
        %138 = arith.addi %108, %137 : i64
        %139 = llvm.load %96 : !llvm.ptr -> i64
        %140 = arith.constant 9 : i32
        %142 = arith.extsi %140 : i32 to i64
        %141 = arith.muli %139, %142 : i64
        %143 = llvm.load %104 : !llvm.ptr -> i64
        %144 = arith.addi %141, %143 : i64
        %145 = llvm.getelementptr %77[%144] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %138, %145 : i64, !llvm.ptr
        %146 = llvm.load %104 : !llvm.ptr -> i64
        %147 = arith.constant 1 : i32
        %149 = arith.extsi %147 : i32 to i64
        %148 = arith.addi %146, %149 : i64
        llvm.store %148, %104 : i64, !llvm.ptr
        cf.br ^bb21
      ^bb23:
      %150 = llvm.load %96 : !llvm.ptr -> i64
      %151 = arith.constant 1 : i32
      %153 = arith.extsi %151 : i32 to i64
      %152 = arith.addi %150, %153 : i64
      llvm.store %152, %96 : i64, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %155 = arith.constant 9 : i32
    %156 = arith.constant 8 : i32
    %157 = arith.extsi %155 : i32 to i64
    %158 = arith.extsi %156 : i32 to i64
    %154 = func.call @calloc(%157, %158) : (i64, i64) -> !llvm.ptr
    %160 = arith.constant 27 : i32
    %161 = arith.constant 8 : i32
    %162 = arith.extsi %160 : i32 to i64
    %163 = arith.extsi %161 : i32 to i64
    %159 = func.call @calloc(%162, %163) : (i64, i64) -> !llvm.ptr
    %164 = llvm.mlir.zero : !llvm.ptr
    %165 = llvm.icmp "eq" %154, %164 : !llvm.ptr
    %166 = scf.if %165 -> (i1) {
      %167 = arith.constant true
      scf.yield %167 : i1
    } else {
      %168 = llvm.mlir.zero : !llvm.ptr
      %169 = llvm.icmp "eq" %159, %168 : !llvm.ptr
      scf.yield %169 : i1
    }
    cf.cond_br %166, ^bb24, ^bb25
    ^bb24:
      func.call @free(%77) : (!llvm.ptr) -> ()
      %171 = arith.constant 1 : i32
      %173 = arith.constant 0 : i32
      %172 = arith.subi %173, %171 : i32
      %174 = arith.extsi %172 : i32 to i64
      func.return %174 : i64
    ^bb25:
      cf.br ^bb26
    ^bb26:
    %175 = arith.constant 1 : i32
    %176 = arith.constant 0 : i32
    %177 = arith.extsi %175 : i32 to i64
    %178 = arith.extsi %176 : i32 to i64
    %179 = llvm.getelementptr %159[%178] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %177, %179 : i64, !llvm.ptr
    %180 = arith.constant 0 : i32
    %181 = arith.extsi %180 : i32 to i64
    %182 = llvm.mlir.constant(1 : i64) : i64
    %183 = llvm.alloca %182 x i64 : (i64) -> !llvm.ptr
    llvm.store %181, %183 : i64, !llvm.ptr
    cf.br ^bb27
    ^bb27:
    %184 = llvm.load %183 : !llvm.ptr -> i64
    %185 = arith.constant 8 : i32
    %187 = arith.extsi %185 : i32 to i64
    %186 = arith.cmpi slt, %184, %187 : i64
    cf.cond_br %186, ^bb28, ^bb29
    ^bb28:
      %188 = arith.constant 0 : i32
      %189 = arith.extsi %188 : i32 to i64
      %190 = llvm.mlir.constant(1 : i64) : i64
      %191 = llvm.alloca %190 x i64 : (i64) -> !llvm.ptr
      llvm.store %189, %191 : i64, !llvm.ptr
      cf.br ^bb30
      ^bb30:
      %192 = llvm.load %191 : !llvm.ptr -> i64
      %193 = arith.constant 3 : i32
      %195 = arith.extsi %193 : i32 to i64
      %194 = arith.cmpi slt, %192, %195 : i64
      cf.cond_br %194, ^bb31, ^bb32
      ^bb31:
        %197 = llvm.load %183 : !llvm.ptr -> i64
        %198 = arith.constant 1 : i32
        %200 = arith.extsi %198 : i32 to i64
        %199 = arith.addi %197, %200 : i64
        %201 = arith.constant 3 : i32
        %203 = arith.extsi %201 : i32 to i64
        %202 = arith.muli %199, %203 : i64
        %204 = llvm.load %191 : !llvm.ptr -> i64
        %205 = arith.constant 1 : i32
        %207 = arith.extsi %205 : i32 to i64
        %206 = arith.addi %204, %207 : i64
        %208 = arith.constant 3 : i32
        %210 = arith.extsi %208 : i32 to i64
        %209 = arith.remsi %206, %210 : i64
        %211 = arith.addi %202, %209 : i64
        %212 = llvm.getelementptr %159[%211] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %196 = llvm.load %212 : !llvm.ptr -> i64
        %214 = llvm.load %183 : !llvm.ptr -> i64
        %215 = arith.constant 3 : i32
        %217 = arith.extsi %215 : i32 to i64
        %216 = arith.muli %214, %217 : i64
        %218 = llvm.load %191 : !llvm.ptr -> i64
        %219 = arith.addi %216, %218 : i64
        %220 = llvm.getelementptr %159[%219] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %213 = llvm.load %220 : !llvm.ptr -> i64
        %221 = arith.addi %196, %213 : i64
        %222 = llvm.load %183 : !llvm.ptr -> i64
        %223 = arith.constant 1 : i32
        %225 = arith.extsi %223 : i32 to i64
        %224 = arith.addi %222, %225 : i64
        %226 = arith.constant 3 : i32
        %228 = arith.extsi %226 : i32 to i64
        %227 = arith.muli %224, %228 : i64
        %229 = llvm.load %191 : !llvm.ptr -> i64
        %230 = arith.constant 1 : i32
        %232 = arith.extsi %230 : i32 to i64
        %231 = arith.addi %229, %232 : i64
        %233 = arith.constant 3 : i32
        %235 = arith.extsi %233 : i32 to i64
        %234 = arith.remsi %231, %235 : i64
        %236 = arith.addi %227, %234 : i64
        %237 = llvm.getelementptr %159[%236] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %221, %237 : i64, !llvm.ptr
        %239 = llvm.load %183 : !llvm.ptr -> i64
        %240 = arith.constant 1 : i32
        %242 = arith.extsi %240 : i32 to i64
        %241 = arith.addi %239, %242 : i64
        %243 = arith.constant 3 : i32
        %245 = arith.extsi %243 : i32 to i64
        %244 = arith.muli %241, %245 : i64
        %246 = llvm.load %191 : !llvm.ptr -> i64
        %247 = arith.constant 2 : i32
        %249 = arith.extsi %247 : i32 to i64
        %248 = arith.addi %246, %249 : i64
        %250 = arith.constant 3 : i32
        %252 = arith.extsi %250 : i32 to i64
        %251 = arith.remsi %248, %252 : i64
        %253 = arith.addi %244, %251 : i64
        %254 = llvm.getelementptr %159[%253] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %238 = llvm.load %254 : !llvm.ptr -> i64
        %256 = llvm.load %183 : !llvm.ptr -> i64
        %257 = arith.constant 3 : i32
        %259 = arith.extsi %257 : i32 to i64
        %258 = arith.muli %256, %259 : i64
        %260 = llvm.load %191 : !llvm.ptr -> i64
        %261 = arith.addi %258, %260 : i64
        %262 = llvm.getelementptr %159[%261] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %255 = llvm.load %262 : !llvm.ptr -> i64
        %263 = arith.addi %238, %255 : i64
        %264 = llvm.load %183 : !llvm.ptr -> i64
        %265 = arith.constant 1 : i32
        %267 = arith.extsi %265 : i32 to i64
        %266 = arith.addi %264, %267 : i64
        %268 = arith.constant 3 : i32
        %270 = arith.extsi %268 : i32 to i64
        %269 = arith.muli %266, %270 : i64
        %271 = llvm.load %191 : !llvm.ptr -> i64
        %272 = arith.constant 2 : i32
        %274 = arith.extsi %272 : i32 to i64
        %273 = arith.addi %271, %274 : i64
        %275 = arith.constant 3 : i32
        %277 = arith.extsi %275 : i32 to i64
        %276 = arith.remsi %273, %277 : i64
        %278 = arith.addi %269, %276 : i64
        %279 = llvm.getelementptr %159[%278] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %263, %279 : i64, !llvm.ptr
        %280 = llvm.load %191 : !llvm.ptr -> i64
        %281 = arith.constant 1 : i32
        %283 = arith.extsi %281 : i32 to i64
        %282 = arith.addi %280, %283 : i64
        llvm.store %282, %191 : i64, !llvm.ptr
        cf.br ^bb30
      ^bb32:
      %284 = llvm.load %183 : !llvm.ptr -> i64
      %285 = arith.constant 1 : i32
      %287 = arith.extsi %285 : i32 to i64
      %286 = arith.addi %284, %287 : i64
      llvm.store %286, %183 : i64, !llvm.ptr
      cf.br ^bb27
    ^bb29:
    %288 = arith.constant 0 : i32
    %289 = arith.extsi %288 : i32 to i64
    llvm.store %289, %183 : i64, !llvm.ptr
    cf.br ^bb33
    ^bb33:
    %290 = llvm.load %183 : !llvm.ptr -> i64
    %291 = arith.constant 8 : i32
    %293 = arith.extsi %291 : i32 to i64
    %292 = arith.cmpi sle, %290, %293 : i64
    cf.cond_br %292, ^bb34, ^bb35
    ^bb34:
      %295 = llvm.load %183 : !llvm.ptr -> i64
      %296 = arith.constant 3 : i32
      %298 = arith.extsi %296 : i32 to i64
      %297 = arith.muli %295, %298 : i64
      %299 = llvm.getelementptr %159[%297] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %294 = llvm.load %299 : !llvm.ptr -> i64
      %300 = llvm.load %183 : !llvm.ptr -> i64
      %301 = llvm.getelementptr %154[%300] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %294, %301 : i64, !llvm.ptr
      %302 = llvm.load %183 : !llvm.ptr -> i64
      %303 = arith.constant 1 : i32
      %305 = arith.extsi %303 : i32 to i64
      %304 = arith.addi %302, %305 : i64
      llvm.store %304, %183 : i64, !llvm.ptr
      cf.br ^bb33
    ^bb35:
    %306 = arith.constant 0 : i32
    %307 = arith.extsi %306 : i32 to i128
    %308 = llvm.mlir.constant(1 : i64) : i64
    %309 = llvm.alloca %308 x i128 : (i64) -> !llvm.ptr
    llvm.store %307, %309 : i128, !llvm.ptr
    %310 = arith.constant 1 : i32
    %311 = arith.extsi %310 : i32 to i64
    %312 = llvm.mlir.constant(1 : i64) : i64
    %313 = llvm.alloca %312 x i64 : (i64) -> !llvm.ptr
    llvm.store %311, %313 : i64, !llvm.ptr
    cf.br ^bb36
    ^bb36:
    %314 = llvm.load %313 : !llvm.ptr -> i64
    %315 = arith.constant 8 : i32
    %317 = arith.extsi %315 : i32 to i64
    %316 = arith.cmpi sle, %314, %317 : i64
    cf.cond_br %316, ^bb37, ^bb38
    ^bb37:
      %319 = arith.constant 3 : i32
      %320 = arith.constant 8 : i32
      %321 = llvm.load %313 : !llvm.ptr -> i64
      %323 = arith.extsi %320 : i32 to i64
      %322 = arith.subi %323, %321 : i64
      %324 = arith.extsi %319 : i32 to i64
      %318 = func.call @ipow128(%324, %322) : (i64, i64) -> i128
      %325 = arith.constant 0 : i32
      %326 = arith.extsi %325 : i32 to i128
      %327 = llvm.mlir.constant(1 : i64) : i64
      %328 = llvm.alloca %327 x i128 : (i64) -> !llvm.ptr
      llvm.store %326, %328 : i128, !llvm.ptr
      %329 = arith.constant 0 : i32
      %330 = arith.extsi %329 : i32 to i64
      %331 = llvm.mlir.constant(1 : i64) : i64
      %332 = llvm.alloca %331 x i64 : (i64) -> !llvm.ptr
      llvm.store %330, %332 : i64, !llvm.ptr
      cf.br ^bb39
      ^bb39:
      %333 = llvm.load %332 : !llvm.ptr -> i64
      %334 = llvm.load %313 : !llvm.ptr -> i64
      %335 = arith.cmpi sle, %333, %334 : i64
      cf.cond_br %335, ^bb40, ^bb41
      ^bb40:
        %336 = llvm.load %313 : !llvm.ptr -> i64
        %337 = llvm.load %332 : !llvm.ptr -> i64
        %338 = arith.subi %336, %337 : i64
        %340 = llvm.load %313 : !llvm.ptr -> i64
        %341 = llvm.load %332 : !llvm.ptr -> i64
        %339 = func.call @comb(%340, %341) : (i64, i64) -> i64
        %343 = llvm.getelementptr %154[%338] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %342 = llvm.load %343 : !llvm.ptr -> i64
        %344 = arith.muli %339, %342 : i64
        %345 = arith.extsi %344 : i64 to i128
        %346 = llvm.load %313 : !llvm.ptr -> i64
        %347 = arith.constant 2 : i32
        %348 = llvm.load %332 : !llvm.ptr -> i64
        %350 = arith.extsi %347 : i32 to i64
        %349 = arith.muli %350, %348 : i64
        %351 = arith.addi %346, %349 : i64
        %352 = llvm.load %328 : !llvm.ptr -> i128
        %353 = func.call @ipow128(%arg0, %351) : (i64, i64) -> i128
        %355 = arith.trunci %345 : i128 to i64
        %356 = arith.trunci %353 : i128 to i64
        %354 = arith.muli %355, %356 : i64
        %358 = arith.trunci %352 : i128 to i64
        %357 = arith.addi %358, %354 : i64
        %359 = arith.extsi %357 : i64 to i128
        llvm.store %359, %328 : i128, !llvm.ptr
        %360 = llvm.load %332 : !llvm.ptr -> i64
        %361 = arith.constant 1 : i32
        %363 = arith.extsi %361 : i32 to i64
        %362 = arith.addi %360, %363 : i64
        llvm.store %362, %332 : i64, !llvm.ptr
        cf.br ^bb39
      ^bb41:
      %364 = llvm.load %309 : !llvm.ptr -> i128
      %366 = arith.constant 8 : i32
      %367 = arith.constant 9 : i32
      %368 = arith.muli %366, %367 : i32
      %369 = llvm.load %313 : !llvm.ptr -> i64
      %371 = arith.extsi %368 : i32 to i64
      %370 = arith.addi %371, %369 : i64
      %372 = llvm.getelementptr %77[%370] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %365 = llvm.load %372 : !llvm.ptr -> i64
      %373 = arith.extsi %365 : i64 to i128
      %375 = arith.trunci %373 : i128 to i64
      %376 = arith.trunci %318 : i128 to i64
      %374 = arith.muli %375, %376 : i64
      %377 = llvm.load %328 : !llvm.ptr -> i128
      %379 = arith.trunci %377 : i128 to i64
      %378 = arith.muli %374, %379 : i64
      %381 = arith.trunci %364 : i128 to i64
      %380 = arith.addi %381, %378 : i64
      %382 = arith.extsi %380 : i64 to i128
      llvm.store %382, %309 : i128, !llvm.ptr
      %383 = llvm.load %313 : !llvm.ptr -> i64
      %384 = arith.constant 1 : i32
      %386 = arith.extsi %384 : i32 to i64
      %385 = arith.addi %383, %386 : i64
      llvm.store %385, %313 : i64, !llvm.ptr
      cf.br ^bb36
    ^bb38:
    func.call @free(%159) : (!llvm.ptr) -> ()
    func.call @free(%154) : (!llvm.ptr) -> ()
    func.call @free(%77) : (!llvm.ptr) -> ()
    %390 = arith.constant 40320 : i32
    %391 = arith.extsi %390 : i32 to i128
    %393 = arith.constant 3 : i32
    %394 = arith.constant 7 : i32
    %395 = arith.extsi %393 : i32 to i64
    %396 = arith.extsi %394 : i32 to i64
    %392 = func.call @ipow128(%395, %396) : (i64, i64) -> i128
    %398 = arith.trunci %391 : i128 to i64
    %399 = arith.trunci %392 : i128 to i64
    %397 = arith.muli %398, %399 : i64
    %400 = arith.extsi %397 : i64 to i128
    %401 = llvm.load %309 : !llvm.ptr -> i128
    %403 = arith.trunci %401 : i128 to i64
    %404 = arith.trunci %400 : i128 to i64
    %402 = arith.divsi %403, %404 : i64
    func.return %402 : i64
  }
  func.func @main() -> i32 {
    %405 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %407 = arith.constant 10 : i32
    %408 = arith.extsi %407 : i32 to i64
    %406 = func.call @distinct_colourings(%408) : (i64) -> i64
    %409 = llvm.call @printf(%405, %406) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %410 = arith.constant 0 : i32
    func.return %410 : i32
  }
}