Problem 793

Median of Products: median of all pairwise products of S_n where S_n is generated by s_{k+1} = s_k^2 mod 50515093, s_0 = 290797. n = 1000003. Binary search on the median value with a two-pointer count.

Answer475808650131120
Output475808650131120
StatusPASS
Native helperno
Runtime180 ms
Peak memory8928 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)?
Space complexityO(n)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 793
# Median of Products: median of all pairwise products of S_n where
# S_n is generated by s_{k+1} = s_k^2 mod 50515093, s_0 = 290797.
# n = 1000003. Binary search on the median value with a two-pointer count.

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

# Iterative quicksort on i64 array.
function quicksort(v: ptr<i64>, n: i64) -> void {
    let stk: ptr<i64> = calloc(256, 8)
    let mut sp: i64 = 0
    stk[0] = 0
    stk[1] = n - 1
    sp = 2
    while sp > 0 {
        let hi: i64 = stk[sp - 1]
        let lo: i64 = stk[sp - 2]
        sp = sp - 2
        if lo >= hi {
            continue
        }
        # partition with pivot at hi
        let pivot: i64 = v[hi]
        let mut store: i64 = lo
        let mut j: i64 = lo
        while j < hi {
            if v[j] <= pivot {
                let tmp: i64 = v[store]
                v[store] = v[j]
                v[j] = tmp
                store = store + 1
            }
            j = j + 1
        }
        let tmp2: i64 = v[store]
        v[store] = v[hi]
        v[hi] = tmp2
        if store - 1 > lo {
            stk[sp] = lo
            stk[sp + 1] = store - 1
            sp = sp + 2
        }
        if store + 1 < hi {
            stk[sp] = store + 1
            stk[sp + 1] = hi
            sp = sp + 2
        }
    }
    free(stk as ptr<void>)
}

function count_at_most(v: ptr<i64>, n: i64, thr: i64) -> i64 {
    let mut count: i64 = 0
    let mut right: i64 = n - 1
    let mut left: i64 = 0
    while left < n {
        while right > left && v[left] * v[right] > thr {
            right = right - 1
        }
        if right <= left {
            break
        }
        count = count + right - left
        left = left + 1
    }
    return count
}

function main() -> i32 {
    let n: i64 = 1000003
    let values: ptr<i64> = calloc(n, 8)
    if values == null {
        return 1
    }
    let mut s0: i64 = 290797
    let mut i: i64 = 0
    while i < n {
        values[i] = s0
        s0 = s0 * s0 % 50515093
        i = i + 1
    }
    quicksort(values, n)
    let lo: i64 = values[0] * values[1]
    let hi: i64 = values[n - 1] * values[n - 2]
    let target: i64 = (n * (n - 1) / 2 + 1) / 2
    let mut lo2: i64 = lo
    let mut hi2: i64 = hi
    while lo2 < hi2 {
        let mid: i64 = lo2 + (hi2 - lo2) / 2
        if count_at_most(values, n, mid) >= target {
            hi2 = mid
        } else {
            lo2 = mid + 1
        }
    }
    free(values as ptr<void>)
    printf("%lld\n", lo2)
    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; }

void quicksort_ptr_i64_i64(int64_t* v, int64_t n);
int64_t count_at_most_ptr_i64_i64_i64(int64_t* v, int64_t n, int64_t thr);
int32_t main(void);



void quicksort_ptr_i64_i64(int64_t* v, int64_t n) {
    int64_t* stk = (int64_t*)(calloc(256, 8));
    int64_t sp = 0;
    stk[0] = 0;
    stk[1] = (n - 1);
    sp = 2;
    while (sp > 0) {
        int64_t hi = stk[(sp - 1)];
        int64_t lo = stk[(sp - 2)];
        sp = (sp - 2);
        if (lo >= hi) {
            continue;
        }
        int64_t pivot = v[hi];
        int64_t store = lo;
        int64_t j = lo;
        while (j < hi) {
            if (v[j] <= pivot) {
                int64_t tmp = v[store];
                v[store] = v[j];
                v[j] = tmp;
                store = (store + 1);
            }
            j = (j + 1);
        }
        int64_t tmp2 = v[store];
        v[store] = v[hi];
        v[hi] = tmp2;
        if ((store - 1) > lo) {
            stk[sp] = lo;
            stk[(sp + 1)] = (store - 1);
            sp = (sp + 2);
        }
        if ((store + 1) < hi) {
            stk[sp] = (store + 1);
            stk[(sp + 1)] = hi;
            sp = (sp + 2);
        }
    }
    free(((void*)(stk)));
}

int64_t count_at_most_ptr_i64_i64_i64(int64_t* v, int64_t n, int64_t thr) {
    int64_t count = 0;
    int64_t right = (n - 1);
    int64_t left = 0;
    while (left < n) {
        while ((right > left && (v[left] * v[right]) > thr)) {
            right = (right - 1);
        }
        if (right <= left) {
            break;
        }
        count = ((count + right) - left);
        left = (left + 1);
    }
    return count;
}

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