Problem 404

C(10^17): count of (a,b,c) from ellipse intersections.

Answer1199215615081353
Output1199215615081353
StatusPASS
Native helperno
Runtime4290 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n^2 log n)
Space complexityO(1)O(n)
ApproachFlow solutionSweep line or pairwise check
VerdictOptimal

Flow source

# Project Euler 404
# C(10^17): count of (a,b,c) from ellipse intersections.

import euler.nt { isqrt }

function gcd_abs(a0: i64, b0: i64) -> i64 {
    let mut a: i64 = a0
    let mut b: i64 = b0
    if a < 0 { a = 0 - a }
    if b < 0 { b = 0 - b }
    while b != 0 {
        let t: i64 = a % b
        a = b
        b = t
    }
    return a
}

function mod_euclid(a: i64, m: i64) -> i64 {
    let mut r: i64 = a % m
    if r < 0 { r = r + m }
    return r
}

function is_odd(n: i64) -> bool {
    return mod_euclid(n, 2) == 1
}

function main() -> i32 {
    let LIMIT: i64 = 100000000000000000
    let m_max: i64 = ((2 * LIMIT) |> isqrt |> isqrt) + 1
    let mut total: i64 = 0
    let mut m: i64 = 1
    while m <= m_max {
        let m2: i64 = m * m
        let mut n_start: i64 = 0 - (m / 3)
        if m % 3 == 0 {
            n_start = n_start + 1
        }
        let mut n: i64 = n_start
        while n < 0 {
            if !(is_odd(m) && is_odd(n)) {
                if mod_euclid(m - 2 * n, 5) != 0 {
                    if gcd_abs(m, 0 - n) == 1 {
                        let n2: i64 = n * n
                        let mn: i64 = m * n
                        let p: i64 = m2 - n2 - 4 * mn
                        let r: i64 = m2 - n2 + mn
                        let a0: i128 = (p as i128) * (r as i128)
                        if a0 > (0 as i128) && a0 <= (LIMIT as i128) {
                            total = total + ((LIMIT as i128) / a0) as i64
                        }
                    }
                }
            }
            n = n + 1
        }

        n = m / 2 + 1
        while n < m {
            if !(is_odd(m) && is_odd(n)) {
                if mod_euclid(m - 2 * n, 5) != 0 {
                    if gcd_abs(m, n) == 1 {
                        let n2: i64 = n * n
                        let mn: i64 = m * n
                        let p: i64 = n2 + 4 * mn - m2
                        let r: i64 = m2 - n2 + mn
                        let a0: i128 = (p as i128) * (r as i128)
                        if a0 > (0 as i128) && a0 <= (LIMIT as i128) {
                            total = total + ((LIMIT as i128) / a0) as i64
                        }
                    }
                }
            }
            n = n + 1
        }
        m = m + 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 gcd_i64_i64(int64_t a0, int64_t b0);
int64_t lcm_i64_i64(int64_t a, int64_t b);
int64_t isqrt_i64(int64_t n);
int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
bool is_prime_i64(int64_t n);
int64_t gcd_abs_i64_i64(int64_t a0, int64_t b0);
int64_t mod_euclid_i64_i64(int64_t a, int64_t m);
bool is_odd_i64(int64_t n);
int32_t main(void);

int64_t gcd_i64_i64(int64_t a0, int64_t b0) {
    int64_t a = a0;
    int64_t b = b0;
    while (b != 0) {
        int64_t t = FLOW_CHECKED_MOD((a), (b));
        a = b;
        b = t;
    }
    return a;
}

int64_t lcm_i64_i64(int64_t a, int64_t b) {
    if ((a == 0 || b == 0)) {
        return 0;
    }
    return (FLOW_CHECKED_DIV((a), (gcd_i64_i64(a, b))) * b);
}

int64_t isqrt_i64(int64_t n) {
    if (n < 2) {
        return n;
    }
    int64_t x = n;
    int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
    while (y < x) {
        x = y;
        y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
    }
    return x;
}

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));
    int64_t result = 0;
    while (b > 0) {
        if (FLOW_CHECKED_MOD((b), (2)) == 1) {
            result = FLOW_CHECKED_MOD(((result + a)), (mod));
        }
        a = FLOW_CHECKED_MOD(((a * 2)), (mod));
        b = FLOW_CHECKED_DIV((b), (2));
    }
    return result;
}

int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
    if (mod == 1) {
        return 0;
    }
    int64_t result = 1;
    int64_t b = FLOW_CHECKED_MOD((base), (mod));
    int64_t e = exp;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            result = mulmod_i64_i64_i64(result, b, mod);
        }
        b = mulmod_i64_i64_i64(b, b, mod);
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return result;
}

bool is_prime_i64(int64_t n) {
    if (n < 2) {
        return 0;
    }
    if (n < 4) {
        return 1;
    }
    if ((FLOW_CHECKED_MOD((n), (2)) == 0 || FLOW_CHECKED_MOD((n), (3)) == 0)) {
        return 0;
    }
    int64_t i = 5;
    while ((i * i) <= n) {
        if ((FLOW_CHECKED_MOD((n), (i)) == 0 || FLOW_CHECKED_MOD((n), ((i + 2))) == 0)) {
            return 0;
        }
        i = (i + 6);
    }
    return 1;
}

