Problem 079

Shortest passcode consistent with keylog login occurrences.

Answer73162890
Output73162890
StatusPASS
Native helperno
Runtime0 ms
Peak memory1088 KB
Time complexityO(n^3) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n!)
Space complexityO(1)O(n)
ApproachFlow solutionPermutation enumeration or constraint search
VerdictOptimal

Flow source

# Project Euler 079
# Shortest passcode consistent with keylog login occurrences.

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 main() -> i32 {
    # before[d*10+e] = 1 if d must appear before e
    let before: ptr<i8> = calloc(100, 1)
    let used: ptr<i8> = calloc(10, 1)
    if before == null || used == null { return 1 }

    let f: ptr<void> = fopen("data/p079.txt", "r")
    if f == null {
        printf("failed to read data/p079.txt\n")
        return 1
    }
    let mut c: i32 = fgetc(f)
    while c >= 0 {
        while c >= 0 && (c < 48 || c > 57) {
            c = fgetc(f)
        }
        if c < 0 { break }
        let a: i32 = c - 48
        c = fgetc(f)
        let b: i32 = c - 48
        c = fgetc(f)
        let d: i32 = c - 48
        used[a] = 1
        used[b] = 1
        used[d] = 1
        before[a * 10 + b] = 1
        before[b * 10 + d] = 1
        before[a * 10 + d] = 1
        c = fgetc(f)
    }
    fclose(f)

    # transitive closure (Floyd) so topological order is unique/shortest
    let mut k: i32 = 0
    while k < 10 {
        let mut i: i32 = 0
        while i < 10 {
            let mut j: i32 = 0
            while j < 10 {
                if before[i * 10 + k] != 0 && before[k * 10 + j] != 0 {
                    before[i * 10 + j] = 1
                }
                j = j + 1
            }
            i = i + 1
        }
        k = k + 1
    }

    # indegree among used digits
    let indeg: ptr<i32> = calloc(10, 4)
    if indeg == null { return 1 }
    let mut i: i32 = 0
    while i < 10 {
        if used[i] != 0 {
            let mut j: i32 = 0
            while j < 10 {
                if used[j] != 0 && before[j * 10 + i] != 0 {
                    indeg[i] = indeg[i] + 1
                }
                j = j + 1
            }
        }
        i = i + 1
    }

    let mut result: i64 = 0
    let mut remaining: i32 = 0
    i = 0
    while i < 10 {
        if used[i] != 0 { remaining = remaining + 1 }
        i = i + 1
    }
    while remaining > 0 {
        let mut pick: i32 = -1
        i = 0
        while i < 10 {
            if used[i] != 0 && indeg[i] == 0 {
                pick = i
                break
            }
            i = i + 1
        }
        if pick < 0 { break }
        result = result * 10 + (pick as i64)
        used[pick] = 0
        remaining = remaining - 1
        let mut j: i32 = 0
        while j < 10 {
            if before[pick * 10 + j] != 0 && used[j] != 0 {
                indeg[j] = indeg[j] - 1
            }
            j = j + 1
        }
    }
    printf("%lld\n", result)
    free(before)
    free(used)
    free(indeg)
    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 main(void);






int32_t main(void) {
    int8_t* before = (int8_t*)(calloc(100, 1));
    int8_t* used = (int8_t*)(calloc(10, 1));
    if ((before == NULL || used == NULL)) {
        return 1;
    }
    void* f = (void*)(fopen("data/p079.txt", "r"));
    if (f == NULL) {
        printf("failed to read data/p079.txt\n");
        return 1;
    }
    int32_t c = fgetc(f);
    while (c >= 0) {
        while ((c >= 0 && (c < 48 || c > 57))) {
            c = fgetc(f);
        }
        if (c < 0) {
            break;
        }
        int32_t a = (c - 48);
        c = fgetc(f);
        int32_t b = (c - 48);
        c = fgetc(f);
        int32_t d = (c - 48);
        used[a] = 1;
        used[b] = 1;
        used[d] = 1;
        before[((a * 10) + b)] = 1;
        before[((b * 10) + d)] = 1;
        before[((a * 10) + d)] = 1;
        c = fgetc(f);
    }
    fclose(f);
    int32_t k = 0;
    while (k < 10) {
        int32_t i = 0;
        while (i < 10) {
            int32_t j = 0;
            while (j < 10) {
                if ((before[((i * 10) + k)] != 0 && before[((k * 10) + j)] != 0)) {
                    before[((i * 10) + j)] = 1;
                }
                j = (j + 1);
            }
            i = (i + 1);
        }
        k = (k + 1);
    }
    int32_t* indeg = (int32_t*)(calloc(10, 4));
    if (indeg == NULL) {
        return 1;
    }
    int32_t i = 0;
    while (i < 10) {
        if (used[i] != 0) {
            int32_t j = 0;
            while (j < 10) {
                if ((used[j] != 0 && before[((j * 10) + i)] != 0)) {
                    indeg[i] = (indeg[i] + 1);
                }
                j = (j + 1);
            }
        }
        i = (i + 1);
    }
    int64_t result = 0;
    int32_t remaining = 0;
    i = 0;
    while (i < 10) {
        if (used[i] != 0) {
            remaining = (remaining + 1);
        }
        i = (i + 1);
    }
    while (remaining > 0) {
        int32_t pick = (-1);
        i = 0;
        while (i < 10) {
            if ((used[i] != 0 && indeg[i] == 0)) {
                pick = i;
                break;
            }
            i = (i + 1);
        }
        if (pick < 0) {
            break;
        }
        result = ((result * 10) + ((int64_t)(pick)));
        used[pick] = 0;
        remaining = (remaining - 1);
        int32_t j = 0;
        while (j < 10) {
            if ((before[((pick * 10) + j)] != 0 && used[j] != 0)) {
                indeg[j] = (indeg[j] - 1);
            }
            j = (j + 1);
        }
    }
    printf("%lld\n", result);
    free(before);
    free(used);
    free(indeg);
    return 0;
}

Generated MLIR

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