Problem 271

Sum of x where x^3 ≡ 1 (mod 13082761331670030), excluding 1.

Answer4617456485273129588
Output4617456485273129588
StatusPASS
Native helperno
Runtime0 ms
Peak memory1200 KB
Time complexityO(n^2) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log n)
Space complexityO(n^2)O(n)
ApproachFlow solutionChinese Remainder Theorem
VerdictSuboptimal

Flow source

# Project Euler 271
# Sum of x where x^3 ≡ 1 (mod 13082761331670030), excluding 1.

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

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

function mod_inverse(a0: i64, m: i64) -> i64 {
    let mut a: i64 = a0 % m
    let mut b: i64 = m
    let mut x0: i64 = 1
    let mut x1: i64 = 0
    while b != 0 {
        let q: i64 = a / b
        let t: i64 = a % b
        a = b
        b = t
        let tx: i64 = x0 - q * x1
        x0 = x1
        x1 = tx
    }
    if x0 < 0 { x0 = x0 + m }
    return x0
}

function crt(a1: i64, a2: i64, n1: i64, n2: i64) -> i64 {
    let mod: i64 = n1 * n2
    let p: i64 = mod_inverse(n1 % n2, n2)
    let q: i64 = mod_inverse(n2 % n1, n1)
    let t1: i64 = mulmod(mulmod(a1, q, mod), n2, mod)
    let t2: i64 = mulmod(mulmod(a2, p, mod), n1, mod)
    let s: i64 = t1 + t2
    if s >= mod { s = s - mod }
    return s
}

function main() -> i32 {
    let factors: ptr<i64> = calloc(14, 8)
    factors[0]=2; factors[1]=3; factors[2]=5; factors[3]=7; factors[4]=11
    factors[5]=13; factors[6]=17; factors[7]=19; factors[8]=23; factors[9]=29
    factors[10]=31; factors[11]=37; factors[12]=41; factors[13]=43

    let MAXS: i64 = 2000
    let sol: ptr<i64> = calloc(MAXS, 8)
    let nsol: ptr<i64> = calloc(MAXS, 8)
    if sol == null || nsol == null { return 1 }

    let mut sc: i64 = 1
    sol[0] = 1
    let mut cur_n: i64 = 2

    let mut fi: i64 = 1
    while fi < 14 {
        let p: i64 = factors[fi]
        let roots: ptr<i64> = calloc(p, 8)
        let mut rc: i64 = 0
        let mut x: i64 = 0
        while x < p {
            if mulmod(mulmod(x, x, p), x, p) == 1 {
                roots[rc] = x
                rc = rc + 1
            }
            x = x + 1
        }
        let mut nc: i64 = 0
        let mut i: i64 = 0
        while i < sc {
            let mut j: i64 = 0
            while j < rc {
                nsol[nc] = crt(sol[i], roots[j], cur_n, p)
                nc = nc + 1
                j = j + 1
            }
            i = i + 1
        }
        i = 0
        while i < nc {
            sol[i] = nsol[i]
            i = i + 1
        }
        sc = nc
        cur_n = cur_n * p
        free(roots)
        fi = fi + 1
    }

    let mut total: i64 = 0
    let mut i: i64 = 0
    while i < sc {
        total = total + sol[i]
        i = i + 1
    }
    total = total - 1
    printf("%lld\n", total)
    free(sol); free(nsol); free(factors)
    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 a0, int64_t b0, int64_t mod);
int64_t mod_inverse_i64_i64(int64_t a0, int64_t m);
int64_t crt_i64_i64_i64_i64(int64_t a1, int64_t a2, int64_t n1, int64_t n2);
int32_t main(void);



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

int64_t mod_inverse_i64_i64(int64_t a0, int64_t m) {
    int64_t a = FLOW_CHECKED_MOD((a0), (m));
    int64_t b = m;
    int64_t x0 = 1;
    int64_t x1 = 0;
    while (b != 0) {
        int64_t q = FLOW_CHECKED_DIV((a), (b));
        int64_t t = FLOW_CHECKED_MOD((a), (b));
        a = b;
        b = t;
        int64_t tx = (x0 - (q * x1));
        x0 = x1;
        x1 = tx;
    }
    if (x0 < 0) {
        x0 = (x0 + m);
    }
    return x0;
}

