Problem 366

Stone Game III / Fibonacci Nim: sum M(n) for n≤10^18 mod 10^8.

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

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(log n)
Space complexityO(1)O(1)
ApproachFlow solutionMatrix exponentiation
VerdictSuboptimal

Flow source

# Project Euler 366
# Stone Game III / Fibonacci Nim: sum M(n) for n≤10^18 mod 10^8.

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

function mulmod(a: i64, b: i64, mod: i64) -> i64 {
    let mut x: i64 = a % mod
    if x < 0 { x = x + mod }
    let mut y: i64 = b % mod
    if y < 0 { y = y + mod }
    let mut res: i64 = 0
    while y > 0 {
        if y % 2 == 1 {
            res = res + x
            if res >= mod { res = res - mod }
        }
        x = x + x
        if x >= mod { x = x - mod }
        y = y / 2
    }
    return res
}

function half_product(a: i64, b: i64, mod: i64) -> i64 {
    # (a * b / 2) % mod, assuming a*b is even. mod = 10^8 even, no inv(2).
    if a % 2 == 0 {
        return mulmod(a / 2, b, mod)
    }
    return mulmod(a, b / 2, mod)
}

function main() -> i32 {
    let MOD: i64 = 100000000
    let TARGET: i64 = 1000000000000000000
    let fib: ptr<i64> = calloc(200, 8)
    if fib == null { return 1 }
    fib[1] = 1
    fib[2] = 2
    let mut flen: i64 = 3
    while true {
        let nxt: i64 = fib[flen - 1] + fib[flen - 2]
        fib[flen] = nxt
        flen = flen + 1
        if nxt > TARGET { break }
    }

    let mut total: i64 = 0
    let mut i: i64 = 1
    while i < flen - 1 {
        let Fi: i64 = fib[i]
        if Fi > TARGET { break }
        let mut max_k: i64 = fib[i + 1] - 1
        if max_k > TARGET { max_k = TARGET }
        let mut max_m0: i64 = max_k - Fi
        if max_m0 > 0 && i - 1 >= 1 {
            if max_m0 > fib[i - 1] - 1 { max_m0 = fib[i - 1] - 1 }
            if max_m0 > 0 {
                let mut S: i64 = 0
                let mut LB: i64 = 1
                let mut j: i64 = 0
                while true {
                    let idx: i64 = i - 2 * j
                    if idx < 1 { break }
                    let mut U: i64 = S + (fib[idx] - 1) / 2
                    if U > max_m0 { U = max_m0 }
                    if LB <= U {
                        let cnt: i64 = U - LB + 1
                        let sum_m: i64 = half_product(LB + U, cnt, MOD)
                        let sub: i64 = mulmod(cnt, S, MOD)
                        total = total + sum_m - sub
                        total = total % MOD
                        if total < 0 { total = total + MOD }
                    }
                    if idx - 2 < 1 { break }
                    let need: i64 = S + (fib[idx] + 1) / 2
                    if need > LB { LB = need }
                    S = S + fib[idx - 2]
                    j = j + 1
                    if LB > max_m0 { break }
                }
            }
        }
        i = i + 1
    }

    printf("%lld\n", total % MOD)
    free(fib)
    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 mulmod_i64_i64_i64(int64_t a, int64_t b, int64_t mod);
int64_t half_product_i64_i64_i64(int64_t a, int64_t b, int64_t mod);
int32_t main(void);



int64_t mulmod_i64_i64_i64(int64_t a, int64_t b, int64_t mod) {
    int64_t x = FLOW_CHECKED_MOD((a), (mod));
    if (x < 0) {
        x = (x + mod);
    }
    int64_t y = FLOW_CHECKED_MOD((b), (mod));
    if (y < 0) {
        y = (y + mod);
    }
    int64_t res = 0;
    while (y > 0) {
        if (FLOW_CHECKED_MOD((y), (2)) == 1) {
            res = (res + x);
            if (res >= mod) {
                res = (res - mod);
            }
        }
        x = (x + x);
        if (x >= mod) {
            x = (x - mod);
        }
        y = FLOW_CHECKED_DIV((y), (2));
    }
    return res;
}

int64_t half_product_i64_i64_i64(int64_t a, int64_t b, int64_t mod) {
    if (FLOW_CHECKED_MOD((a), (2)) == 0) {
        return mulmod_i64_i64_i64(FLOW_CHECKED_DIV((a), (2)), b, mod);
    }
    return mulmod_i64_i64_i64(a, FLOW_CHECKED_DIV((b), (2)), mod);
}

int32_t main(void) {
    int64_t MOD = 100000000;
    int64_t TARGET = 1000000000000000000;
    int64_t* fib = (int64_t*)(calloc(200, 8));
    if (fib == NULL) {
        return 1;
    }
    fib[1] = 1;
    fib[2] = 2;
    int64_t flen = 3;
    while (1) {
        int64_t nxt = (fib[(flen - 1)] + fib[(flen - 2)]);
        fib[flen] = nxt;
        flen = (flen + 1);
        if (nxt > TARGET) {
            break;
        }
    }
    int64_t total = 0;
    int64_t i = 1;
    while (i < (flen - 1)) {
        int64_t Fi = fib[i];
        if (Fi > TARGET) {
            break;
        }
        int64_t max_k = (fib[(i + 1)] - 1);
        if (max_k > TARGET) {
            max_k = TARGET;
        }
        int64_t max_m0 = (max_k - Fi);
        if ((max_m0 > 0 && (i - 1) >= 1)) {
            if (max_m0 > (fib[(i - 1)] - 1)) {
                max_m0 = (fib[(i - 1)] - 1);
            }
            if (max_m0 > 0) {
                int64_t S = 0;
                int64_t LB = 1;
                int64_t j = 0;
                while (1) {
                    int64_t idx = (i - (2 * j));
                    if (idx < 1) {
                        break;
                    }
                    int64_t U = (S + FLOW_CHECKED_DIV(((fib[idx] - 1)), (2)));
                    if (U > max_m0) {
                        U = max_m0;
                    }
                    if (LB <= U) {
                        int64_t cnt = ((U - LB) + 1);
                        int64_t sum_m = half_product_i64_i64_i64((LB + U), cnt, MOD);
                        int64_t sub = mulmod_i64_i64_i64(cnt, S, MOD);
                        total = ((total + sum_m) - sub);
                        total = FLOW_CHECKED_MOD((total), (MOD));
                        if (total < 0) {
                            total = (total + MOD);
                        }
                    }
                    if ((idx - 2) < 1) {
                        break;
                    }
                    int64_t need = (S + FLOW_CHECKED_DIV(((fib[idx] + 1)), (2)));
                    if (need > LB) {
                        LB = need;
                    }
                    S = (S + fib[(idx - 2)]);
                    j = (j + 1);
                    if (LB > max_m0) {
                        break;
                    }
                }
            }
        }
        i = (i + 1);
    }
    printf("%lld\n", FLOW_CHECKED_MOD((total), (MOD)));
    free(fib);
    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 @mulmod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %0 = arith.remsi %arg0, %arg2 : i64
    %1 = llvm.mlir.constant(1 : i64) : i64
    %2 = llvm.alloca %1 x i64 : (i64) -> !llvm.ptr
    llvm.store %0, %2 : i64, !llvm.ptr
    %3 = llvm.load %2 : !llvm.ptr -> i64
    %4 = arith.constant 0 : i32
    %6 = arith.extsi %4 : i32 to i64
    %5 = arith.cmpi slt, %3, %6 : i64
    cf.cond_br %5, ^bb0, ^bb1
    ^bb0:
      %7 = llvm.load %2 : !llvm.ptr -> i64
      %8 = arith.addi %7, %arg2 : i64
      llvm.store %8, %2 : i64, !llvm.ptr
      cf.br ^bb2
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %9 = arith.remsi %arg1, %arg2 : i64
    %10 = llvm.mlir.constant(1 : i64) : i64
    %11 = llvm.alloca %10 x i64 : (i64) -> !llvm.ptr
    llvm.store %9, %11 : i64, !llvm.ptr
    %12 = llvm.load %11 : !llvm.ptr -> i64
    %13 = arith.constant 0 : i32
    %15 = arith.extsi %13 : i32 to i64
    %14 = arith.cmpi slt, %12, %15 : i64
    cf.cond_br %14, ^bb3, ^bb4
    ^bb3:
      %16 = llvm.load %11 : !llvm.ptr -> i64
      %17 = arith.addi %16, %arg2 : i64
      llvm.store %17, %11 : i64, !llvm.ptr
      cf.br ^bb5
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %18 = arith.constant 0 : i32
    %19 = arith.extsi %18 : i32 to i64
    %20 = llvm.mlir.constant(1 : i64) : i64
    %21 = llvm.alloca %20 x i64 : (i64) -> !llvm.ptr
    llvm.store %19, %21 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %22 = llvm.load %11 : !llvm.ptr -> i64
    %23 = arith.constant 0 : i32
    %25 = arith.extsi %23 : i32 to i64
    %24 = arith.cmpi sgt, %22, %25 : i64
    cf.cond_br %24, ^bb7, ^bb8
    ^bb7:
      %26 = llvm.load %11 : !llvm.ptr -> i64
      %27 = arith.constant 2 : i32
      %29 = arith.extsi %27 : i32 to i64
      %28 = arith.remsi %26, %29 : i64
      %30 = arith.constant 1 : i32
      %32 = arith.extsi %30 : i32 to i64
      %31 = arith.cmpi eq, %28, %32 : i64
      cf.cond_br %31, ^bb9, ^bb10
      ^bb9:
        %33 = llvm.load %21 : !llvm.ptr -> i64
        %34 = llvm.load %2 : !llvm.ptr -> i64
        %35 = arith.addi %33, %34 : i64
        llvm.store %35, %21 : i64, !llvm.ptr
        %36 = llvm.load %21 : !llvm.ptr -> i64
        %37 = arith.cmpi sge, %36, %arg2 : i64
        cf.cond_br %37, ^bb12, ^bb13
        ^bb12:
          %38 = llvm.load %21 : !llvm.ptr -> i64
          %39 = arith.subi %38, %arg2 : i64
          llvm.store %39, %21 : i64, !llvm.ptr
          cf.br ^bb14
        ^bb13:
          cf.br ^bb14
        ^bb14:
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %40 = llvm.load %2 : !llvm.ptr -> i64
      %41 = llvm.load %2 : !llvm.ptr -> i64
      %42 = arith.addi %40, %41 : i64
      llvm.store %42, %2 : i64, !llvm.ptr
      %43 = llvm.load %2 : !llvm.ptr -> i64
      %44 = arith.cmpi sge, %43, %arg2 : i64
      cf.cond_br %44, ^bb15, ^bb16
      ^bb15:
        %45 = llvm.load %2 : !llvm.ptr -> i64
        %46 = arith.subi %45, %arg2 : i64
        llvm.store %46, %2 : i64, !llvm.ptr
        cf.br ^bb17
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %47 = llvm.load %11 : !llvm.ptr -> i64
      %48 = arith.constant 2 : i32
      %50 = arith.extsi %48 : i32 to i64
      %49 = arith.divsi %47, %50 : i64
      llvm.store %49, %11 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %51 = llvm.load %21 : !llvm.ptr -> i64
    func.return %51 : i64
  }
  func.func @half_product(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %52 = arith.constant 2 : i32
    %54 = arith.extsi %52 : i32 to i64
    %53 = arith.remsi %arg0, %54 : i64
    %55 = arith.constant 0 : i32
    %57 = arith.extsi %55 : i32 to i64
    %56 = arith.cmpi eq, %53, %57 : i64
    cf.cond_br %56, ^bb18, ^bb19
    ^bb18:
      %59 = arith.constant 2 : i32
      %61 = arith.extsi %59 : i32 to i64
      %60 = arith.divsi %arg0, %61 : i64
      %58 = func.call @mulmod(%60, %arg1, %arg2) : (i64, i64, i64) -> i64
      func.return %58 : i64
    ^bb19:
      cf.br ^bb20
    ^bb20:
    %63 = arith.constant 2 : i32
    %65 = arith.extsi %63 : i32 to i64
    %64 = arith.divsi %arg1, %65 : i64
    %62 = func.call @mulmod(%arg0, %64, %arg2) : (i64, i64, i64) -> i64
    func.return %62 : i64
  }
  func.func @main() -> i32 {
    %66 = arith.constant 100000000 : i32
    %67 = arith.extsi %66 : i32 to i64
    %68 = arith.constant 999999995705032704 : i32
    %69 = arith.extsi %68 : i32 to i64
    %71 = arith.constant 200 : i32
    %72 = arith.constant 8 : i32
    %73 = arith.extsi %71 : i32 to i64
    %74 = arith.extsi %72 : i32 to i64
    %70 = func.call @calloc(%73, %74) : (i64, i64) -> !llvm.ptr
    %75 = llvm.mlir.zero : !llvm.ptr
    %76 = llvm.icmp "eq" %70, %75 : !llvm.ptr
    cf.cond_br %76, ^bb21, ^bb22
    ^bb21:
      %77 = arith.constant 1 : i32
      func.return %77 : i32
    ^bb22:
      cf.br ^bb23
    ^bb23:
    %78 = arith.constant 1 : i32
    %79 = arith.constant 1 : i32
    %80 = arith.extsi %78 : i32 to i64
    %81 = arith.extsi %79 : i32 to i64
    %82 = llvm.getelementptr %70[%81] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %80, %82 : i64, !llvm.ptr
    %83 = arith.constant 2 : i32
    %84 = arith.constant 2 : i32
    %85 = arith.extsi %83 : i32 to i64
    %86 = arith.extsi %84 : i32 to i64
    %87 = llvm.getelementptr %70[%86] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %85, %87 : i64, !llvm.ptr
    %88 = arith.constant 3 : i32
    %89 = arith.extsi %88 : i32 to i64
    %90 = llvm.mlir.constant(1 : i64) : i64
    %91 = llvm.alloca %90 x i64 : (i64) -> !llvm.ptr
    llvm.store %89, %91 : i64, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %92 = arith.constant 1 : i1
    cf.cond_br %92, ^bb25, ^bb26
    ^bb25:
      %94 = llvm.load %91 : !llvm.ptr -> i64
      %95 = arith.constant 1 : i32
      %97 = arith.extsi %95 : i32 to i64
      %96 = arith.subi %94, %97 : i64
      %98 = llvm.getelementptr %70[%96] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %93 = llvm.load %98 : !llvm.ptr -> i64
      %100 = llvm.load %91 : !llvm.ptr -> i64
      %101 = arith.constant 2 : i32
      %103 = arith.extsi %101 : i32 to i64
      %102 = arith.subi %100, %103 : i64
      %104 = llvm.getelementptr %70[%102] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %99 = llvm.load %104 : !llvm.ptr -> i64
      %105 = arith.addi %93, %99 : i64
      %106 = llvm.load %91 : !llvm.ptr -> i64
      %107 = llvm.getelementptr %70[%106] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %105, %107 : i64, !llvm.ptr
      %108 = llvm.load %91 : !llvm.ptr -> i64
      %109 = arith.constant 1 : i32
      %111 = arith.extsi %109 : i32 to i64
      %110 = arith.addi %108, %111 : i64
      llvm.store %110, %91 : i64, !llvm.ptr
      %112 = arith.cmpi sgt, %105, %69 : i64
      cf.cond_br %112, ^bb27, ^bb28
      ^bb27:
        cf.br ^bb26
      ^bb28:
        cf.br ^bb29
      ^bb29:
      cf.br ^bb24
    ^bb26:
    %113 = arith.constant 0 : 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
    cf.br ^bb30
    ^bb30:
    %121 = llvm.load %120 : !llvm.ptr -> i64
    %122 = llvm.load %91 : !llvm.ptr -> i64
    %123 = arith.constant 1 : i32
    %125 = arith.extsi %123 : i32 to i64
    %124 = arith.subi %122, %125 : i64
    %126 = arith.cmpi slt, %121, %124 : i64
    cf.cond_br %126, ^bb31, ^bb32
    ^bb31:
      %128 = llvm.load %120 : !llvm.ptr -> i64
      %129 = llvm.getelementptr %70[%128] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %127 = llvm.load %129 : !llvm.ptr -> i64
      %130 = arith.cmpi sgt, %127, %69 : i64
      cf.cond_br %130, ^bb33, ^bb34
      ^bb33:
        cf.br ^bb32
      ^bb34:
        cf.br ^bb35
      ^bb35:
      %132 = llvm.load %120 : !llvm.ptr -> i64
      %133 = arith.constant 1 : i32
      %135 = arith.extsi %133 : i32 to i64
      %134 = arith.addi %132, %135 : i64
      %136 = llvm.getelementptr %70[%134] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %131 = llvm.load %136 : !llvm.ptr -> i64
      %137 = arith.constant 1 : i32
      %139 = arith.extsi %137 : i32 to i64
      %138 = arith.subi %131, %139 : i64
      %140 = llvm.mlir.constant(1 : i64) : i64
      %141 = llvm.alloca %140 x i64 : (i64) -> !llvm.ptr
      llvm.store %138, %141 : i64, !llvm.ptr
      %142 = llvm.load %141 : !llvm.ptr -> i64
      %143 = arith.cmpi sgt, %142, %69 : i64
      cf.cond_br %143, ^bb36, ^bb37
      ^bb36:
        llvm.store %69, %141 : i64, !llvm.ptr
        cf.br ^bb38
      ^bb37:
        cf.br ^bb38
      ^bb38:
      %144 = llvm.load %141 : !llvm.ptr -> i64
      %145 = arith.subi %144, %127 : i64
      %146 = llvm.mlir.constant(1 : i64) : i64
      %147 = llvm.alloca %146 x i64 : (i64) -> !llvm.ptr
      llvm.store %145, %147 : i64, !llvm.ptr
      %148 = llvm.load %147 : !llvm.ptr -> i64
      %149 = arith.constant 0 : i32
      %151 = arith.extsi %149 : i32 to i64
      %150 = arith.cmpi sgt, %148, %151 : i64
      %152 = scf.if %150 -> (i1) {
        %153 = llvm.load %120 : !llvm.ptr -> i64
        %154 = arith.constant 1 : i32
        %156 = arith.extsi %154 : i32 to i64
        %155 = arith.subi %153, %156 : i64
        %157 = arith.constant 1 : i32
        %159 = arith.extsi %157 : i32 to i64
        %158 = arith.cmpi sge, %155, %159 : i64
        scf.yield %158 : i1
      } else {
        %160 = arith.constant false
        scf.yield %160 : i1
      }
      cf.cond_br %152, ^bb39, ^bb40
      ^bb39:
        %161 = llvm.load %147 : !llvm.ptr -> i64
        %163 = llvm.load %120 : !llvm.ptr -> i64
        %164 = arith.constant 1 : i32
        %166 = arith.extsi %164 : i32 to i64
        %165 = arith.subi %163, %166 : i64
        %167 = llvm.getelementptr %70[%165] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %162 = llvm.load %167 : !llvm.ptr -> i64
        %168 = arith.constant 1 : i32
        %170 = arith.extsi %168 : i32 to i64
        %169 = arith.subi %162, %170 : i64
        %171 = arith.cmpi sgt, %161, %169 : i64
        cf.cond_br %171, ^bb42, ^bb43
        ^bb42:
          %173 = llvm.load %120 : !llvm.ptr -> i64
          %174 = arith.constant 1 : i32
          %176 = arith.extsi %174 : i32 to i64
          %175 = arith.subi %173, %176 : i64
          %177 = llvm.getelementptr %70[%175] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %172 = llvm.load %177 : !llvm.ptr -> i64
          %178 = arith.constant 1 : i32
          %180 = arith.extsi %178 : i32 to i64
          %179 = arith.subi %172, %180 : i64
          llvm.store %179, %147 : i64, !llvm.ptr
          cf.br ^bb44
        ^bb43:
          cf.br ^bb44
        ^bb44:
        %181 = llvm.load %147 : !llvm.ptr -> i64
        %182 = arith.constant 0 : i32
        %184 = arith.extsi %182 : i32 to i64
        %183 = arith.cmpi sgt, %181, %184 : i64
        cf.cond_br %183, ^bb45, ^bb46
        ^bb45:
          %185 = arith.constant 0 : i32
          %186 = arith.extsi %185 : i32 to 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 = arith.constant 1 : i32
          %190 = arith.extsi %189 : i32 to i64
          %191 = llvm.mlir.constant(1 : i64) : i64
          %192 = llvm.alloca %191 x i64 : (i64) -> !llvm.ptr
          llvm.store %190, %192 : i64, !llvm.ptr
          %193 = arith.constant 0 : i32
          %194 = arith.extsi %193 : i32 to i64
          %195 = llvm.mlir.constant(1 : i64) : i64
          %196 = llvm.alloca %195 x i64 : (i64) -> !llvm.ptr
          llvm.store %194, %196 : i64, !llvm.ptr
          cf.br ^bb48
          ^bb48:
          %197 = arith.constant 1 : i1
          cf.cond_br %197, ^bb49, ^bb50
          ^bb49:
            %198 = llvm.load %120 : !llvm.ptr -> i64
            %199 = arith.constant 2 : i32
            %200 = llvm.load %196 : !llvm.ptr -> i64
            %202 = arith.extsi %199 : i32 to i64
            %201 = arith.muli %202, %200 : i64
            %203 = arith.subi %198, %201 : i64
            %204 = arith.constant 1 : i32
            %206 = arith.extsi %204 : i32 to i64
            %205 = arith.cmpi slt, %203, %206 : i64
            cf.cond_br %205, ^bb51, ^bb52
            ^bb51:
              cf.br ^bb50
            ^bb52:
              cf.br ^bb53
            ^bb53:
            %207 = llvm.load %188 : !llvm.ptr -> i64
            %209 = llvm.getelementptr %70[%203] : (!llvm.ptr, i64) -> !llvm.ptr, i64
            %208 = llvm.load %209 : !llvm.ptr -> i64
            %210 = arith.constant 1 : i32
            %212 = arith.extsi %210 : i32 to i64
            %211 = arith.subi %208, %212 : i64
            %213 = arith.constant 2 : i32
            %215 = arith.extsi %213 : i32 to i64
            %214 = arith.divsi %211, %215 : i64
            %216 = arith.addi %207, %214 : i64
            %217 = llvm.mlir.constant(1 : i64) : i64
            %218 = llvm.alloca %217 x i64 : (i64) -> !llvm.ptr
            llvm.store %216, %218 : i64, !llvm.ptr
            %219 = llvm.load %218 : !llvm.ptr -> i64
            %220 = llvm.load %147 : !llvm.ptr -> i64
            %221 = arith.cmpi sgt, %219, %220 : i64
            cf.cond_br %221, ^bb54, ^bb55
            ^bb54:
              %222 = llvm.load %147 : !llvm.ptr -> i64
              llvm.store %222, %218 : i64, !llvm.ptr
              cf.br ^bb56
            ^bb55:
              cf.br ^bb56
            ^bb56:
            %223 = llvm.load %192 : !llvm.ptr -> i64
            %224 = llvm.load %218 : !llvm.ptr -> i64
            %225 = arith.cmpi sle, %223, %224 : i64
            cf.cond_br %225, ^bb57, ^bb58
            ^bb57:
              %226 = llvm.load %218 : !llvm.ptr -> i64
              %227 = llvm.load %192 : !llvm.ptr -> i64
              %228 = arith.subi %226, %227 : i64
              %229 = arith.constant 1 : i32
              %231 = arith.extsi %229 : i32 to i64
              %230 = arith.addi %228, %231 : i64
              %233 = llvm.load %192 : !llvm.ptr -> i64
              %234 = llvm.load %218 : !llvm.ptr -> i64
              %235 = arith.addi %233, %234 : i64
              %232 = func.call @half_product(%235, %230, %67) : (i64, i64, i64) -> i64
              %237 = llvm.load %188 : !llvm.ptr -> i64
              %236 = func.call @mulmod(%230, %237, %67) : (i64, i64, i64) -> i64
              %238 = llvm.load %116 : !llvm.ptr -> i64
              %239 = arith.addi %238, %232 : i64
              %240 = arith.subi %239, %236 : i64
              llvm.store %240, %116 : i64, !llvm.ptr
              %241 = llvm.load %116 : !llvm.ptr -> i64
              %242 = arith.remsi %241, %67 : i64
              llvm.store %242, %116 : i64, !llvm.ptr
              %243 = llvm.load %116 : !llvm.ptr -> i64
              %244 = arith.constant 0 : i32
              %246 = arith.extsi %244 : i32 to i64
              %245 = arith.cmpi slt, %243, %246 : i64
              cf.cond_br %245, ^bb60, ^bb61
              ^bb60:
                %247 = llvm.load %116 : !llvm.ptr -> i64
                %248 = arith.addi %247, %67 : i64
                llvm.store %248, %116 : i64, !llvm.ptr
                cf.br ^bb62
              ^bb61:
                cf.br ^bb62
              ^bb62:
              cf.br ^bb59
            ^bb58:
              cf.br ^bb59
            ^bb59:
            %249 = arith.constant 2 : i32
            %251 = arith.extsi %249 : i32 to i64
            %250 = arith.subi %203, %251 : i64
            %252 = arith.constant 1 : i32
            %254 = arith.extsi %252 : i32 to i64
            %253 = arith.cmpi slt, %250, %254 : i64
            cf.cond_br %253, ^bb63, ^bb64
            ^bb63:
              cf.br ^bb50
            ^bb64:
              cf.br ^bb65
            ^bb65:
            %255 = llvm.load %188 : !llvm.ptr -> i64
            %257 = llvm.getelementptr %70[%203] : (!llvm.ptr, i64) -> !llvm.ptr, i64
            %256 = llvm.load %257 : !llvm.ptr -> i64
            %258 = arith.constant 1 : i32
            %260 = arith.extsi %258 : i32 to i64
            %259 = arith.addi %256, %260 : i64
            %261 = arith.constant 2 : i32
            %263 = arith.extsi %261 : i32 to i64
            %262 = arith.divsi %259, %263 : i64
            %264 = arith.addi %255, %262 : i64
            %265 = llvm.load %192 : !llvm.ptr -> i64
            %266 = arith.cmpi sgt, %264, %265 : i64
            cf.cond_br %266, ^bb66, ^bb67
            ^bb66:
              llvm.store %264, %192 : i64, !llvm.ptr
              cf.br ^bb68
            ^bb67:
              cf.br ^bb68
            ^bb68:
            %267 = llvm.load %188 : !llvm.ptr -> i64
            %269 = arith.constant 2 : i32
            %271 = arith.extsi %269 : i32 to i64
            %270 = arith.subi %203, %271 : i64
            %272 = llvm.getelementptr %70[%270] : (!llvm.ptr, i64) -> !llvm.ptr, i64
            %268 = llvm.load %272 : !llvm.ptr -> i64
            %273 = arith.addi %267, %268 : i64
            llvm.store %273, %188 : i64, !llvm.ptr
            %274 = llvm.load %196 : !llvm.ptr -> i64
            %275 = arith.constant 1 : i32
            %277 = arith.extsi %275 : i32 to i64
            %276 = arith.addi %274, %277 : i64
            llvm.store %276, %196 : i64, !llvm.ptr
            %278 = llvm.load %192 : !llvm.ptr -> i64
            %279 = llvm.load %147 : !llvm.ptr -> i64
            %280 = arith.cmpi sgt, %278, %279 : i64
            cf.cond_br %280, ^bb69, ^bb70
            ^bb69:
              cf.br ^bb50
            ^bb70:
              cf.br ^bb71
            ^bb71:
            cf.br ^bb48
          ^bb50:
          cf.br ^bb47
        ^bb46:
          cf.br ^bb47
        ^bb47:
        cf.br ^bb41
      ^bb40:
        cf.br ^bb41
      ^bb41:
      %281 = llvm.load %120 : !llvm.ptr -> i64
      %282 = arith.constant 1 : i32
      %284 = arith.extsi %282 : i32 to i64
      %283 = arith.addi %281, %284 : i64
      llvm.store %283, %120 : i64, !llvm.ptr
      cf.br ^bb30
    ^bb32:
    %285 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %286 = llvm.load %116 : !llvm.ptr -> i64
    %287 = arith.remsi %286, %67 : i64
    %288 = llvm.call @printf(%285, %287) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%70) : (!llvm.ptr) -> ()
    %290 = arith.constant 0 : i32
    func.return %290 : i32
  }
}