Problem 985

Telescoping triangles: minimum perimeter integer-sided triangle with 20 steps. At each step, angles transform as A' = pi - 2B, B' = pi - 2C, C' = pi - 2A. Count steps until an angle drops below epsilon.

Answer1734334
Output1734334
StatusPASS
Native helperno
Runtime60 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n^2)
Space complexityO(1)O(n^2)
ApproachFlow solutionBottom-up DP
VerdictOptimal

Flow source

# Project Euler 985
# Telescoping triangles: minimum perimeter integer-sided triangle with 20 steps.
# At each step, angles transform as A' = pi - 2B, B' = pi - 2C, C' = pi - 2A.
# Count steps until an angle drops below epsilon.

extern {
    function acos(x: f64) -> f64
}

const EPS: f64 = 1e-12

# Compute the number of telescoping steps for a triangle with sides a, b, c.
function num_steps(a: f64, b: f64, c: f64, max_steps: i32, pi: f64) -> i32 {
    # Compute angles using law of cosines
    let mut cosA: f64 = (b * b + c * c - a * a) / (2.0 * b * c)
    let mut cosB: f64 = (a * a + c * c - b * b) / (2.0 * a * c)
    let mut cosC: f64 = (a * a + b * b - c * c) / (2.0 * a * b)
    if cosA < -1.0 { cosA = -1.0 }
    if cosA > 1.0 { cosA = 1.0 }
    if cosB < -1.0 { cosB = -1.0 }
    if cosB > 1.0 { cosB = 1.0 }
    if cosC < -1.0 { cosC = -1.0 }
    if cosC > 1.0 { cosC = 1.0 }
    let mut A: f64 = acos(cosA)
    let mut B: f64 = acos(cosB)
    let mut C: f64 = acos(cosC)

    let mut steps: i32 = 0
    let mut i: i32 = 0
    while i < max_steps {
        let nA: f64 = pi - 2.0 * B
        let nB: f64 = pi - 2.0 * C
        let nC: f64 = pi - 2.0 * A
        A = nA
        B = nB
        C = nC
        if A <= EPS || B <= EPS || C <= EPS { break }
        steps = steps + 1
        i = i + 1
    }
    return steps
}

function main() -> i32 {
    let PI: f64 = 3.14159265358979323846
    let target_steps: i32 = 20
    let mut best_perimeter: i64 = 0
    let mut found: i32 = 0

    let mut n: i32 = 2
    while n <= 5000000 {
        # Candidate 1: (n, n, n+1)
        let a1: f64 = (n as f64)
        let b1: f64 = (n as f64)
        let c1: f64 = ((n + 1) as f64)
        let s1: i32 = num_steps(a1, b1, c1, target_steps + 2, PI)
        if s1 == target_steps {
            let p: i64 = (n + n + n + 1) as i64
            if found == 0 || p < best_perimeter {
                best_perimeter = p
                found = 1
            }
        }

        # Candidate 2: (n, n+1, n+1)
        let a2: f64 = (n as f64)
        let b2: f64 = ((n + 1) as f64)
        let c2: f64 = ((n + 1) as f64)
        let s2: i32 = num_steps(a2, b2, c2, target_steps + 2, PI)
        if s2 == target_steps {
            let p2: i64 = (n + n + 1 + n + 1) as i64
            if found == 0 || p2 < best_perimeter {
                best_perimeter = p2
                found = 1
            }
        }

        if found != 0 {
            let break_val: i64 = (3 * ((n + 1) as i64) + 1)
            if break_val > best_perimeter {
                break
            }
        }
        n = n + 1
    }

    printf("%lld\n", best_perimeter)
    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; }

double acos(double x);
int32_t num_steps_f64_f64_f64_i32_f64(double a, double b, double c, int32_t max_steps, double pi);
int32_t main(void);

static const double EPS = 1e-12;


int32_t num_steps_f64_f64_f64_i32_f64(double a, double b, double c, int32_t max_steps, double pi) {
    double cosA = ((((b * b) + (c * c)) - (a * a)) / ((2.0 * b) * c));
    double cosB = ((((a * a) + (c * c)) - (b * b)) / ((2.0 * a) * c));
    double cosC = ((((a * a) + (b * b)) - (c * c)) / ((2.0 * a) * b));
    if (cosA < (-1.0)) {
        cosA = (-1.0);
    }
    if (cosA > 1.0) {
        cosA = 1.0;
    }
    if (cosB < (-1.0)) {
        cosB = (-1.0);
    }
    if (cosB > 1.0) {
        cosB = 1.0;
    }
    if (cosC < (-1.0)) {
        cosC = (-1.0);
    }
    if (cosC > 1.0) {
        cosC = 1.0;
    }
    double A = acos(cosA);
    double B = acos(cosB);
    double C = acos(cosC);
    int32_t steps = 0;
    int32_t i = 0;
    while (i < max_steps) {
        double nA = (pi - (2.0 * B));
        double nB = (pi - (2.0 * C));
        double nC = (pi - (2.0 * A));
        A = nA;
        B = nB;
        C = nC;
        if (((A <= EPS || B <= EPS) || C <= EPS)) {
            break;
        }
        steps = (steps + 1);
        i = (i + 1);
    }
    return steps;
}

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