Problem 592

Factorial Trailing Digits II f(N) = odd_part(N!) * 2^{v2(N!) mod 4} mod 2^48 for N=20!, printed as 12 hex digits.

Answer13415DF2BE9C
Output13415DF2BE9C
StatusPASS
Native helperno
Runtime340 ms
Peak memory263296 KB
Time complexityO(n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n)
Space complexityO(n)O(n)
ApproachFlow solutionBig-integer factorial
VerdictOptimal

Flow source

# Project Euler 592
# Factorial Trailing Digits II
# f(N) = odd_part(N!) * 2^{v2(N!) mod 4} mod 2^48 for N=20!, printed as 12 hex digits.

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

const M: i64 = 33554432
const MOD: i64 = 281474976710656
const NVAL: i64 = 2432902008176640000
const HALF: i64 = 16777216

let mut FAC: ptr<i64> = null
let mut TAB: ptr<i64> = null

function mul(A: i64, B: i64) -> i64 {
    let A1: i64 = (A >> 24) & 16777215
    let A2: i64 = A & 16777215
    let B1: i64 = (B >> 24) & 16777215
    let B2: i64 = B & 16777215
    let mid: i64 = ((A1 * B2 + A2 * B1) & 16777215) << 24
    return (mid + A2 * B2) & (MOD - 1)
}

function partial(a: i64, b: i64) -> i64 {
    let ret: i64 = FAC[a] + mul(b, TAB[a])
    return ret % MOD
}

function factorial_oddprod(n0: i64) -> i64 {
    let mut n: i64 = n0 % MOD
    let mut ret: i64 = 1
    let mut b: i64 = 0
    while b < n {
        let mut a: i64 = M
        if n - b < a { a = n - b }
        ret = mul(ret, partial((a - 1) >> 1, b))
        b = b + a
    }
    return ret
}

function print_hex12(v0: i64) -> void {
    let hex: ptr<i8> = calloc(13, 1)
    if hex == null { return }
    let mut v: i64 = v0
    let mut i: i64 = 11
    while i >= 0 {
        let d: i64 = v & 15
        if d < 10 {
            hex[i] = (48 + d) as i8
        } else {
            hex[i] = (55 + d) as i8
        }
        v = v >> 4
        i = i - 1
    }
    hex[12] = 0
    printf("%s\n", hex)
    free(hex)
}

