Problem 202

Laserbeam reflections exiting at C after 12017639147 bounces.

Answer1209002624
Output1209002624
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(1) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(1)O(n)
Space complexityO(1)O(1)
ApproachFlow solutionIterative reflection simulation
VerdictOptimal

Flow source

# Project Euler 202
# Laserbeam reflections exiting at C after 12017639147 bounces.

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

function count_ways(N: i64) -> i64 {
    if N % 2 == 0 { return 0 }
    let M: i64 = (N + 3) / 2
    if M % 3 == 0 { return 0 }

    # factorize M into primes[0..pc)
    let primes: ptr<i64> = calloc(64, 8)
    if primes == null { return 0 }
    let mut pc: i32 = 0
    let mut n: i64 = M
    if n % 2 == 0 {
        primes[pc] = 2
        pc = pc + 1
        while n % 2 == 0 { n = n / 2 }
    }
    while n % 3 == 0 {
        if pc == 0 || primes[pc - 1] != 3 {
            primes[pc] = 3
            pc = pc + 1
        }
        n = n / 3
    }
    let mut f: i64 = 5
    let mut step: i64 = 2
    while f * f <= n {
        if n % f == 0 {
            primes[pc] = f
            pc = pc + 1
            while n % f == 0 { n = n / f }
        }
        f = f + step
        step = 6 - step
    }
    if n > 1 {
        primes[pc] = n
        pc = pc + 1
    }

    let mut phi_M: i64 = M
    let mut i: i32 = 0
    while i < pc {
        let p: i64 = primes[i]
        phi_M = phi_M / p * (p - 1)
        i = i + 1
    }

    let mut has_p1: bool = false
    i = 0
    while i < pc {
        if primes[i] % 3 == 1 { has_p1 = true }
        i = i + 1
    }
    if !has_p1 {
        let mut G: i64 = 0 - 1
        if M % 3 == 1 { G = 1 }
        let mut tw: i64 = 1
        let mut j: i32 = 0
        while j < pc {
            tw = tw * 2
            j = j + 1
        }
        phi_M = phi_M - G * tw
    }
    free(primes)
    return phi_M / 3
}

function main() -> i32 {
    printf("%lld\n", count_ways(12017639147))
    return 0
}

Generated C

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Flow runtime helpers */
typedef struct flow_temp_node { struct flow_temp_node* next; } flow_temp_node;
static flow_temp_node* flow_temp_head = NULL;
static int flow_temp_atexit_set = 0;
__attribute__((unused)) static void flow_temp_free_all(void) {
    while (flow_temp_head) {
        flow_temp_node* n = flow_temp_head;
        flow_temp_head = n->next;
        free(n);
    }
}
__attribute__((unused)) static void* flow_temp_alloc(size_t nbytes) {
    flow_temp_node* node = (flow_temp_node*)malloc(sizeof(flow_temp_node) + nbytes);
    if (!node) return NULL;
    node->next = flow_temp_head;
    flow_temp_head = node;
    if (!flow_temp_atexit_set) {
        flow_temp_atexit_set = 1;
        atexit(flow_temp_free_all);
    }
    return (void*)(node + 1);
}
#ifndef FLOW_DIAG
#define FLOW_DIAG(msg) fprintf(stderr, "%s", (msg))
#endif
#ifndef FLOW_LOG
#define FLOW_LOG(fmt, ...) printf(fmt, __VA_ARGS__)
#endif
#ifndef FLOW_LOG_EMPTY
#define FLOW_LOG_EMPTY(fmt) printf(fmt)
#endif
static char* flow_strcat(const char* a, const char* b) {
    size_t la = strlen(a ? a : ""), lb = strlen(b ? b : "");
    char* r = (char*)flow_temp_alloc(la + lb + 1);
    if (!r) return NULL;
    if (la) memcpy(r, a, la);
    if (lb) memcpy(r + la, b, lb);
    r[la + lb] = '\0';
    return r;
}

#define __flow_in_arr(arr, val) __extension__ ({ \
    int _found = 0; \
    size_t _n = sizeof(arr)/sizeof((arr)[0]); \
    for (size_t _i = 0; _i < _n; _i++) { \
        if ((arr)[_i] == (val)) { _found = 1; break; } \
    } _found; })

