Problem 882

Pure Flow port. Compute S(100000).

Answer15800662276
Output15800662276
StatusPASS
Native helperno
Runtime10 ms
Peak memory1920 KB
Time complexityO(n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)?
Space complexityO(n)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 882: Removing Bits
# Pure Flow port. Compute S(100000).

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

function simplest_between(a_scaled: i64, b_scaled: i64, max_exp: i64) -> i64 {
    let den: i64 = 1 << max_exp

    if a_scaled == -1 {
        if b_scaled == -1 {
            return 0
        }
        return ((b_scaled - 1) / den) * den
    }

    if b_scaled == -1 {
        return ((a_scaled / den) + 1) * den
    }

    let mut k: i64 = 0
    while k <= max_exp {
        let step: i64 = 1 << (max_exp - k)
        let m_low: i64 = a_scaled / step + 1
        let m_high: i64 = (b_scaled - 1) / step
        if m_low <= m_high {
            return m_low * step
        }
        k = k + 1
    }
    return 0
}

function compute_S(n: i64) -> i64 {
    if n <= 1 { return n }

    let mut max_exp: i64 = 0
    let mut tmp: i64 = n
    while tmp > 0 {
        max_exp = max_exp + 1
        tmp = tmp >> 1
    }
    let den: i64 = 1 << max_exp

    let g: ptr<i64> = calloc(n + 1, 8)
    let mut total_scaled: i64 = 0

    let left_opts: ptr<i64> = malloc(64 * 8)
    let right_opts: ptr<i64> = malloc(64 * 8)

    let mut x: i64 = 1
    while x <= n {
        let mut left_count: i64 = 0
        let mut right_count: i64 = 0

        let mut bits: i64 = 0
        let mut tmpx: i64 = x
        while tmpx > 0 {
            bits = bits + 1
            tmpx = tmpx >> 1
        }

        let mut j: i64 = 0
        while j < bits {
            let bit: i64 = (x >> j) & 1
            let higher: i64 = x >> (j + 1)
            let lower: i64 = x & ((1 << j) - 1)
            let y: i64 = (higher << j) | lower

            if bit != 0 {
                left_opts[left_count] = g[y]
                left_count = left_count + 1
            } else {
                right_opts[right_count] = g[y]
                right_count = right_count + 1
            }
            j = j + 1
        }

        let mut changed: bool = true
        while changed {
            changed = false
            if right_count > 0 {
                let mut min_r: i64 = right_opts[0]
                let mut i: i64 = 1
                while i < right_count {
                    if right_opts[i] < min_r {
                        min_r = right_opts[i]
                    }
                    i = i + 1
                }
                let mut new_left_count: i64 = 0
                i = 0
                while i < left_count {
                    if left_opts[i] < min_r {
                        left_opts[new_left_count] = left_opts[i]
                        new_left_count = new_left_count + 1
                    }
                    i = i + 1
                }
                if new_left_count != left_count {
                    left_count = new_left_count
                    changed = true
                }
            }
            if left_count > 0 {
                let mut max_l: i64 = left_opts[0]
                let mut i: i64 = 1
                while i < left_count {
                    if left_opts[i] > max_l {
                        max_l = left_opts[i]
                    }
                    i = i + 1
                }
                let mut new_right_count: i64 = 0
                i = 0
                while i < right_count {
                    if right_opts[i] > max_l {
                        right_opts[new_right_count] = right_opts[i]
                        new_right_count = new_right_count + 1
                    }
                    i = i + 1
                }
                if new_right_count != right_count {
                    right_count = new_right_count
                    changed = true
                }
            }
        }

        let mut max_l: i64 = -1
        let mut min_r: i64 = -1
        if left_count > 0 {
            max_l = left_opts[0]
            let mut i: i64 = 1
            while i < left_count {
                if left_opts[i] > max_l {
                    max_l = left_opts[i]
                }
                i = i + 1
            }
        }
        if right_count > 0 {
            min_r = right_opts[0]
            let mut i: i64 = 1
            while i < right_count {
                if right_opts[i] < min_r {
                    min_r = right_opts[i]
                }
                i = i + 1
            }
        }

        g[x] = simplest_between(max_l, min_r, max_exp)
        total_scaled = total_scaled + x * g[x]
        x = x + 1
    }

    free(left_opts)
    free(right_opts)
    free(g)

    return (total_scaled + den - 1) / den
}