int64_t crt_i64_i64_i64_i64(int64_t a1, int64_t a2, int64_t n1, int64_t n2) {
    int64_t mod = (n1 * n2);
    int64_t p = mod_inverse_i64_i64(FLOW_CHECKED_MOD((n1), (n2)), n2);
    int64_t q = mod_inverse_i64_i64(FLOW_CHECKED_MOD((n2), (n1)), n1);
    int64_t t1 = mulmod_i64_i64_i64(mulmod_i64_i64_i64(a1, q, mod), n2, mod);
    int64_t t2 = mulmod_i64_i64_i64(mulmod_i64_i64_i64(a2, p, mod), n1, mod);
    int64_t s = (t1 + t2);
    if (s >= mod) {
        s = (s - mod);
    }
    return s;
}

int32_t main(void) {
    int64_t* factors = (int64_t*)(calloc(14, 8));
    factors[0] = 2;
    factors[1] = 3;
    factors[2] = 5;
    factors[3] = 7;
    factors[4] = 11;
    factors[5] = 13;
    factors[6] = 17;
    factors[7] = 19;
    factors[8] = 23;
    factors[9] = 29;
    factors[10] = 31;
    factors[11] = 37;
    factors[12] = 41;
    factors[13] = 43;
    int64_t MAXS = 2000;
    int64_t* sol = (int64_t*)(calloc(MAXS, 8));
    int64_t* nsol = (int64_t*)(calloc(MAXS, 8));
    if ((sol == NULL || nsol == NULL)) {
        return 1;
    }
    int64_t sc = 1;
    sol[0] = 1;
    int64_t cur_n = 2;
    int64_t fi = 1;
    while (fi < 14) {
        int64_t p = factors[fi];
        int64_t* roots = (int64_t*)(calloc(p, 8));
        int64_t rc = 0;
        int64_t x = 0;
        while (x < p) {
            if (mulmod_i64_i64_i64(mulmod_i64_i64_i64(x, x, p), x, p) == 1) {
                roots[rc] = x;
                rc = (rc + 1);
            }
            x = (x + 1);
        }
        int64_t nc = 0;
        int64_t i = 0;
        while (i < sc) {
            int64_t j = 0;
            while (j < rc) {
                nsol[nc] = crt_i64_i64_i64_i64(sol[i], roots[j], cur_n, p);
                nc = (nc + 1);
                j = (j + 1);
            }
            i = (i + 1);
        }
        i = 0;
        while (i < nc) {
            sol[i] = nsol[i];
            i = (i + 1);
        }
        sc = nc;
        cur_n = (cur_n * p);
        free(roots);
        fi = (fi + 1);
    }
    int64_t total = 0;
    int64_t i = 0;
    while (i < sc) {
        total = (total + sol[i]);
        i = (i + 1);
    }
    total = (total - 1);
    printf("%lld\n", total);
    free(sol);
    free(nsol);
    free(factors);
    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 = arith.remsi %arg1, %arg2 : i64
    %4 = llvm.mlir.constant(1 : i64) : i64
    %5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
    llvm.store %3, %5 : i64, !llvm.ptr
    %6 = llvm.load %2 : !llvm.ptr -> i64
    %7 = arith.constant 0 : i32
    %9 = arith.extsi %7 : i32 to i64
    %8 = arith.cmpi slt, %6, %9 : i64
    cf.cond_br %8, ^bb0, ^bb1
    ^bb0:
      %10 = llvm.load %2 : !llvm.ptr -> i64
      %11 = arith.addi %10, %arg2 : i64
      llvm.store %11, %2 : i64, !llvm.ptr
      cf.br ^bb2
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %12 = llvm.load %5 : !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 %5 : !llvm.ptr -> i64
      %17 = arith.addi %16, %arg2 : i64
      llvm.store %17, %5 : 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 %5 : !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 %5 : !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 %5 : !llvm.ptr -> i64
      %48 = arith.constant 2 : i32
      %50 = arith.extsi %48 : i32 to i64
      %49 = arith.divsi %47, %50 : i64
      llvm.store %49, %5 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %51 = llvm.load %21 : !llvm.ptr -> i64
    func.return %51 : i64
  }
  func.func @mod_inverse(%arg0: i64, %arg1: i64) -> i64 {
    %52 = arith.remsi %arg0, %arg1 : i64
    %53 = llvm.mlir.constant(1 : i64) : i64
    %54 = llvm.alloca %53 x i64 : (i64) -> !llvm.ptr
    llvm.store %52, %54 : i64, !llvm.ptr
    %55 = llvm.mlir.constant(1 : i64) : i64
    %56 = llvm.alloca %55 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %56 : i64, !llvm.ptr
    %57 = arith.constant 1 : i32
    %58 = arith.extsi %57 : i32 to i64
    %59 = llvm.mlir.constant(1 : i64) : i64
    %60 = llvm.alloca %59 x i64 : (i64) -> !llvm.ptr
    llvm.store %58, %60 : i64, !llvm.ptr
    %61 = arith.constant 0 : i32
    %62 = arith.extsi %61 : i32 to i64
    %63 = llvm.mlir.constant(1 : i64) : i64
    %64 = llvm.alloca %63 x i64 : (i64) -> !llvm.ptr
    llvm.store %62, %64 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %65 = llvm.load %56 : !llvm.ptr -> i64
    %66 = arith.constant 0 : i32
    %68 = arith.extsi %66 : i32 to i64
    %67 = arith.cmpi ne, %65, %68 : i64
    cf.cond_br %67, ^bb19, ^bb20
    ^bb19:
      %69 = llvm.load %54 : !llvm.ptr -> i64
      %70 = llvm.load %56 : !llvm.ptr -> i64
      %71 = arith.divsi %69, %70 : i64
      %72 = llvm.load %54 : !llvm.ptr -> i64
      %73 = llvm.load %56 : !llvm.ptr -> i64
      %74 = arith.remsi %72, %73 : i64
      %75 = llvm.load %56 : !llvm.ptr -> i64
      llvm.store %75, %54 : i64, !llvm.ptr
      llvm.store %74, %56 : i64, !llvm.ptr
      %76 = llvm.load %60 : !llvm.ptr -> i64
      %77 = llvm.load %64 : !llvm.ptr -> i64
      %78 = arith.muli %71, %77 : i64
      %79 = arith.subi %76, %78 : i64
      %80 = llvm.load %64 : !llvm.ptr -> i64
      llvm.store %80, %60 : i64, !llvm.ptr
      llvm.store %79, %64 : i64, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %81 = llvm.load %60 : !llvm.ptr -> i64
    %82 = arith.constant 0 : i32
    %84 = arith.extsi %82 : i32 to i64
    %83 = arith.cmpi slt, %81, %84 : i64
    cf.cond_br %83, ^bb21, ^bb22
    ^bb21:
      %85 = llvm.load %60 : !llvm.ptr -> i64
      %86 = arith.addi %85, %arg1 : i64
      llvm.store %86, %60 : i64, !llvm.ptr
      cf.br ^bb23
    ^bb22:
      cf.br ^bb23
    ^bb23:
    %87 = llvm.load %60 : !llvm.ptr -> i64
    func.return %87 : i64
  }
  func.func @crt(%arg0: i64, %arg1: i64, %arg2: i64, %arg3: i64) -> i64 {
    %88 = arith.muli %arg2, %arg3 : i64
    %90 = arith.remsi %arg2, %arg3 : i64
    %89 = func.call @mod_inverse(%90, %arg3) : (i64, i64) -> i64
    %92 = arith.remsi %arg3, %arg2 : i64
    %91 = func.call @mod_inverse(%92, %arg2) : (i64, i64) -> i64
    %94 = func.call @mulmod(%arg0, %91, %88) : (i64, i64, i64) -> i64
    %93 = func.call @mulmod(%94, %arg3, %88) : (i64, i64, i64) -> i64
    %96 = func.call @mulmod(%arg1, %89, %88) : (i64, i64, i64) -> i64
    %95 = func.call @mulmod(%96, %arg2, %88) : (i64, i64, i64) -> i64
    %97 = arith.addi %93, %95 : i64
    %98 = arith.cmpi sge, %97, %88 : i64
    %99 = scf.if %98 -> (i64) {
      %100 = arith.subi %97, %88 : i64
      scf.yield %100 : i64
    } else {
      scf.yield %97 : i64
    }
    func.return %99 : i64
  }
  func.func @main() -> i32 {
    %102 = arith.constant 14 : i32
    %103 = arith.constant 8 : i32
    %104 = arith.extsi %102 : i32 to i64
    %105 = arith.extsi %103 : i32 to i64
    %101 = func.call @calloc(%104, %105) : (i64, i64) -> !llvm.ptr
    %106 = arith.constant 2 : i32
    %107 = arith.constant 0 : i32
    %108 = arith.extsi %106 : i32 to i64
    %109 = arith.extsi %107 : i32 to i64
    %110 = llvm.getelementptr %101[%109] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %108, %110 : i64, !llvm.ptr
    %111 = arith.constant 3 : i32
    %112 = arith.constant 1 : i32
    %113 = arith.extsi %111 : i32 to i64
    %114 = arith.extsi %112 : i32 to i64
    %115 = llvm.getelementptr %101[%114] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %113, %115 : i64, !llvm.ptr
    %116 = arith.constant 5 : i32
    %117 = arith.constant 2 : i32
    %118 = arith.extsi %116 : i32 to i64
    %119 = arith.extsi %117 : i32 to i64
    %120 = llvm.getelementptr %101[%119] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %118, %120 : i64, !llvm.ptr
    %121 = arith.constant 7 : i32
    %122 = arith.constant 3 : i32
    %123 = arith.extsi %121 : i32 to i64
    %124 = arith.extsi %122 : i32 to i64
    %125 = llvm.getelementptr %101[%124] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %123, %125 : i64, !llvm.ptr
    %126 = arith.constant 11 : i32
    %127 = arith.constant 4 : i32
    %128 = arith.extsi %126 : i32 to i64
    %129 = arith.extsi %127 : i32 to i64
    %130 = llvm.getelementptr %101[%129] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %128, %130 : i64, !llvm.ptr
    %131 = arith.constant 13 : i32
    %132 = arith.constant 5 : i32
    %133 = arith.extsi %131 : i32 to i64
    %134 = arith.extsi %132 : i32 to i64
    %135 = llvm.getelementptr %101[%134] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %133, %135 : i64, !llvm.ptr
    %136 = arith.constant 17 : i32
    %137 = arith.constant 6 : i32
    %138 = arith.extsi %136 : i32 to i64
    %139 = arith.extsi %137 : i32 to i64
    %140 = llvm.getelementptr %101[%139] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %138, %140 : i64, !llvm.ptr
    %141 = arith.constant 19 : i32
    %142 = arith.constant 7 : i32
    %143 = arith.extsi %141 : i32 to i64
    %144 = arith.extsi %142 : i32 to i64
    %145 = llvm.getelementptr %101[%144] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %143, %145 : i64, !llvm.ptr
    %146 = arith.constant 23 : i32
    %147 = arith.constant 8 : i32
    %148 = arith.extsi %146 : i32 to i64
    %149 = arith.extsi %147 : i32 to i64
    %150 = llvm.getelementptr %101[%149] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %148, %150 : i64, !llvm.ptr
    %151 = arith.constant 29 : i32
    %152 = arith.constant 9 : i32
    %153 = arith.extsi %151 : i32 to i64
    %154 = arith.extsi %152 : i32 to i64
    %155 = llvm.getelementptr %101[%154] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %153, %155 : i64, !llvm.ptr
    %156 = arith.constant 31 : i32
    %157 = arith.constant 10 : i32
    %158 = arith.extsi %156 : i32 to i64
    %159 = arith.extsi %157 : i32 to i64
    %160 = llvm.getelementptr %101[%159] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %158, %160 : i64, !llvm.ptr
    %161 = arith.constant 37 : i32
    %162 = arith.constant 11 : i32
    %163 = arith.extsi %161 : i32 to i64
    %164 = arith.extsi %162 : i32 to i64
    %165 = llvm.getelementptr %101[%164] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %163, %165 : i64, !llvm.ptr
    %166 = arith.constant 41 : i32
    %167 = arith.constant 12 : i32
    %168 = arith.extsi %166 : i32 to i64
    %169 = arith.extsi %167 : i32 to i64
    %170 = llvm.getelementptr %101[%169] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %168, %170 : i64, !llvm.ptr
    %171 = arith.constant 43 : i32
    %172 = arith.constant 13 : i32
    %173 = arith.extsi %171 : i32 to i64
    %174 = arith.extsi %172 : i32 to i64
    %175 = llvm.getelementptr %101[%174] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %173, %175 : i64, !llvm.ptr
    %176 = arith.constant 2000 : i32
    %177 = arith.extsi %176 : i32 to i64
    %179 = arith.constant 8 : i32
    %180 = arith.extsi %179 : i32 to i64
    %178 = func.call @calloc(%177, %180) : (i64, i64) -> !llvm.ptr
    %182 = arith.constant 8 : i32
    %183 = arith.extsi %182 : i32 to i64
    %181 = func.call @calloc(%177, %183) : (i64, i64) -> !llvm.ptr
    %184 = llvm.mlir.zero : !llvm.ptr
    %185 = llvm.icmp "eq" %178, %184 : !llvm.ptr
    %186 = scf.if %185 -> (i1) {
      %187 = arith.constant true
      scf.yield %187 : i1
    } else {
      %188 = llvm.mlir.zero : !llvm.ptr
      %189 = llvm.icmp "eq" %181, %188 : !llvm.ptr
      scf.yield %189 : i1
    }
    cf.cond_br %186, ^bb24, ^bb25
    ^bb24:
      %190 = arith.constant 1 : i32
      func.return %190 : i32
    ^bb25:
      cf.br ^bb26
    ^bb26:
    %191 = arith.constant 1 : i32
    %192 = arith.extsi %191 : i32 to i64
    %193 = llvm.mlir.constant(1 : i64) : i64
    %194 = llvm.alloca %193 x i64 : (i64) -> !llvm.ptr
    llvm.store %192, %194 : i64, !llvm.ptr
    %195 = arith.constant 1 : i32
    %196 = arith.constant 0 : i32
    %197 = arith.extsi %195 : i32 to i64
    %198 = arith.extsi %196 : i32 to i64
    %199 = llvm.getelementptr %178[%198] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %197, %199 : i64, !llvm.ptr
    %200 = arith.constant 2 : i32
    %201 = arith.extsi %200 : i32 to i64
    %202 = llvm.mlir.constant(1 : i64) : i64
    %203 = llvm.alloca %202 x i64 : (i64) -> !llvm.ptr
    llvm.store %201, %203 : i64, !llvm.ptr
    %204 = arith.constant 1 : i32
    %205 = arith.extsi %204 : i32 to i64
    %206 = llvm.mlir.constant(1 : i64) : i64
    %207 = llvm.alloca %206 x i64 : (i64) -> !llvm.ptr
    llvm.store %205, %207 : i64, !llvm.ptr
    cf.br ^bb27
    ^bb27:
    %208 = llvm.load %207 : !llvm.ptr -> i64
    %209 = arith.constant 14 : i32
    %211 = arith.extsi %209 : i32 to i64
    %210 = arith.cmpi slt, %208, %211 : i64
    cf.cond_br %210, ^bb28, ^bb29
    ^bb28:
      %213 = llvm.load %207 : !llvm.ptr -> i64
      %214 = llvm.getelementptr %101[%213] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %212 = llvm.load %214 : !llvm.ptr -> i64
      %216 = arith.constant 8 : i32
      %217 = arith.extsi %216 : i32 to i64
      %215 = func.call @calloc(%212, %217) : (i64, i64) -> !llvm.ptr
      %218 = arith.constant 0 : i32
      %219 = arith.extsi %218 : i32 to i64
      %220 = llvm.mlir.constant(1 : i64) : i64
      %221 = llvm.alloca %220 x i64 : (i64) -> !llvm.ptr
      llvm.store %219, %221 : i64, !llvm.ptr
      %222 = arith.constant 0 : i32
      %223 = arith.extsi %222 : i32 to i64
      %224 = llvm.mlir.constant(1 : i64) : i64
      %225 = llvm.alloca %224 x i64 : (i64) -> !llvm.ptr
      llvm.store %223, %225 : i64, !llvm.ptr
      cf.br ^bb30
      ^bb30:
      %226 = llvm.load %225 : !llvm.ptr -> i64
      %227 = arith.cmpi slt, %226, %212 : i64
      cf.cond_br %227, ^bb31, ^bb32
      ^bb31:
        %230 = llvm.load %225 : !llvm.ptr -> i64
        %231 = llvm.load %225 : !llvm.ptr -> i64
        %229 = func.call @mulmod(%230, %231, %212) : (i64, i64, i64) -> i64
        %232 = llvm.load %225 : !llvm.ptr -> i64
        %228 = func.call @mulmod(%229, %232, %212) : (i64, i64, i64) -> i64
        %233 = arith.constant 1 : i32
        %235 = arith.extsi %233 : i32 to i64
        %234 = arith.cmpi eq, %228, %235 : i64
        cf.cond_br %234, ^bb33, ^bb34
        ^bb33:
          %236 = llvm.load %225 : !llvm.ptr -> i64
          %237 = llvm.load %221 : !llvm.ptr -> i64
          %238 = llvm.getelementptr %215[%237] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %236, %238 : i64, !llvm.ptr
          %239 = llvm.load %221 : !llvm.ptr -> i64
          %240 = arith.constant 1 : i32
          %242 = arith.extsi %240 : i32 to i64
          %241 = arith.addi %239, %242 : i64
          llvm.store %241, %221 : i64, !llvm.ptr
          cf.br ^bb35
        ^bb34:
          cf.br ^bb35
        ^bb35:
        %243 = llvm.load %225 : !llvm.ptr -> i64
        %244 = arith.constant 1 : i32
        %246 = arith.extsi %244 : i32 to i64
        %245 = arith.addi %243, %246 : i64
        llvm.store %245, %225 : i64, !llvm.ptr
        cf.br ^bb30
      ^bb32:
      %247 = arith.constant 0 : i32
      %248 = arith.extsi %247 : i32 to i64
      %249 = llvm.mlir.constant(1 : i64) : i64
      %250 = llvm.alloca %249 x i64 : (i64) -> !llvm.ptr
      llvm.store %248, %250 : i64, !llvm.ptr
      %251 = arith.constant 0 : i32
      %252 = arith.extsi %251 : i32 to i64
      %253 = llvm.mlir.constant(1 : i64) : i64
      %254 = llvm.alloca %253 x i64 : (i64) -> !llvm.ptr
      llvm.store %252, %254 : i64, !llvm.ptr
      cf.br ^bb36
      ^bb36:
      %255 = llvm.load %254 : !llvm.ptr -> i64
      %256 = llvm.load %194 : !llvm.ptr -> i64
      %257 = arith.cmpi slt, %255, %256 : i64
      cf.cond_br %257, ^bb37, ^bb38
      ^bb37:
        %258 = arith.constant 0 : i32
        %259 = arith.extsi %258 : i32 to i64
        %260 = llvm.mlir.constant(1 : i64) : i64
        %261 = llvm.alloca %260 x i64 : (i64) -> !llvm.ptr
        llvm.store %259, %261 : i64, !llvm.ptr
        cf.br ^bb39
        ^bb39:
        %262 = llvm.load %261 : !llvm.ptr -> i64
        %263 = llvm.load %221 : !llvm.ptr -> i64
        %264 = arith.cmpi slt, %262, %263 : i64
        cf.cond_br %264, ^bb40, ^bb41
        ^bb40:
          %267 = llvm.load %254 : !llvm.ptr -> i64
          %268 = llvm.getelementptr %178[%267] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %266 = llvm.load %268 : !llvm.ptr -> i64
          %270 = llvm.load %261 : !llvm.ptr -> i64
          %271 = llvm.getelementptr %215[%270] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %269 = llvm.load %271 : !llvm.ptr -> i64
          %272 = llvm.load %203 : !llvm.ptr -> i64
          %265 = func.call @crt(%266, %269, %272, %212) : (i64, i64, i64, i64) -> i64
          %273 = llvm.load %250 : !llvm.ptr -> i64
          %274 = llvm.getelementptr %181[%273] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %265, %274 : i64, !llvm.ptr
          %275 = llvm.load %250 : !llvm.ptr -> i64
          %276 = arith.constant 1 : i32
          %278 = arith.extsi %276 : i32 to i64
          %277 = arith.addi %275, %278 : i64
          llvm.store %277, %250 : i64, !llvm.ptr
          %279 = llvm.load %261 : !llvm.ptr -> i64
          %280 = arith.constant 1 : i32
          %282 = arith.extsi %280 : i32 to i64
          %281 = arith.addi %279, %282 : i64
          llvm.store %281, %261 : i64, !llvm.ptr
          cf.br ^bb39
        ^bb41:
        %283 = llvm.load %254 : !llvm.ptr -> i64
        %284 = arith.constant 1 : i32
        %286 = arith.extsi %284 : i32 to i64
        %285 = arith.addi %283, %286 : i64
        llvm.store %285, %254 : i64, !llvm.ptr
        cf.br ^bb36
      ^bb38:
      %287 = arith.constant 0 : i32
      %288 = arith.extsi %287 : i32 to i64
      llvm.store %288, %254 : i64, !llvm.ptr
      cf.br ^bb42
      ^bb42:
      %289 = llvm.load %254 : !llvm.ptr -> i64
      %290 = llvm.load %250 : !llvm.ptr -> i64
      %291 = arith.cmpi slt, %289, %290 : i64
      cf.cond_br %291, ^bb43, ^bb44
      ^bb43:
        %293 = llvm.load %254 : !llvm.ptr -> i64
        %294 = llvm.getelementptr %181[%293] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %292 = llvm.load %294 : !llvm.ptr -> i64
        %295 = llvm.load %254 : !llvm.ptr -> i64
        %296 = llvm.getelementptr %178[%295] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %292, %296 : i64, !llvm.ptr
        %297 = llvm.load %254 : !llvm.ptr -> i64
        %298 = arith.constant 1 : i32
        %300 = arith.extsi %298 : i32 to i64
        %299 = arith.addi %297, %300 : i64
        llvm.store %299, %254 : i64, !llvm.ptr
        cf.br ^bb42
      ^bb44:
      %301 = llvm.load %250 : !llvm.ptr -> i64
      llvm.store %301, %194 : i64, !llvm.ptr
      %302 = llvm.load %203 : !llvm.ptr -> i64
      %303 = arith.muli %302, %212 : i64
      llvm.store %303, %203 : i64, !llvm.ptr
      func.call @free(%215) : (!llvm.ptr) -> ()
      %305 = llvm.load %207 : !llvm.ptr -> i64
      %306 = arith.constant 1 : i32
      %308 = arith.extsi %306 : i32 to i64
      %307 = arith.addi %305, %308 : i64
      llvm.store %307, %207 : i64, !llvm.ptr
      cf.br ^bb27
    ^bb29:
    %309 = arith.constant 0 : i32
    %310 = arith.extsi %309 : i32 to i64
    %311 = llvm.mlir.constant(1 : i64) : i64
    %312 = llvm.alloca %311 x i64 : (i64) -> !llvm.ptr
    llvm.store %310, %312 : i64, !llvm.ptr
    %313 = arith.constant 0 : i32
    %314 = arith.extsi %313 : i32 to i64
    %315 = llvm.mlir.constant(1 : i64) : i64
    %316 = llvm.alloca %315 x i64 : (i64) -> !llvm.ptr
    llvm.store %314, %316 : i64, !llvm.ptr
    cf.br ^bb45
    ^bb45:
    %317 = llvm.load %316 : !llvm.ptr -> i64
    %318 = llvm.load %194 : !llvm.ptr -> i64
    %319 = arith.cmpi slt, %317, %318 : i64
    cf.cond_br %319, ^bb46, ^bb47
    ^bb46:
      %320 = llvm.load %312 : !llvm.ptr -> i64
      %322 = llvm.load %316 : !llvm.ptr -> i64
      %323 = llvm.getelementptr %178[%322] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %321 = llvm.load %323 : !llvm.ptr -> i64
      %324 = arith.addi %320, %321 : i64
      llvm.store %324, %312 : i64, !llvm.ptr
      %325 = llvm.load %316 : !llvm.ptr -> i64
      %326 = arith.constant 1 : i32
      %328 = arith.extsi %326 : i32 to i64
      %327 = arith.addi %325, %328 : i64
      llvm.store %327, %316 : i64, !llvm.ptr
      cf.br ^bb45
    ^bb47:
    %329 = llvm.load %312 : !llvm.ptr -> i64
    %330 = arith.constant 1 : i32
    %332 = arith.extsi %330 : i32 to i64
    %331 = arith.subi %329, %332 : i64
    llvm.store %331, %312 : i64, !llvm.ptr
    %333 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %334 = llvm.load %312 : !llvm.ptr -> i64
    %335 = llvm.call @printf(%333, %334) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%178) : (!llvm.ptr) -> ()
    func.call @free(%181) : (!llvm.ptr) -> ()
    func.call @free(%101) : (!llvm.ptr) -> ()
    %339 = arith.constant 0 : i32
    func.return %339 : i32
  }
}