Problem 060

Lowest sum for a set of five primes with pairwise prime concatenations.

Answer26033
Output26033
StatusPASS
Native helperno
Runtime1060 ms
Peak memory2352 KB
Time complexityO(n^2) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log log n)
Space complexityO(n^2)O(n)
ApproachFlow solutionSieve of Eratosthenes
VerdictSuboptimal

Flow source

# Project Euler 060
# Lowest sum for a set of five primes with pairwise prime concatenations.

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

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

function pow_mod(a0: i64, e0: i64, m: i64) -> i64 {
    let mut a: i64 = a0 % m
    let mut e: i64 = e0
    let mut r: i64 = 1
    while e > 0 {
        if e % 2 == 1 {
            r = mul_mod(r, a, m)
        }
        a = mul_mod(a, a, m)
        e = e / 2
    }
    return r
}

function mr_check(n: i64, a: i64) -> bool {
    # write n-1 = d * 2^s
    let mut d: i64 = n - 1
    let mut s: i32 = 0
    while d % 2 == 0 {
        d = d / 2
        s = s + 1
    }
    let mut x: i64 = pow_mod(a, d, n)
    if x == 1 || x == n - 1 { return true }
    let mut r: i32 = 1
    while r < s {
        x = mul_mod(x, x, n)
        if x == n - 1 { return true }
        r = r + 1
    }
    return false
}

function is_prime(n: i64) -> bool {
    if n < 2 { return false }
    if n < 4 { return true }
    if n % 2 == 0 || n % 3 == 0 { return false }
    if n < 1000000 {
        let mut i: i64 = 5
        while i * i <= n {
            if n % i == 0 || n % (i + 2) == 0 { return false }
            i = i + 6
        }
        return true
    }
    # deterministic MR for n < 3e9+ suffices with these witnesses
    if !mr_check(n, 2) { return false }
    if !mr_check(n, 3) { return false }
    if !mr_check(n, 5) { return false }
    if !mr_check(n, 7) { return false }
    if !mr_check(n, 11) { return false }
    if !mr_check(n, 13) { return false }
    if !mr_check(n, 23) { return false }
    return true
}

function concat(a: i64, b: i64) -> i64 {
    let mut p: i64 = 1
    let mut x: i64 = b
    while x > 0 {
        p = p * 10
        x = x / 10
    }
    return a * p + b
}

function pair_ok(a: i64, b: i64) -> bool {
    return is_prime(concat(a, b)) && is_prime(concat(b, a))
}

function dfs(depth: i32, start: i32, pc: i32, primes: ptr<i64>, ok: ptr<i8>, choose: ptr<i32>, best: ptr<i64>) -> void {
    if depth == 5 {
        let mut s: i64 = 0
        let mut i: i32 = 0
        while i < 5 {
            s = s + primes[choose[i]]
            i = i + 1
        }
        if best[0] == 0 || s < best[0] {
            best[0] = s
        }
        return
    }
    let mut i: i32 = start
    while i < pc {
        # prune if partial sum already worse
        let mut ok_all: bool = true
        let mut j: i32 = 0
        while j < depth {
            if ok[choose[j] * pc + i] == 0 {
                ok_all = false
                break
            }
            j = j + 1
        }
        if ok_all {
            choose[depth] = i
            # if we have a best, prune by lower bound (remaining min primes)
            let mut partial: i64 = 0
            j = 0
            while j <= depth {
                partial = partial + primes[choose[j]]
                j = j + 1
            }
            if best[0] == 0 || partial < best[0] {
                dfs(depth + 1, i + 1, pc, primes, ok, choose, best)
            }
        }
        i = i + 1
    }
}

function main() -> i32 {
    let limit: i64 = 9000
    let sieve: ptr<i8> = calloc(limit, 1)
    let primes: ptr<i64> = calloc(2000, 8)
    if sieve == null || primes == null { return 1 }
    sieve[0] = 1
    sieve[1] = 1
    let mut p: i64 = 2
    while p * p < limit {
        if sieve[p] == 0 {
            let mut m: i64 = p * p
            while m < limit {
                sieve[m] = 1
                m = m + p
            }
        }
        p = p + 1
    }
    let mut pc: i32 = 0
    p = 3
    while p < limit {
        if sieve[p] == 0 {
            primes[pc] = p
            pc = pc + 1
        }
        p = p + 2
    }

    let ok: ptr<i8> = calloc((pc * pc) as i64, 1)
    if ok == null { return 1 }
    let mut i: i32 = 0
    while i < pc {
        let mut j: i32 = i + 1
        while j < pc {
            if pair_ok(primes[i], primes[j]) {
                ok[i * pc + j] = 1
                ok[j * pc + i] = 1
            }
            j = j + 1
        }
        i = i + 1
    }

    let choose: ptr<i32> = calloc(5, 4)
    let best: ptr<i64> = calloc(1, 8)
    if choose == null || best == null { return 1 }
    best[0] = 0
    dfs(0, 0, pc, primes, ok, choose, best)
    printf("%lld\n", best[0])
    free(best)
    free(choose)
    free(ok)
    free(primes)
    free(sieve)
    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 mul_mod_i64_i64_i64(int64_t a0, int64_t b0, int64_t m);
int64_t pow_mod_i64_i64_i64(int64_t a0, int64_t e0, int64_t m);
bool mr_check_i64_i64(int64_t n, int64_t a);
bool is_prime_i64(int64_t n);
int64_t concat_i64_i64(int64_t a, int64_t b);
bool pair_ok_i64_i64(int64_t a, int64_t b);
void dfs_i32_i32_i32_ptr_i64_ptr_i8_ptr_i32_ptr_i64(int32_t depth, int32_t start, int32_t pc, int64_t* primes, int8_t* ok, int32_t* choose, int64_t* best);
int32_t main(void);



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

int64_t pow_mod_i64_i64_i64(int64_t a0, int64_t e0, int64_t m) {
    int64_t a = FLOW_CHECKED_MOD((a0), (m));
    int64_t e = e0;
    int64_t r = 1;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            r = mul_mod_i64_i64_i64(r, a, m);
        }
        a = mul_mod_i64_i64_i64(a, a, m);
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return r;
}

bool mr_check_i64_i64(int64_t n, int64_t a) {
    int64_t d = (n - 1);
    int32_t s = 0;
    while (FLOW_CHECKED_MOD((d), (2)) == 0) {
        d = FLOW_CHECKED_DIV((d), (2));
        s = (s + 1);
    }
    int64_t x = pow_mod_i64_i64_i64(a, d, n);
    if ((x == 1 || x == (n - 1))) {
        return 1;
    }
    int32_t r = 1;
    while (r < s) {
        x = mul_mod_i64_i64_i64(x, x, n);
        if (x == (n - 1)) {
            return 1;
        }
        r = (r + 1);
    }
    return 0;
}

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;
    }
    if (n < 1000000) {
        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;
    }
    if ((!(mr_check_i64_i64(n, 2)))) {
        return 0;
    }
    if ((!(mr_check_i64_i64(n, 3)))) {
        return 0;
    }
    if ((!(mr_check_i64_i64(n, 5)))) {
        return 0;
    }
    if ((!(mr_check_i64_i64(n, 7)))) {
        return 0;
    }
    if ((!(mr_check_i64_i64(n, 11)))) {
        return 0;
    }
    if ((!(mr_check_i64_i64(n, 13)))) {
        return 0;
    }
    if ((!(mr_check_i64_i64(n, 23)))) {
        return 0;
    }
    return 1;
}

