Problem 798

Card Stacking Game: C(n, s) mod 1e9+7 with n = s = 10^7. Pure Flow port of the native C solver.

Answer132996198
Output132996198
StatusPASS
Native helperno
Runtime2570 ms
Peak memory288464 KB
Time complexityO(n^3) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n)
Space complexityO(n)O(1)
ApproachFlow solutionDirect hand evaluation or enumeration
VerdictSuboptimal

Flow source

# Project Euler 798
# Card Stacking Game: C(n, s) mod 1e9+7 with n = s = 10^7.
# Pure Flow port of the native C solver.

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

const MOD: i64 = 1000000007

function mod_pow(a: i64, e: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = a % MOD
    if b < 0 { b = b + MOD }
    let mut exp: i64 = e
    while exp > 0 {
        if exp % 2 == 1 {
            r = r * b % MOD
        }
        b = b * b % MOD
        exp = exp / 2
    }
    return r
}

# Global factorials and inverse factorials
let mut fact: ptr<i64> = null
let mut inv_fact: ptr<i64> = null

function build_factorials(N: i32) -> void {
    fact = calloc((N + 1) as i64, 8)
    inv_fact = calloc((N + 1) as i64, 8)
    fact[0] = 1
    fact[1] = 1
    let mut i: i32 = 2
    while i <= N {
        fact[i] = fact[i - 1] * (i as i64) % MOD
        i = i + 1
    }
    inv_fact[N] = mod_pow(fact[N], MOD - 2)
    i = N
    while i >= 1 {
        inv_fact[i - 1] = inv_fact[i] * (i as i64) % MOD
        i = i - 1
    }
}

function nCk(n: i32, k: i32) -> i64 {
    if k < 0 || k > n { return 0 }
    return fact[n] * inv_fact[k] % MOD * inv_fact[n - k] % MOD
}

# Walsh-Hadamard Transform (XOR) in place
function fwht_xor(a: ptr<i64>, n: i32) -> void {
    let mut h: i32 = 1
    while h < n {
        let step: i32 = h << 1
        let mut i: i32 = 0
        while i < n {
            let mut j: i32 = i
            while j < i + h {
                let x: i64 = a[j]
                let y: i64 = a[j + h]
                let mut u: i64 = x + y
                if u >= MOD { u = u - MOD }
                let mut v: i64 = 0
                if x >= y {
                    v = x - y
                } else {
                    v = x + MOD - y
                }
                a[j] = u
                a[j + h] = v
                j = j + 1
            }
            i = i + step
        }
        h = step
    }
}

# Q(X,k) = X*C(X+k+1,k+1) - (k+1)*C(X+k+1,k+2)
function Q_of(X: i32, k: i32) -> i64 {
    let n1: i32 = X + k + 1
    let c1: i64 = nCk(n1, k + 1)
    let c2: i64 = nCk(n1, k + 2)
    let mut val: i64 = (X as i64) * c1 - ((k + 1) as i64) * c2
    val = val % MOD
    if val < 0 { val = val + MOD }
    return val
}

# Build single-suit Grundy distribution, padded to length L
function build_single_suit(a: ptr<i64>, n: i32, L: i32) -> void {
    memset(a, 0, (L as i64) * 8)
    if n <= 0 { return }
    if n == 1 {
        a[0] = 2
        return
    }

    build_factorials(n)

    let pow2_n2: i64 = mod_pow(2, (n - 2) as i64)
    let a0: i64 = (pow2_n2 + 2) % MOD
    let a1: i64 = (pow2_n2 + (n - 2) as i64) % MOD
    a[0] = a0
    if n > 1 { a[1] = a1 }
    if n > 2 {
        let pow2_n3: i64 = mod_pow(2, (n - 3) as i64)
        a[2] = (pow2_n3 + (n - 3) as i64) % MOD
    }

    let inv4: i64 = mod_pow(4, MOD - 2)

    # Odd Grundy values: g = 2k+3, starting at (X0=n-4, k=0)
    let X0a: i32 = n - 4
    if 3 < n && X0a >= 0 {
        let mut k: i32 = 0
        let mut X: i32 = X0a
        let mut F: i64 = (mod_pow(2, (X + 1) as i64) + MOD - 1) % MOD
        while true {
            let g: i32 = 2 * k + 3
            if g >= n || X < 0 { break }
            a[g] = (F + Q_of(X, k)) % MOD

            if X < 2 { break }
            let c_xk_1: i64 = nCk(X + k - 1, k)
            let c_xk: i64 = nCk(X + k, k)
            let mut tmp: i64 = F - 2 * c_xk_1 - c_xk
            tmp = tmp % MOD
            if tmp < 0 { tmp = tmp + MOD }
            tmp = tmp * inv4 % MOD
            let c_next: i64 = nCk(X + k - 1, k + 1)
            F = (2 * tmp - c_next) % MOD
            if F < 0 { F = F + MOD }
            k = k + 1
            X = X - 2
        }
    }

    # Even Grundy values: g = 2k+4, starting at (X0=n-5, k=0)
    let X0b: i32 = n - 5
    if 4 < n && X0b >= 0 {
        let mut k: i32 = 0
        let mut X: i32 = X0b
        let mut F: i64 = (mod_pow(2, (X + 1) as i64) + MOD - 1) % MOD
        while true {
            let g: i32 = 2 * k + 4
            if g >= n || X < 0 { break }
            a[g] = (F + Q_of(X, k)) % MOD

            if X < 2 { break }
            let c_xk_1: i64 = nCk(X + k - 1, k)
            let c_xk: i64 = nCk(X + k, k)
            let mut tmp: i64 = F - 2 * c_xk_1 - c_xk
            tmp = tmp % MOD
            if tmp < 0 { tmp = tmp + MOD }
            tmp = tmp * inv4 % MOD
            let c_next: i64 = nCk(X + k - 1, k + 1)
            F = (2 * tmp - c_next) % MOD
            if F < 0 { F = F + MOD }
            k = k + 1
            X = X - 2
        }
    }
}

function main() -> i32 {
    let n: i32 = 10000000
    let s: i32 = 10000000

    if n == 0 {
        printf("1\n")
        return 0
    }

    # L = next power of 2 >= n
    let mut L: i32 = 1
    while L < n {
        L = L << 1
    }

    let f: ptr<i64> = calloc(L as i64, 8)
    build_single_suit(f, n, L)

    # Free factorials to save memory
    free(fact)
    free(inv_fact)

    fwht_xor(f, L)

    # Pointwise exponentiation and sum
    let mut total: i64 = 0
    let mut i: i32 = 0
    while i < L {
        total = total + mod_pow(f[i], s as i64)
        if i % 8192 == 0 {
            total = total % MOD
        }
        i = i + 1
    }
    total = total % MOD

    let inv_L: i64 = mod_pow(L as i64, MOD - 2)
    let result: i64 = total * inv_L % MOD

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

int64_t mod_pow_i64_i64(int64_t a, int64_t e);
void build_factorials_i32(int32_t N);
int64_t nCk_i32_i32(int32_t n, int32_t k);
void fwht_xor_ptr_i64_i32(int64_t* a, int32_t n);
int64_t Q_of_i32_i32(int32_t X, int32_t k);
void build_single_suit_ptr_i64_i32_i32(int64_t* a, int32_t n, int32_t L);
int32_t main(void);

static const int64_t MOD = 1000000007;

/* Module statics */
static int64_t* fact = NULL;
static int64_t* inv_fact = NULL;




int64_t mod_pow_i64_i64(int64_t a, int64_t e) {
    int64_t r = 1;
    int64_t b = FLOW_CHECKED_MOD((a), (MOD));
    if (b < 0) {
        b = (b + MOD);
    }
    int64_t exp = e;
    while (exp > 0) {
        if (FLOW_CHECKED_MOD((exp), (2)) == 1) {
            r = FLOW_CHECKED_MOD(((r * b)), (MOD));
        }
        b = FLOW_CHECKED_MOD(((b * b)), (MOD));
        exp = FLOW_CHECKED_DIV((exp), (2));
    }
    return r;
}

void build_factorials_i32(int32_t N) {
    fact = calloc(((int64_t)((N + 1))), 8);
    inv_fact = calloc(((int64_t)((N + 1))), 8);
    fact[0] = 1;
    fact[1] = 1;
    int32_t i = 2;
    while (i <= N) {
        fact[i] = FLOW_CHECKED_MOD(((fact[(i - 1)] * ((int64_t)(i)))), (MOD));
        i = (i + 1);
    }
    inv_fact[N] = mod_pow_i64_i64(fact[N], (MOD - 2));
    i = N;
    while (i >= 1) {
        inv_fact[(i - 1)] = FLOW_CHECKED_MOD(((inv_fact[i] * ((int64_t)(i)))), (MOD));
        i = (i - 1);
    }
}

int64_t nCk_i32_i32(int32_t n, int32_t k) {
    if ((k < 0 || k > n)) {
        return 0;
    }
    return FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((fact[n] * inv_fact[k])), (MOD)) * inv_fact[(n - k)])), (MOD));
}

