Problem 061

Sum of the only ordered set of six cyclic 4-digit figurate numbers.

Answer28684
Output28684
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n^2)
Space complexityO(n^2)O(n)
ApproachFlow solutionFigurate number enumeration
VerdictOptimal

Flow source

# Project Euler 061
# Sum of the only ordered set of six cyclic 4-digit figurate numbers.

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

function poly(t: i32, n: i64) -> i64 {
    if t == 3 { return n * (n + 1) / 2 }
    if t == 4 { return n * n }
    if t == 5 { return n * (3 * n - 1) / 2 }
    if t == 6 { return n * (2 * n - 1) }
    if t == 7 { return n * (5 * n - 3) / 2 }
    return n * (3 * n - 2)
}

function search(depth: i32, nums: ptr<i32>, counts: ptr<i32>, used: ptr<i8>, chain: ptr<i32>, answer: ptr<i64>) -> bool {
    if depth == 6 {
        if chain[5] % 100 == chain[0] / 100 {
            let mut s: i64 = 0
            let mut i: i32 = 0
            while i < 6 {
                s = s + (chain[i] as i64)
                i = i + 1
            }
            answer[0] = s
            return true
        }
        return false
    }
    let mut ti: i32 = 0
    while ti < 6 {
        if used[ti] == 0 {
            used[ti] = 1
            let mut k: i32 = 0
            while k < counts[ti] {
                let v: i32 = nums[ti * 200 + k]
                if depth == 0 || v / 100 == chain[depth - 1] % 100 {
                    chain[depth] = v
                    if search(depth + 1, nums, counts, used, chain, answer) {
                        return true
                    }
                }
                k = k + 1
            }
            used[ti] = 0
        }
        ti = ti + 1
    }
    return false
}

