Problem 879

Touch-screen Password: count distinct passwords on an n x n grid. Bitmask DP: dp[cur][mask] = number of non-empty continuations.

Answer4350069824940
Output4350069824940
StatusPASS
Native helperno
Runtime30 ms
Peak memory12368 KB
Time complexityO(n^3) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n log log n)
Space complexityO(n^2)O(n)
ApproachFlow solutionSieve or enumeration
VerdictSuboptimal

Flow source

# Project Euler 879
# Touch-screen Password: count distinct passwords on an n x n grid.
# Bitmask DP: dp[cur][mask] = number of non-empty continuations.

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

function popcount(x: i32) -> i32 {
    let mut c: i32 = 0
    let mut v: i32 = x
    while v != 0 {
        c = c + 1
        v = v & (v - 1)
    }
    return c
}

function ctz(x: i32) -> i32 {
    let mut v: i32 = x
    let mut c: i32 = 0
    while (v & 1) == 0 {
        c = c + 1
        v = v >> 1
    }
    return c
}

function gcd_i(a0: i32, b0: i32) -> i32 {
    let mut a: i32 = a0
    let mut b: i32 = b0
    while b != 0 {
        let t: i32 = a % b
        a = b
        b = t
    }
    return a
}

function iabs(x: i32) -> i32 {
    if x < 0 {
        return -x
    }
    return x
}

function count_passwords(n: i32) -> i64 {
    let N: i32 = n * n
    if N < 2 {
        return 0
    }

    let between: ptr<i32> = calloc(256, 4)
    if between == null {
        return 0
    }

    # precompute_between
    for a in 0..N {
        let xa: i32 = a % n
        let ya: i32 = a / n
        for b in 0..N {
            if a == b {
                continue
            }
            let xb: i32 = b % n
            let yb: i32 = b / n
            let dx: i32 = xb - xa
            let dy: i32 = yb - ya
            let g: i32 = gcd_i(iabs(dx), iabs(dy))
            if g <= 1 {
                continue
            }
            let sx: i32 = dx / g
            let sy: i32 = dy / g
            let mut mask: i32 = 0
            for k in 1..g {
                let x: i32 = xa + sx * k
                let y: i32 = ya + sy * k
                mask = mask | (1 << (y * n + x))
            }
            between[a * 16 + b] = mask
        }
    }

    let all_mask: i32 = (1 << N) - 1
    let masksz: i64 = 65536

    # dp flat array: dp[cur * 65536 + mask]
    let dp: ptr<i64> = calloc((N as i64) * masksz, 8)
    if dp == null {
        return 0
    }

    # buckets by popcount
    let buckets: ptr<i32> = calloc(17 * 65536, 4)
    let bucket_count: ptr<i32> = calloc(17, 4)
    if buckets == null || bucket_count == null {
        return 0
    }
    for mask in 0..(1 << N) {
        let pc: i32 = popcount(mask)
        buckets[pc * 65536 + bucket_count[pc]] = mask
        bucket_count[pc] = bucket_count[pc] + 1
    }

    for k in 1..(N + 1) {
        let kk: i32 = N + 1 - k
        for bi in 0..bucket_count[kk] {
            let mask: i32 = buckets[kk * 65536 + bi]
            let remaining: i32 = all_mask ^ mask
            if remaining == 0 {
                continue
            }
            let mut m: i32 = mask
            while m != 0 {
                let lsb: i32 = m & (-m)
                let cur: i32 = ctz(lsb)
                m = m ^ lsb

                let mut total: i64 = 0
                let mut rem: i32 = remaining
                while rem != 0 {
                    let bit: i32 = rem & (-rem)
                    let nxt: i32 = ctz(bit)
                    rem = rem ^ bit

                    if (between[cur * 16 + nxt] & remaining) == 0 {
                        total = total + 1 + dp[(nxt as i64) * masksz + ((mask | bit) as i64)]
                    }
                }
                dp[(cur as i64) * masksz + (mask as i64)] = total
            }
        }
    }

    let mut total_passwords: i64 = 0
    for start in 0..N {
        total_passwords = total_passwords + dp[(start as i64) * masksz + ((1 << start) as i64)]
    }

    free(dp)
    free(buckets)
    free(bucket_count)
    free(between)
    return total_passwords
}

