Problem 887

Bounded Binary Search — sum Q(N,d) for d=0..7, N<=7^10.

Answer39896187138661622
Output39896187138661622
StatusPASS
Native helperno
Runtime0 ms
Peak memory10464 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 887
# Bounded Binary Search — sum Q(N,d) for d=0..7, N<=7^10.

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

# Memo: key = q*16 + (d+8) since d can be small negatives; use hash
const HSIZE: i64 = 1 << 20
let mut HKEY: ptr<i64> = null
let mut HVAL: ptr<i64> = null
let mut HUSE: ptr<i8> = null

function pack(q: i64, d: i64) -> i64 {
    return (q << 32) ^ (d + 1000000000)
}

function max_n(q: i64, d: i64) -> i64 {
    if d < 0 { return 1 }
    if q == 0 { return 1 }
    if d >= q - 1 {
        return 1 << q
    }
    let key: i64 = pack(q, d)
    let mut h: i64 = (key * 11400714819323198485) & (HSIZE - 1)
    while HUSE[h] != 0 {
        if HKEY[h] == key { return HVAL[h] }
        h = (h + 1) & (HSIZE - 1)
    }
    let left: i64 = max_n(q - 1, d - 1)
    let ans: i64 = left + max_n(q - 1, left + d - 1)
    HUSE[h] = 1
    HKEY[h] = key
    HVAL[h] = ans
    return ans
}

function sum_Q_upto(limit: i64, d: i64) -> i64 {
    if limit <= 0 { return 0 }
    if d == 0 {
        return limit * (limit - 1) / 2
    }
    let mut total: i64 = 0
    let mut prev_cap: i64 = 0
    let mut q: i64 = 0
    while prev_cap < limit {
        let mut cap: i64 = max_n(q, d)
        if cap > limit { cap = limit }
        total = total + q * (cap - prev_cap)
        prev_cap = cap
        q = q + 1
    }
    return total
}

