Problem 945

XOR-Equation C: count pairs 0<=a<=b<=N where (a XOR-product b) has no even-position bits. Uses GF(2) polynomial arithmetic and digit DP.

Answer83357132
Output83357132
StatusPASS
Native helperno
Runtime240 ms
Peak memory2208 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n * d * s)
Space complexityO(1)O(d * s)
ApproachFlow solutionDigit DP
VerdictUnknown

Flow source

# Project Euler 945
# XOR-Equation C: count pairs 0<=a<=b<=N where (a XOR-product b) has no even-position bits.
# Uses GF(2) polynomial arithmetic and digit DP.

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

# 16-bit compaction lookup tables
let mut even16: ptr<i64> = null as ptr<i64>
let mut odd16: ptr<i64> = null as ptr<i64>

function clz64(x: i64) -> i32 {
    if x == 0 { return 64 }
    if x < 0 { return 0 }
    let mut n: i32 = 0
    let mut v: i64 = x
    while v != 0 {
        v = v >> 1
        n = n + 1
    }
    return 64 - n
}

function build_tables() -> void {
    even16 = calloc(65536, 8)
    odd16 = calloc(65536, 8)
    for x in 0..65536 {
        let mut e: i64 = 0
        let mut o: i64 = 0
        for i in 0..8 {
            let b: i64 = 1 << (2 * i)
            if ((x as i64) & b) != 0 { e = e | (1 << i) }
            if ((x as i64) & (b << 1)) != 0 { o = o | (1 << i) }
        }
        even16[x] = e
        odd16[x] = o
    }
}

# Split x into (E, O) where E = even-position bits compacted, O = odd-position bits compacted.
function split_u(x: i64, E: ptr<i64>, O: ptr<i64>) -> void {
    let mut e: i64 = 0
    let mut o: i64 = 0
    let mut shift: i32 = 0
    let mut val: i64 = x
    while val != 0 {
        let chunk: i64 = val & 0xFFFF
        e = e | (even16[chunk] << shift)
        o = o | (odd16[chunk] << shift)
        val = val >> 16
        if val < 0 { val = val & 0xFFFFFFFFFFFF }
        shift = shift + 8
    }
    E[0] = e
    O[0] = o
}

# Polynomial remainder a mod b over GF(2)
function gf2_mod(a: i64, b: i64) -> i64 {
    if b == 0 { return a }
    let db: i32 = 63 - clz64(b)
    let mut val: i64 = a
    while val != 0 {
        let da: i32 = 63 - clz64(val)
        if da < db { break }
        val = val ^ (b << (da - db))
    }
    return val
}

# Polynomial GCD over GF(2)
function gf2_gcd(a: i64, b: i64) -> i64 {
    let mut x: i64 = a
    let mut y: i64 = b
    while y != 0 {
        let t: i64 = gf2_mod(x, y)
        x = y
        y = t
    }
    return x
}

# Ordered solution count for [0, 2^bits - 1]
function ordered_full(bits: i32) -> i64 {
    let mut sign: i64 = 1
    if (bits & 1) != 0 { sign = -1 }
    let num: i64 = (1 << (bits + 1)) * (3 * (bits as i64) + 4) + sign
    return num / 9
}

# For b = 2^k + y, count a in [0, 2^k - 1] satisfying the condition
function count_a_for_upper(k: i32, y: i64) -> i64 {
    let m: i32 = k / 2
    let Y0: ptr<i64> = calloc(1, 8)
    let Y1: ptr<i64> = calloc(1, 8)
    split_u(y, Y0, Y1)

    let result: i64 = 0
    if (k & 1) == 0 {
        # k even: k = 2m
        if Y1[0] == 0 {
            result = 1 << m
        } else {
            let P: i64 = Y0[0] ^ (1 << m)
            let g: i64 = gf2_gcd(P, Y1[0] << 1)
            result = 1 << (63 - clz64(g))
        }
    } else {
        # k odd: k = 2m+1
        if Y0[0] == 0 {
            result = 1 << (m + 1)
        } else {
            let uQ: i64 = ((1 << m) ^ Y1[0]) << 1
            let g: i64 = gf2_gcd(Y0[0], uQ)
            result = 1 << (63 - clz64(g))
        }
    }

    free(Y0)
    free(Y1)
    return result
}

# Ordered count of pairs (a,b) with 0<=a,b<=N
function ordered_S(N: i64) -> i64 {
    if N < 0 { return 0 }
    if N == 0 { return 1 }

    let bits: i32 = 64 - clz64(N)
    let all_ones: i64 = (1 << bits) - 1
    if N == all_ones {
        return ordered_full(bits)
    }

    let k: i32 = bits - 1
    let M: i64 = 1 << k
    let r: i64 = N - M

    let base: i64 = ordered_full(k)
    let mut cross: i64 = 0
    let mut y: i64 = 0
    while y <= r {
        cross = cross + count_a_for_upper(k, y)
        y = y + 1
    }

    return base + 2 * cross
}

function main() -> i32 {
    build_tables()

    let mut N: i64 = 1
    let mut i: i32 = 0
    while i < 7 {
        N = N * 10
        i = i + 1
    }

    let s: i64 = ordered_S(N)
    printf("%lld\n", (s + 1) / 2)

    free(even16)
    free(odd16)
    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; }

int32_t clz64_i64(int64_t x);
void build_tables(void);
void split_u_i64_ptr_i64_ptr_i64(int64_t x, int64_t* E, int64_t* O);
int64_t gf2_mod_i64_i64(int64_t a, int64_t b);
int64_t gf2_gcd_i64_i64(int64_t a, int64_t b);
int64_t ordered_full_i32(int32_t bits);
int64_t count_a_for_upper_i32_i64(int32_t k, int64_t y);
int64_t ordered_S_i64(int64_t N);
int32_t main(void);

