Problem 999

Rescaled elliptic divisibility sequence: a_n mod 1234567891. Uses the doubling recurrence for the EDS block (W_{n-3}..W_{n+4}) in O(log n), then rescales by INV_TWO^(n^2/4).

Answer801096743
Output801096743
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n log n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 999
# Rescaled elliptic divisibility sequence: a_n mod 1234567891.
# Uses the doubling recurrence for the EDS block (W_{n-3}..W_{n+4})
# in O(log n), then rescales by INV_TWO^(n^2/4).

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

const MOD: i64 = 1234567891
const INV_TWO: i64 = 617283946

function norm(x: i64) -> i64 {
    let mut r: i64 = x % MOD
    if r < 0 { r = r + MOD }
    return r
}

function mod_mul(a: i64, b: i64) -> i64 {
    return ((norm(a) as i128) * norm(b) % MOD) as i64
}

function mod_sub(a: i64, b: i64) -> i64 {
    let mut r: i64 = norm(a) - norm(b)
    if r < 0 { r = r + MOD }
    return r
}

function mod_pow_generic(base: i64, exp: i128) -> i64 {
    let mut b: i64 = norm(base)
    let mut result: i64 = 1
    let mut e: i128 = exp
    while e > 0 {
        if (e & 1) != 0 {
            result = mod_mul(result, b)
        }
        b = mod_mul(b, b)
        e = e >> 1
    }
    return result
}

function small_w(index: i64) -> i64 {
    if index < 0 { return norm(-small_w(-index)) }
    if index == 0 { return norm(0) }
    if index == 1 { return norm(1) }
    if index == 2 { return norm(2) }
    if index == 3 { return norm(-4) }
    if index == 4 { return norm(-32) }
    if index == 5 { return norm(-192) }
    if index == 6 { return norm(3584) }
    if index == 7 { return norm(77824) }
    return norm(262144)
}

# W_{2*index - 1} from terms around W_index.
function odd_val(s: ptr<i64>, start: i64, index: i64) -> i64 {
    let a: i64 = s[index + 1 - start]
    let b: i64 = mod_pow_generic(s[index - 1 - start], 3 as i128)
    let c: i64 = s[index - 2 - start]
    let d: i64 = mod_pow_generic(s[index - start], 3 as i128)
    return mod_sub(mod_mul(a, b), mod_mul(c, d))
}

# W_{2*index}; division is only by the fixed W_2 = 2.
function even_val(s: ptr<i64>, start: i64, index: i64) -> i64 {
    let a: i64 = s[index - start]
    let p1: i64 = mod_pow_generic(s[index - 1 - start], 2 as i128)
    let c2: i64 = s[index + 2 - start]
    let p2: i64 = mod_pow_generic(s[index + 1 - start], 2 as i128)
    let e2: i64 = s[index - 2 - start]
    let inner: i64 = mod_sub(mod_mul(c2, p1), mod_mul(e2, p2))
    return mod_mul(mod_mul(a, INV_TWO), inner)
}

# Return (W_{n-3}, ..., W_{n+4}) mod MOD as a malloc'd ptr<i64> of size 8.
function eds_block(n: i64) -> ptr<i64> {
    let r: ptr<i64> = malloc(64) as ptr<i64>
    if n <= 4 {
        for i in 0..8 {
            r[i] = small_w(n - 3 + (i as i64))
        }
        return r
    }
    let middle: i64 = n / 2
    let s: ptr<i64> = eds_block(middle)
    let start: i64 = middle - 3
    if n % 2 == 0 {
        r[0] = odd_val(s, start, middle - 1)
        r[1] = even_val(s, start, middle - 1)
        r[2] = odd_val(s, start, middle)
        r[3] = even_val(s, start, middle)
        r[4] = odd_val(s, start, middle + 1)
        r[5] = even_val(s, start, middle + 1)
        r[6] = odd_val(s, start, middle + 2)
        r[7] = even_val(s, start, middle + 2)
    } else {
        r[0] = even_val(s, start, middle - 1)
        r[1] = odd_val(s, start, middle)
        r[2] = even_val(s, start, middle)
        r[3] = odd_val(s, start, middle + 1)
        r[4] = even_val(s, start, middle + 1)
        r[5] = odd_val(s, start, middle + 2)
        r[6] = even_val(s, start, middle + 2)
        r[7] = odd_val(s, start, middle + 3)
    }
    free(s as ptr<void>)
    return r
}

