Problem 336

2011th maximix arrangement for 11 carriages.

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

Performance comparison

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

Flow source

# Project Euler 336
# 2011th maximix arrangement for 11 carriages.

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

function next_perm(a: ptr<i8>, n: i32) -> bool {
    let mut i: i32 = n - 2
    while i >= 0 && a[i] >= a[i + 1] {
        i = i - 1
    }
    if i < 0 { return false }
    let mut j: i32 = n - 1
    while a[j] <= a[i] {
        j = j - 1
    }
    let t: i8 = a[i]
    a[i] = a[j]
    a[j] = t
    let mut L: i32 = i + 1
    let mut R: i32 = n - 1
    while L < R {
        let u: i8 = a[L]
        a[L] = a[R]
        a[R] = u
        L = L + 1
        R = R - 1
    }
    return true
}

function reverse_range(a: ptr<i8>, L0: i32, R0: i32) -> i32 {
    let mut L: i32 = L0
    let mut R: i32 = R0
    while L < R {
        let t: i8 = a[L]
        a[L] = a[R]
        a[R] = t
        L = L + 1
        R = R - 1
    }
    return 0
}

function main() -> i32 {
    let length: i32 = 11
    let stop: i32 = 2011
    let train: ptr<i8> = calloc(length as i64, 1)
    let current: ptr<i8> = calloc(length as i64, 1)
    if train == null || current == null { return 1 }
    # CABDEFGHIJK
    train[0]=67; train[1]=65; train[2]=66
    for i in 3..length {
        train[i] = (65 + i) as i8  # D=68 at i=3 ...
    }
    # Fix: should be C A B D E F G H I J K
    train[0]=67; train[1]=65; train[2]=66; train[3]=68; train[4]=69
    train[5]=70; train[6]=71; train[7]=72; train[8]=73; train[9]=74; train[10]=75

    let max_rot: i32 = (length - 1) * 2 - 1
    let mut num_found: i32 = 0
    while true {
        for i in 0..length {
            current[i] = train[i]
        }
        let mut want: i8 = 65
        let mut rotations: i32 = 0
        let mut pos: i32 = 0
        while pos < length - 1 {
            if current[pos] == want { break }
            if current[length - 1] == want && pos != length - 2 { break }
            let mut j: i32 = pos + 1
            while current[j] != want {
                j = j + 1
            }
            if j < length - 1 {
                reverse_range(current, j, length - 1)
                rotations = rotations + 1
            }
            reverse_range(current, pos, length - 1)
            rotations = rotations + 1
            want = want + 1
            pos = pos + 1
        }
        if rotations == max_rot {
            num_found = num_found + 1
            if num_found == stop { break }
        }
        if !next_perm(train, length) { break }
    }
    # print train as string
    for k in 0..length {
        printf("%c", train[k] as i32)
    }
    printf("\n")
    free(train); free(current)
    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; }

bool next_perm_ptr_i8_i32(int8_t* a, int32_t n);
int32_t reverse_range_ptr_i8_i32_i32(int8_t* a, int32_t L0, int32_t R0);
int32_t main(void);



bool next_perm_ptr_i8_i32(int8_t* a, int32_t n) {
    int32_t i = (n - 2);
    while ((i >= 0 && a[i] >= a[(i + 1)])) {
        i = (i - 1);
    }
    if (i < 0) {
        return 0;
    }
    int32_t j = (n - 1);
    while (a[j] <= a[i]) {
        j = (j - 1);
    }
    int8_t t = a[i];
    a[i] = a[j];
    a[j] = t;
    int32_t L = (i + 1);
    int32_t R = (n - 1);
    while (L < R) {
        int8_t u = a[L];
        a[L] = a[R];
        a[R] = u;
        L = (L + 1);
        R = (R - 1);
    }
    return 1;
}

int32_t reverse_range_ptr_i8_i32_i32(int8_t* a, int32_t L0, int32_t R0) {
    int32_t L = L0;
    int32_t R = R0;
    while (L < R) {
        int8_t t = a[L];
        a[L] = a[R];
        a[R] = t;
        L = (L + 1);
        R = (R - 1);
    }
    return 0;
}

