Problem 784

Reciprocal Pairs: F(2e6). For each r, count divisors d of (r-1)(r+1) with d <= r-1 and sum base+d+n/d.

Answer5833303012576429231
Output5833303012576429231
StatusPASS
Native helperno
Runtime1530 ms
Peak memory18160 KB
Time complexityO(n^3) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n log log n)
Space complexityO(n^2)O(n)
ApproachFlow solutionSieve-based divisor sums
VerdictSuboptimal

Flow source

# Project Euler 784
# Reciprocal Pairs: F(2e6).
# For each r, count divisors d of (r-1)(r+1) with d <= r-1 and sum base+d+n/d.

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

function bitlen_lsb(x: i64) -> i32 {
    let c: i32 = 0
    let mut v: i64 = x & -x
    while v != 0 {
        v = v >> 1
        c = c + 1
    }
    return c
}

function main() -> i32 {
    let N: i64 = 2000000
    let spf: ptr<i64> = calloc(N + 2, 8)
    let primes: ptr<i64> = malloc((N + 2) * 8)
    let mut pc: i64 = 0
    spf[1] = 1
    for i in 2..(N + 2) {
        if spf[i] == 0 {
            spf[i] = i
            primes[pc] = i
            pc = pc + 1
        }
        for j in 0..pc {
            let p: i64 = primes[j]
            let ip: i64 = i * p
            if ip > N + 1 || p > spf[i] {
                break
            }
            spf[ip] = p
        }
    }

    let mut total: i64 = 0
    let mut r: i64 = 2
    while r < N {
        let mut kmax: i64 = N - r
        if kmax > r - 1 {
            kmax = r - 1
        }
        if kmax <= 0 {
            r = r + 1
            continue
        }
        let n_val: i64 = r * r - 1
        let base: i64 = 2 * r
        if kmax == 1 {
            total = total + base + 1 + n_val
            r = r + 1
            continue
        }
        let mut a: i64 = r - 1
        let mut b: i64 = r + 1
        let fac_p: ptr<i64> = malloc(64 * 8)
        let fac_e: ptr<i64> = malloc(64 * 8)
        let mut nf: i64 = 0
        if (r & 1) != 0 {
            let ea: i32 = bitlen_lsb(a) - 1
            let eb: i32 = bitlen_lsb(b) - 1
            let e2: i32 = ea + eb
            a = a >> ea
            b = b >> eb
            if 2 <= kmax {
                fac_p[nf] = 2
                fac_e[nf] = e2 as i64
                nf = nf + 1
            }
        }
        let mut x: i64 = a
        while x > 1 {
            let p: i64 = spf[x]
            if p > kmax {
                break
            }
            let mut e: i64 = 0
            while x > 1 && spf[x] == p {
                x = x / p
                e = e + 1
            }
            fac_p[nf] = p
            fac_e[nf] = e
            nf = nf + 1
        }
        x = b
        while x > 1 {
            let p: i64 = spf[x]
            if p > kmax {
                break
            }
            let mut e: i64 = 0
            while x > 1 && spf[x] == p {
                x = x / p
                e = e + 1
            }
            fac_p[nf] = p
            fac_e[nf] = e
            nf = nf + 1
        }

        let divs: ptr<i64> = malloc(4096 * 8)
        let newd: ptr<i64> = malloc(4096 * 8)
        let mut nd: i64 = 1
        divs[0] = 1
        for fi in 0..nf {
            let p: i64 = fac_p[fi]
            let e: i64 = fac_e[fi]
            let prev_n: i64 = nd
            let mut pow_p: i64 = 1
            let mut nn: i64 = 0
            for ee in 0..(e + 1) {
                for di in 0..prev_n {
                    let v: i64 = divs[di] * pow_p
                    if v <= kmax {
                        newd[nn] = v
                        nn = nn + 1
                    }
                }
                pow_p = pow_p * p
                if pow_p > kmax {
                    break
                }
            }
            for i in 0..nn {
                divs[i] = newd[i]
            }
            nd = nn
        }
        for i in 0..nd {
            total = total + base + divs[i] + n_val / divs[i]
        }
        free(fac_p)
        free(fac_e)
        free(divs)
        free(newd)
        r = r + 1
    }
    free(spf)
    free(primes)
    printf("%lld\n", total)
    return 0
}

