Problem 105

Sum of elements of special sum sets in data/p105.txt.

Answer73702
Output73702
StatusPASS
Native helperno
Runtime0 ms
Peak memory1104 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n * sum)
Space complexityO(1)O(sum)
ApproachFlow solutionSubset sum DP
VerdictUnknown

Flow source

# Project Euler 105
# Sum of elements of special sum sets in data/p105.txt.

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 sort_arr(a: ptr<i32>, n: i32) -> void {
    let mut i: i32 = 0
    while i < n {
        let mut j: i32 = i + 1
        while j < n {
            if a[j] < a[i] {
                let t: i32 = a[i]
                a[i] = a[j]
                a[j] = t
            }
            j = j + 1
        }
        i = i + 1
    }
}

function is_special(a: ptr<i32>, n: i32) -> bool {
    let mut k: i32 = 1
    while 2 * k <= n {
        let mut small: i32 = 0
        let mut i: i32 = 0
        while i <= k {
            small = small + a[i]
            i = i + 1
        }
        let mut large: i32 = 0
        i = n - k
        while i < n {
            large = large + a[i]
            i = i + 1
        }
        if small <= large { return false }
        k = k + 1
    }
    let mut seen: ptr<i8> = calloc(30000, 1)
    if seen == null { return false }
    let lim: i32 = 1 << n
    let mut mask: i32 = 1
    while mask < lim {
        let mut s: i32 = 0
        let mut i: i32 = 0
        while i < n {
            if ((mask >> i) & 1) == 1 {
                s = s + a[i]
            }
            i = i + 1
        }
        if seen[s] != 0 {
            free(seen)
            return false
        }
        seen[s] = 1
        mask = mask + 1
    }
    free(seen)
    return true
}

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

    let mut total: i64 = 0
    let mut n: i32 = 0
    let mut cur: i32 = 0
    let mut in_num: bool = false
    let mut c: i32 = fgetc(f)
    while true {
        if c >= 48 && c <= 57 {
            cur = cur * 10 + (c - 48)
            in_num = true
        } else {
            if in_num {
                a[n] = cur
                n = n + 1
                cur = 0
                in_num = false
            }
            if c == 10 || c < 0 {
                if n > 0 {
                    sort_arr(a, n)
                    if is_special(a, n) {
                        let mut s: i32 = 0
                        let mut i: i32 = 0
                        while i < n {
                            s = s + a[i]
                            i = i + 1
                        }
                        total = total + (s as i64)
                    }
                    n = 0
                }
                if c < 0 { break }
            }
        }
        if c < 0 { break }
        c = fgetc(f)
    }
    fclose(f)
    free(a)
    printf("%lld\n", total)
    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; }

void sort_arr_ptr_i32_i32(int32_t* a, int32_t n);
bool is_special_ptr_i32_i32(int32_t* a, int32_t n);
int32_t main(void);






void sort_arr_ptr_i32_i32(int32_t* a, int32_t n) {
    int32_t i = 0;
    while (i < n) {
        int32_t j = (i + 1);
        while (j < n) {
            if (a[j] < a[i]) {
                int32_t t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
            j = (j + 1);
        }
        i = (i + 1);
    }
}

bool is_special_ptr_i32_i32(int32_t* a, int32_t n) {
    int32_t k = 1;
    while ((2 * k) <= n) {
        int32_t small = 0;
        int32_t i = 0;
        while (i <= k) {
            small = (small + a[i]);
            i = (i + 1);
        }
        int32_t large = 0;
        i = (n - k);
        while (i < n) {
            large = (large + a[i]);
            i = (i + 1);
        }
        if (small <= large) {
            return 0;
        }
        k = (k + 1);
    }
    int8_t* seen = (int8_t*)(calloc(30000, 1));
    if (seen == NULL) {
        return 0;
    }
    int32_t lim = FLOW_CHECKED_SHL((1), (n));
    int32_t mask = 1;
    while (mask < lim) {
        int32_t s = 0;
        int32_t i = 0;
        while (i < n) {
            if ((FLOW_CHECKED_SHR((mask), (i)) & 1) == 1) {
                s = (s + a[i]);
            }
            i = (i + 1);
        }
        if (seen[s] != 0) {
            free(seen);
            return 0;
        }
        seen[s] = 1;
        mask = (mask + 1);
    }
    free(seen);
    return 1;
}

int32_t main(void) {
    void* f = (void*)(fopen("data/p105.txt", "r"));
    if (f == NULL) {
        printf("failed to read data/p105.txt\n");
        return 1;
    }
    int32_t* a = (int32_t*)(calloc(20, 4));
    if (a == NULL) {
        return 1;
    }
    int64_t total = 0;
    int32_t n = 0;
    int32_t cur = 0;
    bool in_num = 0;
    int32_t c = fgetc(f);
    while (1) {
        if ((c >= 48 && c <= 57)) {
            cur = ((cur * 10) + (c - 48));
            in_num = 1;
        } else {
            if (in_num) {
                a[n] = cur;
                n = (n + 1);
                cur = 0;
                in_num = 0;
            }
            if ((c == 10 || c < 0)) {
                if (n > 0) {
                    sort_arr_ptr_i32_i32(a, n);
                    if (is_special_ptr_i32_i32(a, n)) {
                        int32_t s = 0;
                        int32_t i = 0;
                        while (i < n) {
                            s = (s + a[i]);
                            i = (i + 1);
                        }
                        total = (total + ((int64_t)(s)));
                    }
                    n = 0;
                }
                if (c < 0) {
                    break;
                }
            }
        }
        if (c < 0) {
            break;
        }
        c = fgetc(f);
    }
    fclose(f);
    free(a);
    printf("%lld\n", total);
    return 0;
}

Generated MLIR

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