function w_mod(n: i64) -> i64 {
    if n < 0 { return norm(-w_mod(-n)) }
    let blk: ptr<i64> = eds_block(n)
    let result: i64 = blk[3]
    free(blk as ptr<void>)
    return result
}

function a_mod(n: i64) -> i64 {
    let rem: i64 = n % 4
    let mut sign: i64 = -1
    if rem == 1 || rem == 2 { sign = 1 }
    let nn: i128 = (n as i128) * (n as i128)
    let exp: i128 = nn / 4
    let inverse_scale: i64 = mod_pow_generic(INV_TWO, exp)
    let w: i64 = w_mod(n)
    let sw: i64 = norm(sign * w)
    return mod_mul(sw, inverse_scale)
}

function main() -> i32 {
    let result: i64 = a_mod(1000000000000000003)
    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 norm_i64(int64_t x);
int64_t mod_mul_i64_i64(int64_t a, int64_t b);
int64_t mod_sub_i64_i64(int64_t a, int64_t b);
int64_t mod_pow_generic_i64_i128(int64_t base, __int128 exp);
int64_t small_w_i64(int64_t index);
int64_t odd_val_ptr_i64_i64_i64(int64_t* s, int64_t start, int64_t index);
int64_t even_val_ptr_i64_i64_i64(int64_t* s, int64_t start, int64_t index);
int64_t* eds_block_i64(int64_t n);
int64_t w_mod_i64(int64_t n);
int64_t a_mod_i64(int64_t n);
int32_t main(void);

static const int64_t MOD = 1234567891;
static const int64_t INV_TWO = 617283946;



int64_t norm_i64(int64_t x) {
    int64_t r = FLOW_CHECKED_MOD((x), (MOD));
    if (r < 0) {
        r = (r + MOD);
    }
    return r;
}

int64_t mod_mul_i64_i64(int64_t a, int64_t b) {
    return ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(norm_i64(a))) * norm_i64(b))), (MOD))));
}

int64_t mod_sub_i64_i64(int64_t a, int64_t b) {
    int64_t r = (norm_i64(a) - norm_i64(b));
    if (r < 0) {
        r = (r + MOD);
    }
    return r;
}

int64_t mod_pow_generic_i64_i128(int64_t base, __int128 exp) {
    int64_t b = norm_i64(base);
    int64_t result = 1;
    __int128 e = exp;
    while (e > 0) {
        if ((e & 1) != 0) {
            result = mod_mul_i64_i64(result, b);
        }
        b = mod_mul_i64_i64(b, b);
        e = FLOW_CHECKED_SHR((e), (1));
    }
    return result;
}

int64_t small_w_i64(int64_t index) {
    if (index < 0) {
        return norm_i64((-small_w_i64((-index))));
    }
    if (index == 0) {
        return norm_i64(0);
    }
    if (index == 1) {
        return norm_i64(1);
    }
    if (index == 2) {
        return norm_i64(2);
    }
    if (index == 3) {
        return norm_i64((-4));
    }
    if (index == 4) {
        return norm_i64((-32));
    }
    if (index == 5) {
        return norm_i64((-192));
    }
    if (index == 6) {
        return norm_i64(3584);
    }
    if (index == 7) {
        return norm_i64(77824);
    }
    return norm_i64(262144);
}

int64_t odd_val_ptr_i64_i64_i64(int64_t* s, int64_t start, int64_t index) {
    int64_t a = s[((index + 1) - start)];
    int64_t b = mod_pow_generic_i64_i128(s[((index - 1) - start)], ((__int128)(3)));
    int64_t c = s[((index - 2) - start)];
    int64_t d = mod_pow_generic_i64_i128(s[(index - start)], ((__int128)(3)));
    return mod_sub_i64_i64(mod_mul_i64_i64(a, b), mod_mul_i64_i64(c, d));
}

