Problem 868

Belfry Maths — Steinhaus–Johnson–Trotter rank of a word.

Answer3832914911887589
Output3832914911887589
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 868
# Belfry Maths — Steinhaus–Johnson–Trotter rank of a word.

function sjt_rank(perm: ptr<i64>, n: i64) -> i64 {
    if n <= 1 { return 0 }
    let mut pos: i64 = 0
    while pos < n {
        if perm[pos] == n { break }
        pos = pos + 1
    }
    # remove n into base[0..n-2]
    let mut base: [i64; 32]
    let mut j: i64 = 0
    let mut i: i64 = 0
    while i < n {
        if i != pos {
            base[j] = perm[i]
            j = j + 1
        }
        i = i + 1
    }
    let r: i64 = sjt_rank(&base[0], n - 1)
    let mut t: i64 = pos
    if (r & 1) == 0 {
        t = n - 1 - pos
    }
    return r * n + t
}

function swaps_to_reach(word: ptr<i8>, n: i64) -> i64 {
    # Map letters to 1..n by sorted order
    let mut letters: [i8; 32]
    let mut i: i64 = 0
    while i < n {
        letters[i] = word[i]
        i = i + 1
    }
    # insertion sort copy for ranking
    let mut sorted: [i8; 32]
    i = 0
    while i < n {
        sorted[i] = letters[i]
        i = i + 1
    }
    let mut a: i64 = 1
    while a < n {
        let key: i8 = sorted[a]
        let mut b: i64 = a
        while b > 0 && sorted[b - 1] > key {
            sorted[b] = sorted[b - 1]
            b = b - 1
        }
        sorted[b] = key
        a = a + 1
    }
    let mut perm: [i64; 32]
    i = 0
    while i < n {
        let ch: i8 = letters[i]
        let mut rank: i64 = 1
        let mut j: i64 = 0
        while j < n {
            if sorted[j] == ch {
                rank = j + 1
                break
            }
            j = j + 1
        }
        perm[i] = rank
        i = i + 1
    }
    return sjt_rank(&perm[0], n)
}

function main() -> i32 {
    let word: ptr<i8> = "NOWPICKBELFRYMATHS"
    printf("%lld\n", swaps_to_reach(word, 18))
    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 sjt_rank_ptr_i64_i64(int64_t* perm, int64_t n);
int64_t swaps_to_reach_ptr_i8_i64(int8_t* word, int64_t n);
int32_t main(void);

int64_t sjt_rank_ptr_i64_i64(int64_t* perm, int64_t n) {
    if (n <= 1) {
        return 0;
    }
    int64_t pos = 0;
    while (pos < n) {
        if (perm[pos] == n) {
            break;
        }
        pos = (pos + 1);
    }
    int64_t base[32];
    int64_t j = 0;
    int64_t i = 0;
    while (i < n) {
        if (i != pos) {
            base[j] = perm[i];
            j = (j + 1);
        }
        i = (i + 1);
    }
    int64_t r = sjt_rank_ptr_i64_i64((&(base[0])), (n - 1));
    int64_t t = pos;
    if ((r & 1) == 0) {
        t = ((n - 1) - pos);
    }
    return ((r * n) + t);
}

int64_t swaps_to_reach_ptr_i8_i64(int8_t* word, int64_t n) {
    int8_t letters[32];
    int64_t i = 0;
    while (i < n) {
        letters[i] = word[i];
        i = (i + 1);
    }
    int8_t sorted[32];
    i = 0;
    while (i < n) {
        sorted[i] = (((unsigned)(i) < 32) ? letters[i] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(i), 32), flow_fault_handler("array index out of bounds"), letters[0]));
        i = (i + 1);
    }
    int64_t a = 1;
    while (a < n) {
        int8_t key = (((unsigned)(a) < 32) ? sorted[a] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(a), 32), flow_fault_handler("array index out of bounds"), sorted[0]));
        int64_t b = a;
        while ((b > 0 && (((unsigned)((b - 1)) < 32) ? sorted[(b - 1)] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)((b - 1)), 32), flow_fault_handler("array index out of bounds"), sorted[0])) > key)) {
            sorted[b] = (((unsigned)((b - 1)) < 32) ? sorted[(b - 1)] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)((b - 1)), 32), flow_fault_handler("array index out of bounds"), sorted[0]));
            b = (b - 1);
        }
        sorted[b] = key;
        a = (a + 1);
    }
    int64_t perm[32];
    i = 0;
    while (i < n) {
        int8_t ch = (((unsigned)(i) < 32) ? letters[i] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(i), 32), flow_fault_handler("array index out of bounds"), letters[0]));
        int64_t rank = 1;
        int64_t j = 0;
        while (j < n) {
            if ((((unsigned)(j) < 32) ? sorted[j] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(j), 32), flow_fault_handler("array index out of bounds"), sorted[0])) == ch) {
                rank = (j + 1);
                break;
            }
            j = (j + 1);
        }
        perm[i] = rank;
        i = (i + 1);
    }
    return sjt_rank_ptr_i64_i64((&(perm[0])), n);
}