int64_t concat_i64_i64(int64_t a, int64_t b) {
    int64_t p = 1;
    int64_t x = b;
    while (x > 0) {
        p = (p * 10);
        x = FLOW_CHECKED_DIV((x), (10));
    }
    return ((a * p) + b);
}

bool pair_ok_i64_i64(int64_t a, int64_t b) {
    return (is_prime_i64(concat_i64_i64(a, b)) && is_prime_i64(concat_i64_i64(b, a)));
}

void dfs_i32_i32_i32_ptr_i64_ptr_i8_ptr_i32_ptr_i64(int32_t depth, int32_t start, int32_t pc, int64_t* primes, int8_t* ok, int32_t* choose, int64_t* best) {
    if (depth == 5) {
        int64_t s = 0;
        int32_t i = 0;
        while (i < 5) {
            s = (s + primes[choose[i]]);
            i = (i + 1);
        }
        if ((best[0] == 0 || s < best[0])) {
            best[0] = s;
        }
        return;
    }
    int32_t i = start;
    while (i < pc) {
        bool ok_all = 1;
        int32_t j = 0;
        while (j < depth) {
            if (ok[((choose[j] * pc) + i)] == 0) {
                ok_all = 0;
                break;
            }
            j = (j + 1);
        }
        if (ok_all) {
            choose[depth] = i;
            int64_t partial = 0;
            j = 0;
            while (j <= depth) {
                partial = (partial + primes[choose[j]]);
                j = (j + 1);
            }
            if ((best[0] == 0 || partial < best[0])) {
                dfs_i32_i32_i32_ptr_i64_ptr_i8_ptr_i32_ptr_i64((depth + 1), (i + 1), pc, primes, ok, choose, best);
            }
        }
        i = (i + 1);
    }
}