Generated C

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Flow runtime helpers */
typedef struct flow_temp_node { struct flow_temp_node* next; } flow_temp_node;
static flow_temp_node* flow_temp_head = NULL;
static int flow_temp_atexit_set = 0;
__attribute__((unused)) static void flow_temp_free_all(void) {
    while (flow_temp_head) {
        flow_temp_node* n = flow_temp_head;
        flow_temp_head = n->next;
        free(n);
    }
}
__attribute__((unused)) static void* flow_temp_alloc(size_t nbytes) {
    flow_temp_node* node = (flow_temp_node*)malloc(sizeof(flow_temp_node) + nbytes);
    if (!node) return NULL;
    node->next = flow_temp_head;
    flow_temp_head = node;
    if (!flow_temp_atexit_set) {
        flow_temp_atexit_set = 1;
        atexit(flow_temp_free_all);
    }
    return (void*)(node + 1);
}
#ifndef FLOW_DIAG
#define FLOW_DIAG(msg) fprintf(stderr, "%s", (msg))
#endif
#ifndef FLOW_LOG
#define FLOW_LOG(fmt, ...) printf(fmt, __VA_ARGS__)
#endif
#ifndef FLOW_LOG_EMPTY
#define FLOW_LOG_EMPTY(fmt) printf(fmt)
#endif
static char* flow_strcat(const char* a, const char* b) {
    size_t la = strlen(a ? a : ""), lb = strlen(b ? b : "");
    char* r = (char*)flow_temp_alloc(la + lb + 1);
    if (!r) return NULL;
    if (la) memcpy(r, a, la);
    if (lb) memcpy(r + la, b, lb);
    r[la + lb] = '\0';
    return r;
}

#define __flow_in_arr(arr, val) __extension__ ({ \
    int _found = 0; \
    size_t _n = sizeof(arr)/sizeof((arr)[0]); \
    for (size_t _i = 0; _i < _n; _i++) { \
        if ((arr)[_i] == (val)) { _found = 1; break; } \
    } _found; })

/* Unified fault handler (MISRA #279) — override with -DFLOW_FAULT_HANDLER=fn */
#ifndef FLOW_FAULT_HANDLER
__attribute__((unused)) static inline void flow_fault_handler(const char* msg) {
    fprintf(stderr, "flow: %s\n", msg ? msg : "fault");
    abort();
#if defined(__GNUC__) || defined(__clang__)
    __builtin_unreachable();
#endif
}
#else
#define flow_fault_handler FLOW_FAULT_HANDLER
#endif
#define flow_div_by_zero_handler() flow_fault_handler("division by zero")
#define flow_shift_ub_handler() flow_fault_handler("invalid shift (amount out of range or left-shift of negative)")

#ifndef FLOW_CHECKED_DIV
#define FLOW_CHECKED_DIV(L, R) (((R) != 0) ? ((L) / (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_MOD
#define FLOW_CHECKED_MOD(L, R) (((R) != 0) ? ((L) % (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHL
#define FLOW_CHECKED_SHL(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull)) && ((L) >= 0)) ? ((L) << (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHR
#define FLOW_CHECKED_SHR(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull))) ? ((L) >> (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif

#include <math.h>

void* _ui_state = NULL;

static inline float i32_to_f32(int32_t v) { return (float)v; }

/* Host stub for @gpu kernels (device codegen replaces this). */
static inline int32_t gpu_thread_id(void) { return 0; }

int32_t bitlen_lsb_i64(int64_t x);
int32_t main(void);




int32_t bitlen_lsb_i64(int64_t x) {
    int32_t c = 0;
    int64_t v = (x & (-x));
    while (v != 0) {
        v = FLOW_CHECKED_SHR((v), (1));
        c = (c + 1);
    }
    return c;
}