void fwht_xor_ptr_i64_i32(int64_t* a, int32_t n) {
    int32_t h = 1;
    while (h < n) {
        int32_t step = FLOW_CHECKED_SHL((h), (1));
        int32_t i = 0;
        while (i < n) {
            int32_t j = i;
            while (j < (i + h)) {
                int64_t x = a[j];
                int64_t y = a[(j + h)];
                int64_t u = (x + y);
                if (u >= MOD) {
                    u = (u - MOD);
                }
                int64_t v = 0;
                if (x >= y) {
                    v = (x - y);
                } else {
                    v = ((x + MOD) - y);
                }
                a[j] = u;
                a[(j + h)] = v;
                j = (j + 1);
            }
            i = (i + step);
        }
        h = step;
    }
}

int64_t Q_of_i32_i32(int32_t X, int32_t k) {
    int32_t n1 = ((X + k) + 1);
    int64_t c1 = nCk_i32_i32(n1, (k + 1));
    int64_t c2 = nCk_i32_i32(n1, (k + 2));
    int64_t val = ((((int64_t)(X)) * c1) - (((int64_t)((k + 1))) * c2));
    val = FLOW_CHECKED_MOD((val), (MOD));
    if (val < 0) {
        val = (val + MOD);
    }
    return val;
}

void build_single_suit_ptr_i64_i32_i32(int64_t* a, int32_t n, int32_t L) {
    memset(a, 0, (((int64_t)(L)) * 8));
    if (n <= 0) {
        return;
    }
    if (n == 1) {
        a[0] = 2;
        return;
    }
    build_factorials_i32(n);
    int64_t pow2_n2 = mod_pow_i64_i64(2, ((int64_t)((n - 2))));
    int64_t a0 = FLOW_CHECKED_MOD(((pow2_n2 + 2)), (MOD));
    int64_t a1 = FLOW_CHECKED_MOD(((pow2_n2 + ((int64_t)((n - 2))))), (MOD));
    a[0] = a0;
    if (n > 1) {
        a[1] = a1;
    }
    if (n > 2) {
        int64_t pow2_n3 = mod_pow_i64_i64(2, ((int64_t)((n - 3))));
        a[2] = FLOW_CHECKED_MOD(((pow2_n3 + ((int64_t)((n - 3))))), (MOD));
    }
    int64_t inv4 = mod_pow_i64_i64(4, (MOD - 2));
    int32_t X0a = (n - 4);
    if ((3 < n && X0a >= 0)) {
        int32_t k = 0;
        int32_t X = X0a;
        int64_t F = FLOW_CHECKED_MOD((((mod_pow_i64_i64(2, ((int64_t)((X + 1)))) + MOD) - 1)), (MOD));
        while (1) {
            int32_t g = ((2 * k) + 3);
            if ((g >= n || X < 0)) {
                break;
            }
            a[g] = FLOW_CHECKED_MOD(((F + Q_of_i32_i32(X, k))), (MOD));
            if (X < 2) {
                break;
            }
            int64_t c_xk_1 = nCk_i32_i32(((X + k) - 1), k);
            int64_t c_xk = nCk_i32_i32((X + k), k);
            int64_t tmp = ((F - (2 * c_xk_1)) - c_xk);
            tmp = FLOW_CHECKED_MOD((tmp), (MOD));
            if (tmp < 0) {
                tmp = (tmp + MOD);
            }
            tmp = FLOW_CHECKED_MOD(((tmp * inv4)), (MOD));
            int64_t c_next = nCk_i32_i32(((X + k) - 1), (k + 1));
            F = FLOW_CHECKED_MOD((((2 * tmp) - c_next)), (MOD));
            if (F < 0) {
                F = (F + MOD);
            }
            k = (k + 1);
            X = (X - 2);
        }
    }
    int32_t X0b = (n - 5);
    if ((4 < n && X0b >= 0)) {
        int32_t k = 0;
        int32_t X = X0b;
        int64_t F = FLOW_CHECKED_MOD((((mod_pow_i64_i64(2, ((int64_t)((X + 1)))) + MOD) - 1)), (MOD));
        while (1) {
            int32_t g = ((2 * k) + 4);
            if ((g >= n || X < 0)) {
                break;
            }
            a[g] = FLOW_CHECKED_MOD(((F + Q_of_i32_i32(X, k))), (MOD));
            if (X < 2) {
                break;
            }
            int64_t c_xk_1 = nCk_i32_i32(((X + k) - 1), k);
            int64_t c_xk = nCk_i32_i32((X + k), k);
            int64_t tmp = ((F - (2 * c_xk_1)) - c_xk);
            tmp = FLOW_CHECKED_MOD((tmp), (MOD));
            if (tmp < 0) {
                tmp = (tmp + MOD);
            }
            tmp = FLOW_CHECKED_MOD(((tmp * inv4)), (MOD));
            int64_t c_next = nCk_i32_i32(((X + k) - 1), (k + 1));
            F = FLOW_CHECKED_MOD((((2 * tmp) - c_next)), (MOD));
            if (F < 0) {
                F = (F + MOD);
            }
            k = (k + 1);
            X = (X - 2);
        }
    }
}