int64_t even_val_ptr_i64_i64_i64(int64_t* s, int64_t start, int64_t index) {
    int64_t a = s[(index - start)];
    int64_t p1 = mod_pow_generic_i64_i128(s[((index - 1) - start)], ((__int128)(2)));
    int64_t c2 = s[((index + 2) - start)];
    int64_t p2 = mod_pow_generic_i64_i128(s[((index + 1) - start)], ((__int128)(2)));
    int64_t e2 = s[((index - 2) - start)];
    int64_t inner = mod_sub_i64_i64(mod_mul_i64_i64(c2, p1), mod_mul_i64_i64(e2, p2));
    return mod_mul_i64_i64(mod_mul_i64_i64(a, INV_TWO), inner);
}

int64_t* eds_block_i64(int64_t n) {
    int64_t* r = (int64_t*)(((int64_t*)(malloc(64))));
    if (n <= 4) {
        int32_t __flow_step_1 = 1;
        for (int32_t i = 0; (0 <= 8) ? i < 8 : i > 8; i += (0 <= 8) ? 1 : -1) {
            r[i] = small_w_i64(((n - 3) + ((int64_t)(i))));
        }
        return r;
    }
    int64_t middle = FLOW_CHECKED_DIV((n), (2));
    int64_t* s = (int64_t*)(eds_block_i64(middle));
    int64_t start = (middle - 3);
    if (FLOW_CHECKED_MOD((n), (2)) == 0) {
        r[0] = odd_val_ptr_i64_i64_i64(s, start, (middle - 1));
        r[1] = even_val_ptr_i64_i64_i64(s, start, (middle - 1));
        r[2] = odd_val_ptr_i64_i64_i64(s, start, middle);
        r[3] = even_val_ptr_i64_i64_i64(s, start, middle);
        r[4] = odd_val_ptr_i64_i64_i64(s, start, (middle + 1));
        r[5] = even_val_ptr_i64_i64_i64(s, start, (middle + 1));
        r[6] = odd_val_ptr_i64_i64_i64(s, start, (middle + 2));
        r[7] = even_val_ptr_i64_i64_i64(s, start, (middle + 2));
    } else {
        r[0] = even_val_ptr_i64_i64_i64(s, start, (middle - 1));
        r[1] = odd_val_ptr_i64_i64_i64(s, start, middle);
        r[2] = even_val_ptr_i64_i64_i64(s, start, middle);
        r[3] = odd_val_ptr_i64_i64_i64(s, start, (middle + 1));
        r[4] = even_val_ptr_i64_i64_i64(s, start, (middle + 1));
        r[5] = odd_val_ptr_i64_i64_i64(s, start, (middle + 2));
        r[6] = even_val_ptr_i64_i64_i64(s, start, (middle + 2));
        r[7] = odd_val_ptr_i64_i64_i64(s, start, (middle + 3));
    }
    free(((void*)(s)));
    return r;
}

int64_t w_mod_i64(int64_t n) {
    if (n < 0) {
        return norm_i64((-w_mod_i64((-n))));
    }
    int64_t* blk = (int64_t*)(eds_block_i64(n));
    int64_t result = blk[3];
    free(((void*)(blk)));
    return result;
}

int64_t a_mod_i64(int64_t n) {
    int64_t rem = FLOW_CHECKED_MOD((n), (4));
    int64_t sign = (-1);
    if ((rem == 1 || rem == 2)) {
        sign = 1;
    }
    __int128 nn = (((__int128)(n)) * ((__int128)(n)));
    __int128 exp = FLOW_CHECKED_DIV((nn), (4));
    int64_t inverse_scale = mod_pow_generic_i64_i128(INV_TWO, exp);
    int64_t w = w_mod_i64(n);
    int64_t sw = norm_i64((sign * w));
    return mod_mul_i64_i64(sw, inverse_scale);
}

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