int32_t main(void) {
    int32_t length = 11;
    int32_t stop = 2011;
    int8_t* train = (int8_t*)(calloc(((int64_t)(length)), 1));
    int8_t* current = (int8_t*)(calloc(((int64_t)(length)), 1));
    if ((train == NULL || current == NULL)) {
        return 1;
    }
    train[0] = 67;
    train[1] = 65;
    train[2] = 66;
    int32_t __flow_step_1 = 1;
    for (int32_t i = 3; (3 <= length) ? i < length : i > length; i += (3 <= length) ? 1 : -1) {
        train[i] = ((int8_t)((65 + i)));
    }
    train[0] = 67;
    train[1] = 65;
    train[2] = 66;
    train[3] = 68;
    train[4] = 69;
    train[5] = 70;
    train[6] = 71;
    train[7] = 72;
    train[8] = 73;
    train[9] = 74;
    train[10] = 75;
    int32_t max_rot = (((length - 1) * 2) - 1);
    int32_t num_found = 0;
    while (1) {
        int32_t __flow_step_2 = 1;
        for (int32_t i = 0; (0 <= length) ? i < length : i > length; i += (0 <= length) ? 1 : -1) {
            current[i] = train[i];
        }
        int8_t want = 65;
        int32_t rotations = 0;
        int32_t pos = 0;
        while (pos < (length - 1)) {
            if (current[pos] == want) {
                break;
            }
            if ((current[(length - 1)] == want && pos != (length - 2))) {
                break;
            }
            int32_t j = (pos + 1);
            while (current[j] != want) {
                j = (j + 1);
            }
            if (j < (length - 1)) {
                reverse_range_ptr_i8_i32_i32(current, j, (length - 1));
                rotations = (rotations + 1);
            }
            reverse_range_ptr_i8_i32_i32(current, pos, (length - 1));
            rotations = (rotations + 1);
            want = (want + 1);
            pos = (pos + 1);
        }
        if (rotations == max_rot) {
            num_found = (num_found + 1);
            if (num_found == stop) {
                break;
            }
        }
        if ((!(next_perm_ptr_i8_i32(train, length)))) {
            break;
        }
    }
    int32_t __flow_step_3 = 1;
    for (int32_t k = 0; (0 <= length) ? k < length : k > length; k += (0 <= length) ? 1 : -1) {
        printf("%c", ((int32_t)(train[k])));
    }
    printf("\n");
    free(train);
    free(current);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%c\00") {addr_space = 0 : i32} : !llvm.array<3 x i8>
  llvm.mlir.global internal constant @str_1("\n\00") {addr_space = 0 : i32} : !llvm.array<2 x i8>
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @next_perm(%arg0: !llvm.ptr, %arg1: i32) -> i1 {
    %0 = arith.constant 2 : i32
    %1 = arith.subi %arg1, %0 : i32
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i32 : (i64) -> !llvm.ptr
    llvm.store %1, %3 : i32, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %4 = llvm.load %3 : !llvm.ptr -> i32
    %5 = arith.constant 0 : i32
    %6 = arith.cmpi sge, %4, %5 : i32
    %7 = scf.if %6 -> (i1) {
      %9 = llvm.load %3 : !llvm.ptr -> i32
      %10 = arith.extsi %9 : i32 to i64
      %11 = llvm.getelementptr %arg0[%10] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %8 = llvm.load %11 : !llvm.ptr -> i8
      %13 = llvm.load %3 : !llvm.ptr -> i32
      %14 = arith.constant 1 : i32
      %15 = arith.addi %13, %14 : i32
      %16 = arith.extsi %15 : i32 to i64
      %17 = llvm.getelementptr %arg0[%16] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %12 = llvm.load %17 : !llvm.ptr -> i8
      %19 = arith.extsi %8 : i8 to i32
      %20 = arith.extsi %12 : i8 to i32
      %18 = arith.cmpi sge, %19, %20 : i32
      scf.yield %18 : i1
    } else {
      %21 = arith.constant false
      scf.yield %21 : i1
    }
    cf.cond_br %7, ^bb1, ^bb2
    ^bb1:
      %22 = llvm.load %3 : !llvm.ptr -> i32
      %23 = arith.constant 1 : i32
      %24 = arith.subi %22, %23 : i32
      llvm.store %24, %3 : i32, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %25 = llvm.load %3 : !llvm.ptr -> i32
    %26 = arith.constant 0 : i32
    %27 = arith.cmpi slt, %25, %26 : i32
    cf.cond_br %27, ^bb3, ^bb4
    ^bb3:
      %28 = arith.constant 0 : i1
      func.return %28 : i1
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %29 = arith.constant 1 : i32
    %30 = arith.subi %arg1, %29 : i32
    %31 = llvm.mlir.constant(1 : i64) : i64
    %32 = llvm.alloca %31 x i32 : (i64) -> !llvm.ptr
    llvm.store %30, %32 : i32, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %34 = llvm.load %32 : !llvm.ptr -> i32
    %35 = arith.extsi %34 : i32 to i64
    %36 = llvm.getelementptr %arg0[%35] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    %33 = llvm.load %36 : !llvm.ptr -> i8
    %38 = llvm.load %3 : !llvm.ptr -> i32
    %39 = arith.extsi %38 : i32 to i64
    %40 = llvm.getelementptr %arg0[%39] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    %37 = llvm.load %40 : !llvm.ptr -> i8
    %42 = arith.extsi %33 : i8 to i32
    %43 = arith.extsi %37 : i8 to i32
    %41 = arith.cmpi sle, %42, %43 : i32
    cf.cond_br %41, ^bb7, ^bb8
    ^bb7:
      %44 = llvm.load %32 : !llvm.ptr -> i32
      %45 = arith.constant 1 : i32
      %46 = arith.subi %44, %45 : i32
      llvm.store %46, %32 : i32, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %48 = llvm.load %3 : !llvm.ptr -> i32
    %49 = arith.extsi %48 : i32 to i64
    %50 = llvm.getelementptr %arg0[%49] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    %47 = llvm.load %50 : !llvm.ptr -> i8
    %52 = llvm.load %32 : !llvm.ptr -> i32
    %53 = arith.extsi %52 : i32 to i64
    %54 = llvm.getelementptr %arg0[%53] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    %51 = llvm.load %54 : !llvm.ptr -> i8
    %55 = llvm.load %3 : !llvm.ptr -> i32
    %56 = arith.extsi %55 : i32 to i64
    %57 = llvm.getelementptr %arg0[%56] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %51, %57 : i8, !llvm.ptr
    %58 = llvm.load %32 : !llvm.ptr -> i32
    %59 = arith.extsi %58 : i32 to i64
    %60 = llvm.getelementptr %arg0[%59] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %47, %60 : i8, !llvm.ptr
    %61 = llvm.load %3 : !llvm.ptr -> i32
    %62 = arith.constant 1 : i32
    %63 = arith.addi %61, %62 : i32
    %64 = llvm.mlir.constant(1 : i64) : i64
    %65 = llvm.alloca %64 x i32 : (i64) -> !llvm.ptr
    llvm.store %63, %65 : i32, !llvm.ptr
    %66 = arith.constant 1 : i32
    %67 = arith.subi %arg1, %66 : i32
    %68 = llvm.mlir.constant(1 : i64) : i64
    %69 = llvm.alloca %68 x i32 : (i64) -> !llvm.ptr
    llvm.store %67, %69 : i32, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %70 = llvm.load %65 : !llvm.ptr -> i32
    %71 = llvm.load %69 : !llvm.ptr -> i32
    %72 = arith.cmpi slt, %70, %71 : i32
    cf.cond_br %72, ^bb10, ^bb11
    ^bb10:
      %74 = llvm.load %65 : !llvm.ptr -> i32
      %75 = arith.extsi %74 : i32 to i64
      %76 = llvm.getelementptr %arg0[%75] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %73 = llvm.load %76 : !llvm.ptr -> i8
      %78 = llvm.load %69 : !llvm.ptr -> i32
      %79 = arith.extsi %78 : i32 to i64
      %80 = llvm.getelementptr %arg0[%79] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %77 = llvm.load %80 : !llvm.ptr -> i8
      %81 = llvm.load %65 : !llvm.ptr -> i32
      %82 = arith.extsi %81 : i32 to i64
      %83 = llvm.getelementptr %arg0[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %77, %83 : i8, !llvm.ptr
      %84 = llvm.load %69 : !llvm.ptr -> i32
      %85 = arith.extsi %84 : i32 to i64
      %86 = llvm.getelementptr %arg0[%85] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %73, %86 : i8, !llvm.ptr
      %87 = llvm.load %65 : !llvm.ptr -> i32
      %88 = arith.constant 1 : i32
      %89 = arith.addi %87, %88 : i32
      llvm.store %89, %65 : i32, !llvm.ptr
      %90 = llvm.load %69 : !llvm.ptr -> i32
      %91 = arith.constant 1 : i32
      %92 = arith.subi %90, %91 : i32
      llvm.store %92, %69 : i32, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %93 = arith.constant 1 : i1
    func.return %93 : i1
  }
  func.func @reverse_range(%arg0: !llvm.ptr, %arg1: i32, %arg2: i32) -> i32 {
    %94 = llvm.mlir.constant(1 : i64) : i64
    %95 = llvm.alloca %94 x i32 : (i64) -> !llvm.ptr
    llvm.store %arg1, %95 : i32, !llvm.ptr
    %96 = llvm.mlir.constant(1 : i64) : i64
    %97 = llvm.alloca %96 x i32 : (i64) -> !llvm.ptr
    llvm.store %arg2, %97 : i32, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %98 = llvm.load %95 : !llvm.ptr -> i32
    %99 = llvm.load %97 : !llvm.ptr -> i32
    %100 = arith.cmpi slt, %98, %99 : i32
    cf.cond_br %100, ^bb13, ^bb14
    ^bb13:
      %102 = llvm.load %95 : !llvm.ptr -> i32
      %103 = arith.extsi %102 : i32 to i64
      %104 = llvm.getelementptr %arg0[%103] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %101 = llvm.load %104 : !llvm.ptr -> i8
      %106 = llvm.load %97 : !llvm.ptr -> i32
      %107 = arith.extsi %106 : i32 to i64
      %108 = llvm.getelementptr %arg0[%107] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %105 = llvm.load %108 : !llvm.ptr -> i8
      %109 = llvm.load %95 : !llvm.ptr -> i32
      %110 = arith.extsi %109 : i32 to i64
      %111 = llvm.getelementptr %arg0[%110] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %105, %111 : i8, !llvm.ptr
      %112 = llvm.load %97 : !llvm.ptr -> i32
      %113 = arith.extsi %112 : i32 to i64
      %114 = llvm.getelementptr %arg0[%113] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %101, %114 : i8, !llvm.ptr
      %115 = llvm.load %95 : !llvm.ptr -> i32
      %116 = arith.constant 1 : i32
      %117 = arith.addi %115, %116 : i32
      llvm.store %117, %95 : i32, !llvm.ptr
      %118 = llvm.load %97 : !llvm.ptr -> i32
      %119 = arith.constant 1 : i32
      %120 = arith.subi %118, %119 : i32
      llvm.store %120, %97 : i32, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %121 = arith.constant 0 : i32
    func.return %121 : i32
  }
  func.func @main() -> i32 {
    %122 = arith.constant 11 : i32
    %123 = arith.constant 2011 : i32
    %125 = arith.extsi %122 : i32 to i64
    %126 = arith.constant 1 : i32
    %127 = arith.extsi %126 : i32 to i64
    %124 = func.call @calloc(%125, %127) : (i64, i64) -> !llvm.ptr
    %129 = arith.extsi %122 : i32 to i64
    %130 = arith.constant 1 : i32
    %131 = arith.extsi %130 : i32 to i64
    %128 = func.call @calloc(%129, %131) : (i64, i64) -> !llvm.ptr
    %132 = llvm.mlir.zero : !llvm.ptr
    %133 = llvm.icmp "eq" %124, %132 : !llvm.ptr
    %134 = scf.if %133 -> (i1) {
      %135 = arith.constant true
      scf.yield %135 : i1
    } else {
      %136 = llvm.mlir.zero : !llvm.ptr
      %137 = llvm.icmp "eq" %128, %136 : !llvm.ptr
      scf.yield %137 : i1
    }
    cf.cond_br %134, ^bb15, ^bb16
    ^bb15:
      %138 = arith.constant 1 : i32
      func.return %138 : i32
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %139 = arith.constant 67 : i32
    %140 = arith.constant 0 : i32
    %141 = arith.trunci %139 : i32 to i8
    %142 = arith.extsi %140 : i32 to i64
    %143 = llvm.getelementptr %124[%142] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %141, %143 : i8, !llvm.ptr
    %144 = arith.constant 65 : i32
    %145 = arith.constant 1 : i32
    %146 = arith.trunci %144 : i32 to i8
    %147 = arith.extsi %145 : i32 to i64
    %148 = llvm.getelementptr %124[%147] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %146, %148 : i8, !llvm.ptr
    %149 = arith.constant 66 : i32
    %150 = arith.constant 2 : i32
    %151 = arith.trunci %149 : i32 to i8
    %152 = arith.extsi %150 : i32 to i64
    %153 = llvm.getelementptr %124[%152] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %151, %153 : i8, !llvm.ptr
    %154 = arith.constant 3 : i32
    %155 = arith.index_cast %154 : i32 to index
    %156 = arith.index_cast %122 : i32 to index
    %158 = arith.constant 1 : index
    %159 = arith.constant -1 : index
    %160 = arith.cmpi sle, %155, %156 : index
    %157 = arith.select %160, %158, %159 : index
    cf.br ^bb18(%155 : index)
    ^bb18(%161: index):
    %162 = arith.cmpi slt, %161, %156 : index
    %163 = arith.cmpi sgt, %161, %156 : index
    %164 = arith.select %160, %162, %163 : i1
    cf.cond_br %164, ^bb19(%161 : index), ^bb20(%161 : index)
    ^bb19(%165: index):
      %166 = arith.constant 65 : i32
      %168 = arith.index_cast %165 : index to i32
      %167 = arith.addi %166, %168 : i32
      %169 = arith.trunci %167 : i32 to i8
      %170 = arith.index_cast %165 : index to i64
      %171 = llvm.getelementptr %124[%170] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %169, %171 : i8, !llvm.ptr
      %172 = arith.addi %165, %157 : index
      cf.br ^bb18(%172 : index)
    ^bb20(%173: index):
    %174 = arith.constant 67 : i32
    %175 = arith.constant 0 : i32
    %176 = arith.trunci %174 : i32 to i8
    %177 = arith.extsi %175 : i32 to i64
    %178 = llvm.getelementptr %124[%177] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %176, %178 : i8, !llvm.ptr
    %179 = arith.constant 65 : i32
    %180 = arith.constant 1 : i32
    %181 = arith.trunci %179 : i32 to i8
    %182 = arith.extsi %180 : i32 to i64
    %183 = llvm.getelementptr %124[%182] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %181, %183 : i8, !llvm.ptr
    %184 = arith.constant 66 : i32
    %185 = arith.constant 2 : i32
    %186 = arith.trunci %184 : i32 to i8
    %187 = arith.extsi %185 : i32 to i64
    %188 = llvm.getelementptr %124[%187] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %186, %188 : i8, !llvm.ptr
    %189 = arith.constant 68 : i32
    %190 = arith.constant 3 : i32
    %191 = arith.trunci %189 : i32 to i8
    %192 = arith.extsi %190 : i32 to i64
    %193 = llvm.getelementptr %124[%192] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %191, %193 : i8, !llvm.ptr
    %194 = arith.constant 69 : i32
    %195 = arith.constant 4 : i32
    %196 = arith.trunci %194 : i32 to i8
    %197 = arith.extsi %195 : i32 to i64
    %198 = llvm.getelementptr %124[%197] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %196, %198 : i8, !llvm.ptr
    %199 = arith.constant 70 : i32
    %200 = arith.constant 5 : i32
    %201 = arith.trunci %199 : i32 to i8
    %202 = arith.extsi %200 : i32 to i64
    %203 = llvm.getelementptr %124[%202] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %201, %203 : i8, !llvm.ptr
    %204 = arith.constant 71 : i32
    %205 = arith.constant 6 : i32
    %206 = arith.trunci %204 : i32 to i8
    %207 = arith.extsi %205 : i32 to i64
    %208 = llvm.getelementptr %124[%207] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %206, %208 : i8, !llvm.ptr
    %209 = arith.constant 72 : i32
    %210 = arith.constant 7 : i32
    %211 = arith.trunci %209 : i32 to i8
    %212 = arith.extsi %210 : i32 to i64
    %213 = llvm.getelementptr %124[%212] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %211, %213 : i8, !llvm.ptr
    %214 = arith.constant 73 : i32
    %215 = arith.constant 8 : i32
    %216 = arith.trunci %214 : i32 to i8
    %217 = arith.extsi %215 : i32 to i64
    %218 = llvm.getelementptr %124[%217] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %216, %218 : i8, !llvm.ptr
    %219 = arith.constant 74 : i32
    %220 = arith.constant 9 : i32
    %221 = arith.trunci %219 : i32 to i8
    %222 = arith.extsi %220 : i32 to i64
    %223 = llvm.getelementptr %124[%222] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %221, %223 : i8, !llvm.ptr
    %224 = arith.constant 75 : i32
    %225 = arith.constant 10 : i32
    %226 = arith.trunci %224 : i32 to i8
    %227 = arith.extsi %225 : i32 to i64
    %228 = llvm.getelementptr %124[%227] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %226, %228 : i8, !llvm.ptr
    %229 = arith.constant 1 : i32
    %230 = arith.subi %122, %229 : i32
    %231 = arith.constant 2 : i32
    %232 = arith.muli %230, %231 : i32
    %233 = arith.constant 1 : i32
    %234 = arith.subi %232, %233 : i32
    %235 = arith.constant 0 : i32
    %236 = llvm.mlir.constant(1 : i64) : i64
    %237 = llvm.alloca %236 x i32 : (i64) -> !llvm.ptr
    llvm.store %235, %237 : i32, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %238 = arith.constant 1 : i1
    cf.cond_br %238, ^bb22, ^bb23
    ^bb22:
      %239 = arith.constant 0 : i32
      %240 = arith.index_cast %239 : i32 to index
      %241 = arith.index_cast %122 : i32 to index
      %243 = arith.constant 1 : index
      %244 = arith.constant -1 : index
      %245 = arith.cmpi sle, %240, %241 : index
      %242 = arith.select %245, %243, %244 : index
      cf.br ^bb24(%240 : index)
      ^bb24(%246: index):
      %247 = arith.cmpi slt, %246, %241 : index
      %248 = arith.cmpi sgt, %246, %241 : index
      %249 = arith.select %245, %247, %248 : i1
      cf.cond_br %249, ^bb25(%246 : index), ^bb26(%246 : index)
      ^bb25(%250: index):
        %252 = arith.index_cast %250 : index to i64
        %253 = llvm.getelementptr %124[%252] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        %251 = llvm.load %253 : !llvm.ptr -> i8
        %254 = arith.index_cast %250 : index to i64
        %255 = llvm.getelementptr %128[%254] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        llvm.store %251, %255 : i8, !llvm.ptr
        %256 = arith.addi %250, %242 : index
        cf.br ^bb24(%256 : index)
      ^bb26(%257: index):
      %258 = arith.constant 65 : i32
      %259 = arith.trunci %258 : i32 to i8
      %260 = llvm.mlir.constant(1 : i64) : i64
      %261 = llvm.alloca %260 x i8 : (i64) -> !llvm.ptr
      llvm.store %259, %261 : i8, !llvm.ptr
      %262 = arith.constant 0 : i32
      %263 = llvm.mlir.constant(1 : i64) : i64
      %264 = llvm.alloca %263 x i32 : (i64) -> !llvm.ptr
      llvm.store %262, %264 : i32, !llvm.ptr
      %265 = arith.constant 0 : i32
      %266 = llvm.mlir.constant(1 : i64) : i64
      %267 = llvm.alloca %266 x i32 : (i64) -> !llvm.ptr
      llvm.store %265, %267 : i32, !llvm.ptr
      cf.br ^bb27
      ^bb27:
      %268 = llvm.load %267 : !llvm.ptr -> i32
      %269 = arith.constant 1 : i32
      %270 = arith.subi %122, %269 : i32
      %271 = arith.cmpi slt, %268, %270 : i32
      cf.cond_br %271, ^bb28, ^bb29
      ^bb28:
        %273 = llvm.load %267 : !llvm.ptr -> i32
        %274 = arith.extsi %273 : i32 to i64
        %275 = llvm.getelementptr %128[%274] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        %272 = llvm.load %275 : !llvm.ptr -> i8
        %276 = llvm.load %261 : !llvm.ptr -> i8
        %278 = arith.extsi %272 : i8 to i32
        %279 = arith.extsi %276 : i8 to i32
        %277 = arith.cmpi eq, %278, %279 : i32
        cf.cond_br %277, ^bb30, ^bb31
        ^bb30:
          cf.br ^bb29
        ^bb31:
          cf.br ^bb32
        ^bb32:
        %281 = arith.constant 1 : i32
        %282 = arith.subi %122, %281 : i32
        %283 = arith.extsi %282 : i32 to i64
        %284 = llvm.getelementptr %128[%283] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        %280 = llvm.load %284 : !llvm.ptr -> i8
        %285 = llvm.load %261 : !llvm.ptr -> i8
        %287 = arith.extsi %280 : i8 to i32
        %288 = arith.extsi %285 : i8 to i32
        %286 = arith.cmpi eq, %287, %288 : i32
        %289 = scf.if %286 -> (i1) {
          %290 = llvm.load %267 : !llvm.ptr -> i32
          %291 = arith.constant 2 : i32
          %292 = arith.subi %122, %291 : i32
          %293 = arith.cmpi ne, %290, %292 : i32
          scf.yield %293 : i1
        } else {
          %294 = arith.constant false
          scf.yield %294 : i1
        }
        cf.cond_br %289, ^bb33, ^bb34
        ^bb33:
          cf.br ^bb29
        ^bb34:
          cf.br ^bb35
        ^bb35:
        %295 = llvm.load %267 : !llvm.ptr -> i32
        %296 = arith.constant 1 : i32
        %297 = arith.addi %295, %296 : i32
        %298 = llvm.mlir.constant(1 : i64) : i64
        %299 = llvm.alloca %298 x i32 : (i64) -> !llvm.ptr
        llvm.store %297, %299 : i32, !llvm.ptr
        cf.br ^bb36
        ^bb36:
        %301 = llvm.load %299 : !llvm.ptr -> i32
        %302 = arith.extsi %301 : i32 to i64
        %303 = llvm.getelementptr %128[%302] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        %300 = llvm.load %303 : !llvm.ptr -> i8
        %304 = llvm.load %261 : !llvm.ptr -> i8
        %306 = arith.extsi %300 : i8 to i32
        %307 = arith.extsi %304 : i8 to i32
        %305 = arith.cmpi ne, %306, %307 : i32
        cf.cond_br %305, ^bb37, ^bb38
        ^bb37:
          %308 = llvm.load %299 : !llvm.ptr -> i32
          %309 = arith.constant 1 : i32
          %310 = arith.addi %308, %309 : i32
          llvm.store %310, %299 : i32, !llvm.ptr
          cf.br ^bb36
        ^bb38:
        %311 = llvm.load %299 : !llvm.ptr -> i32
        %312 = arith.constant 1 : i32
        %313 = arith.subi %122, %312 : i32
        %314 = arith.cmpi slt, %311, %313 : i32
        cf.cond_br %314, ^bb39, ^bb40
        ^bb39:
          %316 = llvm.load %299 : !llvm.ptr -> i32
          %317 = arith.constant 1 : i32
          %318 = arith.subi %122, %317 : i32
          %315 = func.call @reverse_range(%128, %316, %318) : (!llvm.ptr, i32, i32) -> i32
          %319 = llvm.load %264 : !llvm.ptr -> i32
          %320 = arith.constant 1 : i32
          %321 = arith.addi %319, %320 : i32
          llvm.store %321, %264 : i32, !llvm.ptr
          cf.br ^bb41
        ^bb40:
          cf.br ^bb41
        ^bb41:
        %323 = llvm.load %267 : !llvm.ptr -> i32
        %324 = arith.constant 1 : i32
        %325 = arith.subi %122, %324 : i32
        %322 = func.call @reverse_range(%128, %323, %325) : (!llvm.ptr, i32, i32) -> i32
        %326 = llvm.load %264 : !llvm.ptr -> i32
        %327 = arith.constant 1 : i32
        %328 = arith.addi %326, %327 : i32
        llvm.store %328, %264 : i32, !llvm.ptr
        %329 = llvm.load %261 : !llvm.ptr -> i8
        %330 = arith.constant 1 : i32
        %332 = arith.extsi %329 : i8 to i32
        %331 = arith.addi %332, %330 : i32
        %333 = arith.trunci %331 : i32 to i8
        llvm.store %333, %261 : i8, !llvm.ptr
        %334 = llvm.load %267 : !llvm.ptr -> i32
        %335 = arith.constant 1 : i32
        %336 = arith.addi %334, %335 : i32
        llvm.store %336, %267 : i32, !llvm.ptr
        cf.br ^bb27
      ^bb29:
      %337 = llvm.load %264 : !llvm.ptr -> i32
      %338 = arith.cmpi eq, %337, %234 : i32
      cf.cond_br %338, ^bb42, ^bb43
      ^bb42:
        %339 = llvm.load %237 : !llvm.ptr -> i32
        %340 = arith.constant 1 : i32
        %341 = arith.addi %339, %340 : i32
        llvm.store %341, %237 : i32, !llvm.ptr
        %342 = llvm.load %237 : !llvm.ptr -> i32
        %343 = arith.cmpi eq, %342, %123 : i32
        cf.cond_br %343, ^bb45, ^bb46
        ^bb45:
          cf.br ^bb23
        ^bb46:
          cf.br ^bb47
        ^bb47:
        cf.br ^bb44
      ^bb43:
        cf.br ^bb44
      ^bb44:
      %344 = func.call @next_perm(%124, %122) : (!llvm.ptr, i32) -> i1
      %346 = arith.constant 1 : i1
      %345 = arith.xori %344, %346 : i1
      cf.cond_br %345, ^bb48, ^bb49
      ^bb48:
        cf.br ^bb23
      ^bb49:
        cf.br ^bb50
      ^bb50:
      cf.br ^bb21
    ^bb23:
    %348 = arith.constant 0 : i32
    %349 = arith.index_cast %348 : i32 to index
    %350 = arith.index_cast %122 : i32 to index
    %352 = arith.constant 1 : index
    %353 = arith.constant -1 : index
    %354 = arith.cmpi sle, %349, %350 : index
    %351 = arith.select %354, %352, %353 : index
    cf.br ^bb51(%349 : index)
    ^bb51(%355: index):
    %356 = arith.cmpi slt, %355, %350 : index
    %357 = arith.cmpi sgt, %355, %350 : index
    %358 = arith.select %354, %356, %357 : i1
    cf.cond_br %358, ^bb52(%355 : index), ^bb53(%355 : index)
    ^bb52(%359: index):
      %360 = llvm.mlir.addressof @str_0 : !llvm.ptr
      %362 = arith.index_cast %359 : index to i64
      %363 = llvm.getelementptr %124[%362] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %361 = llvm.load %363 : !llvm.ptr -> i8
      %364 = arith.extsi %361 : i8 to i32
      %365 = llvm.call @printf(%360, %364) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i32) -> i32
      %366 = arith.addi %359, %351 : index
      cf.br ^bb51(%366 : index)
    ^bb53(%367: index):
    %368 = llvm.mlir.addressof @str_1 : !llvm.ptr
    %369 = llvm.call @printf(%368) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr) -> i32
    func.call @free(%124) : (!llvm.ptr) -> ()
    func.call @free(%128) : (!llvm.ptr) -> ()
    %372 = arith.constant 0 : i32
    func.return %372 : i32
  }
}