Problem 753

Fermat Equation: sum over primes p < 6,000,000 of F(p), where F(p) counts ordered triples (a,b,c) with 1<=a,b,c<p and a^3+b^3=c^3 (mod p).

Answer4714126766770661630
Output4714126766770661630
StatusPASS
Native helperno
Runtime30 ms
Peak memory29152 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

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

Flow source

# Project Euler 753
# Fermat Equation: sum over primes p < 6,000,000 of F(p),
# where F(p) counts ordered triples (a,b,c) with 1<=a,b,c<p and a^3+b^3=c^3 (mod p).

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
    function malloc(n: i64) -> ptr<void>
    function memset(s: ptr<void>, c: i32, n: i64) -> ptr<void>
    function sqrt(x: f64) -> f64
}

function isqrt_ll(n: i64) -> i32 {
    if n <= 0 { return 0 }
    let mut r: i32 = (sqrt(n as f64)) as i32
    while (r as i64) * (r as i64) > n {
        r = r - 1
    }
    while ((r + 1) as i64) * ((r + 1) as i64) <= n {
        r = r + 1
    }
    return r
}

function main() -> i32 {
    let limit_exclusive: i32 = 6000000
    let max_p: i32 = limit_exclusive - 1

    # Odd-only sieve: sieve_odd[i] is 1 iff (2*i+1) is prime (i>=1).
    let size: i32 = (max_p / 2) + 1
    let sieve_odd: ptr<i8> = calloc(size as i64, 1)
    memset(sieve_odd as ptr<void>, 1, size as i64)
    sieve_odd[0] = 0

    let r: i32 = isqrt_ll(max_p as i64)
    let mut i: i32 = 1
    while i <= r / 2 {
        if sieve_odd[i] != 0 {
            let p: i32 = 2 * i + 1
            let start: i32 = (p * p) / 2
            let mut j: i32 = start
            while j < size {
                sieve_odd[j] = 0
                j = j + p
            }
        }
        i = i + 1
    }

    # Count primes
    let mut cnt: i32 = 1
    let mut ii: i32 = 1
    while ii < size {
        if sieve_odd[ii] != 0 {
            cnt = cnt + 1
        }
        ii = ii + 1
    }

    # Build primes list
    let primes_list: ptr<i32> = malloc((cnt as i64) * 4)
    let mut nprimes: i32 = 0
    primes_list[nprimes] = 2
    nprimes = nprimes + 1
    let mut iii: i32 = 1
    while iii < size {
        if sieve_odd[iii] != 0 {
            primes_list[nprimes] = 2 * iii + 1
            nprimes = nprimes + 1
        }
        iii = iii + 1
    }
    if nprimes > 0 && primes_list[nprimes - 1] > max_p {
        nprimes = nprimes - 1
    }

    # Build u_by_p for primes p = 1 (mod 3) with 4p = u^2 + 27v^2
    let n: i32 = max_p
    let n4: i64 = 4 * (n as i64)
    let vmax: i32 = isqrt_ll(n4 / 27)
    let u_by_p: ptr<i32> = calloc((n + 1) as i64, 4)

    let mut v: i32 = 1
    while v <= vmax {
        let base: i64 = 27 * (v as i64) * (v as i64)
        let umax: i32 = isqrt_ll(n4 - base)
        let mut u: i32 = v & 1
        while u <= umax {
            let p: i64 = ((u as i64) * (u as i64) + base) >> 2
            if p <= (n as i64) && (p % 3 == 1) {
                if p != 2 && (p & 1) != 0 {
                    if sieve_odd[(p >> 1) as i32] != 0 {
                        if u_by_p[p as i32] == 0 {
                            u_by_p[p as i32] = u
                        }
                    }
                }
            }
            u = u + 2
        }
        v = v + 1
    }

    # Compute total
    let mut total: i64 = 0
    let mut idx: i32 = 0
    while idx < nprimes {
        let p: i32 = primes_list[idx]
        if p >= limit_exclusive {
            break
        }
        if p == 3 {
            total = total + 2
        } else {
            if p % 3 == 2 {
                total = total + ((p as i64) - 1) * ((p as i64) - 2)
            } else {
                let u: i32 = u_by_p[p]
                let mut ap: i32 = 0
                if u % 3 == 2 {
                    ap = u
                } else {
                    ap = -u
                }
                total = total + ((p as i64) - 1) * ((p as i64) - (ap as i64) - 8)
            }
        }
        idx = idx + 1
    }

    printf("%lld\n", total)

    free(sieve_odd as ptr<void>)
    free(primes_list as ptr<void>)
    free(u_by_p as ptr<void>)

    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; }