function main() -> i32 {
    printf("%lld\n", count_passwords(4))
    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 popcount_i32(int32_t x);
int32_t ctz_i32(int32_t x);
int32_t gcd_i_i32_i32(int32_t a0, int32_t b0);
int32_t iabs_i32(int32_t x);
int64_t count_passwords_i32(int32_t n);
int32_t main(void);




int32_t popcount_i32(int32_t x) {
    int32_t c = 0;
    int32_t v = x;
    while (v != 0) {
        c = (c + 1);
        v = (v & (v - 1));
    }
    return c;
}

int32_t ctz_i32(int32_t x) {
    int32_t v = x;
    int32_t c = 0;
    while ((v & 1) == 0) {
        c = (c + 1);
        v = FLOW_CHECKED_SHR((v), (1));
    }
    return c;
}

int32_t gcd_i_i32_i32(int32_t a0, int32_t b0) {
    int32_t a = a0;
    int32_t b = b0;
    while (b != 0) {
        int32_t t = FLOW_CHECKED_MOD((a), (b));
        a = b;
        b = t;
    }
    return a;
}

int32_t iabs_i32(int32_t x) {
    if (x < 0) {
        return (-x);
    }
    return x;
}

int64_t count_passwords_i32(int32_t n) {
    int32_t N = (n * n);
    if (N < 2) {
        return 0;
    }
    int32_t* between = (int32_t*)(calloc(256, 4));
    if (between == NULL) {
        return 0;
    }
    int32_t __flow_step_1 = 1;
    for (int32_t a = 0; (0 <= N) ? a < N : a > N; a += (0 <= N) ? 1 : -1) {
        int32_t xa = FLOW_CHECKED_MOD((a), (n));
        int32_t ya = FLOW_CHECKED_DIV((a), (n));
        int32_t __flow_step_2 = 1;
        for (int32_t b = 0; (0 <= N) ? b < N : b > N; b += (0 <= N) ? 1 : -1) {
            if (a == b) {
                continue;
            }
            int32_t xb = FLOW_CHECKED_MOD((b), (n));
            int32_t yb = FLOW_CHECKED_DIV((b), (n));
            int32_t dx = (xb - xa);
            int32_t dy = (yb - ya);
            int32_t g = gcd_i_i32_i32(iabs_i32(dx), iabs_i32(dy));
            if (g <= 1) {
                continue;
            }
            int32_t sx = FLOW_CHECKED_DIV((dx), (g));
            int32_t sy = FLOW_CHECKED_DIV((dy), (g));
            int32_t mask = 0;
            int32_t __flow_step_3 = 1;
            for (int32_t k = 1; (1 <= g) ? k < g : k > g; k += (1 <= g) ? 1 : -1) {
                int32_t x = (xa + (sx * k));
                int32_t y = (ya + (sy * k));
                mask = (mask | FLOW_CHECKED_SHL((1), (((y * n) + x))));
            }
            between[((a * 16) + b)] = mask;
        }
    }
    int32_t all_mask = (FLOW_CHECKED_SHL((1), (N)) - 1);
    int64_t masksz = 65536;
    int64_t* dp = (int64_t*)(calloc((((int64_t)(N)) * masksz), 8));
    if (dp == NULL) {
        return 0;
    }
    int32_t* buckets = (int32_t*)(calloc((17 * 65536), 4));
    int32_t* bucket_count = (int32_t*)(calloc(17, 4));
    if ((buckets == NULL || bucket_count == NULL)) {
        return 0;
    }
    int32_t __flow_step_4 = 1;
    for (int32_t mask = 0; (0 <= FLOW_CHECKED_SHL((1), (N))) ? mask < FLOW_CHECKED_SHL((1), (N)) : mask > FLOW_CHECKED_SHL((1), (N)); mask += (0 <= FLOW_CHECKED_SHL((1), (N))) ? 1 : -1) {
        int32_t pc = popcount_i32(mask);
        buckets[((pc * 65536) + bucket_count[pc])] = mask;
        bucket_count[pc] = (bucket_count[pc] + 1);
    }
    int32_t __flow_step_5 = 1;
    for (int32_t k = 1; (1 <= (N + 1)) ? k < (N + 1) : k > (N + 1); k += (1 <= (N + 1)) ? 1 : -1) {
        int32_t kk = ((N + 1) - k);
        int32_t __flow_step_6 = 1;
        for (int32_t bi = 0; (0 <= bucket_count[kk]) ? bi < bucket_count[kk] : bi > bucket_count[kk]; bi += (0 <= bucket_count[kk]) ? 1 : -1) {
            int32_t mask = buckets[((kk * 65536) + bi)];
            int32_t remaining = (all_mask ^ mask);
            if (remaining == 0) {
                continue;
            }
            int32_t m = mask;
            while (m != 0) {
                int32_t lsb = (m & (-m));
                int32_t cur = ctz_i32(lsb);
                m = (m ^ lsb);
                int64_t total = 0;
                int32_t rem = remaining;
                while (rem != 0) {
                    int32_t bit = (rem & (-rem));
                    int32_t nxt = ctz_i32(bit);
                    rem = (rem ^ bit);
                    if ((between[((cur * 16) + nxt)] & remaining) == 0) {
                        total = ((total + 1) + dp[((((int64_t)(nxt)) * masksz) + ((int64_t)((mask | bit))))]);
                    }
                }
                dp[((((int64_t)(cur)) * masksz) + ((int64_t)(mask)))] = total;
            }
        }
    }
    int64_t total_passwords = 0;
    int32_t __flow_step_7 = 1;
    for (int32_t start = 0; (0 <= N) ? start < N : start > N; start += (0 <= N) ? 1 : -1) {
        total_passwords = (total_passwords + dp[((((int64_t)(start)) * masksz) + ((int64_t)(FLOW_CHECKED_SHL((1), (start)))))]);
    }
    free(dp);
    free(buckets);
    free(bucket_count);
    free(between);
    return total_passwords;
}

int32_t main(void) {
    printf("%lld\n", count_passwords_i32(4));
    return 0;
}

Generated MLIR

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