/* Unified fault handler (MISRA #279) — override with -DFLOW_FAULT_HANDLER=fn */
#ifndef FLOW_FAULT_HANDLER
__attribute__((unused)) static inline void flow_fault_handler(const char* msg) {
    fprintf(stderr, "flow: %s\n", msg ? msg : "fault");
    abort();
#if defined(__GNUC__) || defined(__clang__)
    __builtin_unreachable();
#endif
}
#else
#define flow_fault_handler FLOW_FAULT_HANDLER
#endif
#define flow_div_by_zero_handler() flow_fault_handler("division by zero")
#define flow_shift_ub_handler() flow_fault_handler("invalid shift (amount out of range or left-shift of negative)")

#ifndef FLOW_CHECKED_DIV
#define FLOW_CHECKED_DIV(L, R) (((R) != 0) ? ((L) / (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_MOD
#define FLOW_CHECKED_MOD(L, R) (((R) != 0) ? ((L) % (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHL
#define FLOW_CHECKED_SHL(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull)) && ((L) >= 0)) ? ((L) << (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHR
#define FLOW_CHECKED_SHR(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull))) ? ((L) >> (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif

#include <math.h>

void* _ui_state = NULL;

static inline float i32_to_f32(int32_t v) { return (float)v; }

/* Host stub for @gpu kernels (device codegen replaces this). */
static inline int32_t gpu_thread_id(void) { return 0; }

int64_t count_ways_i64(int64_t N);
int32_t main(void);



int64_t count_ways_i64(int64_t N) {
    if (FLOW_CHECKED_MOD((N), (2)) == 0) {
        return 0;
    }
    int64_t M = FLOW_CHECKED_DIV(((N + 3)), (2));
    if (FLOW_CHECKED_MOD((M), (3)) == 0) {
        return 0;
    }
    int64_t* primes = (int64_t*)(calloc(64, 8));
    if (primes == NULL) {
        return 0;
    }
    int32_t pc = 0;
    int64_t n = M;
    if (FLOW_CHECKED_MOD((n), (2)) == 0) {
        primes[pc] = 2;
        pc = (pc + 1);
        while (FLOW_CHECKED_MOD((n), (2)) == 0) {
            n = FLOW_CHECKED_DIV((n), (2));
        }
    }
    while (FLOW_CHECKED_MOD((n), (3)) == 0) {
        if ((pc == 0 || primes[(pc - 1)] != 3)) {
            primes[pc] = 3;
            pc = (pc + 1);
        }
        n = FLOW_CHECKED_DIV((n), (3));
    }
    int64_t f = 5;
    int64_t step = 2;
    while ((f * f) <= n) {
        if (FLOW_CHECKED_MOD((n), (f)) == 0) {
            primes[pc] = f;
            pc = (pc + 1);
            while (FLOW_CHECKED_MOD((n), (f)) == 0) {
                n = FLOW_CHECKED_DIV((n), (f));
            }
        }
        f = (f + step);
        step = (6 - step);
    }
    if (n > 1) {
        primes[pc] = n;
        pc = (pc + 1);
    }
    int64_t phi_M = M;
    int32_t i = 0;
    while (i < pc) {
        int64_t p = primes[i];
        phi_M = (FLOW_CHECKED_DIV((phi_M), (p)) * (p - 1));
        i = (i + 1);
    }
    bool has_p1 = 0;
    i = 0;
    while (i < pc) {
        if (FLOW_CHECKED_MOD((primes[i]), (3)) == 1) {
            has_p1 = 1;
        }
        i = (i + 1);
    }
    if ((!(has_p1))) {
        int64_t G = (0 - 1);
        if (FLOW_CHECKED_MOD((M), (3)) == 1) {
            G = 1;
        }
        int64_t tw = 1;
        int32_t j = 0;
        while (j < pc) {
            tw = (tw * 2);
            j = (j + 1);
        }
        phi_M = (phi_M - (G * tw));
    }
    free(primes);
    return FLOW_CHECKED_DIV((phi_M), (3));
}

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