Problem 748

Upside Down Diophantine Equation: last 9 digits of S(10^16). Pure Flow port of the Gaussian integer parametrization.

Answer276402862
Output276402862
StatusPASS
Native helperno
Runtime970 ms
Peak memory1104 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n)
Space complexityO(1)O(n)
ApproachFlow solutionBig-integer arithmetic
VerdictOptimal

Flow source

# Project Euler 748
# Upside Down Diophantine Equation: last 9 digits of S(10^16).
# Pure Flow port of the Gaussian integer parametrization.

function gcd_ll(a: i64, b: i64) -> i64 {
    let mut x: i64 = a
    if x < 0 { x = 0 - x }
    let mut y: i64 = b
    if y < 0 { y = 0 - y }
    while y != 0 {
        let t: i64 = x % y
        x = y
        y = t
    }
    return x
}

function isqrt128(n: i128) -> i128 {
    if n == 0 { return 0 }
    let mut x: i128 = n
    let mut y: i128 = (x + 1) / 2
    while y < x {
        x = y
        y = (x + n / x) / 2
    }
    return x
}

function fourth_root_floor128(n: i128) -> i128 {
    if n == 0 { return 0 }
    let mut x: i128 = n |> isqrt128 |> isqrt128
    let mut xp1: i128 = x + 1
    while xp1 * xp1 * xp1 * xp1 <= n {
        x = xp1
        xp1 = x + 1
    }
    while x * x * x * x > n {
        x = x - 1
    }
    return x
}

function isqrt64(n: i64) -> i64 {
    if n <= 0 { return 0 }
    let mut x: i64 = n
    let mut y: i64 = (x + 1) / 2
    while y < x {
        x = y
        y = (x + n / x) / 2
    }
    return x
}

function main() -> i32 {
    let N: i64 = 10000000000000000
    let modv: i64 = 1000000000

    let Nsq: i128 = (N as i128) * (N as i128)
    let val: i128 = 2 * Nsq / 13
    let r_max: i64 = (fourth_root_floor128(val)) as i64

    let m_max: i64 = isqrt64(r_max)

    let mut total: i64 = 0
    let THRESH: i64 = 1000000000000000000

    let mut m: i64 = 1
    while m <= m_max {
        let mm: i64 = m * m
        let n_max: i64 = isqrt64(r_max - mm)

        let n_start: i64 = 0
        if (m & 1) == 0 {
            n_start = 1
        }

        let mut n: i64 = n_start
        while n <= n_max {
            if gcd_ll(m, n) != 1 {
                n = n + 2
                continue
            }

            let nn: i64 = n * n
            let r: i64 = mm + nn

            let u: i64 = mm - nn
            let v: i64 = 2 * m * n

            let mut a: i64 = 3 * u - 2 * v
            if a < 0 { a = 0 - a }
            let mut b: i64 = 3 * v + 2 * u
            if b < 0 { b = 0 - b }

            let mut p: i64 = a
            let mut q: i64 = b
            if a < b {
                p = b
                q = a
            }

            if p % 13 == 0 && q % 13 == 0 {
                n = n + 2
                continue
            }

            let x: i64 = q * r
            let y: i64 = p * r
            if x > N || y > N {
                n = n + 2
                continue
            }
            let z: i64 = p * q
            if z > N {
                n = n + 2
                continue
            }

            total = total + x + y + z
            if total >= THRESH {
                total = total % modv
            }
            n = n + 2
        }
        m = m + 1
    }

    printf("%lld\n", total % modv)
    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_ll_i64_i64(int64_t a, int64_t b);
__int128 isqrt128_i128(__int128 n);
__int128 fourth_root_floor128_i128(__int128 n);
int64_t isqrt64_i64(int64_t n);
int32_t main(void);

int64_t gcd_ll_i64_i64(int64_t a, int64_t b) {
    int64_t x = a;
    if (x < 0) {
        x = (0 - x);
    }
    int64_t y = b;
    if (y < 0) {
        y = (0 - y);
    }
    while (y != 0) {
        int64_t t = FLOW_CHECKED_MOD((x), (y));
        x = y;
        y = t;
    }
    return x;
}

__int128 isqrt128_i128(__int128 n) {
    if (n == 0) {
        return 0;
    }
    __int128 x = n;
    __int128 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;
}

__int128 fourth_root_floor128_i128(__int128 n) {
    if (n == 0) {
        return 0;
    }
    __int128 x = isqrt128_i128(isqrt128_i128(n));
    __int128 xp1 = (x + 1);
    while ((((xp1 * xp1) * xp1) * xp1) <= n) {
        x = xp1;
        xp1 = (x + 1);
    }
    while ((((x * x) * x) * x) > n) {
        x = (x - 1);
    }
    return x;
}

int64_t isqrt64_i64(int64_t n) {
    if (n <= 0) {
        return 0;
    }
    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;
}

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