Problem 532
Nanobots on Geodesics — minimal n with arc length > 1000.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n) | ? |
| Space complexity | O(n) | ? |
| Approach | Flow solution | Not curated |
| Verdict | Unknown |
Flow source
# Project Euler 532
# Nanobots on Geodesics — minimal n with arc length > 1000.
extern {
function sin(x: f64) -> f64
function cos(x: f64) -> f64
function sqrt(x: f64) -> f64
function log(x: f64) -> f64
function exp(x: f64) -> f64
function fabs(x: f64) -> f64
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const PI: f64 = 3.14159265358979323846
const RADIUS: f64 = 0.999
function tanh_f(y: f64) -> f64 {
let e2: f64 = exp(2.0 * y)
return (e2 - 1.0) / (e2 + 1.0)
}
function simpson(a0: f64, b0: f64, fa: f64, fm: f64, fb: f64) -> f64 {
return (b0 - a0) * (fa + 4.0 * fm + fb) / 6.0
}
function integrand(y: f64, u: f64) -> f64 {
let t: f64 = tanh_f(y)
return sqrt(1.0 - u * t * t)
}
function adaptive_simpson(a: f64, b: f64, u: f64, eps: f64) -> f64 {
let stack: ptr<f64> = calloc(7 * 10000, 8)
if stack == null { return 0.0 }
let fa: f64 = integrand(a, u)
let fb: f64 = integrand(b, u)
let m: f64 = (a + b) / 2.0
let fm: f64 = integrand(m, u)
let whole: f64 = simpson(a, b, fa, fm, fb)
let mut sp: i64 = 0
stack[0] = a; stack[1] = b; stack[2] = fa; stack[3] = fm
stack[4] = fb; stack[5] = whole; stack[6] = eps
sp = 1
let mut total: f64 = 0.0
while sp > 0 {
sp = sp - 1
let base: i64 = sp * 7
let a1: f64 = stack[base]
let b1: f64 = stack[base + 1]
let fa1: f64 = stack[base + 2]
let fm1: f64 = stack[base + 3]
let fb1: f64 = stack[base + 4]
let s1: f64 = stack[base + 5]
let eps1: f64 = stack[base + 6]
let m1: f64 = (a1 + b1) / 2.0
let lm: f64 = (a1 + m1) / 2.0
let rm: f64 = (m1 + b1) / 2.0
let flm: f64 = integrand(lm, u)
let frm: f64 = integrand(rm, u)
let left: f64 = simpson(a1, m1, fa1, flm, fm1)
let right: f64 = simpson(m1, b1, fm1, frm, fb1)
if fabs((left + right) - s1) <= 15.0 * eps1 {
total = total + left + right + ((left + right) - s1) / 15.0
} else {
let e2: f64 = eps1 / 2.0
let b2: i64 = sp * 7
stack[b2] = m1; stack[b2 + 1] = b1; stack[b2 + 2] = fm1
stack[b2 + 3] = frm; stack[b2 + 4] = fb1; stack[b2 + 5] = right; stack[b2 + 6] = e2
sp = sp + 1
let b3: i64 = sp * 7
stack[b3] = a1; stack[b3 + 1] = m1; stack[b3 + 2] = fa1
stack[b3 + 3] = flm; stack[b3 + 4] = fm1; stack[b3 + 5] = left; stack[b3 + 6] = e2
sp = sp + 1
}
}
free(stack)
return total
}
function length_per_bot(n: i64) -> f64 {
let alpha: f64 = PI / (n as f64)
let s: f64 = sin(alpha)
let u: f64 = s * s
let y_max: f64 = 0.5 * log((1.0 + RADIUS) / (1.0 - RADIUS))
let i_val: f64 = adaptive_simpson(0.0, y_max, u, 1.0e-12)
return i_val / s
}
function main() -> i32 {
let target: f64 = 1000.0
let mut hi: i64 = 3
while length_per_bot(hi) <= target {
hi = hi * 2
}
let mut lo: i64 = hi / 2
while lo + 1 < hi {
let mid: i64 = (lo + hi) / 2
if length_per_bot(mid) > target {
hi = mid
} else {
lo = mid
}
}
let n: i64 = hi
let total: f64 = (n as f64) * length_per_bot(n)
printf("%.2f\n", total)
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 tanh_f_f64(double y);
double simpson_f64_f64_f64_f64_f64(double a0, double b0, double fa, double fm, double fb);
double integrand_f64_f64(double y, double u);
double adaptive_simpson_f64_f64_f64_f64(double a, double b, double u, double eps);
double length_per_bot_i64(int64_t n);
int32_t main(void);
static const double PI = 3.14159265358979323846;
static const double RADIUS = 0.999;
double tanh_f_f64(double y) {
double e2 = exp((2.0 * y));
return ((e2 - 1.0) / (e2 + 1.0));
}
double simpson_f64_f64_f64_f64_f64(double a0, double b0, double fa, double fm, double fb) {
return (((b0 - a0) * ((fa + (4.0 * fm)) + fb)) / 6.0);
}
double integrand_f64_f64(double y, double u) {
double t = tanh_f_f64(y);
return sqrt((1.0 - ((u * t) * t)));
}
double adaptive_simpson_f64_f64_f64_f64(double a, double b, double u, double eps) {
double* stack = (double*)(calloc((7 * 10000), 8));
if (stack == NULL) {
return 0.0;
}
double fa = integrand_f64_f64(a, u);
double fb = integrand_f64_f64(b, u);
double m = ((a + b) / 2.0);
double fm = integrand_f64_f64(m, u);
double whole = simpson_f64_f64_f64_f64_f64(a, b, fa, fm, fb);
int64_t sp = 0;
stack[0] = a;
stack[1] = b;
stack[2] = fa;
stack[3] = fm;
stack[4] = fb;
stack[5] = whole;
stack[6] = eps;
sp = 1;
double total = 0.0;
while (sp > 0) {
sp = (sp - 1);
int64_t base = (sp * 7);
double a1 = stack[base];
double b1 = stack[(base + 1)];
double fa1 = stack[(base + 2)];
double fm1 = stack[(base + 3)];
double fb1 = stack[(base + 4)];
double s1 = stack[(base + 5)];
double eps1 = stack[(base + 6)];
double m1 = ((a1 + b1) / 2.0);
double lm = ((a1 + m1) / 2.0);
double rm = ((m1 + b1) / 2.0);
double flm = integrand_f64_f64(lm, u);
double frm = integrand_f64_f64(rm, u);
double left = simpson_f64_f64_f64_f64_f64(a1, m1, fa1, flm, fm1);
double right = simpson_f64_f64_f64_f64_f64(m1, b1, fm1, frm, fb1);
if (fabs(((left + right) - s1)) <= (15.0 * eps1)) {
total = (((total + left) + right) + (((left + right) - s1) / 15.0));
} else {
double e2 = (eps1 / 2.0);
int64_t b2 = (sp * 7);
stack[b2] = m1;
stack[(b2 + 1)] = b1;
stack[(b2 + 2)] = fm1;
stack[(b2 + 3)] = frm;
stack[(b2 + 4)] = fb1;
stack[(b2 + 5)] = right;
stack[(b2 + 6)] = e2;
sp = (sp + 1);
int64_t b3 = (sp * 7);
stack[b3] = a1;
stack[(b3 + 1)] = m1;
stack[(b3 + 2)] = fa1;
stack[(b3 + 3)] = flm;
stack[(b3 + 4)] = fm1;
stack[(b3 + 5)] = left;
stack[(b3 + 6)] = e2;
sp = (sp + 1);
}
}
free(stack);
return total;
}
double length_per_bot_i64(int64_t n) {
double alpha = (PI / ((double)(n)));
double s = sin(alpha);
double u = (s * s);
double y_max = (0.5 * log(((1.0 + RADIUS) / (1.0 - RADIUS))));
double i_val = adaptive_simpson_f64_f64_f64_f64(0.0, y_max, u, 1.0e-12);
return (i_val / s);
}
int32_t main(void) {
double target = 1000.0;
int64_t hi = 3;
while (length_per_bot_i64(hi) <= target) {
hi = (hi * 2);
}
int64_t lo = FLOW_CHECKED_DIV((hi), (2));
while ((lo + 1) < hi) {
int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
if (length_per_bot_i64(mid) > target) {
hi = mid;
} else {
lo = mid;
}
}
int64_t n = hi;
double total = (((double)(n)) * length_per_bot_i64(n));
printf("%.2f\n", total);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%.2f\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
func.func private @sin(f64) -> f64
func.func private @cos(f64) -> f64
func.func private @sqrt(f64) -> f64
func.func private @log(f64) -> f64
func.func private @exp(f64) -> f64
func.func private @fabs(f64) -> f64
func.func private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
// Constant: PI
llvm.mlir.global internal constant @PI(3.14159265358979323846 : f64) : f64
// Constant: RADIUS
llvm.mlir.global internal constant @RADIUS(0.999 : f64) : f64
func.func @tanh_f(%arg0: f64) -> f64 {
%0 = arith.constant 2.0 : f32
%2 = arith.extf %0 : f32 to f64
%1 = arith.mulf %2, %arg0 : f64
%3 = math.exp %1 : f64
%4 = arith.constant 1.0 : f32
%6 = arith.extf %4 : f32 to f64
%5 = arith.subf %3, %6 : f64
%7 = arith.constant 1.0 : f32
%9 = arith.extf %7 : f32 to f64
%8 = arith.addf %3, %9 : f64
%10 = arith.divf %5, %8 : f64
func.return %10 : f64
}
func.func @simpson(%arg0: f64, %arg1: f64, %arg2: f64, %arg3: f64, %arg4: f64) -> f64 {
%11 = arith.subf %arg1, %arg0 : f64
%12 = arith.constant 4.0 : f32
%14 = arith.extf %12 : f32 to f64
%13 = arith.mulf %14, %arg3 : f64
%15 = arith.addf %arg2, %13 : f64
%16 = arith.addf %15, %arg4 : f64
%17 = arith.mulf %11, %16 : f64
%18 = arith.constant 6.0 : f32
%20 = arith.extf %18 : f32 to f64
%19 = arith.divf %17, %20 : f64
func.return %19 : f64
}
func.func @integrand(%arg0: f64, %arg1: f64) -> f64 {
%21 = func.call @tanh_f(%arg0) : (f64) -> f64
%22 = arith.constant 1.0 : f32
%23 = arith.mulf %arg1, %21 : f64
%24 = arith.mulf %23, %21 : f64
%26 = arith.extf %22 : f32 to f64
%25 = arith.subf %26, %24 : f64
%27 = math.sqrt %25 : f64
func.return %27 : f64
}
func.func @adaptive_simpson(%arg0: f64, %arg1: f64, %arg2: f64, %arg3: f64) -> f64 {
%29 = arith.constant 7 : i32
%30 = arith.constant 10000 : i32
%31 = arith.muli %29, %30 : i32
%32 = arith.constant 8 : i32
%33 = arith.extsi %31 : i32 to i64
%34 = arith.extsi %32 : i32 to i64
%28 = func.call @calloc(%33, %34) : (i64, i64) -> !llvm.ptr
%35 = llvm.mlir.zero : !llvm.ptr
%36 = llvm.icmp "eq" %28, %35 : !llvm.ptr
cf.cond_br %36, ^bb0, ^bb1
^bb0:
%37 = arith.constant 0.0 : f32
%38 = arith.extf %37 : f32 to f64
func.return %38 : f64
^bb1:
cf.br ^bb2
^bb2:
%39 = func.call @integrand(%arg0, %arg2) : (f64, f64) -> f64
%40 = func.call @integrand(%arg1, %arg2) : (f64, f64) -> f64
%41 = arith.addf %arg0, %arg1 : f64
%42 = arith.constant 2.0 : f32
%44 = arith.extf %42 : f32 to f64
%43 = arith.divf %41, %44 : f64
%45 = func.call @integrand(%43, %arg2) : (f64, f64) -> f64
%46 = func.call @simpson(%arg0, %arg1, %39, %45, %40) : (f64, f64, f64, f64, f64) -> f64
%47 = arith.constant 0 : i32
%48 = arith.extsi %47 : i32 to i64
%49 = llvm.mlir.constant(1 : i64) : i64
%50 = llvm.alloca %49 x i64 : (i64) -> !llvm.ptr
llvm.store %48, %50 : i64, !llvm.ptr
%51 = arith.constant 0 : i32
%52 = arith.extsi %51 : i32 to i64
%53 = llvm.getelementptr %28[%52] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %arg0, %53 : f64, !llvm.ptr
%54 = arith.constant 1 : i32
%55 = arith.extsi %54 : i32 to i64
%56 = llvm.getelementptr %28[%55] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %arg1, %56 : f64, !llvm.ptr
%57 = arith.constant 2 : i32
%58 = arith.extsi %57 : i32 to i64
%59 = llvm.getelementptr %28[%58] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %39, %59 : f64, !llvm.ptr
%60 = arith.constant 3 : i32
%61 = arith.extsi %60 : i32 to i64
%62 = llvm.getelementptr %28[%61] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %45, %62 : f64, !llvm.ptr
%63 = arith.constant 4 : i32
%64 = arith.extsi %63 : i32 to i64
%65 = llvm.getelementptr %28[%64] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %40, %65 : f64, !llvm.ptr
%66 = arith.constant 5 : i32
%67 = arith.extsi %66 : i32 to i64
%68 = llvm.getelementptr %28[%67] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %46, %68 : f64, !llvm.ptr
%69 = arith.constant 6 : i32
%70 = arith.extsi %69 : i32 to i64
%71 = llvm.getelementptr %28[%70] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %arg3, %71 : f64, !llvm.ptr
%72 = arith.constant 1 : i32
%73 = arith.extsi %72 : i32 to i64
llvm.store %73, %50 : i64, !llvm.ptr
%74 = arith.constant 0.0 : f32
%75 = arith.extf %74 : f32 to f64
%76 = llvm.mlir.constant(1 : i64) : i64
%77 = llvm.alloca %76 x f64 : (i64) -> !llvm.ptr
llvm.store %75, %77 : f64, !llvm.ptr
cf.br ^bb3
^bb3:
%78 = llvm.load %50 : !llvm.ptr -> i64
%79 = arith.constant 0 : i32
%81 = arith.extsi %79 : i32 to i64
%80 = arith.cmpi sgt, %78, %81 : i64
cf.cond_br %80, ^bb4, ^bb5
^bb4:
%82 = llvm.load %50 : !llvm.ptr -> i64
%83 = arith.constant 1 : i32
%85 = arith.extsi %83 : i32 to i64
%84 = arith.subi %82, %85 : i64
llvm.store %84, %50 : i64, !llvm.ptr
%86 = llvm.load %50 : !llvm.ptr -> i64
%87 = arith.constant 7 : i32
%89 = arith.extsi %87 : i32 to i64
%88 = arith.muli %86, %89 : i64
%91 = llvm.getelementptr %28[%88] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%90 = llvm.load %91 : !llvm.ptr -> f64
%93 = arith.constant 1 : i32
%95 = arith.extsi %93 : i32 to i64
%94 = arith.addi %88, %95 : i64
%96 = llvm.getelementptr %28[%94] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%92 = llvm.load %96 : !llvm.ptr -> f64
%98 = arith.constant 2 : i32
%100 = arith.extsi %98 : i32 to i64
%99 = arith.addi %88, %100 : i64
%101 = llvm.getelementptr %28[%99] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%97 = llvm.load %101 : !llvm.ptr -> f64
%103 = arith.constant 3 : i32
%105 = arith.extsi %103 : i32 to i64
%104 = arith.addi %88, %105 : i64
%106 = llvm.getelementptr %28[%104] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%102 = llvm.load %106 : !llvm.ptr -> f64
%108 = arith.constant 4 : i32
%110 = arith.extsi %108 : i32 to i64
%109 = arith.addi %88, %110 : i64
%111 = llvm.getelementptr %28[%109] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%107 = llvm.load %111 : !llvm.ptr -> f64
%113 = arith.constant 5 : i32
%115 = arith.extsi %113 : i32 to i64
%114 = arith.addi %88, %115 : i64
%116 = llvm.getelementptr %28[%114] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%112 = llvm.load %116 : !llvm.ptr -> f64
%118 = arith.constant 6 : i32
%120 = arith.extsi %118 : i32 to i64
%119 = arith.addi %88, %120 : i64
%121 = llvm.getelementptr %28[%119] : (!llvm.ptr, i64) -> !llvm.ptr, f64
%117 = llvm.load %121 : !llvm.ptr -> f64
%122 = arith.addf %90, %92 : f64
%123 = arith.constant 2.0 : f32
%125 = arith.extf %123 : f32 to f64
%124 = arith.divf %122, %125 : f64
%126 = arith.addf %90, %124 : f64
%127 = arith.constant 2.0 : f32
%129 = arith.extf %127 : f32 to f64
%128 = arith.divf %126, %129 : f64
%130 = arith.addf %124, %92 : f64
%131 = arith.constant 2.0 : f32
%133 = arith.extf %131 : f32 to f64
%132 = arith.divf %130, %133 : f64
%134 = func.call @integrand(%128, %arg2) : (f64, f64) -> f64
%135 = func.call @integrand(%132, %arg2) : (f64, f64) -> f64
%136 = func.call @simpson(%90, %124, %97, %134, %102) : (f64, f64, f64, f64, f64) -> f64
%137 = func.call @simpson(%124, %92, %102, %135, %107) : (f64, f64, f64, f64, f64) -> f64
%138 = arith.addf %136, %137 : f64
%139 = arith.subf %138, %112 : f64
%140 = math.absf %139 : f64
%141 = arith.constant 15.0 : f32
%143 = arith.extf %141 : f32 to f64
%142 = arith.mulf %143, %117 : f64
%144 = arith.cmpf ole, %140, %142 : f64
cf.cond_br %144, ^bb6, ^bb7
^bb6:
%145 = llvm.load %77 : !llvm.ptr -> f64
%146 = arith.addf %145, %136 : f64
%147 = arith.addf %146, %137 : f64
%148 = arith.addf %136, %137 : f64
%149 = arith.subf %148, %112 : f64
%150 = arith.constant 15.0 : f32
%152 = arith.extf %150 : f32 to f64
%151 = arith.divf %149, %152 : f64
%153 = arith.addf %147, %151 : f64
llvm.store %153, %77 : f64, !llvm.ptr
cf.br ^bb8
^bb7:
%154 = arith.constant 2.0 : f32
%156 = arith.extf %154 : f32 to f64
%155 = arith.divf %117, %156 : f64
%157 = llvm.load %50 : !llvm.ptr -> i64
%158 = arith.constant 7 : i32
%160 = arith.extsi %158 : i32 to i64
%159 = arith.muli %157, %160 : i64
%161 = llvm.getelementptr %28[%159] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %124, %161 : f64, !llvm.ptr
%162 = arith.constant 1 : i32
%164 = arith.extsi %162 : i32 to i64
%163 = arith.addi %159, %164 : i64
%165 = llvm.getelementptr %28[%163] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %92, %165 : f64, !llvm.ptr
%166 = arith.constant 2 : i32
%168 = arith.extsi %166 : i32 to i64
%167 = arith.addi %159, %168 : i64
%169 = llvm.getelementptr %28[%167] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %102, %169 : f64, !llvm.ptr
%170 = arith.constant 3 : i32
%172 = arith.extsi %170 : i32 to i64
%171 = arith.addi %159, %172 : i64
%173 = llvm.getelementptr %28[%171] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %135, %173 : f64, !llvm.ptr
%174 = arith.constant 4 : i32
%176 = arith.extsi %174 : i32 to i64
%175 = arith.addi %159, %176 : i64
%177 = llvm.getelementptr %28[%175] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %107, %177 : f64, !llvm.ptr
%178 = arith.constant 5 : i32
%180 = arith.extsi %178 : i32 to i64
%179 = arith.addi %159, %180 : i64
%181 = llvm.getelementptr %28[%179] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %137, %181 : f64, !llvm.ptr
%182 = arith.constant 6 : i32
%184 = arith.extsi %182 : i32 to i64
%183 = arith.addi %159, %184 : i64
%185 = llvm.getelementptr %28[%183] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %155, %185 : f64, !llvm.ptr
%186 = llvm.load %50 : !llvm.ptr -> i64
%187 = arith.constant 1 : i32
%189 = arith.extsi %187 : i32 to i64
%188 = arith.addi %186, %189 : i64
llvm.store %188, %50 : i64, !llvm.ptr
%190 = llvm.load %50 : !llvm.ptr -> i64
%191 = arith.constant 7 : i32
%193 = arith.extsi %191 : i32 to i64
%192 = arith.muli %190, %193 : i64
%194 = llvm.getelementptr %28[%192] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %90, %194 : f64, !llvm.ptr
%195 = arith.constant 1 : i32
%197 = arith.extsi %195 : i32 to i64
%196 = arith.addi %192, %197 : i64
%198 = llvm.getelementptr %28[%196] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %124, %198 : f64, !llvm.ptr
%199 = arith.constant 2 : i32
%201 = arith.extsi %199 : i32 to i64
%200 = arith.addi %192, %201 : i64
%202 = llvm.getelementptr %28[%200] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %97, %202 : f64, !llvm.ptr
%203 = arith.constant 3 : i32
%205 = arith.extsi %203 : i32 to i64
%204 = arith.addi %192, %205 : i64
%206 = llvm.getelementptr %28[%204] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %134, %206 : f64, !llvm.ptr
%207 = arith.constant 4 : i32
%209 = arith.extsi %207 : i32 to i64
%208 = arith.addi %192, %209 : i64
%210 = llvm.getelementptr %28[%208] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %102, %210 : f64, !llvm.ptr
%211 = arith.constant 5 : i32
%213 = arith.extsi %211 : i32 to i64
%212 = arith.addi %192, %213 : i64
%214 = llvm.getelementptr %28[%212] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %136, %214 : f64, !llvm.ptr
%215 = arith.constant 6 : i32
%217 = arith.extsi %215 : i32 to i64
%216 = arith.addi %192, %217 : i64
%218 = llvm.getelementptr %28[%216] : (!llvm.ptr, i64) -> !llvm.ptr, f64
llvm.store %155, %218 : f64, !llvm.ptr
%219 = llvm.load %50 : !llvm.ptr -> i64
%220 = arith.constant 1 : i32
%222 = arith.extsi %220 : i32 to i64
%221 = arith.addi %219, %222 : i64
llvm.store %221, %50 : i64, !llvm.ptr
cf.br ^bb8
^bb8:
cf.br ^bb3
^bb5:
func.call @free(%28) : (!llvm.ptr) -> ()
%224 = llvm.load %77 : !llvm.ptr -> f64
func.return %224 : f64
}
func.func @length_per_bot(%arg0: i64) -> f64 {
%225 = llvm.mlir.addressof @PI : !llvm.ptr
%226 = llvm.load %225 : !llvm.ptr -> f64
%227 = arith.sitofp %arg0 : i64 to f64
%228 = arith.divf %226, %227 : f64
%229 = math.sin %228 : f64
%230 = arith.mulf %229, %229 : f64
%231 = arith.constant 0.5 : f32
%232 = arith.constant 1.0 : f32
%233 = llvm.mlir.addressof @RADIUS : !llvm.ptr
%234 = llvm.load %233 : !llvm.ptr -> f64
%236 = arith.extf %232 : f32 to f64
%235 = arith.addf %236, %234 : f64
%237 = arith.constant 1.0 : f32
%238 = llvm.mlir.addressof @RADIUS : !llvm.ptr
%239 = llvm.load %238 : !llvm.ptr -> f64
%241 = arith.extf %237 : f32 to f64
%240 = arith.subf %241, %239 : f64
%242 = arith.divf %235, %240 : f64
%243 = math.log %242 : f64
%245 = arith.extf %231 : f32 to f64
%244 = arith.mulf %245, %243 : f64
%247 = arith.constant 0.0 : f32
%248 = arith.constant 0 : f32
%249 = arith.extf %247 : f32 to f64
%250 = arith.extf %248 : f32 to f64
%246 = func.call @adaptive_simpson(%249, %244, %230, %250) : (f64, f64, f64, f64) -> f64
%251 = arith.divf %246, %229 : f64
func.return %251 : f64
}
func.func @main() -> i32 {
%252 = arith.constant 1000.0 : f32
%253 = arith.extf %252 : f32 to f64
%254 = arith.constant 3 : i32
%255 = arith.extsi %254 : i32 to i64
%256 = llvm.mlir.constant(1 : i64) : i64
%257 = llvm.alloca %256 x i64 : (i64) -> !llvm.ptr
llvm.store %255, %257 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%259 = llvm.load %257 : !llvm.ptr -> i64
%258 = func.call @length_per_bot(%259) : (i64) -> f64
%260 = arith.cmpf ole, %258, %253 : f64
cf.cond_br %260, ^bb10, ^bb11
^bb10:
%261 = llvm.load %257 : !llvm.ptr -> i64
%262 = arith.constant 2 : i32
%264 = arith.extsi %262 : i32 to i64
%263 = arith.muli %261, %264 : i64
llvm.store %263, %257 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%265 = llvm.load %257 : !llvm.ptr -> i64
%266 = arith.constant 2 : i32
%268 = arith.extsi %266 : i32 to i64
%267 = arith.divsi %265, %268 : i64
%269 = llvm.mlir.constant(1 : i64) : i64
%270 = llvm.alloca %269 x i64 : (i64) -> !llvm.ptr
llvm.store %267, %270 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%271 = llvm.load %270 : !llvm.ptr -> i64
%272 = arith.constant 1 : i32
%274 = arith.extsi %272 : i32 to i64
%273 = arith.addi %271, %274 : i64
%275 = llvm.load %257 : !llvm.ptr -> i64
%276 = arith.cmpi slt, %273, %275 : i64
cf.cond_br %276, ^bb13, ^bb14
^bb13:
%277 = llvm.load %270 : !llvm.ptr -> i64
%278 = llvm.load %257 : !llvm.ptr -> i64
%279 = arith.addi %277, %278 : i64
%280 = arith.constant 2 : i32
%282 = arith.extsi %280 : i32 to i64
%281 = arith.divsi %279, %282 : i64
%283 = func.call @length_per_bot(%281) : (i64) -> f64
%284 = arith.cmpf ogt, %283, %253 : f64
cf.cond_br %284, ^bb15, ^bb16
^bb15:
llvm.store %281, %257 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
llvm.store %281, %270 : i64, !llvm.ptr
cf.br ^bb17
^bb17:
cf.br ^bb12
^bb14:
%285 = llvm.load %257 : !llvm.ptr -> i64
%286 = arith.sitofp %285 : i64 to f64
%287 = func.call @length_per_bot(%285) : (i64) -> f64
%288 = arith.mulf %286, %287 : f64
%289 = llvm.mlir.addressof @str_0 : !llvm.ptr
%290 = llvm.call @printf(%289, %288) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
%291 = arith.constant 0 : i32
func.return %291 : i32
}
}