function main() -> i32 {
    HKEY = calloc(HSIZE, 8)
    HVAL = calloc(HSIZE, 8)
    HUSE = calloc(HSIZE, 1)
    let mut limit: i64 = 1
    let mut i: i64 = 0
    while i < 10 {
        limit = limit * 7
        i = i + 1
    }
    let mut ans: i64 = 0
    let mut d: i64 = 0
    while d <= 7 {
        ans = ans + sum_Q_upto(limit, d)
        d = d + 1
    }
    printf("%lld\n", ans)
    free(HKEY)
    free(HVAL)
    free(HUSE)
    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 pack_i64_i64(int64_t q, int64_t d);
int64_t max_n_i64_i64(int64_t q, int64_t d);
int64_t sum_Q_upto_i64_i64(int64_t limit, int64_t d);
int32_t main(void);

static const int64_t HSIZE = FLOW_CHECKED_SHL((1), (20));

/* Module statics */
static int64_t* HKEY = NULL;
static int64_t* HVAL = NULL;
static int8_t* HUSE = NULL;



int64_t pack_i64_i64(int64_t q, int64_t d) {
    return (FLOW_CHECKED_SHL((q), (32)) ^ (d + 1000000000));
}

int64_t max_n_i64_i64(int64_t q, int64_t d) {
    if (d < 0) {
        return 1;
    }
    if (q == 0) {
        return 1;
    }
    if (d >= (q - 1)) {
        return FLOW_CHECKED_SHL((1), (q));
    }
    int64_t key = pack_i64_i64(q, d);
    int64_t h = ((key * ((__int128)0x9E3779B97F4A7C15ULL)) & (HSIZE - 1));
    while (HUSE[h] != 0) {
        if (HKEY[h] == key) {
            return HVAL[h];
        }
        h = ((h + 1) & (HSIZE - 1));
    }
    int64_t left = max_n_i64_i64((q - 1), (d - 1));
    int64_t ans = (left + max_n_i64_i64((q - 1), ((left + d) - 1)));
    HUSE[h] = 1;
    HKEY[h] = key;
    HVAL[h] = ans;
    return ans;
}

int64_t sum_Q_upto_i64_i64(int64_t limit, int64_t d) {
    if (limit <= 0) {
        return 0;
    }
    if (d == 0) {
        return FLOW_CHECKED_DIV(((limit * (limit - 1))), (2));
    }
    int64_t total = 0;
    int64_t prev_cap = 0;
    int64_t q = 0;
    while (prev_cap < limit) {
        int64_t cap = max_n_i64_i64(q, d);
        if (cap > limit) {
            cap = limit;
        }
        total = (total + (q * (cap - prev_cap)));
        prev_cap = cap;
        q = (q + 1);
    }
    return total;
}

int32_t main(void) {
    HKEY = calloc(HSIZE, 8);
    HVAL = calloc(HSIZE, 8);
    HUSE = calloc(HSIZE, 1);
    int64_t limit = 1;
    int64_t i = 0;
    while (i < 10) {
        limit = (limit * 7);
        i = (i + 1);
    }
    int64_t ans = 0;
    int64_t d = 0;
    while (d <= 7) {
        ans = (ans + sum_Q_upto_i64_i64(limit, d));
        d = (d + 1);
    }
    printf("%lld\n", ans);
    free(HKEY);
    free(HVAL);
    free(HUSE);
    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) -> ()
  // Constant: HSIZE
  llvm.mlir.global internal constant @HSIZE(1048576 : i64) : i64
  // Module static: HKEY
  llvm.mlir.global internal @HKEY() {addr_space = 0 : i32} : !llvm.ptr {
    %0 = llvm.mlir.zero : !llvm.ptr
    llvm.return %0 : !llvm.ptr
  }
  // Module static: HVAL
  llvm.mlir.global internal @HVAL() {addr_space = 0 : i32} : !llvm.ptr {
    %1 = llvm.mlir.zero : !llvm.ptr
    llvm.return %1 : !llvm.ptr
  }
  // Module static: HUSE
  llvm.mlir.global internal @HUSE() {addr_space = 0 : i32} : !llvm.ptr {
    %2 = llvm.mlir.zero : !llvm.ptr
    llvm.return %2 : !llvm.ptr
  }
  func.func @pack(%arg0: i64, %arg1: i64) -> i64 {
    %3 = arith.constant 32 : i32
    %5 = arith.extsi %3 : i32 to i64
    %4 = arith.shli %arg0, %5 : i64
    %6 = arith.constant 1000000000 : i32
    %8 = arith.extsi %6 : i32 to i64
    %7 = arith.addi %arg1, %8 : i64
    %9 = arith.xori %4, %7 : i64
    func.return %9 : i64
  }
  func.func @max_n(%arg0: i64, %arg1: i64) -> i64 {
    %10 = arith.constant 0 : i32
    %12 = arith.extsi %10 : i32 to i64
    %11 = arith.cmpi slt, %arg1, %12 : i64
    cf.cond_br %11, ^bb0, ^bb1
    ^bb0:
      %13 = arith.constant 1 : i32
      %14 = arith.extsi %13 : i32 to i64
      func.return %14 : i64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %15 = arith.constant 0 : i32
    %17 = arith.extsi %15 : i32 to i64
    %16 = arith.cmpi eq, %arg0, %17 : i64
    cf.cond_br %16, ^bb3, ^bb4
    ^bb3:
      %18 = arith.constant 1 : i32
      %19 = arith.extsi %18 : i32 to i64
      func.return %19 : i64
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %20 = arith.constant 1 : i32
    %22 = arith.extsi %20 : i32 to i64
    %21 = arith.subi %arg0, %22 : i64
    %23 = arith.cmpi sge, %arg1, %21 : i64
    cf.cond_br %23, ^bb6, ^bb7
    ^bb6:
      %24 = arith.constant 1 : i32
      %26 = arith.extsi %24 : i32 to i64
      %25 = arith.shli %26, %arg0 : i64
      func.return %25 : i64
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %27 = func.call @pack(%arg0, %arg1) : (i64, i64) -> i64
    %28 = arith.constant 11400714815028231189 : i32
    %30 = arith.extsi %28 : i32 to i64
    %29 = arith.muli %27, %30 : i64
    %31 = llvm.mlir.addressof @HSIZE : !llvm.ptr
    %32 = llvm.load %31 : !llvm.ptr -> i64
    %33 = arith.constant 1 : i32
    %35 = arith.extsi %33 : i32 to i64
    %34 = arith.subi %32, %35 : i64
    %36 = arith.andi %29, %34 : i64
    %37 = llvm.mlir.constant(1 : i64) : i64
    %38 = llvm.alloca %37 x i64 : (i64) -> !llvm.ptr
    llvm.store %36, %38 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %40 = llvm.mlir.addressof @HUSE : !llvm.ptr
    %41 = llvm.load %40 : !llvm.ptr -> !llvm.ptr
    %42 = llvm.load %38 : !llvm.ptr -> i64
    %43 = llvm.getelementptr %41[%42] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    %39 = llvm.load %43 : !llvm.ptr -> i8
    %44 = arith.constant 0 : i32
    %46 = arith.extsi %39 : i8 to i32
    %45 = arith.cmpi ne, %46, %44 : i32
    cf.cond_br %45, ^bb10, ^bb11
    ^bb10:
      %48 = llvm.mlir.addressof @HKEY : !llvm.ptr
      %49 = llvm.load %48 : !llvm.ptr -> !llvm.ptr
      %50 = llvm.load %38 : !llvm.ptr -> i64
      %51 = llvm.getelementptr %49[%50] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %47 = llvm.load %51 : !llvm.ptr -> i64
      %52 = arith.cmpi eq, %47, %27 : i64
      cf.cond_br %52, ^bb12, ^bb13
      ^bb12:
        %54 = llvm.mlir.addressof @HVAL : !llvm.ptr
        %55 = llvm.load %54 : !llvm.ptr -> !llvm.ptr
        %56 = llvm.load %38 : !llvm.ptr -> i64
        %57 = llvm.getelementptr %55[%56] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %53 = llvm.load %57 : !llvm.ptr -> i64
        func.return %53 : i64
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %58 = llvm.load %38 : !llvm.ptr -> i64
      %59 = arith.constant 1 : i32
      %61 = arith.extsi %59 : i32 to i64
      %60 = arith.addi %58, %61 : i64
      %62 = llvm.mlir.addressof @HSIZE : !llvm.ptr
      %63 = llvm.load %62 : !llvm.ptr -> i64
      %64 = arith.constant 1 : i32
      %66 = arith.extsi %64 : i32 to i64
      %65 = arith.subi %63, %66 : i64
      %67 = arith.andi %60, %65 : i64
      llvm.store %67, %38 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %69 = arith.constant 1 : i32
    %71 = arith.extsi %69 : i32 to i64
    %70 = arith.subi %arg0, %71 : i64
    %72 = arith.constant 1 : i32
    %74 = arith.extsi %72 : i32 to i64
    %73 = arith.subi %arg1, %74 : i64
    %68 = func.call @max_n(%70, %73) : (i64, i64) -> i64
    %76 = arith.constant 1 : i32
    %78 = arith.extsi %76 : i32 to i64
    %77 = arith.subi %arg0, %78 : i64
    %79 = arith.addi %68, %arg1 : i64
    %80 = arith.constant 1 : i32
    %82 = arith.extsi %80 : i32 to i64
    %81 = arith.subi %79, %82 : i64
    %75 = func.call @max_n(%77, %81) : (i64, i64) -> i64
    %83 = arith.addi %68, %75 : i64
    %84 = arith.constant 1 : i32
    %85 = llvm.mlir.addressof @HUSE : !llvm.ptr
    %86 = llvm.load %85 : !llvm.ptr -> !llvm.ptr
    %87 = llvm.load %38 : !llvm.ptr -> i64
    %88 = arith.trunci %84 : i32 to i8
    %89 = llvm.getelementptr %86[%87] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %88, %89 : i8, !llvm.ptr
    %90 = llvm.mlir.addressof @HKEY : !llvm.ptr
    %91 = llvm.load %90 : !llvm.ptr -> !llvm.ptr
    %92 = llvm.load %38 : !llvm.ptr -> i64
    %93 = llvm.getelementptr %91[%92] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %27, %93 : i64, !llvm.ptr
    %94 = llvm.mlir.addressof @HVAL : !llvm.ptr
    %95 = llvm.load %94 : !llvm.ptr -> !llvm.ptr
    %96 = llvm.load %38 : !llvm.ptr -> i64
    %97 = llvm.getelementptr %95[%96] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %83, %97 : i64, !llvm.ptr
    func.return %83 : i64
  }
  func.func @sum_Q_upto(%arg0: i64, %arg1: i64) -> i64 {
    %98 = arith.constant 0 : i32
    %100 = arith.extsi %98 : i32 to i64
    %99 = arith.cmpi sle, %arg0, %100 : i64
    cf.cond_br %99, ^bb15, ^bb16
    ^bb15:
      %101 = arith.constant 0 : i32
      %102 = arith.extsi %101 : i32 to i64
      func.return %102 : i64
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %103 = arith.constant 0 : i32
    %105 = arith.extsi %103 : i32 to i64
    %104 = arith.cmpi eq, %arg1, %105 : i64
    cf.cond_br %104, ^bb18, ^bb19
    ^bb18:
      %106 = arith.constant 1 : i32
      %108 = arith.extsi %106 : i32 to i64
      %107 = arith.subi %arg0, %108 : i64
      %109 = arith.muli %arg0, %107 : i64
      %110 = arith.constant 2 : i32
      %112 = arith.extsi %110 : i32 to i64
      %111 = arith.divsi %109, %112 : i64
      func.return %111 : i64
    ^bb19:
      cf.br ^bb20
    ^bb20:
    %113 = arith.constant 0 : 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
    %117 = arith.constant 0 : i32
    %118 = arith.extsi %117 : i32 to i64
    %119 = llvm.mlir.constant(1 : i64) : i64
    %120 = llvm.alloca %119 x i64 : (i64) -> !llvm.ptr
    llvm.store %118, %120 : i64, !llvm.ptr
    %121 = arith.constant 0 : i32
    %122 = arith.extsi %121 : i32 to i64
    %123 = llvm.mlir.constant(1 : i64) : i64
    %124 = llvm.alloca %123 x i64 : (i64) -> !llvm.ptr
    llvm.store %122, %124 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %125 = llvm.load %120 : !llvm.ptr -> i64
    %126 = arith.cmpi slt, %125, %arg0 : i64
    cf.cond_br %126, ^bb22, ^bb23
    ^bb22:
      %128 = llvm.load %124 : !llvm.ptr -> i64
      %127 = func.call @max_n(%128, %arg1) : (i64, i64) -> i64
      %129 = llvm.mlir.constant(1 : i64) : i64
      %130 = llvm.alloca %129 x i64 : (i64) -> !llvm.ptr
      llvm.store %127, %130 : i64, !llvm.ptr
      %131 = llvm.load %130 : !llvm.ptr -> i64
      %132 = arith.cmpi sgt, %131, %arg0 : i64
      cf.cond_br %132, ^bb24, ^bb25
      ^bb24:
        llvm.store %arg0, %130 : i64, !llvm.ptr
        cf.br ^bb26
      ^bb25:
        cf.br ^bb26
      ^bb26:
      %133 = llvm.load %116 : !llvm.ptr -> i64
      %134 = llvm.load %124 : !llvm.ptr -> i64
      %135 = llvm.load %130 : !llvm.ptr -> i64
      %136 = llvm.load %120 : !llvm.ptr -> i64
      %137 = arith.subi %135, %136 : i64
      %138 = arith.muli %134, %137 : i64
      %139 = arith.addi %133, %138 : i64
      llvm.store %139, %116 : i64, !llvm.ptr
      %140 = llvm.load %130 : !llvm.ptr -> i64
      llvm.store %140, %120 : i64, !llvm.ptr
      %141 = llvm.load %124 : !llvm.ptr -> i64
      %142 = arith.constant 1 : i32
      %144 = arith.extsi %142 : i32 to i64
      %143 = arith.addi %141, %144 : i64
      llvm.store %143, %124 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %145 = llvm.load %116 : !llvm.ptr -> i64
    func.return %145 : i64
  }
  func.func @main() -> i32 {
    %147 = llvm.mlir.addressof @HSIZE : !llvm.ptr
    %148 = llvm.load %147 : !llvm.ptr -> i64
    %149 = arith.constant 8 : i32
    %150 = arith.extsi %149 : i32 to i64
    %146 = func.call @calloc(%148, %150) : (i64, i64) -> !llvm.ptr
    %151 = llvm.mlir.addressof @HKEY : !llvm.ptr
    llvm.store %146, %151 : !llvm.ptr, !llvm.ptr
    %153 = llvm.mlir.addressof @HSIZE : !llvm.ptr
    %154 = llvm.load %153 : !llvm.ptr -> i64
    %155 = arith.constant 8 : i32
    %156 = arith.extsi %155 : i32 to i64
    %152 = func.call @calloc(%154, %156) : (i64, i64) -> !llvm.ptr
    %157 = llvm.mlir.addressof @HVAL : !llvm.ptr
    llvm.store %152, %157 : !llvm.ptr, !llvm.ptr
    %159 = llvm.mlir.addressof @HSIZE : !llvm.ptr
    %160 = llvm.load %159 : !llvm.ptr -> i64
    %161 = arith.constant 1 : i32
    %162 = arith.extsi %161 : i32 to i64
    %158 = func.call @calloc(%160, %162) : (i64, i64) -> !llvm.ptr
    %163 = llvm.mlir.addressof @HUSE : !llvm.ptr
    llvm.store %158, %163 : !llvm.ptr, !llvm.ptr
    %164 = arith.constant 1 : i32
    %165 = arith.extsi %164 : i32 to i64
    %166 = llvm.mlir.constant(1 : i64) : i64
    %167 = llvm.alloca %166 x i64 : (i64) -> !llvm.ptr
    llvm.store %165, %167 : i64, !llvm.ptr
    %168 = arith.constant 0 : i32
    %169 = arith.extsi %168 : i32 to i64
    %170 = llvm.mlir.constant(1 : i64) : i64
    %171 = llvm.alloca %170 x i64 : (i64) -> !llvm.ptr
    llvm.store %169, %171 : i64, !llvm.ptr
    cf.br ^bb27
    ^bb27:
    %172 = llvm.load %171 : !llvm.ptr -> i64
    %173 = arith.constant 10 : i32
    %175 = arith.extsi %173 : i32 to i64
    %174 = arith.cmpi slt, %172, %175 : i64
    cf.cond_br %174, ^bb28, ^bb29
    ^bb28:
      %176 = llvm.load %167 : !llvm.ptr -> i64
      %177 = arith.constant 7 : i32
      %179 = arith.extsi %177 : i32 to i64
      %178 = arith.muli %176, %179 : i64
      llvm.store %178, %167 : i64, !llvm.ptr
      %180 = llvm.load %171 : !llvm.ptr -> i64
      %181 = arith.constant 1 : i32
      %183 = arith.extsi %181 : i32 to i64
      %182 = arith.addi %180, %183 : i64
      llvm.store %182, %171 : i64, !llvm.ptr
      cf.br ^bb27
    ^bb29:
    %184 = arith.constant 0 : i32
    %185 = arith.extsi %184 : i32 to i64
    %186 = llvm.mlir.constant(1 : i64) : i64
    %187 = llvm.alloca %186 x i64 : (i64) -> !llvm.ptr
    llvm.store %185, %187 : i64, !llvm.ptr
    %188 = arith.constant 0 : i32
    %189 = arith.extsi %188 : i32 to i64
    %190 = llvm.mlir.constant(1 : i64) : i64
    %191 = llvm.alloca %190 x i64 : (i64) -> !llvm.ptr
    llvm.store %189, %191 : i64, !llvm.ptr
    cf.br ^bb30
    ^bb30:
    %192 = llvm.load %191 : !llvm.ptr -> i64
    %193 = arith.constant 7 : i32
    %195 = arith.extsi %193 : i32 to i64
    %194 = arith.cmpi sle, %192, %195 : i64
    cf.cond_br %194, ^bb31, ^bb32
    ^bb31:
      %196 = llvm.load %187 : !llvm.ptr -> i64
      %198 = llvm.load %167 : !llvm.ptr -> i64
      %199 = llvm.load %191 : !llvm.ptr -> i64
      %197 = func.call @sum_Q_upto(%198, %199) : (i64, i64) -> i64
      %200 = arith.addi %196, %197 : i64
      llvm.store %200, %187 : i64, !llvm.ptr
      %201 = llvm.load %191 : !llvm.ptr -> i64
      %202 = arith.constant 1 : i32
      %204 = arith.extsi %202 : i32 to i64
      %203 = arith.addi %201, %204 : i64
      llvm.store %203, %191 : i64, !llvm.ptr
      cf.br ^bb30
    ^bb32:
    %205 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %206 = llvm.load %187 : !llvm.ptr -> i64
    %207 = llvm.call @printf(%205, %206) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %209 = llvm.mlir.addressof @HKEY : !llvm.ptr
    %210 = llvm.load %209 : !llvm.ptr -> !llvm.ptr
    func.call @free(%210) : (!llvm.ptr) -> ()
    %212 = llvm.mlir.addressof @HVAL : !llvm.ptr
    %213 = llvm.load %212 : !llvm.ptr -> !llvm.ptr
    func.call @free(%213) : (!llvm.ptr) -> ()
    %215 = llvm.mlir.addressof @HUSE : !llvm.ptr
    %216 = llvm.load %215 : !llvm.ptr -> !llvm.ptr
    func.call @free(%216) : (!llvm.ptr) -> ()
    %217 = arith.constant 0 : i32
    func.return %217 : i32
  }
}