Problem 149

Maximum-sum subsequence in a 2000×2000 lagged-Fibonacci matrix.

Answer52852124
Output52852124
StatusPASS
Native helperno
Runtime30 ms
Peak memory32384 KB
Time complexityO(n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(log n)
Space complexityO(n)O(1)
ApproachFlow solutionMatrix exponentiation
VerdictSuboptimal

Flow source

# Project Euler 149
# Maximum-sum subsequence in a 2000×2000 lagged-Fibonacci matrix.

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

function max_sum(data: ptr<i64>, first: i64, last: i64, increment: i64) -> i64 {
    let mut result: i64 = data[first]
    let mut current: i64 = 0
    let mut i: i64 = first
    while i <= last {
        current = current + data[i]
        if current < 0 { current = 0 }
        if result < current { result = current }
        i = i + increment
    }
    return result
}

function main() -> i32 {
    let size: i64 = 2000
    let length: i64 = size * size
    let data: ptr<i64> = calloc(length, 8)
    if data == null { return 1 }

    let mut k: i64 = 1
    let mut idx: i64 = 0
    while idx < 55 && idx < length {
        let val: i64 = (100003 - 200003 * k + 300007 * k * k * k) % 1000000 - 500000
        # fix negative mod
        let mut v: i64 = (100003 - 200003 * k + 300007 * k * k * k) % 1000000
        if v < 0 { v = v + 1000000 }
        data[idx] = v - 500000
        k = k + 1
        idx = idx + 1
    }
    idx = 55
    while idx < length {
        let mut s: i64 = data[idx - 24] + data[idx - 55] + 1000000
        s = s % 1000000
        if s < 0 { s = s + 1000000 }
        data[idx] = s - 500000
        idx = idx + 1
    }

    let last: i64 = size - 1
    let mut result: i64 = data[0]

    let mut y: i64 = 0
    while y < size {
        let current: i64 = max_sum(data, y * size, y * size + last, 1)
        if result < current { result = current }
        y = y + 1
    }
    let mut x: i64 = 0
    while x < size {
        let current: i64 = max_sum(data, x, last * size + x, size)
        if result < current { result = current }
        x = x + 1
    }
    x = 0
    while x < size {
        let current: i64 = max_sum(data, x, (last - x) * size + last, size + 1)
        if result < current { result = current }
        x = x + 1
    }
    y = 1
    while y < size {
        let current: i64 = max_sum(data, y * size, last * size + (y), size + 1)
        if result < current { result = current }
        y = y + 1
    }
    x = 0
    while x < size {
        let current: i64 = max_sum(data, x, x * size, size - 1)
        if result < current { result = current }
        x = x + 1
    }
    y = 1
    while y < size {
        let current: i64 = max_sum(data, y * size + last, last * size + y, size - 1)
        if result < current { result = current }
        y = y + 1
    }

    printf("%lld\n", result)
    free(data)
    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 max_sum_ptr_i64_i64_i64_i64(int64_t* data, int64_t first, int64_t last, int64_t increment);
int32_t main(void);



int64_t max_sum_ptr_i64_i64_i64_i64(int64_t* data, int64_t first, int64_t last, int64_t increment) {
    int64_t result = data[first];
    int64_t current = 0;
    int64_t i = first;
    while (i <= last) {
        current = (current + data[i]);
        if (current < 0) {
            current = 0;
        }
        if (result < current) {
            result = current;
        }
        i = (i + increment);
    }
    return result;
}

int32_t main(void) {
    int64_t size = 2000;
    int64_t length = (size * size);
    int64_t* data = (int64_t*)(calloc(length, 8));
    if (data == NULL) {
        return 1;
    }
    int64_t k = 1;
    int64_t idx = 0;
    while ((idx < 55 && idx < length)) {
        int64_t val = (FLOW_CHECKED_MOD((((100003 - (200003 * k)) + (((300007 * k) * k) * k))), (1000000)) - 500000);
        int64_t v = FLOW_CHECKED_MOD((((100003 - (200003 * k)) + (((300007 * k) * k) * k))), (1000000));
        if (v < 0) {
            v = (v + 1000000);
        }
        data[idx] = (v - 500000);
        k = (k + 1);
        idx = (idx + 1);
    }
    idx = 55;
    while (idx < length) {
        int64_t s = ((data[(idx - 24)] + data[(idx - 55)]) + 1000000);
        s = FLOW_CHECKED_MOD((s), (1000000));
        if (s < 0) {
            s = (s + 1000000);
        }
        data[idx] = (s - 500000);
        idx = (idx + 1);
    }
    int64_t last = (size - 1);
    int64_t result = data[0];
    int64_t y = 0;
    while (y < size) {
        int64_t current = max_sum_ptr_i64_i64_i64_i64(data, (y * size), ((y * size) + last), 1);
        if (result < current) {
            result = current;
        }
        y = (y + 1);
    }
    int64_t x = 0;
    while (x < size) {
        int64_t current = max_sum_ptr_i64_i64_i64_i64(data, x, ((last * size) + x), size);
        if (result < current) {
            result = current;
        }
        x = (x + 1);
    }
    x = 0;
    while (x < size) {
        int64_t current = max_sum_ptr_i64_i64_i64_i64(data, x, (((last - x) * size) + last), (size + 1));
        if (result < current) {
            result = current;
        }
        x = (x + 1);
    }
    y = 1;
    while (y < size) {
        int64_t current = max_sum_ptr_i64_i64_i64_i64(data, (y * size), ((last * size) + y), (size + 1));
        if (result < current) {
            result = current;
        }
        y = (y + 1);
    }
    x = 0;
    while (x < size) {
        int64_t current = max_sum_ptr_i64_i64_i64_i64(data, x, (x * size), (size - 1));
        if (result < current) {
            result = current;
        }
        x = (x + 1);
    }
    y = 1;
    while (y < size) {
        int64_t current = max_sum_ptr_i64_i64_i64_i64(data, ((y * size) + last), ((last * size) + y), (size - 1));
        if (result < current) {
            result = current;
        }
        y = (y + 1);
    }
    printf("%lld\n", result);
    free(data);
    return 0;
}

Generated MLIR

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