int32_t main(void) {
    int64_t limit = 9000;
    int8_t* sieve = (int8_t*)(calloc(limit, 1));
    int64_t* primes = (int64_t*)(calloc(2000, 8));
    if ((sieve == NULL || primes == NULL)) {
        return 1;
    }
    sieve[0] = 1;
    sieve[1] = 1;
    int64_t p = 2;
    while ((p * p) < limit) {
        if (sieve[p] == 0) {
            int64_t m = (p * p);
            while (m < limit) {
                sieve[m] = 1;
                m = (m + p);
            }
        }
        p = (p + 1);
    }
    int32_t pc = 0;
    p = 3;
    while (p < limit) {
        if (sieve[p] == 0) {
            primes[pc] = p;
            pc = (pc + 1);
        }
        p = (p + 2);
    }
    int8_t* ok = (int8_t*)(calloc(((int64_t)((pc * pc))), 1));
    if (ok == NULL) {
        return 1;
    }
    int32_t i = 0;
    while (i < pc) {
        int32_t j = (i + 1);
        while (j < pc) {
            if (pair_ok_i64_i64(primes[i], primes[j])) {
                ok[((i * pc) + j)] = 1;
                ok[((j * pc) + i)] = 1;
            }
            j = (j + 1);
        }
        i = (i + 1);
    }
    int32_t* choose = (int32_t*)(calloc(5, 4));
    int64_t* best = (int64_t*)(calloc(1, 8));
    if ((choose == NULL || best == NULL)) {
        return 1;
    }
    best[0] = 0;
    dfs_i32_i32_i32_ptr_i64_ptr_i8_ptr_i32_ptr_i64(0, 0, pc, primes, ok, choose, best);
    printf("%lld\n", best[0]);
    free(best);
    free(choose);
    free(ok);
    free(primes);
    free(sieve);
    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 @mul_mod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %0 = arith.remsi %arg0, %arg2 : i64
    %1 = llvm.mlir.constant(1 : i64) : i64
    %2 = llvm.alloca %1 x i64 : (i64) -> !llvm.ptr
    llvm.store %0, %2 : i64, !llvm.ptr
    %3 = llvm.mlir.constant(1 : i64) : i64
    %4 = llvm.alloca %3 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %4 : i64, !llvm.ptr
    %5 = arith.constant 0 : i32
    %6 = arith.extsi %5 : i32 to i64
    %7 = llvm.mlir.constant(1 : i64) : i64
    %8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
    llvm.store %6, %8 : i64, !llvm.ptr
    %9 = llvm.load %2 : !llvm.ptr -> i64
    %10 = arith.constant 0 : i32
    %12 = arith.extsi %10 : i32 to i64
    %11 = arith.cmpi slt, %9, %12 : i64
    cf.cond_br %11, ^bb0, ^bb1
    ^bb0:
      %13 = llvm.load %2 : !llvm.ptr -> i64
      %14 = arith.addi %13, %arg2 : i64
      llvm.store %14, %2 : i64, !llvm.ptr
      cf.br ^bb2
    ^bb1:
      cf.br ^bb2
    ^bb2:
    cf.br ^bb3
    ^bb3:
    %15 = llvm.load %4 : !llvm.ptr -> i64
    %16 = arith.constant 0 : i32
    %18 = arith.extsi %16 : i32 to i64
    %17 = arith.cmpi sgt, %15, %18 : i64
    cf.cond_br %17, ^bb4, ^bb5
    ^bb4:
      %19 = llvm.load %4 : !llvm.ptr -> i64
      %20 = arith.constant 2 : i32
      %22 = arith.extsi %20 : i32 to i64
      %21 = arith.remsi %19, %22 : i64
      %23 = arith.constant 1 : i32
      %25 = arith.extsi %23 : i32 to i64
      %24 = arith.cmpi eq, %21, %25 : i64
      cf.cond_br %24, ^bb6, ^bb7
      ^bb6:
        %26 = llvm.load %8 : !llvm.ptr -> i64
        %27 = llvm.load %2 : !llvm.ptr -> i64
        %28 = arith.addi %26, %27 : i64
        llvm.store %28, %8 : i64, !llvm.ptr
        %29 = llvm.load %8 : !llvm.ptr -> i64
        %30 = arith.cmpi sge, %29, %arg2 : i64
        cf.cond_br %30, ^bb9, ^bb10
        ^bb9:
          %31 = llvm.load %8 : !llvm.ptr -> i64
          %32 = arith.subi %31, %arg2 : i64
          llvm.store %32, %8 : i64, !llvm.ptr
          cf.br ^bb11
        ^bb10:
          cf.br ^bb11
        ^bb11:
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %33 = llvm.load %2 : !llvm.ptr -> i64
      %34 = llvm.load %2 : !llvm.ptr -> i64
      %35 = arith.addi %33, %34 : i64
      llvm.store %35, %2 : i64, !llvm.ptr
      %36 = llvm.load %2 : !llvm.ptr -> i64
      %37 = arith.cmpi sge, %36, %arg2 : i64
      cf.cond_br %37, ^bb12, ^bb13
      ^bb12:
        %38 = llvm.load %2 : !llvm.ptr -> i64
        %39 = arith.subi %38, %arg2 : i64
        llvm.store %39, %2 : i64, !llvm.ptr
        cf.br ^bb14
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %40 = llvm.load %4 : !llvm.ptr -> i64
      %41 = arith.constant 2 : i32
      %43 = arith.extsi %41 : i32 to i64
      %42 = arith.divsi %40, %43 : i64
      llvm.store %42, %4 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %44 = llvm.load %8 : !llvm.ptr -> i64
    func.return %44 : i64
  }
  func.func @pow_mod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %45 = arith.remsi %arg0, %arg2 : i64
    %46 = llvm.mlir.constant(1 : i64) : i64
    %47 = llvm.alloca %46 x i64 : (i64) -> !llvm.ptr
    llvm.store %45, %47 : i64, !llvm.ptr
    %48 = llvm.mlir.constant(1 : i64) : i64
    %49 = llvm.alloca %48 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %49 : i64, !llvm.ptr
    %50 = arith.constant 1 : i32
    %51 = arith.extsi %50 : i32 to i64
    %52 = llvm.mlir.constant(1 : i64) : i64
    %53 = llvm.alloca %52 x i64 : (i64) -> !llvm.ptr
    llvm.store %51, %53 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %54 = llvm.load %49 : !llvm.ptr -> i64
    %55 = arith.constant 0 : i32
    %57 = arith.extsi %55 : i32 to i64
    %56 = arith.cmpi sgt, %54, %57 : i64
    cf.cond_br %56, ^bb16, ^bb17
    ^bb16:
      %58 = llvm.load %49 : !llvm.ptr -> i64
      %59 = arith.constant 2 : i32
      %61 = arith.extsi %59 : i32 to i64
      %60 = arith.remsi %58, %61 : i64
      %62 = arith.constant 1 : i32
      %64 = arith.extsi %62 : i32 to i64
      %63 = arith.cmpi eq, %60, %64 : i64
      cf.cond_br %63, ^bb18, ^bb19
      ^bb18:
        %66 = llvm.load %53 : !llvm.ptr -> i64
        %67 = llvm.load %47 : !llvm.ptr -> i64
        %65 = func.call @mul_mod(%66, %67, %arg2) : (i64, i64, i64) -> i64
        llvm.store %65, %53 : i64, !llvm.ptr
        cf.br ^bb20
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %69 = llvm.load %47 : !llvm.ptr -> i64
      %70 = llvm.load %47 : !llvm.ptr -> i64
      %68 = func.call @mul_mod(%69, %70, %arg2) : (i64, i64, i64) -> i64
      llvm.store %68, %47 : i64, !llvm.ptr
      %71 = llvm.load %49 : !llvm.ptr -> i64
      %72 = arith.constant 2 : i32
      %74 = arith.extsi %72 : i32 to i64
      %73 = arith.divsi %71, %74 : i64
      llvm.store %73, %49 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %75 = llvm.load %53 : !llvm.ptr -> i64
    func.return %75 : i64
  }
  func.func @mr_check(%arg0: i64, %arg1: i64) -> i1 {
    %76 = arith.constant 1 : i32
    %78 = arith.extsi %76 : i32 to i64
    %77 = arith.subi %arg0, %78 : i64
    %79 = llvm.mlir.constant(1 : i64) : i64
    %80 = llvm.alloca %79 x i64 : (i64) -> !llvm.ptr
    llvm.store %77, %80 : i64, !llvm.ptr
    %81 = arith.constant 0 : i32
    %82 = llvm.mlir.constant(1 : i64) : i64
    %83 = llvm.alloca %82 x i32 : (i64) -> !llvm.ptr
    llvm.store %81, %83 : i32, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %84 = llvm.load %80 : !llvm.ptr -> i64
    %85 = arith.constant 2 : i32
    %87 = arith.extsi %85 : i32 to i64
    %86 = arith.remsi %84, %87 : i64
    %88 = arith.constant 0 : i32
    %90 = arith.extsi %88 : i32 to i64
    %89 = arith.cmpi eq, %86, %90 : i64
    cf.cond_br %89, ^bb22, ^bb23
    ^bb22:
      %91 = llvm.load %80 : !llvm.ptr -> i64
      %92 = arith.constant 2 : i32
      %94 = arith.extsi %92 : i32 to i64
      %93 = arith.divsi %91, %94 : i64
      llvm.store %93, %80 : i64, !llvm.ptr
      %95 = llvm.load %83 : !llvm.ptr -> i32
      %96 = arith.constant 1 : i32
      %97 = arith.addi %95, %96 : i32
      llvm.store %97, %83 : i32, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %99 = llvm.load %80 : !llvm.ptr -> i64
    %98 = func.call @pow_mod(%arg1, %99, %arg0) : (i64, i64, i64) -> i64
    %100 = llvm.mlir.constant(1 : i64) : i64
    %101 = llvm.alloca %100 x i64 : (i64) -> !llvm.ptr
    llvm.store %98, %101 : i64, !llvm.ptr
    %102 = llvm.load %101 : !llvm.ptr -> i64
    %103 = arith.constant 1 : i32
    %105 = arith.extsi %103 : i32 to i64
    %104 = arith.cmpi eq, %102, %105 : i64
    %106 = scf.if %104 -> (i1) {
      %107 = arith.constant true
      scf.yield %107 : i1
    } else {
      %108 = llvm.load %101 : !llvm.ptr -> i64
      %109 = arith.constant 1 : i32
      %111 = arith.extsi %109 : i32 to i64
      %110 = arith.subi %arg0, %111 : i64
      %112 = arith.cmpi eq, %108, %110 : i64
      scf.yield %112 : i1
    }
    cf.cond_br %106, ^bb24, ^bb25
    ^bb24:
      %113 = arith.constant 1 : i1
      func.return %113 : i1
    ^bb25:
      cf.br ^bb26
    ^bb26:
    %114 = arith.constant 1 : i32
    %115 = llvm.mlir.constant(1 : i64) : i64
    %116 = llvm.alloca %115 x i32 : (i64) -> !llvm.ptr
    llvm.store %114, %116 : i32, !llvm.ptr
    cf.br ^bb27
    ^bb27:
    %117 = llvm.load %116 : !llvm.ptr -> i32
    %118 = llvm.load %83 : !llvm.ptr -> i32
    %119 = arith.cmpi slt, %117, %118 : i32
    cf.cond_br %119, ^bb28, ^bb29
    ^bb28:
      %121 = llvm.load %101 : !llvm.ptr -> i64
      %122 = llvm.load %101 : !llvm.ptr -> i64
      %120 = func.call @mul_mod(%121, %122, %arg0) : (i64, i64, i64) -> i64
      llvm.store %120, %101 : i64, !llvm.ptr
      %123 = llvm.load %101 : !llvm.ptr -> i64
      %124 = arith.constant 1 : i32
      %126 = arith.extsi %124 : i32 to i64
      %125 = arith.subi %arg0, %126 : i64
      %127 = arith.cmpi eq, %123, %125 : i64
      cf.cond_br %127, ^bb30, ^bb31
      ^bb30:
        %128 = arith.constant 1 : i1
        func.return %128 : i1
      ^bb31:
        cf.br ^bb32
      ^bb32:
      %129 = llvm.load %116 : !llvm.ptr -> i32
      %130 = arith.constant 1 : i32
      %131 = arith.addi %129, %130 : i32
      llvm.store %131, %116 : i32, !llvm.ptr
      cf.br ^bb27
    ^bb29:
    %132 = arith.constant 0 : i1
    func.return %132 : i1
  }
  func.func @is_prime(%arg0: i64) -> i1 {
    %133 = arith.constant 2 : i32
    %135 = arith.extsi %133 : i32 to i64
    %134 = arith.cmpi slt, %arg0, %135 : i64
    cf.cond_br %134, ^bb33, ^bb34
    ^bb33:
      %136 = arith.constant 0 : i1
      func.return %136 : i1
    ^bb34:
      cf.br ^bb35
    ^bb35:
    %137 = arith.constant 4 : i32
    %139 = arith.extsi %137 : i32 to i64
    %138 = arith.cmpi slt, %arg0, %139 : i64
    cf.cond_br %138, ^bb36, ^bb37
    ^bb36:
      %140 = arith.constant 1 : i1
      func.return %140 : i1
    ^bb37:
      cf.br ^bb38
    ^bb38:
    %141 = arith.constant 2 : i32
    %143 = arith.extsi %141 : i32 to i64
    %142 = arith.remsi %arg0, %143 : i64
    %144 = arith.constant 0 : i32
    %146 = arith.extsi %144 : i32 to i64
    %145 = arith.cmpi eq, %142, %146 : i64
    %147 = scf.if %145 -> (i1) {
      %148 = arith.constant true
      scf.yield %148 : i1
    } else {
      %149 = arith.constant 3 : i32
      %151 = arith.extsi %149 : i32 to i64
      %150 = arith.remsi %arg0, %151 : i64
      %152 = arith.constant 0 : i32
      %154 = arith.extsi %152 : i32 to i64
      %153 = arith.cmpi eq, %150, %154 : i64
      scf.yield %153 : i1
    }
    cf.cond_br %147, ^bb39, ^bb40
    ^bb39:
      %155 = arith.constant 0 : i1
      func.return %155 : i1
    ^bb40:
      cf.br ^bb41
    ^bb41:
    %156 = arith.constant 1000000 : i32
    %158 = arith.extsi %156 : i32 to i64
    %157 = arith.cmpi slt, %arg0, %158 : i64
    cf.cond_br %157, ^bb42, ^bb43
    ^bb42:
      %159 = arith.constant 5 : i32
      %160 = arith.extsi %159 : i32 to i64
      %161 = llvm.mlir.constant(1 : i64) : i64
      %162 = llvm.alloca %161 x i64 : (i64) -> !llvm.ptr
      llvm.store %160, %162 : i64, !llvm.ptr
      cf.br ^bb45
      ^bb45:
      %163 = llvm.load %162 : !llvm.ptr -> i64
      %164 = llvm.load %162 : !llvm.ptr -> i64
      %165 = arith.muli %163, %164 : i64
      %166 = arith.cmpi sle, %165, %arg0 : i64
      cf.cond_br %166, ^bb46, ^bb47
      ^bb46:
        %167 = llvm.load %162 : !llvm.ptr -> i64
        %168 = arith.remsi %arg0, %167 : i64
        %169 = arith.constant 0 : i32
        %171 = arith.extsi %169 : i32 to i64
        %170 = arith.cmpi eq, %168, %171 : i64
        %172 = scf.if %170 -> (i1) {
          %173 = arith.constant true
          scf.yield %173 : i1
        } else {
          %174 = llvm.load %162 : !llvm.ptr -> i64
          %175 = arith.constant 2 : i32
          %177 = arith.extsi %175 : i32 to i64
          %176 = arith.addi %174, %177 : i64
          %178 = arith.remsi %arg0, %176 : i64
          %179 = arith.constant 0 : i32
          %181 = arith.extsi %179 : i32 to i64
          %180 = arith.cmpi eq, %178, %181 : i64
          scf.yield %180 : i1
        }
        cf.cond_br %172, ^bb48, ^bb49
        ^bb48:
          %182 = arith.constant 0 : i1
          func.return %182 : i1
        ^bb49:
          cf.br ^bb50
        ^bb50:
        %183 = llvm.load %162 : !llvm.ptr -> i64
        %184 = arith.constant 6 : i32
        %186 = arith.extsi %184 : i32 to i64
        %185 = arith.addi %183, %186 : i64
        llvm.store %185, %162 : i64, !llvm.ptr
        cf.br ^bb45
      ^bb47:
      %187 = arith.constant 1 : i1
      func.return %187 : i1
    ^bb43:
      cf.br ^bb44
    ^bb44:
    %189 = arith.constant 2 : i32
    %190 = arith.extsi %189 : i32 to i64
    %188 = func.call @mr_check(%arg0, %190) : (i64, i64) -> i1
    %192 = arith.constant 1 : i1
    %191 = arith.xori %188, %192 : i1
    cf.cond_br %191, ^bb51, ^bb52
    ^bb51:
      %194 = arith.constant 0 : i1
      func.return %194 : i1
    ^bb52:
      cf.br ^bb53
    ^bb53:
    %196 = arith.constant 3 : i32
    %197 = arith.extsi %196 : i32 to i64
    %195 = func.call @mr_check(%arg0, %197) : (i64, i64) -> i1
    %199 = arith.constant 1 : i1
    %198 = arith.xori %195, %199 : i1
    cf.cond_br %198, ^bb54, ^bb55
    ^bb54:
      %201 = arith.constant 0 : i1
      func.return %201 : i1
    ^bb55:
      cf.br ^bb56
    ^bb56:
    %203 = arith.constant 5 : i32
    %204 = arith.extsi %203 : i32 to i64
    %202 = func.call @mr_check(%arg0, %204) : (i64, i64) -> i1
    %206 = arith.constant 1 : i1
    %205 = arith.xori %202, %206 : i1
    cf.cond_br %205, ^bb57, ^bb58
    ^bb57:
      %208 = arith.constant 0 : i1
      func.return %208 : i1
    ^bb58:
      cf.br ^bb59
    ^bb59:
    %210 = arith.constant 7 : i32
    %211 = arith.extsi %210 : i32 to i64
    %209 = func.call @mr_check(%arg0, %211) : (i64, i64) -> i1
    %213 = arith.constant 1 : i1
    %212 = arith.xori %209, %213 : i1
    cf.cond_br %212, ^bb60, ^bb61
    ^bb60:
      %215 = arith.constant 0 : i1
      func.return %215 : i1
    ^bb61:
      cf.br ^bb62
    ^bb62:
    %217 = arith.constant 11 : i32
    %218 = arith.extsi %217 : i32 to i64
    %216 = func.call @mr_check(%arg0, %218) : (i64, i64) -> i1
    %220 = arith.constant 1 : i1
    %219 = arith.xori %216, %220 : i1
    cf.cond_br %219, ^bb63, ^bb64
    ^bb63:
      %222 = arith.constant 0 : i1
      func.return %222 : i1
    ^bb64:
      cf.br ^bb65
    ^bb65:
    %224 = arith.constant 13 : i32
    %225 = arith.extsi %224 : i32 to i64
    %223 = func.call @mr_check(%arg0, %225) : (i64, i64) -> i1
    %227 = arith.constant 1 : i1
    %226 = arith.xori %223, %227 : i1
    cf.cond_br %226, ^bb66, ^bb67
    ^bb66:
      %229 = arith.constant 0 : i1
      func.return %229 : i1
    ^bb67:
      cf.br ^bb68
    ^bb68:
    %231 = arith.constant 23 : i32
    %232 = arith.extsi %231 : i32 to i64
    %230 = func.call @mr_check(%arg0, %232) : (i64, i64) -> i1
    %234 = arith.constant 1 : i1
    %233 = arith.xori %230, %234 : i1
    cf.cond_br %233, ^bb69, ^bb70
    ^bb69:
      %236 = arith.constant 0 : i1
      func.return %236 : i1
    ^bb70:
      cf.br ^bb71
    ^bb71:
    %237 = arith.constant 1 : i1
    func.return %237 : i1
  }
  func.func @concat(%arg0: i64, %arg1: i64) -> i64 {
    %238 = arith.constant 1 : i32
    %239 = arith.extsi %238 : i32 to i64
    %240 = llvm.mlir.constant(1 : i64) : i64
    %241 = llvm.alloca %240 x i64 : (i64) -> !llvm.ptr
    llvm.store %239, %241 : i64, !llvm.ptr
    %242 = llvm.mlir.constant(1 : i64) : i64
    %243 = llvm.alloca %242 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %243 : i64, !llvm.ptr
    cf.br ^bb72
    ^bb72:
    %244 = llvm.load %243 : !llvm.ptr -> i64
    %245 = arith.constant 0 : i32
    %247 = arith.extsi %245 : i32 to i64
    %246 = arith.cmpi sgt, %244, %247 : i64
    cf.cond_br %246, ^bb73, ^bb74
    ^bb73:
      %248 = llvm.load %241 : !llvm.ptr -> i64
      %249 = arith.constant 10 : i32
      %251 = arith.extsi %249 : i32 to i64
      %250 = arith.muli %248, %251 : i64
      llvm.store %250, %241 : i64, !llvm.ptr
      %252 = llvm.load %243 : !llvm.ptr -> i64
      %253 = arith.constant 10 : i32
      %255 = arith.extsi %253 : i32 to i64
      %254 = arith.divsi %252, %255 : i64
      llvm.store %254, %243 : i64, !llvm.ptr
      cf.br ^bb72
    ^bb74:
    %256 = llvm.load %241 : !llvm.ptr -> i64
    %257 = arith.muli %arg0, %256 : i64
    %258 = arith.addi %257, %arg1 : i64
    func.return %258 : i64
  }
  func.func @pair_ok(%arg0: i64, %arg1: i64) -> i1 {
    %260 = func.call @concat(%arg0, %arg1) : (i64, i64) -> i64
    %259 = func.call @is_prime(%260) : (i64) -> i1
    %261 = scf.if %259 -> (i1) {
      %263 = func.call @concat(%arg1, %arg0) : (i64, i64) -> i64
      %262 = func.call @is_prime(%263) : (i64) -> i1
      scf.yield %262 : i1
    } else {
      %264 = arith.constant false
      scf.yield %264 : i1
    }
    func.return %261 : i1
  }
  func.func @dfs(%arg0: i32, %arg1: i32, %arg2: i32, %arg3: !llvm.ptr, %arg4: !llvm.ptr, %arg5: !llvm.ptr, %arg6: !llvm.ptr) -> () {
    %265 = arith.constant 5 : i32
    %266 = arith.cmpi eq, %arg0, %265 : i32
    cf.cond_br %266, ^bb75, ^bb76
    ^bb75:
      %267 = arith.constant 0 : i32
      %268 = arith.extsi %267 : i32 to i64
      %269 = llvm.mlir.constant(1 : i64) : i64
      %270 = llvm.alloca %269 x i64 : (i64) -> !llvm.ptr
      llvm.store %268, %270 : i64, !llvm.ptr
      %271 = arith.constant 0 : i32
      %272 = llvm.mlir.constant(1 : i64) : i64
      %273 = llvm.alloca %272 x i32 : (i64) -> !llvm.ptr
      llvm.store %271, %273 : i32, !llvm.ptr
      cf.br ^bb78
      ^bb78:
      %274 = llvm.load %273 : !llvm.ptr -> i32
      %275 = arith.constant 5 : i32
      %276 = arith.cmpi slt, %274, %275 : i32
      cf.cond_br %276, ^bb79, ^bb80
      ^bb79:
        %277 = llvm.load %270 : !llvm.ptr -> i64
        %280 = llvm.load %273 : !llvm.ptr -> i32
        %281 = arith.extsi %280 : i32 to i64
        %282 = llvm.getelementptr %arg5[%281] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %279 = llvm.load %282 : !llvm.ptr -> i32
        %283 = arith.extsi %279 : i32 to i64
        %284 = llvm.getelementptr %arg3[%283] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %278 = llvm.load %284 : !llvm.ptr -> i64
        %285 = arith.addi %277, %278 : i64
        llvm.store %285, %270 : i64, !llvm.ptr
        %286 = llvm.load %273 : !llvm.ptr -> i32
        %287 = arith.constant 1 : i32
        %288 = arith.addi %286, %287 : i32
        llvm.store %288, %273 : i32, !llvm.ptr
        cf.br ^bb78
      ^bb80:
      %290 = arith.constant 0 : i32
      %291 = arith.extsi %290 : i32 to i64
      %292 = llvm.getelementptr %arg6[%291] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %289 = llvm.load %292 : !llvm.ptr -> i64
      %293 = arith.constant 0 : i32
      %295 = arith.extsi %293 : i32 to i64
      %294 = arith.cmpi eq, %289, %295 : i64
      %296 = scf.if %294 -> (i1) {
        %297 = arith.constant true
        scf.yield %297 : i1
      } else {
        %298 = llvm.load %270 : !llvm.ptr -> i64
        %300 = arith.constant 0 : i32
        %301 = arith.extsi %300 : i32 to i64
        %302 = llvm.getelementptr %arg6[%301] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %299 = llvm.load %302 : !llvm.ptr -> i64
        %303 = arith.cmpi slt, %298, %299 : i64
        scf.yield %303 : i1
      }
      cf.cond_br %296, ^bb81, ^bb82
      ^bb81:
        %304 = llvm.load %270 : !llvm.ptr -> i64
        %305 = arith.constant 0 : i32
        %306 = arith.extsi %305 : i32 to i64
        %307 = llvm.getelementptr %arg6[%306] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %304, %307 : i64, !llvm.ptr
        cf.br ^bb83
      ^bb82:
        cf.br ^bb83
      ^bb83:
      func.return
    ^bb76:
      cf.br ^bb77
    ^bb77:
    %308 = llvm.mlir.constant(1 : i64) : i64
    %309 = llvm.alloca %308 x i32 : (i64) -> !llvm.ptr
    llvm.store %arg1, %309 : i32, !llvm.ptr
    cf.br ^bb84
    ^bb84:
    %310 = llvm.load %309 : !llvm.ptr -> i32
    %311 = arith.cmpi slt, %310, %arg2 : i32
    cf.cond_br %311, ^bb85, ^bb86
    ^bb85:
      %312 = arith.constant 1 : i1
      %313 = llvm.mlir.constant(1 : i64) : i64
      %314 = llvm.alloca %313 x i1 : (i64) -> !llvm.ptr
      llvm.store %312, %314 : i1, !llvm.ptr
      %315 = arith.constant 0 : i32
      %316 = llvm.mlir.constant(1 : i64) : i64
      %317 = llvm.alloca %316 x i32 : (i64) -> !llvm.ptr
      llvm.store %315, %317 : i32, !llvm.ptr
      cf.br ^bb87
      ^bb87:
      %318 = llvm.load %317 : !llvm.ptr -> i32
      %319 = arith.cmpi slt, %318, %arg0 : i32
      cf.cond_br %319, ^bb88, ^bb89
      ^bb88:
        %322 = llvm.load %317 : !llvm.ptr -> i32
        %323 = arith.extsi %322 : i32 to i64
        %324 = llvm.getelementptr %arg5[%323] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %321 = llvm.load %324 : !llvm.ptr -> i32
        %325 = arith.muli %321, %arg2 : i32
        %326 = llvm.load %309 : !llvm.ptr -> i32
        %327 = arith.addi %325, %326 : i32
        %328 = arith.extsi %327 : i32 to i64
        %329 = llvm.getelementptr %arg4[%328] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        %320 = llvm.load %329 : !llvm.ptr -> i8
        %330 = arith.constant 0 : i32
        %332 = arith.extsi %320 : i8 to i32
        %331 = arith.cmpi eq, %332, %330 : i32
        cf.cond_br %331, ^bb90, ^bb91
        ^bb90:
          %333 = arith.constant 0 : i1
          llvm.store %333, %314 : i1, !llvm.ptr
          cf.br ^bb89
        ^bb91:
          cf.br ^bb92
        ^bb92:
        %334 = llvm.load %317 : !llvm.ptr -> i32
        %335 = arith.constant 1 : i32
        %336 = arith.addi %334, %335 : i32
        llvm.store %336, %317 : i32, !llvm.ptr
        cf.br ^bb87
      ^bb89:
      %337 = llvm.load %314 : !llvm.ptr -> i1
      cf.cond_br %337, ^bb93, ^bb94
      ^bb93:
        %338 = llvm.load %309 : !llvm.ptr -> i32
        %339 = arith.extsi %arg0 : i32 to i64
        %340 = llvm.getelementptr %arg5[%339] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        llvm.store %338, %340 : i32, !llvm.ptr
        %341 = arith.constant 0 : i32
        %342 = arith.extsi %341 : i32 to i64
        %343 = llvm.mlir.constant(1 : i64) : i64
        %344 = llvm.alloca %343 x i64 : (i64) -> !llvm.ptr
        llvm.store %342, %344 : i64, !llvm.ptr
        %345 = arith.constant 0 : i32
        llvm.store %345, %317 : i32, !llvm.ptr
        cf.br ^bb96
        ^bb96:
        %346 = llvm.load %317 : !llvm.ptr -> i32
        %347 = arith.cmpi sle, %346, %arg0 : i32
        cf.cond_br %347, ^bb97, ^bb98
        ^bb97:
          %348 = llvm.load %344 : !llvm.ptr -> i64
          %351 = llvm.load %317 : !llvm.ptr -> i32
          %352 = arith.extsi %351 : i32 to i64
          %353 = llvm.getelementptr %arg5[%352] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %350 = llvm.load %353 : !llvm.ptr -> i32
          %354 = arith.extsi %350 : i32 to i64
          %355 = llvm.getelementptr %arg3[%354] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %349 = llvm.load %355 : !llvm.ptr -> i64
          %356 = arith.addi %348, %349 : i64
          llvm.store %356, %344 : i64, !llvm.ptr
          %357 = llvm.load %317 : !llvm.ptr -> i32
          %358 = arith.constant 1 : i32
          %359 = arith.addi %357, %358 : i32
          llvm.store %359, %317 : i32, !llvm.ptr
          cf.br ^bb96
        ^bb98:
        %361 = arith.constant 0 : i32
        %362 = arith.extsi %361 : i32 to i64
        %363 = llvm.getelementptr %arg6[%362] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %360 = llvm.load %363 : !llvm.ptr -> i64
        %364 = arith.constant 0 : i32
        %366 = arith.extsi %364 : i32 to i64
        %365 = arith.cmpi eq, %360, %366 : i64
        %367 = scf.if %365 -> (i1) {
          %368 = arith.constant true
          scf.yield %368 : i1
        } else {
          %369 = llvm.load %344 : !llvm.ptr -> i64
          %371 = arith.constant 0 : i32
          %372 = arith.extsi %371 : i32 to i64
          %373 = llvm.getelementptr %arg6[%372] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %370 = llvm.load %373 : !llvm.ptr -> i64
          %374 = arith.cmpi slt, %369, %370 : i64
          scf.yield %374 : i1
        }
        cf.cond_br %367, ^bb99, ^bb100
        ^bb99:
          %376 = arith.constant 1 : i32
          %377 = arith.addi %arg0, %376 : i32
          %378 = llvm.load %309 : !llvm.ptr -> i32
          %379 = arith.constant 1 : i32
          %380 = arith.addi %378, %379 : i32
          func.call @dfs(%377, %380, %arg2, %arg3, %arg4, %arg5, %arg6) : (i32, i32, i32, !llvm.ptr, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> ()
          cf.br ^bb101
        ^bb100:
          cf.br ^bb101
        ^bb101:
        cf.br ^bb95
      ^bb94:
        cf.br ^bb95
      ^bb95:
      %381 = llvm.load %309 : !llvm.ptr -> i32
      %382 = arith.constant 1 : i32
      %383 = arith.addi %381, %382 : i32
      llvm.store %383, %309 : i32, !llvm.ptr
      cf.br ^bb84
    ^bb86:
    func.return
  }
  func.func @main() -> i32 {
    %384 = arith.constant 9000 : i32
    %385 = arith.extsi %384 : i32 to i64
    %387 = arith.constant 1 : i32
    %388 = arith.extsi %387 : i32 to i64
    %386 = func.call @calloc(%385, %388) : (i64, i64) -> !llvm.ptr
    %390 = arith.constant 2000 : i32
    %391 = arith.constant 8 : i32
    %392 = arith.extsi %390 : i32 to i64
    %393 = arith.extsi %391 : i32 to i64
    %389 = func.call @calloc(%392, %393) : (i64, i64) -> !llvm.ptr
    %394 = llvm.mlir.zero : !llvm.ptr
    %395 = llvm.icmp "eq" %386, %394 : !llvm.ptr
    %396 = scf.if %395 -> (i1) {
      %397 = arith.constant true
      scf.yield %397 : i1
    } else {
      %398 = llvm.mlir.zero : !llvm.ptr
      %399 = llvm.icmp "eq" %389, %398 : !llvm.ptr
      scf.yield %399 : i1
    }
    cf.cond_br %396, ^bb102, ^bb103
    ^bb102:
      %400 = arith.constant 1 : i32
      func.return %400 : i32
    ^bb103:
      cf.br ^bb104
    ^bb104:
    %401 = arith.constant 1 : i32
    %402 = arith.constant 0 : i32
    %403 = arith.trunci %401 : i32 to i8
    %404 = arith.extsi %402 : i32 to i64
    %405 = llvm.getelementptr %386[%404] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %403, %405 : i8, !llvm.ptr
    %406 = arith.constant 1 : i32
    %407 = arith.constant 1 : i32
    %408 = arith.trunci %406 : i32 to i8
    %409 = arith.extsi %407 : i32 to i64
    %410 = llvm.getelementptr %386[%409] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %408, %410 : i8, !llvm.ptr
    %411 = arith.constant 2 : i32
    %412 = arith.extsi %411 : i32 to i64
    %413 = llvm.mlir.constant(1 : i64) : i64
    %414 = llvm.alloca %413 x i64 : (i64) -> !llvm.ptr
    llvm.store %412, %414 : i64, !llvm.ptr
    cf.br ^bb105
    ^bb105:
    %415 = llvm.load %414 : !llvm.ptr -> i64
    %416 = llvm.load %414 : !llvm.ptr -> i64
    %417 = arith.muli %415, %416 : i64
    %418 = arith.cmpi slt, %417, %385 : i64
    cf.cond_br %418, ^bb106, ^bb107
    ^bb106:
      %420 = llvm.load %414 : !llvm.ptr -> i64
      %421 = llvm.getelementptr %386[%420] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %419 = llvm.load %421 : !llvm.ptr -> i8
      %422 = arith.constant 0 : i32
      %424 = arith.extsi %419 : i8 to i32
      %423 = arith.cmpi eq, %424, %422 : i32
      cf.cond_br %423, ^bb108, ^bb109
      ^bb108:
        %425 = llvm.load %414 : !llvm.ptr -> i64
        %426 = llvm.load %414 : !llvm.ptr -> i64
        %427 = arith.muli %425, %426 : i64
        %428 = llvm.mlir.constant(1 : i64) : i64
        %429 = llvm.alloca %428 x i64 : (i64) -> !llvm.ptr
        llvm.store %427, %429 : i64, !llvm.ptr
        cf.br ^bb111
        ^bb111:
        %430 = llvm.load %429 : !llvm.ptr -> i64
        %431 = arith.cmpi slt, %430, %385 : i64
        cf.cond_br %431, ^bb112, ^bb113
        ^bb112:
          %432 = arith.constant 1 : i32
          %433 = llvm.load %429 : !llvm.ptr -> i64
          %434 = arith.trunci %432 : i32 to i8
          %435 = llvm.getelementptr %386[%433] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %434, %435 : i8, !llvm.ptr
          %436 = llvm.load %429 : !llvm.ptr -> i64
          %437 = llvm.load %414 : !llvm.ptr -> i64
          %438 = arith.addi %436, %437 : i64
          llvm.store %438, %429 : i64, !llvm.ptr
          cf.br ^bb111
        ^bb113:
        cf.br ^bb110
      ^bb109:
        cf.br ^bb110
      ^bb110:
      %439 = llvm.load %414 : !llvm.ptr -> i64
      %440 = arith.constant 1 : i32
      %442 = arith.extsi %440 : i32 to i64
      %441 = arith.addi %439, %442 : i64
      llvm.store %441, %414 : i64, !llvm.ptr
      cf.br ^bb105
    ^bb107:
    %443 = arith.constant 0 : i32
    %444 = llvm.mlir.constant(1 : i64) : i64
    %445 = llvm.alloca %444 x i32 : (i64) -> !llvm.ptr
    llvm.store %443, %445 : i32, !llvm.ptr
    %446 = arith.constant 3 : i32
    %447 = arith.extsi %446 : i32 to i64
    llvm.store %447, %414 : i64, !llvm.ptr
    cf.br ^bb114
    ^bb114:
    %448 = llvm.load %414 : !llvm.ptr -> i64
    %449 = arith.cmpi slt, %448, %385 : i64
    cf.cond_br %449, ^bb115, ^bb116
    ^bb115:
      %451 = llvm.load %414 : !llvm.ptr -> i64
      %452 = llvm.getelementptr %386[%451] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %450 = llvm.load %452 : !llvm.ptr -> i8
      %453 = arith.constant 0 : i32
      %455 = arith.extsi %450 : i8 to i32
      %454 = arith.cmpi eq, %455, %453 : i32
      cf.cond_br %454, ^bb117, ^bb118
      ^bb117:
        %456 = llvm.load %414 : !llvm.ptr -> i64
        %457 = llvm.load %445 : !llvm.ptr -> i32
        %458 = arith.extsi %457 : i32 to i64
        %459 = llvm.getelementptr %389[%458] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %456, %459 : i64, !llvm.ptr
        %460 = llvm.load %445 : !llvm.ptr -> i32
        %461 = arith.constant 1 : i32
        %462 = arith.addi %460, %461 : i32
        llvm.store %462, %445 : i32, !llvm.ptr
        cf.br ^bb119
      ^bb118:
        cf.br ^bb119
      ^bb119:
      %463 = llvm.load %414 : !llvm.ptr -> i64
      %464 = arith.constant 2 : i32
      %466 = arith.extsi %464 : i32 to i64
      %465 = arith.addi %463, %466 : i64
      llvm.store %465, %414 : i64, !llvm.ptr
      cf.br ^bb114
    ^bb116:
    %468 = llvm.load %445 : !llvm.ptr -> i32
    %469 = llvm.load %445 : !llvm.ptr -> i32
    %470 = arith.muli %468, %469 : i32
    %471 = arith.extsi %470 : i32 to i64
    %472 = arith.constant 1 : i32
    %473 = arith.extsi %472 : i32 to i64
    %467 = func.call @calloc(%471, %473) : (i64, i64) -> !llvm.ptr
    %474 = llvm.mlir.zero : !llvm.ptr
    %475 = llvm.icmp "eq" %467, %474 : !llvm.ptr
    cf.cond_br %475, ^bb120, ^bb121
    ^bb120:
      %476 = arith.constant 1 : i32
      func.return %476 : i32
    ^bb121:
      cf.br ^bb122
    ^bb122:
    %477 = arith.constant 0 : i32
    %478 = llvm.mlir.constant(1 : i64) : i64
    %479 = llvm.alloca %478 x i32 : (i64) -> !llvm.ptr
    llvm.store %477, %479 : i32, !llvm.ptr
    cf.br ^bb123
    ^bb123:
    %480 = llvm.load %479 : !llvm.ptr -> i32
    %481 = llvm.load %445 : !llvm.ptr -> i32
    %482 = arith.cmpi slt, %480, %481 : i32
    cf.cond_br %482, ^bb124, ^bb125
    ^bb124:
      %483 = llvm.load %479 : !llvm.ptr -> i32
      %484 = arith.constant 1 : i32
      %485 = arith.addi %483, %484 : i32
      %486 = llvm.mlir.constant(1 : i64) : i64
      %487 = llvm.alloca %486 x i32 : (i64) -> !llvm.ptr
      llvm.store %485, %487 : i32, !llvm.ptr
      cf.br ^bb126
      ^bb126:
      %488 = llvm.load %487 : !llvm.ptr -> i32
      %489 = llvm.load %445 : !llvm.ptr -> i32
      %490 = arith.cmpi slt, %488, %489 : i32
      cf.cond_br %490, ^bb127, ^bb128
      ^bb127:
        %493 = llvm.load %479 : !llvm.ptr -> i32
        %494 = arith.extsi %493 : i32 to i64
        %495 = llvm.getelementptr %389[%494] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %492 = llvm.load %495 : !llvm.ptr -> i64
        %497 = llvm.load %487 : !llvm.ptr -> i32
        %498 = arith.extsi %497 : i32 to i64
        %499 = llvm.getelementptr %389[%498] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %496 = llvm.load %499 : !llvm.ptr -> i64
        %491 = func.call @pair_ok(%492, %496) : (i64, i64) -> i1
        cf.cond_br %491, ^bb129, ^bb130
        ^bb129:
          %500 = arith.constant 1 : i32
          %501 = llvm.load %479 : !llvm.ptr -> i32
          %502 = llvm.load %445 : !llvm.ptr -> i32
          %503 = arith.muli %501, %502 : i32
          %504 = llvm.load %487 : !llvm.ptr -> i32
          %505 = arith.addi %503, %504 : i32
          %506 = arith.trunci %500 : i32 to i8
          %507 = arith.extsi %505 : i32 to i64
          %508 = llvm.getelementptr %467[%507] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %506, %508 : i8, !llvm.ptr
          %509 = arith.constant 1 : i32
          %510 = llvm.load %487 : !llvm.ptr -> i32
          %511 = llvm.load %445 : !llvm.ptr -> i32
          %512 = arith.muli %510, %511 : i32
          %513 = llvm.load %479 : !llvm.ptr -> i32
          %514 = arith.addi %512, %513 : i32
          %515 = arith.trunci %509 : i32 to i8
          %516 = arith.extsi %514 : i32 to i64
          %517 = llvm.getelementptr %467[%516] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %515, %517 : i8, !llvm.ptr
          cf.br ^bb131
        ^bb130:
          cf.br ^bb131
        ^bb131:
        %518 = llvm.load %487 : !llvm.ptr -> i32
        %519 = arith.constant 1 : i32
        %520 = arith.addi %518, %519 : i32
        llvm.store %520, %487 : i32, !llvm.ptr
        cf.br ^bb126
      ^bb128:
      %521 = llvm.load %479 : !llvm.ptr -> i32
      %522 = arith.constant 1 : i32
      %523 = arith.addi %521, %522 : i32
      llvm.store %523, %479 : i32, !llvm.ptr
      cf.br ^bb123
    ^bb125:
    %525 = arith.constant 5 : i32
    %526 = arith.constant 4 : i32
    %527 = arith.extsi %525 : i32 to i64
    %528 = arith.extsi %526 : i32 to i64
    %524 = func.call @calloc(%527, %528) : (i64, i64) -> !llvm.ptr
    %530 = arith.constant 1 : i32
    %531 = arith.constant 8 : i32
    %532 = arith.extsi %530 : i32 to i64
    %533 = arith.extsi %531 : i32 to i64
    %529 = func.call @calloc(%532, %533) : (i64, i64) -> !llvm.ptr
    %534 = llvm.mlir.zero : !llvm.ptr
    %535 = llvm.icmp "eq" %524, %534 : !llvm.ptr
    %536 = scf.if %535 -> (i1) {
      %537 = arith.constant true
      scf.yield %537 : i1
    } else {
      %538 = llvm.mlir.zero : !llvm.ptr
      %539 = llvm.icmp "eq" %529, %538 : !llvm.ptr
      scf.yield %539 : i1
    }
    cf.cond_br %536, ^bb132, ^bb133
    ^bb132:
      %540 = arith.constant 1 : i32
      func.return %540 : i32
    ^bb133:
      cf.br ^bb134
    ^bb134:
    %541 = arith.constant 0 : i32
    %542 = arith.constant 0 : i32
    %543 = arith.extsi %541 : i32 to i64
    %544 = arith.extsi %542 : i32 to i64
    %545 = llvm.getelementptr %529[%544] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %543, %545 : i64, !llvm.ptr
    %547 = arith.constant 0 : i32
    %548 = arith.constant 0 : i32
    %549 = llvm.load %445 : !llvm.ptr -> i32
    func.call @dfs(%547, %548, %549, %389, %467, %524, %529) : (i32, i32, i32, !llvm.ptr, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> ()
    %550 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %552 = arith.constant 0 : i32
    %553 = arith.extsi %552 : i32 to i64
    %554 = llvm.getelementptr %529[%553] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %551 = llvm.load %554 : !llvm.ptr -> i64
    %555 = llvm.call @printf(%550, %551) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%529) : (!llvm.ptr) -> ()
    func.call @free(%524) : (!llvm.ptr) -> ()
    func.call @free(%467) : (!llvm.ptr) -> ()
    func.call @free(%389) : (!llvm.ptr) -> ()
    func.call @free(%386) : (!llvm.ptr) -> ()
    %561 = arith.constant 0 : i32
    func.return %561 : i32
  }
}