int32_t main(void) {
    int64_t N = 2000000;
    int64_t* spf = (int64_t*)(calloc((N + 2), 8));
    int64_t* primes = (int64_t*)(malloc(((N + 2) * 8)));
    int64_t pc = 0;
    spf[1] = 1;
    int32_t __flow_step_1 = 1;
    for (int32_t i = 2; (2 <= (N + 2)) ? i < (N + 2) : i > (N + 2); i += (2 <= (N + 2)) ? 1 : -1) {
        if (spf[i] == 0) {
            spf[i] = i;
            primes[pc] = i;
            pc = (pc + 1);
        }
        int32_t __flow_step_2 = 1;
        for (int32_t j = 0; (0 <= pc) ? j < pc : j > pc; j += (0 <= pc) ? 1 : -1) {
            int64_t p = primes[j];
            int64_t ip = (i * p);
            if ((ip > (N + 1) || p > spf[i])) {
                break;
            }
            spf[ip] = p;
        }
    }
    int64_t total = 0;
    int64_t r = 2;
    while (r < N) {
        int64_t kmax = (N - r);
        if (kmax > (r - 1)) {
            kmax = (r - 1);
        }
        if (kmax <= 0) {
            r = (r + 1);
            continue;
        }
        int64_t n_val = ((r * r) - 1);
        int64_t base = (2 * r);
        if (kmax == 1) {
            total = (((total + base) + 1) + n_val);
            r = (r + 1);
            continue;
        }
        int64_t a = (r - 1);
        int64_t b = (r + 1);
        int64_t* fac_p = (int64_t*)(malloc((64 * 8)));
        int64_t* fac_e = (int64_t*)(malloc((64 * 8)));
        int64_t nf = 0;
        if ((r & 1) != 0) {
            int32_t ea = (bitlen_lsb_i64(a) - 1);
            int32_t eb = (bitlen_lsb_i64(b) - 1);
            int32_t e2 = (ea + eb);
            a = FLOW_CHECKED_SHR((a), (ea));
            b = FLOW_CHECKED_SHR((b), (eb));
            if (2 <= kmax) {
                fac_p[nf] = 2;
                fac_e[nf] = ((int64_t)(e2));
                nf = (nf + 1);
            }
        }
        int64_t x = a;
        while (x > 1) {
            int64_t p = spf[x];
            if (p > kmax) {
                break;
            }
            int64_t e = 0;
            while ((x > 1 && spf[x] == p)) {
                x = FLOW_CHECKED_DIV((x), (p));
                e = (e + 1);
            }
            fac_p[nf] = p;
            fac_e[nf] = e;
            nf = (nf + 1);
        }
        x = b;
        while (x > 1) {
            int64_t p = spf[x];
            if (p > kmax) {
                break;
            }
            int64_t e = 0;
            while ((x > 1 && spf[x] == p)) {
                x = FLOW_CHECKED_DIV((x), (p));
                e = (e + 1);
            }
            fac_p[nf] = p;
            fac_e[nf] = e;
            nf = (nf + 1);
        }
        int64_t* divs = (int64_t*)(malloc((4096 * 8)));
        int64_t* newd = (int64_t*)(malloc((4096 * 8)));
        int64_t nd = 1;
        divs[0] = 1;
        int32_t __flow_step_3 = 1;
        for (int32_t fi = 0; (0 <= nf) ? fi < nf : fi > nf; fi += (0 <= nf) ? 1 : -1) {
            int64_t p = fac_p[fi];
            int64_t e = fac_e[fi];
            int64_t prev_n = nd;
            int64_t pow_p = 1;
            int64_t nn = 0;
            int32_t __flow_step_4 = 1;
            for (int32_t ee = 0; (0 <= (e + 1)) ? ee < (e + 1) : ee > (e + 1); ee += (0 <= (e + 1)) ? 1 : -1) {
                int32_t __flow_step_5 = 1;
                for (int32_t di = 0; (0 <= prev_n) ? di < prev_n : di > prev_n; di += (0 <= prev_n) ? 1 : -1) {
                    int64_t v = (divs[di] * pow_p);
                    if (v <= kmax) {
                        newd[nn] = v;
                        nn = (nn + 1);
                    }
                }
                pow_p = (pow_p * p);
                if (pow_p > kmax) {
                    break;
                }
            }
            int32_t __flow_step_6 = 1;
            for (int32_t i = 0; (0 <= nn) ? i < nn : i > nn; i += (0 <= nn) ? 1 : -1) {
                divs[i] = newd[i];
            }
            nd = nn;
        }
        int32_t __flow_step_7 = 1;
        for (int32_t i = 0; (0 <= nd) ? i < nd : i > nd; i += (0 <= nd) ? 1 : -1) {
            total = (((total + base) + divs[i]) + FLOW_CHECKED_DIV((n_val), (divs[i])));
        }
        free(fac_p);
        free(fac_e);
        free(divs);
        free(newd);
        r = (r + 1);
    }
    free(spf);
    free(primes);
    printf("%lld\n", total);
    return 0;
}

Generated MLIR

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