/* Module statics */
static int64_t* even16 = ((int64_t*)(NULL));
static int64_t* odd16 = ((int64_t*)(NULL));



int32_t clz64_i64(int64_t x) {
    if (x == 0) {
        return 64;
    }
    if (x < 0) {
        return 0;
    }
    int32_t n = 0;
    int64_t v = x;
    while (v != 0) {
        v = FLOW_CHECKED_SHR((v), (1));
        n = (n + 1);
    }
    return (64 - n);
}

void build_tables(void) {
    even16 = calloc(65536, 8);
    odd16 = calloc(65536, 8);
    int32_t __flow_step_1 = 1;
    for (int32_t x = 0; (0 <= 65536) ? x < 65536 : x > 65536; x += (0 <= 65536) ? 1 : -1) {
        int64_t e = 0;
        int64_t o = 0;
        int32_t __flow_step_2 = 1;
        for (int32_t i = 0; (0 <= 8) ? i < 8 : i > 8; i += (0 <= 8) ? 1 : -1) {
            int64_t b = FLOW_CHECKED_SHL((1), ((2 * i)));
            if ((((int64_t)(x)) & b) != 0) {
                e = (e | FLOW_CHECKED_SHL((1), (i)));
            }
            if ((((int64_t)(x)) & FLOW_CHECKED_SHL((b), (1))) != 0) {
                o = (o | FLOW_CHECKED_SHL((1), (i)));
            }
        }
        even16[x] = e;
        odd16[x] = o;
    }
}

void split_u_i64_ptr_i64_ptr_i64(int64_t x, int64_t* E, int64_t* O) {
    int64_t e = 0;
    int64_t o = 0;
    int32_t shift = 0;
    int64_t val = x;
    while (val != 0) {
        int64_t chunk = (val & 65535);
        e = (e | FLOW_CHECKED_SHL((even16[chunk]), (shift)));
        o = (o | FLOW_CHECKED_SHL((odd16[chunk]), (shift)));
        val = FLOW_CHECKED_SHR((val), (16));
        if (val < 0) {
            val = (val & 281474976710655);
        }
        shift = (shift + 8);
    }
    E[0] = e;
    O[0] = o;
}

int64_t gf2_mod_i64_i64(int64_t a, int64_t b) {
    if (b == 0) {
        return a;
    }
    int32_t db = (63 - clz64_i64(b));
    int64_t val = a;
    while (val != 0) {
        int32_t da = (63 - clz64_i64(val));
        if (da < db) {
            break;
        }
        val = (val ^ FLOW_CHECKED_SHL((b), ((da - db))));
    }
    return val;
}

int64_t gf2_gcd_i64_i64(int64_t a, int64_t b) {
    int64_t x = a;
    int64_t y = b;
    while (y != 0) {
        int64_t t = gf2_mod_i64_i64(x, y);
        x = y;
        y = t;
    }
    return x;
}

int64_t ordered_full_i32(int32_t bits) {
    int64_t sign = 1;
    if ((bits & 1) != 0) {
        sign = (-1);
    }
    int64_t num = ((FLOW_CHECKED_SHL((1), ((bits + 1))) * ((3 * ((int64_t)(bits))) + 4)) + sign);
    return FLOW_CHECKED_DIV((num), (9));
}

int64_t count_a_for_upper_i32_i64(int32_t k, int64_t y) {
    int32_t m = FLOW_CHECKED_DIV((k), (2));
    int64_t* Y0 = (int64_t*)(calloc(1, 8));
    int64_t* Y1 = (int64_t*)(calloc(1, 8));
    split_u_i64_ptr_i64_ptr_i64(y, Y0, Y1);
    int64_t result = 0;
    if ((k & 1) == 0) {
        if (Y1[0] == 0) {
            result = FLOW_CHECKED_SHL((1), (m));
        } else {
            int64_t P = (Y0[0] ^ FLOW_CHECKED_SHL((1), (m)));
            int64_t g = gf2_gcd_i64_i64(P, FLOW_CHECKED_SHL((Y1[0]), (1)));
            result = FLOW_CHECKED_SHL((1), ((63 - clz64_i64(g))));
        }
    } else {
        if (Y0[0] == 0) {
            result = FLOW_CHECKED_SHL((1), ((m + 1)));
        } else {
            int64_t uQ = FLOW_CHECKED_SHL(((FLOW_CHECKED_SHL((1), (m)) ^ Y1[0])), (1));
            int64_t g = gf2_gcd_i64_i64(Y0[0], uQ);
            result = FLOW_CHECKED_SHL((1), ((63 - clz64_i64(g))));
        }
    }
    free(Y0);
    free(Y1);
    return result;
}

int64_t ordered_S_i64(int64_t N) {
    if (N < 0) {
        return 0;
    }
    if (N == 0) {
        return 1;
    }
    int32_t bits = (64 - clz64_i64(N));
    int64_t all_ones = (FLOW_CHECKED_SHL((1), (bits)) - 1);
    if (N == all_ones) {
        return ordered_full_i32(bits);
    }
    int32_t k = (bits - 1);
    int64_t M = FLOW_CHECKED_SHL((1), (k));
    int64_t r = (N - M);
    int64_t base = ordered_full_i32(k);
    int64_t cross = 0;
    int64_t y = 0;
    while (y <= r) {
        cross = (cross + count_a_for_upper_i32_i64(k, y));
        y = (y + 1);
    }
    return (base + (2 * cross));
}

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