int32_t main(void) {
    int8_t* word = (int8_t*)("NOWPICKBELFRYMATHS");
    printf("%lld\n", swaps_to_reach_ptr_i8_i64(word, 18));
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("NOWPICKBELFRYMATHS\00") {addr_space = 0 : i32} : !llvm.array<19 x i8>
  llvm.mlir.global internal constant @str_1("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func @sjt_rank(%arg0: !llvm.ptr, %arg1: i64) -> i64 {
    %0 = arith.constant 1 : i32
    %2 = arith.extsi %0 : i32 to i64
    %1 = arith.cmpi sle, %arg1, %2 : i64
    cf.cond_br %1, ^bb0, ^bb1
    ^bb0:
      %3 = arith.constant 0 : i32
      %4 = arith.extsi %3 : i32 to i64
      func.return %4 : i64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %5 = arith.constant 0 : i32
    %6 = arith.extsi %5 : i32 to i64
    %7 = llvm.mlir.constant(1 : i64) : i64
    %8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
    llvm.store %6, %8 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %9 = llvm.load %8 : !llvm.ptr -> i64
    %10 = arith.cmpi slt, %9, %arg1 : i64
    cf.cond_br %10, ^bb4, ^bb5
    ^bb4:
      %12 = llvm.load %8 : !llvm.ptr -> i64
      %13 = llvm.getelementptr %arg0[%12] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %11 = llvm.load %13 : !llvm.ptr -> i64
      %14 = arith.cmpi eq, %11, %arg1 : i64
      cf.cond_br %14, ^bb6, ^bb7
      ^bb6:
        cf.br ^bb5
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %15 = llvm.load %8 : !llvm.ptr -> i64
      %16 = arith.constant 1 : i32
      %18 = arith.extsi %16 : i32 to i64
      %17 = arith.addi %15, %18 : i64
      llvm.store %17, %8 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %19 = memref.alloca() {type = memref<32xi64>} : memref<32xi64>
    %20 = arith.constant 0 : i32
    %21 = arith.extsi %20 : i32 to i64
    %22 = llvm.mlir.constant(1 : i64) : i64
    %23 = llvm.alloca %22 x i64 : (i64) -> !llvm.ptr
    llvm.store %21, %23 : i64, !llvm.ptr
    %24 = arith.constant 0 : i32
    %25 = arith.extsi %24 : i32 to i64
    %26 = llvm.mlir.constant(1 : i64) : i64
    %27 = llvm.alloca %26 x i64 : (i64) -> !llvm.ptr
    llvm.store %25, %27 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %28 = llvm.load %27 : !llvm.ptr -> i64
    %29 = arith.cmpi slt, %28, %arg1 : i64
    cf.cond_br %29, ^bb10, ^bb11
    ^bb10:
      %30 = llvm.load %27 : !llvm.ptr -> i64
      %31 = llvm.load %8 : !llvm.ptr -> i64
      %32 = arith.cmpi ne, %30, %31 : i64
      cf.cond_br %32, ^bb12, ^bb13
      ^bb12:
        %34 = llvm.load %27 : !llvm.ptr -> i64
        %35 = llvm.getelementptr %arg0[%34] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %33 = llvm.load %35 : !llvm.ptr -> i64
        %36 = llvm.load %23 : !llvm.ptr -> i64
        %37 = arith.index_cast %36 : i32 to index
        memref.store %33, %19[%37] : memref<32xi64>
        %38 = llvm.load %23 : !llvm.ptr -> i64
        %39 = arith.constant 1 : i32
        %41 = arith.extsi %39 : i32 to i64
        %40 = arith.addi %38, %41 : i64
        llvm.store %40, %23 : i64, !llvm.ptr
        cf.br ^bb14
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %42 = llvm.load %27 : !llvm.ptr -> i64
      %43 = arith.constant 1 : i32
      %45 = arith.extsi %43 : i32 to i64
      %44 = arith.addi %42, %45 : i64
      llvm.store %44, %27 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %47 = arith.constant 0 : i32
    %48 = arith.extsi %47 : i32 to i64
    %49 = memref.extract_aligned_pointer_as_index %19 : memref<32xi64> -> index
    %50 = arith.index_cast %49 : index to i64
    %51 = arith.constant 8 : i64
    %52 = arith.muli %48, %51 : i64
    %53 = arith.addi %50, %52 : i64
    %54 = llvm.inttoptr %53 : i64 to !llvm.ptr
    %55 = arith.constant 1 : i32
    %57 = arith.extsi %55 : i32 to i64
    %56 = arith.subi %arg1, %57 : i64
    %46 = func.call @sjt_rank(%54, %56) : (!llvm.ptr, i64) -> i64
    %58 = llvm.load %8 : !llvm.ptr -> i64
    %59 = llvm.mlir.constant(1 : i64) : i64
    %60 = llvm.alloca %59 x i64 : (i64) -> !llvm.ptr
    llvm.store %58, %60 : i64, !llvm.ptr
    %61 = arith.constant 1 : i32
    %63 = arith.extsi %61 : i32 to i64
    %62 = arith.andi %46, %63 : i64
    %64 = arith.constant 0 : i32
    %66 = arith.extsi %64 : i32 to i64
    %65 = arith.cmpi eq, %62, %66 : i64
    cf.cond_br %65, ^bb15, ^bb16
    ^bb15:
      %67 = arith.constant 1 : i32
      %69 = arith.extsi %67 : i32 to i64
      %68 = arith.subi %arg1, %69 : i64
      %70 = llvm.load %8 : !llvm.ptr -> i64
      %71 = arith.subi %68, %70 : i64
      llvm.store %71, %60 : i64, !llvm.ptr
      cf.br ^bb17
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %72 = arith.muli %46, %arg1 : i64
    %73 = llvm.load %60 : !llvm.ptr -> i64
    %74 = arith.addi %72, %73 : i64
    func.return %74 : i64
  }
  func.func @swaps_to_reach(%arg0: !llvm.ptr, %arg1: i64) -> i64 {
    %75 = memref.alloca() {type = memref<32xi8>} : memref<32xi8>
    %76 = arith.constant 0 : i32
    %77 = arith.extsi %76 : i32 to i64
    %78 = llvm.mlir.constant(1 : i64) : i64
    %79 = llvm.alloca %78 x i64 : (i64) -> !llvm.ptr
    llvm.store %77, %79 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %80 = llvm.load %79 : !llvm.ptr -> i64
    %81 = arith.cmpi slt, %80, %arg1 : i64
    cf.cond_br %81, ^bb19, ^bb20
    ^bb19:
      %83 = llvm.load %79 : !llvm.ptr -> i64
      %84 = llvm.getelementptr %arg0[%83] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %82 = llvm.load %84 : !llvm.ptr -> i8
      %85 = llvm.load %79 : !llvm.ptr -> i64
      %86 = arith.index_cast %85 : i32 to index
      memref.store %82, %75[%86] : memref<32xi8>
      %87 = llvm.load %79 : !llvm.ptr -> i64
      %88 = arith.constant 1 : i32
      %90 = arith.extsi %88 : i32 to i64
      %89 = arith.addi %87, %90 : i64
      llvm.store %89, %79 : i64, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %91 = memref.alloca() {type = memref<32xi8>} : memref<32xi8>
    %92 = arith.constant 0 : i32
    %93 = arith.extsi %92 : i32 to i64
    llvm.store %93, %79 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %94 = llvm.load %79 : !llvm.ptr -> i64
    %95 = arith.cmpi slt, %94, %arg1 : i64
    cf.cond_br %95, ^bb22, ^bb23
    ^bb22:
      %97 = llvm.load %79 : !llvm.ptr -> i64
      %98 = arith.index_cast %97 : i32 to index
      %96 = memref.load %75[%98] : memref<32xi8>
      %99 = llvm.load %79 : !llvm.ptr -> i64
      %100 = arith.index_cast %99 : i32 to index
      memref.store %96, %91[%100] : memref<32xi8>
      %101 = llvm.load %79 : !llvm.ptr -> i64
      %102 = arith.constant 1 : i32
      %104 = arith.extsi %102 : i32 to i64
      %103 = arith.addi %101, %104 : i64
      llvm.store %103, %79 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %105 = arith.constant 1 : i32
    %106 = arith.extsi %105 : i32 to i64
    %107 = llvm.mlir.constant(1 : i64) : i64
    %108 = llvm.alloca %107 x i64 : (i64) -> !llvm.ptr
    llvm.store %106, %108 : i64, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %109 = llvm.load %108 : !llvm.ptr -> i64
    %110 = arith.cmpi slt, %109, %arg1 : i64
    cf.cond_br %110, ^bb25, ^bb26
    ^bb25:
      %112 = llvm.load %108 : !llvm.ptr -> i64
      %113 = arith.index_cast %112 : i32 to index
      %111 = memref.load %91[%113] : memref<32xi8>
      %114 = llvm.load %108 : !llvm.ptr -> i64
      %115 = llvm.mlir.constant(1 : i64) : i64
      %116 = llvm.alloca %115 x i64 : (i64) -> !llvm.ptr
      llvm.store %114, %116 : i64, !llvm.ptr
      cf.br ^bb27
      ^bb27:
      %117 = llvm.load %116 : !llvm.ptr -> i64
      %118 = arith.constant 0 : i32
      %120 = arith.extsi %118 : i32 to i64
      %119 = arith.cmpi sgt, %117, %120 : i64
      %121 = scf.if %119 -> (i1) {
        %123 = llvm.load %116 : !llvm.ptr -> i64
        %124 = arith.constant 1 : i32
        %126 = arith.extsi %124 : i32 to i64
        %125 = arith.subi %123, %126 : i64
        %127 = arith.index_cast %125 : i32 to index
        %122 = memref.load %91[%127] : memref<32xi8>
        %129 = arith.extsi %122 : i8 to i32
        %130 = arith.extsi %111 : i8 to i32
        %128 = arith.cmpi sgt, %129, %130 : i32
        scf.yield %128 : i1
      } else {
        %131 = arith.constant false
        scf.yield %131 : i1
      }
      cf.cond_br %121, ^bb28, ^bb29
      ^bb28:
        %133 = llvm.load %116 : !llvm.ptr -> i64
        %134 = arith.constant 1 : i32
        %136 = arith.extsi %134 : i32 to i64
        %135 = arith.subi %133, %136 : i64
        %137 = arith.index_cast %135 : i32 to index
        %132 = memref.load %91[%137] : memref<32xi8>
        %138 = llvm.load %116 : !llvm.ptr -> i64
        %139 = arith.index_cast %138 : i32 to index
        memref.store %132, %91[%139] : memref<32xi8>
        %140 = llvm.load %116 : !llvm.ptr -> i64
        %141 = arith.constant 1 : i32
        %143 = arith.extsi %141 : i32 to i64
        %142 = arith.subi %140, %143 : i64
        llvm.store %142, %116 : i64, !llvm.ptr
        cf.br ^bb27
      ^bb29:
      %144 = llvm.load %116 : !llvm.ptr -> i64
      %145 = arith.index_cast %144 : i32 to index
      memref.store %111, %91[%145] : memref<32xi8>
      %146 = llvm.load %108 : !llvm.ptr -> i64
      %147 = arith.constant 1 : i32
      %149 = arith.extsi %147 : i32 to i64
      %148 = arith.addi %146, %149 : i64
      llvm.store %148, %108 : i64, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %150 = memref.alloca() {type = memref<32xi64>} : memref<32xi64>
    %151 = arith.constant 0 : i32
    %152 = arith.extsi %151 : i32 to i64
    llvm.store %152, %79 : i64, !llvm.ptr
    cf.br ^bb30
    ^bb30:
    %153 = llvm.load %79 : !llvm.ptr -> i64
    %154 = arith.cmpi slt, %153, %arg1 : i64
    cf.cond_br %154, ^bb31, ^bb32
    ^bb31:
      %156 = llvm.load %79 : !llvm.ptr -> i64
      %157 = arith.index_cast %156 : i32 to index
      %155 = memref.load %75[%157] : memref<32xi8>
      %158 = arith.constant 1 : i32
      %159 = arith.extsi %158 : i32 to i64
      %160 = llvm.mlir.constant(1 : i64) : i64
      %161 = llvm.alloca %160 x i64 : (i64) -> !llvm.ptr
      llvm.store %159, %161 : i64, !llvm.ptr
      %162 = arith.constant 0 : i32
      %163 = arith.extsi %162 : i32 to i64
      %164 = llvm.mlir.constant(1 : i64) : i64
      %165 = llvm.alloca %164 x i64 : (i64) -> !llvm.ptr
      llvm.store %163, %165 : i64, !llvm.ptr
      cf.br ^bb33
      ^bb33:
      %166 = llvm.load %165 : !llvm.ptr -> i64
      %167 = arith.cmpi slt, %166, %arg1 : i64
      cf.cond_br %167, ^bb34, ^bb35
      ^bb34:
        %169 = llvm.load %165 : !llvm.ptr -> i64
        %170 = arith.index_cast %169 : i32 to index
        %168 = memref.load %91[%170] : memref<32xi8>
        %172 = arith.extsi %168 : i8 to i32
        %173 = arith.extsi %155 : i8 to i32
        %171 = arith.cmpi eq, %172, %173 : i32
        cf.cond_br %171, ^bb36, ^bb37
        ^bb36:
          %174 = llvm.load %165 : !llvm.ptr -> i64
          %175 = arith.constant 1 : i32
          %177 = arith.extsi %175 : i32 to i64
          %176 = arith.addi %174, %177 : i64
          llvm.store %176, %161 : i64, !llvm.ptr
          cf.br ^bb35
        ^bb37:
          cf.br ^bb38
        ^bb38:
        %178 = llvm.load %165 : !llvm.ptr -> i64
        %179 = arith.constant 1 : i32
        %181 = arith.extsi %179 : i32 to i64
        %180 = arith.addi %178, %181 : i64
        llvm.store %180, %165 : i64, !llvm.ptr
        cf.br ^bb33
      ^bb35:
      %182 = llvm.load %161 : !llvm.ptr -> i64
      %183 = llvm.load %79 : !llvm.ptr -> i64
      %184 = arith.index_cast %183 : i32 to index
      memref.store %182, %150[%184] : memref<32xi64>
      %185 = llvm.load %79 : !llvm.ptr -> i64
      %186 = arith.constant 1 : i32
      %188 = arith.extsi %186 : i32 to i64
      %187 = arith.addi %185, %188 : i64
      llvm.store %187, %79 : i64, !llvm.ptr
      cf.br ^bb30
    ^bb32:
    %190 = arith.constant 0 : i32
    %191 = arith.extsi %190 : i32 to i64
    %192 = memref.extract_aligned_pointer_as_index %150 : memref<32xi64> -> index
    %193 = arith.index_cast %192 : index to i64
    %194 = arith.constant 8 : i64
    %195 = arith.muli %191, %194 : i64
    %196 = arith.addi %193, %195 : i64
    %197 = llvm.inttoptr %196 : i64 to !llvm.ptr
    %189 = func.call @sjt_rank(%197, %arg1) : (!llvm.ptr, i64) -> i64
    func.return %189 : i64
  }
  func.func @main() -> i32 {
    %198 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %199 = llvm.mlir.addressof @str_1 : !llvm.ptr
    %201 = arith.constant 18 : i32
    %202 = arith.extsi %201 : i32 to i64
    %200 = func.call @swaps_to_reach(%198, %202) : (!llvm.ptr, i64) -> i64
    %203 = llvm.call @printf(%199, %200) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %204 = arith.constant 0 : i32
    func.return %204 : i32
  }
}