Problem 083

Minimal path sum with four-way moves (Dijkstra).

Answer425185
Output425185
StatusPASS
Native helperno
Runtime30 ms
Peak memory1232 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n^2)
Space complexityO(n)O(n^2)
ApproachFlow solutionBottom-up DP
VerdictOptimal

Flow source

# Project Euler 083
# Minimal path sum with four-way moves (Dijkstra).

extern {
    function fopen(path: string, mode: string) -> ptr<void>
    function fgetc(f: ptr<void>) -> i32
    function fclose(f: ptr<void>) -> i32
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
}

function read_int(f: ptr<void>) -> i32 {
    let mut c: i32 = fgetc(f)
    while c >= 0 && (c < 48 || c > 57) {
        c = fgetc(f)
    }
    if c < 0 { return -1 }
    let mut v: i32 = 0
    while c >= 48 && c <= 57 {
        v = v * 10 + (c - 48)
        c = fgetc(f)
    }
    return v
}

function main() -> i32 {
    let n: i32 = 80
    let N: i32 = n * n
    let a: ptr<i64> = calloc(N as i64, 8)
    let dist: ptr<i64> = calloc(N as i64, 8)
    let done: ptr<i8> = calloc(N as i64, 1)
    if a == null || dist == null || done == null { return 1 }

    let f: ptr<void> = fopen("data/p083.txt", "r")
    if f == null {
        printf("failed to read data/p083.txt\n")
        return 1
    }
    let mut i: i32 = 0
    while i < N {
        a[i] = read_int(f) as i64
        dist[i] = 1000000000000
        i = i + 1
    }
    fclose(f)

    dist[0] = a[0]
    let mut iter: i32 = 0
    while iter < N {
        let mut u: i32 = -1
        let mut best: i64 = 1000000000000
        let mut v: i32 = 0
        while v < N {
            if done[v] == 0 && dist[v] < best {
                best = dist[v]
                u = v
            }
            v = v + 1
        }
        if u < 0 { break }
        done[u] = 1
        let r: i32 = u / n
        let c: i32 = u % n
        # neighbors
        let mut k: i32 = 0
        while k < 4 {
            let mut nr: i32 = r
            let mut nc: i32 = c
            match k {
                0 => { nr = r - 1 }
                1 => { nr = r + 1 }
                2 => { nc = c - 1 }
                _ => { nc = c + 1 }
            }
            if nr >= 0 && nr < n && nc >= 0 && nc < n {
                let w: i32 = nr * n + nc
                let nd: i64 = dist[u] + a[w]
                if nd < dist[w] {
                    dist[w] = nd
                }
            }
            k = k + 1
        }
        iter = iter + 1
    }
    printf("%lld\n", dist[N - 1])
    free(a)
    free(dist)
    free(done)
    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; }

int32_t read_int_ptr_void(void* f);
int32_t main(void);






int32_t read_int_ptr_void(void* f) {
    int32_t c = fgetc(f);
    while ((c >= 0 && (c < 48 || c > 57))) {
        c = fgetc(f);
    }
    if (c < 0) {
        return (-1);
    }
    int32_t v = 0;
    while ((c >= 48 && c <= 57)) {
        v = ((v * 10) + (c - 48));
        c = fgetc(f);
    }
    return v;
}

int32_t main(void) {
    int32_t n = 80;
    int32_t N = (n * n);
    int64_t* a = (int64_t*)(calloc(((int64_t)(N)), 8));
    int64_t* dist = (int64_t*)(calloc(((int64_t)(N)), 8));
    int8_t* done = (int8_t*)(calloc(((int64_t)(N)), 1));
    if (((a == NULL || dist == NULL) || done == NULL)) {
        return 1;
    }
    void* f = (void*)(fopen("data/p083.txt", "r"));
    if (f == NULL) {
        printf("failed to read data/p083.txt\n");
        return 1;
    }
    int32_t i = 0;
    while (i < N) {
        a[i] = ((int64_t)(read_int_ptr_void(f)));
        dist[i] = 1000000000000;
        i = (i + 1);
    }
    fclose(f);
    dist[0] = a[0];
    int32_t iter = 0;
    while (iter < N) {
        int32_t u = (-1);
        int64_t best = 1000000000000;
        int32_t v = 0;
        while (v < N) {
            if ((done[v] == 0 && dist[v] < best)) {
                best = dist[v];
                u = v;
            }
            v = (v + 1);
        }
        if (u < 0) {
            break;
        }
        done[u] = 1;
        int32_t r = FLOW_CHECKED_DIV((u), (n));
        int32_t c = FLOW_CHECKED_MOD((u), (n));
        int32_t k = 0;
        while (k < 4) {
            int32_t nr = r;
            int32_t nc = c;
            { // match block
                if ((k) == 0) {
                    nr = (r - 1);
                } else if ((k) == 1) {
                    nr = (r + 1);
                } else if ((k) == 2) {
                    nc = (c - 1);
                } else { // exhaustive
                    nc = (c + 1);
                }
            } // end match
            if ((((nr >= 0 && nr < n) && nc >= 0) && nc < n)) {
                int32_t w = ((nr * n) + nc);
                int64_t nd = (dist[u] + a[w]);
                if (nd < dist[w]) {
                    dist[w] = nd;
                }
            }
            k = (k + 1);
        }
        iter = (iter + 1);
    }
    printf("%lld\n", dist[(N - 1)]);
    free(a);
    free(dist);
    free(done);
    return 0;
}

Generated MLIR

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