Problem 011

Greatest product of four adjacent numbers in the 20x20 grid. Uses Flow for-ranges throughout for clean iteration.

Answer70600674
Output70600674
StatusPASS
Native helperno
Runtime0 ms
Peak memory1104 KB
Time complexityO(n^3) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n)
Space complexityO(n)O(1)
ApproachFlow solutionLinear scan of grid directions
VerdictSuboptimal

Flow source

# Project Euler 011
# Greatest product of four adjacent numbers in the 20x20 grid.
#
# Uses Flow for-ranges throughout for clean iteration.

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 at(grid: ptr<i32>, n: i32, r: i32, c: i32) -> i64 {
    return grid[r * n + c] as i64
}

function solve(grid: ptr<i32>, n: i32, k: i32) -> i64 {
    let mut best: i64 = 0
    for r in 0..n {
        for c in 0..n {
            if c + k <= n {
                let mut p: i64 = 1
                for i in 0..k {
                    p = p * at(grid, n, r, c + i)
                }
                if p > best { best = p }
            }
            if r + k <= n {
                let mut p: i64 = 1
                for i in 0..k {
                    p = p * at(grid, n, r + i, c)
                }
                if p > best { best = p }
            }
            if r + k <= n && c + k <= n {
                let mut p: i64 = 1
                for i in 0..k {
                    p = p * at(grid, n, r + i, c + i)
                }
                if p > best { best = p }
            }
            if r + k <= n && c - k + 1 >= 0 {
                let mut p: i64 = 1
                for i in 0..k {
                    p = p * at(grid, n, r + i, c - i)
                }
                if p > best { best = p }
            }
        }
    }
    return best
}

function main() -> i32 {
    let n: i32 = 20
    let grid: ptr<i32> = calloc((n * n) as i64, 4)
    if grid == null {
        return 1
    }
    let f: ptr<void> = fopen("data/p011.txt", "r")
    if f == null {
        printf("failed to read data/p011.txt\n")
        free(grid)
        return 1
    }
    for i in 0..(n * n) {
        let v: i32 = read_int(f)
        if v < 0 {
            break
        }
        grid[i] = v
    }
    fclose(f)
    printf("%lld\n", solve(grid, n, 4))
    free(grid)
    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);
int64_t at_ptr_i32_i32_i32_i32(int32_t* grid, int32_t n, int32_t r, int32_t c);
int64_t solve_ptr_i32_i32_i32(int32_t* grid, int32_t n, int32_t k);
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;
}

int64_t at_ptr_i32_i32_i32_i32(int32_t* grid, int32_t n, int32_t r, int32_t c) {
    return ((int64_t)(grid[((r * n) + c)]));
}

int64_t solve_ptr_i32_i32_i32(int32_t* grid, int32_t n, int32_t k) {
    int64_t best = 0;
    int32_t __flow_step_1 = 1;
    for (int32_t r = 0; (0 <= n) ? r < n : r > n; r += (0 <= n) ? 1 : -1) {
        int32_t __flow_step_2 = 1;
        for (int32_t c = 0; (0 <= n) ? c < n : c > n; c += (0 <= n) ? 1 : -1) {
            if ((c + k) <= n) {
                int64_t p = 1;
                int32_t __flow_step_3 = 1;
                for (int32_t i = 0; (0 <= k) ? i < k : i > k; i += (0 <= k) ? 1 : -1) {
                    p = (p * at_ptr_i32_i32_i32_i32(grid, n, r, (c + i)));
                }
                if (p > best) {
                    best = p;
                }
            }
            if ((r + k) <= n) {
                int64_t p = 1;
                int32_t __flow_step_4 = 1;
                for (int32_t i = 0; (0 <= k) ? i < k : i > k; i += (0 <= k) ? 1 : -1) {
                    p = (p * at_ptr_i32_i32_i32_i32(grid, n, (r + i), c));
                }
                if (p > best) {
                    best = p;
                }
            }
            if (((r + k) <= n && (c + k) <= n)) {
                int64_t p = 1;
                int32_t __flow_step_5 = 1;
                for (int32_t i = 0; (0 <= k) ? i < k : i > k; i += (0 <= k) ? 1 : -1) {
                    p = (p * at_ptr_i32_i32_i32_i32(grid, n, (r + i), (c + i)));
                }
                if (p > best) {
                    best = p;
                }
            }
            if (((r + k) <= n && ((c - k) + 1) >= 0)) {
                int64_t p = 1;
                int32_t __flow_step_6 = 1;
                for (int32_t i = 0; (0 <= k) ? i < k : i > k; i += (0 <= k) ? 1 : -1) {
                    p = (p * at_ptr_i32_i32_i32_i32(grid, n, (r + i), (c - i)));
                }
                if (p > best) {
                    best = p;
                }
            }
        }
    }
    return best;
}

