Problem 878

XOR-Equation B — count orbits with invariant <= 1e6 and b <= 1e17.

Answer23707109
Output23707109
StatusPASS
Native helperno
Runtime120 ms
Peak memory1120 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n * k)
Space complexityO(n)O(n)
ApproachFlow solutionKey search and XOR decryption
VerdictUnknown

Flow source

# Project Euler 878
# XOR-Equation B — count orbits with invariant <= 1e6 and b <= 1e17.

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

function clmul(x0: i64, y0: i64) -> i64 {
    let mut x: i64 = x0
    let mut y: i64 = y0
    if x == 0 || y == 0 { return 0 }
    let mut bx: i64 = 0
    let mut by: i64 = 0
    let mut t: i64 = x
    while t > 0 {
        bx = bx + (t & 1)
        t = t >> 1
    }
    t = y
    while t > 0 {
        by = by + (t & 1)
        t = t >> 1
    }
    if bx < by {
        let tmp: i64 = x
        x = y
        y = tmp
    }
    let mut result: i64 = 0
    while y != 0 {
        let bit: i64 = y & -y
        let mut sh: i64 = 0
        let mut b: i64 = bit
        while b > 1 {
            b = b >> 1
            sh = sh + 1
        }
        result = result ^ (x << sh)
        y = y ^ bit
    }
    return result
}

function count_forward(a0: i64, b0: i64, limit: i64) -> i64 {
    let mut a: i64 = a0
    let mut b: i64 = b0
    let mut total: i64 = 0
    while b <= limit {
        total = total + 1
        let na: i64 = b
        let nb: i64 = a ^ (b << 1)
        a = na
        b = nb
    }
    return total
}

function G(limit: i64, max_value: i64) -> i64 {
    if max_value == 0 { return 1 }
    let mut bl: i64 = 0
    let mut mv: i64 = max_value
    while mv > 0 {
        bl = bl + 1
        mv = mv >> 1
    }
    let seed_limit: i64 = 1 << ((bl + 2) / 2)
    let squares: ptr<i64> = calloc(seed_limit, 8)
    let mut x: i64 = 0
    while x < seed_limit {
        squares[x] = clmul(x, x)
        x = x + 1
    }
    let mut total: i64 = 1
    let mut a: i64 = 0
    while a < seed_limit {
        let square_a: i64 = squares[a]
        let mut b: i64 = a
        if b < 1 { b = 1 }
        while b < seed_limit {
            let invariant: i64 = square_a ^ squares[b] ^ (clmul(a, b) << 1)
            if invariant <= max_value {
                if (b ^ (a << 1)) > a {
                    total = total + count_forward(a, b, limit)
                }
            }
            b = b + 1
        }
        a = a + 1
    }
    free(squares)
    return total
}

function main() -> i32 {
    printf("%lld\n", G(100000000000000000, 1000000))
    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 clmul_i64_i64(int64_t x0, int64_t y0);
int64_t count_forward_i64_i64_i64(int64_t a0, int64_t b0, int64_t limit);
int64_t G_i64_i64(int64_t limit, int64_t max_value);
int32_t main(void);



int64_t clmul_i64_i64(int64_t x0, int64_t y0) {
    int64_t x = x0;
    int64_t y = y0;
    if ((x == 0 || y == 0)) {
        return 0;
    }
    int64_t bx = 0;
    int64_t by = 0;
    int64_t t = x;
    while (t > 0) {
        bx = (bx + (t & 1));
        t = FLOW_CHECKED_SHR((t), (1));
    }
    t = y;
    while (t > 0) {
        by = (by + (t & 1));
        t = FLOW_CHECKED_SHR((t), (1));
    }
    if (bx < by) {
        int64_t tmp = x;
        x = y;
        y = tmp;
    }
    int64_t result = 0;
    while (y != 0) {
        int64_t bit = (y & (-y));
        int64_t sh = 0;
        int64_t b = bit;
        while (b > 1) {
            b = FLOW_CHECKED_SHR((b), (1));
            sh = (sh + 1);
        }
        result = (result ^ FLOW_CHECKED_SHL((x), (sh)));
        y = (y ^ bit);
    }
    return result;
}

int64_t count_forward_i64_i64_i64(int64_t a0, int64_t b0, int64_t limit) {
    int64_t a = a0;
    int64_t b = b0;
    int64_t total = 0;
    while (b <= limit) {
        total = (total + 1);
        int64_t na = b;
        int64_t nb = (a ^ FLOW_CHECKED_SHL((b), (1)));
        a = na;
        b = nb;
    }
    return total;
}

int64_t G_i64_i64(int64_t limit, int64_t max_value) {
    if (max_value == 0) {
        return 1;
    }
    int64_t bl = 0;
    int64_t mv = max_value;
    while (mv > 0) {
        bl = (bl + 1);
        mv = FLOW_CHECKED_SHR((mv), (1));
    }
    int64_t seed_limit = FLOW_CHECKED_SHL((1), (FLOW_CHECKED_DIV(((bl + 2)), (2))));
    int64_t* squares = (int64_t*)(calloc(seed_limit, 8));
    int64_t x = 0;
    while (x < seed_limit) {
        squares[x] = clmul_i64_i64(x, x);
        x = (x + 1);
    }
    int64_t total = 1;
    int64_t a = 0;
    while (a < seed_limit) {
        int64_t square_a = squares[a];
        int64_t b = a;
        if (b < 1) {
            b = 1;
        }
        while (b < seed_limit) {
            int64_t invariant = ((square_a ^ squares[b]) ^ FLOW_CHECKED_SHL((clmul_i64_i64(a, b)), (1)));
            if (invariant <= max_value) {
                if ((b ^ FLOW_CHECKED_SHL((a), (1))) > a) {
                    total = (total + count_forward_i64_i64_i64(a, b, limit));
                }
            }
            b = (b + 1);
        }
        a = (a + 1);
    }
    free(squares);
    return total;
}

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