function main() -> i32 {
    FAC = calloc(HALF, 8)
    TAB = calloc(HALF, 8)
    if FAC == null || TAB == null { return 1 }
    FAC[0] = 1
    TAB[0] = 1
    let mut i: i64 = 1
    while i < HALF {
        let p: i64 = 2 * i + 1
        FAC[i] = mul(FAC[i - 1], p)
        TAB[i] = (mul(TAB[i - 1], p) + FAC[i - 1]) % MOD
        i = i + 1
    }

    let mut ans: i64 = 1
    let mut expo: i64 = 0
    let mut n: i64 = NVAL
    while n > 1 {
        n = n >> 1
        expo = expo + n
    }
    expo = expo % 4
    i = 0
    while i < expo {
        ans = ans * 2
        i = i + 1
    }

    n = NVAL
    while n > 0 {
        ans = mul(ans, factorial_oddprod(n))
        n = n >> 1
    }

    print_hex12(ans)
    free(FAC)
    free(TAB)
    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 mul_i64_i64(int64_t A, int64_t B);
int64_t partial_i64_i64(int64_t a, int64_t b);
int64_t factorial_oddprod_i64(int64_t n0);
void print_hex12_i64(int64_t v0);
int32_t main(void);

static const int64_t M = 33554432;
static const int64_t MOD = 281474976710656;
static const int64_t NVAL = 2432902008176640000;
static const int64_t HALF = 16777216;

/* Module statics */
static int64_t* FAC = NULL;
static int64_t* TAB = NULL;



int64_t mul_i64_i64(int64_t A, int64_t B) {
    int64_t A1 = (FLOW_CHECKED_SHR((A), (24)) & 16777215);
    int64_t A2 = (A & 16777215);
    int64_t B1 = (FLOW_CHECKED_SHR((B), (24)) & 16777215);
    int64_t B2 = (B & 16777215);
    int64_t mid = FLOW_CHECKED_SHL(((((A1 * B2) + (A2 * B1)) & 16777215)), (24));
    return ((mid + (A2 * B2)) & (MOD - 1));
}

int64_t partial_i64_i64(int64_t a, int64_t b) {
    int64_t ret = (FAC[a] + mul_i64_i64(b, TAB[a]));
    return FLOW_CHECKED_MOD((ret), (MOD));
}

int64_t factorial_oddprod_i64(int64_t n0) {
    int64_t n = FLOW_CHECKED_MOD((n0), (MOD));
    int64_t ret = 1;
    int64_t b = 0;
    while (b < n) {
        int64_t a = M;
        if ((n - b) < a) {
            a = (n - b);
        }
        ret = mul_i64_i64(ret, partial_i64_i64(FLOW_CHECKED_SHR(((a - 1)), (1)), b));
        b = (b + a);
    }
    return ret;
}

void print_hex12_i64(int64_t v0) {
    int8_t* hex = (int8_t*)(calloc(13, 1));
    if (hex == NULL) {
        return;
    }
    int64_t v = v0;
    int64_t i = 11;
    while (i >= 0) {
        int64_t d = (v & 15);
        if (d < 10) {
            hex[i] = ((int8_t)((48 + d)));
        } else {
            hex[i] = ((int8_t)((55 + d)));
        }
        v = FLOW_CHECKED_SHR((v), (4));
        i = (i - 1);
    }
    hex[12] = 0;
    printf("%s\n", hex);
    free(hex);
}

int32_t main(void) {
    FAC = calloc(HALF, 8);
    TAB = calloc(HALF, 8);
    if ((FAC == NULL || TAB == NULL)) {
        return 1;
    }
    FAC[0] = 1;
    TAB[0] = 1;
    int64_t i = 1;
    while (i < HALF) {
        int64_t p = ((2 * i) + 1);
        FAC[i] = mul_i64_i64(FAC[(i - 1)], p);
        TAB[i] = FLOW_CHECKED_MOD(((mul_i64_i64(TAB[(i - 1)], p) + FAC[(i - 1)])), (MOD));
        i = (i + 1);
    }
    int64_t ans = 1;
    int64_t expo = 0;
    int64_t n = NVAL;
    while (n > 1) {
        n = FLOW_CHECKED_SHR((n), (1));
        expo = (expo + n);
    }
    expo = FLOW_CHECKED_MOD((expo), (4));
    i = 0;
    while (i < expo) {
        ans = (ans * 2);
        i = (i + 1);
    }
    n = NVAL;
    while (n > 0) {
        ans = mul_i64_i64(ans, factorial_oddprod_i64(n));
        n = FLOW_CHECKED_SHR((n), (1));
    }
    print_hex12_i64(ans);
    free(FAC);
    free(TAB);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%s\n\00") {addr_space = 0 : i32} : !llvm.array<4 x i8>
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  // Constant: M
  llvm.mlir.global internal constant @M(33554432 : i64) : i64
  // Constant: MOD
  llvm.mlir.global internal constant @MOD(281474976710656 : i64) : i64
  // Constant: NVAL
  llvm.mlir.global internal constant @NVAL(2432902008176640000 : i64) : i64
  // Constant: HALF
  llvm.mlir.global internal constant @HALF(16777216 : i64) : i64
  // Module static: FAC
  llvm.mlir.global internal @FAC() {addr_space = 0 : i32} : !llvm.ptr {
    %0 = llvm.mlir.zero : !llvm.ptr
    llvm.return %0 : !llvm.ptr
  }
  // Module static: TAB
  llvm.mlir.global internal @TAB() {addr_space = 0 : i32} : !llvm.ptr {
    %1 = llvm.mlir.zero : !llvm.ptr
    llvm.return %1 : !llvm.ptr
  }
  func.func @mul(%arg0: i64, %arg1: i64) -> i64 {
    %2 = arith.constant 24 : i32
    %4 = arith.extsi %2 : i32 to i64
    %3 = arith.shrsi %arg0, %4 : i64
    %5 = arith.constant 16777215 : i32
    %7 = arith.extsi %5 : i32 to i64
    %6 = arith.andi %3, %7 : i64
    %8 = arith.constant 16777215 : i32
    %10 = arith.extsi %8 : i32 to i64
    %9 = arith.andi %arg0, %10 : i64
    %11 = arith.constant 24 : i32
    %13 = arith.extsi %11 : i32 to i64
    %12 = arith.shrsi %arg1, %13 : i64
    %14 = arith.constant 16777215 : i32
    %16 = arith.extsi %14 : i32 to i64
    %15 = arith.andi %12, %16 : i64
    %17 = arith.constant 16777215 : i32
    %19 = arith.extsi %17 : i32 to i64
    %18 = arith.andi %arg1, %19 : i64
    %20 = arith.muli %6, %18 : i64
    %21 = arith.muli %9, %15 : i64
    %22 = arith.addi %20, %21 : i64
    %23 = arith.constant 16777215 : i32
    %25 = arith.extsi %23 : i32 to i64
    %24 = arith.andi %22, %25 : i64
    %26 = arith.constant 24 : i32
    %28 = arith.extsi %26 : i32 to i64
    %27 = arith.shli %24, %28 : i64
    %29 = arith.muli %9, %18 : i64
    %30 = arith.addi %27, %29 : i64
    %31 = llvm.mlir.addressof @MOD : !llvm.ptr
    %32 = llvm.load %31 : !llvm.ptr -> i64
    %33 = arith.constant 1 : i32
    %35 = arith.extsi %33 : i32 to i64
    %34 = arith.subi %32, %35 : i64
    %36 = arith.andi %30, %34 : i64
    func.return %36 : i64
  }
  func.func @partial(%arg0: i64, %arg1: i64) -> i64 {
    %38 = llvm.mlir.addressof @FAC : !llvm.ptr
    %39 = llvm.load %38 : !llvm.ptr -> !llvm.ptr
    %40 = llvm.getelementptr %39[%arg0] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %37 = llvm.load %40 : !llvm.ptr -> i64
    %43 = llvm.mlir.addressof @TAB : !llvm.ptr
    %44 = llvm.load %43 : !llvm.ptr -> !llvm.ptr
    %45 = llvm.getelementptr %44[%arg0] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %42 = llvm.load %45 : !llvm.ptr -> i64
    %41 = func.call @mul(%arg1, %42) : (i64, i64) -> i64
    %46 = arith.addi %37, %41 : i64
    %47 = llvm.mlir.addressof @MOD : !llvm.ptr
    %48 = llvm.load %47 : !llvm.ptr -> i64
    %49 = arith.remsi %46, %48 : i64
    func.return %49 : i64
  }
  func.func @factorial_oddprod(%arg0: i64) -> i64 {
    %50 = llvm.mlir.addressof @MOD : !llvm.ptr
    %51 = llvm.load %50 : !llvm.ptr -> i64
    %52 = arith.remsi %arg0, %51 : i64
    %53 = llvm.mlir.constant(1 : i64) : i64
    %54 = llvm.alloca %53 x i64 : (i64) -> !llvm.ptr
    llvm.store %52, %54 : i64, !llvm.ptr
    %55 = arith.constant 1 : i32
    %56 = arith.extsi %55 : i32 to i64
    %57 = llvm.mlir.constant(1 : i64) : i64
    %58 = llvm.alloca %57 x i64 : (i64) -> !llvm.ptr
    llvm.store %56, %58 : i64, !llvm.ptr
    %59 = arith.constant 0 : i32
    %60 = arith.extsi %59 : i32 to i64
    %61 = llvm.mlir.constant(1 : i64) : i64
    %62 = llvm.alloca %61 x i64 : (i64) -> !llvm.ptr
    llvm.store %60, %62 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %63 = llvm.load %62 : !llvm.ptr -> i64
    %64 = llvm.load %54 : !llvm.ptr -> i64
    %65 = arith.cmpi slt, %63, %64 : i64
    cf.cond_br %65, ^bb1, ^bb2
    ^bb1:
      %66 = llvm.mlir.addressof @M : !llvm.ptr
      %67 = llvm.load %66 : !llvm.ptr -> i64
      %68 = llvm.mlir.constant(1 : i64) : i64
      %69 = llvm.alloca %68 x i64 : (i64) -> !llvm.ptr
      llvm.store %67, %69 : i64, !llvm.ptr
      %70 = llvm.load %54 : !llvm.ptr -> i64
      %71 = llvm.load %62 : !llvm.ptr -> i64
      %72 = arith.subi %70, %71 : i64
      %73 = llvm.load %69 : !llvm.ptr -> i64
      %74 = arith.cmpi slt, %72, %73 : i64
      cf.cond_br %74, ^bb3, ^bb4
      ^bb3:
        %75 = llvm.load %54 : !llvm.ptr -> i64
        %76 = llvm.load %62 : !llvm.ptr -> i64
        %77 = arith.subi %75, %76 : i64
        llvm.store %77, %69 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %79 = llvm.load %58 : !llvm.ptr -> i64
      %81 = llvm.load %69 : !llvm.ptr -> i64
      %82 = arith.constant 1 : i32
      %84 = arith.extsi %82 : i32 to i64
      %83 = arith.subi %81, %84 : i64
      %85 = arith.constant 1 : i32
      %87 = arith.extsi %85 : i32 to i64
      %86 = arith.shrsi %83, %87 : i64
      %88 = llvm.load %62 : !llvm.ptr -> i64
      %80 = func.call @partial(%86, %88) : (i64, i64) -> i64
      %78 = func.call @mul(%79, %80) : (i64, i64) -> i64
      llvm.store %78, %58 : i64, !llvm.ptr
      %89 = llvm.load %62 : !llvm.ptr -> i64
      %90 = llvm.load %69 : !llvm.ptr -> i64
      %91 = arith.addi %89, %90 : i64
      llvm.store %91, %62 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %92 = llvm.load %58 : !llvm.ptr -> i64
    func.return %92 : i64
  }
  func.func @print_hex12(%arg0: i64) -> () {
    %94 = arith.constant 13 : i32
    %95 = arith.constant 1 : i32
    %96 = arith.extsi %94 : i32 to i64
    %97 = arith.extsi %95 : i32 to i64
    %93 = func.call @calloc(%96, %97) : (i64, i64) -> !llvm.ptr
    %98 = llvm.mlir.zero : !llvm.ptr
    %99 = llvm.icmp "eq" %93, %98 : !llvm.ptr
    cf.cond_br %99, ^bb6, ^bb7
    ^bb6:
      func.return
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %100 = llvm.mlir.constant(1 : i64) : i64
    %101 = llvm.alloca %100 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %101 : i64, !llvm.ptr
    %102 = arith.constant 11 : i32
    %103 = arith.extsi %102 : i32 to i64
    %104 = llvm.mlir.constant(1 : i64) : i64
    %105 = llvm.alloca %104 x i64 : (i64) -> !llvm.ptr
    llvm.store %103, %105 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %106 = llvm.load %105 : !llvm.ptr -> i64
    %107 = arith.constant 0 : i32
    %109 = arith.extsi %107 : i32 to i64
    %108 = arith.cmpi sge, %106, %109 : i64
    cf.cond_br %108, ^bb10, ^bb11
    ^bb10:
      %110 = llvm.load %101 : !llvm.ptr -> i64
      %111 = arith.constant 15 : i32
      %113 = arith.extsi %111 : i32 to i64
      %112 = arith.andi %110, %113 : i64
      %114 = arith.constant 10 : i32
      %116 = arith.extsi %114 : i32 to i64
      %115 = arith.cmpi slt, %112, %116 : i64
      cf.cond_br %115, ^bb12, ^bb13
      ^bb12:
        %117 = arith.constant 48 : i32
        %119 = arith.extsi %117 : i32 to i64
        %118 = arith.addi %119, %112 : i64
        %120 = arith.trunci %118 : i64 to i8
        %121 = llvm.load %105 : !llvm.ptr -> i64
        %122 = llvm.getelementptr %93[%121] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        llvm.store %120, %122 : i8, !llvm.ptr
        cf.br ^bb14
      ^bb13:
        %123 = arith.constant 55 : i32
        %125 = arith.extsi %123 : i32 to i64
        %124 = arith.addi %125, %112 : i64
        %126 = arith.trunci %124 : i64 to i8
        %127 = llvm.load %105 : !llvm.ptr -> i64
        %128 = llvm.getelementptr %93[%127] : (!llvm.ptr, i64) -> !llvm.ptr, i8
        llvm.store %126, %128 : i8, !llvm.ptr
        cf.br ^bb14
      ^bb14:
      %129 = llvm.load %101 : !llvm.ptr -> i64
      %130 = arith.constant 4 : i32
      %132 = arith.extsi %130 : i32 to i64
      %131 = arith.shrsi %129, %132 : i64
      llvm.store %131, %101 : i64, !llvm.ptr
      %133 = llvm.load %105 : !llvm.ptr -> i64
      %134 = arith.constant 1 : i32
      %136 = arith.extsi %134 : i32 to i64
      %135 = arith.subi %133, %136 : i64
      llvm.store %135, %105 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %137 = arith.constant 0 : i32
    %138 = arith.constant 12 : i32
    %139 = arith.trunci %137 : i32 to i8
    %140 = arith.extsi %138 : i32 to i64
    %141 = llvm.getelementptr %93[%140] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %139, %141 : i8, !llvm.ptr
    %142 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %143 = llvm.call @printf(%142, %93) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, !llvm.ptr) -> i32
    func.call @free(%93) : (!llvm.ptr) -> ()
    func.return
  }
  func.func @main() -> i32 {
    %146 = llvm.mlir.addressof @HALF : !llvm.ptr
    %147 = llvm.load %146 : !llvm.ptr -> i64
    %148 = arith.constant 8 : i32
    %149 = arith.extsi %148 : i32 to i64
    %145 = func.call @calloc(%147, %149) : (i64, i64) -> !llvm.ptr
    %150 = llvm.mlir.addressof @FAC : !llvm.ptr
    llvm.store %145, %150 : !llvm.ptr, !llvm.ptr
    %152 = llvm.mlir.addressof @HALF : !llvm.ptr
    %153 = llvm.load %152 : !llvm.ptr -> i64
    %154 = arith.constant 8 : i32
    %155 = arith.extsi %154 : i32 to i64
    %151 = func.call @calloc(%153, %155) : (i64, i64) -> !llvm.ptr
    %156 = llvm.mlir.addressof @TAB : !llvm.ptr
    llvm.store %151, %156 : !llvm.ptr, !llvm.ptr
    %157 = llvm.mlir.addressof @FAC : !llvm.ptr
    %158 = llvm.load %157 : !llvm.ptr -> !llvm.ptr
    %159 = llvm.mlir.zero : !llvm.ptr
    %160 = llvm.icmp "eq" %158, %159 : !llvm.ptr
    %161 = scf.if %160 -> (i1) {
      %162 = arith.constant true
      scf.yield %162 : i1
    } else {
      %163 = llvm.mlir.addressof @TAB : !llvm.ptr
      %164 = llvm.load %163 : !llvm.ptr -> !llvm.ptr
      %165 = llvm.mlir.zero : !llvm.ptr
      %166 = llvm.icmp "eq" %164, %165 : !llvm.ptr
      scf.yield %166 : i1
    }
    cf.cond_br %161, ^bb15, ^bb16
    ^bb15:
      %167 = arith.constant 1 : i32
      func.return %167 : i32
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %168 = arith.constant 1 : i32
    %169 = llvm.mlir.addressof @FAC : !llvm.ptr
    %170 = llvm.load %169 : !llvm.ptr -> !llvm.ptr
    %171 = arith.constant 0 : i32
    %172 = arith.extsi %168 : i32 to i64
    %173 = arith.extsi %171 : i32 to i64
    %174 = llvm.getelementptr %170[%173] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %172, %174 : i64, !llvm.ptr
    %175 = arith.constant 1 : i32
    %176 = llvm.mlir.addressof @TAB : !llvm.ptr
    %177 = llvm.load %176 : !llvm.ptr -> !llvm.ptr
    %178 = arith.constant 0 : i32
    %179 = arith.extsi %175 : i32 to i64
    %180 = arith.extsi %178 : i32 to i64
    %181 = llvm.getelementptr %177[%180] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %179, %181 : i64, !llvm.ptr
    %182 = arith.constant 1 : i32
    %183 = arith.extsi %182 : i32 to i64
    %184 = llvm.mlir.constant(1 : i64) : i64
    %185 = llvm.alloca %184 x i64 : (i64) -> !llvm.ptr
    llvm.store %183, %185 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %186 = llvm.load %185 : !llvm.ptr -> i64
    %187 = llvm.mlir.addressof @HALF : !llvm.ptr
    %188 = llvm.load %187 : !llvm.ptr -> i64
    %189 = arith.cmpi slt, %186, %188 : i64
    cf.cond_br %189, ^bb19, ^bb20
    ^bb19:
      %190 = arith.constant 2 : i32
      %191 = llvm.load %185 : !llvm.ptr -> i64
      %193 = arith.extsi %190 : i32 to i64
      %192 = arith.muli %193, %191 : i64
      %194 = arith.constant 1 : i32
      %196 = arith.extsi %194 : i32 to i64
      %195 = arith.addi %192, %196 : i64
      %199 = llvm.mlir.addressof @FAC : !llvm.ptr
      %200 = llvm.load %199 : !llvm.ptr -> !llvm.ptr
      %201 = llvm.load %185 : !llvm.ptr -> i64
      %202 = arith.constant 1 : i32
      %204 = arith.extsi %202 : i32 to i64
      %203 = arith.subi %201, %204 : i64
      %205 = llvm.getelementptr %200[%203] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %198 = llvm.load %205 : !llvm.ptr -> i64
      %197 = func.call @mul(%198, %195) : (i64, i64) -> i64
      %206 = llvm.mlir.addressof @FAC : !llvm.ptr
      %207 = llvm.load %206 : !llvm.ptr -> !llvm.ptr
      %208 = llvm.load %185 : !llvm.ptr -> i64
      %209 = llvm.getelementptr %207[%208] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %197, %209 : i64, !llvm.ptr
      %212 = llvm.mlir.addressof @TAB : !llvm.ptr
      %213 = llvm.load %212 : !llvm.ptr -> !llvm.ptr
      %214 = llvm.load %185 : !llvm.ptr -> i64
      %215 = arith.constant 1 : i32
      %217 = arith.extsi %215 : i32 to i64
      %216 = arith.subi %214, %217 : i64
      %218 = llvm.getelementptr %213[%216] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %211 = llvm.load %218 : !llvm.ptr -> i64
      %210 = func.call @mul(%211, %195) : (i64, i64) -> i64
      %220 = llvm.mlir.addressof @FAC : !llvm.ptr
      %221 = llvm.load %220 : !llvm.ptr -> !llvm.ptr
      %222 = llvm.load %185 : !llvm.ptr -> i64
      %223 = arith.constant 1 : i32
      %225 = arith.extsi %223 : i32 to i64
      %224 = arith.subi %222, %225 : i64
      %226 = llvm.getelementptr %221[%224] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %219 = llvm.load %226 : !llvm.ptr -> i64
      %227 = arith.addi %210, %219 : i64
      %228 = llvm.mlir.addressof @MOD : !llvm.ptr
      %229 = llvm.load %228 : !llvm.ptr -> i64
      %230 = arith.remsi %227, %229 : i64
      %231 = llvm.mlir.addressof @TAB : !llvm.ptr
      %232 = llvm.load %231 : !llvm.ptr -> !llvm.ptr
      %233 = llvm.load %185 : !llvm.ptr -> i64
      %234 = llvm.getelementptr %232[%233] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %230, %234 : i64, !llvm.ptr
      %235 = llvm.load %185 : !llvm.ptr -> i64
      %236 = arith.constant 1 : i32
      %238 = arith.extsi %236 : i32 to i64
      %237 = arith.addi %235, %238 : i64
      llvm.store %237, %185 : i64, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %239 = arith.constant 1 : i32
    %240 = arith.extsi %239 : i32 to i64
    %241 = llvm.mlir.constant(1 : i64) : i64
    %242 = llvm.alloca %241 x i64 : (i64) -> !llvm.ptr
    llvm.store %240, %242 : i64, !llvm.ptr
    %243 = arith.constant 0 : i32
    %244 = arith.extsi %243 : i32 to i64
    %245 = llvm.mlir.constant(1 : i64) : i64
    %246 = llvm.alloca %245 x i64 : (i64) -> !llvm.ptr
    llvm.store %244, %246 : i64, !llvm.ptr
    %247 = llvm.mlir.addressof @NVAL : !llvm.ptr
    %248 = llvm.load %247 : !llvm.ptr -> i64
    %249 = llvm.mlir.constant(1 : i64) : i64
    %250 = llvm.alloca %249 x i64 : (i64) -> !llvm.ptr
    llvm.store %248, %250 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %251 = llvm.load %250 : !llvm.ptr -> i64
    %252 = arith.constant 1 : i32
    %254 = arith.extsi %252 : i32 to i64
    %253 = arith.cmpi sgt, %251, %254 : i64
    cf.cond_br %253, ^bb22, ^bb23
    ^bb22:
      %255 = llvm.load %250 : !llvm.ptr -> i64
      %256 = arith.constant 1 : i32
      %258 = arith.extsi %256 : i32 to i64
      %257 = arith.shrsi %255, %258 : i64
      llvm.store %257, %250 : i64, !llvm.ptr
      %259 = llvm.load %246 : !llvm.ptr -> i64
      %260 = llvm.load %250 : !llvm.ptr -> i64
      %261 = arith.addi %259, %260 : i64
      llvm.store %261, %246 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %262 = llvm.load %246 : !llvm.ptr -> i64
    %263 = arith.constant 4 : i32
    %265 = arith.extsi %263 : i32 to i64
    %264 = arith.remsi %262, %265 : i64
    llvm.store %264, %246 : i64, !llvm.ptr
    %266 = arith.constant 0 : i32
    %267 = arith.extsi %266 : i32 to i64
    llvm.store %267, %185 : i64, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %268 = llvm.load %185 : !llvm.ptr -> i64
    %269 = llvm.load %246 : !llvm.ptr -> i64
    %270 = arith.cmpi slt, %268, %269 : i64
    cf.cond_br %270, ^bb25, ^bb26
    ^bb25:
      %271 = llvm.load %242 : !llvm.ptr -> i64
      %272 = arith.constant 2 : i32
      %274 = arith.extsi %272 : i32 to i64
      %273 = arith.muli %271, %274 : i64
      llvm.store %273, %242 : i64, !llvm.ptr
      %275 = llvm.load %185 : !llvm.ptr -> i64
      %276 = arith.constant 1 : i32
      %278 = arith.extsi %276 : i32 to i64
      %277 = arith.addi %275, %278 : i64
      llvm.store %277, %185 : i64, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %279 = llvm.mlir.addressof @NVAL : !llvm.ptr
    %280 = llvm.load %279 : !llvm.ptr -> i64
    llvm.store %280, %250 : i64, !llvm.ptr
    cf.br ^bb27
    ^bb27:
    %281 = llvm.load %250 : !llvm.ptr -> i64
    %282 = arith.constant 0 : i32
    %284 = arith.extsi %282 : i32 to i64
    %283 = arith.cmpi sgt, %281, %284 : i64
    cf.cond_br %283, ^bb28, ^bb29
    ^bb28:
      %286 = llvm.load %242 : !llvm.ptr -> i64
      %288 = llvm.load %250 : !llvm.ptr -> i64
      %287 = func.call @factorial_oddprod(%288) : (i64) -> i64
      %285 = func.call @mul(%286, %287) : (i64, i64) -> i64
      llvm.store %285, %242 : i64, !llvm.ptr
      %289 = llvm.load %250 : !llvm.ptr -> i64
      %290 = arith.constant 1 : i32
      %292 = arith.extsi %290 : i32 to i64
      %291 = arith.shrsi %289, %292 : i64
      llvm.store %291, %250 : i64, !llvm.ptr
      cf.br ^bb27
    ^bb29:
    %294 = llvm.load %242 : !llvm.ptr -> i64
    func.call @print_hex12(%294) : (i64) -> ()
    %296 = llvm.mlir.addressof @FAC : !llvm.ptr
    %297 = llvm.load %296 : !llvm.ptr -> !llvm.ptr
    func.call @free(%297) : (!llvm.ptr) -> ()
    %299 = llvm.mlir.addressof @TAB : !llvm.ptr
    %300 = llvm.load %299 : !llvm.ptr -> !llvm.ptr
    func.call @free(%300) : (!llvm.ptr) -> ()
    %301 = arith.constant 0 : i32
    func.return %301 : i32
  }
}