int32_t isqrt_ll_i64(int64_t n);
int32_t main(void);






int32_t isqrt_ll_i64(int64_t n) {
    if (n <= 0) {
        return 0;
    }
    int32_t r = ((int32_t)(sqrt(((double)(n)))));
    while ((((int64_t)(r)) * ((int64_t)(r))) > n) {
        r = (r - 1);
    }
    while ((((int64_t)((r + 1))) * ((int64_t)((r + 1)))) <= n) {
        r = (r + 1);
    }
    return r;
}

int32_t main(void) {
    int32_t limit_exclusive = 6000000;
    int32_t max_p = (limit_exclusive - 1);
    int32_t size = (FLOW_CHECKED_DIV((max_p), (2)) + 1);
    int8_t* sieve_odd = (int8_t*)(calloc(((int64_t)(size)), 1));
    memset(((void*)(sieve_odd)), 1, ((int64_t)(size)));
    sieve_odd[0] = 0;
    int32_t r = isqrt_ll_i64(((int64_t)(max_p)));
    int32_t i = 1;
    while (i <= FLOW_CHECKED_DIV((r), (2))) {
        if (sieve_odd[i] != 0) {
            int32_t p = ((2 * i) + 1);
            int32_t start = FLOW_CHECKED_DIV(((p * p)), (2));
            int32_t j = start;
            while (j < size) {
                sieve_odd[j] = 0;
                j = (j + p);
            }
        }
        i = (i + 1);
    }
    int32_t cnt = 1;
    int32_t ii = 1;
    while (ii < size) {
        if (sieve_odd[ii] != 0) {
            cnt = (cnt + 1);
        }
        ii = (ii + 1);
    }
    int32_t* primes_list = (int32_t*)(malloc((((int64_t)(cnt)) * 4)));
    int32_t nprimes = 0;
    primes_list[nprimes] = 2;
    nprimes = (nprimes + 1);
    int32_t iii = 1;
    while (iii < size) {
        if (sieve_odd[iii] != 0) {
            primes_list[nprimes] = ((2 * iii) + 1);
            nprimes = (nprimes + 1);
        }
        iii = (iii + 1);
    }
    if ((nprimes > 0 && primes_list[(nprimes - 1)] > max_p)) {
        nprimes = (nprimes - 1);
    }
    int32_t n = max_p;
    int64_t n4 = (4 * ((int64_t)(n)));
    int32_t vmax = isqrt_ll_i64(FLOW_CHECKED_DIV((n4), (27)));
    int32_t* u_by_p = (int32_t*)(calloc(((int64_t)((n + 1))), 4));
    int32_t v = 1;
    while (v <= vmax) {
        int64_t base = ((27 * ((int64_t)(v))) * ((int64_t)(v)));
        int32_t umax = isqrt_ll_i64((n4 - base));
        int32_t u = (v & 1);
        while (u <= umax) {
            int64_t p = FLOW_CHECKED_SHR((((((int64_t)(u)) * ((int64_t)(u))) + base)), (2));
            if ((p <= ((int64_t)(n)) && FLOW_CHECKED_MOD((p), (3)) == 1)) {
                if ((p != 2 && (p & 1) != 0)) {
                    if (sieve_odd[((int32_t)(FLOW_CHECKED_SHR((p), (1))))] != 0) {
                        if (u_by_p[((int32_t)(p))] == 0) {
                            u_by_p[((int32_t)(p))] = u;
                        }
                    }
                }
            }
            u = (u + 2);
        }
        v = (v + 1);
    }
    int64_t total = 0;
    int32_t idx = 0;
    while (idx < nprimes) {
        int32_t p = primes_list[idx];
        if (p >= limit_exclusive) {
            break;
        }
        if (p == 3) {
            total = (total + 2);
        } else {
            if (FLOW_CHECKED_MOD((p), (3)) == 2) {
                total = (total + ((((int64_t)(p)) - 1) * (((int64_t)(p)) - 2)));
            } else {
                int32_t u = u_by_p[p];
                int32_t ap = 0;
                if (FLOW_CHECKED_MOD((u), (3)) == 2) {
                    ap = u;
                } else {
                    ap = (-u);
                }
                total = (total + ((((int64_t)(p)) - 1) * ((((int64_t)(p)) - ((int64_t)(ap))) - 8)));
            }
        }
        idx = (idx + 1);
    }
    printf("%lld\n", total);
    free(((void*)(sieve_odd)));
    free(((void*)(primes_list)));
    free(((void*)(u_by_p)));
    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 private @malloc(i64) -> !llvm.ptr
  func.func private @memset(!llvm.ptr, i32, i64) -> !llvm.ptr
  func.func private @sqrt(f64) -> f64
  func.func @isqrt_ll(%arg0: i64) -> i32 {
    %0 = arith.constant 0 : i32
    %2 = arith.extsi %0 : i32 to i64
    %1 = arith.cmpi sle, %arg0, %2 : i64
    cf.cond_br %1, ^bb0, ^bb1
    ^bb0:
      %3 = arith.constant 0 : i32
      func.return %3 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %4 = arith.sitofp %arg0 : i64 to f64
    %5 = math.sqrt %4 : f64
    %6 = arith.fptosi %5 : f64 to i32
    %7 = llvm.mlir.constant(1 : i64) : i64
    %8 = llvm.alloca %7 x i32 : (i64) -> !llvm.ptr
    llvm.store %6, %8 : i32, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %9 = llvm.load %8 : !llvm.ptr -> i32
    %10 = arith.extsi %9 : i32 to i64
    %11 = llvm.load %8 : !llvm.ptr -> i32
    %12 = arith.extsi %11 : i32 to i64
    %13 = arith.muli %10, %12 : i64
    %14 = arith.cmpi sgt, %13, %arg0 : i64
    cf.cond_br %14, ^bb4, ^bb5
    ^bb4:
      %15 = llvm.load %8 : !llvm.ptr -> i32
      %16 = arith.constant 1 : i32
      %17 = arith.subi %15, %16 : i32
      llvm.store %17, %8 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    cf.br ^bb6
    ^bb6:
    %18 = llvm.load %8 : !llvm.ptr -> i32
    %19 = arith.constant 1 : i32
    %20 = arith.addi %18, %19 : i32
    %21 = arith.extsi %20 : i32 to i64
    %22 = llvm.load %8 : !llvm.ptr -> i32
    %23 = arith.constant 1 : i32
    %24 = arith.addi %22, %23 : i32
    %25 = arith.extsi %24 : i32 to i64
    %26 = arith.muli %21, %25 : i64
    %27 = arith.cmpi sle, %26, %arg0 : i64
    cf.cond_br %27, ^bb7, ^bb8
    ^bb7:
      %28 = llvm.load %8 : !llvm.ptr -> i32
      %29 = arith.constant 1 : i32
      %30 = arith.addi %28, %29 : i32
      llvm.store %30, %8 : i32, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %31 = llvm.load %8 : !llvm.ptr -> i32
    func.return %31 : i32
  }
  func.func @main() -> i32 {
    %32 = arith.constant 6000000 : i32
    %33 = arith.constant 1 : i32
    %34 = arith.subi %32, %33 : i32
    %35 = arith.constant 2 : i32
    %36 = arith.divsi %34, %35 : i32
    %37 = arith.constant 1 : i32
    %38 = arith.addi %36, %37 : i32
    %40 = arith.extsi %38 : i32 to i64
    %41 = arith.constant 1 : i32
    %42 = arith.extsi %41 : i32 to i64
    %39 = func.call @calloc(%40, %42) : (i64, i64) -> !llvm.ptr
    %44 = arith.constant 1 : i32
    %45 = arith.extsi %38 : i32 to i64
    %43 = func.call @memset(%39, %44, %45) : (!llvm.ptr, i32, i64) -> !llvm.ptr
    %46 = arith.constant 0 : i32
    %47 = arith.constant 0 : i32
    %48 = arith.trunci %46 : i32 to i8
    %49 = arith.extsi %47 : i32 to i64
    %50 = llvm.getelementptr %39[%49] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %48, %50 : i8, !llvm.ptr
    %52 = arith.extsi %34 : i32 to i64
    %51 = func.call @isqrt_ll(%52) : (i64) -> i32
    %53 = arith.constant 1 : i32
    %54 = llvm.mlir.constant(1 : i64) : i64
    %55 = llvm.alloca %54 x i32 : (i64) -> !llvm.ptr
    llvm.store %53, %55 : i32, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %56 = llvm.load %55 : !llvm.ptr -> i32
    %57 = arith.constant 2 : i32
    %58 = arith.divsi %51, %57 : i32
    %59 = arith.cmpi sle, %56, %58 : i32
    cf.cond_br %59, ^bb10, ^bb11
    ^bb10:
      %61 = llvm.load %55 : !llvm.ptr -> i32
      %62 = arith.extsi %61 : i32 to i64
      %63 = llvm.getelementptr %39[%62] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %60 = llvm.load %63 : !llvm.ptr -> i8
      %64 = arith.constant 0 : i32
      %66 = arith.extsi %60 : i8 to i32
      %65 = arith.cmpi ne, %66, %64 : i32
      cf.cond_br %65, ^bb12, ^bb13
      ^bb12:
        %67 = arith.constant 2 : i32
        %68 = llvm.load %55 : !llvm.ptr -> i32
        %69 = arith.muli %67, %68 : i32
        %70 = arith.constant 1 : i32
        %71 = arith.addi %69, %70 : i32
        %72 = arith.muli %71, %71 : i32
        %73 = arith.constant 2 : i32
        %74 = arith.divsi %72, %73 : i32
        %75 = llvm.mlir.constant(1 : i64) : i64
        %76 = llvm.alloca %75 x i32 : (i64) -> !llvm.ptr
        llvm.store %74, %76 : i32, !llvm.ptr
        cf.br ^bb15
        ^bb15:
        %77 = llvm.load %76 : !llvm.ptr -> i32
        %78 = arith.cmpi slt, %77, %38 : i32
        cf.cond_br %78, ^bb16, ^bb17
        ^bb16:
          %79 = arith.constant 0 : i32
          %80 = llvm.load %76 : !llvm.ptr -> i32
          %81 = arith.trunci %79 : i32 to i8
          %82 = arith.extsi %80 : i32 to i64
          %83 = llvm.getelementptr %39[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %81, %83 : i8, !llvm.ptr
          %84 = llvm.load %76 : !llvm.ptr -> i32
          %85 = arith.addi %84, %71 : i32
          llvm.store %85, %76 : i32, !llvm.ptr
          cf.br ^bb15
        ^bb17:
        cf.br ^bb14
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %86 = llvm.load %55 : !llvm.ptr -> i32
      %87 = arith.constant 1 : i32
      %88 = arith.addi %86, %87 : i32
      llvm.store %88, %55 : i32, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %89 = arith.constant 1 : i32
    %90 = llvm.mlir.constant(1 : i64) : i64
    %91 = llvm.alloca %90 x i32 : (i64) -> !llvm.ptr
    llvm.store %89, %91 : i32, !llvm.ptr
    %92 = arith.constant 1 : i32
    %93 = llvm.mlir.constant(1 : i64) : i64
    %94 = llvm.alloca %93 x i32 : (i64) -> !llvm.ptr
    llvm.store %92, %94 : i32, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %95 = llvm.load %94 : !llvm.ptr -> i32
    %96 = arith.cmpi slt, %95, %38 : i32
    cf.cond_br %96, ^bb19, ^bb20
    ^bb19:
      %98 = llvm.load %94 : !llvm.ptr -> i32
      %99 = arith.extsi %98 : i32 to i64
      %100 = llvm.getelementptr %39[%99] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %97 = llvm.load %100 : !llvm.ptr -> i8
      %101 = arith.constant 0 : i32
      %103 = arith.extsi %97 : i8 to i32
      %102 = arith.cmpi ne, %103, %101 : i32
      cf.cond_br %102, ^bb21, ^bb22
      ^bb21:
        %104 = llvm.load %91 : !llvm.ptr -> i32
        %105 = arith.constant 1 : i32
        %106 = arith.addi %104, %105 : i32
        llvm.store %106, %91 : i32, !llvm.ptr
        cf.br ^bb23
      ^bb22:
        cf.br ^bb23
      ^bb23:
      %107 = llvm.load %94 : !llvm.ptr -> i32
      %108 = arith.constant 1 : i32
      %109 = arith.addi %107, %108 : i32
      llvm.store %109, %94 : i32, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %111 = llvm.load %91 : !llvm.ptr -> i32
    %112 = arith.extsi %111 : i32 to i64
    %113 = arith.constant 4 : i32
    %115 = arith.extsi %113 : i32 to i64
    %114 = arith.muli %112, %115 : i64
    %110 = func.call @malloc(%114) : (i64) -> !llvm.ptr
    %116 = arith.constant 0 : i32
    %117 = llvm.mlir.constant(1 : i64) : i64
    %118 = llvm.alloca %117 x i32 : (i64) -> !llvm.ptr
    llvm.store %116, %118 : i32, !llvm.ptr
    %119 = arith.constant 2 : i32
    %120 = llvm.load %118 : !llvm.ptr -> i32
    %121 = arith.extsi %120 : i32 to i64
    %122 = llvm.getelementptr %110[%121] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    llvm.store %119, %122 : i32, !llvm.ptr
    %123 = llvm.load %118 : !llvm.ptr -> i32
    %124 = arith.constant 1 : i32
    %125 = arith.addi %123, %124 : i32
    llvm.store %125, %118 : i32, !llvm.ptr
    %126 = arith.constant 1 : i32
    %127 = llvm.mlir.constant(1 : i64) : i64
    %128 = llvm.alloca %127 x i32 : (i64) -> !llvm.ptr
    llvm.store %126, %128 : i32, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %129 = llvm.load %128 : !llvm.ptr -> i32
    %130 = arith.cmpi slt, %129, %38 : i32
    cf.cond_br %130, ^bb25, ^bb26
    ^bb25:
      %132 = llvm.load %128 : !llvm.ptr -> i32
      %133 = arith.extsi %132 : i32 to i64
      %134 = llvm.getelementptr %39[%133] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %131 = llvm.load %134 : !llvm.ptr -> i8
      %135 = arith.constant 0 : i32
      %137 = arith.extsi %131 : i8 to i32
      %136 = arith.cmpi ne, %137, %135 : i32
      cf.cond_br %136, ^bb27, ^bb28
      ^bb27:
        %138 = arith.constant 2 : i32
        %139 = llvm.load %128 : !llvm.ptr -> i32
        %140 = arith.muli %138, %139 : i32
        %141 = arith.constant 1 : i32
        %142 = arith.addi %140, %141 : i32
        %143 = llvm.load %118 : !llvm.ptr -> i32
        %144 = arith.extsi %143 : i32 to i64
        %145 = llvm.getelementptr %110[%144] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        llvm.store %142, %145 : i32, !llvm.ptr
        %146 = llvm.load %118 : !llvm.ptr -> i32
        %147 = arith.constant 1 : i32
        %148 = arith.addi %146, %147 : i32
        llvm.store %148, %118 : i32, !llvm.ptr
        cf.br ^bb29
      ^bb28:
        cf.br ^bb29
      ^bb29:
      %149 = llvm.load %128 : !llvm.ptr -> i32
      %150 = arith.constant 1 : i32
      %151 = arith.addi %149, %150 : i32
      llvm.store %151, %128 : i32, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %152 = llvm.load %118 : !llvm.ptr -> i32
    %153 = arith.constant 0 : i32
    %154 = arith.cmpi sgt, %152, %153 : i32
    %155 = scf.if %154 -> (i1) {
      %157 = llvm.load %118 : !llvm.ptr -> i32
      %158 = arith.constant 1 : i32
      %159 = arith.subi %157, %158 : i32
      %160 = arith.extsi %159 : i32 to i64
      %161 = llvm.getelementptr %110[%160] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %156 = llvm.load %161 : !llvm.ptr -> i32
      %162 = arith.cmpi sgt, %156, %34 : i32
      scf.yield %162 : i1
    } else {
      %163 = arith.constant false
      scf.yield %163 : i1
    }
    cf.cond_br %155, ^bb30, ^bb31
    ^bb30:
      %164 = llvm.load %118 : !llvm.ptr -> i32
      %165 = arith.constant 1 : i32
      %166 = arith.subi %164, %165 : i32
      llvm.store %166, %118 : i32, !llvm.ptr
      cf.br ^bb32
    ^bb31:
      cf.br ^bb32
    ^bb32:
    %167 = arith.constant 4 : i32
    %168 = arith.extsi %34 : i32 to i64
    %170 = arith.extsi %167 : i32 to i64
    %169 = arith.muli %170, %168 : i64
    %172 = arith.constant 27 : i32
    %174 = arith.extsi %172 : i32 to i64
    %173 = arith.divsi %169, %174 : i64
    %171 = func.call @isqrt_ll(%173) : (i64) -> i32
    %176 = arith.constant 1 : i32
    %177 = arith.addi %34, %176 : i32
    %178 = arith.extsi %177 : i32 to i64
    %179 = arith.constant 4 : i32
    %180 = arith.extsi %179 : i32 to i64
    %175 = func.call @calloc(%178, %180) : (i64, i64) -> !llvm.ptr
    %181 = arith.constant 1 : i32
    %182 = llvm.mlir.constant(1 : i64) : i64
    %183 = llvm.alloca %182 x i32 : (i64) -> !llvm.ptr
    llvm.store %181, %183 : i32, !llvm.ptr
    cf.br ^bb33
    ^bb33:
    %184 = llvm.load %183 : !llvm.ptr -> i32
    %185 = arith.cmpi sle, %184, %171 : i32
    cf.cond_br %185, ^bb34, ^bb35
    ^bb34:
      %186 = arith.constant 27 : i32
      %187 = llvm.load %183 : !llvm.ptr -> i32
      %188 = arith.extsi %187 : i32 to i64
      %190 = arith.extsi %186 : i32 to i64
      %189 = arith.muli %190, %188 : i64
      %191 = llvm.load %183 : !llvm.ptr -> i32
      %192 = arith.extsi %191 : i32 to i64
      %193 = arith.muli %189, %192 : i64
      %195 = arith.subi %169, %193 : i64
      %194 = func.call @isqrt_ll(%195) : (i64) -> i32
      %196 = llvm.load %183 : !llvm.ptr -> i32
      %197 = arith.constant 1 : i32
      %198 = arith.andi %196, %197 : i32
      %199 = llvm.mlir.constant(1 : i64) : i64
      %200 = llvm.alloca %199 x i32 : (i64) -> !llvm.ptr
      llvm.store %198, %200 : i32, !llvm.ptr
      cf.br ^bb36
      ^bb36:
      %201 = llvm.load %200 : !llvm.ptr -> i32
      %202 = arith.cmpi sle, %201, %194 : i32
      cf.cond_br %202, ^bb37, ^bb38
      ^bb37:
        %203 = llvm.load %200 : !llvm.ptr -> i32
        %204 = arith.extsi %203 : i32 to i64
        %205 = llvm.load %200 : !llvm.ptr -> i32
        %206 = arith.extsi %205 : i32 to i64
        %207 = arith.muli %204, %206 : i64
        %208 = arith.addi %207, %193 : i64
        %209 = arith.constant 2 : i32
        %211 = arith.extsi %209 : i32 to i64
        %210 = arith.shrsi %208, %211 : i64
        %212 = arith.extsi %34 : i32 to i64
        %213 = arith.cmpi sle, %210, %212 : i64
        %214 = scf.if %213 -> (i1) {
          %215 = arith.constant 3 : i32
          %217 = arith.extsi %215 : i32 to i64
          %216 = arith.remsi %210, %217 : i64
          %218 = arith.constant 1 : i32
          %220 = arith.extsi %218 : i32 to i64
          %219 = arith.cmpi eq, %216, %220 : i64
          scf.yield %219 : i1
        } else {
          %221 = arith.constant false
          scf.yield %221 : i1
        }
        cf.cond_br %214, ^bb39, ^bb40
        ^bb39:
          %222 = arith.constant 2 : i32
          %224 = arith.extsi %222 : i32 to i64
          %223 = arith.cmpi ne, %210, %224 : i64
          %225 = scf.if %223 -> (i1) {
            %226 = arith.constant 1 : i32
            %228 = arith.extsi %226 : i32 to i64
            %227 = arith.andi %210, %228 : i64
            %229 = arith.constant 0 : i32
            %231 = arith.extsi %229 : i32 to i64
            %230 = arith.cmpi ne, %227, %231 : i64
            scf.yield %230 : i1
          } else {
            %232 = arith.constant false
            scf.yield %232 : i1
          }
          cf.cond_br %225, ^bb42, ^bb43
          ^bb42:
            %234 = arith.constant 1 : i32
            %236 = arith.extsi %234 : i32 to i64
            %235 = arith.shrsi %210, %236 : i64
            %237 = arith.trunci %235 : i64 to i32
            %238 = arith.extsi %237 : i32 to i64
            %239 = llvm.getelementptr %39[%238] : (!llvm.ptr, i64) -> !llvm.ptr, i8
            %233 = llvm.load %239 : !llvm.ptr -> i8
            %240 = arith.constant 0 : i32
            %242 = arith.extsi %233 : i8 to i32
            %241 = arith.cmpi ne, %242, %240 : i32
            cf.cond_br %241, ^bb45, ^bb46
            ^bb45:
              %244 = arith.trunci %210 : i64 to i32
              %245 = arith.extsi %244 : i32 to i64
              %246 = llvm.getelementptr %175[%245] : (!llvm.ptr, i64) -> !llvm.ptr, i32
              %243 = llvm.load %246 : !llvm.ptr -> i32
              %247 = arith.constant 0 : i32
              %248 = arith.cmpi eq, %243, %247 : i32
              cf.cond_br %248, ^bb48, ^bb49
              ^bb48:
                %249 = llvm.load %200 : !llvm.ptr -> i32
                %250 = arith.trunci %210 : i64 to i32
                %251 = arith.extsi %250 : i32 to i64
                %252 = llvm.getelementptr %175[%251] : (!llvm.ptr, i64) -> !llvm.ptr, i32
                llvm.store %249, %252 : i32, !llvm.ptr
                cf.br ^bb50
              ^bb49:
                cf.br ^bb50
              ^bb50:
              cf.br ^bb47
            ^bb46:
              cf.br ^bb47
            ^bb47:
            cf.br ^bb44
          ^bb43:
            cf.br ^bb44
          ^bb44:
          cf.br ^bb41
        ^bb40:
          cf.br ^bb41
        ^bb41:
        %253 = llvm.load %200 : !llvm.ptr -> i32
        %254 = arith.constant 2 : i32
        %255 = arith.addi %253, %254 : i32
        llvm.store %255, %200 : i32, !llvm.ptr
        cf.br ^bb36
      ^bb38:
      %256 = llvm.load %183 : !llvm.ptr -> i32
      %257 = arith.constant 1 : i32
      %258 = arith.addi %256, %257 : i32
      llvm.store %258, %183 : i32, !llvm.ptr
      cf.br ^bb33
    ^bb35:
    %259 = arith.constant 0 : i32
    %260 = arith.extsi %259 : i32 to i64
    %261 = llvm.mlir.constant(1 : i64) : i64
    %262 = llvm.alloca %261 x i64 : (i64) -> !llvm.ptr
    llvm.store %260, %262 : i64, !llvm.ptr
    %263 = arith.constant 0 : i32
    %264 = llvm.mlir.constant(1 : i64) : i64
    %265 = llvm.alloca %264 x i32 : (i64) -> !llvm.ptr
    llvm.store %263, %265 : i32, !llvm.ptr
    cf.br ^bb51
    ^bb51:
    %266 = llvm.load %265 : !llvm.ptr -> i32
    %267 = llvm.load %118 : !llvm.ptr -> i32
    %268 = arith.cmpi slt, %266, %267 : i32
    cf.cond_br %268, ^bb52, ^bb53
    ^bb52:
      %270 = llvm.load %265 : !llvm.ptr -> i32
      %271 = arith.extsi %270 : i32 to i64
      %272 = llvm.getelementptr %110[%271] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %269 = llvm.load %272 : !llvm.ptr -> i32
      %273 = arith.cmpi sge, %269, %32 : i32
      cf.cond_br %273, ^bb54, ^bb55
      ^bb54:
        cf.br ^bb53
      ^bb55:
        cf.br ^bb56
      ^bb56:
      %274 = arith.constant 3 : i32
      %275 = arith.cmpi eq, %269, %274 : i32
      cf.cond_br %275, ^bb57, ^bb58
      ^bb57:
        %276 = llvm.load %262 : !llvm.ptr -> i64
        %277 = arith.constant 2 : i32
        %279 = arith.extsi %277 : i32 to i64
        %278 = arith.addi %276, %279 : i64
        llvm.store %278, %262 : i64, !llvm.ptr
        cf.br ^bb59
      ^bb58:
        %280 = arith.constant 3 : i32
        %281 = arith.remsi %269, %280 : i32
        %282 = arith.constant 2 : i32
        %283 = arith.cmpi eq, %281, %282 : i32
        cf.cond_br %283, ^bb60, ^bb61
        ^bb60:
          %284 = llvm.load %262 : !llvm.ptr -> i64
          %285 = arith.extsi %269 : i32 to i64
          %286 = arith.constant 1 : i32
          %288 = arith.extsi %286 : i32 to i64
          %287 = arith.subi %285, %288 : i64
          %289 = arith.extsi %269 : i32 to i64
          %290 = arith.constant 2 : i32
          %292 = arith.extsi %290 : i32 to i64
          %291 = arith.subi %289, %292 : i64
          %293 = arith.muli %287, %291 : i64
          %294 = arith.addi %284, %293 : i64
          llvm.store %294, %262 : i64, !llvm.ptr
          cf.br ^bb62
        ^bb61:
          %296 = arith.extsi %269 : i32 to i64
          %297 = llvm.getelementptr %175[%296] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %295 = llvm.load %297 : !llvm.ptr -> i32
          %298 = arith.constant 0 : i32
          %299 = llvm.mlir.constant(1 : i64) : i64
          %300 = llvm.alloca %299 x i32 : (i64) -> !llvm.ptr
          llvm.store %298, %300 : i32, !llvm.ptr
          %301 = arith.constant 3 : i32
          %302 = arith.remsi %295, %301 : i32
          %303 = arith.constant 2 : i32
          %304 = arith.cmpi eq, %302, %303 : i32
          cf.cond_br %304, ^bb63, ^bb64
          ^bb63:
            llvm.store %295, %300 : i32, !llvm.ptr
            cf.br ^bb65
          ^bb64:
            %306 = arith.constant 0 : i32
            %305 = arith.subi %306, %295 : i32
            llvm.store %305, %300 : i32, !llvm.ptr
            cf.br ^bb65
          ^bb65:
          %307 = llvm.load %262 : !llvm.ptr -> i64
          %308 = arith.extsi %269 : i32 to i64
          %309 = arith.constant 1 : i32
          %311 = arith.extsi %309 : i32 to i64
          %310 = arith.subi %308, %311 : i64
          %312 = arith.extsi %269 : i32 to i64
          %313 = llvm.load %300 : !llvm.ptr -> i32
          %314 = arith.extsi %313 : i32 to i64
          %315 = arith.subi %312, %314 : i64
          %316 = arith.constant 8 : i32
          %318 = arith.extsi %316 : i32 to i64
          %317 = arith.subi %315, %318 : i64
          %319 = arith.muli %310, %317 : i64
          %320 = arith.addi %307, %319 : i64
          llvm.store %320, %262 : i64, !llvm.ptr
          cf.br ^bb62
        ^bb62:
        cf.br ^bb59
      ^bb59:
      %321 = llvm.load %265 : !llvm.ptr -> i32
      %322 = arith.constant 1 : i32
      %323 = arith.addi %321, %322 : i32
      llvm.store %323, %265 : i32, !llvm.ptr
      cf.br ^bb51
    ^bb53:
    %324 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %325 = llvm.load %262 : !llvm.ptr -> i64
    %326 = llvm.call @printf(%324, %325) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%39) : (!llvm.ptr) -> ()
    func.call @free(%110) : (!llvm.ptr) -> ()
    func.call @free(%175) : (!llvm.ptr) -> ()
    %330 = arith.constant 0 : i32
    func.return %330 : i32
  }
}