Problem 237

Tours on 4xn grid; T(10^12) mod 1e8.

Answer15836928
Output15836928
StatusPASS
Native helperno
Runtime10 ms
Peak memory2736 KB
Time complexityO(n) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(log n)
Space complexityO(n^2)O(n^2)
ApproachFlow solutionMatrix exponentiation
VerdictSuboptimal

Flow source

# Project Euler 237
# Tours on 4xn grid; T(10^12) mod 1e8.

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

# Encode patterns as small ids 0..7
# 0:1234 1:1432 2:3214 3:1##2 4:12## 5:##12 6:#12# 7:####
const LEFT_BORDER: i32 = 3
const RIGHT_BORDER: i32 = 7
const MOD: i64 = 100000000

let mut NEI: ptr<i8> = null  # 8x8
let mut IS_BORDER: ptr<i8> = null
let mut CACHE: ptr<i64> = null
let mut CACHE_KEY: ptr<i64> = null
let mut CACHE_N: i64 = 0
const CACHE_CAP: i64 = 200000

function key3(left: i32, right: i32, length: i64) -> i64 {
    return ((left as i64) << 56) + ((right as i64) << 48) + length
}

function cache_get(k: i64) -> i64 {
    let mut i: i64 = 0
    while i < CACHE_N {
        if CACHE_KEY[i] == k { return CACHE[i] }
        i = i + 1
    }
    return 0 - 1
}

function cache_put(k: i64, v: i64) -> void {
    if CACHE_N < CACHE_CAP {
        CACHE_KEY[CACHE_N] = k
        CACHE[CACHE_N] = v
        CACHE_N = CACHE_N + 1
    }
}

function search(left: i32, right: i32, length: i64) -> i64 {
    if length == 1 {
        if NEI[(left as i64) * 8 + (right as i64)] == 1 { return 1 }
        return 0
    }
    let k: i64 = key3(left, right, length)
    let hit: i64 = cache_get(k)
    if hit >= 0 { return hit }
    let mut result: i64 = 0
    let mut nxt: i32 = 0
    while nxt < 8 {
        if IS_BORDER[nxt] == 1 {
            let mut pow2: i64 = 1
            while pow2 < length / 2 {
                pow2 = pow2 * 2
            }
            let left_half: i64 = search(left, nxt, pow2)
            let right_half: i64 = search(nxt, right, length - pow2)
            result = (result + (left_half * right_half) % MOD) % MOD
        }
        nxt = nxt + 1
    }
    cache_put(k, result)
    return result
}

function add_nei(a: i32, b: i32) -> void {
    NEI[(a as i64) * 8 + (b as i64)] = 1
    IS_BORDER[a] = 1
}