int64_t gcd_abs_i64_i64(int64_t a0, int64_t b0) {
    int64_t a = a0;
    int64_t b = b0;
    if (a < 0) {
        a = (0 - a);
    }
    if (b < 0) {
        b = (0 - b);
    }
    while (b != 0) {
        int64_t t = FLOW_CHECKED_MOD((a), (b));
        a = b;
        b = t;
    }
    return a;
}

int64_t mod_euclid_i64_i64(int64_t a, int64_t m) {
    int64_t r = FLOW_CHECKED_MOD((a), (m));
    if (r < 0) {
        r = (r + m);
    }
    return r;
}

bool is_odd_i64(int64_t n) {
    return mod_euclid_i64_i64(n, 2) == 1;
}

int32_t main(void) {
    int64_t LIMIT = 100000000000000000;
    int64_t m_max = (isqrt_i64(isqrt_i64((2 * LIMIT))) + 1);
    int64_t total = 0;
    int64_t m = 1;
    while (m <= m_max) {
        int64_t m2 = (m * m);
        int64_t n_start = (0 - FLOW_CHECKED_DIV((m), (3)));
        if (FLOW_CHECKED_MOD((m), (3)) == 0) {
            n_start = (n_start + 1);
        }
        int64_t n = n_start;
        while (n < 0) {
            if ((!((is_odd_i64(m) && is_odd_i64(n))))) {
                if (mod_euclid_i64_i64((m - (2 * n)), 5) != 0) {
                    if (gcd_abs_i64_i64(m, (0 - n)) == 1) {
                        int64_t n2 = (n * n);
                        int64_t mn = (m * n);
                        int64_t p = ((m2 - n2) - (4 * mn));
                        int64_t r = ((m2 - n2) + mn);
                        __int128 a0 = (((__int128)(p)) * ((__int128)(r)));
                        if ((a0 > ((__int128)(0)) && a0 <= ((__int128)(LIMIT)))) {
                            total = (total + ((int64_t)(FLOW_CHECKED_DIV((((__int128)(LIMIT))), (a0)))));
                        }
                    }
                }
            }
            n = (n + 1);
        }
        n = (FLOW_CHECKED_DIV((m), (2)) + 1);
        while (n < m) {
            if ((!((is_odd_i64(m) && is_odd_i64(n))))) {
                if (mod_euclid_i64_i64((m - (2 * n)), 5) != 0) {
                    if (gcd_abs_i64_i64(m, n) == 1) {
                        int64_t n2 = (n * n);
                        int64_t mn = (m * n);
                        int64_t p = ((n2 + (4 * mn)) - m2);
                        int64_t r = ((m2 - n2) + mn);
                        __int128 a0 = (((__int128)(p)) * ((__int128)(r)));
                        if ((a0 > ((__int128)(0)) && a0 <= ((__int128)(LIMIT)))) {
                            total = (total + ((int64_t)(FLOW_CHECKED_DIV((((__int128)(LIMIT))), (a0)))));
                        }
                    }
                }
            }
            n = (n + 1);
        }
        m = (m + 1);
    }
    printf("%lld\n", total);
    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 @gcd(%arg0: i64, %arg1: i64) -> i64 {
    %0 = llvm.mlir.constant(1 : i64) : i64
    %1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %1 : i64, !llvm.ptr
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %3 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %4 = llvm.load %3 : !llvm.ptr -> i64
    %5 = arith.constant 0 : i32
    %7 = arith.extsi %5 : i32 to i64
    %6 = arith.cmpi ne, %4, %7 : i64
    cf.cond_br %6, ^bb1, ^bb2
    ^bb1:
      %8 = llvm.load %1 : !llvm.ptr -> i64
      %9 = llvm.load %3 : !llvm.ptr -> i64
      %10 = arith.remsi %8, %9 : i64
      %11 = llvm.load %3 : !llvm.ptr -> i64
      llvm.store %11, %1 : i64, !llvm.ptr
      llvm.store %10, %3 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %12 = llvm.load %1 : !llvm.ptr -> i64
    func.return %12 : i64
  }
  func.func @lcm(%arg0: i64, %arg1: i64) -> i64 {
    %13 = arith.constant 0 : i32
    %15 = arith.extsi %13 : i32 to i64
    %14 = arith.cmpi eq, %arg0, %15 : i64
    %16 = scf.if %14 -> (i1) {
      %17 = arith.constant true
      scf.yield %17 : i1
    } else {
      %18 = arith.constant 0 : i32
      %20 = arith.extsi %18 : i32 to i64
      %19 = arith.cmpi eq, %arg1, %20 : i64
      scf.yield %19 : i1
    }
    cf.cond_br %16, ^bb3, ^bb4
    ^bb3:
      %21 = arith.constant 0 : i32
      %22 = arith.extsi %21 : i32 to i64
      func.return %22 : i64
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %23 = func.call @gcd(%arg0, %arg1) : (i64, i64) -> i64
    %24 = arith.divsi %arg0, %23 : i64
    %25 = arith.muli %24, %arg1 : i64
    func.return %25 : i64
  }
  func.func @isqrt(%arg0: i64) -> i64 {
    %26 = arith.constant 2 : i32
    %28 = arith.extsi %26 : i32 to i64
    %27 = arith.cmpi slt, %arg0, %28 : i64
    cf.cond_br %27, ^bb6, ^bb7
    ^bb6:
      func.return %arg0 : i64
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %29 = llvm.mlir.constant(1 : i64) : i64
    %30 = llvm.alloca %29 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %30 : i64, !llvm.ptr
    %31 = llvm.load %30 : !llvm.ptr -> i64
    %32 = arith.constant 1 : i32
    %34 = arith.extsi %32 : i32 to i64
    %33 = arith.addi %31, %34 : i64
    %35 = arith.constant 2 : i32
    %37 = arith.extsi %35 : i32 to i64
    %36 = arith.divsi %33, %37 : i64
    %38 = llvm.mlir.constant(1 : i64) : i64
    %39 = llvm.alloca %38 x i64 : (i64) -> !llvm.ptr
    llvm.store %36, %39 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %40 = llvm.load %39 : !llvm.ptr -> i64
    %41 = llvm.load %30 : !llvm.ptr -> i64
    %42 = arith.cmpi slt, %40, %41 : i64
    cf.cond_br %42, ^bb10, ^bb11
    ^bb10:
      %43 = llvm.load %39 : !llvm.ptr -> i64
      llvm.store %43, %30 : i64, !llvm.ptr
      %44 = llvm.load %30 : !llvm.ptr -> i64
      %45 = llvm.load %30 : !llvm.ptr -> i64
      %46 = arith.divsi %arg0, %45 : i64
      %47 = arith.addi %44, %46 : i64
      %48 = arith.constant 2 : i32
      %50 = arith.extsi %48 : i32 to i64
      %49 = arith.divsi %47, %50 : i64
      llvm.store %49, %39 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %51 = llvm.load %30 : !llvm.ptr -> i64
    func.return %51 : i64
  }
  func.func @mulmod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %52 = arith.remsi %arg0, %arg2 : 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 = arith.remsi %arg1, %arg2 : i64
    %56 = llvm.mlir.constant(1 : i64) : i64
    %57 = llvm.alloca %56 x i64 : (i64) -> !llvm.ptr
    llvm.store %55, %57 : i64, !llvm.ptr
    %58 = arith.constant 0 : i32
    %59 = arith.extsi %58 : i32 to i64
    %60 = llvm.mlir.constant(1 : i64) : i64
    %61 = llvm.alloca %60 x i64 : (i64) -> !llvm.ptr
    llvm.store %59, %61 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %62 = llvm.load %57 : !llvm.ptr -> i64
    %63 = arith.constant 0 : i32
    %65 = arith.extsi %63 : i32 to i64
    %64 = arith.cmpi sgt, %62, %65 : i64
    cf.cond_br %64, ^bb13, ^bb14
    ^bb13:
      %66 = llvm.load %57 : !llvm.ptr -> i64
      %67 = arith.constant 2 : i32
      %69 = arith.extsi %67 : i32 to i64
      %68 = arith.remsi %66, %69 : i64
      %70 = arith.constant 1 : i32
      %72 = arith.extsi %70 : i32 to i64
      %71 = arith.cmpi eq, %68, %72 : i64
      cf.cond_br %71, ^bb15, ^bb16
      ^bb15:
        %73 = llvm.load %61 : !llvm.ptr -> i64
        %74 = llvm.load %54 : !llvm.ptr -> i64
        %75 = arith.addi %73, %74 : i64
        %76 = arith.remsi %75, %arg2 : i64
        llvm.store %76, %61 : i64, !llvm.ptr
        cf.br ^bb17
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %77 = llvm.load %54 : !llvm.ptr -> i64
      %78 = arith.constant 2 : i32
      %80 = arith.extsi %78 : i32 to i64
      %79 = arith.muli %77, %80 : i64
      %81 = arith.remsi %79, %arg2 : i64
      llvm.store %81, %54 : i64, !llvm.ptr
      %82 = llvm.load %57 : !llvm.ptr -> i64
      %83 = arith.constant 2 : i32
      %85 = arith.extsi %83 : i32 to i64
      %84 = arith.divsi %82, %85 : i64
      llvm.store %84, %57 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %86 = llvm.load %61 : !llvm.ptr -> i64
    func.return %86 : i64
  }
  func.func @mod_pow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %87 = arith.constant 1 : i32
    %89 = arith.extsi %87 : i32 to i64
    %88 = arith.cmpi eq, %arg2, %89 : i64
    cf.cond_br %88, ^bb18, ^bb19
    ^bb18:
      %90 = arith.constant 0 : i32
      %91 = arith.extsi %90 : i32 to i64
      func.return %91 : i64
    ^bb19:
      cf.br ^bb20
    ^bb20:
    %92 = arith.constant 1 : i32
    %93 = arith.extsi %92 : i32 to i64
    %94 = llvm.mlir.constant(1 : i64) : i64
    %95 = llvm.alloca %94 x i64 : (i64) -> !llvm.ptr
    llvm.store %93, %95 : i64, !llvm.ptr
    %96 = arith.remsi %arg0, %arg2 : i64
    %97 = llvm.mlir.constant(1 : i64) : i64
    %98 = llvm.alloca %97 x i64 : (i64) -> !llvm.ptr
    llvm.store %96, %98 : i64, !llvm.ptr
    %99 = llvm.mlir.constant(1 : i64) : i64
    %100 = llvm.alloca %99 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %100 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %101 = llvm.load %100 : !llvm.ptr -> i64
    %102 = arith.constant 0 : i32
    %104 = arith.extsi %102 : i32 to i64
    %103 = arith.cmpi sgt, %101, %104 : i64
    cf.cond_br %103, ^bb22, ^bb23
    ^bb22:
      %105 = llvm.load %100 : !llvm.ptr -> i64
      %106 = arith.constant 2 : i32
      %108 = arith.extsi %106 : i32 to i64
      %107 = arith.remsi %105, %108 : i64
      %109 = arith.constant 1 : i32
      %111 = arith.extsi %109 : i32 to i64
      %110 = arith.cmpi eq, %107, %111 : i64
      cf.cond_br %110, ^bb24, ^bb25
      ^bb24:
        %113 = llvm.load %95 : !llvm.ptr -> i64
        %114 = llvm.load %98 : !llvm.ptr -> i64
        %112 = func.call @mulmod(%113, %114, %arg2) : (i64, i64, i64) -> i64
        llvm.store %112, %95 : i64, !llvm.ptr
        cf.br ^bb26
      ^bb25:
        cf.br ^bb26
      ^bb26:
      %116 = llvm.load %98 : !llvm.ptr -> i64
      %117 = llvm.load %98 : !llvm.ptr -> i64
      %115 = func.call @mulmod(%116, %117, %arg2) : (i64, i64, i64) -> i64
      llvm.store %115, %98 : i64, !llvm.ptr
      %118 = llvm.load %100 : !llvm.ptr -> i64
      %119 = arith.constant 2 : i32
      %121 = arith.extsi %119 : i32 to i64
      %120 = arith.divsi %118, %121 : i64
      llvm.store %120, %100 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %122 = llvm.load %95 : !llvm.ptr -> i64
    func.return %122 : i64
  }
  func.func @is_prime(%arg0: i64) -> i1 {
    %123 = arith.constant 2 : i32
    %125 = arith.extsi %123 : i32 to i64
    %124 = arith.cmpi slt, %arg0, %125 : i64
    cf.cond_br %124, ^bb27, ^bb28
    ^bb27:
      %126 = arith.constant 0 : i1
      func.return %126 : i1
    ^bb28:
      cf.br ^bb29
    ^bb29:
    %127 = arith.constant 4 : i32
    %129 = arith.extsi %127 : i32 to i64
    %128 = arith.cmpi slt, %arg0, %129 : i64
    cf.cond_br %128, ^bb30, ^bb31
    ^bb30:
      %130 = arith.constant 1 : i1
      func.return %130 : i1
    ^bb31:
      cf.br ^bb32
    ^bb32:
    %131 = arith.constant 2 : i32
    %133 = arith.extsi %131 : i32 to i64
    %132 = arith.remsi %arg0, %133 : i64
    %134 = arith.constant 0 : i32
    %136 = arith.extsi %134 : i32 to i64
    %135 = arith.cmpi eq, %132, %136 : i64
    %137 = scf.if %135 -> (i1) {
      %138 = arith.constant true
      scf.yield %138 : i1
    } else {
      %139 = arith.constant 3 : i32
      %141 = arith.extsi %139 : i32 to i64
      %140 = arith.remsi %arg0, %141 : i64
      %142 = arith.constant 0 : i32
      %144 = arith.extsi %142 : i32 to i64
      %143 = arith.cmpi eq, %140, %144 : i64
      scf.yield %143 : i1
    }
    cf.cond_br %137, ^bb33, ^bb34
    ^bb33:
      %145 = arith.constant 0 : i1
      func.return %145 : i1
    ^bb34:
      cf.br ^bb35
    ^bb35:
    %146 = arith.constant 5 : i32
    %147 = arith.extsi %146 : i32 to i64
    %148 = llvm.mlir.constant(1 : i64) : i64
    %149 = llvm.alloca %148 x i64 : (i64) -> !llvm.ptr
    llvm.store %147, %149 : i64, !llvm.ptr
    cf.br ^bb36
    ^bb36:
    %150 = llvm.load %149 : !llvm.ptr -> i64
    %151 = llvm.load %149 : !llvm.ptr -> i64
    %152 = arith.muli %150, %151 : i64
    %153 = arith.cmpi sle, %152, %arg0 : i64
    cf.cond_br %153, ^bb37, ^bb38
    ^bb37:
      %154 = llvm.load %149 : !llvm.ptr -> i64
      %155 = arith.remsi %arg0, %154 : i64
      %156 = arith.constant 0 : i32
      %158 = arith.extsi %156 : i32 to i64
      %157 = arith.cmpi eq, %155, %158 : i64
      %159 = scf.if %157 -> (i1) {
        %160 = arith.constant true
        scf.yield %160 : i1
      } else {
        %161 = llvm.load %149 : !llvm.ptr -> i64
        %162 = arith.constant 2 : i32
        %164 = arith.extsi %162 : i32 to i64
        %163 = arith.addi %161, %164 : i64
        %165 = arith.remsi %arg0, %163 : i64
        %166 = arith.constant 0 : i32
        %168 = arith.extsi %166 : i32 to i64
        %167 = arith.cmpi eq, %165, %168 : i64
        scf.yield %167 : i1
      }
      cf.cond_br %159, ^bb39, ^bb40
      ^bb39:
        %169 = arith.constant 0 : i1
        func.return %169 : i1
      ^bb40:
        cf.br ^bb41
      ^bb41:
      %170 = llvm.load %149 : !llvm.ptr -> i64
      %171 = arith.constant 6 : i32
      %173 = arith.extsi %171 : i32 to i64
      %172 = arith.addi %170, %173 : i64
      llvm.store %172, %149 : i64, !llvm.ptr
      cf.br ^bb36
    ^bb38:
    %174 = arith.constant 1 : i1
    func.return %174 : i1
  }
  func.func @gcd_abs(%arg0: i64, %arg1: i64) -> i64 {
    %175 = llvm.mlir.constant(1 : i64) : i64
    %176 = llvm.alloca %175 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %176 : i64, !llvm.ptr
    %177 = llvm.mlir.constant(1 : i64) : i64
    %178 = llvm.alloca %177 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %178 : i64, !llvm.ptr
    %179 = llvm.load %176 : !llvm.ptr -> i64
    %180 = arith.constant 0 : i32
    %182 = arith.extsi %180 : i32 to i64
    %181 = arith.cmpi slt, %179, %182 : i64
    cf.cond_br %181, ^bb42, ^bb43
    ^bb42:
      %183 = arith.constant 0 : i32
      %184 = llvm.load %176 : !llvm.ptr -> i64
      %186 = arith.extsi %183 : i32 to i64
      %185 = arith.subi %186, %184 : i64
      llvm.store %185, %176 : i64, !llvm.ptr
      cf.br ^bb44
    ^bb43:
      cf.br ^bb44
    ^bb44:
    %187 = llvm.load %178 : !llvm.ptr -> i64
    %188 = arith.constant 0 : i32
    %190 = arith.extsi %188 : i32 to i64
    %189 = arith.cmpi slt, %187, %190 : i64
    cf.cond_br %189, ^bb45, ^bb46
    ^bb45:
      %191 = arith.constant 0 : i32
      %192 = llvm.load %178 : !llvm.ptr -> i64
      %194 = arith.extsi %191 : i32 to i64
      %193 = arith.subi %194, %192 : i64
      llvm.store %193, %178 : i64, !llvm.ptr
      cf.br ^bb47
    ^bb46:
      cf.br ^bb47
    ^bb47:
    cf.br ^bb48
    ^bb48:
    %195 = llvm.load %178 : !llvm.ptr -> i64
    %196 = arith.constant 0 : i32
    %198 = arith.extsi %196 : i32 to i64
    %197 = arith.cmpi ne, %195, %198 : i64
    cf.cond_br %197, ^bb49, ^bb50
    ^bb49:
      %199 = llvm.load %176 : !llvm.ptr -> i64
      %200 = llvm.load %178 : !llvm.ptr -> i64
      %201 = arith.remsi %199, %200 : i64
      %202 = llvm.load %178 : !llvm.ptr -> i64
      llvm.store %202, %176 : i64, !llvm.ptr
      llvm.store %201, %178 : i64, !llvm.ptr
      cf.br ^bb48
    ^bb50:
    %203 = llvm.load %176 : !llvm.ptr -> i64
    func.return %203 : i64
  }
  func.func @mod_euclid(%arg0: i64, %arg1: i64) -> i64 {
    %204 = arith.remsi %arg0, %arg1 : i64
    %205 = llvm.mlir.constant(1 : i64) : i64
    %206 = llvm.alloca %205 x i64 : (i64) -> !llvm.ptr
    llvm.store %204, %206 : i64, !llvm.ptr
    %207 = llvm.load %206 : !llvm.ptr -> i64
    %208 = arith.constant 0 : i32
    %210 = arith.extsi %208 : i32 to i64
    %209 = arith.cmpi slt, %207, %210 : i64
    cf.cond_br %209, ^bb51, ^bb52
    ^bb51:
      %211 = llvm.load %206 : !llvm.ptr -> i64
      %212 = arith.addi %211, %arg1 : i64
      llvm.store %212, %206 : i64, !llvm.ptr
      cf.br ^bb53
    ^bb52:
      cf.br ^bb53
    ^bb53:
    %213 = llvm.load %206 : !llvm.ptr -> i64
    func.return %213 : i64
  }
  func.func @is_odd(%arg0: i64) -> i1 {
    %215 = arith.constant 2 : i32
    %216 = arith.extsi %215 : i32 to i64
    %214 = func.call @mod_euclid(%arg0, %216) : (i64, i64) -> i64
    %217 = arith.constant 1 : i32
    %219 = arith.extsi %217 : i32 to i64
    %218 = arith.cmpi eq, %214, %219 : i64
    func.return %218 : i1
  }
  func.func @main() -> i32 {
    %220 = arith.constant 99999995705032704 : i32
    %221 = arith.extsi %220 : i32 to i64
    %224 = arith.constant 2 : i32
    %226 = arith.extsi %224 : i32 to i64
    %225 = arith.muli %226, %221 : i64
    %223 = func.call @isqrt(%225) : (i64) -> i64
    %222 = func.call @isqrt(%223) : (i64) -> i64
    %227 = arith.constant 1 : i32
    %229 = arith.extsi %227 : i32 to i64
    %228 = arith.addi %222, %229 : i64
    %230 = arith.constant 0 : i32
    %231 = arith.extsi %230 : i32 to i64
    %232 = llvm.mlir.constant(1 : i64) : i64
    %233 = llvm.alloca %232 x i64 : (i64) -> !llvm.ptr
    llvm.store %231, %233 : i64, !llvm.ptr
    %234 = arith.constant 1 : i32
    %235 = arith.extsi %234 : i32 to i64
    %236 = llvm.mlir.constant(1 : i64) : i64
    %237 = llvm.alloca %236 x i64 : (i64) -> !llvm.ptr
    llvm.store %235, %237 : i64, !llvm.ptr
    cf.br ^bb54
    ^bb54:
    %238 = llvm.load %237 : !llvm.ptr -> i64
    %239 = arith.cmpi sle, %238, %228 : i64
    cf.cond_br %239, ^bb55, ^bb56
    ^bb55:
      %240 = llvm.load %237 : !llvm.ptr -> i64
      %241 = llvm.load %237 : !llvm.ptr -> i64
      %242 = arith.muli %240, %241 : i64
      %243 = arith.constant 0 : i32
      %244 = llvm.load %237 : !llvm.ptr -> i64
      %245 = arith.constant 3 : i32
      %247 = arith.extsi %245 : i32 to i64
      %246 = arith.divsi %244, %247 : i64
      %249 = arith.extsi %243 : i32 to i64
      %248 = arith.subi %249, %246 : i64
      %250 = llvm.mlir.constant(1 : i64) : i64
      %251 = llvm.alloca %250 x i64 : (i64) -> !llvm.ptr
      llvm.store %248, %251 : i64, !llvm.ptr
      %252 = llvm.load %237 : !llvm.ptr -> i64
      %253 = arith.constant 3 : i32
      %255 = arith.extsi %253 : i32 to i64
      %254 = arith.remsi %252, %255 : i64
      %256 = arith.constant 0 : i32
      %258 = arith.extsi %256 : i32 to i64
      %257 = arith.cmpi eq, %254, %258 : i64
      cf.cond_br %257, ^bb57, ^bb58
      ^bb57:
        %259 = llvm.load %251 : !llvm.ptr -> i64
        %260 = arith.constant 1 : i32
        %262 = arith.extsi %260 : i32 to i64
        %261 = arith.addi %259, %262 : i64
        llvm.store %261, %251 : i64, !llvm.ptr
        cf.br ^bb59
      ^bb58:
        cf.br ^bb59
      ^bb59:
      %263 = llvm.load %251 : !llvm.ptr -> i64
      %264 = llvm.mlir.constant(1 : i64) : i64
      %265 = llvm.alloca %264 x i64 : (i64) -> !llvm.ptr
      llvm.store %263, %265 : i64, !llvm.ptr
      cf.br ^bb60
      ^bb60:
      %266 = llvm.load %265 : !llvm.ptr -> i64
      %267 = arith.constant 0 : i32
      %269 = arith.extsi %267 : i32 to i64
      %268 = arith.cmpi slt, %266, %269 : i64
      cf.cond_br %268, ^bb61, ^bb62
      ^bb61:
        %271 = llvm.load %237 : !llvm.ptr -> i64
        %270 = func.call @is_odd(%271) : (i64) -> i1
        %272 = scf.if %270 -> (i1) {
          %274 = llvm.load %265 : !llvm.ptr -> i64
          %273 = func.call @is_odd(%274) : (i64) -> i1
          scf.yield %273 : i1
        } else {
          %275 = arith.constant false
          scf.yield %275 : i1
        }
        %277 = arith.constant 1 : i1
        %276 = arith.xori %272, %277 : i1
        cf.cond_br %276, ^bb63, ^bb64
        ^bb63:
          %280 = llvm.load %237 : !llvm.ptr -> i64
          %281 = arith.constant 2 : i32
          %282 = llvm.load %265 : !llvm.ptr -> i64
          %284 = arith.extsi %281 : i32 to i64
          %283 = arith.muli %284, %282 : i64
          %285 = arith.subi %280, %283 : i64
          %286 = arith.constant 5 : i32
          %287 = arith.extsi %286 : i32 to i64
          %279 = func.call @mod_euclid(%285, %287) : (i64, i64) -> i64
          %288 = arith.constant 0 : i32
          %290 = arith.extsi %288 : i32 to i64
          %289 = arith.cmpi ne, %279, %290 : i64
          cf.cond_br %289, ^bb66, ^bb67
          ^bb66:
            %292 = llvm.load %237 : !llvm.ptr -> i64
            %293 = arith.constant 0 : i32
            %294 = llvm.load %265 : !llvm.ptr -> i64
            %296 = arith.extsi %293 : i32 to i64
            %295 = arith.subi %296, %294 : i64
            %291 = func.call @gcd_abs(%292, %295) : (i64, i64) -> i64
            %297 = arith.constant 1 : i32
            %299 = arith.extsi %297 : i32 to i64
            %298 = arith.cmpi eq, %291, %299 : i64
            cf.cond_br %298, ^bb69, ^bb70
            ^bb69:
              %300 = llvm.load %265 : !llvm.ptr -> i64
              %301 = llvm.load %265 : !llvm.ptr -> i64
              %302 = arith.muli %300, %301 : i64
              %303 = llvm.load %237 : !llvm.ptr -> i64
              %304 = llvm.load %265 : !llvm.ptr -> i64
              %305 = arith.muli %303, %304 : i64
              %306 = arith.subi %242, %302 : i64
              %307 = arith.constant 4 : i32
              %309 = arith.extsi %307 : i32 to i64
              %308 = arith.muli %309, %305 : i64
              %310 = arith.subi %306, %308 : i64
              %311 = arith.subi %242, %302 : i64
              %312 = arith.addi %311, %305 : i64
              %313 = arith.extsi %310 : i64 to i128
              %314 = arith.extsi %312 : i64 to i128
              %316 = arith.trunci %313 : i128 to i64
              %317 = arith.trunci %314 : i128 to i64
              %315 = arith.muli %316, %317 : i64
              %318 = arith.extsi %315 : i64 to i128
              %319 = arith.constant 0 : i32
              %320 = arith.extsi %319 : i32 to i128
              %322 = arith.trunci %318 : i128 to i64
              %323 = arith.trunci %320 : i128 to i64
              %321 = arith.cmpi sgt, %322, %323 : i64
              %324 = scf.if %321 -> (i1) {
                %325 = arith.extsi %221 : i64 to i128
                %327 = arith.trunci %318 : i128 to i64
                %328 = arith.trunci %325 : i128 to i64
                %326 = arith.cmpi sle, %327, %328 : i64
                scf.yield %326 : i1
              } else {
                %329 = arith.constant false
                scf.yield %329 : i1
              }
              cf.cond_br %324, ^bb72, ^bb73
              ^bb72:
                %330 = llvm.load %233 : !llvm.ptr -> i64
                %331 = arith.extsi %221 : i64 to i128
                %333 = arith.trunci %331 : i128 to i64
                %334 = arith.trunci %318 : i128 to i64
                %332 = arith.divsi %333, %334 : i64
                %335 = arith.addi %330, %332 : i64
                llvm.store %335, %233 : i64, !llvm.ptr
                cf.br ^bb74
              ^bb73:
                cf.br ^bb74
              ^bb74:
              cf.br ^bb71
            ^bb70:
              cf.br ^bb71
            ^bb71:
            cf.br ^bb68
          ^bb67:
            cf.br ^bb68
          ^bb68:
          cf.br ^bb65
        ^bb64:
          cf.br ^bb65
        ^bb65:
        %336 = llvm.load %265 : !llvm.ptr -> i64
        %337 = arith.constant 1 : i32
        %339 = arith.extsi %337 : i32 to i64
        %338 = arith.addi %336, %339 : i64
        llvm.store %338, %265 : i64, !llvm.ptr
        cf.br ^bb60
      ^bb62:
      %340 = llvm.load %237 : !llvm.ptr -> i64
      %341 = arith.constant 2 : i32
      %343 = arith.extsi %341 : i32 to i64
      %342 = arith.divsi %340, %343 : i64
      %344 = arith.constant 1 : i32
      %346 = arith.extsi %344 : i32 to i64
      %345 = arith.addi %342, %346 : i64
      llvm.store %345, %265 : i64, !llvm.ptr
      cf.br ^bb75
      ^bb75:
      %347 = llvm.load %265 : !llvm.ptr -> i64
      %348 = llvm.load %237 : !llvm.ptr -> i64
      %349 = arith.cmpi slt, %347, %348 : i64
      cf.cond_br %349, ^bb76, ^bb77
      ^bb76:
        %351 = llvm.load %237 : !llvm.ptr -> i64
        %350 = func.call @is_odd(%351) : (i64) -> i1
        %352 = scf.if %350 -> (i1) {
          %354 = llvm.load %265 : !llvm.ptr -> i64
          %353 = func.call @is_odd(%354) : (i64) -> i1
          scf.yield %353 : i1
        } else {
          %355 = arith.constant false
          scf.yield %355 : i1
        }
        %357 = arith.constant 1 : i1
        %356 = arith.xori %352, %357 : i1
        cf.cond_br %356, ^bb78, ^bb79
        ^bb78:
          %360 = llvm.load %237 : !llvm.ptr -> i64
          %361 = arith.constant 2 : i32
          %362 = llvm.load %265 : !llvm.ptr -> i64
          %364 = arith.extsi %361 : i32 to i64
          %363 = arith.muli %364, %362 : i64
          %365 = arith.subi %360, %363 : i64
          %366 = arith.constant 5 : i32
          %367 = arith.extsi %366 : i32 to i64
          %359 = func.call @mod_euclid(%365, %367) : (i64, i64) -> i64
          %368 = arith.constant 0 : i32
          %370 = arith.extsi %368 : i32 to i64
          %369 = arith.cmpi ne, %359, %370 : i64
          cf.cond_br %369, ^bb81, ^bb82
          ^bb81:
            %372 = llvm.load %237 : !llvm.ptr -> i64
            %373 = llvm.load %265 : !llvm.ptr -> i64
            %371 = func.call @gcd_abs(%372, %373) : (i64, i64) -> i64
            %374 = arith.constant 1 : i32
            %376 = arith.extsi %374 : i32 to i64
            %375 = arith.cmpi eq, %371, %376 : i64
            cf.cond_br %375, ^bb84, ^bb85
            ^bb84:
              %377 = llvm.load %265 : !llvm.ptr -> i64
              %378 = llvm.load %265 : !llvm.ptr -> i64
              %379 = arith.muli %377, %378 : i64
              %380 = llvm.load %237 : !llvm.ptr -> i64
              %381 = llvm.load %265 : !llvm.ptr -> i64
              %382 = arith.muli %380, %381 : i64
              %383 = arith.constant 4 : i32
              %385 = arith.extsi %383 : i32 to i64
              %384 = arith.muli %385, %382 : i64
              %386 = arith.addi %379, %384 : i64
              %387 = arith.subi %386, %242 : i64
              %388 = arith.subi %242, %379 : i64
              %389 = arith.addi %388, %382 : i64
              %390 = arith.extsi %387 : i64 to i128
              %391 = arith.extsi %389 : i64 to i128
              %393 = arith.trunci %390 : i128 to i64
              %394 = arith.trunci %391 : i128 to i64
              %392 = arith.muli %393, %394 : i64
              %395 = arith.extsi %392 : i64 to i128
              %396 = arith.constant 0 : i32
              %397 = arith.extsi %396 : i32 to i128
              %399 = arith.trunci %395 : i128 to i64
              %400 = arith.trunci %397 : i128 to i64
              %398 = arith.cmpi sgt, %399, %400 : i64
              %401 = scf.if %398 -> (i1) {
                %402 = arith.extsi %221 : i64 to i128
                %404 = arith.trunci %395 : i128 to i64
                %405 = arith.trunci %402 : i128 to i64
                %403 = arith.cmpi sle, %404, %405 : i64
                scf.yield %403 : i1
              } else {
                %406 = arith.constant false
                scf.yield %406 : i1
              }
              cf.cond_br %401, ^bb87, ^bb88
              ^bb87:
                %407 = llvm.load %233 : !llvm.ptr -> i64
                %408 = arith.extsi %221 : i64 to i128
                %410 = arith.trunci %408 : i128 to i64
                %411 = arith.trunci %395 : i128 to i64
                %409 = arith.divsi %410, %411 : i64
                %412 = arith.addi %407, %409 : i64
                llvm.store %412, %233 : i64, !llvm.ptr
                cf.br ^bb89
              ^bb88:
                cf.br ^bb89
              ^bb89:
              cf.br ^bb86
            ^bb85:
              cf.br ^bb86
            ^bb86:
            cf.br ^bb83
          ^bb82:
            cf.br ^bb83
          ^bb83:
          cf.br ^bb80
        ^bb79:
          cf.br ^bb80
        ^bb80:
        %413 = llvm.load %265 : !llvm.ptr -> i64
        %414 = arith.constant 1 : i32
        %416 = arith.extsi %414 : i32 to i64
        %415 = arith.addi %413, %416 : i64
        llvm.store %415, %265 : i64, !llvm.ptr
        cf.br ^bb75
      ^bb77:
      %417 = llvm.load %237 : !llvm.ptr -> i64
      %418 = arith.constant 1 : i32
      %420 = arith.extsi %418 : i32 to i64
      %419 = arith.addi %417, %420 : i64
      llvm.store %419, %237 : i64, !llvm.ptr
      cf.br ^bb54
    ^bb56:
    %421 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %422 = llvm.load %233 : !llvm.ptr -> i64
    %423 = llvm.call @printf(%421, %422) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %424 = arith.constant 0 : i32
    func.return %424 : i32
  }
}