Problem 516

5-smooth Totients — S(10^12) mod 2^32.

Answer939087315
Output939087315
StatusPASS
Native helperno
Runtime100 ms
Peak memory5872 KB
Time complexityO(n^3) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n log log n)
Space complexityO(1)O(n)
ApproachFlow solutionSieve-based totient computation
VerdictSuboptimal

Flow source

# Project Euler 516
# 5-smooth Totients — S(10^12) mod 2^32.

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

function is_prime(x0: i64) -> i32 {
    let x: i64 = x0
    if x % 2 == 0 || x % 3 == 0 || x % 5 == 0 {
        if x == 2 || x == 3 || x == 5 { return 1 }
        return 0
    }
    let mut i: i64 = 7
    let mut pos: i32 = 1
    # wheel deltas
    while i * i <= x {
        if x % i == 0 { return 0 }
        if pos == 0 { i = i + 6 }
        elif pos == 1 { i = i + 4 }
        elif pos == 2 { i = i + 2 }
        elif pos == 3 { i = i + 4 }
        elif pos == 4 { i = i + 2 }
        elif pos == 5 { i = i + 4 }
        elif pos == 6 { i = i + 6 }
        else { i = i + 2 }
        pos = (pos + 1) & 7
    }
    if x > 1 { return 1 }
    return 0
}

function main() -> i32 {
    let limit: i64 = 1000000000000
    let hamming: ptr<i64> = calloc(200000, 8)
    let primes: ptr<i64> = calloc(200000, 8)
    if hamming == null || primes == null { return 1 }
    let mut hn: i64 = 0
    let mut pn: i64 = 0
    let mut two: i64 = 1
    while two <= limit {
        let mut three: i64 = 1
        while two * three <= limit {
            let mut five: i64 = 1
            while two * three * five <= limit {
                let current: i64 = two * three * five
                hamming[hn] = current
                hn = hn + 1
                if current > 5 && is_prime(current + 1) == 1 {
                    primes[pn] = current + 1
                    pn = pn + 1
                }
                if five > limit / 5 { break }
                five = five * 5
            }
            if three > limit / 3 { break }
            three = three * 3
        }
        if two > limit / 2 { break }
        two = two * 2
    }
    # sort hamming and primes (insertion / selection is fine for ~few thousand)
    let mut i: i64 = 0
    while i < hn {
        let mut j: i64 = i + 1
        while j < hn {
            if hamming[j] < hamming[i] {
                let t: i64 = hamming[i]
                hamming[i] = hamming[j]
                hamming[j] = t
            }
            j = j + 1
        }
        i = i + 1
    }
    i = 0
    while i < pn {
        let mut j2: i64 = i + 1
        while j2 < pn {
            if primes[j2] < primes[i] {
                let t2: i64 = primes[i]
                primes[i] = primes[j2]
                primes[j2] = t2
            }
            j2 = j2 + 1
        }
        i = i + 1
    }

    # stack for DFS: (number, largest_prime)
    let stack_n: ptr<i64> = calloc(200000, 8)
    let stack_p: ptr<i64> = calloc(200000, 8)
    if stack_n == null || stack_p == null { return 1 }
    let mut sp: i64 = 0
    stack_n[0] = 1
    stack_p[0] = 1
    sp = 1
    let mut total: i64 = 0
    while sp > 0 {
        sp = sp - 1
        let number: i64 = stack_n[sp]
        let largest_prime: i64 = stack_p[sp]
        let mut xi: i64 = 0
        while xi < hn {
            let x: i64 = hamming[xi]
            if x > limit / number { break }
            let next_value: i64 = x * number
            total = total + next_value
            xi = xi + 1
        }
        # find first prime > largest_prime
        let mut lo: i64 = 0
        let mut hi: i64 = pn
        while lo < hi {
            let mid: i64 = (lo + hi) / 2
            if primes[mid] <= largest_prime { lo = mid + 1 } else { hi = mid }
        }
        let mut pi: i64 = lo
        while pi < pn {
            let prime: i64 = primes[pi]
            if prime > limit / number { break }
            stack_n[sp] = prime * number
            stack_p[sp] = prime
            sp = sp + 1
            pi = pi + 1
        }
    }
    total = total & 4294967295
    printf("%lld\n", total)
    free(hamming); free(primes); free(stack_n); free(stack_p)
    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 is_prime_i64(int64_t x0);
int32_t main(void);



int32_t is_prime_i64(int64_t x0) {
    int64_t x = x0;
    if (((FLOW_CHECKED_MOD((x), (2)) == 0 || FLOW_CHECKED_MOD((x), (3)) == 0) || FLOW_CHECKED_MOD((x), (5)) == 0)) {
        if (((x == 2 || x == 3) || x == 5)) {
            return 1;
        }
        return 0;
    }
    int64_t i = 7;
    int32_t pos = 1;
    while ((i * i) <= x) {
        if (FLOW_CHECKED_MOD((x), (i)) == 0) {
            return 0;
        }
        if (pos == 0) {
            i = (i + 6);
        } else if (pos == 1) {
            i = (i + 4);
        } else if (pos == 2) {
            i = (i + 2);
        } else if (pos == 3) {
            i = (i + 4);
        } else if (pos == 4) {
            i = (i + 2);
        } else if (pos == 5) {
            i = (i + 4);
        } else if (pos == 6) {
            i = (i + 6);
        } else {
            i = (i + 2);
        }
        pos = ((pos + 1) & 7);
    }
    if (x > 1) {
        return 1;
    }
    return 0;
}

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