function main() -> i32 {
    NEI = calloc(64, 1)
    IS_BORDER = calloc(8, 1)
    CACHE = calloc(CACHE_CAP, 8)
    CACHE_KEY = calloc(CACHE_CAP, 8)
    if NEI == null || CACHE == null { return 1 }
    # neighbors from python list
    add_nei(0,0); add_nei(1,1); add_nei(2,2)
    add_nei(1,3); add_nei(2,3); add_nei(3,0)
    add_nei(0,4); add_nei(0,5)
    add_nei(4,1); add_nei(5,2)
    add_nei(3,6); add_nei(6,3)
    add_nei(4,3); add_nei(3,5); add_nei(3,4); add_nei(5,3)
    add_nei(0,7); add_nei(3,7)
    printf("%lld\n", search(LEFT_BORDER, RIGHT_BORDER, 1000000000000))
    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 key3_i32_i32_i64(int32_t left, int32_t right, int64_t length);
int64_t cache_get_i64(int64_t k);
void cache_put_i64_i64(int64_t k, int64_t v);
int64_t search_i32_i32_i64(int32_t left, int32_t right, int64_t length);
void add_nei_i32_i32(int32_t a, int32_t b);
int32_t main(void);

static const int32_t LEFT_BORDER = 3;
static const int32_t RIGHT_BORDER = 7;
static const int64_t MOD = 100000000;
static const int64_t CACHE_CAP = 200000;

/* Module statics */
static int8_t* NEI = NULL;
static int8_t* IS_BORDER = NULL;
static int64_t* CACHE = NULL;
static int64_t* CACHE_KEY = NULL;
static int64_t CACHE_N = 0;



int64_t key3_i32_i32_i64(int32_t left, int32_t right, int64_t length) {
    return ((FLOW_CHECKED_SHL((((int64_t)(left))), (56)) + FLOW_CHECKED_SHL((((int64_t)(right))), (48))) + length);
}

int64_t cache_get_i64(int64_t k) {
    int64_t i = 0;
    while (i < CACHE_N) {
        if (CACHE_KEY[i] == k) {
            return CACHE[i];
        }
        i = (i + 1);
    }
    return (0 - 1);
}

void cache_put_i64_i64(int64_t k, int64_t v) {
    if (CACHE_N < CACHE_CAP) {
        CACHE_KEY[CACHE_N] = k;
        CACHE[CACHE_N] = v;
        CACHE_N = (CACHE_N + 1);
    }
}

int64_t search_i32_i32_i64(int32_t left, int32_t right, int64_t length) {
    if (length == 1) {
        if (NEI[((((int64_t)(left)) * 8) + ((int64_t)(right)))] == 1) {
            return 1;
        }
        return 0;
    }
    int64_t k = key3_i32_i32_i64(left, right, length);
    int64_t hit = cache_get_i64(k);
    if (hit >= 0) {
        return hit;
    }
    int64_t result = 0;
    int32_t nxt = 0;
    while (nxt < 8) {
        if (IS_BORDER[nxt] == 1) {
            int64_t pow2 = 1;
            while (pow2 < FLOW_CHECKED_DIV((length), (2))) {
                pow2 = (pow2 * 2);
            }
            int64_t left_half = search_i32_i32_i64(left, nxt, pow2);
            int64_t right_half = search_i32_i32_i64(nxt, right, (length - pow2));
            result = FLOW_CHECKED_MOD(((result + FLOW_CHECKED_MOD(((left_half * right_half)), (MOD)))), (MOD));
        }
        nxt = (nxt + 1);
    }
    cache_put_i64_i64(k, result);
    return result;
}

void add_nei_i32_i32(int32_t a, int32_t b) {
    NEI[((((int64_t)(a)) * 8) + ((int64_t)(b)))] = 1;
    IS_BORDER[a] = 1;
}

int32_t main(void) {
    NEI = calloc(64, 1);
    IS_BORDER = calloc(8, 1);
    CACHE = calloc(CACHE_CAP, 8);
    CACHE_KEY = calloc(CACHE_CAP, 8);
    if ((NEI == NULL || CACHE == NULL)) {
        return 1;
    }
    add_nei_i32_i32(0, 0);
    add_nei_i32_i32(1, 1);
    add_nei_i32_i32(2, 2);
    add_nei_i32_i32(1, 3);
    add_nei_i32_i32(2, 3);
    add_nei_i32_i32(3, 0);
    add_nei_i32_i32(0, 4);
    add_nei_i32_i32(0, 5);
    add_nei_i32_i32(4, 1);
    add_nei_i32_i32(5, 2);
    add_nei_i32_i32(3, 6);
    add_nei_i32_i32(6, 3);
    add_nei_i32_i32(4, 3);
    add_nei_i32_i32(3, 5);
    add_nei_i32_i32(3, 4);
    add_nei_i32_i32(5, 3);
    add_nei_i32_i32(0, 7);
    add_nei_i32_i32(3, 7);
    printf("%lld\n", search_i32_i32_i64(LEFT_BORDER, RIGHT_BORDER, 1000000000000));
    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: LEFT_BORDER
  llvm.mlir.global internal constant @LEFT_BORDER(3 : i32) : i32
  // Constant: RIGHT_BORDER
  llvm.mlir.global internal constant @RIGHT_BORDER(7 : i32) : i32
  // Constant: MOD
  llvm.mlir.global internal constant @MOD(100000000 : i64) : i64
  // Module static: NEI
  llvm.mlir.global internal @NEI() {addr_space = 0 : i32} : !llvm.ptr {
    %0 = llvm.mlir.zero : !llvm.ptr
    llvm.return %0 : !llvm.ptr
  }
  // Module static: IS_BORDER
  llvm.mlir.global internal @IS_BORDER() {addr_space = 0 : i32} : !llvm.ptr {
    %1 = llvm.mlir.zero : !llvm.ptr
    llvm.return %1 : !llvm.ptr
  }
  // Module static: CACHE
  llvm.mlir.global internal @CACHE() {addr_space = 0 : i32} : !llvm.ptr {
    %2 = llvm.mlir.zero : !llvm.ptr
    llvm.return %2 : !llvm.ptr
  }
  // Module static: CACHE_KEY
  llvm.mlir.global internal @CACHE_KEY() {addr_space = 0 : i32} : !llvm.ptr {
    %3 = llvm.mlir.zero : !llvm.ptr
    llvm.return %3 : !llvm.ptr
  }
  // Module static: CACHE_N
  llvm.mlir.global internal @CACHE_N(0 : i64) : i64
  // Constant: CACHE_CAP
  llvm.mlir.global internal constant @CACHE_CAP(200000 : i64) : i64
  func.func @key3(%arg0: i32, %arg1: i32, %arg2: i64) -> i64 {
    %4 = arith.extsi %arg0 : i32 to i64
    %5 = arith.constant 56 : i32
    %7 = arith.extsi %5 : i32 to i64
    %6 = arith.shli %4, %7 : i64
    %8 = arith.extsi %arg1 : i32 to i64
    %9 = arith.constant 48 : i32
    %11 = arith.extsi %9 : i32 to i64
    %10 = arith.shli %8, %11 : i64
    %12 = arith.addi %6, %10 : i64
    %13 = arith.addi %12, %arg2 : i64
    func.return %13 : i64
  }
  func.func @cache_get(%arg0: i64) -> i64 {
    %14 = arith.constant 0 : i32
    %15 = arith.extsi %14 : i32 to i64
    %16 = llvm.mlir.constant(1 : i64) : i64
    %17 = llvm.alloca %16 x i64 : (i64) -> !llvm.ptr
    llvm.store %15, %17 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %18 = llvm.load %17 : !llvm.ptr -> i64
    %19 = llvm.mlir.addressof @CACHE_N : !llvm.ptr
    %20 = llvm.load %19 : !llvm.ptr -> i64
    %21 = arith.cmpi slt, %18, %20 : i64
    cf.cond_br %21, ^bb1, ^bb2
    ^bb1:
      %23 = llvm.mlir.addressof @CACHE_KEY : !llvm.ptr
      %24 = llvm.load %23 : !llvm.ptr -> !llvm.ptr
      %25 = llvm.load %17 : !llvm.ptr -> i64
      %26 = llvm.getelementptr %24[%25] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %22 = llvm.load %26 : !llvm.ptr -> i64
      %27 = arith.cmpi eq, %22, %arg0 : i64
      cf.cond_br %27, ^bb3, ^bb4
      ^bb3:
        %29 = llvm.mlir.addressof @CACHE : !llvm.ptr
        %30 = llvm.load %29 : !llvm.ptr -> !llvm.ptr
        %31 = llvm.load %17 : !llvm.ptr -> i64
        %32 = llvm.getelementptr %30[%31] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %28 = llvm.load %32 : !llvm.ptr -> i64
        func.return %28 : i64
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %33 = llvm.load %17 : !llvm.ptr -> i64
      %34 = arith.constant 1 : i32
      %36 = arith.extsi %34 : i32 to i64
      %35 = arith.addi %33, %36 : i64
      llvm.store %35, %17 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %37 = arith.constant 0 : i32
    %38 = arith.constant 1 : i32
    %39 = arith.subi %37, %38 : i32
    %40 = arith.extsi %39 : i32 to i64
    func.return %40 : i64
  }
  func.func @cache_put(%arg0: i64, %arg1: i64) -> () {
    %41 = llvm.mlir.addressof @CACHE_N : !llvm.ptr
    %42 = llvm.load %41 : !llvm.ptr -> i64
    %43 = llvm.mlir.addressof @CACHE_CAP : !llvm.ptr
    %44 = llvm.load %43 : !llvm.ptr -> i64
    %45 = arith.cmpi slt, %42, %44 : i64
    cf.cond_br %45, ^bb6, ^bb7
    ^bb6:
      %46 = llvm.mlir.addressof @CACHE_KEY : !llvm.ptr
      %47 = llvm.load %46 : !llvm.ptr -> !llvm.ptr
      %48 = llvm.mlir.addressof @CACHE_N : !llvm.ptr
      %49 = llvm.load %48 : !llvm.ptr -> i64
      %50 = llvm.getelementptr %47[%49] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %arg0, %50 : i64, !llvm.ptr
      %51 = llvm.mlir.addressof @CACHE : !llvm.ptr
      %52 = llvm.load %51 : !llvm.ptr -> !llvm.ptr
      %53 = llvm.mlir.addressof @CACHE_N : !llvm.ptr
      %54 = llvm.load %53 : !llvm.ptr -> i64
      %55 = llvm.getelementptr %52[%54] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %arg1, %55 : i64, !llvm.ptr
      %56 = llvm.mlir.addressof @CACHE_N : !llvm.ptr
      %57 = llvm.load %56 : !llvm.ptr -> i64
      %58 = arith.constant 1 : i32
      %60 = arith.extsi %58 : i32 to i64
      %59 = arith.addi %57, %60 : i64
      %61 = llvm.mlir.addressof @CACHE_N : !llvm.ptr
      llvm.store %59, %61 : i64, !llvm.ptr
      cf.br ^bb8
    ^bb7:
      cf.br ^bb8
    ^bb8:
    func.return
  }
  func.func @search(%arg0: i32, %arg1: i32, %arg2: i64) -> i64 {
    %62 = arith.constant 1 : i32
    %64 = arith.extsi %62 : i32 to i64
    %63 = arith.cmpi eq, %arg2, %64 : i64
    cf.cond_br %63, ^bb9, ^bb10
    ^bb9:
      %66 = llvm.mlir.addressof @NEI : !llvm.ptr
      %67 = llvm.load %66 : !llvm.ptr -> !llvm.ptr
      %68 = arith.extsi %arg0 : i32 to i64
      %69 = arith.constant 8 : i32
      %71 = arith.extsi %69 : i32 to i64
      %70 = arith.muli %68, %71 : i64
      %72 = arith.extsi %arg1 : i32 to i64
      %73 = arith.addi %70, %72 : i64
      %74 = llvm.getelementptr %67[%73] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %65 = llvm.load %74 : !llvm.ptr -> i8
      %75 = arith.constant 1 : i32
      %77 = arith.extsi %65 : i8 to i32
      %76 = arith.cmpi eq, %77, %75 : i32
      cf.cond_br %76, ^bb12, ^bb13
      ^bb12:
        %78 = arith.constant 1 : i32
        %79 = arith.extsi %78 : i32 to i64
        func.return %79 : i64
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %80 = arith.constant 0 : i32
      %81 = arith.extsi %80 : i32 to i64
      func.return %81 : i64
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %82 = func.call @key3(%arg0, %arg1, %arg2) : (i32, i32, i64) -> i64
    %83 = func.call @cache_get(%82) : (i64) -> i64
    %84 = arith.constant 0 : i32
    %86 = arith.extsi %84 : i32 to i64
    %85 = arith.cmpi sge, %83, %86 : i64
    cf.cond_br %85, ^bb15, ^bb16
    ^bb15:
      func.return %83 : i64
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %87 = arith.constant 0 : i32
    %88 = arith.extsi %87 : i32 to i64
    %89 = llvm.mlir.constant(1 : i64) : i64
    %90 = llvm.alloca %89 x i64 : (i64) -> !llvm.ptr
    llvm.store %88, %90 : i64, !llvm.ptr
    %91 = arith.constant 0 : i32
    %92 = llvm.mlir.constant(1 : i64) : i64
    %93 = llvm.alloca %92 x i32 : (i64) -> !llvm.ptr
    llvm.store %91, %93 : i32, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %94 = llvm.load %93 : !llvm.ptr -> i32
    %95 = arith.constant 8 : i32
    %96 = arith.cmpi slt, %94, %95 : i32
    cf.cond_br %96, ^bb19, ^bb20
    ^bb19:
      %98 = llvm.mlir.addressof @IS_BORDER : !llvm.ptr
      %99 = llvm.load %98 : !llvm.ptr -> !llvm.ptr
      %100 = llvm.load %93 : !llvm.ptr -> i32
      %101 = arith.extsi %100 : i32 to i64
      %102 = llvm.getelementptr %99[%101] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %97 = llvm.load %102 : !llvm.ptr -> i8
      %103 = arith.constant 1 : i32
      %105 = arith.extsi %97 : i8 to i32
      %104 = arith.cmpi eq, %105, %103 : i32
      cf.cond_br %104, ^bb21, ^bb22
      ^bb21:
        %106 = arith.constant 1 : i32
        %107 = arith.extsi %106 : i32 to i64
        %108 = llvm.mlir.constant(1 : i64) : i64
        %109 = llvm.alloca %108 x i64 : (i64) -> !llvm.ptr
        llvm.store %107, %109 : i64, !llvm.ptr
        cf.br ^bb24
        ^bb24:
        %110 = llvm.load %109 : !llvm.ptr -> i64
        %111 = arith.constant 2 : i32
        %113 = arith.extsi %111 : i32 to i64
        %112 = arith.divsi %arg2, %113 : i64
        %114 = arith.cmpi slt, %110, %112 : i64
        cf.cond_br %114, ^bb25, ^bb26
        ^bb25:
          %115 = llvm.load %109 : !llvm.ptr -> i64
          %116 = arith.constant 2 : i32
          %118 = arith.extsi %116 : i32 to i64
          %117 = arith.muli %115, %118 : i64
          llvm.store %117, %109 : i64, !llvm.ptr
          cf.br ^bb24
        ^bb26:
        %120 = llvm.load %93 : !llvm.ptr -> i32
        %121 = llvm.load %109 : !llvm.ptr -> i64
        %119 = func.call @search(%arg0, %120, %121) : (i32, i32, i64) -> i64
        %123 = llvm.load %93 : !llvm.ptr -> i32
        %124 = llvm.load %109 : !llvm.ptr -> i64
        %125 = arith.subi %arg2, %124 : i64
        %122 = func.call @search(%123, %arg1, %125) : (i32, i32, i64) -> i64
        %126 = llvm.load %90 : !llvm.ptr -> i64
        %127 = arith.muli %119, %122 : i64
        %128 = llvm.mlir.addressof @MOD : !llvm.ptr
        %129 = llvm.load %128 : !llvm.ptr -> i64
        %130 = arith.remsi %127, %129 : i64
        %131 = arith.addi %126, %130 : i64
        %132 = llvm.mlir.addressof @MOD : !llvm.ptr
        %133 = llvm.load %132 : !llvm.ptr -> i64
        %134 = arith.remsi %131, %133 : i64
        llvm.store %134, %90 : i64, !llvm.ptr
        cf.br ^bb23
      ^bb22:
        cf.br ^bb23
      ^bb23:
      %135 = llvm.load %93 : !llvm.ptr -> i32
      %136 = arith.constant 1 : i32
      %137 = arith.addi %135, %136 : i32
      llvm.store %137, %93 : i32, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %139 = llvm.load %90 : !llvm.ptr -> i64
    func.call @cache_put(%82, %139) : (i64, i64) -> ()
    %140 = llvm.load %90 : !llvm.ptr -> i64
    func.return %140 : i64
  }
  func.func @add_nei(%arg0: i32, %arg1: i32) -> () {
    %141 = arith.constant 1 : i32
    %142 = llvm.mlir.addressof @NEI : !llvm.ptr
    %143 = llvm.load %142 : !llvm.ptr -> !llvm.ptr
    %144 = arith.extsi %arg0 : i32 to i64
    %145 = arith.constant 8 : i32
    %147 = arith.extsi %145 : i32 to i64
    %146 = arith.muli %144, %147 : i64
    %148 = arith.extsi %arg1 : i32 to i64
    %149 = arith.addi %146, %148 : i64
    %150 = arith.trunci %141 : i32 to i8
    %151 = llvm.getelementptr %143[%149] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %150, %151 : i8, !llvm.ptr
    %152 = arith.constant 1 : i32
    %153 = llvm.mlir.addressof @IS_BORDER : !llvm.ptr
    %154 = llvm.load %153 : !llvm.ptr -> !llvm.ptr
    %155 = arith.trunci %152 : i32 to i8
    %156 = arith.extsi %arg0 : i32 to i64
    %157 = llvm.getelementptr %154[%156] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %155, %157 : i8, !llvm.ptr
    func.return
  }
  func.func @main() -> i32 {
    %159 = arith.constant 64 : i32
    %160 = arith.constant 1 : i32
    %161 = arith.extsi %159 : i32 to i64
    %162 = arith.extsi %160 : i32 to i64
    %158 = func.call @calloc(%161, %162) : (i64, i64) -> !llvm.ptr
    %163 = llvm.mlir.addressof @NEI : !llvm.ptr
    llvm.store %158, %163 : !llvm.ptr, !llvm.ptr
    %165 = arith.constant 8 : i32
    %166 = arith.constant 1 : i32
    %167 = arith.extsi %165 : i32 to i64
    %168 = arith.extsi %166 : i32 to i64
    %164 = func.call @calloc(%167, %168) : (i64, i64) -> !llvm.ptr
    %169 = llvm.mlir.addressof @IS_BORDER : !llvm.ptr
    llvm.store %164, %169 : !llvm.ptr, !llvm.ptr
    %171 = llvm.mlir.addressof @CACHE_CAP : !llvm.ptr
    %172 = llvm.load %171 : !llvm.ptr -> i64
    %173 = arith.constant 8 : i32
    %174 = arith.extsi %173 : i32 to i64
    %170 = func.call @calloc(%172, %174) : (i64, i64) -> !llvm.ptr
    %175 = llvm.mlir.addressof @CACHE : !llvm.ptr
    llvm.store %170, %175 : !llvm.ptr, !llvm.ptr
    %177 = llvm.mlir.addressof @CACHE_CAP : !llvm.ptr
    %178 = llvm.load %177 : !llvm.ptr -> i64
    %179 = arith.constant 8 : i32
    %180 = arith.extsi %179 : i32 to i64
    %176 = func.call @calloc(%178, %180) : (i64, i64) -> !llvm.ptr
    %181 = llvm.mlir.addressof @CACHE_KEY : !llvm.ptr
    llvm.store %176, %181 : !llvm.ptr, !llvm.ptr
    %182 = llvm.mlir.addressof @NEI : !llvm.ptr
    %183 = llvm.load %182 : !llvm.ptr -> !llvm.ptr
    %184 = llvm.mlir.zero : !llvm.ptr
    %185 = llvm.icmp "eq" %183, %184 : !llvm.ptr
    %186 = scf.if %185 -> (i1) {
      %187 = arith.constant true
      scf.yield %187 : i1
    } else {
      %188 = llvm.mlir.addressof @CACHE : !llvm.ptr
      %189 = llvm.load %188 : !llvm.ptr -> !llvm.ptr
      %190 = llvm.mlir.zero : !llvm.ptr
      %191 = llvm.icmp "eq" %189, %190 : !llvm.ptr
      scf.yield %191 : i1
    }
    cf.cond_br %186, ^bb27, ^bb28
    ^bb27:
      %192 = arith.constant 1 : i32
      func.return %192 : i32
    ^bb28:
      cf.br ^bb29
    ^bb29:
    %194 = arith.constant 0 : i32
    %195 = arith.constant 0 : i32
    func.call @add_nei(%194, %195) : (i32, i32) -> ()
    %197 = arith.constant 1 : i32
    %198 = arith.constant 1 : i32
    func.call @add_nei(%197, %198) : (i32, i32) -> ()
    %200 = arith.constant 2 : i32
    %201 = arith.constant 2 : i32
    func.call @add_nei(%200, %201) : (i32, i32) -> ()
    %203 = arith.constant 1 : i32
    %204 = arith.constant 3 : i32
    func.call @add_nei(%203, %204) : (i32, i32) -> ()
    %206 = arith.constant 2 : i32
    %207 = arith.constant 3 : i32
    func.call @add_nei(%206, %207) : (i32, i32) -> ()
    %209 = arith.constant 3 : i32
    %210 = arith.constant 0 : i32
    func.call @add_nei(%209, %210) : (i32, i32) -> ()
    %212 = arith.constant 0 : i32
    %213 = arith.constant 4 : i32
    func.call @add_nei(%212, %213) : (i32, i32) -> ()
    %215 = arith.constant 0 : i32
    %216 = arith.constant 5 : i32
    func.call @add_nei(%215, %216) : (i32, i32) -> ()
    %218 = arith.constant 4 : i32
    %219 = arith.constant 1 : i32
    func.call @add_nei(%218, %219) : (i32, i32) -> ()
    %221 = arith.constant 5 : i32
    %222 = arith.constant 2 : i32
    func.call @add_nei(%221, %222) : (i32, i32) -> ()
    %224 = arith.constant 3 : i32
    %225 = arith.constant 6 : i32
    func.call @add_nei(%224, %225) : (i32, i32) -> ()
    %227 = arith.constant 6 : i32
    %228 = arith.constant 3 : i32
    func.call @add_nei(%227, %228) : (i32, i32) -> ()
    %230 = arith.constant 4 : i32
    %231 = arith.constant 3 : i32
    func.call @add_nei(%230, %231) : (i32, i32) -> ()
    %233 = arith.constant 3 : i32
    %234 = arith.constant 5 : i32
    func.call @add_nei(%233, %234) : (i32, i32) -> ()
    %236 = arith.constant 3 : i32
    %237 = arith.constant 4 : i32
    func.call @add_nei(%236, %237) : (i32, i32) -> ()
    %239 = arith.constant 5 : i32
    %240 = arith.constant 3 : i32
    func.call @add_nei(%239, %240) : (i32, i32) -> ()
    %242 = arith.constant 0 : i32
    %243 = arith.constant 7 : i32
    func.call @add_nei(%242, %243) : (i32, i32) -> ()
    %245 = arith.constant 3 : i32
    %246 = arith.constant 7 : i32
    func.call @add_nei(%245, %246) : (i32, i32) -> ()
    %247 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %249 = llvm.mlir.addressof @LEFT_BORDER : !llvm.ptr
    %250 = llvm.load %249 : !llvm.ptr -> i32
    %251 = llvm.mlir.addressof @RIGHT_BORDER : !llvm.ptr
    %252 = llvm.load %251 : !llvm.ptr -> i32
    %253 = arith.constant 995705032704 : i32
    %254 = arith.extsi %253 : i32 to i64
    %248 = func.call @search(%250, %252, %254) : (i32, i32, i64) -> i64
    %255 = llvm.call @printf(%247, %248) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %256 = arith.constant 0 : i32
    func.return %256 : i32
  }
}