int32_t main(void) {
    int32_t n = 10000000;
    int32_t s = 10000000;
    if (n == 0) {
        printf("1\n");
        return 0;
    }
    int32_t L = 1;
    while (L < n) {
        L = FLOW_CHECKED_SHL((L), (1));
    }
    int64_t* f = (int64_t*)(calloc(((int64_t)(L)), 8));
    build_single_suit_ptr_i64_i32_i32(f, n, L);
    free(fact);
    free(inv_fact);
    fwht_xor_ptr_i64_i32(f, L);
    int64_t total = 0;
    int32_t i = 0;
    while (i < L) {
        total = (total + mod_pow_i64_i64(f[i], ((int64_t)(s))));
        if (FLOW_CHECKED_MOD((i), (8192)) == 0) {
            total = FLOW_CHECKED_MOD((total), (MOD));
        }
        i = (i + 1);
    }
    total = FLOW_CHECKED_MOD((total), (MOD));
    int64_t inv_L = mod_pow_i64_i64(((int64_t)(L)), (MOD - 2));
    int64_t result = FLOW_CHECKED_MOD(((total * inv_L)), (MOD));
    free(f);
    printf("%lld\n", result);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("1\n\00") {addr_space = 0 : i32} : !llvm.array<3 x i8>
  llvm.mlir.global internal constant @str_1("%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 private @memset(!llvm.ptr, i32, i64) -> !llvm.ptr
  // Constant: MOD
  llvm.mlir.global internal constant @MOD(1000000007 : i64) : i64
  func.func @mod_pow(%arg0: i64, %arg1: i64) -> i64 {
    %0 = arith.constant 1 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
    llvm.store %1, %3 : i64, !llvm.ptr
    %4 = llvm.mlir.addressof @MOD : !llvm.ptr
    %5 = llvm.load %4 : !llvm.ptr -> i64
    %6 = arith.remsi %arg0, %5 : i64
    %7 = llvm.mlir.constant(1 : i64) : i64
    %8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
    llvm.store %6, %8 : i64, !llvm.ptr
    %9 = llvm.load %8 : !llvm.ptr -> i64
    %10 = arith.constant 0 : i32
    %12 = arith.extsi %10 : i32 to i64
    %11 = arith.cmpi slt, %9, %12 : i64
    cf.cond_br %11, ^bb0, ^bb1
    ^bb0:
      %13 = llvm.load %8 : !llvm.ptr -> i64
      %14 = llvm.mlir.addressof @MOD : !llvm.ptr
      %15 = llvm.load %14 : !llvm.ptr -> i64
      %16 = arith.addi %13, %15 : i64
      llvm.store %16, %8 : i64, !llvm.ptr
      cf.br ^bb2
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %17 = llvm.mlir.constant(1 : i64) : i64
    %18 = llvm.alloca %17 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %18 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %19 = llvm.load %18 : !llvm.ptr -> i64
    %20 = arith.constant 0 : i32
    %22 = arith.extsi %20 : i32 to i64
    %21 = arith.cmpi sgt, %19, %22 : i64
    cf.cond_br %21, ^bb4, ^bb5
    ^bb4:
      %23 = llvm.load %18 : !llvm.ptr -> i64
      %24 = arith.constant 2 : i32
      %26 = arith.extsi %24 : i32 to i64
      %25 = arith.remsi %23, %26 : i64
      %27 = arith.constant 1 : i32
      %29 = arith.extsi %27 : i32 to i64
      %28 = arith.cmpi eq, %25, %29 : i64
      cf.cond_br %28, ^bb6, ^bb7
      ^bb6:
        %30 = llvm.load %3 : !llvm.ptr -> i64
        %31 = llvm.load %8 : !llvm.ptr -> i64
        %32 = arith.muli %30, %31 : i64
        %33 = llvm.mlir.addressof @MOD : !llvm.ptr
        %34 = llvm.load %33 : !llvm.ptr -> i64
        %35 = arith.remsi %32, %34 : i64
        llvm.store %35, %3 : i64, !llvm.ptr
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %36 = llvm.load %8 : !llvm.ptr -> i64
      %37 = llvm.load %8 : !llvm.ptr -> i64
      %38 = arith.muli %36, %37 : i64
      %39 = llvm.mlir.addressof @MOD : !llvm.ptr
      %40 = llvm.load %39 : !llvm.ptr -> i64
      %41 = arith.remsi %38, %40 : i64
      llvm.store %41, %8 : i64, !llvm.ptr
      %42 = llvm.load %18 : !llvm.ptr -> i64
      %43 = arith.constant 2 : i32
      %45 = arith.extsi %43 : i32 to i64
      %44 = arith.divsi %42, %45 : i64
      llvm.store %44, %18 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %46 = llvm.load %3 : !llvm.ptr -> i64
    func.return %46 : i64
  }
  // Module static: fact
  llvm.mlir.global internal @fact() {addr_space = 0 : i32} : !llvm.ptr {
    %47 = llvm.mlir.zero : !llvm.ptr
    llvm.return %47 : !llvm.ptr
  }
  // Module static: inv_fact
  llvm.mlir.global internal @inv_fact() {addr_space = 0 : i32} : !llvm.ptr {
    %48 = llvm.mlir.zero : !llvm.ptr
    llvm.return %48 : !llvm.ptr
  }
  func.func @build_factorials(%arg0: i32) -> () {
    %50 = arith.constant 1 : i32
    %51 = arith.addi %arg0, %50 : i32
    %52 = arith.extsi %51 : i32 to i64
    %53 = arith.constant 8 : i32
    %54 = arith.extsi %53 : i32 to i64
    %49 = func.call @calloc(%52, %54) : (i64, i64) -> !llvm.ptr
    %55 = llvm.mlir.addressof @fact : !llvm.ptr
    llvm.store %49, %55 : !llvm.ptr, !llvm.ptr
    %57 = arith.constant 1 : i32
    %58 = arith.addi %arg0, %57 : i32
    %59 = arith.extsi %58 : i32 to i64
    %60 = arith.constant 8 : i32
    %61 = arith.extsi %60 : i32 to i64
    %56 = func.call @calloc(%59, %61) : (i64, i64) -> !llvm.ptr
    %62 = llvm.mlir.addressof @inv_fact : !llvm.ptr
    llvm.store %56, %62 : !llvm.ptr, !llvm.ptr
    %63 = arith.constant 1 : i32
    %64 = llvm.mlir.addressof @fact : !llvm.ptr
    %65 = llvm.load %64 : !llvm.ptr -> !llvm.ptr
    %66 = arith.constant 0 : i32
    %67 = arith.extsi %63 : i32 to i64
    %68 = arith.extsi %66 : i32 to i64
    %69 = llvm.getelementptr %65[%68] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %67, %69 : i64, !llvm.ptr
    %70 = arith.constant 1 : i32
    %71 = llvm.mlir.addressof @fact : !llvm.ptr
    %72 = llvm.load %71 : !llvm.ptr -> !llvm.ptr
    %73 = arith.constant 1 : i32
    %74 = arith.extsi %70 : i32 to i64
    %75 = arith.extsi %73 : i32 to i64
    %76 = llvm.getelementptr %72[%75] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %74, %76 : i64, !llvm.ptr
    %77 = arith.constant 2 : i32
    %78 = llvm.mlir.constant(1 : i64) : i64
    %79 = llvm.alloca %78 x i32 : (i64) -> !llvm.ptr
    llvm.store %77, %79 : i32, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %80 = llvm.load %79 : !llvm.ptr -> i32
    %81 = arith.cmpi sle, %80, %arg0 : i32
    cf.cond_br %81, ^bb10, ^bb11
    ^bb10:
      %83 = llvm.mlir.addressof @fact : !llvm.ptr
      %84 = llvm.load %83 : !llvm.ptr -> !llvm.ptr
      %85 = llvm.load %79 : !llvm.ptr -> i32
      %86 = arith.constant 1 : i32
      %87 = arith.subi %85, %86 : i32
      %88 = arith.extsi %87 : i32 to i64
      %89 = llvm.getelementptr %84[%88] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %82 = llvm.load %89 : !llvm.ptr -> i64
      %90 = llvm.load %79 : !llvm.ptr -> i32
      %91 = arith.extsi %90 : i32 to i64
      %92 = arith.muli %82, %91 : i64
      %93 = llvm.mlir.addressof @MOD : !llvm.ptr
      %94 = llvm.load %93 : !llvm.ptr -> i64
      %95 = arith.remsi %92, %94 : i64
      %96 = llvm.mlir.addressof @fact : !llvm.ptr
      %97 = llvm.load %96 : !llvm.ptr -> !llvm.ptr
      %98 = llvm.load %79 : !llvm.ptr -> i32
      %99 = arith.extsi %98 : i32 to i64
      %100 = llvm.getelementptr %97[%99] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %95, %100 : i64, !llvm.ptr
      %101 = llvm.load %79 : !llvm.ptr -> i32
      %102 = arith.constant 1 : i32
      %103 = arith.addi %101, %102 : i32
      llvm.store %103, %79 : i32, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %106 = llvm.mlir.addressof @fact : !llvm.ptr
    %107 = llvm.load %106 : !llvm.ptr -> !llvm.ptr
    %108 = arith.extsi %arg0 : i32 to i64
    %109 = llvm.getelementptr %107[%108] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %105 = llvm.load %109 : !llvm.ptr -> i64
    %110 = llvm.mlir.addressof @MOD : !llvm.ptr
    %111 = llvm.load %110 : !llvm.ptr -> i64
    %112 = arith.constant 2 : i32
    %114 = arith.extsi %112 : i32 to i64
    %113 = arith.subi %111, %114 : i64
    %104 = func.call @mod_pow(%105, %113) : (i64, i64) -> i64
    %115 = llvm.mlir.addressof @inv_fact : !llvm.ptr
    %116 = llvm.load %115 : !llvm.ptr -> !llvm.ptr
    %117 = arith.extsi %arg0 : i32 to i64
    %118 = llvm.getelementptr %116[%117] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %104, %118 : i64, !llvm.ptr
    llvm.store %arg0, %79 : i32, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %119 = llvm.load %79 : !llvm.ptr -> i32
    %120 = arith.constant 1 : i32
    %121 = arith.cmpi sge, %119, %120 : i32
    cf.cond_br %121, ^bb13, ^bb14
    ^bb13:
      %123 = llvm.mlir.addressof @inv_fact : !llvm.ptr
      %124 = llvm.load %123 : !llvm.ptr -> !llvm.ptr
      %125 = llvm.load %79 : !llvm.ptr -> i32
      %126 = arith.extsi %125 : i32 to i64
      %127 = llvm.getelementptr %124[%126] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %122 = llvm.load %127 : !llvm.ptr -> i64
      %128 = llvm.load %79 : !llvm.ptr -> i32
      %129 = arith.extsi %128 : i32 to i64
      %130 = arith.muli %122, %129 : i64
      %131 = llvm.mlir.addressof @MOD : !llvm.ptr
      %132 = llvm.load %131 : !llvm.ptr -> i64
      %133 = arith.remsi %130, %132 : i64
      %134 = llvm.mlir.addressof @inv_fact : !llvm.ptr
      %135 = llvm.load %134 : !llvm.ptr -> !llvm.ptr
      %136 = llvm.load %79 : !llvm.ptr -> i32
      %137 = arith.constant 1 : i32
      %138 = arith.subi %136, %137 : i32
      %139 = arith.extsi %138 : i32 to i64
      %140 = llvm.getelementptr %135[%139] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %133, %140 : i64, !llvm.ptr
      %141 = llvm.load %79 : !llvm.ptr -> i32
      %142 = arith.constant 1 : i32
      %143 = arith.subi %141, %142 : i32
      llvm.store %143, %79 : i32, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    func.return
  }
  func.func @nCk(%arg0: i32, %arg1: i32) -> i64 {
    %144 = arith.constant 0 : i32
    %145 = arith.cmpi slt, %arg1, %144 : i32
    %146 = scf.if %145 -> (i1) {
      %147 = arith.constant true
      scf.yield %147 : i1
    } else {
      %148 = arith.cmpi sgt, %arg1, %arg0 : i32
      scf.yield %148 : i1
    }
    cf.cond_br %146, ^bb15, ^bb16
    ^bb15:
      %149 = arith.constant 0 : i32
      %150 = arith.extsi %149 : i32 to i64
      func.return %150 : i64
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %152 = llvm.mlir.addressof @fact : !llvm.ptr
    %153 = llvm.load %152 : !llvm.ptr -> !llvm.ptr
    %154 = arith.extsi %arg0 : i32 to i64
    %155 = llvm.getelementptr %153[%154] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %151 = llvm.load %155 : !llvm.ptr -> i64
    %157 = llvm.mlir.addressof @inv_fact : !llvm.ptr
    %158 = llvm.load %157 : !llvm.ptr -> !llvm.ptr
    %159 = arith.extsi %arg1 : i32 to i64
    %160 = llvm.getelementptr %158[%159] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %156 = llvm.load %160 : !llvm.ptr -> i64
    %161 = arith.muli %151, %156 : i64
    %162 = llvm.mlir.addressof @MOD : !llvm.ptr
    %163 = llvm.load %162 : !llvm.ptr -> i64
    %164 = arith.remsi %161, %163 : i64
    %166 = llvm.mlir.addressof @inv_fact : !llvm.ptr
    %167 = llvm.load %166 : !llvm.ptr -> !llvm.ptr
    %168 = arith.subi %arg0, %arg1 : i32
    %169 = arith.extsi %168 : i32 to i64
    %170 = llvm.getelementptr %167[%169] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %165 = llvm.load %170 : !llvm.ptr -> i64
    %171 = arith.muli %164, %165 : i64
    %172 = llvm.mlir.addressof @MOD : !llvm.ptr
    %173 = llvm.load %172 : !llvm.ptr -> i64
    %174 = arith.remsi %171, %173 : i64
    func.return %174 : i64
  }
  func.func @fwht_xor(%arg0: !llvm.ptr, %arg1: i32) -> () {
    %175 = arith.constant 1 : i32
    %176 = llvm.mlir.constant(1 : i64) : i64
    %177 = llvm.alloca %176 x i32 : (i64) -> !llvm.ptr
    llvm.store %175, %177 : i32, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %178 = llvm.load %177 : !llvm.ptr -> i32
    %179 = arith.cmpi slt, %178, %arg1 : i32
    cf.cond_br %179, ^bb19, ^bb20
    ^bb19:
      %180 = llvm.load %177 : !llvm.ptr -> i32
      %181 = arith.constant 1 : i32
      %182 = arith.shli %180, %181 : i32
      %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 ^bb21
      ^bb21:
      %186 = llvm.load %185 : !llvm.ptr -> i32
      %187 = arith.cmpi slt, %186, %arg1 : i32
      cf.cond_br %187, ^bb22, ^bb23
      ^bb22:
        %188 = llvm.load %185 : !llvm.ptr -> i32
        %189 = llvm.mlir.constant(1 : i64) : i64
        %190 = llvm.alloca %189 x i32 : (i64) -> !llvm.ptr
        llvm.store %188, %190 : i32, !llvm.ptr
        cf.br ^bb24
        ^bb24:
        %191 = llvm.load %190 : !llvm.ptr -> i32
        %192 = llvm.load %185 : !llvm.ptr -> i32
        %193 = llvm.load %177 : !llvm.ptr -> i32
        %194 = arith.addi %192, %193 : i32
        %195 = arith.cmpi slt, %191, %194 : i32
        cf.cond_br %195, ^bb25, ^bb26
        ^bb25:
          %197 = llvm.load %190 : !llvm.ptr -> i32
          %198 = arith.extsi %197 : i32 to i64
          %199 = llvm.getelementptr %arg0[%198] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %196 = llvm.load %199 : !llvm.ptr -> i64
          %201 = llvm.load %190 : !llvm.ptr -> i32
          %202 = llvm.load %177 : !llvm.ptr -> i32
          %203 = arith.addi %201, %202 : i32
          %204 = arith.extsi %203 : i32 to i64
          %205 = llvm.getelementptr %arg0[%204] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %200 = llvm.load %205 : !llvm.ptr -> i64
          %206 = arith.addi %196, %200 : 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 %208 : !llvm.ptr -> i64
          %210 = llvm.mlir.addressof @MOD : !llvm.ptr
          %211 = llvm.load %210 : !llvm.ptr -> i64
          %212 = arith.cmpi sge, %209, %211 : i64
          cf.cond_br %212, ^bb27, ^bb28
          ^bb27:
            %213 = llvm.load %208 : !llvm.ptr -> i64
            %214 = llvm.mlir.addressof @MOD : !llvm.ptr
            %215 = llvm.load %214 : !llvm.ptr -> i64
            %216 = arith.subi %213, %215 : i64
            llvm.store %216, %208 : i64, !llvm.ptr
            cf.br ^bb29
          ^bb28:
            cf.br ^bb29
          ^bb29:
          %217 = arith.constant 0 : i32
          %218 = arith.extsi %217 : i32 to i64
          %219 = llvm.mlir.constant(1 : i64) : i64
          %220 = llvm.alloca %219 x i64 : (i64) -> !llvm.ptr
          llvm.store %218, %220 : i64, !llvm.ptr
          %221 = arith.cmpi sge, %196, %200 : i64
          cf.cond_br %221, ^bb30, ^bb31
          ^bb30:
            %222 = arith.subi %196, %200 : i64
            llvm.store %222, %220 : i64, !llvm.ptr
            cf.br ^bb32
          ^bb31:
            %223 = llvm.mlir.addressof @MOD : !llvm.ptr
            %224 = llvm.load %223 : !llvm.ptr -> i64
            %225 = arith.addi %196, %224 : i64
            %226 = arith.subi %225, %200 : i64
            llvm.store %226, %220 : i64, !llvm.ptr
            cf.br ^bb32
          ^bb32:
          %227 = llvm.load %208 : !llvm.ptr -> i64
          %228 = llvm.load %190 : !llvm.ptr -> i32
          %229 = arith.extsi %228 : i32 to i64
          %230 = llvm.getelementptr %arg0[%229] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %227, %230 : i64, !llvm.ptr
          %231 = llvm.load %220 : !llvm.ptr -> i64
          %232 = llvm.load %190 : !llvm.ptr -> i32
          %233 = llvm.load %177 : !llvm.ptr -> i32
          %234 = arith.addi %232, %233 : i32
          %235 = arith.extsi %234 : i32 to i64
          %236 = llvm.getelementptr %arg0[%235] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %231, %236 : i64, !llvm.ptr
          %237 = llvm.load %190 : !llvm.ptr -> i32
          %238 = arith.constant 1 : i32
          %239 = arith.addi %237, %238 : i32
          llvm.store %239, %190 : i32, !llvm.ptr
          cf.br ^bb24
        ^bb26:
        %240 = llvm.load %185 : !llvm.ptr -> i32
        %241 = arith.addi %240, %182 : i32
        llvm.store %241, %185 : i32, !llvm.ptr
        cf.br ^bb21
      ^bb23:
      llvm.store %182, %177 : i32, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    func.return
  }
  func.func @Q_of(%arg0: i32, %arg1: i32) -> i64 {
    %242 = arith.addi %arg0, %arg1 : i32
    %243 = arith.constant 1 : i32
    %244 = arith.addi %242, %243 : i32
    %246 = arith.constant 1 : i32
    %247 = arith.addi %arg1, %246 : i32
    %245 = func.call @nCk(%244, %247) : (i32, i32) -> i64
    %249 = arith.constant 2 : i32
    %250 = arith.addi %arg1, %249 : i32
    %248 = func.call @nCk(%244, %250) : (i32, i32) -> i64
    %251 = arith.extsi %arg0 : i32 to i64
    %252 = arith.muli %251, %245 : i64
    %253 = arith.constant 1 : i32
    %254 = arith.addi %arg1, %253 : i32
    %255 = arith.extsi %254 : i32 to i64
    %256 = arith.muli %255, %248 : i64
    %257 = arith.subi %252, %256 : i64
    %258 = llvm.mlir.constant(1 : i64) : i64
    %259 = llvm.alloca %258 x i64 : (i64) -> !llvm.ptr
    llvm.store %257, %259 : i64, !llvm.ptr
    %260 = llvm.load %259 : !llvm.ptr -> i64
    %261 = llvm.mlir.addressof @MOD : !llvm.ptr
    %262 = llvm.load %261 : !llvm.ptr -> i64
    %263 = arith.remsi %260, %262 : i64
    llvm.store %263, %259 : i64, !llvm.ptr
    %264 = llvm.load %259 : !llvm.ptr -> i64
    %265 = arith.constant 0 : i32
    %267 = arith.extsi %265 : i32 to i64
    %266 = arith.cmpi slt, %264, %267 : i64
    cf.cond_br %266, ^bb33, ^bb34
    ^bb33:
      %268 = llvm.load %259 : !llvm.ptr -> i64
      %269 = llvm.mlir.addressof @MOD : !llvm.ptr
      %270 = llvm.load %269 : !llvm.ptr -> i64
      %271 = arith.addi %268, %270 : i64
      llvm.store %271, %259 : i64, !llvm.ptr
      cf.br ^bb35
    ^bb34:
      cf.br ^bb35
    ^bb35:
    %272 = llvm.load %259 : !llvm.ptr -> i64
    func.return %272 : i64
  }
  func.func @build_single_suit(%arg0: !llvm.ptr, %arg1: i32, %arg2: i32) -> () {
    %274 = arith.constant 0 : i32
    %275 = arith.extsi %arg2 : i32 to i64
    %276 = arith.constant 8 : i32
    %278 = arith.extsi %276 : i32 to i64
    %277 = arith.muli %275, %278 : i64
    %273 = func.call @memset(%arg0, %274, %277) : (!llvm.ptr, i32, i64) -> !llvm.ptr
    %279 = arith.constant 0 : i32
    %280 = arith.cmpi sle, %arg1, %279 : i32
    cf.cond_br %280, ^bb36, ^bb37
    ^bb36:
      func.return
    ^bb37:
      cf.br ^bb38
    ^bb38:
    %281 = arith.constant 1 : i32
    %282 = arith.cmpi eq, %arg1, %281 : i32
    cf.cond_br %282, ^bb39, ^bb40
    ^bb39:
      %283 = arith.constant 2 : i32
      %284 = arith.constant 0 : i32
      %285 = arith.extsi %283 : i32 to i64
      %286 = arith.extsi %284 : i32 to i64
      %287 = llvm.getelementptr %arg0[%286] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %285, %287 : i64, !llvm.ptr
      func.return
    ^bb40:
      cf.br ^bb41
    ^bb41:
    func.call @build_factorials(%arg1) : (i32) -> ()
    %290 = arith.constant 2 : i32
    %291 = arith.constant 2 : i32
    %292 = arith.subi %arg1, %291 : i32
    %293 = arith.extsi %292 : i32 to i64
    %294 = arith.extsi %290 : i32 to i64
    %289 = func.call @mod_pow(%294, %293) : (i64, i64) -> i64
    %295 = arith.constant 2 : i32
    %297 = arith.extsi %295 : i32 to i64
    %296 = arith.addi %289, %297 : i64
    %298 = llvm.mlir.addressof @MOD : !llvm.ptr
    %299 = llvm.load %298 : !llvm.ptr -> i64
    %300 = arith.remsi %296, %299 : i64
    %301 = arith.constant 2 : i32
    %302 = arith.subi %arg1, %301 : i32
    %303 = arith.extsi %302 : i32 to i64
    %304 = arith.addi %289, %303 : i64
    %305 = llvm.mlir.addressof @MOD : !llvm.ptr
    %306 = llvm.load %305 : !llvm.ptr -> i64
    %307 = arith.remsi %304, %306 : i64
    %308 = arith.constant 0 : i32
    %309 = arith.extsi %308 : i32 to i64
    %310 = llvm.getelementptr %arg0[%309] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %300, %310 : i64, !llvm.ptr
    %311 = arith.constant 1 : i32
    %312 = arith.cmpi sgt, %arg1, %311 : i32
    cf.cond_br %312, ^bb42, ^bb43
    ^bb42:
      %313 = arith.constant 1 : i32
      %314 = arith.extsi %313 : i32 to i64
      %315 = llvm.getelementptr %arg0[%314] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %307, %315 : i64, !llvm.ptr
      cf.br ^bb44
    ^bb43:
      cf.br ^bb44
    ^bb44:
    %316 = arith.constant 2 : i32
    %317 = arith.cmpi sgt, %arg1, %316 : i32
    cf.cond_br %317, ^bb45, ^bb46
    ^bb45:
      %319 = arith.constant 2 : i32
      %320 = arith.constant 3 : i32
      %321 = arith.subi %arg1, %320 : i32
      %322 = arith.extsi %321 : i32 to i64
      %323 = arith.extsi %319 : i32 to i64
      %318 = func.call @mod_pow(%323, %322) : (i64, i64) -> i64
      %324 = arith.constant 3 : i32
      %325 = arith.subi %arg1, %324 : i32
      %326 = arith.extsi %325 : i32 to i64
      %327 = arith.addi %318, %326 : i64
      %328 = llvm.mlir.addressof @MOD : !llvm.ptr
      %329 = llvm.load %328 : !llvm.ptr -> i64
      %330 = arith.remsi %327, %329 : i64
      %331 = arith.constant 2 : i32
      %332 = arith.extsi %331 : i32 to i64
      %333 = llvm.getelementptr %arg0[%332] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %330, %333 : i64, !llvm.ptr
      cf.br ^bb47
    ^bb46:
      cf.br ^bb47
    ^bb47:
    %335 = arith.constant 4 : i32
    %336 = llvm.mlir.addressof @MOD : !llvm.ptr
    %337 = llvm.load %336 : !llvm.ptr -> i64
    %338 = arith.constant 2 : i32
    %340 = arith.extsi %338 : i32 to i64
    %339 = arith.subi %337, %340 : i64
    %341 = arith.extsi %335 : i32 to i64
    %334 = func.call @mod_pow(%341, %339) : (i64, i64) -> i64
    %342 = arith.constant 4 : i32
    %343 = arith.subi %arg1, %342 : i32
    %344 = arith.constant 3 : i32
    %345 = arith.cmpi slt, %344, %arg1 : i32
    %346 = scf.if %345 -> (i1) {
      %347 = arith.constant 0 : i32
      %348 = arith.cmpi sge, %343, %347 : i32
      scf.yield %348 : i1
    } else {
      %349 = arith.constant false
      scf.yield %349 : i1
    }
    cf.cond_br %346, ^bb48, ^bb49
    ^bb48:
      %350 = arith.constant 0 : i32
      %351 = llvm.mlir.constant(1 : i64) : i64
      %352 = llvm.alloca %351 x i32 : (i64) -> !llvm.ptr
      llvm.store %350, %352 : i32, !llvm.ptr
      %353 = llvm.mlir.constant(1 : i64) : i64
      %354 = llvm.alloca %353 x i32 : (i64) -> !llvm.ptr
      llvm.store %343, %354 : i32, !llvm.ptr
      %356 = arith.constant 2 : i32
      %357 = llvm.load %354 : !llvm.ptr -> i32
      %358 = arith.constant 1 : i32
      %359 = arith.addi %357, %358 : i32
      %360 = arith.extsi %359 : i32 to i64
      %361 = arith.extsi %356 : i32 to i64
      %355 = func.call @mod_pow(%361, %360) : (i64, i64) -> i64
      %362 = llvm.mlir.addressof @MOD : !llvm.ptr
      %363 = llvm.load %362 : !llvm.ptr -> i64
      %364 = arith.addi %355, %363 : i64
      %365 = arith.constant 1 : i32
      %367 = arith.extsi %365 : i32 to i64
      %366 = arith.subi %364, %367 : i64
      %368 = llvm.mlir.addressof @MOD : !llvm.ptr
      %369 = llvm.load %368 : !llvm.ptr -> i64
      %370 = arith.remsi %366, %369 : i64
      %371 = llvm.mlir.constant(1 : i64) : i64
      %372 = llvm.alloca %371 x i64 : (i64) -> !llvm.ptr
      llvm.store %370, %372 : i64, !llvm.ptr
      cf.br ^bb51
      ^bb51:
      %373 = arith.constant 1 : i1
      cf.cond_br %373, ^bb52, ^bb53
      ^bb52:
        %374 = arith.constant 2 : i32
        %375 = llvm.load %352 : !llvm.ptr -> i32
        %376 = arith.muli %374, %375 : i32
        %377 = arith.constant 3 : i32
        %378 = arith.addi %376, %377 : i32
        %379 = arith.cmpi sge, %378, %arg1 : i32
        %380 = scf.if %379 -> (i1) {
          %381 = arith.constant true
          scf.yield %381 : i1
        } else {
          %382 = llvm.load %354 : !llvm.ptr -> i32
          %383 = arith.constant 0 : i32
          %384 = arith.cmpi slt, %382, %383 : i32
          scf.yield %384 : i1
        }
        cf.cond_br %380, ^bb54, ^bb55
        ^bb54:
          cf.br ^bb53
        ^bb55:
          cf.br ^bb56
        ^bb56:
        %385 = llvm.load %372 : !llvm.ptr -> i64
        %387 = llvm.load %354 : !llvm.ptr -> i32
        %388 = llvm.load %352 : !llvm.ptr -> i32
        %386 = func.call @Q_of(%387, %388) : (i32, i32) -> i64
        %389 = arith.addi %385, %386 : i64
        %390 = llvm.mlir.addressof @MOD : !llvm.ptr
        %391 = llvm.load %390 : !llvm.ptr -> i64
        %392 = arith.remsi %389, %391 : i64
        %393 = arith.extsi %378 : i32 to i64
        %394 = llvm.getelementptr %arg0[%393] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %392, %394 : i64, !llvm.ptr
        %395 = llvm.load %354 : !llvm.ptr -> i32
        %396 = arith.constant 2 : i32
        %397 = arith.cmpi slt, %395, %396 : i32
        cf.cond_br %397, ^bb57, ^bb58
        ^bb57:
          cf.br ^bb53
        ^bb58:
          cf.br ^bb59
        ^bb59:
        %399 = llvm.load %354 : !llvm.ptr -> i32
        %400 = llvm.load %352 : !llvm.ptr -> i32
        %401 = arith.addi %399, %400 : i32
        %402 = arith.constant 1 : i32
        %403 = arith.subi %401, %402 : i32
        %404 = llvm.load %352 : !llvm.ptr -> i32
        %398 = func.call @nCk(%403, %404) : (i32, i32) -> i64
        %406 = llvm.load %354 : !llvm.ptr -> i32
        %407 = llvm.load %352 : !llvm.ptr -> i32
        %408 = arith.addi %406, %407 : i32
        %409 = llvm.load %352 : !llvm.ptr -> i32
        %405 = func.call @nCk(%408, %409) : (i32, i32) -> i64
        %410 = llvm.load %372 : !llvm.ptr -> i64
        %411 = arith.constant 2 : i32
        %413 = arith.extsi %411 : i32 to i64
        %412 = arith.muli %413, %398 : i64
        %414 = arith.subi %410, %412 : i64
        %415 = arith.subi %414, %405 : i64
        %416 = llvm.mlir.constant(1 : i64) : i64
        %417 = llvm.alloca %416 x i64 : (i64) -> !llvm.ptr
        llvm.store %415, %417 : i64, !llvm.ptr
        %418 = llvm.load %417 : !llvm.ptr -> i64
        %419 = llvm.mlir.addressof @MOD : !llvm.ptr
        %420 = llvm.load %419 : !llvm.ptr -> i64
        %421 = arith.remsi %418, %420 : i64
        llvm.store %421, %417 : i64, !llvm.ptr
        %422 = llvm.load %417 : !llvm.ptr -> i64
        %423 = arith.constant 0 : i32
        %425 = arith.extsi %423 : i32 to i64
        %424 = arith.cmpi slt, %422, %425 : i64
        cf.cond_br %424, ^bb60, ^bb61
        ^bb60:
          %426 = llvm.load %417 : !llvm.ptr -> i64
          %427 = llvm.mlir.addressof @MOD : !llvm.ptr
          %428 = llvm.load %427 : !llvm.ptr -> i64
          %429 = arith.addi %426, %428 : i64
          llvm.store %429, %417 : i64, !llvm.ptr
          cf.br ^bb62
        ^bb61:
          cf.br ^bb62
        ^bb62:
        %430 = llvm.load %417 : !llvm.ptr -> i64
        %431 = arith.muli %430, %334 : i64
        %432 = llvm.mlir.addressof @MOD : !llvm.ptr
        %433 = llvm.load %432 : !llvm.ptr -> i64
        %434 = arith.remsi %431, %433 : i64
        llvm.store %434, %417 : i64, !llvm.ptr
        %436 = llvm.load %354 : !llvm.ptr -> i32
        %437 = llvm.load %352 : !llvm.ptr -> i32
        %438 = arith.addi %436, %437 : i32
        %439 = arith.constant 1 : i32
        %440 = arith.subi %438, %439 : i32
        %441 = llvm.load %352 : !llvm.ptr -> i32
        %442 = arith.constant 1 : i32
        %443 = arith.addi %441, %442 : i32
        %435 = func.call @nCk(%440, %443) : (i32, i32) -> i64
        %444 = arith.constant 2 : i32
        %445 = llvm.load %417 : !llvm.ptr -> i64
        %447 = arith.extsi %444 : i32 to i64
        %446 = arith.muli %447, %445 : i64
        %448 = arith.subi %446, %435 : i64
        %449 = llvm.mlir.addressof @MOD : !llvm.ptr
        %450 = llvm.load %449 : !llvm.ptr -> i64
        %451 = arith.remsi %448, %450 : i64
        llvm.store %451, %372 : i64, !llvm.ptr
        %452 = llvm.load %372 : !llvm.ptr -> i64
        %453 = arith.constant 0 : i32
        %455 = arith.extsi %453 : i32 to i64
        %454 = arith.cmpi slt, %452, %455 : i64
        cf.cond_br %454, ^bb63, ^bb64
        ^bb63:
          %456 = llvm.load %372 : !llvm.ptr -> i64
          %457 = llvm.mlir.addressof @MOD : !llvm.ptr
          %458 = llvm.load %457 : !llvm.ptr -> i64
          %459 = arith.addi %456, %458 : i64
          llvm.store %459, %372 : i64, !llvm.ptr
          cf.br ^bb65
        ^bb64:
          cf.br ^bb65
        ^bb65:
        %460 = llvm.load %352 : !llvm.ptr -> i32
        %461 = arith.constant 1 : i32
        %462 = arith.addi %460, %461 : i32
        llvm.store %462, %352 : i32, !llvm.ptr
        %463 = llvm.load %354 : !llvm.ptr -> i32
        %464 = arith.constant 2 : i32
        %465 = arith.subi %463, %464 : i32
        llvm.store %465, %354 : i32, !llvm.ptr
        cf.br ^bb51
      ^bb53:
      cf.br ^bb50
    ^bb49:
      cf.br ^bb50
    ^bb50:
    %466 = arith.constant 5 : i32
    %467 = arith.subi %arg1, %466 : i32
    %468 = arith.constant 4 : i32
    %469 = arith.cmpi slt, %468, %arg1 : i32
    %470 = scf.if %469 -> (i1) {
      %471 = arith.constant 0 : i32
      %472 = arith.cmpi sge, %467, %471 : i32
      scf.yield %472 : i1
    } else {
      %473 = arith.constant false
      scf.yield %473 : i1
    }
    cf.cond_br %470, ^bb66, ^bb67
    ^bb66:
      %474 = arith.constant 0 : i32
      %475 = llvm.mlir.constant(1 : i64) : i64
      %476 = llvm.alloca %475 x i32 : (i64) -> !llvm.ptr
      llvm.store %474, %476 : i32, !llvm.ptr
      %477 = llvm.mlir.constant(1 : i64) : i64
      %478 = llvm.alloca %477 x i32 : (i64) -> !llvm.ptr
      llvm.store %467, %478 : i32, !llvm.ptr
      %480 = arith.constant 2 : i32
      %481 = llvm.load %478 : !llvm.ptr -> i32
      %482 = arith.constant 1 : i32
      %483 = arith.addi %481, %482 : i32
      %484 = arith.extsi %483 : i32 to i64
      %485 = arith.extsi %480 : i32 to i64
      %479 = func.call @mod_pow(%485, %484) : (i64, i64) -> i64
      %486 = llvm.mlir.addressof @MOD : !llvm.ptr
      %487 = llvm.load %486 : !llvm.ptr -> i64
      %488 = arith.addi %479, %487 : i64
      %489 = arith.constant 1 : i32
      %491 = arith.extsi %489 : i32 to i64
      %490 = arith.subi %488, %491 : i64
      %492 = llvm.mlir.addressof @MOD : !llvm.ptr
      %493 = llvm.load %492 : !llvm.ptr -> i64
      %494 = arith.remsi %490, %493 : i64
      %495 = llvm.mlir.constant(1 : i64) : i64
      %496 = llvm.alloca %495 x i64 : (i64) -> !llvm.ptr
      llvm.store %494, %496 : i64, !llvm.ptr
      cf.br ^bb69
      ^bb69:
      %497 = arith.constant 1 : i1
      cf.cond_br %497, ^bb70, ^bb71
      ^bb70:
        %498 = arith.constant 2 : i32
        %499 = llvm.load %476 : !llvm.ptr -> i32
        %500 = arith.muli %498, %499 : i32
        %501 = arith.constant 4 : i32
        %502 = arith.addi %500, %501 : i32
        %503 = arith.cmpi sge, %502, %arg1 : i32
        %504 = scf.if %503 -> (i1) {
          %505 = arith.constant true
          scf.yield %505 : i1
        } else {
          %506 = llvm.load %478 : !llvm.ptr -> i32
          %507 = arith.constant 0 : i32
          %508 = arith.cmpi slt, %506, %507 : i32
          scf.yield %508 : i1
        }
        cf.cond_br %504, ^bb72, ^bb73
        ^bb72:
          cf.br ^bb71
        ^bb73:
          cf.br ^bb74
        ^bb74:
        %509 = llvm.load %496 : !llvm.ptr -> i64
        %511 = llvm.load %478 : !llvm.ptr -> i32
        %512 = llvm.load %476 : !llvm.ptr -> i32
        %510 = func.call @Q_of(%511, %512) : (i32, i32) -> i64
        %513 = arith.addi %509, %510 : i64
        %514 = llvm.mlir.addressof @MOD : !llvm.ptr
        %515 = llvm.load %514 : !llvm.ptr -> i64
        %516 = arith.remsi %513, %515 : i64
        %517 = arith.extsi %502 : i32 to i64
        %518 = llvm.getelementptr %arg0[%517] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %516, %518 : i64, !llvm.ptr
        %519 = llvm.load %478 : !llvm.ptr -> i32
        %520 = arith.constant 2 : i32
        %521 = arith.cmpi slt, %519, %520 : i32
        cf.cond_br %521, ^bb75, ^bb76
        ^bb75:
          cf.br ^bb71
        ^bb76:
          cf.br ^bb77
        ^bb77:
        %523 = llvm.load %478 : !llvm.ptr -> i32
        %524 = llvm.load %476 : !llvm.ptr -> i32
        %525 = arith.addi %523, %524 : i32
        %526 = arith.constant 1 : i32
        %527 = arith.subi %525, %526 : i32
        %528 = llvm.load %476 : !llvm.ptr -> i32
        %522 = func.call @nCk(%527, %528) : (i32, i32) -> i64
        %530 = llvm.load %478 : !llvm.ptr -> i32
        %531 = llvm.load %476 : !llvm.ptr -> i32
        %532 = arith.addi %530, %531 : i32
        %533 = llvm.load %476 : !llvm.ptr -> i32
        %529 = func.call @nCk(%532, %533) : (i32, i32) -> i64
        %534 = llvm.load %496 : !llvm.ptr -> i64
        %535 = arith.constant 2 : i32
        %537 = arith.extsi %535 : i32 to i64
        %536 = arith.muli %537, %522 : i64
        %538 = arith.subi %534, %536 : i64
        %539 = arith.subi %538, %529 : i64
        %540 = llvm.mlir.constant(1 : i64) : i64
        %541 = llvm.alloca %540 x i64 : (i64) -> !llvm.ptr
        llvm.store %539, %541 : i64, !llvm.ptr
        %542 = llvm.load %541 : !llvm.ptr -> i64
        %543 = llvm.mlir.addressof @MOD : !llvm.ptr
        %544 = llvm.load %543 : !llvm.ptr -> i64
        %545 = arith.remsi %542, %544 : i64
        llvm.store %545, %541 : i64, !llvm.ptr
        %546 = llvm.load %541 : !llvm.ptr -> i64
        %547 = arith.constant 0 : i32
        %549 = arith.extsi %547 : i32 to i64
        %548 = arith.cmpi slt, %546, %549 : i64
        cf.cond_br %548, ^bb78, ^bb79
        ^bb78:
          %550 = llvm.load %541 : !llvm.ptr -> i64
          %551 = llvm.mlir.addressof @MOD : !llvm.ptr
          %552 = llvm.load %551 : !llvm.ptr -> i64
          %553 = arith.addi %550, %552 : i64
          llvm.store %553, %541 : i64, !llvm.ptr
          cf.br ^bb80
        ^bb79:
          cf.br ^bb80
        ^bb80:
        %554 = llvm.load %541 : !llvm.ptr -> i64
        %555 = arith.muli %554, %334 : i64
        %556 = llvm.mlir.addressof @MOD : !llvm.ptr
        %557 = llvm.load %556 : !llvm.ptr -> i64
        %558 = arith.remsi %555, %557 : i64
        llvm.store %558, %541 : i64, !llvm.ptr
        %560 = llvm.load %478 : !llvm.ptr -> i32
        %561 = llvm.load %476 : !llvm.ptr -> i32
        %562 = arith.addi %560, %561 : i32
        %563 = arith.constant 1 : i32
        %564 = arith.subi %562, %563 : i32
        %565 = llvm.load %476 : !llvm.ptr -> i32
        %566 = arith.constant 1 : i32
        %567 = arith.addi %565, %566 : i32
        %559 = func.call @nCk(%564, %567) : (i32, i32) -> i64
        %568 = arith.constant 2 : i32
        %569 = llvm.load %541 : !llvm.ptr -> i64
        %571 = arith.extsi %568 : i32 to i64
        %570 = arith.muli %571, %569 : i64
        %572 = arith.subi %570, %559 : i64
        %573 = llvm.mlir.addressof @MOD : !llvm.ptr
        %574 = llvm.load %573 : !llvm.ptr -> i64
        %575 = arith.remsi %572, %574 : i64
        llvm.store %575, %496 : i64, !llvm.ptr
        %576 = llvm.load %496 : !llvm.ptr -> i64
        %577 = arith.constant 0 : i32
        %579 = arith.extsi %577 : i32 to i64
        %578 = arith.cmpi slt, %576, %579 : i64
        cf.cond_br %578, ^bb81, ^bb82
        ^bb81:
          %580 = llvm.load %496 : !llvm.ptr -> i64
          %581 = llvm.mlir.addressof @MOD : !llvm.ptr
          %582 = llvm.load %581 : !llvm.ptr -> i64
          %583 = arith.addi %580, %582 : i64
          llvm.store %583, %496 : i64, !llvm.ptr
          cf.br ^bb83
        ^bb82:
          cf.br ^bb83
        ^bb83:
        %584 = llvm.load %476 : !llvm.ptr -> i32
        %585 = arith.constant 1 : i32
        %586 = arith.addi %584, %585 : i32
        llvm.store %586, %476 : i32, !llvm.ptr
        %587 = llvm.load %478 : !llvm.ptr -> i32
        %588 = arith.constant 2 : i32
        %589 = arith.subi %587, %588 : i32
        llvm.store %589, %478 : i32, !llvm.ptr
        cf.br ^bb69
      ^bb71:
      cf.br ^bb68
    ^bb67:
      cf.br ^bb68
    ^bb68:
    func.return
  }
  func.func @main() -> i32 {
    %590 = arith.constant 10000000 : i32
    %591 = arith.constant 10000000 : i32
    %592 = arith.constant 0 : i32
    %593 = arith.cmpi eq, %590, %592 : i32
    cf.cond_br %593, ^bb84, ^bb85
    ^bb84:
      %594 = llvm.mlir.addressof @str_0 : !llvm.ptr
      %595 = llvm.call @printf(%594) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr) -> i32
      %596 = arith.constant 0 : i32
      func.return %596 : i32
    ^bb85:
      cf.br ^bb86
    ^bb86:
    %597 = arith.constant 1 : i32
    %598 = llvm.mlir.constant(1 : i64) : i64
    %599 = llvm.alloca %598 x i32 : (i64) -> !llvm.ptr
    llvm.store %597, %599 : i32, !llvm.ptr
    cf.br ^bb87
    ^bb87:
    %600 = llvm.load %599 : !llvm.ptr -> i32
    %601 = arith.cmpi slt, %600, %590 : i32
    cf.cond_br %601, ^bb88, ^bb89
    ^bb88:
      %602 = llvm.load %599 : !llvm.ptr -> i32
      %603 = arith.constant 1 : i32
      %604 = arith.shli %602, %603 : i32
      llvm.store %604, %599 : i32, !llvm.ptr
      cf.br ^bb87
    ^bb89:
    %606 = llvm.load %599 : !llvm.ptr -> i32
    %607 = arith.extsi %606 : i32 to i64
    %608 = arith.constant 8 : i32
    %609 = arith.extsi %608 : i32 to i64
    %605 = func.call @calloc(%607, %609) : (i64, i64) -> !llvm.ptr
    %611 = llvm.load %599 : !llvm.ptr -> i32
    func.call @build_single_suit(%605, %590, %611) : (!llvm.ptr, i32, i32) -> ()
    %613 = llvm.mlir.addressof @fact : !llvm.ptr
    %614 = llvm.load %613 : !llvm.ptr -> !llvm.ptr
    func.call @free(%614) : (!llvm.ptr) -> ()
    %616 = llvm.mlir.addressof @inv_fact : !llvm.ptr
    %617 = llvm.load %616 : !llvm.ptr -> !llvm.ptr
    func.call @free(%617) : (!llvm.ptr) -> ()
    %619 = llvm.load %599 : !llvm.ptr -> i32
    func.call @fwht_xor(%605, %619) : (!llvm.ptr, i32) -> ()
    %620 = arith.constant 0 : i32
    %621 = arith.extsi %620 : i32 to i64
    %622 = llvm.mlir.constant(1 : i64) : i64
    %623 = llvm.alloca %622 x i64 : (i64) -> !llvm.ptr
    llvm.store %621, %623 : i64, !llvm.ptr
    %624 = arith.constant 0 : i32
    %625 = llvm.mlir.constant(1 : i64) : i64
    %626 = llvm.alloca %625 x i32 : (i64) -> !llvm.ptr
    llvm.store %624, %626 : i32, !llvm.ptr
    cf.br ^bb90
    ^bb90:
    %627 = llvm.load %626 : !llvm.ptr -> i32
    %628 = llvm.load %599 : !llvm.ptr -> i32
    %629 = arith.cmpi slt, %627, %628 : i32
    cf.cond_br %629, ^bb91, ^bb92
    ^bb91:
      %630 = llvm.load %623 : !llvm.ptr -> i64
      %633 = llvm.load %626 : !llvm.ptr -> i32
      %634 = arith.extsi %633 : i32 to i64
      %635 = llvm.getelementptr %605[%634] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %632 = llvm.load %635 : !llvm.ptr -> i64
      %636 = arith.extsi %591 : i32 to i64
      %631 = func.call @mod_pow(%632, %636) : (i64, i64) -> i64
      %637 = arith.addi %630, %631 : i64
      llvm.store %637, %623 : i64, !llvm.ptr
      %638 = llvm.load %626 : !llvm.ptr -> i32
      %639 = arith.constant 8192 : i32
      %640 = arith.remsi %638, %639 : i32
      %641 = arith.constant 0 : i32
      %642 = arith.cmpi eq, %640, %641 : i32
      cf.cond_br %642, ^bb93, ^bb94
      ^bb93:
        %643 = llvm.load %623 : !llvm.ptr -> i64
        %644 = llvm.mlir.addressof @MOD : !llvm.ptr
        %645 = llvm.load %644 : !llvm.ptr -> i64
        %646 = arith.remsi %643, %645 : i64
        llvm.store %646, %623 : i64, !llvm.ptr
        cf.br ^bb95
      ^bb94:
        cf.br ^bb95
      ^bb95:
      %647 = llvm.load %626 : !llvm.ptr -> i32
      %648 = arith.constant 1 : i32
      %649 = arith.addi %647, %648 : i32
      llvm.store %649, %626 : i32, !llvm.ptr
      cf.br ^bb90
    ^bb92:
    %650 = llvm.load %623 : !llvm.ptr -> i64
    %651 = llvm.mlir.addressof @MOD : !llvm.ptr
    %652 = llvm.load %651 : !llvm.ptr -> i64
    %653 = arith.remsi %650, %652 : i64
    llvm.store %653, %623 : i64, !llvm.ptr
    %655 = llvm.load %599 : !llvm.ptr -> i32
    %656 = arith.extsi %655 : i32 to i64
    %657 = llvm.mlir.addressof @MOD : !llvm.ptr
    %658 = llvm.load %657 : !llvm.ptr -> i64
    %659 = arith.constant 2 : i32
    %661 = arith.extsi %659 : i32 to i64
    %660 = arith.subi %658, %661 : i64
    %654 = func.call @mod_pow(%656, %660) : (i64, i64) -> i64
    %662 = llvm.load %623 : !llvm.ptr -> i64
    %663 = arith.muli %662, %654 : i64
    %664 = llvm.mlir.addressof @MOD : !llvm.ptr
    %665 = llvm.load %664 : !llvm.ptr -> i64
    %666 = arith.remsi %663, %665 : i64
    func.call @free(%605) : (!llvm.ptr) -> ()
    %668 = llvm.mlir.addressof @str_1 : !llvm.ptr
    %669 = llvm.call @printf(%668, %666) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %670 = arith.constant 0 : i32
    func.return %670 : i32
  }
}