Problem 435

sum_{x=0..100} F_n(x) mod 15! for n=10^15. F_n(x) = (f_n x^{n+2} + f_{n+1} x^{n+1} - x) / (x^2 + x - 1)

Answer252541322550
Output252541322550
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 435
# sum_{x=0..100} F_n(x) mod 15! for n=10^15.
# F_n(x) = (f_n x^{n+2} + f_{n+1} x^{n+1} - x) / (x^2 + x - 1)

function modmul(a: i64, b: i64, mod: i64) -> i64 {
    let r: i128 = ((a % mod) as i128) * ((b % mod) as i128) % (mod as i128)
    if r < (0 as i128) { return (r + (mod as i128)) as i64 }
    return r as i64
}

function modpow(base0: i64, exp0: i64, mod: i64) -> i64 {
    if mod == 1 { return 0 }
    let mut r: i64 = 1
    let mut b: i64 = base0 % mod
    if b < 0 { b = b + mod }
    let mut e: i64 = exp0
    while e > 0 {
        if e % 2 == 1 { r = modmul(r, b, mod) }
        b = modmul(b, b, mod)
        e = e / 2
    }
    return r
}

# Returns F_n mod m via doubling (matrix [[1,1],[1,0]]^n).
function fib_mod(n0: i64, mod: i64) -> i64 {
    if n0 == 0 { return 0 }
    if mod == 1 { return 0 }
    # compute (F_n, F_{n+1})
    let mut a: i64 = 0
    let mut b: i64 = 1
    # process bits of n from high to low after leading 1
    let mut bit: i32 = 62
    while bit >= 0 {
        if ((n0 >> bit) & 1) != 0 { break }
        bit = bit - 1
    }
    # start with F_1=1, F_2=1 for the leading 1
    a = 1
    b = 1
    bit = bit - 1
    while bit >= 0 {
        # double: (F_k, F_{k+1}) -> (F_{2k}, F_{2k+1})
        let f2k: i64 = modmul(a, modmul(2 * b - a + mod, 1, mod), mod)
        # F_{2k} = F_k*(2F_{k+1}-F_k)
        let t: i64 = (2 * b - a) % mod
        if t < 0 { t = t + mod }
        let F2k: i64 = modmul(a, t, mod)
        let F2k1: i64 = (modmul(a, a, mod) + modmul(b, b, mod)) % mod
        a = F2k
        b = F2k1
        if ((n0 >> bit) & 1) != 0 {
            # (F, Fnext) -> (Fnext, F+Fnext)
            let na: i64 = b
            let nb: i64 = (a + b) % mod
            a = na
            b = nb
        }
        bit = bit - 1
    }
    return a
}

function F_poly(n: i64, x: i64, mod: i64) -> i64 {
    if x == 0 { return 0 }
    let den: i64 = x * x + x - 1
    let big: i64 = mod * den
    let fn: i64 = fib_mod(n, big)
    let fn1: i64 = fib_mod(n + 1, big)
    let mut num: i64 = modmul(fn, modpow(x, n + 2, big), big)
    num = (num + modmul(fn1, modpow(x, n + 1, big), big)) % big
    num = (num - x) % big
    if num < 0 { num = num + big }
    return num / den
}

