Problem 107

Maximum saving from converting network to MST (Kruskal).

Answer259679
Output259679
StatusPASS
Native helperno
Runtime0 ms
Peak memory1136 KB
Time complexityO(n^3) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(e log v)
Space complexityO(n)O(v)
ApproachFlow solutionKruskal or Prim algorithm
VerdictUnknown

Flow source

# Project Euler 107
# Maximum saving from converting network to MST (Kruskal).

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
}

struct Edge {
    u: i32,
    v: i32,
    w: i32
}

function find(parent: ptr<i32>, x0: i32) -> i32 {
    let mut x: i32 = x0
    while parent[x] != x {
        parent[x] = parent[parent[x]]
        x = parent[x]
    }
    return x
}

function main() -> i32 {
    let n: i32 = 40
    let f: ptr<void> = fopen("data/p107.txt", "r")
    if f == null {
        printf("failed to read data/p107.txt\n")
        return 1
    }

    # edges: u,v,w — at most n*(n-1)/2
    let max_e: i32 = n * n
    let edges: ptr<Edge> = calloc(max_e as i64, 12) as ptr<Edge>
    if edges == null { return 1 }

    let mut ec: i32 = 0
    let mut total: i64 = 0
    let mut r: i32 = 0
    let mut c: i32 = 0
    let mut cur: i32 = 0
    let mut in_num: bool = false
    let mut ch: i32 = fgetc(f)
    while ch >= 0 {
        if ch >= 48 && ch <= 57 {
            cur = cur * 10 + (ch - 48)
            in_num = true
        } elif ch == 45 {
            # dash = no edge; skip until comma/newline
            in_num = false
            cur = 0
        } else {
            if in_num {
                if c > r {
                    edges[ec].u = r
                    edges[ec].v = c
                    edges[ec].w = cur
                    ec = ec + 1
                    total = total + (cur as i64)
                }
                in_num = false
                cur = 0
            }
            if ch == 44 {
                c = c + 1
            } elif ch == 10 {
                r = r + 1
                c = 0
            }
        }
        ch = fgetc(f)
    }
    if in_num && c > r {
        edges[ec].u = r
        edges[ec].v = c
        edges[ec].w = cur
        ec = ec + 1
        total = total + (cur as i64)
    }
    fclose(f)

    # sort edges by weight (shell sort)
    let mut gap: i32 = ec / 2
    while gap > 0 {
        let mut i: i32 = gap
        while i < ec {
            let mut j: i32 = i
            while j >= gap && edges[j].w < edges[j - gap].w {
                let tu: i32 = edges[j].u
                let tv: i32 = edges[j].v
                let tw: i32 = edges[j].w
                edges[j].u = edges[j - gap].u
                edges[j].v = edges[j - gap].v
                edges[j].w = edges[j - gap].w
                edges[j - gap].u = tu
                edges[j - gap].v = tv
                edges[j - gap].w = tw
                j = j - gap
            }
            i = i + 1
        }
        gap = gap / 2
    }

    let parent: ptr<i32> = calloc(n as i64, 4)
    if parent == null { return 1 }
    let mut i: i32 = 0
    while i < n {
        parent[i] = i
        i = i + 1
    }

    let mut mst: i64 = 0
    let mut used: i32 = 0
    i = 0
    while i < ec && used < n - 1 {
        let a: i32 = find(parent, edges[i].u)
        let b: i32 = find(parent, edges[i].v)
        if a != b {
            parent[a] = b
            mst = mst + (edges[i].w as i64)
            used = used + 1
        }
        i = i + 1
    }

    printf("%lld\n", total - mst)
    free(parent)
    free(edges)
    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; }

typedef struct Edge Edge;

struct Edge {
    int32_t u;
    int32_t v;
    int32_t w;
};

int32_t find_ptr_i32_i32(int32_t* parent, int32_t x0);
int32_t main(void);






int32_t find_ptr_i32_i32(int32_t* parent, int32_t x0) {
    int32_t x = x0;
    while (parent[x] != x) {
        parent[x] = parent[parent[x]];
        x = parent[x];
    }
    return x;
}

int32_t main(void) {
    int32_t n = 40;
    void* f = (void*)(fopen("data/p107.txt", "r"));
    if (f == NULL) {
        printf("failed to read data/p107.txt\n");
        return 1;
    }
    int32_t max_e = (n * n);
    Edge* edges = (Edge*)(((Edge*)(calloc(((int64_t)(max_e)), 12))));
    if (edges == NULL) {
        return 1;
    }
    int32_t ec = 0;
    int64_t total = 0;
    int32_t r = 0;
    int32_t c = 0;
    int32_t cur = 0;
    bool in_num = 0;
    int32_t ch = fgetc(f);
    while (ch >= 0) {
        if ((ch >= 48 && ch <= 57)) {
            cur = ((cur * 10) + (ch - 48));
            in_num = 1;
        } else if (ch == 45) {
            in_num = 0;
            cur = 0;
        } else {
            if (in_num) {
                if (c > r) {
                    edges[ec].u = r;
                    edges[ec].v = c;
                    edges[ec].w = cur;
                    ec = (ec + 1);
                    total = (total + ((int64_t)(cur)));
                }
                in_num = 0;
                cur = 0;
            }
            if (ch == 44) {
                c = (c + 1);
            } else if (ch == 10) {
                r = (r + 1);
                c = 0;
            }
        }
        ch = fgetc(f);
    }
    if ((in_num && c > r)) {
        edges[ec].u = r;
        edges[ec].v = c;
        edges[ec].w = cur;
        ec = (ec + 1);
        total = (total + ((int64_t)(cur)));
    }
    fclose(f);
    int32_t gap = FLOW_CHECKED_DIV((ec), (2));
    while (gap > 0) {
        int32_t i = gap;
        while (i < ec) {
            int32_t j = i;
            while ((j >= gap && edges[j].w < edges[(j - gap)].w)) {
                int32_t tu = edges[j].u;
                int32_t tv = edges[j].v;
                int32_t tw = edges[j].w;
                edges[j].u = edges[(j - gap)].u;
                edges[j].v = edges[(j - gap)].v;
                edges[j].w = edges[(j - gap)].w;
                edges[(j - gap)].u = tu;
                edges[(j - gap)].v = tv;
                edges[(j - gap)].w = tw;
                j = (j - gap);
            }
            i = (i + 1);
        }
        gap = FLOW_CHECKED_DIV((gap), (2));
    }
    int32_t* parent = (int32_t*)(calloc(((int64_t)(n)), 4));
    if (parent == NULL) {
        return 1;
    }
    int32_t i = 0;
    while (i < n) {
        parent[i] = i;
        i = (i + 1);
    }
    int64_t mst = 0;
    int32_t used = 0;
    i = 0;
    while ((i < ec && used < (n - 1))) {
        int32_t a = find_ptr_i32_i32(parent, edges[i].u);
        int32_t b = find_ptr_i32_i32(parent, edges[i].v);
        if (a != b) {
            parent[a] = b;
            mst = (mst + ((int64_t)(edges[i].w)));
            used = (used + 1);
        }
        i = (i + 1);
    }
    printf("%lld\n", (total - mst));
    free(parent);
    free(edges);
    return 0;
}

Generated MLIR

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