function main() -> i32 {
    let nums: ptr<i32> = calloc(6 * 200, 4)
    let counts: ptr<i32> = calloc(6, 4)
    let used: ptr<i8> = calloc(6, 1)
    let chain: ptr<i32> = calloc(6, 4)
    let answer: ptr<i64> = calloc(1, 8)
    if nums == null || counts == null || used == null || chain == null || answer == null {
        return 1
    }

    let mut t: i32 = 3
    while t <= 8 {
        let ti: i32 = t - 3
        let mut n: i64 = 1
        while true {
            let v: i64 = poly(t, n)
            if v >= 10000 { break }
            if v >= 1000 {
                nums[ti * 200 + counts[ti]] = v as i32
                counts[ti] = counts[ti] + 1
            }
            n = n + 1
        }
        t = t + 1
    }

    search(0, nums, counts, used, chain, answer)
    printf("%lld\n", answer[0])
    free(answer)
    free(chain)
    free(used)
    free(counts)
    free(nums)
    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 poly_i32_i64(int32_t t, int64_t n);
bool search_i32_ptr_i32_ptr_i32_ptr_i8_ptr_i32_ptr_i64(int32_t depth, int32_t* nums, int32_t* counts, int8_t* used, int32_t* chain, int64_t* answer);
int32_t main(void);



int64_t poly_i32_i64(int32_t t, int64_t n) {
    if (t == 3) {
        return FLOW_CHECKED_DIV(((n * (n + 1))), (2));
    }
    if (t == 4) {
        return (n * n);
    }
    if (t == 5) {
        return FLOW_CHECKED_DIV(((n * ((3 * n) - 1))), (2));
    }
    if (t == 6) {
        return (n * ((2 * n) - 1));
    }
    if (t == 7) {
        return FLOW_CHECKED_DIV(((n * ((5 * n) - 3))), (2));
    }
    return (n * ((3 * n) - 2));
}

bool search_i32_ptr_i32_ptr_i32_ptr_i8_ptr_i32_ptr_i64(int32_t depth, int32_t* nums, int32_t* counts, int8_t* used, int32_t* chain, int64_t* answer) {
    if (depth == 6) {
        if (FLOW_CHECKED_MOD((chain[5]), (100)) == FLOW_CHECKED_DIV((chain[0]), (100))) {
            int64_t s = 0;
            int32_t i = 0;
            while (i < 6) {
                s = (s + ((int64_t)(chain[i])));
                i = (i + 1);
            }
            answer[0] = s;
            return 1;
        }
        return 0;
    }
    int32_t ti = 0;
    while (ti < 6) {
        if (used[ti] == 0) {
            used[ti] = 1;
            int32_t k = 0;
            while (k < counts[ti]) {
                int32_t v = nums[((ti * 200) + k)];
                if ((depth == 0 || FLOW_CHECKED_DIV((v), (100)) == FLOW_CHECKED_MOD((chain[(depth - 1)]), (100)))) {
                    chain[depth] = v;
                    if (search_i32_ptr_i32_ptr_i32_ptr_i8_ptr_i32_ptr_i64((depth + 1), nums, counts, used, chain, answer)) {
                        return 1;
                    }
                }
                k = (k + 1);
            }
            used[ti] = 0;
        }
        ti = (ti + 1);
    }
    return 0;
}

int32_t main(void) {
    int32_t* nums = (int32_t*)(calloc((6 * 200), 4));
    int32_t* counts = (int32_t*)(calloc(6, 4));
    int8_t* used = (int8_t*)(calloc(6, 1));
    int32_t* chain = (int32_t*)(calloc(6, 4));
    int64_t* answer = (int64_t*)(calloc(1, 8));
    if (((((nums == NULL || counts == NULL) || used == NULL) || chain == NULL) || answer == NULL)) {
        return 1;
    }
    int32_t t = 3;
    while (t <= 8) {
        int32_t ti = (t - 3);
        int64_t n = 1;
        while (1) {
            int64_t v = poly_i32_i64(t, n);
            if (v >= 10000) {
                break;
            }
            if (v >= 1000) {
                nums[((ti * 200) + counts[ti])] = ((int32_t)(v));
                counts[ti] = (counts[ti] + 1);
            }
            n = (n + 1);
        }
        t = (t + 1);
    }
    search_i32_ptr_i32_ptr_i32_ptr_i8_ptr_i32_ptr_i64(0, nums, counts, used, chain, answer);
    printf("%lld\n", answer[0]);
    free(answer);
    free(chain);
    free(used);
    free(counts);
    free(nums);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @poly(%arg0: i32, %arg1: i64) -> i64 {
    %0 = arith.constant 3 : i32
    %1 = arith.cmpi eq, %arg0, %0 : i32
    cf.cond_br %1, ^bb0, ^bb1
    ^bb0:
      %2 = arith.constant 1 : i32
      %4 = arith.extsi %2 : i32 to i64
      %3 = arith.addi %arg1, %4 : i64
      %5 = arith.muli %arg1, %3 : i64
      %6 = arith.constant 2 : i32
      %8 = arith.extsi %6 : i32 to i64
      %7 = arith.divsi %5, %8 : i64
      func.return %7 : i64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %9 = arith.constant 4 : i32
    %10 = arith.cmpi eq, %arg0, %9 : i32
    cf.cond_br %10, ^bb3, ^bb4
    ^bb3:
      %11 = arith.muli %arg1, %arg1 : i64
      func.return %11 : i64
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %12 = arith.constant 5 : i32
    %13 = arith.cmpi eq, %arg0, %12 : i32
    cf.cond_br %13, ^bb6, ^bb7
    ^bb6:
      %14 = arith.constant 3 : i32
      %16 = arith.extsi %14 : i32 to i64
      %15 = arith.muli %16, %arg1 : i64
      %17 = arith.constant 1 : i32
      %19 = arith.extsi %17 : i32 to i64
      %18 = arith.subi %15, %19 : i64
      %20 = arith.muli %arg1, %18 : i64
      %21 = arith.constant 2 : i32
      %23 = arith.extsi %21 : i32 to i64
      %22 = arith.divsi %20, %23 : i64
      func.return %22 : i64
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %24 = arith.constant 6 : i32
    %25 = arith.cmpi eq, %arg0, %24 : i32
    cf.cond_br %25, ^bb9, ^bb10
    ^bb9:
      %26 = arith.constant 2 : i32
      %28 = arith.extsi %26 : i32 to i64
      %27 = arith.muli %28, %arg1 : i64
      %29 = arith.constant 1 : i32
      %31 = arith.extsi %29 : i32 to i64
      %30 = arith.subi %27, %31 : i64
      %32 = arith.muli %arg1, %30 : i64
      func.return %32 : i64
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %33 = arith.constant 7 : i32
    %34 = arith.cmpi eq, %arg0, %33 : i32
    cf.cond_br %34, ^bb12, ^bb13
    ^bb12:
      %35 = arith.constant 5 : i32
      %37 = arith.extsi %35 : i32 to i64
      %36 = arith.muli %37, %arg1 : i64
      %38 = arith.constant 3 : i32
      %40 = arith.extsi %38 : i32 to i64
      %39 = arith.subi %36, %40 : i64
      %41 = arith.muli %arg1, %39 : i64
      %42 = arith.constant 2 : i32
      %44 = arith.extsi %42 : i32 to i64
      %43 = arith.divsi %41, %44 : i64
      func.return %43 : i64
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %45 = arith.constant 3 : i32
    %47 = arith.extsi %45 : i32 to i64
    %46 = arith.muli %47, %arg1 : i64
    %48 = arith.constant 2 : i32
    %50 = arith.extsi %48 : i32 to i64
    %49 = arith.subi %46, %50 : i64
    %51 = arith.muli %arg1, %49 : i64
    func.return %51 : i64
  }
  func.func @search(%arg0: i32, %arg1: !llvm.ptr, %arg2: !llvm.ptr, %arg3: !llvm.ptr, %arg4: !llvm.ptr, %arg5: !llvm.ptr) -> i1 {
    %52 = arith.constant 6 : i32
    %53 = arith.cmpi eq, %arg0, %52 : i32
    cf.cond_br %53, ^bb15, ^bb16
    ^bb15:
      %55 = arith.constant 5 : i32
      %56 = arith.extsi %55 : i32 to i64
      %57 = llvm.getelementptr %arg4[%56] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %54 = llvm.load %57 : !llvm.ptr -> i32
      %58 = arith.constant 100 : i32
      %59 = arith.remsi %54, %58 : i32
      %61 = arith.constant 0 : i32
      %62 = arith.extsi %61 : i32 to i64
      %63 = llvm.getelementptr %arg4[%62] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      %60 = llvm.load %63 : !llvm.ptr -> i32
      %64 = arith.constant 100 : i32
      %65 = arith.divsi %60, %64 : i32
      %66 = arith.cmpi eq, %59, %65 : i32
      cf.cond_br %66, ^bb18, ^bb19
      ^bb18:
        %67 = arith.constant 0 : i32
        %68 = arith.extsi %67 : i32 to i64
        %69 = llvm.mlir.constant(1 : i64) : i64
        %70 = llvm.alloca %69 x i64 : (i64) -> !llvm.ptr
        llvm.store %68, %70 : i64, !llvm.ptr
        %71 = arith.constant 0 : i32
        %72 = llvm.mlir.constant(1 : i64) : i64
        %73 = llvm.alloca %72 x i32 : (i64) -> !llvm.ptr
        llvm.store %71, %73 : i32, !llvm.ptr
        cf.br ^bb21
        ^bb21:
        %74 = llvm.load %73 : !llvm.ptr -> i32
        %75 = arith.constant 6 : i32
        %76 = arith.cmpi slt, %74, %75 : i32
        cf.cond_br %76, ^bb22, ^bb23
        ^bb22:
          %77 = llvm.load %70 : !llvm.ptr -> i64
          %79 = llvm.load %73 : !llvm.ptr -> i32
          %80 = arith.extsi %79 : i32 to i64
          %81 = llvm.getelementptr %arg4[%80] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %78 = llvm.load %81 : !llvm.ptr -> i32
          %82 = arith.extsi %78 : i32 to i64
          %83 = arith.addi %77, %82 : i64
          llvm.store %83, %70 : i64, !llvm.ptr
          %84 = llvm.load %73 : !llvm.ptr -> i32
          %85 = arith.constant 1 : i32
          %86 = arith.addi %84, %85 : i32
          llvm.store %86, %73 : i32, !llvm.ptr
          cf.br ^bb21
        ^bb23:
        %87 = llvm.load %70 : !llvm.ptr -> i64
        %88 = arith.constant 0 : i32
        %89 = arith.extsi %88 : i32 to i64
        %90 = llvm.getelementptr %arg5[%89] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %87, %90 : i64, !llvm.ptr
        %91 = arith.constant 1 : i1
        func.return %91 : i1
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %92 = arith.constant 0 : i1
      func.return %92 : i1
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %93 = arith.constant 0 : i32
    %94 = llvm.mlir.constant(1 : i64) : i64
    %95 = llvm.alloca %94 x i32 : (i64) -> !llvm.ptr
    llvm.store %93, %95 : i32, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %96 = llvm.load %95 : !llvm.ptr -> i32
    %97 = arith.constant 6 : i32
    %98 = arith.cmpi slt, %96, %97 : i32
    cf.cond_br %98, ^bb25, ^bb26
    ^bb25:
      %100 = llvm.load %95 : !llvm.ptr -> i32
      %101 = arith.extsi %100 : i32 to i64
      %102 = llvm.getelementptr %arg3[%101] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %99 = llvm.load %102 : !llvm.ptr -> i8
      %103 = arith.constant 0 : i32
      %105 = arith.extsi %99 : i8 to i32
      %104 = arith.cmpi eq, %105, %103 : i32
      cf.cond_br %104, ^bb27, ^bb28
      ^bb27:
        %106 = arith.constant 1 : i32
        %107 = llvm.load %95 : !llvm.ptr -> i32
        %108 = arith.trunci %106 : i32 to i8
        %109 = arith.extsi %107 : i32 to i64
        %110 = llvm.getelementptr %arg3[%109] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        llvm.store %108, %110 : i8, !llvm.ptr
        %111 = arith.constant 0 : i32
        %112 = llvm.mlir.constant(1 : i64) : i64
        %113 = llvm.alloca %112 x i32 : (i64) -> !llvm.ptr
        llvm.store %111, %113 : i32, !llvm.ptr
        cf.br ^bb30
        ^bb30:
        %114 = llvm.load %113 : !llvm.ptr -> i32
        %116 = llvm.load %95 : !llvm.ptr -> i32
        %117 = arith.extsi %116 : i32 to i64
        %118 = llvm.getelementptr %arg2[%117] : (!llvm.ptr, i64) -> !llvm.ptr, i32
        %115 = llvm.load %118 : !llvm.ptr -> i32
        %119 = arith.cmpi slt, %114, %115 : i32
        cf.cond_br %119, ^bb31, ^bb32
        ^bb31:
          %121 = llvm.load %95 : !llvm.ptr -> i32
          %122 = arith.constant 200 : i32
          %123 = arith.muli %121, %122 : i32
          %124 = llvm.load %113 : !llvm.ptr -> i32
          %125 = arith.addi %123, %124 : i32
          %126 = arith.extsi %125 : i32 to i64
          %127 = llvm.getelementptr %arg1[%126] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %120 = llvm.load %127 : !llvm.ptr -> i32
          %128 = arith.constant 0 : i32
          %129 = arith.cmpi eq, %arg0, %128 : i32
          %130 = scf.if %129 -> (i1) {
            %131 = arith.constant true
            scf.yield %131 : i1
          } else {
            %132 = arith.constant 100 : i32
            %133 = arith.divsi %120, %132 : i32
            %135 = arith.constant 1 : i32
            %136 = arith.subi %arg0, %135 : i32
            %137 = arith.extsi %136 : i32 to i64
            %138 = llvm.getelementptr %arg4[%137] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            %134 = llvm.load %138 : !llvm.ptr -> i32
            %139 = arith.constant 100 : i32
            %140 = arith.remsi %134, %139 : i32
            %141 = arith.cmpi eq, %133, %140 : i32
            scf.yield %141 : i1
          }
          cf.cond_br %130, ^bb33, ^bb34
          ^bb33:
            %142 = arith.extsi %arg0 : i32 to i64
            %143 = llvm.getelementptr %arg4[%142] : (!llvm.ptr, i64) -> !llvm.ptr, i32
            llvm.store %120, %143 : i32, !llvm.ptr
            %145 = arith.constant 1 : i32
            %146 = arith.addi %arg0, %145 : i32
            %144 = func.call @search(%146, %arg1, %arg2, %arg3, %arg4, %arg5) : (i32, !llvm.ptr, !llvm.ptr, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> i1
            cf.cond_br %144, ^bb36, ^bb37
            ^bb36:
              %147 = arith.constant 1 : i1
              func.return %147 : i1
            ^bb37:
              cf.br ^bb38
            ^bb38:
            cf.br ^bb35
          ^bb34:
            cf.br ^bb35
          ^bb35:
          %148 = llvm.load %113 : !llvm.ptr -> i32
          %149 = arith.constant 1 : i32
          %150 = arith.addi %148, %149 : i32
          llvm.store %150, %113 : i32, !llvm.ptr
          cf.br ^bb30
        ^bb32:
        %151 = arith.constant 0 : i32
        %152 = llvm.load %95 : !llvm.ptr -> i32
        %153 = arith.trunci %151 : i32 to i8
        %154 = arith.extsi %152 : i32 to i64
        %155 = llvm.getelementptr %arg3[%154] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        llvm.store %153, %155 : i8, !llvm.ptr
        cf.br ^bb29
      ^bb28:
        cf.br ^bb29
      ^bb29:
      %156 = llvm.load %95 : !llvm.ptr -> i32
      %157 = arith.constant 1 : i32
      %158 = arith.addi %156, %157 : i32
      llvm.store %158, %95 : i32, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %159 = arith.constant 0 : i1
    func.return %159 : i1
  }
  func.func @main() -> i32 {
    %161 = arith.constant 6 : i32
    %162 = arith.constant 200 : i32
    %163 = arith.muli %161, %162 : i32
    %164 = arith.constant 4 : i32
    %165 = arith.extsi %163 : i32 to i64
    %166 = arith.extsi %164 : i32 to i64
    %160 = func.call @calloc(%165, %166) : (i64, i64) -> !llvm.ptr
    %168 = arith.constant 6 : i32
    %169 = arith.constant 4 : i32
    %170 = arith.extsi %168 : i32 to i64
    %171 = arith.extsi %169 : i32 to i64
    %167 = func.call @calloc(%170, %171) : (i64, i64) -> !llvm.ptr
    %173 = arith.constant 6 : i32
    %174 = arith.constant 1 : i32
    %175 = arith.extsi %173 : i32 to i64
    %176 = arith.extsi %174 : i32 to i64
    %172 = func.call @calloc(%175, %176) : (i64, i64) -> !llvm.ptr
    %178 = arith.constant 6 : i32
    %179 = arith.constant 4 : i32
    %180 = arith.extsi %178 : i32 to i64
    %181 = arith.extsi %179 : i32 to i64
    %177 = func.call @calloc(%180, %181) : (i64, i64) -> !llvm.ptr
    %183 = arith.constant 1 : i32
    %184 = arith.constant 8 : i32
    %185 = arith.extsi %183 : i32 to i64
    %186 = arith.extsi %184 : i32 to i64
    %182 = func.call @calloc(%185, %186) : (i64, i64) -> !llvm.ptr
    %187 = llvm.mlir.zero : !llvm.ptr
    %188 = llvm.icmp "eq" %160, %187 : !llvm.ptr
    %189 = scf.if %188 -> (i1) {
      %190 = arith.constant true
      scf.yield %190 : i1
    } else {
      %191 = llvm.mlir.zero : !llvm.ptr
      %192 = llvm.icmp "eq" %167, %191 : !llvm.ptr
      scf.yield %192 : i1
    }
    %193 = scf.if %189 -> (i1) {
      %194 = arith.constant true
      scf.yield %194 : i1
    } else {
      %195 = llvm.mlir.zero : !llvm.ptr
      %196 = llvm.icmp "eq" %172, %195 : !llvm.ptr
      scf.yield %196 : i1
    }
    %197 = scf.if %193 -> (i1) {
      %198 = arith.constant true
      scf.yield %198 : i1
    } else {
      %199 = llvm.mlir.zero : !llvm.ptr
      %200 = llvm.icmp "eq" %177, %199 : !llvm.ptr
      scf.yield %200 : i1
    }
    %201 = scf.if %197 -> (i1) {
      %202 = arith.constant true
      scf.yield %202 : i1
    } else {
      %203 = llvm.mlir.zero : !llvm.ptr
      %204 = llvm.icmp "eq" %182, %203 : !llvm.ptr
      scf.yield %204 : i1
    }
    cf.cond_br %201, ^bb39, ^bb40
    ^bb39:
      %205 = arith.constant 1 : i32
      func.return %205 : i32
    ^bb40:
      cf.br ^bb41
    ^bb41:
    %206 = arith.constant 3 : i32
    %207 = llvm.mlir.constant(1 : i64) : i64
    %208 = llvm.alloca %207 x i32 : (i64) -> !llvm.ptr
    llvm.store %206, %208 : i32, !llvm.ptr
    cf.br ^bb42
    ^bb42:
    %209 = llvm.load %208 : !llvm.ptr -> i32
    %210 = arith.constant 8 : i32
    %211 = arith.cmpi sle, %209, %210 : i32
    cf.cond_br %211, ^bb43, ^bb44
    ^bb43:
      %212 = llvm.load %208 : !llvm.ptr -> i32
      %213 = arith.constant 3 : i32
      %214 = arith.subi %212, %213 : i32
      %215 = arith.constant 1 : i32
      %216 = arith.extsi %215 : i32 to i64
      %217 = llvm.mlir.constant(1 : i64) : i64
      %218 = llvm.alloca %217 x i64 : (i64) -> !llvm.ptr
      llvm.store %216, %218 : i64, !llvm.ptr
      cf.br ^bb45
      ^bb45:
      %219 = arith.constant 1 : i1
      cf.cond_br %219, ^bb46, ^bb47
      ^bb46:
        %221 = llvm.load %208 : !llvm.ptr -> i32
        %222 = llvm.load %218 : !llvm.ptr -> i64
        %220 = func.call @poly(%221, %222) : (i32, i64) -> i64
        %223 = arith.constant 10000 : i32
        %225 = arith.extsi %223 : i32 to i64
        %224 = arith.cmpi sge, %220, %225 : i64
        cf.cond_br %224, ^bb48, ^bb49
        ^bb48:
          cf.br ^bb47
        ^bb49:
          cf.br ^bb50
        ^bb50:
        %226 = arith.constant 1000 : i32
        %228 = arith.extsi %226 : i32 to i64
        %227 = arith.cmpi sge, %220, %228 : i64
        cf.cond_br %227, ^bb51, ^bb52
        ^bb51:
          %229 = arith.trunci %220 : i64 to i32
          %230 = arith.constant 200 : i32
          %231 = arith.muli %214, %230 : i32
          %233 = arith.extsi %214 : i32 to i64
          %234 = llvm.getelementptr %167[%233] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %232 = llvm.load %234 : !llvm.ptr -> i32
          %235 = arith.addi %231, %232 : i32
          %236 = arith.extsi %235 : i32 to i64
          %237 = llvm.getelementptr %160[%236] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          llvm.store %229, %237 : i32, !llvm.ptr
          %239 = arith.extsi %214 : i32 to i64
          %240 = llvm.getelementptr %167[%239] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          %238 = llvm.load %240 : !llvm.ptr -> i32
          %241 = arith.constant 1 : i32
          %242 = arith.addi %238, %241 : i32
          %243 = arith.extsi %214 : i32 to i64
          %244 = llvm.getelementptr %167[%243] : (!llvm.ptr, i64) -> !llvm.ptr, i32
          llvm.store %242, %244 : i32, !llvm.ptr
          cf.br ^bb53
        ^bb52:
          cf.br ^bb53
        ^bb53:
        %245 = llvm.load %218 : !llvm.ptr -> i64
        %246 = arith.constant 1 : i32
        %248 = arith.extsi %246 : i32 to i64
        %247 = arith.addi %245, %248 : i64
        llvm.store %247, %218 : i64, !llvm.ptr
        cf.br ^bb45
      ^bb47:
      %249 = llvm.load %208 : !llvm.ptr -> i32
      %250 = arith.constant 1 : i32
      %251 = arith.addi %249, %250 : i32
      llvm.store %251, %208 : i32, !llvm.ptr
      cf.br ^bb42
    ^bb44:
    %253 = arith.constant 0 : i32
    %252 = func.call @search(%253, %160, %167, %172, %177, %182) : (i32, !llvm.ptr, !llvm.ptr, !llvm.ptr, !llvm.ptr, !llvm.ptr) -> i1
    %254 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %256 = arith.constant 0 : i32
    %257 = arith.extsi %256 : i32 to i64
    %258 = llvm.getelementptr %182[%257] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %255 = llvm.load %258 : !llvm.ptr -> i64
    %259 = llvm.call @printf(%254, %255) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%182) : (!llvm.ptr) -> ()
    func.call @free(%177) : (!llvm.ptr) -> ()
    func.call @free(%172) : (!llvm.ptr) -> ()
    func.call @free(%167) : (!llvm.ptr) -> ()
    func.call @free(%160) : (!llvm.ptr) -> ()
    %265 = arith.constant 0 : i32
    func.return %265 : i32
  }
}