function main() -> i32 {
    let MOD: i64 = 1307674368000
    let N: i64 = 1000000000000000
    # sanity: F_7(11)
    let check: i64 = F_poly(7, 11, 1000000000000)
    if check != 268357683 {
        printf("check failed %lld\n", check)
        return 1
    }
    let mut total: i64 = 0
    let mut x: i64 = 0
    while x <= 100 {
        total = (total + F_poly(N, x, MOD)) % MOD
        x = x + 1
    }
    printf("%lld\n", total)
    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 modmul_i64_i64_i64(int64_t a, int64_t b, int64_t mod);
int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
int64_t fib_mod_i64_i64(int64_t n0, int64_t mod);
int64_t F_poly_i64_i64_i64(int64_t n, int64_t x, int64_t mod);
int32_t main(void);

int64_t modmul_i64_i64_i64(int64_t a, int64_t b, int64_t mod) {
    __int128 r = FLOW_CHECKED_MOD(((((__int128)(FLOW_CHECKED_MOD((a), (mod)))) * ((__int128)(FLOW_CHECKED_MOD((b), (mod)))))), (((__int128)(mod))));
    if (r < ((__int128)(0))) {
        return ((int64_t)((r + ((__int128)(mod)))));
    }
    return ((int64_t)(r));
}

int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod) {
    if (mod == 1) {
        return 0;
    }
    int64_t r = 1;
    int64_t b = FLOW_CHECKED_MOD((base0), (mod));
    if (b < 0) {
        b = (b + mod);
    }
    int64_t e = exp0;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            r = modmul_i64_i64_i64(r, b, mod);
        }
        b = modmul_i64_i64_i64(b, b, mod);
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return r;
}

int64_t fib_mod_i64_i64(int64_t n0, int64_t mod) {
    if (n0 == 0) {
        return 0;
    }
    if (mod == 1) {
        return 0;
    }
    int64_t a = 0;
    int64_t b = 1;
    int32_t bit = 62;
    while (bit >= 0) {
        if ((FLOW_CHECKED_SHR((n0), (bit)) & 1) != 0) {
            break;
        }
        bit = (bit - 1);
    }
    a = 1;
    b = 1;
    bit = (bit - 1);
    while (bit >= 0) {
        int64_t f2k = modmul_i64_i64_i64(a, modmul_i64_i64_i64((((2 * b) - a) + mod), 1, mod), mod);
        int64_t t = FLOW_CHECKED_MOD((((2 * b) - a)), (mod));
        if (t < 0) {
            t = (t + mod);
        }
        int64_t F2k = modmul_i64_i64_i64(a, t, mod);
        int64_t F2k1 = FLOW_CHECKED_MOD(((modmul_i64_i64_i64(a, a, mod) + modmul_i64_i64_i64(b, b, mod))), (mod));
        a = F2k;
        b = F2k1;
        if ((FLOW_CHECKED_SHR((n0), (bit)) & 1) != 0) {
            int64_t na = b;
            int64_t nb = FLOW_CHECKED_MOD(((a + b)), (mod));
            a = na;
            b = nb;
        }
        bit = (bit - 1);
    }
    return a;
}

int64_t F_poly_i64_i64_i64(int64_t n, int64_t x, int64_t mod) {
    if (x == 0) {
        return 0;
    }
    int64_t den = (((x * x) + x) - 1);
    int64_t big = (mod * den);
    int64_t fn = fib_mod_i64_i64(n, big);
    int64_t fn1 = fib_mod_i64_i64((n + 1), big);
    int64_t num = modmul_i64_i64_i64(fn, modpow_i64_i64_i64(x, (n + 2), big), big);
    num = FLOW_CHECKED_MOD(((num + modmul_i64_i64_i64(fn1, modpow_i64_i64_i64(x, (n + 1), big), big))), (big));
    num = FLOW_CHECKED_MOD(((num - x)), (big));
    if (num < 0) {
        num = (num + big);
    }
    return FLOW_CHECKED_DIV((num), (den));
}

int32_t main(void) {
    int64_t MOD = 1307674368000;
    int64_t N = 1000000000000000;
    int64_t check = F_poly_i64_i64_i64(7, 11, 1000000000000);
    if (check != 268357683) {
        printf("check failed %lld\n", check);
        return 1;
    }
    int64_t total = 0;
    int64_t x = 0;
    while (x <= 100) {
        total = FLOW_CHECKED_MOD(((total + F_poly_i64_i64_i64(N, x, MOD))), (MOD));
        x = (x + 1);
    }
    printf("%lld\n", total);
    return 0;
}

Generated MLIR

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