function main() -> i32 {
    printf("%lld\n", compute_S(100000))
    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 simplest_between_i64_i64_i64(int64_t a_scaled, int64_t b_scaled, int64_t max_exp);
int64_t compute_S_i64(int64_t n);
int32_t main(void);




int64_t simplest_between_i64_i64_i64(int64_t a_scaled, int64_t b_scaled, int64_t max_exp) {
    int64_t den = FLOW_CHECKED_SHL((1), (max_exp));
    if (a_scaled == (-1)) {
        if (b_scaled == (-1)) {
            return 0;
        }
        return (FLOW_CHECKED_DIV(((b_scaled - 1)), (den)) * den);
    }
    if (b_scaled == (-1)) {
        return ((FLOW_CHECKED_DIV((a_scaled), (den)) + 1) * den);
    }
    int64_t k = 0;
    while (k <= max_exp) {
        int64_t step = FLOW_CHECKED_SHL((1), ((max_exp - k)));
        int64_t m_low = (FLOW_CHECKED_DIV((a_scaled), (step)) + 1);
        int64_t m_high = FLOW_CHECKED_DIV(((b_scaled - 1)), (step));
        if (m_low <= m_high) {
            return (m_low * step);
        }
        k = (k + 1);
    }
    return 0;
}

int64_t compute_S_i64(int64_t n) {
    if (n <= 1) {
        return n;
    }
    int64_t max_exp = 0;
    int64_t tmp = n;
    while (tmp > 0) {
        max_exp = (max_exp + 1);
        tmp = FLOW_CHECKED_SHR((tmp), (1));
    }
    int64_t den = FLOW_CHECKED_SHL((1), (max_exp));
    int64_t* g = (int64_t*)(calloc((n + 1), 8));
    int64_t total_scaled = 0;
    int64_t* left_opts = (int64_t*)(malloc((64 * 8)));
    int64_t* right_opts = (int64_t*)(malloc((64 * 8)));
    int64_t x = 1;
    while (x <= n) {
        int64_t left_count = 0;
        int64_t right_count = 0;
        int64_t bits = 0;
        int64_t tmpx = x;
        while (tmpx > 0) {
            bits = (bits + 1);
            tmpx = FLOW_CHECKED_SHR((tmpx), (1));
        }
        int64_t j = 0;
        while (j < bits) {
            int64_t bit = (FLOW_CHECKED_SHR((x), (j)) & 1);
            int64_t higher = FLOW_CHECKED_SHR((x), ((j + 1)));
            int64_t lower = (x & (FLOW_CHECKED_SHL((1), (j)) - 1));
            int64_t y = (FLOW_CHECKED_SHL((higher), (j)) | lower);
            if (bit != 0) {
                left_opts[left_count] = g[y];
                left_count = (left_count + 1);
            } else {
                right_opts[right_count] = g[y];
                right_count = (right_count + 1);
            }
            j = (j + 1);
        }
        bool changed = 1;
        while (changed) {
            changed = 0;
            if (right_count > 0) {
                int64_t min_r = right_opts[0];
                int64_t i = 1;
                while (i < right_count) {
                    if (right_opts[i] < min_r) {
                        min_r = right_opts[i];
                    }
                    i = (i + 1);
                }
                int64_t new_left_count = 0;
                i = 0;
                while (i < left_count) {
                    if (left_opts[i] < min_r) {
                        left_opts[new_left_count] = left_opts[i];
                        new_left_count = (new_left_count + 1);
                    }
                    i = (i + 1);
                }
                if (new_left_count != left_count) {
                    left_count = new_left_count;
                    changed = 1;
                }
            }
            if (left_count > 0) {
                int64_t max_l = left_opts[0];
                int64_t i = 1;
                while (i < left_count) {
                    if (left_opts[i] > max_l) {
                        max_l = left_opts[i];
                    }
                    i = (i + 1);
                }
                int64_t new_right_count = 0;
                i = 0;
                while (i < right_count) {
                    if (right_opts[i] > max_l) {
                        right_opts[new_right_count] = right_opts[i];
                        new_right_count = (new_right_count + 1);
                    }
                    i = (i + 1);
                }
                if (new_right_count != right_count) {
                    right_count = new_right_count;
                    changed = 1;
                }
            }
        }
        int64_t max_l = (-1);
        int64_t min_r = (-1);
        if (left_count > 0) {
            max_l = left_opts[0];
            int64_t i = 1;
            while (i < left_count) {
                if (left_opts[i] > max_l) {
                    max_l = left_opts[i];
                }
                i = (i + 1);
            }
        }
        if (right_count > 0) {
            min_r = right_opts[0];
            int64_t i = 1;
            while (i < right_count) {
                if (right_opts[i] < min_r) {
                    min_r = right_opts[i];
                }
                i = (i + 1);
            }
        }
        g[x] = simplest_between_i64_i64_i64(max_l, min_r, max_exp);
        total_scaled = (total_scaled + (x * g[x]));
        x = (x + 1);
    }
    free(left_opts);
    free(right_opts);
    free(g);
    return FLOW_CHECKED_DIV((((total_scaled + den) - 1)), (den));
}

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