int32_t main(void) {
    int32_t n = 20;
    int32_t* grid = (int32_t*)(calloc(((int64_t)((n * n))), 4));
    if (grid == NULL) {
        return 1;
    }
    void* f = (void*)(fopen("data/p011.txt", "r"));
    if (f == NULL) {
        printf("failed to read data/p011.txt\n");
        free(grid);
        return 1;
    }
    int32_t __flow_step_7 = 1;
    for (int32_t i = 0; (0 <= (n * n)) ? i < (n * n) : i > (n * n); i += (0 <= (n * n)) ? 1 : -1) {
        int32_t v = read_int_ptr_void(f);
        if (v < 0) {
            break;
        }
        grid[i] = v;
    }
    fclose(f);
    printf("%lld\n", solve_ptr_i32_i32_i32(grid, n, 4));
    free(grid);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("data/p011.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/p011.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 @at(%arg0: !llvm.ptr, %arg1: i32, %arg2: i32, %arg3: i32) -> i64 {
    %44 = arith.muli %arg2, %arg1 : i32
    %45 = arith.addi %44, %arg3 : i32
    %46 = arith.extsi %45 : i32 to i64
    %47 = llvm.getelementptr %arg0[%46] : (!llvm.ptr, i64) -> !llvm.ptr, i32
    %43 = llvm.load %47 : !llvm.ptr -> i32
    %48 = arith.extsi %43 : i32 to i64
    func.return %48 : i64
  }
  func.func @solve(%arg0: !llvm.ptr, %arg1: i32, %arg2: i32) -> i64 {
    %49 = arith.constant 0 : i32
    %50 = arith.extsi %49 : i32 to i64
    %51 = llvm.mlir.constant(1 : i64) : i64
    %52 = llvm.alloca %51 x i64 : (i64) -> !llvm.ptr
    llvm.store %50, %52 : i64, !llvm.ptr
    %53 = arith.constant 0 : i32
    %54 = arith.index_cast %53 : i32 to index
    %55 = arith.index_cast %arg1 : i32 to index
    %57 = arith.constant 1 : index
    %58 = arith.constant -1 : index
    %59 = arith.cmpi sle, %54, %55 : index
    %56 = arith.select %59, %57, %58 : index
    cf.br ^bb9(%54 : index)
    ^bb9(%60: index):
    %61 = arith.cmpi slt, %60, %55 : index
    %62 = arith.cmpi sgt, %60, %55 : index
    %63 = arith.select %59, %61, %62 : i1
    cf.cond_br %63, ^bb10(%60 : index), ^bb11(%60 : index)
    ^bb10(%64: index):
      %65 = arith.constant 0 : i32
      %66 = arith.index_cast %65 : i32 to index
      %67 = arith.index_cast %arg1 : i32 to index
      %69 = arith.constant 1 : index
      %70 = arith.constant -1 : index
      %71 = arith.cmpi sle, %66, %67 : index
      %68 = arith.select %71, %69, %70 : index
      cf.br ^bb12(%66 : index)
      ^bb12(%72: index):
      %73 = arith.cmpi slt, %72, %67 : index
      %74 = arith.cmpi sgt, %72, %67 : index
      %75 = arith.select %71, %73, %74 : i1
      cf.cond_br %75, ^bb13(%72 : index), ^bb14(%72 : index)
      ^bb13(%76: index):
        %78 = arith.index_cast %76 : index to i32
        %77 = arith.addi %78, %arg2 : i32
        %79 = arith.cmpi sle, %77, %arg1 : i32
        cf.cond_br %79, ^bb15, ^bb16
        ^bb15:
          %80 = arith.constant 1 : i32
          %81 = arith.extsi %80 : i32 to i64
          %82 = llvm.mlir.constant(1 : i64) : i64
          %83 = llvm.alloca %82 x i64 : (i64) -> !llvm.ptr
          llvm.store %81, %83 : i64, !llvm.ptr
          %84 = arith.constant 0 : i32
          %85 = arith.index_cast %84 : i32 to index
          %86 = arith.index_cast %arg2 : i32 to index
          %88 = arith.constant 1 : index
          %89 = arith.constant -1 : index
          %90 = arith.cmpi sle, %85, %86 : index
          %87 = arith.select %90, %88, %89 : index
          cf.br ^bb18(%85 : index)
          ^bb18(%91: index):
          %92 = arith.cmpi slt, %91, %86 : index
          %93 = arith.cmpi sgt, %91, %86 : index
          %94 = arith.select %90, %92, %93 : i1
          cf.cond_br %94, ^bb19(%91 : index), ^bb20(%91 : index)
          ^bb19(%95: index):
            %96 = llvm.load %83 : !llvm.ptr -> i64
            %98 = arith.addi %76, %95 : index
            %99 = arith.index_cast %64 : index to i32
            %100 = arith.index_cast %98 : index to i32
            %97 = func.call @at(%arg0, %arg1, %99, %100) : (!llvm.ptr, i32, i32, i32) -> i64
            %101 = arith.muli %96, %97 : i64
            llvm.store %101, %83 : i64, !llvm.ptr
            %102 = arith.addi %95, %87 : index
            cf.br ^bb18(%102 : index)
          ^bb20(%103: index):
          %104 = llvm.load %83 : !llvm.ptr -> i64
          %105 = llvm.load %52 : !llvm.ptr -> i64
          %106 = arith.cmpi sgt, %104, %105 : i64
          cf.cond_br %106, ^bb21, ^bb22
          ^bb21:
            %107 = llvm.load %83 : !llvm.ptr -> i64
            llvm.store %107, %52 : i64, !llvm.ptr
            cf.br ^bb23
          ^bb22:
            cf.br ^bb23
          ^bb23:
          cf.br ^bb17
        ^bb16:
          cf.br ^bb17
        ^bb17:
        %109 = arith.index_cast %64 : index to i32
        %108 = arith.addi %109, %arg2 : i32
        %110 = arith.cmpi sle, %108, %arg1 : i32
        cf.cond_br %110, ^bb24, ^bb25
        ^bb24:
          %111 = arith.constant 1 : i32
          %112 = arith.extsi %111 : i32 to i64
          %113 = llvm.mlir.constant(1 : i64) : i64
          %114 = llvm.alloca %113 x i64 : (i64) -> !llvm.ptr
          llvm.store %112, %114 : i64, !llvm.ptr
          %115 = arith.constant 0 : i32
          %116 = arith.index_cast %115 : i32 to index
          %117 = arith.index_cast %arg2 : i32 to index
          %119 = arith.constant 1 : index
          %120 = arith.constant -1 : index
          %121 = arith.cmpi sle, %116, %117 : index
          %118 = arith.select %121, %119, %120 : index
          cf.br ^bb27(%116 : index)
          ^bb27(%122: index):
          %123 = arith.cmpi slt, %122, %117 : index
          %124 = arith.cmpi sgt, %122, %117 : index
          %125 = arith.select %121, %123, %124 : i1
          cf.cond_br %125, ^bb28(%122 : index), ^bb29(%122 : index)
          ^bb28(%126: index):
            %127 = llvm.load %114 : !llvm.ptr -> i64
            %129 = arith.addi %64, %126 : index
            %130 = arith.index_cast %129 : index to i32
            %131 = arith.index_cast %76 : index to i32
            %128 = func.call @at(%arg0, %arg1, %130, %131) : (!llvm.ptr, i32, i32, i32) -> i64
            %132 = arith.muli %127, %128 : i64
            llvm.store %132, %114 : i64, !llvm.ptr
            %133 = arith.addi %126, %118 : index
            cf.br ^bb27(%133 : index)
          ^bb29(%134: index):
          %135 = llvm.load %114 : !llvm.ptr -> i64
          %136 = llvm.load %52 : !llvm.ptr -> i64
          %137 = arith.cmpi sgt, %135, %136 : i64
          cf.cond_br %137, ^bb30, ^bb31
          ^bb30:
            %138 = llvm.load %114 : !llvm.ptr -> i64
            llvm.store %138, %52 : i64, !llvm.ptr
            cf.br ^bb32
          ^bb31:
            cf.br ^bb32
          ^bb32:
          cf.br ^bb26
        ^bb25:
          cf.br ^bb26
        ^bb26:
        %140 = arith.index_cast %64 : index to i32
        %139 = arith.addi %140, %arg2 : i32
        %141 = arith.cmpi sle, %139, %arg1 : i32
        %142 = scf.if %141 -> (i1) {
          %144 = arith.index_cast %76 : index to i32
          %143 = arith.addi %144, %arg2 : i32
          %145 = arith.cmpi sle, %143, %arg1 : i32
          scf.yield %145 : i1
        } else {
          %146 = arith.constant false
          scf.yield %146 : i1
        }
        cf.cond_br %142, ^bb33, ^bb34
        ^bb33:
          %147 = arith.constant 1 : i32
          %148 = arith.extsi %147 : i32 to i64
          %149 = llvm.mlir.constant(1 : i64) : i64
          %150 = llvm.alloca %149 x i64 : (i64) -> !llvm.ptr
          llvm.store %148, %150 : i64, !llvm.ptr
          %151 = arith.constant 0 : i32
          %152 = arith.index_cast %151 : i32 to index
          %153 = arith.index_cast %arg2 : i32 to index
          %155 = arith.constant 1 : index
          %156 = arith.constant -1 : index
          %157 = arith.cmpi sle, %152, %153 : index
          %154 = arith.select %157, %155, %156 : index
          cf.br ^bb36(%152 : index)
          ^bb36(%158: index):
          %159 = arith.cmpi slt, %158, %153 : index
          %160 = arith.cmpi sgt, %158, %153 : index
          %161 = arith.select %157, %159, %160 : i1
          cf.cond_br %161, ^bb37(%158 : index), ^bb38(%158 : index)
          ^bb37(%162: index):
            %163 = llvm.load %150 : !llvm.ptr -> i64
            %165 = arith.addi %64, %162 : index
            %166 = arith.addi %76, %162 : index
            %167 = arith.index_cast %165 : index to i32
            %168 = arith.index_cast %166 : index to i32
            %164 = func.call @at(%arg0, %arg1, %167, %168) : (!llvm.ptr, i32, i32, i32) -> i64
            %169 = arith.muli %163, %164 : i64
            llvm.store %169, %150 : i64, !llvm.ptr
            %170 = arith.addi %162, %154 : index
            cf.br ^bb36(%170 : index)
          ^bb38(%171: index):
          %172 = llvm.load %150 : !llvm.ptr -> i64
          %173 = llvm.load %52 : !llvm.ptr -> i64
          %174 = arith.cmpi sgt, %172, %173 : i64
          cf.cond_br %174, ^bb39, ^bb40
          ^bb39:
            %175 = llvm.load %150 : !llvm.ptr -> i64
            llvm.store %175, %52 : i64, !llvm.ptr
            cf.br ^bb41
          ^bb40:
            cf.br ^bb41
          ^bb41:
          cf.br ^bb35
        ^bb34:
          cf.br ^bb35
        ^bb35:
        %177 = arith.index_cast %64 : index to i32
        %176 = arith.addi %177, %arg2 : i32
        %178 = arith.cmpi sle, %176, %arg1 : i32
        %179 = scf.if %178 -> (i1) {
          %181 = arith.index_cast %76 : index to i32
          %180 = arith.subi %181, %arg2 : i32
          %182 = arith.constant 1 : i32
          %183 = arith.addi %180, %182 : i32
          %184 = arith.constant 0 : i32
          %185 = arith.cmpi sge, %183, %184 : i32
          scf.yield %185 : i1
        } else {
          %186 = arith.constant false
          scf.yield %186 : i1
        }
        cf.cond_br %179, ^bb42, ^bb43
        ^bb42:
          %187 = arith.constant 1 : i32
          %188 = arith.extsi %187 : i32 to i64
          %189 = llvm.mlir.constant(1 : i64) : i64
          %190 = llvm.alloca %189 x i64 : (i64) -> !llvm.ptr
          llvm.store %188, %190 : i64, !llvm.ptr
          %191 = arith.constant 0 : i32
          %192 = arith.index_cast %191 : i32 to index
          %193 = arith.index_cast %arg2 : i32 to index
          %195 = arith.constant 1 : index
          %196 = arith.constant -1 : index
          %197 = arith.cmpi sle, %192, %193 : index
          %194 = arith.select %197, %195, %196 : index
          cf.br ^bb45(%192 : index)
          ^bb45(%198: index):
          %199 = arith.cmpi slt, %198, %193 : index
          %200 = arith.cmpi sgt, %198, %193 : index
          %201 = arith.select %197, %199, %200 : i1
          cf.cond_br %201, ^bb46(%198 : index), ^bb47(%198 : index)
          ^bb46(%202: index):
            %203 = llvm.load %190 : !llvm.ptr -> i64
            %205 = arith.addi %64, %202 : index
            %206 = arith.subi %76, %202 : index
            %207 = arith.index_cast %205 : index to i32
            %208 = arith.index_cast %206 : index to i32
            %204 = func.call @at(%arg0, %arg1, %207, %208) : (!llvm.ptr, i32, i32, i32) -> i64
            %209 = arith.muli %203, %204 : i64
            llvm.store %209, %190 : i64, !llvm.ptr
            %210 = arith.addi %202, %194 : index
            cf.br ^bb45(%210 : index)
          ^bb47(%211: index):
          %212 = llvm.load %190 : !llvm.ptr -> i64
          %213 = llvm.load %52 : !llvm.ptr -> i64
          %214 = arith.cmpi sgt, %212, %213 : i64
          cf.cond_br %214, ^bb48, ^bb49
          ^bb48:
            %215 = llvm.load %190 : !llvm.ptr -> i64
            llvm.store %215, %52 : i64, !llvm.ptr
            cf.br ^bb50
          ^bb49:
            cf.br ^bb50
          ^bb50:
          cf.br ^bb44
        ^bb43:
          cf.br ^bb44
        ^bb44:
        %216 = arith.addi %76, %68 : index
        cf.br ^bb12(%216 : index)
      ^bb14(%217: index):
      %218 = arith.addi %64, %56 : index
      cf.br ^bb9(%218 : index)
    ^bb11(%219: index):
    %220 = llvm.load %52 : !llvm.ptr -> i64
    func.return %220 : i64
  }
  func.func @main() -> i32 {
    %221 = arith.constant 20 : i32
    %223 = arith.muli %221, %221 : i32
    %224 = arith.extsi %223 : i32 to i64
    %225 = arith.constant 4 : i32
    %226 = arith.extsi %225 : i32 to i64
    %222 = func.call @calloc(%224, %226) : (i64, i64) -> !llvm.ptr
    %227 = llvm.mlir.zero : !llvm.ptr
    %228 = llvm.icmp "eq" %222, %227 : !llvm.ptr
    cf.cond_br %228, ^bb51, ^bb52
    ^bb51:
      %229 = arith.constant 1 : i32
      func.return %229 : i32
    ^bb52:
      cf.br ^bb53
    ^bb53:
    %231 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %232 = llvm.mlir.addressof @str_1 : !llvm.ptr
    %230 = func.call @fopen(%231, %232) : (!llvm.ptr, !llvm.ptr) -> !llvm.ptr
    %233 = llvm.mlir.zero : !llvm.ptr
    %234 = llvm.icmp "eq" %230, %233 : !llvm.ptr
    cf.cond_br %234, ^bb54, ^bb55
    ^bb54:
      %235 = llvm.mlir.addressof @str_2 : !llvm.ptr
      %236 = llvm.call @printf(%235) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr) -> i32
      func.call @free(%222) : (!llvm.ptr) -> ()
      %238 = arith.constant 1 : i32
      func.return %238 : i32
    ^bb55:
      cf.br ^bb56
    ^bb56:
    %239 = arith.constant 0 : i32
    %240 = arith.muli %221, %221 : i32
    %241 = arith.index_cast %239 : i32 to index
    %242 = arith.index_cast %240 : i32 to index
    %244 = arith.constant 1 : index
    %245 = arith.constant -1 : index
    %246 = arith.cmpi sle, %241, %242 : index
    %243 = arith.select %246, %244, %245 : index
    cf.br ^bb57(%241 : index)
    ^bb57(%247: index):
    %248 = arith.cmpi slt, %247, %242 : index
    %249 = arith.cmpi sgt, %247, %242 : index
    %250 = arith.select %246, %248, %249 : i1
    cf.cond_br %250, ^bb58(%247 : index), ^bb59(%247 : index)
    ^bb58(%251: index):
      %252 = func.call @read_int(%230) : (!llvm.ptr) -> i32
      %253 = arith.constant 0 : i32
      %254 = arith.cmpi slt, %252, %253 : i32
      cf.cond_br %254, ^bb60, ^bb61
      ^bb60:
        cf.br ^bb59(%251 : index)
      ^bb61:
        cf.br ^bb62
      ^bb62:
      %255 = arith.index_cast %251 : index to i64
      %256 = llvm.getelementptr %222[%255] : (!llvm.ptr, i64) -> !llvm.ptr, i32
      llvm.store %252, %256 : i32, !llvm.ptr
      %257 = arith.addi %251, %243 : index
      cf.br ^bb57(%257 : index)
    ^bb59(%258: index):
    %259 = func.call @fclose(%230) : (!llvm.ptr) -> i32
    %260 = llvm.mlir.addressof @str_3 : !llvm.ptr
    %262 = arith.constant 4 : i32
    %261 = func.call @solve(%222, %221, %262) : (!llvm.ptr, i32, i32) -> i64
    %263 = llvm.call @printf(%260, %261) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%222) : (!llvm.ptr) -> ()
    %265 = arith.constant 0 : i32
    func.return %265 : i32
  }
}