Problem 711

Binary Blackboard — S(12345678) mod 10^9+7. Ported from native C to pure Flow.

Answer541510990
Output541510990
StatusPASS
Native helperno
Runtime40 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n log n)
Space complexityO(1)O(n)
ApproachFlow solutionModular DP or matrix exponentiation
VerdictOptimal

Flow source

# Project Euler 711
# Binary Blackboard — S(12345678) mod 10^9+7.
# Ported from native C to pure Flow.

const MOD: i64 = 1000000007

function modpow(b0: i64, e0: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = b0 % MOD
    let mut e: i64 = e0
    while e > 0 {
        if (e & 1) == 1 {
            r = r * b % MOD
        }
        b = b * b % MOD
        e = e >> 1
    }
    return r
}

function sum_pows_8(k: i64) -> i64 {
    if k <= 0 {
        return 0
    }
    return (modpow(8, k) - 1 + MOD) % MOD * modpow(7, MOD - 2) % MOD
}

function sum_pows_4_1_to_m(m: i64) -> i64 {
    if m <= 0 {
        return 0
    }
    return (modpow(4, m + 1) - 4 + MOD) % MOD * modpow(3, MOD - 2) % MOD
}

function sum_T_upto(K: i64) -> i64 {
    if K <= 0 {
        return 0
    }
    let mut T: i64 = 0
    let mut s: i64 = 0
    let mut pow2: i64 = 1
    let mut pow4: i64 = 1
    for i in 0..K {
        let add: i64 = (pow2 + 2) % MOD * pow4 % MOD
        T = (T + T + add) % MOD
        s = s + T
        if s >= MOD {
            s = s - MOD
        }
        pow2 = (pow2 + pow2) % MOD
        pow4 = pow4 * 4 % MOD
    }
    return s
}

function S(N: i64) -> i64 {
    if N % 2 == 0 {
        let m: i64 = N / 2
        let A: i64 = (sum_pows_8(m) + sum_T_upto(m - 1)) % MOD
        let B: i64 = (sum_pows_4_1_to_m(m) - m % MOD + MOD) % MOD
        return (A + modpow(4, m) + B) % MOD
    } else {
        let m: i64 = (N - 1) / 2
        let A: i64 = (sum_pows_8(m + 1) + sum_T_upto(m)) % MOD
        let B: i64 = (sum_pows_4_1_to_m(m) - m % MOD + MOD) % MOD
        return (A + B) % MOD
    }
}

function main() -> i32 {
    printf("%lld\n", S(12345678))
    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 modpow_i64_i64(int64_t b0, int64_t e0);
int64_t sum_pows_8_i64(int64_t k);
int64_t sum_pows_4_1_to_m_i64(int64_t m);
int64_t sum_T_upto_i64(int64_t K);
int64_t S_i64(int64_t N);
int32_t main(void);

static const int64_t MOD = 1000000007;

int64_t modpow_i64_i64(int64_t b0, int64_t e0) {
    int64_t r = 1;
    int64_t b = FLOW_CHECKED_MOD((b0), (MOD));
    int64_t e = e0;
    while (e > 0) {
        if ((e & 1) == 1) {
            r = FLOW_CHECKED_MOD(((r * b)), (MOD));
        }
        b = FLOW_CHECKED_MOD(((b * b)), (MOD));
        e = FLOW_CHECKED_SHR((e), (1));
    }
    return r;
}

int64_t sum_pows_8_i64(int64_t k) {
    if (k <= 0) {
        return 0;
    }
    return FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((((modpow_i64_i64(8, k) - 1) + MOD)), (MOD)) * modpow_i64_i64(7, (MOD - 2)))), (MOD));
}

int64_t sum_pows_4_1_to_m_i64(int64_t m) {
    if (m <= 0) {
        return 0;
    }
    return FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((((modpow_i64_i64(4, (m + 1)) - 4) + MOD)), (MOD)) * modpow_i64_i64(3, (MOD - 2)))), (MOD));
}

int64_t sum_T_upto_i64(int64_t K) {
    if (K <= 0) {
        return 0;
    }
    int64_t T = 0;
    int64_t s = 0;
    int64_t pow2 = 1;
    int64_t pow4 = 1;
    int32_t __flow_step_1 = 1;
    for (int32_t i = 0; (0 <= K) ? i < K : i > K; i += (0 <= K) ? 1 : -1) {
        int64_t add = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((pow2 + 2)), (MOD)) * pow4)), (MOD));
        T = FLOW_CHECKED_MOD((((T + T) + add)), (MOD));
        s = (s + T);
        if (s >= MOD) {
            s = (s - MOD);
        }
        pow2 = FLOW_CHECKED_MOD(((pow2 + pow2)), (MOD));
        pow4 = FLOW_CHECKED_MOD(((pow4 * 4)), (MOD));
    }
    return s;
}

int64_t S_i64(int64_t N) {
    if (FLOW_CHECKED_MOD((N), (2)) == 0) {
        int64_t m = FLOW_CHECKED_DIV((N), (2));
        int64_t A = FLOW_CHECKED_MOD(((sum_pows_8_i64(m) + sum_T_upto_i64((m - 1)))), (MOD));
        int64_t B = FLOW_CHECKED_MOD((((sum_pows_4_1_to_m_i64(m) - FLOW_CHECKED_MOD((m), (MOD))) + MOD)), (MOD));
        return FLOW_CHECKED_MOD((((A + modpow_i64_i64(4, m)) + B)), (MOD));
    } else {
        int64_t m = FLOW_CHECKED_DIV(((N - 1)), (2));
        int64_t A = FLOW_CHECKED_MOD(((sum_pows_8_i64((m + 1)) + sum_T_upto_i64(m))), (MOD));
        int64_t B = FLOW_CHECKED_MOD((((sum_pows_4_1_to_m_i64(m) - FLOW_CHECKED_MOD((m), (MOD))) + MOD)), (MOD));
        return FLOW_CHECKED_MOD(((A + B)), (MOD));
    }
}

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